You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Comprehensive README covering:
- What compiles (types, math, arrays, broadcasting)
- Package registry API with examples
- Built-in Plotly mappings
- How to add your own package
- js() escape hatch with value passing
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Use `optimize=false` for functions that build arrays:
47
+
48
+
```julia
49
+
functionmake_data(n::Int, freq::Float64)
50
+
x = Float64[]
51
+
for i in1:n
52
+
push!(x, Float64(i) *0.1)
53
+
end
54
+
y =sin.(x .* freq)
55
+
return (x, y)
56
+
end
57
+
58
+
result = JST.compile(make_data, (Int, Float64); optimize=false)
59
+
```
60
+
61
+
Produces:
62
+
```javascript
63
+
functionmake_data(n, freq) {
64
+
x = [];
65
+
// ... for loop with x.push() ...
66
+
y =x.map(_b=> _b * freq).map(_b=>Math.sin(_b));
67
+
return [x, y];
68
+
}
69
+
```
70
+
71
+
JST auto-detects when `optimize=false` is needed (when the optimized IR contains Julia's internal memory management) and falls back automatically when used via [Therapy.jl](https://github.com/GroupTherapyOrg/Therapy.jl).
72
+
73
+
## Package Compilation Registry
74
+
75
+
JST can compile calls to registered Julia packages into their JavaScript equivalents — instead of compiling the package's internal implementation.
76
+
77
+
### Registering a Package
78
+
79
+
```julia
80
+
import JavaScriptTarget as JST
81
+
82
+
# Register a function: (module, function_name) → JS compiler
83
+
JST.register_package_compilation!(MyPlotLib, :scatter) do ctx, kwargs, pos_args
JST.register_package_compilation!(@__MODULE__, :my_chart) do ctx, kwargs, pos_args
125
+
JST.build_js_object_from_kwargs(kwargs)
126
+
end
127
+
end
128
+
end
129
+
```
130
+
131
+
When JST encounters `MyPackage.my_chart(data=x, options=cfg)` in compiled code, it emits `{"data": x, "options": cfg}` instead of trying to compile the function's Julia implementation.
132
+
133
+
### How It Works
134
+
135
+
1. Julia's `code_typed` lowers keyword calls to `Core.kwcall(NamedTuple{names}(values), func)`
136
+
2. JST extracts the kwarg names from the NamedTuple type parameters
137
+
3. The registered compiler function receives the compiled kwarg values
138
+
4. The compiler emits the JS equivalent (object literals, function calls, etc.)
139
+
140
+
The registry is checked for both keyword calls (`Core.kwcall`) and positional calls. Functions are matched by `(parentmodule(fn), nameof(fn))`.
141
+
142
+
## `js()` Escape Hatch
143
+
144
+
For direct browser API access, `js()` emits raw JavaScript:
145
+
146
+
```julia
147
+
js("document.title = 'Hello'")
148
+
js("console.log('value:', \$1)", my_value) # $1 substituted with compiled expression
149
+
```
150
+
151
+
## Related
152
+
153
+
-[Therapy.jl](https://github.com/GroupTherapyOrg/Therapy.jl) — Signals-based web framework using JST for compilation
154
+
-[Sessions.jl](https://github.com/GroupTherapyOrg/Sessions.jl) — Notebook IDE with JST-powered export
0 commit comments