Skip to content

Commit 023dd9e

Browse files
Dale-Blackclaude
andcommitted
Add js() escape hatch for raw JavaScript emission
When JST encounters a call to a function named "js", it emits the string argument as raw JavaScript code instead of compiling the call. Used by Therapy.jl to call browser APIs (document, localStorage, etc.) from compiled @island handlers and effects. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent f1bc4f4 commit 023dd9e

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

src/compiler/codegen.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -968,6 +968,23 @@ function compile_invoke(ctx::JSCompilationContext, expr::Expr)
968968
return "jl_print($(join(call_args, ", ")))"
969969
end
970970

971+
# JS escape hatch: js("raw code") → emits string content as raw JavaScript
972+
# Used by Therapy.jl to call browser APIs (document, localStorage, etc.)
973+
if func_name == "js"
974+
if length(expr.args) >= 3 && expr.args[3] isa String
975+
return expr.args[3]
976+
end
977+
# Fallback: compiled arg with quotes stripped
978+
if length(call_args) >= 1
979+
s = call_args[1]
980+
if length(s) >= 2 && s[1] == '"' && s[end] == '"'
981+
return replace(replace(s[2:end-1], "\\\"" => "\""), "\\\\" => "\\")
982+
end
983+
return s
984+
end
985+
return "/* js() called with no arguments */"
986+
end
987+
971988
# Math: div, fld, mod, cld, rem
972989
if func_name == "div" && length(call_args) >= 2
973990
require_runtime!(ctx, :jl_div)

0 commit comments

Comments
 (0)