Skip to content

Commit

Permalink
no longer set globals for toplevel initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
hhugo committed Sep 30, 2024
1 parent 186e5d7 commit b86799c
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 37 deletions.
19 changes: 12 additions & 7 deletions compiler/lib-dynlink/js_of_ocaml_compiler_dynlink.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
4 changes: 3 additions & 1 deletion compiler/lib-dynlink/js_of_ocaml_compiler_dynlink.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 10 additions & 0 deletions compiler/lib-dynlink/stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
3 changes: 3 additions & 0 deletions compiler/tests-check-prim/main.output5
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions compiler/tests-check-prim/unix-unix.output5
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions runtime/stdlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
60 changes: 38 additions & 22 deletions runtime/toplevel.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions toplevel/examples/lwt_toplevel/toplevel.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit b86799c

Please sign in to comment.