Skip to content
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

fix(cli): Rework JSOO hacks to ensure stdin always gets data #614

Merged
merged 1 commit into from
Apr 21, 2021
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 compiler/esy.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion compiler/esy.lock/index.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions compiler/grainc/dune
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)))
49 changes: 0 additions & 49 deletions compiler/grainc/jsoo_hacks.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 6 additions & 0 deletions compiler/grainc/jsoo_header.re
Original file line number Diff line number Diff line change
@@ -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')"),
)
});