Skip to content

Compiler: revisit static env handling #1708

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 3 commits into from
Oct 10, 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
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 (#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)
Expand Down
27 changes: 27 additions & 0 deletions compiler/tests-env/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
(executable
(modes js)
(js_of_ocaml
(flags :standard --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)))
2 changes: 2 additions & 0 deletions compiler/tests-env/setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
globalThis.jsoo_env = {};
globalThis.jsoo_env["JSOO_A"] = "from-global-this";
10 changes: 10 additions & 0 deletions compiler/tests-env/test.ml
Original file line number Diff line number Diff line change
@@ -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"
4 changes: 4 additions & 0 deletions compiler/tests-env/test.reference
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
JSOO_A = from-global-this
JSOO_B = from-env
JSOO_C = from-jsoo-args
JSOO_D not found
14 changes: 10 additions & 4 deletions runtime/sys.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down