From 9034dcffc0523df75350c10dc629e0c8990287bb Mon Sep 17 00:00:00 2001 From: Hugo Heuzard Date: Sat, 18 Dec 2021 14:26:51 +0100 Subject: [PATCH 1/4] Compiler: switch to globalThis --- compiler/bin-js_of_ocaml/build_fs.ml | 2 +- compiler/bin-js_of_ocaml/compile.ml | 2 +- compiler/bin-jsoo_fs/jsoo_fs.ml | 2 +- compiler/lib/driver.ml | 49 ++++++++++++++++++---------- compiler/lib/driver.mli | 2 +- compiler/lib/reserved.ml | 1 + compiler/tests-compiler/empty_cma.ml | 2 +- compiler/tests-compiler/sourcemap.ml | 2 +- 8 files changed, 39 insertions(+), 23 deletions(-) diff --git a/compiler/bin-js_of_ocaml/build_fs.ml b/compiler/bin-js_of_ocaml/build_fs.ml index 453541e8af..82191dee8c 100644 --- a/compiler/bin-js_of_ocaml/build_fs.ml +++ b/compiler/bin-js_of_ocaml/build_fs.ml @@ -71,7 +71,7 @@ function jsoo_create_file_extern(name,content){ let pfs_fmt = Pretty_print.to_out_channel chan in Driver.f ~standalone:true - ~global:`Auto + ~global:`globalThis pfs_fmt (Parse_bytecode.Debug.create ~toplevel:false false) code) diff --git a/compiler/bin-js_of_ocaml/compile.ml b/compiler/bin-js_of_ocaml/compile.ml index a60ad02bcf..c280d3c75a 100644 --- a/compiler/bin-js_of_ocaml/compile.ml +++ b/compiler/bin-js_of_ocaml/compile.ml @@ -57,7 +57,7 @@ let run let global = match wrap_with_fun with | Some fun_name -> `Bind_to fun_name - | None -> `Auto + | None -> `globalThis in Jsoo_cmdline.Arg.eval common; (match output_file with diff --git a/compiler/bin-jsoo_fs/jsoo_fs.ml b/compiler/bin-jsoo_fs/jsoo_fs.ml index 1ca803e6fe..ea04cf85f2 100644 --- a/compiler/bin-jsoo_fs/jsoo_fs.ml +++ b/compiler/bin-jsoo_fs/jsoo_fs.ml @@ -98,7 +98,7 @@ function jsoo_create_file_extern(name,content){ let pfs_fmt = Pretty_print.to_out_channel chan in Driver.f ~standalone:true - ~global:`Auto + ~global:`globalThis pfs_fmt (Parse_bytecode.Debug.create ~toplevel:false false) code) diff --git a/compiler/lib/driver.ml b/compiler/lib/driver.ml index 49673cf880..57dd3ed0c0 100644 --- a/compiler/lib/driver.ml +++ b/compiler/lib/driver.ml @@ -325,7 +325,7 @@ let output formatter ~standalone ~custom_header ?source_map () js = Js_output.program formatter ?source_map js; if times () then Format.eprintf " write: %a@." Timer.print t -let pack ~global { Linker.runtime_code = js; always_required_codes } = +let pack ~global ~standalone { Linker.runtime_code = js; always_required_codes } = let module J = Javascript in let t = Timer.make () in if times () then Format.eprintf "Start Optimizing js...@."; @@ -363,20 +363,7 @@ let pack ~global { Linker.runtime_code = js; always_required_codes } = | `Function -> f | `Bind_to _ -> f | `Custom name -> J.ECall (f, [ J.EVar (J.ident name), `Not_spread ], J.N) - | `Auto -> - let global = - J.ECall - ( J.EFun - ( None - , [] - , [ ( J.Statement (J.Return_statement (Some (J.EVar (J.ident "this")))) - , J.N ) - ] - , J.N ) - , [] - , J.N ) - in - J.ECall (f, [ global, `Not_spread ], J.N) + | `globalThis -> J.ECall (f, [ J.EVar (J.ident "globalThis"), `Not_spread ], J.N) in match global with | `Bind_to name -> @@ -397,6 +384,34 @@ let pack ~global { Linker.runtime_code = js; always_required_codes } = in let runtime_js = wrap_in_iifa ~can_use_strict:true js in let js = List.flatten always_required_js @ runtime_js in + let js = match global, standalone with + | (`Function | `Bind_to _ | `Custom _), _ -> js + | `globalThis, false -> js + | `globalThis, true -> + let s = + {| +(function (Object) { + typeof globalThis !== 'object' && ( + this ? + get() : + (Object.defineProperty(Object.prototype, '_T_', { + configurable: true, + get: get + }), _T_) + ); + function get() { + var global = this || self; + global.globalThis = global; + delete Object.prototype._T_; + } +}(Object)); +|} + in + let lex = Lexing.from_string s in + let lex = Parse_js.Lexer.of_lexbuf lex in + let e = Parse_js.parse lex in + e @ js + in (* post pack optim *) let t3 = Timer.make () in let js = (new Js_traverse.simpl)#program js in @@ -428,7 +443,7 @@ type profile = Code.program -> Code.program let f ?(standalone = true) - ?(global = `Auto) + ?(global = `globalThis) ?(profile = o1) ?(dynlink = false) ?(linkall = false) @@ -450,7 +465,7 @@ let f let emit = generate d ~exported_runtime +> link ~standalone ~linkall ~export_runtime:dynlink - +> pack ~global + +> pack ~global ~standalone +> coloring +> check_js +> output formatter ~standalone ~custom_header ?source_map () diff --git a/compiler/lib/driver.mli b/compiler/lib/driver.mli index 2330669be2..3e24674c18 100644 --- a/compiler/lib/driver.mli +++ b/compiler/lib/driver.mli @@ -22,7 +22,7 @@ type profile val f : ?standalone:bool - -> ?global:[ `Auto | `Function | `Bind_to of string | `Custom of string ] + -> ?global:[ `globalThis | `Function | `Bind_to of string | `Custom of string ] -> ?profile:profile -> ?dynlink:bool -> ?linkall:bool diff --git a/compiler/lib/reserved.ml b/compiler/lib/reserved.ml index ae5a989740..8961502d16 100644 --- a/compiler/lib/reserved.ml +++ b/compiler/lib/reserved.ml @@ -115,6 +115,7 @@ let provided = ; "Math" ; "JSON" ; "Object" + ; "globalThis" ; "RegExp" ; "String" ; "Boolean" diff --git a/compiler/tests-compiler/empty_cma.ml b/compiler/tests-compiler/empty_cma.ml index 45dc62e0c1..fece9667ee 100644 --- a/compiler/tests-compiler/empty_cma.ml +++ b/compiler/tests-compiler/empty_cma.ml @@ -29,7 +29,7 @@ let%expect_test _ = Sys.remove "empty.js"; [%expect {| - (function(joo_global_object){"use strict";return}(function(){return this}())); + (function(joo_global_object){"use strict";return}(globalThis)); //# sourceMappingURL=empty.map |}] diff --git a/compiler/tests-compiler/sourcemap.ml b/compiler/tests-compiler/sourcemap.ml index 28b92b1b91..ddbb519734 100644 --- a/compiler/tests-compiler/sourcemap.ml +++ b/compiler/tests-compiler/sourcemap.ml @@ -66,7 +66,7 @@ let%expect_test _ = 5: var Test=[0,id]; 6: runtime.caml_register_global(0,Test,"Test"); 7: return} - 8: (function(){return this}())); + 8: (globalThis)); 9: 10: //# sourceMappingURL=test.map null:-1:-1 -> 3:4 From 877ca4e79c8b77dc047b75befc42c971e1a76d8e Mon Sep 17 00:00:00 2001 From: Hugo Heuzard Date: Sat, 18 Dec 2021 14:39:16 +0100 Subject: [PATCH 2/4] Compiler: no longer use joo_global_object --- compiler/bin-js_of_ocaml/build_fs.ml | 8 ++++---- compiler/bin-jsoo_fs/jsoo_fs.ml | 8 ++++---- compiler/lib/constant.ml | 2 +- compiler/lib/driver.ml | 2 +- compiler/tests-compiler/empty_cma.ml | 4 ++-- compiler/tests-compiler/sourcemap.ml | 4 ++-- lib/js_of_ocaml/js.ml | 2 +- runtime/bigarray.js | 4 ++-- runtime/bigstring.js | 4 ++-- runtime/dynlink.js | 6 +++--- runtime/fs.js | 22 +++++++++++----------- runtime/fs_node.js | 22 +++++++++++----------- runtime/graphics.js | 4 ++-- runtime/ieee_754.js | 8 ++++---- runtime/jslib.js | 20 ++++++++++---------- runtime/jslib_js_of_ocaml.js | 6 +++--- runtime/marshal.js | 6 +++--- runtime/mlBytes.js | 4 ++-- runtime/nat.js | 2 +- runtime/stdlib.js | 4 ++-- runtime/sys.js | 28 ++++++++++++++-------------- runtime/toplevel.js | 8 ++++---- runtime/unix.js | 4 ++-- 23 files changed, 91 insertions(+), 91 deletions(-) diff --git a/compiler/bin-js_of_ocaml/build_fs.ml b/compiler/bin-js_of_ocaml/build_fs.ml index 82191dee8c..807f92cce3 100644 --- a/compiler/bin-js_of_ocaml/build_fs.ml +++ b/compiler/bin-js_of_ocaml/build_fs.ml @@ -51,11 +51,11 @@ let f { files; output_file; include_dirs } = {| //Provides: jsoo_create_file_extern function jsoo_create_file_extern(name,content){ - if(joo_global_object.jsoo_create_file) - joo_global_object.jsoo_create_file(name,content); + if(globalThis.jsoo_create_file) + globalThis.jsoo_create_file(name,content); else { - if(!joo_global_object.caml_fs_tmp) joo_global_object.caml_fs_tmp = []; - joo_global_object.caml_fs_tmp.push({name:name,content:content}); + if(!globalThis.caml_fs_tmp) globalThis.caml_fs_tmp = []; + globalThis.caml_fs_tmp.push({name:name,content:content}); } return 0; } diff --git a/compiler/bin-jsoo_fs/jsoo_fs.ml b/compiler/bin-jsoo_fs/jsoo_fs.ml index ea04cf85f2..2f9f8eb4d8 100644 --- a/compiler/bin-jsoo_fs/jsoo_fs.ml +++ b/compiler/bin-jsoo_fs/jsoo_fs.ml @@ -78,11 +78,11 @@ let f { files; output_file; include_dirs } = {| //Provides: jsoo_create_file_extern function jsoo_create_file_extern(name,content){ - if(joo_global_object.caml_create_file) - joo_global_object.caml_create_file(name,content); + if(globalThis.caml_create_file) + globalThis.caml_create_file(name,content); else { - if(!joo_global_object.caml_fs_tmp) joo_global_object.caml_fs_tmp = []; - joo_global_object.caml_fs_tmp.push({name:name,content:content}); + if(!globalThis.caml_fs_tmp) globalThis.caml_fs_tmp = []; + globalThis.caml_fs_tmp.push({name:name,content:content}); } return 0; } diff --git a/compiler/lib/constant.ml b/compiler/lib/constant.ml index 2acc8050ff..844a20a2de 100644 --- a/compiler/lib/constant.ml +++ b/compiler/lib/constant.ml @@ -18,4 +18,4 @@ open! Stdlib -let global_object = "joo_global_object" +let global_object = "globalThis" diff --git a/compiler/lib/driver.ml b/compiler/lib/driver.ml index 57dd3ed0c0..b46b412f42 100644 --- a/compiler/lib/driver.ml +++ b/compiler/lib/driver.ml @@ -363,7 +363,7 @@ let pack ~global ~standalone { Linker.runtime_code = js; always_required_codes } | `Function -> f | `Bind_to _ -> f | `Custom name -> J.ECall (f, [ J.EVar (J.ident name), `Not_spread ], J.N) - | `globalThis -> J.ECall (f, [ J.EVar (J.ident "globalThis"), `Not_spread ], J.N) + | `globalThis -> J.ECall (f, [ J.EVar (J.ident global_object), `Not_spread ], J.N) in match global with | `Bind_to name -> diff --git a/compiler/tests-compiler/empty_cma.ml b/compiler/tests-compiler/empty_cma.ml index fece9667ee..794c6c8e96 100644 --- a/compiler/tests-compiler/empty_cma.ml +++ b/compiler/tests-compiler/empty_cma.ml @@ -29,7 +29,7 @@ let%expect_test _ = Sys.remove "empty.js"; [%expect {| - (function(joo_global_object){"use strict";return}(globalThis)); +(function(globalThis){"use strict";return}(globalThis)); - //# sourceMappingURL=empty.map +//# sourceMappingURL=empty.map |}] diff --git a/compiler/tests-compiler/sourcemap.ml b/compiler/tests-compiler/sourcemap.ml index ddbb519734..8a1720bc51 100644 --- a/compiler/tests-compiler/sourcemap.ml +++ b/compiler/tests-compiler/sourcemap.ml @@ -59,9 +59,9 @@ let%expect_test _ = $ cat "test.ml" 1: let id x = x $ cat "test.js" - 1: (function(joo_global_object) + 1: (function(globalThis) 2: {"use strict"; - 3: var runtime=joo_global_object.jsoo_runtime; + 3: var runtime=globalThis.jsoo_runtime; 4: function id(x){return x} 5: var Test=[0,id]; 6: runtime.caml_register_global(0,Test,"Test"); diff --git a/lib/js_of_ocaml/js.ml b/lib/js_of_ocaml/js.ml index e95c2c1b3f..54cfcacd6d 100644 --- a/lib/js_of_ocaml/js.ml +++ b/lib/js_of_ocaml/js.ml @@ -64,7 +64,7 @@ module Js = struct external pure_js_expr : string -> 'a = "caml_pure_js_expr" - let global = pure_js_expr "joo_global_object" + let global = pure_js_expr "globalThis" external callback : ('a -> 'b) -> ('c, 'a -> 'b) meth_callback = "%identity" diff --git a/runtime/bigarray.js b/runtime/bigarray.js index b1b724dae0..0114ce8902 100644 --- a/runtime/bigarray.js +++ b/runtime/bigarray.js @@ -54,7 +54,7 @@ function caml_ba_get_size_per_element(kind){ //Requires: caml_ba_get_size_per_element //Requires: caml_invalid_argument function caml_ba_create_buffer(kind, size){ - var g = joo_global_object; + var g = globalThis; var view; switch(kind){ case 0: view = g.Float32Array; break; @@ -877,7 +877,7 @@ function caml_ba_to_typed_array(ba){ //Provides: caml_ba_kind_of_typed_array mutable //Requires: caml_invalid_argument function caml_ba_kind_of_typed_array(ta){ - var g = joo_global_object; + var g = globalThis; var kind; if (ta instanceof g.Float32Array) kind = 0; else if (ta instanceof g.Float64Array) kind = 1; diff --git a/runtime/bigstring.js b/runtime/bigstring.js index b82dd588b9..b7ac54d8b9 100644 --- a/runtime/bigstring.js +++ b/runtime/bigstring.js @@ -19,14 +19,14 @@ function bigstring_to_typed_array(bs) { //Provides: bigstring_of_array_buffer mutable //Requires: caml_ba_create_unsafe function bigstring_of_array_buffer(ab) { - var ta = new joo_global_object.Uint8Array(ab); + var ta = new globalThis.Uint8Array(ab); return caml_ba_create_unsafe(12, 0, [ta.length], ta); } //Provides: bigstring_of_typed_array mutable //Requires: caml_ba_create_unsafe function bigstring_of_typed_array(ba) { - var ta = new joo_global_object.Uint8Array(ba.buffer, ba.byteOffset, ba.length * ba.BYTES_PER_ELEMENT); + var ta = new globalThis.Uint8Array(ba.buffer, ba.byteOffset, ba.length * ba.BYTES_PER_ELEMENT); return caml_ba_create_unsafe(12, 0, [ta.length], ta); } diff --git a/runtime/dynlink.js b/runtime/dynlink.js index 842a0ee19e..6b6ebc5eff 100644 --- a/runtime/dynlink.js +++ b/runtime/dynlink.js @@ -17,14 +17,14 @@ // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. //Provides: current_libs -var current_libs = [0, joo_global_object] +var current_libs = [0, globalThis] //Provides: caml_dynlink_open_lib //Requires: current_libs, caml_failwith //Requires: caml_jsstring_of_string function caml_dynlink_open_lib (_mode,file) { var name = caml_jsstring_of_string(file); - joo_global_object.console.log("Dynlink: try to open ", name); + globalThis.console.log("Dynlink: try to open ", name); //caml_failwith("file not found: "+name) current_libs.push({}); return current_libs.length; @@ -42,7 +42,7 @@ function caml_dynlink_close_lib (idx) { //Requires: caml_jsstring_of_string function caml_dynlink_lookup_symbol (idx, fun_name) { var name = caml_jsstring_of_string(fun_name); - joo_global_object.console.log("Dynlink: look for symbol ", name); + globalThis.console.log("Dynlink: look for symbol ", name); if(current_libs[idx] && current_libs[idx][name]) return current_libs[idx][name]; return 0; diff --git a/runtime/fs.js b/runtime/fs.js index 2ee2a47d4d..c3658d9e39 100644 --- a/runtime/fs.js +++ b/runtime/fs.js @@ -26,8 +26,8 @@ function caml_trailing_slash(name){ //Provides: caml_current_dir //Requires: caml_trailing_slash, fs_node_supported -if(fs_node_supported () && joo_global_object.process && joo_global_object.process.cwd) - var caml_current_dir = joo_global_object.process.cwd().replace(/\\/g,'/'); +if(fs_node_supported () && globalThis.process && globalThis.process.cwd) + var caml_current_dir = globalThis.process.cwd().replace(/\\/g,'/'); else var caml_current_dir = "/static"; caml_current_dir = caml_trailing_slash(caml_current_dir); @@ -70,8 +70,8 @@ function make_path_is_absolute() { } return; } - if(fs_node_supported () && joo_global_object.process && joo_global_object.process.platform) { - return joo_global_object.process.platform === 'win32' ? win32 : posix; + if(fs_node_supported () && globalThis.process && globalThis.process.platform) { + return globalThis.process.platform === 'win32' ? win32 : posix; } else return posix } @@ -282,11 +282,11 @@ function caml_ba_map_file_bytecode(argv,argn){ //Provides: jsoo_create_file_extern function jsoo_create_file_extern(name,content){ - if(joo_global_object.jsoo_create_file) - joo_global_object.jsoo_create_file(name,content); + if(globalThis.jsoo_create_file) + globalThis.jsoo_create_file(name,content); else { - if(!joo_global_object.caml_fs_tmp) joo_global_object.caml_fs_tmp = []; - joo_global_object.caml_fs_tmp.push({name:name,content:content}); + if(!globalThis.caml_fs_tmp) globalThis.caml_fs_tmp = []; + globalThis.caml_fs_tmp.push({name:name,content:content}); } return 0; } @@ -294,14 +294,14 @@ function jsoo_create_file_extern(name,content){ //Provides: caml_fs_init //Requires: jsoo_create_file function caml_fs_init (){ - var tmp=joo_global_object.caml_fs_tmp + var tmp=globalThis.caml_fs_tmp if(tmp){ for(var i = 0; i < tmp.length; i++){ jsoo_create_file(tmp[i].name,tmp[i].content); } } - joo_global_object.jsoo_create_file = jsoo_create_file; - joo_global_object.caml_fs_tmp = []; + globalThis.jsoo_create_file = jsoo_create_file; + globalThis.caml_fs_tmp = []; return 0; } diff --git a/runtime/fs_node.js b/runtime/fs_node.js index 7437cc14f1..947f1ed2fc 100644 --- a/runtime/fs_node.js +++ b/runtime/fs_node.js @@ -20,9 +20,9 @@ //Provides: fs_node_supported function fs_node_supported () { return ( - typeof joo_global_object.process !== 'undefined' - && typeof joo_global_object.process.versions !== 'undefined' - && typeof joo_global_object.process.versions.node !== 'undefined') + typeof globalThis.process !== 'undefined' + && typeof globalThis.process.versions !== 'undefined' + && typeof globalThis.process.versions.node !== 'undefined') } //Provides: fs_node_supported //If: browser @@ -252,9 +252,9 @@ MlNodeFile.prototype.length = function () { } MlNodeFile.prototype.write = function(offset,buf,buf_offset,len){ var a = caml_array_of_string(buf); - if(! (a instanceof joo_global_object.Uint8Array)) - a = new joo_global_object.Uint8Array(a); - var buffer = joo_global_object.Buffer.from(a); + if(! (a instanceof globalThis.Uint8Array)) + a = new globalThis.Uint8Array(a); + var buffer = globalThis.Buffer.from(a); try { this.fs.writeSync(this.fd, buffer, buf_offset, len, offset); } catch (err) { @@ -264,9 +264,9 @@ MlNodeFile.prototype.write = function(offset,buf,buf_offset,len){ } MlNodeFile.prototype.read = function(offset,buf,buf_offset,len){ var a = caml_array_of_bytes(buf); - if(! (a instanceof joo_global_object.Uint8Array)) - a = new joo_global_object.Uint8Array(a); - var buffer = joo_global_object.Buffer.from(a); + if(! (a instanceof globalThis.Uint8Array)) + a = new globalThis.Uint8Array(a); + var buffer = globalThis.Buffer.from(a); try { this.fs.readSync(this.fd, buffer, buf_offset, len, offset); } catch (err) { @@ -278,8 +278,8 @@ MlNodeFile.prototype.read = function(offset,buf,buf_offset,len){ return 0 } MlNodeFile.prototype.read_one = function(offset){ - var a = new joo_global_object.Uint8Array(1); - var buffer = joo_global_object.Buffer.from(a); + var a = new globalThis.Uint8Array(1); + var buffer = globalThis.Buffer.from(a); try { this.fs.readSync(this.fd, buffer, 0, 1, offset); } catch (err) { diff --git a/runtime/graphics.js b/runtime/graphics.js index b7d382aa96..a71941aad2 100644 --- a/runtime/graphics.js +++ b/runtime/graphics.js @@ -42,7 +42,7 @@ function caml_gr_state_set(ctx) { //Requires: caml_failwith //Requires: caml_jsstring_of_string function caml_gr_open_graph(info){ - var g = joo_global_object; + var g = globalThis; var info = caml_jsstring_of_string(info); function get(name){ var res = info.match("(^|,) *"+name+" *= *([a-zA-Z0-9_]+) *(,|$)"); @@ -446,7 +446,7 @@ function caml_gr_draw_image(im,x,y){ canvas.width = s.width; canvas.height = s.height; canvas.getContext("2d").putImageData(im,0,0); - var image = new joo_global_object.Image(); + var image = new globalThis.Image(); image.onload = function () { s.context.drawImage(image,x,s.height - im.height - y); im.image = image; diff --git a/runtime/ieee_754.js b/runtime/ieee_754.js index 93ad3f60f1..aaa84f0ec1 100644 --- a/runtime/ieee_754.js +++ b/runtime/ieee_754.js @@ -67,9 +67,9 @@ function caml_int64_bits_of_float (x) { //Provides: caml_int32_bits_of_float const //Requires: jsoo_floor_log2 function caml_int32_bits_of_float (x) { - var float32a = new joo_global_object.Float32Array(1); + var float32a = new globalThis.Float32Array(1); float32a[0] = x; - var int32a = new joo_global_object.Int32Array(float32a.buffer); + var int32a = new globalThis.Int32Array(float32a.buffer); return int32a[0] | 0; } @@ -174,9 +174,9 @@ function caml_trunc_float(x){ //Provides: caml_int32_float_of_bits const function caml_int32_float_of_bits (x) { - var int32a = new joo_global_object.Int32Array(1); + var int32a = new globalThis.Int32Array(1); int32a[0] = x; - var float32a = new joo_global_object.Float32Array(int32a.buffer); + var float32a = new globalThis.Float32Array(int32a.buffer); return float32a[0]; } diff --git a/runtime/jslib.js b/runtime/jslib.js index 7429c07a83..986bf2fa7a 100644 --- a/runtime/jslib.js +++ b/runtime/jslib.js @@ -38,7 +38,7 @@ function caml_js_typeof(o) { return typeof o; } //Provides: caml_js_on_ie const function caml_js_on_ie () { var ua = - joo_global_object.navigator?joo_global_object.navigator.userAgent:""; + globalThis.navigator?globalThis.navigator.userAgent:""; return ua.indexOf("MSIE") != -1 && ua.indexOf("Opera") != 0; } @@ -71,7 +71,7 @@ function caml_js_html_entities(s) { /////////// Debugging console //Provides: caml_js_get_console const function caml_js_get_console () { - var c = joo_global_object.console?joo_global_object.console:{}; + var c = globalThis.console?globalThis.console:{}; var m = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml", "trace", "group", "groupCollapsed", "groupEnd", "time", "timeEnd"]; function f () {} @@ -98,7 +98,7 @@ function caml_trampoline_return(f,args) { //Requires: caml_utf16_of_utf8 function js_print_stdout(s) { var s = caml_utf16_of_utf8(s); - var g = joo_global_object; + var g = globalThis; if (g.process && g.process.stdout && g.process.stdout.write) { g.process.stdout.write(s) } else { @@ -114,7 +114,7 @@ function js_print_stdout(s) { //Requires: caml_utf16_of_utf8 function js_print_stderr(s) { var s = caml_utf16_of_utf8(s); - var g = joo_global_object; + var g = globalThis; if (g.process && g.process.stdout && g.process.stdout.write) { g.process.stderr.write(s) } else { @@ -141,19 +141,19 @@ function caml_is_js() { function caml_wrap_exception(e) { if(e instanceof Array) return e; //Stack_overflow: chrome, safari - if(joo_global_object.RangeError - && e instanceof joo_global_object.RangeError + if(globalThis.RangeError + && e instanceof globalThis.RangeError && e.message && e.message.match(/maximum call stack/i)) return caml_return_exn_constant(caml_global_data.Stack_overflow); //Stack_overflow: firefox - if(joo_global_object.InternalError - && e instanceof joo_global_object.InternalError + if(globalThis.InternalError + && e instanceof globalThis.InternalError && e.message && e.message.match(/too much recursion/i)) return caml_return_exn_constant(caml_global_data.Stack_overflow); //Wrap Error in Js.Error exception - if(e instanceof joo_global_object.Error && caml_named_value("jsError")) + if(e instanceof globalThis.Error && caml_named_value("jsError")) return [0,caml_named_value("jsError"),e]; //fallback: wrapped in Failure return [0,caml_global_data.Failure,caml_string_of_jsstring (String(e))]; @@ -164,7 +164,7 @@ function caml_wrap_exception(e) { //Requires: caml_global_data function caml_exn_with_js_backtrace(exn, force) { //never reraise for constant exn - if(!exn.js_error || force || exn[0] == 248) exn.js_error = new joo_global_object.Error("Js exception containing backtrace"); + if(!exn.js_error || force || exn[0] == 248) exn.js_error = new globalThis.Error("Js exception containing backtrace"); return exn; } diff --git a/runtime/jslib_js_of_ocaml.js b/runtime/jslib_js_of_ocaml.js index e6585d4966..0dde653d85 100644 --- a/runtime/jslib_js_of_ocaml.js +++ b/runtime/jslib_js_of_ocaml.js @@ -68,7 +68,7 @@ function caml_js_var(x) { //Checks that x has the form ident[.ident]* if(!x.match(/^[a-zA-Z_$][a-zA-Z_$0-9]*(\.[a-zA-Z_$][a-zA-Z_$0-9]*)*$/)){ js_print_stderr("caml_js_var: \"" + x + "\" is not a valid JavaScript variable. continuing .."); - //joo_global_object.console.error("Js.Unsafe.eval_string") + //globalThis.console.error("Js.Unsafe.eval_string") } return eval(x); } @@ -247,7 +247,7 @@ function caml_js_export_var (){ if(typeof module !== 'undefined' && module && module.exports) return module.exports else - return joo_global_object; + return globalThis; } @@ -255,7 +255,7 @@ function caml_js_export_var (){ //Requires: caml_failwith //Weakdef function caml_xmlhttprequest_create(unit){ - var g = joo_global_object; + var g = globalThis; if(typeof g.XMLHttpRequest !== 'undefined') { try { return new g.XMLHttpRequest } catch (e) { }; } diff --git a/runtime/marshal.js b/runtime/marshal.js index 3a63901e98..46b39e02b3 100644 --- a/runtime/marshal.js +++ b/runtime/marshal.js @@ -412,7 +412,7 @@ function caml_marshal_data_size (s, ofs) { //Provides: MlObjectTable var MlObjectTable; -if (typeof joo_global_object.WeakMap === 'undefined') { +if (typeof globalThis.WeakMap === 'undefined') { MlObjectTable = function() { /* polyfill (using linear search) */ function NaiveLookup(objs) { this.objs = objs; } @@ -432,7 +432,7 @@ if (typeof joo_global_object.WeakMap === 'undefined') { } else { MlObjectTable = function MlObjectTable() { - this.objs = []; this.lookup = new joo_global_object.WeakMap(); + this.objs = []; this.lookup = new globalThis.WeakMap(); }; } @@ -505,7 +505,7 @@ var caml_output_val = function (){ /* Marshal.Compat_32 is redundant since integers are 32-bit anyway */ if (closures) - joo_global_object.console.warn("in caml_output_val: flag Marshal.Closures is not supported."); + globalThis.console.warn("in caml_output_val: flag Marshal.Closures is not supported."); var writer = new Writer (); var stack = []; diff --git a/runtime/mlBytes.js b/runtime/mlBytes.js index f2b33e0bec..5f6ae50ebf 100644 --- a/runtime/mlBytes.js +++ b/runtime/mlBytes.js @@ -452,8 +452,8 @@ function caml_convert_string_to_bytes (s) { //Provides: caml_convert_bytes_to_array function caml_convert_bytes_to_array (s) { /* Assumes not ARRAY */ - if(joo_global_object.Uint8Array) { - var a = new joo_global_object.Uint8Array(s.l); + if(globalThis.Uint8Array) { + var a = new globalThis.Uint8Array(s.l); } else { var a = new Array(s.l); } diff --git a/runtime/nat.js b/runtime/nat.js index f6f1ac0e05..ef1ae4ae60 100644 --- a/runtime/nat.js +++ b/runtime/nat.js @@ -11,7 +11,7 @@ function initialize_nat() { //Provides: MlNat function MlNat(x){ - this.data = new joo_global_object.Int32Array(x); + this.data = new globalThis.Int32Array(x); // length_nat isn't external, so we have to make the Obj.size // work out right. The +2 to array length seems to work. this.length = this.data.length + 2 diff --git a/runtime/stdlib.js b/runtime/stdlib.js index 2a8502d79f..22d1c5101d 100644 --- a/runtime/stdlib.js +++ b/runtime/stdlib.js @@ -66,8 +66,8 @@ var caml_global_data = [0]; //Provides: caml_register_global (const, shallow, const) //Requires: caml_global_data function caml_register_global (n, v, name_opt) { - if(name_opt && joo_global_object.toplevelReloc) - n = joo_global_object.toplevelReloc(name_opt); + if(name_opt && globalThis.toplevelReloc) + n = globalThis.toplevelReloc(name_opt); caml_global_data[n + 1] = v; if(name_opt) caml_global_data[name_opt] = v; } diff --git a/runtime/sys.js b/runtime/sys.js index 59caacfca8..78983b9a82 100644 --- a/runtime/sys.js +++ b/runtime/sys.js @@ -26,7 +26,7 @@ function caml_raise_sys_error (msg) { //Provides: caml_sys_exit //Requires: caml_invalid_argument function caml_sys_exit (code) { - var g = joo_global_object; + var g = globalThis; if(g.quit) g.quit(code); //nodejs if(g.process && g.process.exit) @@ -91,7 +91,7 @@ function caml_fatal_uncaught_exception(err){ var msg = caml_format_exception(err); var at_exit = caml_named_value("Pervasives.do_at_exit"); if(at_exit) { at_exit(0) } - joo_global_object.console.error("Fatal error: exception " + msg + "\n"); + globalThis.console.error("Fatal error: exception " + msg + "\n"); } } else { @@ -102,9 +102,9 @@ function caml_fatal_uncaught_exception(err){ //Provides: caml_set_static_env function caml_set_static_env(k,v){ - if(!joo_global_object.jsoo_static_env) - joo_global_object.jsoo_static_env = {} - joo_global_object.jsoo_static_env[k] = v; + if(!globalThis.jsoo_static_env) + globalThis.jsoo_static_env = {} + globalThis.jsoo_static_env[k] = v; return 0; } //Provides: caml_sys_getenv (const) @@ -112,16 +112,16 @@ function caml_set_static_env(k,v){ //Requires: caml_string_of_jsstring //Requires: caml_jsstring_of_string function caml_sys_getenv (name) { - var g = joo_global_object; + var g = globalThis; var n = caml_jsstring_of_string(name); //nodejs env if(g.process && g.process.env && g.process.env[n] != undefined) return caml_string_of_jsstring(g.process.env[n]); - if(joo_global_object.jsoo_static_env - && joo_global_object.jsoo_static_env[n]) - return caml_string_of_jsstring(joo_global_object.jsoo_static_env[n]) + if(globalThis.jsoo_static_env + && globalThis.jsoo_static_env[n]) + return caml_string_of_jsstring(globalThis.jsoo_static_env[n]) caml_raise_not_found (); } @@ -134,7 +134,7 @@ function caml_sys_unsafe_getenv(name){ //Provides: caml_argv //Requires: caml_string_of_jsstring var caml_argv = ((function () { - var g = joo_global_object; + var g = globalThis; var main = "a.out"; var args = [] @@ -248,9 +248,9 @@ function caml_sys_const_backend_type () { } //Provides: os_type -var os_type = (joo_global_object.process && - joo_global_object.process.platform && - joo_global_object.process.platform == "win32") ? "Cygwin" : "Unix"; +var os_type = (globalThis.process && + globalThis.process.platform && + globalThis.process.platform == "win32") ? "Cygwin" : "Unix"; //Provides: caml_sys_get_config const @@ -324,7 +324,7 @@ function caml_spacetime_only_works_for_native_code() { //Always //Requires: caml_fatal_uncaught_exception function caml_setup_uncaught_exception_handler() { - var g = joo_global_object; + var g = globalThis; if(g.process && g.process.on) { g.process.on('uncaughtException', function (err, origin) { caml_fatal_uncaught_exception(err); diff --git a/runtime/toplevel.js b/runtime/toplevel.js index 5811722ebf..72591012be 100644 --- a/runtime/toplevel.js +++ b/runtime/toplevel.js @@ -52,8 +52,8 @@ function caml_get_section_table () { //Requires: caml_failwith //Version: < 4.08 function caml_reify_bytecode (code, _sz) { - if(joo_global_object.toplevelCompile) - return joo_global_object.toplevelCompile([0,code]); + if(globalThis.toplevelCompile) + return globalThis.toplevelCompile([0,code]); else caml_failwith("Toplevel not initialized (toplevelCompile)") } @@ -61,8 +61,8 @@ function caml_reify_bytecode (code, _sz) { //Requires: caml_failwith //Version: >= 4.08 function caml_reify_bytecode (code, _sz,_) { - if(joo_global_object.toplevelCompile) - return [0, 0, joo_global_object.toplevelCompile(code)]; + if(globalThis.toplevelCompile) + return [0, 0, globalThis.toplevelCompile(code)]; else caml_failwith("Toplevel not initialized (toplevelCompile)") } diff --git a/runtime/unix.js b/runtime/unix.js index c20f6ba87e..68ba4e8cc3 100644 --- a/runtime/unix.js +++ b/runtime/unix.js @@ -186,8 +186,8 @@ function unix_unlink(name) { //Provides: unix_getuid //Requires: caml_raise_not_found function unix_getuid(unit) { - if(joo_global_object.process && joo_global_object.process.getuid){ - return joo_global_object.process.getuid(); + if(globalThis.process && globalThis.process.getuid){ + return globalThis.process.getuid(); } caml_raise_not_found(); } From 81b3f6653883d07650b65ba85911dff4e2a8d7ae Mon Sep 17 00:00:00 2001 From: Hugo Heuzard Date: Sat, 18 Dec 2021 20:36:12 +0100 Subject: [PATCH 3/4] Compiler: restore compat with joo_global_object MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Javier Chávarri --- compiler/lib/constant.ml | 2 ++ compiler/lib/driver.ml | 33 ++++++++++++++++++++++++--------- compiler/lib/linker.ml | 10 ++++++++++ 3 files changed, 36 insertions(+), 9 deletions(-) diff --git a/compiler/lib/constant.ml b/compiler/lib/constant.ml index 844a20a2de..d0e13536dd 100644 --- a/compiler/lib/constant.ml +++ b/compiler/lib/constant.ml @@ -19,3 +19,5 @@ open! Stdlib let global_object = "globalThis" + +let old_global_object = "joo_global_object" diff --git a/compiler/lib/driver.ml b/compiler/lib/driver.ml index b46b412f42..d323c830d0 100644 --- a/compiler/lib/driver.ml +++ b/compiler/lib/driver.ml @@ -355,6 +355,20 @@ let pack ~global ~standalone { Linker.runtime_code = js; always_required_codes } else js in let wrap_in_iifa ~can_use_strict js = + let js = + let o = new Js_traverse.free in + let js = o#program js in + if StringSet.mem Constant.old_global_object o#get_free_name + then + ( J.Statement + (J.Variable_statement + [ ( J.ident Constant.old_global_object + , Some (J.EVar (J.ident global_object), J.N) ) + ]) + , J.N ) + :: js + else js + in let f = J.EFun (None, [ J.ident global_object ], use_strict js ~can_use_strict, J.U) in @@ -384,12 +398,13 @@ let pack ~global ~standalone { Linker.runtime_code = js; always_required_codes } in let runtime_js = wrap_in_iifa ~can_use_strict:true js in let js = List.flatten always_required_js @ runtime_js in - let js = match global, standalone with - | (`Function | `Bind_to _ | `Custom _), _ -> js + let js = + match global, standalone with + | (`Function | `Bind_to _ | `Custom _), _ -> js | `globalThis, false -> js | `globalThis, true -> - let s = - {| + let s = + {| (function (Object) { typeof globalThis !== 'object' && ( this ? @@ -406,11 +421,11 @@ let pack ~global ~standalone { Linker.runtime_code = js; always_required_codes } } }(Object)); |} - in - let lex = Lexing.from_string s in - let lex = Parse_js.Lexer.of_lexbuf lex in - let e = Parse_js.parse lex in - e @ js + in + let lex = Lexing.from_string s in + let lex = Parse_js.Lexer.of_lexbuf lex in + let e = Parse_js.parse lex in + e @ js in (* post pack optim *) let t3 = Timer.make () in diff --git a/compiler/lib/linker.ml b/compiler/lib/linker.ml index 4d0273e088..31859414d0 100644 --- a/compiler/lib/linker.ml +++ b/compiler/lib/linker.ml @@ -295,6 +295,16 @@ let check_primitive ~name pi ~code ~requires = let freename = StringSet.diff freename Reserved.keyword in let freename = StringSet.diff freename Reserved.provided in let freename = StringSet.remove Constant.global_object freename in + if StringSet.mem Constant.old_global_object freename && false + (* Don't warn yet, we want to give a transition period where both + "globalThis" and "joo_global_object" are allowed without extra + noise *) + then + warn + "warning: %s: 'joo_global_object' is being deprecated, please use `globalThis` \ + instead@." + (loc pi); + let freename = StringSet.remove Constant.old_global_object freename in if not (StringSet.mem name free#get_def_name) then warn From 782470357a3b5641127942dcec8b0a9d2dc7d9ca Mon Sep 17 00:00:00 2001 From: Hugo Heuzard Date: Mon, 20 Dec 2021 17:30:51 +0100 Subject: [PATCH 4/4] Changes --- CHANGES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.md b/CHANGES.md index 44a9738d0d..37f43a735e 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -7,6 +7,7 @@ * Compiler: improve static evaluation of cond (#1178) * Compiler: be more consistent dealing with js vs ocaml strings (#984) * Compiler: Compiler: add BigInt to provided symbols (fix #1168) (#1191) +* Compiler: use globalThis, drop joo_global_object #1193 * Lib: add messageEvent to Dom_html (#1164) * Lib: add PerformanceObserver API (#1164) * Lib: add CSSStyleDeclaration.{setProperty, getPropertyValue, getPropertyPriority, removeProperty} (#1170)