diff --git a/CHANGES.md b/CHANGES.md index 2cbae0c613..fd6bd8f8a8 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -14,6 +14,7 @@ * Runtime/wasm: fix Unix.times (#2096) * Ppx: disable spurious warning for unused "self" in object litteral (#2128) * Ppx: fix labelled arguments for methods (#2126) +* Runtime: runtime with target-env=browser should not rely on "require(..)" (#2129) # 6.2.0 (2025-07-30) - Lille diff --git a/compiler/tests-compiler/target_env.ml b/compiler/tests-compiler/target_env.ml index 70144741ad..8b17a16f1e 100644 --- a/compiler/tests-compiler/target_env.ml +++ b/compiler/tests-compiler/target_env.ml @@ -61,3 +61,46 @@ let () = without node filesystem Sys.readdir ok empty root |}] + +module Jsoo = Js_of_ocaml_compiler +module StringSet = Set.Make (String) + +class find_require r = + object + inherit Jsoo.Js_traverse.iter as super + + method! expression e = + (match e with + | ECall (EVar (Jsoo.Javascript.S { name = Utf8 "require"; loc; _ }), _, args, _) + -> ( + match args with + | [ Arg name ] -> ( + match name with + | EStr (Utf8 name) -> r := (name, loc) :: !r + | _ -> r := ("", loc) :: !r) + | _ -> assert false) + | _ -> ()); + super#expression e + end + +(* Check target-env=browser doesn't use 'require("node:...")' *) +let%expect_test _ = + let test flags = + let r = ref [] in + let o = new find_require r in + let p = compile_and_parse_whole_program ~flags "" in + o#program p; + let ss = + List.fold_left + (fun acc (x, _loc) -> + let x = if String.starts_with ~prefix:"node:" x then "node" else x in + StringSet.add x acc) + StringSet.empty + !r + in + StringSet.iter (fun name -> Printf.eprintf "%s\n" name) ss + in + test [ "--linkall" ]; + [%expect {| node |}]; + test [ "--linkall"; "--target-env=browser" ]; + [%expect {| |}] diff --git a/runtime/js/sys.js b/runtime/js/sys.js index 98a9ed5001..e43c3974a0 100644 --- a/runtime/js/sys.js +++ b/runtime/js/sys.js @@ -384,6 +384,14 @@ function caml_sys_temp_dir_name(_unit) { } } +//Provides: caml_sys_temp_dir_name +//Requires: caml_string_of_jsstring +//Version: >= 5.4 +//If: browser +function caml_sys_temp_dir_name(_unit) { + return caml_string_of_jsstring(""); +} + //Provides: caml_sys_convert_signal_number //Version: >= 5.4 function caml_sys_convert_signal_number(signo) { diff --git a/runtime/js/unix.js b/runtime/js/unix.js index 953b2ad0d4..146042617b 100644 --- a/runtime/js/unix.js +++ b/runtime/js/unix.js @@ -228,6 +228,14 @@ function caml_strerror(errno) { } } +//Provides: caml_strerror +//Requires: unix_error +//If: browser +function caml_strerror(errno) { + const code = unix_error[errno]; + code || "Unknown error " + errno; +} + //Provides: unix_error_message //Alias: caml_unix_error_message //Requires: caml_strerror, caml_string_of_jsstring