Run JavaScript code, right inside Elixir. Just like this:
> JSEngine.load(["/path/to/file.js"]);
{:ok, nil}
> JSEngine.run("function add(a, b) { return a + b; }")
{:ok, nil}
> JSEngine.call("add", [1, 2])
{:ok, 3}
There are a couple other JS-in-Elixir libraries, but either they're just wrappers around IPC that serialize your function calls into files, or else they have weird edge cases and issues compiling.
This version is implemented in Rust on top of Deno Core, so it's modern, reliable, safe, and fast—and lives fully within the BEAM.
- Converts JS values to Elixir terms
- Converts function arguments passed in
call
from Elixir terms to JS values - Automatically unwraps promises
Because Deno Core has so much packed in, lots more features are within easy reach.
- Module loading: Right now,
load()
just executes single self-contained JS files. With module-loading, it's possible to load an ES module thatimport
s dependencies, and have those dependencies loaded automatically. - TypeScript support: Automatically load and resolve TypeScript files—no build step required.
- Multiple environments: Right now, a single JavaScript environment (
v8::Isolate
) is supported, but multiple independent environments could be supported.
If available in Hex, the package can be installed
by adding jsengine
to your list of dependencies in mix.exs
:
def deps do
[
{:jsengine, "~> 0.1.0"}
]
end
Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/jsengine.