Module khepri_fun

Anonymous function extraction private API.


Anonymous function extraction private API.

This module is responsible for extracting the code of an anonymous function. The goal is to be able to store the extracted function and execute it later, regardless of the availability of the initial Erlang module which declared it.

This module also provides a way for the caller to indicate forbidden operations or function calls.

This module works on assembly code to perform all checks and prepare the storable copy of a function. It uses beam_disasm:file/1 from the compiler application to extract the assembly code. After the assembly code was extracted and modified, the compiler is used again to compile the code back to an executable module.

If the anonymous function calls other functions, either in the same module or in another one, the code of the called functions is extracted and copied as well. This is to make sure the result is completely standalone.

To avoid any copies of standard Erlang APIs or Khepri itself, it is possible to specify a list of modules which should not be copied. In this case, calls to functions in those modules are left unmodified.

Once the code was extracted and verified, a new module is generated as an "assembly form", ready to be compiled again to an executable module. The generated module has a single run/N function. This function contains the code of the extracted anonymous function.

Because this process works on the assembly code, it means that if the initial module hosting the anonymous function was compiled with Erlang version N, it will probably not compile or run on older versions of Erlang. The reason is that a newer compiler may use instructions which are unknown to older runtimes.

There is a special treatment for anonymous functions evaluated by erl_eval (e.g. in the Erlang shell). "erl_eval functions" are lambdas parsed from text and are evaluated using erl_eval.

This kind of lambdas becomes a local function in the erl_eval module.

Their assembly code isn't available in the erl_eval module. However, the abstract code (i.e. after parsing but before compilation) is available in the env. We compile that abstract code and extract the assembly from that compiled beam.

This module is private. The documentation is still visible because it may help understand some implementation details. However, this module should never be called directly outside of Khepri.

Data Types


beam_instr() = atom() | tuple()


ensure_instruction_is_permitted_fun() = fun((Instruction::beam_instr()) -> ok)

Function which evaluates the given instruction and returns ok if it is permitted, throws an exception otherwise.


Fun = fun
          ({jump, _})    -> ok;
          ({move, _, _}) -> ok;
          ({trim, _, _}) -> ok;
          (Unknown)      -> throw({unknown_instruction, Unknown})


is_standalone_fun_still_needed_fun() = fun((#{calls := #{Call::mfa() => true}, errors := [Error::any()]}) -> IsNeeded::boolean())

Function which evaluates if the extracted function is still relevant in the end. It returns true if it is, false otherwise.

It takes a map with the following members:


options() = #{ensure_instruction_is_permitted => ensure_instruction_is_permitted_fun(), should_process_function => should_process_function_fun(), is_standalone_fun_still_needed => is_standalone_fun_still_needed_fun()}

Options to tune the extraction of an anonymous function.


should_process_function_fun() = fun((Module::module(), Function::atom(), Arity::arity(), FromModule::module()) -> ShouldProcess::boolean())

Function which returns true if a called function should be extracted and followed, false otherwise.

Module, Function and Arity qualify the function being called.

FromModule indicates the module performing the call. This is useful to distinguish local calls (FromModule == Module) from remote calls.


Fun = fun(Module, Name, Arity, FromModule) ->
              Module =:= FromModule orelse
              erlang:function_exported(Module, Name, Arity)


standalone_fun() = #standalone_fun{module = module(), beam = binary(), arity = arity(), literal_funs = [khepri_fun:standalone_fun()], env = list()} | function()

The result of an extraction, as returned by to_standalone_fun/2.

It can be stored, passed between processes and Erlang nodes. To execute the extracted function, simply call exec/2 which works like erlang:apply/2.

Function Index

to_standalone_fun/1Extracts the given anonymous function.
to_standalone_fun/2Extracts the given anonymous function.
exec/2Executes a previously extracted anonymous function.

Function Details


to_standalone_fun(Fun) -> StandaloneFun

Fun: the anonymous function to extract

returns: a standalone function record or the same anonymous function if no extraction was needed.

Extracts the given anonymous function

This is the same as:
khepri_fun:to_standalone_fun(Fun, #{}).


to_standalone_fun(Fun, Options) -> StandaloneFun

Fun: the anonymous function to extract
Options: a map of options

returns: a standalone function record or the same anonymous function if no extraction was needed.

Extracts the given anonymous function


exec(StandaloneFun, Args) -> Ret

StandaloneFun: the extracted function as returned by to_standalone_fun/2.
Args: the list of arguments to pass to the extracted function.

returns: the return value of the extracted function.

Executes a previously extracted anonymous function.

This is the equivalent of erlang:apply/2 but it supports extracted anonymous functions.

The list of Args must match the arity of the anonymous function.

Generated by EDoc