Skip to content

Compiler: use globalThis, drop joo_global_object #1193

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 4 commits into from
Jan 3, 2022
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
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 5 additions & 5 deletions compiler/bin-js_of_ocaml/build_fs.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion compiler/bin-js_of_ocaml/compile.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions compiler/bin-jsoo_fs/jsoo_fs.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion compiler/lib/constant.ml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@

open! Stdlib

let global_object = "joo_global_object"
let global_object = "globalThis"

let old_global_object = "joo_global_object"
64 changes: 47 additions & 17 deletions compiler/lib/driver.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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...@.";
Expand Down Expand Up @@ -355,6 +355,20 @@ let pack ~global { 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
Expand All @@ -363,20 +377,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 global_object), `Not_spread ], J.N)
in
match global with
| `Bind_to name ->
Expand All @@ -397,6 +398,35 @@ 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
Expand Down Expand Up @@ -428,7 +458,7 @@ type profile = Code.program -> Code.program

let f
?(standalone = true)
?(global = `Auto)
?(global = `globalThis)
?(profile = o1)
?(dynlink = false)
?(linkall = false)
Expand All @@ -450,7 +480,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 ()
Expand Down
2 changes: 1 addition & 1 deletion compiler/lib/driver.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions compiler/lib/linker.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions compiler/lib/reserved.ml
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ let provided =
; "Math"
; "JSON"
; "Object"
; "globalThis"
; "RegExp"
; "String"
; "Boolean"
Expand Down
4 changes: 2 additions & 2 deletions compiler/tests-compiler/empty_cma.ml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ let%expect_test _ =
Sys.remove "empty.js";
[%expect
{|
(function(joo_global_object){"use strict";return}(function(){return this}()));
(function(globalThis){"use strict";return}(globalThis));

//# sourceMappingURL=empty.map
//# sourceMappingURL=empty.map
|}]
6 changes: 3 additions & 3 deletions compiler/tests-compiler/sourcemap.ml
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ 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");
7: return}
8: (function(){return this}()));
8: (globalThis));
9:
10: //# sourceMappingURL=test.map
null:-1:-1 -> 3:4
Expand Down
2 changes: 1 addition & 1 deletion lib/js_of_ocaml/js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
4 changes: 2 additions & 2 deletions runtime/bigarray.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions runtime/bigstring.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
6 changes: 3 additions & 3 deletions runtime/dynlink.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
22 changes: 11 additions & 11 deletions runtime/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -282,26 +282,26 @@ 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;
}

//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;
}

Expand Down
Loading