Skip to content

Commit 7227daf

Browse files
committed
Runtime: fix for target-env browser
1 parent 83a01ed commit 7227daf

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

compiler/tests-compiler/target_env.ml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,52 @@ let () =
6161
without node filesystem
6262
Sys.readdir ok
6363
empty root |}]
64+
65+
module Jsoo = Js_of_ocaml_compiler
66+
67+
class find_require r =
68+
object
69+
inherit Jsoo.Js_traverse.iter as super
70+
71+
method! expression e =
72+
(match e with
73+
| ECall (EVar (Jsoo.Javascript.S { name = Utf8 "require"; loc; _ }), _, args, _)
74+
-> (
75+
match args with
76+
| [ Arg name ] -> (
77+
match name with
78+
| EStr (Utf8 name) -> r := (name, loc) :: !r
79+
| _ -> r := ("<Unknown>", loc) :: !r)
80+
| _ -> assert false)
81+
| _ -> ());
82+
super#expression e
83+
end
84+
85+
(* Check target-env=browser doesn't use 'require("node:...")' *)
86+
let%expect_test _ =
87+
let loc_to_string = function
88+
| Jsoo.Javascript.N -> "N"
89+
| U -> "U"
90+
| Pi pi -> Jsoo.Parse_info.to_string pi
91+
in
92+
let test flags =
93+
let r = ref [] in
94+
let o = new find_require r in
95+
let p = compile_and_parse_whole_program ~flags "" in
96+
o#program p;
97+
List.iter (fun (name, loc) -> Printf.eprintf "%s: %s\n" (loc_to_string loc) name) !r
98+
in
99+
test [ "--linkall" ];
100+
[%expect
101+
{|
102+
test.js:7688:15: node:tty
103+
test.js:7502:25: node:child_process
104+
test.js:7314:17: node:util
105+
test.js:6355:15: node:fs
106+
test.js:1163:20: node:fs
107+
test.js:1143:20: node:fs
108+
test.js:1097:34: node:fs
109+
test.js:1001:17: node:fs
110+
|}];
111+
test [ "--linkall"; "--target-env=browser" ];
112+
[%expect {| |}]

runtime/js/sys.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,14 @@ function caml_sys_temp_dir_name(_unit) {
384384
}
385385
}
386386

387+
//Provides: caml_sys_temp_dir_name
388+
//Requires: caml_string_of_jsstring
389+
//Version: >= 5.4
390+
//If: browser
391+
function caml_sys_temp_dir_name(_unit) {
392+
return caml_string_of_jsstring("");
393+
}
394+
387395
//Provides: caml_sys_convert_signal_number
388396
//Version: >= 5.4
389397
function caml_sys_convert_signal_number(signo) {

runtime/js/unix.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,14 @@ function caml_strerror(errno) {
228228
}
229229
}
230230

231+
//Provides: caml_strerror
232+
//Requires: unix_error
233+
//If: browser
234+
function caml_strerror(errno) {
235+
const code = unix_error[errno];
236+
code || ("Unknown error " + errno)
237+
}
238+
231239
//Provides: unix_error_message
232240
//Alias: caml_unix_error_message
233241
//Requires: caml_strerror, caml_string_of_jsstring

0 commit comments

Comments
 (0)