apply

Cross-runtime dynamic function invocation for Erlang and JavaScript.

Resolve functions by string path in the target runtime and call them, returning Result(any, String). Error messages share a unified path/arity + reason style on both targets.

Values

pub fn apply(
  raw_path: String,
  args: args_tuple,
) -> Result(any, String)

Dynamically invoke a runtime function - the main entry point.

  • raw_path: "Module:Function" on Erlang, "object.property" on JavaScript.
  • args: must be a tuple; elements are spread as call arguments in order.

Returns an error when args is not a tuple, and a unified error message when the path is missing, the target is not callable, or the call throws.

apply.apply("erlang:length", #([1, 2, 3])) // Ok(3)
apply.apply("Math.max", #(1, 5))           // Ok(5)
pub fn call_erl(raw: String, arity: Int) -> any

Quick check helper: fetch an Erlang function, crashing on failure.

Prefer apply in real code; when apply is not enough, fetch the function first and define the call behaviour yourself.

pub fn call_js(raw: String) -> any

Quick check helper: fetch a JS object, crashing on failure.

Prefer apply in real code; when apply is not enough, fetch the object first and define the call behaviour yourself.

pub fn get_erl_func(
  raw_path: String,
  erl_arity: Int,
) -> Result(any, String)

Fetch an Erlang function reference (Erlang target only).

Accepts "Module:Function", or a bare function name which is prefixed with erlang:. Returns Ok(fun) on success; always returns a runtime error on the JavaScript target.

let assert Ok(map) = apply.get_erl_func("lists:map", 2)

Errors include the arity, e.g. "erlang:length/2 is not exported in erlang (existing arities: 1)".

pub fn get_js_obj(raw_script: String) -> Result(any, String)

Fetch a JavaScript runtime object or function (JavaScript target only).

Takes a dotted path such as "Math.PI" or "console.log".

let assert Ok(3.141592653589793) = apply.get_js_obj("Math.PI")
let assert Ok(max) = apply.get_js_obj("Math.max")

Always returns a runtime error on the Erlang target.

pub fn is_function(func: function) -> Bool

Whether a value is a function.

pub fn is_tuple(any: tuple) -> Bool

Whether a value is a tuple.

pub fn main() -> Int

Demo entry point.

pub fn platform_name() -> String

The current runtime platform.

Returns "erlang" on the Erlang target and "javascript" on the JavaScript target.

Search Document