Host Interop · Plain Julia Extensions

Host Interop & Overlays

Connect compiled Julia code to any browser or Node.js API (DOM, Canvas2D, WebAudio, WebGPU, or custom JavaScript functions) using typed ccall imports, host_modules, WasmTarget.METHOD_TABLE, and standard wasm:js-string builtins.

Zero-Import Core, Explicit Host Capabilities

Pure numeric Julia code compiles to standalone WasmGC modules with zero imports. When a package needs browser or host services, WasmTarget.jl keeps the boundary explicit and type-checked through three composable building blocks:

MechanismRoleBoundary Cost
host_modulesAllows specific foreign module names (such as "dom" or "canvas2d") to compile directly to WebAssembly import entries.Direct Wasm call
WasmTarget.METHOD_TABLELets packages swap native Julia fallback methods for WasmGC ccall bodies only during WasmTarget.compile.Zero runtime dispatch
to_js_string / from_js_stringConverts between Julia UTF-8 String and JS WasmExternRef using standard wasm:js-string builtins.No linear-memory buffer

Declaring Typed Host Imports

Any @noinline function whose body executes ccall((:op, "mod"), Ret, (Args...), args...) compiles directly to a typed WebAssembly import when "mod" is passed in host_modules.

Julia Import Stubhost_modules = ["env_log"]
using WasmTarget

@noinline function log_value(tag::WasmTarget.WasmExternRef, x::Float64)::Nothing
    ccall((:log_value, "env_log"), Cvoid, (WasmTarget.WasmExternRef, Float64), tag, x)
end

function report(x::Float64)::Float64
    y = sin(x) * exp(-0.25 * x)
    log_value(WasmTarget.to_js_string("result"), y)
    return y
end

bytes = WasmTarget.compile(report, (Float64,); host_modules = ["env_log"])
Browser / Node HostWebAssembly.instantiate
const imports = {
  env_log: {
    log_value(tag, x) {
      console.log(tag, x);
    },
  },
};

const { instance } = await WebAssembly.instantiate(
  bytes,
  imports,
  { builtins: ["js-string"] }
);

instance.exports.report(1.5);

Method Overlays via @wasm_overlay

Packages rarely want browser ccall stubs to run on native Julia (where a canvas2d shared library does not exist). Instead, a package defines a safe native method for SSR, testing, or analysis, and registers a Wasm-only override in WasmTarget.EXT_METHOD_TABLE via WasmTarget.@wasm_overlay.

const _HOST_SINK = Ref{Int64}(0)

# Native Julia execution (unit tests, SSR, signal discovery)
@noinline function draw_line(x1::Float64, y1::Float64, x2::Float64, y2::Float64)::Int64
    _HOST_SINK[] = Int64(0)
    return Int64(0)
end

# Used only during WasmTarget.compile
WasmTarget.@wasm_overlay @noinline function draw_line(
    x1::Float64,
    y1::Float64,
    x2::Float64,
    y2::Float64,
)::Int64
    return ccall(
        (:draw_line, "canvas2d"),
        Int64,
        (Float64, Float64, Float64, Float64),
        x1,
        y1,
        x2,
        y2,
    )
end

Because WasmTarget.jl layers WasmTarget.EXT_METHOD_TABLE and WasmTarget.METHOD_TABLE on top of Base only during compilation, the overlay never changes native Julia behavior and works cleanly from any external package.

Passing Strings with wasm:js-string

Inside WasmGC, Julia String values are stored as WasmArray{WasmI8} UTF-8 byte arrays. To pass strings to or from JavaScript without a linear-memory heap, WasmTarget.jl provides WasmTarget.to_js_string(::String) and WasmTarget.from_js_string(::WasmExternRef), compiled directly to standard WebAssembly wasm:js-string builtins (fromCharCodeArray, length, intoCharCodeArray).

# Julia -> JS string (returns WasmExternRef)
js_label = WasmTarget.to_js_string("x = " * string(val))

# JS -> Julia UTF-8 String (accepts WasmExternRef)
jl_input = WasmTarget.from_js_string(raw_js_str)

Instantiate modules that pass strings with { builtins: ["js-string"] } in browsers and Node.js 26+.

Case Study: Therapy.jl Reactive DOM Bridge

Therapy.jl compiles every @island component into a standalone WasmGC module importing a compact "dom" host module. DOM elements and DocumentFragment slots are indexed once at hydration time by integer hydration key (hk::Int32), avoiding repeated querySelector calls and raw externref globals.

# From Therapy.jl src/Compiler/DOMBridge.jl
@noinline _wasm_dom_set_text_i64(hk::Int32, v::Int64)::Nothing = ccall(
    (:set_text_i64, "dom"),
    Cvoid,
    (Int32, Int64),
    hk,
    v,
)

@noinline _wasm_dom_set_text_str(hk::Int32, s::WasmTarget.WasmExternRef)::Nothing = ccall(
    (:set_text_str, "dom"),
    Cvoid,
    (Int32, WasmTarget.WasmExternRef),
    hk,
    s,
)

# Reactive string update inside a compiled @island effect
_wasm_dom_set_text_str(Int32(hk), WasmTarget.to_js_string(new_val))

Case Study: WasmMakie.jl 72-Op Canvas2D Surface

WasmMakie.jl generates all 72 Canvas2D host operations from a single CANVAS_OPS declaration table. Every operation generates a native Julia stub for testing alongside a WasmTarget.METHOD_TABLE overlay that calls ccall((:op, "canvas2d"), ...), host import metadata (import_specs()), and JavaScript glue (js_glue()).

# From WasmMakie.jl src/ops.jl (generated for all 72 Canvas2D ops)
for op in CANVAS_OPS
    fname = Symbol(:canvas_, op.name)
    # 1. Native host-safe stub (SSR, signal analysis, RecordingCtx tests)
    @eval @noinline function $fname($(argexprs...))::$(op.ret)
        _WASM_HOST_SINK[] = Int64(0)
        return $zero_val
    end
    # 2. WasmGC overlay calling the "canvas2d" host module
    @eval Base.Experimental.@overlay WasmTarget.METHOD_TABLE @noinline function $fname(
        $(argexprs...),
    )::$(op.ret)
        return ccall(($op_sym, "canvas2d"), $(op.ret), $argtypes_tuple, $(argnames...))
    end
end

Dual contexts (RecordingCtx on the Julia host vs. WasmCtx inside compiled WasmGC) let the exact same Makie plotting code emit build-time HTML command streams or run live at 60fps inside a Therapy @island.