Skip to content

Reduce number of global names #1696

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
requested at compile time (--enable with-js-error) or at startup (OCAMLRUNPARAM=b=1)
* Runtime: allow dynlink of precompiled js with separate compilation (#1676)
* Lib: Modify Typed_array API for compatibility with WebAssembly

* Toplevel: no longer set globals for toplevel initialization

## Bug fixes
* Runtime: fix parsing of unsigned integers (0u2147483648) (#1633, #1666)
Expand Down
4 changes: 2 additions & 2 deletions compiler/bin-js_of_ocaml/build_fs.ml
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ function jsoo_create_file_extern(name,content){
if(globalThis.jsoo_create_file)
globalThis.jsoo_create_file(name,content);
else {
if(!globalThis.caml_fs_tmp) globalThis.caml_fs_tmp = [];
globalThis.caml_fs_tmp.push({name:name,content:content});
if(!globalThis.jsoo_fs_tmp) globalThis.jsoo_fs_tmp = [];
globalThis.jsoo_fs_tmp.push({name:name,content:content});
}
return 0;
}
Expand Down
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.output
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,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 @@ -232,6 +233,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/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.output
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,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 @@ -341,6 +342,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
3 changes: 3 additions & 0 deletions compiler/tests-check-prim/unix-win32.output
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,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 @@ -306,6 +307,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-win32.output5
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,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 @@ -255,6 +256,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_getpwuid
Expand Down
4 changes: 2 additions & 2 deletions compiler/tests-full/fs.expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
if(c.jsoo_create_file)
c.jsoo_create_file(a, b);
else{
if(! c.caml_fs_tmp) c.caml_fs_tmp = [];
c.caml_fs_tmp.push({name: a, content: b});
if(! c.jsoo_fs_tmp) c.jsoo_fs_tmp = [];
c.jsoo_fs_tmp.push({name: a, content: b});
}
return 0;
}
Expand Down
8 changes: 4 additions & 4 deletions runtime/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,23 +300,23 @@ function caml_ba_map_file_bytecode(argv, argn) {
function jsoo_create_file_extern(name, content) {
if (globalThis.jsoo_create_file) globalThis.jsoo_create_file(name, content);
else {
if (!globalThis.caml_fs_tmp) globalThis.caml_fs_tmp = [];
globalThis.caml_fs_tmp.push({ name: name, content: content });
if (!globalThis.jsoo_fs_tmp) globalThis.jsoo_fs_tmp = [];
globalThis.jsoo_fs_tmp.push({ name: name, content: content });
}
return 0;
}

//Provides: caml_fs_init
//Requires: jsoo_create_file
function caml_fs_init() {
var tmp = globalThis.caml_fs_tmp;
var tmp = globalThis.jsoo_fs_tmp;
if (tmp) {
for (var i = 0; i < tmp.length; i++) {
jsoo_create_file(tmp[i].name, tmp[i].content);
}
}
globalThis.jsoo_create_file = jsoo_create_file;
globalThis.caml_fs_tmp = [];
globalThis.jsoo_fs_tmp = [];
return 0;
}

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
64 changes: 42 additions & 22 deletions runtime/toplevel.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,38 +90,58 @@ function caml_dynlink_get_bytecode_sections() {
return caml_global_data.sections;
}

//Provides: jsoo_toplevel_compile
//Requires: caml_failwith
var jsoo_toplevel_compile = undefined;

//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, caml_failwith
//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)");
if (!jsoo_toplevel_compile) {
caml_failwith("Toplevel not initialized (jsoo_toplevel_compile)");
}
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, caml_failwith
//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)");
if (!jsoo_toplevel_compile) {
caml_failwith("Toplevel not initialized (jsoo_toplevel_compile)");
}
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
Loading