File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -51,18 +51,21 @@ function compileAndRun(source) {
5151 diagnostics = result . diagnostics || [ ] ;
5252
5353 // Step 2: Execute the compiled JS and capture output
54- // Prepend runtime overrides so jl_println captures output
54+ // The codegen output includes jl_println (console.log) + $main() call.
55+ // Strip the $main() call, override jl_println to capture, then call $main().
56+ var jsBody = js . replace ( / \$ m a i n \( \) ; ? \s * $ / , '' ) ; // Remove trailing $main() call
5557 var execCode =
5658 'var _jl_output = [];\n' +
57- 'function jl_println() {\n' +
59+ jsBody + '\n' +
60+ 'jl_println = function() {\n' +
5861 ' var a = []; for (var i = 0; i < arguments.length; i++) {\n' +
5962 ' var v = arguments[i]; a.push(v === null ? "nothing" : v === undefined ? "missing" : String(v));\n' +
60- ' }\n _jl_output.push(a.join("") + "\\n");\n}\n' +
61- 'function jl_print() {\n' +
63+ ' }\n _jl_output.push(a.join("") + "\\n");\n}; \n' +
64+ 'jl_print = function () {\n' +
6265 ' var a = []; for (var i = 0; i < arguments.length; i++) {\n' +
6366 ' var v = arguments[i]; a.push(v === null ? "nothing" : v === undefined ? "missing" : String(v));\n' +
64- ' }\n _jl_output.push(a.join(""));\n}\n' +
65- js + ' \n' +
67+ ' }\n _jl_output.push(a.join(""));\n}; \n' +
68+ 'if (typeof $main === "function") { var _r = $main(); if (_r !== null && _r !== undefined) _jl_output.push(String(_r) + "\\n"); } \n' +
6669 '_jl_output.join("")' ;
6770
6871 try {
You can’t perform that action at this time.
0 commit comments