From 8b4163f56e43c3da4e542e55e9a44e2da361983c Mon Sep 17 00:00:00 2001 From: Hugo Heuzard Date: Mon, 30 Sep 2024 17:10:40 +0200 Subject: [PATCH] no longer set globals for toplevel initialization --- .../js_of_ocaml_compiler_dynlink.ml | 19 +++--- .../js_of_ocaml_compiler_dynlink.mli | 4 +- compiler/lib-dynlink/stubs.c | 10 ++++ compiler/tests-check-prim/main.output5 | 3 + compiler/tests-check-prim/unix-unix.output5 | 3 + runtime/stdlib.js | 8 ++- runtime/toplevel.js | 60 ++++++++++++------- toplevel/examples/lwt_toplevel/toplevel.ml | 10 ++-- 8 files changed, 80 insertions(+), 37 deletions(-) diff --git a/compiler/lib-dynlink/js_of_ocaml_compiler_dynlink.ml b/compiler/lib-dynlink/js_of_ocaml_compiler_dynlink.ml index d98e70651e..1bc2bc2f5a 100644 --- a/compiler/lib-dynlink/js_of_ocaml_compiler_dynlink.ml +++ b/compiler/lib-dynlink/js_of_ocaml_compiler_dynlink.ml @@ -12,6 +12,14 @@ type bytecode_sections = external get_bytecode_sections : unit -> bytecode_sections = "jsoo_get_bytecode_sections" +external toplevel_init_compile : + (string -> Instruct.debug_event list array -> unit -> J.t) -> unit + = "jsoo_toplevel_init_compile" + +external toplevel_init_reloc : (J.t -> int) -> unit = "jsoo_toplevel_init_reloc" + +let eval_ref = ref (fun (_ : string) -> failwith "toplevel: eval not initialized") + let normalize_bytecode code = match Ocaml_version.compare Ocaml_version.current [ 5; 2 ] < 0 with | true -> code @@ -47,10 +55,7 @@ let () = flush stdout; flush stderr; let js = Buffer.contents b in - let res : string -> unit -> J.t = - Obj.magic (J.get global (J.string "toplevelEval")) - in - res (js : string) + !eval_ref js in let toplevel_eval (x : string) : unit -> J.t = let f : J.t = J.eval_string x in @@ -76,6 +81,6 @@ let () = | Some i -> i | None -> Js_of_ocaml_compiler.Ocaml_compiler.Symtable.reloc_ident name in - J.set global (J.string "toplevelCompile") (Obj.magic toplevel_compile) (*XXX HACK!*); - J.set global (J.string "toplevelEval") (Obj.magic toplevel_eval); - J.set global (J.string "toplevelReloc") (Obj.magic toplevel_reloc) + eval_ref := toplevel_eval; + toplevel_init_compile toplevel_compile; + toplevel_init_reloc toplevel_reloc diff --git a/compiler/lib-dynlink/js_of_ocaml_compiler_dynlink.mli b/compiler/lib-dynlink/js_of_ocaml_compiler_dynlink.mli index 078d90e03a..0a6fe84ab3 100644 --- a/compiler/lib-dynlink/js_of_ocaml_compiler_dynlink.mli +++ b/compiler/lib-dynlink/js_of_ocaml_compiler_dynlink.mli @@ -17,4 +17,6 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *) -(** Deliberately empty *) +(*/*) + +val eval_ref : (string -> unit -> Jsoo_runtime.Js.t) ref diff --git a/compiler/lib-dynlink/stubs.c b/compiler/lib-dynlink/stubs.c index aba1fc6f72..a3dcc2c92b 100644 --- a/compiler/lib-dynlink/stubs.c +++ b/compiler/lib-dynlink/stubs.c @@ -4,3 +4,13 @@ void jsoo_get_bytecode_sections () { fprintf(stderr, "Unimplemented Javascript primitive jsoo_get_bytecode_sections!\n"); exit(1); } + +void jsoo_toplevel_init_compile () { + fprintf(stderr, "Unimplemented Javascript primitive jsoo_toplevel_init_compile!\n"); + exit(1); +} + +void jsoo_toplevel_init_reloc () { + fprintf(stderr, "Unimplemented Javascript primitive jsoo_toplevel_init_reloc!\n"); + exit(1); +} diff --git a/compiler/tests-check-prim/main.output5 b/compiler/tests-check-prim/main.output5 index c2501c6317..470dc712b8 100644 --- a/compiler/tests-check-prim/main.output5 +++ b/compiler/tests-check-prim/main.output5 @@ -150,6 +150,7 @@ caml_build_symbols caml_is_printable caml_maybe_print_stats caml_register_global +jsoo_toplevel_reloc From +str.js: caml_str_initialize @@ -178,6 +179,8 @@ caml_terminfo_backup caml_terminfo_resume caml_terminfo_setup caml_terminfo_standout +jsoo_toplevel_init_compile +jsoo_toplevel_init_reloc From +unix.js: caml_unix_cleanup diff --git a/compiler/tests-check-prim/unix-unix.output5 b/compiler/tests-check-prim/unix-unix.output5 index 799210ad46..3ac8083bb9 100644 --- a/compiler/tests-check-prim/unix-unix.output5 +++ b/compiler/tests-check-prim/unix-unix.output5 @@ -261,6 +261,7 @@ caml_build_symbols caml_is_printable caml_maybe_print_stats caml_register_global +jsoo_toplevel_reloc From +str.js: caml_str_initialize @@ -289,6 +290,8 @@ caml_terminfo_backup caml_terminfo_resume caml_terminfo_setup caml_terminfo_standout +jsoo_toplevel_init_compile +jsoo_toplevel_init_reloc From +unix.js: caml_unix_cleanup diff --git a/runtime/stdlib.js b/runtime/stdlib.js index d9e267df76..dcbc5d1e45 100644 --- a/runtime/stdlib.js +++ b/runtime/stdlib.js @@ -162,14 +162,18 @@ function caml_build_symbols(symb) { return r; } +//Provides: jsoo_toplevel_reloc +var jsoo_toplevel_reloc = undefined; + //Provides: caml_register_global (const, shallow, const) //Requires: caml_global_data, caml_callback, caml_build_symbols //Requires: caml_failwith +//Requires: jsoo_toplevel_reloc function caml_register_global(n, v, name_opt) { if (name_opt) { var name = name_opt; - if (globalThis.toplevelReloc) { - n = caml_callback(globalThis.toplevelReloc, [name]); + if (jsoo_toplevel_reloc) { + n = caml_callback(jsoo_toplevel_reloc, [name]); } else if (caml_global_data.symbols) { if (!caml_global_data.symidx) { caml_global_data.symidx = caml_build_symbols(caml_global_data.symbols); diff --git a/runtime/toplevel.js b/runtime/toplevel.js index 385b00b80f..35974c8953 100644 --- a/runtime/toplevel.js +++ b/runtime/toplevel.js @@ -90,38 +90,54 @@ function caml_dynlink_get_bytecode_sections() { return caml_global_data.sections; } +//Provides: jsoo_toplevel_compile +//Requires: caml_failwith +var jsoo_toplevel_compile = function () { + caml_failwith("Toplevel not initialized (jsoo_toplevel_compile)"); +}; + +//Provides: jsoo_toplevel_init_compile +//Requires: jsoo_toplevel_compile +function jsoo_toplevel_init_compile(f) { + jsoo_toplevel_compile = f; +} + +//Provides: jsoo_toplevel_init_reloc +//Requires: jsoo_toplevel_reloc +function jsoo_toplevel_init_reloc(f) { + jsoo_toplevel_reloc = f; +} + //Provides: caml_reify_bytecode -//Requires: caml_failwith,caml_callback +//Requires: caml_callback //Requires: caml_string_of_array, caml_ba_to_typed_array +//Requires: jsoo_toplevel_compile //Version: >= 5.2 function caml_reify_bytecode(code, debug, _digest) { - if (globalThis.toplevelCompile) { - code = caml_string_of_array(caml_ba_to_typed_array(code)); - return [0, 0, caml_callback(globalThis.toplevelCompile, [code, debug])]; - } else caml_failwith("Toplevel not initialized (toplevelCompile)"); + code = caml_string_of_array(caml_ba_to_typed_array(code)); + return [0, 0, caml_callback(jsoo_toplevel_compile, [code, debug])]; } //Provides: caml_reify_bytecode -//Requires: caml_failwith,caml_callback +//Requires: caml_callback //Requires: caml_string_of_array, caml_uint8_array_of_bytes +//Requires: jsoo_toplevel_compile //Version: < 5.2 function caml_reify_bytecode(code, debug, _digest) { - if (globalThis.toplevelCompile) { - var len = 0; - var all = []; - for (var i = 1; i < code.length; i++) { - var a = caml_uint8_array_of_bytes(code[i]); - all.push(a); - len += a.length; - } - code = new Uint8Array(len); - for (var i = 0, len = 0; i < all.length; i++) { - code.set(all[i], len); - len += all[i].length; - } - code = caml_string_of_array(code); - return [0, 0, caml_callback(globalThis.toplevelCompile, [code, debug])]; - } else caml_failwith("Toplevel not initialized (toplevelCompile)"); + var len = 0; + var all = []; + for (var i = 1; i < code.length; i++) { + var a = caml_uint8_array_of_bytes(code[i]); + all.push(a); + len += a.length; + } + code = new Uint8Array(len); + for (var i = 0, len = 0; i < all.length; i++) { + code.set(all[i], len); + len += all[i].length; + } + code = caml_string_of_array(code); + return [0, 0, caml_callback(jsoo_toplevel_compile, [code, debug])]; } //Provides: caml_static_release_bytecode diff --git a/toplevel/examples/lwt_toplevel/toplevel.ml b/toplevel/examples/lwt_toplevel/toplevel.ml index 0999f2e69d..acf49a4aa7 100644 --- a/toplevel/examples/lwt_toplevel/toplevel.ml +++ b/toplevel/examples/lwt_toplevel/toplevel.ml @@ -276,11 +276,11 @@ let setup_share_button ~output = let setup_js_preview () = let ph = by_id "last-js" in - let runcode : string -> 'a = Js.Unsafe.global##.toplevelEval in - Js.Unsafe.global##.toplevelEval - := fun bc -> - ph##.innerHTML := Js.string bc; - runcode bc + let runcode : string -> 'a = !Js_of_ocaml_compiler_dynlink.eval_ref in + Js_of_ocaml_compiler_dynlink.eval_ref := + fun bc -> + ph##.innerHTML := Js.string bc; + runcode bc let current_position = ref 0