Skip to content

Commit 1edf056

Browse files
authored
Compiler: revisit static env handling (#1708)
1 parent 566ca51 commit 1edf056

File tree

6 files changed

+54
-4
lines changed

6 files changed

+54
-4
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
* Compiler: introduce a Targetint module
1818
that follows the semantic of the backend (js or wasm)
1919
* Compiler: warn on joo_global_object
20+
* Compiler: revisit static env handling (#1708)
2021
* Runtime: change Sys.os_type on windows (Cygwin -> Win32)
2122
* Runtime: backtraces are really expensive, they need to be be explicitly
2223
requested at compile time (--enable with-js-error) or at startup (OCAMLRUNPARAM=b=1)

compiler/tests-env/dune

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
(executable
2+
(modes js)
3+
(js_of_ocaml
4+
(flags :standard --setenv JSOO_C=from-jsoo-args))
5+
(name test))
6+
7+
(rule
8+
(target test.js)
9+
(action
10+
(with-outputs-to
11+
%{target}
12+
(cat %{dep:setup.js} %{dep:./test.bc.js}))))
13+
14+
(rule
15+
(target test.output)
16+
(action
17+
(with-outputs-to
18+
%{target}
19+
(setenv
20+
JSOO_B
21+
from-env
22+
(run node %{dep:./test.js})))))
23+
24+
(rule
25+
(alias runtest)
26+
(action
27+
(diff test.reference test.output)))

compiler/tests-env/setup.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
globalThis.jsoo_env = {};
2+
globalThis.jsoo_env["JSOO_A"] = "from-global-this";

compiler/tests-env/test.ml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
let test x =
2+
match Sys.getenv x with
3+
| exception Not_found -> print_endline (x ^ " not found")
4+
| v -> print_endline (x ^ " = " ^ v)
5+
6+
let () =
7+
test "JSOO_A";
8+
test "JSOO_B";
9+
test "JSOO_C";
10+
test "JSOO_D"

compiler/tests-env/test.reference

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
JSOO_A = from-global-this
2+
JSOO_B = from-env
3+
JSOO_C = from-jsoo-args
4+
JSOO_D not found

runtime/sys.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,21 +98,27 @@ function caml_fatal_uncaught_exception(err) {
9898
}
9999
}
100100

101+
//Provides: jsoo_static_env
102+
var jsoo_static_env = {};
103+
101104
//Provides: caml_set_static_env
105+
//Requires: jsoo_static_env
102106
function caml_set_static_env(k, v) {
103-
if (!globalThis.jsoo_static_env) globalThis.jsoo_static_env = {};
104-
globalThis.jsoo_static_env[k] = v;
107+
jsoo_static_env[k] = v;
105108
return 0;
106109
}
107110

108111
//Provides: jsoo_sys_getenv (const)
112+
//Requires: jsoo_static_env
109113
function jsoo_sys_getenv(n) {
114+
if (jsoo_static_env[n]) return jsoo_static_env[n];
110115
var process = globalThis.process;
111116
//nodejs env
112117
if (process && process.env && process.env[n] !== undefined)
113118
return process.env[n];
114-
if (globalThis.jsoo_static_env && globalThis.jsoo_static_env[n])
115-
return globalThis.jsoo_static_env[n];
119+
if (globalThis.jsoo_env && typeof globalThis.jsoo_env[n] === "string") {
120+
return globalThis.jsoo_env[n];
121+
}
116122
}
117123

118124
//Provides: caml_sys_getenv (const)

0 commit comments

Comments
 (0)