diff --git a/compiler/esy.json b/compiler/esy.json index 7b6977e301..7804a732d4 100644 --- a/compiler/esy.json +++ b/compiler/esy.json @@ -29,6 +29,7 @@ "@opam/fp": "0.0.1", "@opam/fs": "0.0.2", "@opam/grain_dypgen": "0.2", + "@opam/js_of_ocaml": ">= 3.6.0", "@opam/ocamlgraph": ">= 2.0.0", "@opam/ppx_deriving_yojson": ">= 3.5.2", "@opam/ppx_sexp_conv": ">= 0.14.0", diff --git a/compiler/esy.lock/index.json b/compiler/esy.lock/index.json index 67964201fe..5c3aa86510 100644 --- a/compiler/esy.lock/index.json +++ b/compiler/esy.lock/index.json @@ -1,5 +1,5 @@ { - "checksum": "6214b6aa62d4cbc7e9ef4a09ba160489", + "checksum": "a88b8197b2d0d5c114e3cfd73b8a2ebd", "root": "@grain/compiler@link-dev:./esy.json", "node": { "ocaml@4.11.0@d41d8cd9": { @@ -1347,6 +1347,7 @@ "@opam/ppx_sexp_conv@opam:v0.14.3@1ee195f4", "@opam/ppx_deriving_yojson@opam:3.6.1@faf11a7c", "@opam/ocamlgraph@opam:2.0.0@3dafd524", + "@opam/js_of_ocaml@opam:3.9.0@db27df96", "@opam/grain_dypgen@opam:0.2@ef01d3b4", "@opam/fs@github:facebookexperimental/reason-native:fs.opam#a33f1528a6dd86c67f365e226c81312733181c87@d41d8cd9", "@opam/fp@github:facebookexperimental/reason-native:fp.opam#a33f1528a6dd86c67f365e226c81312733181c87@d41d8cd9", diff --git a/compiler/grainc/dune b/compiler/grainc/dune index 92a507b529..f70c9fc4e3 100644 --- a/compiler/grainc/dune +++ b/compiler/grainc/dune @@ -12,7 +12,12 @@ (rule (target grainc_js.re) (action - (copy %{dep:grainc.re} %{target}))) + ; This cats the jsoo_header.re to the top of grainc.re to build grainc_js.re + (with-stdout-to + %{target} + (progn + (cat %{dep:jsoo_header.re}) + (cat %{dep:grainc.re}))))) (executable (name grainc_js) @@ -21,7 +26,7 @@ (modules grainc_js) ; exe is only here so dune-build-info works (modes exe js) - (libraries grain grain_diagnostics binaryen.js dune-build-info) + (libraries grain grain_diagnostics binaryen.js dune-build-info js_of_ocaml) (js_of_ocaml (flags --no-sourcemap --no-extern-fs --quiet) (javascript_files runtime.js jsoo_hacks.js))) diff --git a/compiler/grainc/jsoo_hacks.js b/compiler/grainc/jsoo_hacks.js index 5bc4590ee0..97765e794b 100644 --- a/compiler/grainc/jsoo_hacks.js +++ b/compiler/grainc/jsoo_hacks.js @@ -1,52 +1,3 @@ -//Provides: caml_sys_open -//Requires: caml_raise_sys_error, caml_global_data -//Requires: caml_create_bytes,MlFakeFile,MlNodeFile -//Requires: js_print_stderr, js_print_stdout -//Requires: caml_std_output -//Requires: resolve_fs_device -//Requires: caml_jsbytes_of_string -function caml_sys_open_internal(idx, output, file, flags) { - if (caml_global_data.fds === undefined) caml_global_data.fds = new Array(); - flags = flags ? flags : {}; - var info = {}; - info.file = file; - info.offset = flags.append ? file.length() : 0; - info.flags = flags; - info.output = output; - caml_global_data.fds[idx] = info; - if (!caml_global_data.fd_last_idx || idx > caml_global_data.fd_last_idx) - caml_global_data.fd_last_idx = idx; - return idx; -} -function caml_sys_open(name, flags, _perms) { - var f = {}; - while (flags) { - switch (flags[1]) { - case 0: f.rdonly = 1; break; - case 1: f.wronly = 1; break; - case 2: f.append = 1; break; - case 3: f.create = 1; break; - case 4: f.truncate = 1; break; - case 5: f.excl = 1; break; - case 6: f.binary = 1; break; - case 7: f.text = 1; break; - case 8: f.nonblock = 1; break; - } - flags = flags[2]; - } - if (f.rdonly && f.wronly) - caml_raise_sys_error(caml_jsbytes_of_string(name) + " : flags Open_rdonly and Open_wronly are not compatible"); - if (f.text && f.binary) - caml_raise_sys_error(caml_jsbytes_of_string(name) + " : flags Open_text and Open_binary are not compatible"); - var root = resolve_fs_device(name); - var file = root.device.open(root.rest, f); - var idx = caml_global_data.fd_last_idx ? caml_global_data.fd_last_idx : 0; - return caml_sys_open_internal(idx + 1, caml_std_output, file, f); -} -caml_sys_open_internal(0, caml_std_output, new MlNodeFile(0)); //stdin -caml_sys_open_internal(1, js_print_stdout, new MlFakeFile(caml_create_bytes(0))); //stdout -caml_sys_open_internal(2, js_print_stderr, new MlFakeFile(caml_create_bytes(0))); //stderr - //Provides: caml_make_path //Requires: caml_current_dir //Requires: caml_jsstring_of_string diff --git a/compiler/grainc/jsoo_header.re b/compiler/grainc/jsoo_header.re new file mode 100644 index 0000000000..fcaa586cc8 --- /dev/null +++ b/compiler/grainc/jsoo_header.re @@ -0,0 +1,6 @@ +// This code is used to refill the mock stdin with the actual 0 file descriptor +Js_of_ocaml.Sys_js.set_channel_filler(stdin, () => { + Js_of_ocaml.Js.to_string( + Js_of_ocaml.Js.Unsafe.js_expr("require('fs').readFileSync(0, 'utf8')"), + ) +});