Quantcast

erl_eval function object

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

erl_eval function object

Paul Mineiro
The documentation for erl_eval says that the NonlocalFunctionHandler is
called when a functional object (fun) is called; however I'm not seeing
this.

Any insight would be appreciated.

Cheers,

-- p

-----
-module (evaltest).
-compile (export_all).

doit (String) ->
  { ok, Scanned, _ } = erl_scan:string (String),
  { ok, Parsed } = erl_parse:parse_exprs (Scanned),
  Bindings = erl_eval:new_bindings (),
  erl_eval:exprs (Parsed,
                  Bindings,
                  { value, fun local/2 },
                  { value, fun non_local/2 }).

local (Name, Arguments) ->
  io:format ("local ~p ~p~n", [ Name, Arguments ]),
  false.

non_local (FuncSpec, Arguments) ->
  io:format ("non_local ~p ~p~n", [ FuncSpec, Arguments ]),
  case FuncSpec of
    { M, F } -> erlang:apply (M, F, Arguments);
    Func when is_function (Func) -> erlang:apply (Func, Arguments)
  end.
-----

% erl
Erlang (BEAM) emulator version 5.6.5 [source] [smp:2] [async-threads:0] [kernel-poll:false]
Eshell V5.6.5  (abort with ^G)
1> evaltest:doit ("(fun () -> 10 + 2 end) ().").
non_local {erlang,'+'} [10,2]
{value,12,[]}

-----

________________________________________________________________
erlang-questions mailing list. See http://www.erlang.org/faq.html
erlang-questions (at) erlang.org

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: erl_eval function object

Witold Baryluk
Dnia 2009-08-04, wto o godzinie 10:38 -0700, Paul Mineiro pisze:
> The documentation for erl_eval says that the NonlocalFunctionHandler is
> called when a functional object (fun) is called; however I'm not seeing
> this.
...
> 1> evaltest:doit ("(fun () -> 10 + 2 end) ().").
> non_local {erlang,'+'} [10,2]
> {value,12,[]}


But it looks, that it is called here. :)


--
Witold Baryluk <[hidden email]>

signature.asc (204 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: erl_eval function object

Paul Mineiro
That's the call to erlang:'+'/2.  The call I'd like to (also) see is to
the anonymous fun enclosing it.

-- p

On Tue, 4 Aug 2009, Witold Baryluk wrote:

> Dnia 2009-08-04, wto o godzinie 10:38 -0700, Paul Mineiro pisze:
> > The documentation for erl_eval says that the NonlocalFunctionHandler is
> > called when a functional object (fun) is called; however I'm not seeing
> > this.
> ...
> > 1> evaltest:doit ("(fun () -> 10 + 2 end) ().").
> > non_local {erlang,'+'} [10,2]
> > {value,12,[]}
>
>
> But it looks, that it is called here. :)
>
>
> --
> Witold Baryluk <[hidden email]>
>


________________________________________________________________
erlang-questions mailing list. See http://www.erlang.org/faq.html
erlang-questions (at) erlang.org

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: erl_eval function object

Hans Bolinder-2
In reply to this post by Paul Mineiro
[Paul Mineiro:]
> The documentation for erl_eval says that the NonlocalFunctionHandler is
> called when a functional object (fun) is called; however I'm not seeing
> this.

> -----
> -module (evaltest).
> -compile (export_all).
>
> doit (String) ->
>   { ok, Scanned, _ } = erl_scan:string (String),
>   { ok, Parsed } = erl_parse:parse_exprs (Scanned),
>   Bindings = erl_eval:new_bindings (),
>   erl_eval:exprs (Parsed,
>                   Bindings,
>                   { value, fun local/2 },
>                   { value, fun non_local/2 }).
>
> local (Name, Arguments) ->
>   io:format ("local ~p ~p~n", [ Name, Arguments ]),
>   false.
>
> non_local (FuncSpec, Arguments) ->
>   io:format ("non_local ~p ~p~n", [ FuncSpec, Arguments ]),
>   case FuncSpec of
>     { M, F } -> erlang:apply (M, F, Arguments);
>     Func when is_function (Func) -> erlang:apply (Func, Arguments)
>   end.
> -----
>
> % erl
> Erlang (BEAM) emulator version 5.6.5 [source] [smp:2] [async-threads:0] [kernel-poll:false]
> Eshell V5.6.5  (abort with ^G)
> 1> evaltest:doit ("(fun () -> 10 + 2 end) ().").
> non_local {erlang,'+'} [10,2]
> {value,12,[]}

There is a functional object for every fun-end expression, and it is
created in one of the clauses of the erl_eval:expr/5 function. As
indicated by the "ugly hack" comment in that clause, the fun's
existence is an implementation detail; it is possible (but unlikely)
that some day the fun will no longer be needed. When evaluating
expressions erl_eval does a careful job not to call the non-local
function handler function for such "helper" funs.

The non-local function handler will be called with the fun as the
first argument in cases like this:

1> evaltest:doit ("(fun math:sqrt/1) (2).").
non_local #Fun<math.sqrt.1> [2]
{value,1.4142135623730951,[]}

Best regards,

Hans Bolinder, Erlang/OTP team, Ericsson

________________________________________________________________
erlang-questions mailing list. See http://www.erlang.org/faq.html
erlang-questions (at) erlang.org

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: erl_eval function object

Hans Bolinder-2
In reply to this post by Paul Mineiro
[Paul Mineiro:]
> The documentation for erl_eval says that the NonlocalFunctionHandler is
> called when a functional object (fun) is called; however I'm not seeing
> this.

[I replied with some nonsense:]
> There is a functional object for every fun-end expression, and it is
> created in one of the clauses of the erl_eval:expr/5 function. As
> indicated by the "ugly hack" comment in that clause, the fun's
> existence is an implementation detail; it is possible (but unlikely)
> that some day the fun will no longer be needed.

Correction. A fun will always have to be created.

The non-local function handler is primarily a means to implement the
restricted shell mode (see shell(3)). Some harmless calls are skipped,
which makes it somewhat simpler to write non-local function handlers.

erlang:apply--which is skipped when calling non-local function
handlers--is already mentioned in a note. I'll extend that note to
mention shell funs as well.

Best regards,

Hans Bolinder, Erlang/OTP team, Ericsson

________________________________________________________________
erlang-questions mailing list. See http://www.erlang.org/faq.html
erlang-questions (at) erlang.org

Loading...