From c42bf078bee32315db9c1fd2a46ac1432a77dcce Mon Sep 17 00:00:00 2001 From: Hugo Heuzard Date: Wed, 9 Oct 2024 22:20:34 +0200 Subject: [PATCH 1/3] Compiler: revisit static env handling --- CHANGES.md | 1 + compiler/tests-env/dune | 27 +++++++++++++++++++++++++++ compiler/tests-env/setup.js | 2 ++ compiler/tests-env/test.ml | 10 ++++++++++ compiler/tests-env/test.reference | 4 ++++ runtime/sys.js | 14 ++++++++++---- 6 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 compiler/tests-env/dune create mode 100644 compiler/tests-env/setup.js create mode 100644 compiler/tests-env/test.ml create mode 100644 compiler/tests-env/test.reference diff --git a/CHANGES.md b/CHANGES.md index 00cdcbb41f..e3a9975d72 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -17,6 +17,7 @@ * Compiler: introduce a Targetint module that follows the semantic of the backend (js or wasm) * Compiler: warn on joo_global_object +* Compiler: revisit static env handling (#???) * Runtime: change Sys.os_type on windows (Cygwin -> Win32) * Runtime: backtraces are really expensive, they need to be be explicitly requested at compile time (--enable with-js-error) or at startup (OCAMLRUNPARAM=b=1) diff --git a/compiler/tests-env/dune b/compiler/tests-env/dune new file mode 100644 index 0000000000..0f0902a54b --- /dev/null +++ b/compiler/tests-env/dune @@ -0,0 +1,27 @@ +(executable + (modes js) + (js_of_ocaml + (flags --setenv JSOO_C=from-jsoo-args)) + (name test)) + +(rule + (target test.js) + (action + (with-outputs-to + %{target} + (cat %{dep:setup.js} %{dep:./test.bc.js})))) + +(rule + (target test.output) + (action + (with-outputs-to + %{target} + (setenv + JSOO_B + from-env + (run node %{dep:./test.js}))))) + +(rule + (alias runtest) + (action + (diff test.reference test.output))) diff --git a/compiler/tests-env/setup.js b/compiler/tests-env/setup.js new file mode 100644 index 0000000000..82695362be --- /dev/null +++ b/compiler/tests-env/setup.js @@ -0,0 +1,2 @@ +globalThis.jsoo_env = {}; +globalThis.jsoo_env["JSOO_A"] = "from-global-this"; diff --git a/compiler/tests-env/test.ml b/compiler/tests-env/test.ml new file mode 100644 index 0000000000..069e0f9fa4 --- /dev/null +++ b/compiler/tests-env/test.ml @@ -0,0 +1,10 @@ +let test x = + match Sys.getenv x with + | exception Not_found -> print_endline (x ^ " not found") + | v -> print_endline (x ^ " = " ^ v) + +let () = + test "JSOO_A"; + test "JSOO_B"; + test "JSOO_C"; + test "JSOO_D" diff --git a/compiler/tests-env/test.reference b/compiler/tests-env/test.reference new file mode 100644 index 0000000000..df88bf0319 --- /dev/null +++ b/compiler/tests-env/test.reference @@ -0,0 +1,4 @@ +JSOO_A = from-global-this +JSOO_B = from-env +JSOO_C = from-jsoo-args +JSOO_D not found diff --git a/runtime/sys.js b/runtime/sys.js index 8f2f1afb71..466ad111ee 100644 --- a/runtime/sys.js +++ b/runtime/sys.js @@ -98,21 +98,27 @@ function caml_fatal_uncaught_exception(err) { } } +//Provides: jsoo_static_env +var jsoo_static_env = {}; + //Provides: caml_set_static_env +//Requires: jsoo_static_env function caml_set_static_env(k, v) { - if (!globalThis.jsoo_static_env) globalThis.jsoo_static_env = {}; - globalThis.jsoo_static_env[k] = v; + jsoo_static_env[k] = v; return 0; } //Provides: jsoo_sys_getenv (const) +//Requires: jsoo_static_env function jsoo_sys_getenv(n) { + if (jsoo_static_env[n]) return jsoo_static_env[n]; var process = globalThis.process; //nodejs env if (process && process.env && process.env[n] !== undefined) return process.env[n]; - if (globalThis.jsoo_static_env && globalThis.jsoo_static_env[n]) - return globalThis.jsoo_static_env[n]; + if (globalThis.jsoo_env && typeof globalThis.jsoo_env[n] === "string") { + return globalThis.jsoo_env[n]; + } } //Provides: caml_sys_getenv (const) From 1026f429fac33a3e0c62cb37bd621ac987ee9571 Mon Sep 17 00:00:00 2001 From: Hugo Heuzard Date: Wed, 9 Oct 2024 23:50:38 +0200 Subject: [PATCH 2/3] WIP --- compiler/tests-env/dune | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/tests-env/dune b/compiler/tests-env/dune index 0f0902a54b..908269b062 100644 --- a/compiler/tests-env/dune +++ b/compiler/tests-env/dune @@ -1,7 +1,7 @@ (executable (modes js) (js_of_ocaml - (flags --setenv JSOO_C=from-jsoo-args)) + (flags :standard --setenv JSOO_C=from-jsoo-args)) (name test)) (rule From 5759865f1306e695cadf50e709f996b07b243f67 Mon Sep 17 00:00:00 2001 From: Hugo Heuzard Date: Wed, 9 Oct 2024 23:52:33 +0200 Subject: [PATCH 3/3] changes --- CHANGES.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index e3a9975d72..61f0fe94cf 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -17,7 +17,7 @@ * Compiler: introduce a Targetint module that follows the semantic of the backend (js or wasm) * Compiler: warn on joo_global_object -* Compiler: revisit static env handling (#???) +* Compiler: revisit static env handling (#1708) * Runtime: change Sys.os_type on windows (Cygwin -> Win32) * Runtime: backtraces are really expensive, they need to be be explicitly requested at compile time (--enable with-js-error) or at startup (OCAMLRUNPARAM=b=1)