Jskalc

Jskalc

MquickjsEx - Embed JavaScript in Elixir without Node.js

I’ve just released MquickjsEx, an Elixir library for embedding JavaScript execution directly in your BEAM process using MQuickJS. It’s a new project that got a huge attention on hackernews.

Why?

I built this to let LLMs execute JavaScript code securely with minimal overhead. When an LLM generates JS code for data transformation or custom logic, I wanted to run it in a sandbox where I control exactly what functions are available - no filesystem, no network, just the tools I expose.

I will be also experimenting if it would be possible to create a very fast Server Side Renderer for my other library, LiveVue.

What is it?

MquickjsEx wraps MQuickJS (Fabrice Bellard’s minimal JS engine) via NIFs. Similar to what pythonx does for Python, but the API is inspired by tv-labs/lua.

Key points:

  • No external runtime - no Node.js, Bun, or Deno installation
  • In-process - runs inside the BEAM via NIFs, no subprocess spawning
  • Tiny footprint - MQuickJS can run in as little as 10KB RAM (default 64KB)
  • Sandboxed - no filesystem or network access by default
  • Bidirectional - call JS from Elixir and Elixir from JS

Usage

# Create context and evaluate code
{:ok, ctx} = MquickjsEx.new()
{:ok, 3} = MquickjsEx.eval(ctx, "1 + 2")

# Expose Elixir functions to JavaScript
ctx = MquickjsEx.set!(ctx, :fetch_data, fn [table] -> MyApp.Repo.all(table) end)
{result, _ctx} = MquickjsEx.eval!(ctx, "fetch_data('users').length")

# Define reusable API modules
defmodule MathAPI do
  use MquickjsEx.API, scope: "math"
  defjs add(a, b), do: a + b
end

{:ok, ctx} = MquickjsEx.new()
{:ok, ctx} = MquickjsEx.load_api(ctx, MathAPI)
{:ok, 5} = MquickjsEx.eval(ctx, "math.add(2, 3)")

Important Limitations

MQuickJS implements a subset of JavaScript close to ES5 with a stricter mode. You need to know these:

Arrays cannot have holes:

a = []
a[10] = 1;  // TypeError - can only extend at end
[1, , 3]    // SyntaxError

No direct eval:

eval('1 + 2');       // Forbidden
(1, eval)('1 + 2');  // OK (indirect eval)

No value boxing:

new Number(1)  // Not supported

Limited Date support:
Only Date.now() works.

ASCII-only for some string ops:
toLowerCase() / toUpperCase() only handle ASCII.

Re-execution pattern for callbacks:
When JS calls an Elixir function, execution pauses, Elixir runs the callback, then JS re-executes from the start with cached results. This means your JS code should be idempotent - no side effects that accumulate on replay.

Installation

def deps do
  [{:mquickjs_ex, "~> 0.1.0"}]
end

Full docs and more examples: GitHub - Valian/mquickjs_ex: Embedded JS runtime for Elixir based on MQuickJS · GitHub.

I need to disclose, it was created by collaborating closely with Claude Code Opus 4.5, otherwise it would be not possible.

Feedback welcome - this is the first release. Hope you’ll like it!

https://github.com/Valian/mquickjs_ex

Most Liked

garrison

garrison

Do you foresee the limited syntax (i.e. no es6) posing a problem for SSR with LiveVue? The no holes thing is kinda odd as I would think it wouldn’t be too hard to grow the array accordingly, but I’m sure Bellard is wiser than I.

As an aside, it’s interesting that nearly every new library posted here lately has been written with Claude. It would appear the vibes are hitting critical mass.

Jskalc

Jskalc

Do you foresee the limited syntax (i.e. no es6) posing a problem for SSR with LiveVue?

I’m not sure. Will investigate further soon to understand both feature and performance limitations.

That project exists because on Sunday I noticed an interesting library and though “huh, it would be nice to have it in Elixir. Maybe Opus could handle it?”.

And indeed, it handled it fairly well!

Where Next?

Popular in Announcing Top

jakub-zawislak
Hi everyone, I’m coming from the Symfony (PHP) framework. I like Phoenix, but it has a one thing that was build much better in the Symfo...
New
tmbb
I’ve been working on two packages (not on hex.pm yet) to build admin interfaces for phoenix apps: bureaucrat - which contains a bunch ...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 44532 311
New
sasajuric
I’d like to announce a small library called boundaries. This is an experimental project which explores the idea of enforcing boundaries ...
New
ityonemo
Currently just starting out on a new mini-project - getting zig NIFs to run in elixir. https://github.com/ityonemo/zigler The idea here...
New
mindok
What is ContEx? A pure Elixir server-side data plotting/charting library outputting SVG. It has nice barcharts in particular and works g...
New
kip
Image is an image processing library for Elixir. It is based upon the fabulous vix library that provides a libvips wrapper for Elixir. I...
622 19460 194
New

Other popular topics Top

hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44139 214
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement