Skip to content

Commit

Permalink
Runtime: allow dynlink of precompiled js with separate compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
hhugo committed Sep 19, 2024
1 parent 693fa62 commit 95e71a2
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
35 changes: 33 additions & 2 deletions compiler/tests-dynlink-js/dune
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
(name main)
(modules main)
(libraries js_of_ocaml)
(modes byte))
(link_flags
(:standard -linkall))
(modes js byte))

(rule
(target main.js)
Expand All @@ -19,15 +21,44 @@
(action
(run %{bin:js_of_ocaml} %{dep:./plugin.cmo})))

(rule
(target plugin2.cma)
(action
(run
%{bin:ocamlc}
-a
%{dep:./plugin2a.ml}
%{dep:./plugin2b.ml}
-o
plugin2.cma)))

(rule
(target plugin2.js)
(action
(run %{bin:js_of_ocaml} %{dep:./plugin2.cma})))

(rule
(target main.out)
(deps plugin.js)
(deps plugin.js plugin2.js)
(action
(with-outputs-to
%{target}
(run %{bin:node} %{dep:./main.js}))))

(rule
(target main.out2)
(deps plugin.js plugin2.js)
(action
(with-outputs-to
%{target}
(run %{bin:node} %{dep:./main.bc.js}))))

(rule
(alias runtest)
(action
(diff main.out.expected main.out)))

(rule
(alias runtest)
(action
(diff main.out.expected main.out2)))
2 changes: 2 additions & 0 deletions compiler/tests-dynlink-js/main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ let require s =
Js.Unsafe.fun_call (Js.Unsafe.js_expr "require") [| Js.Unsafe.inject (Js.string s) |]

let () = require "./plugin.js"

let () = require "./plugin2.js"
11 changes: 9 additions & 2 deletions runtime/stdlib.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,15 @@ var caml_global_data = [0];
//Requires: caml_jsstring_of_string
function caml_build_symbols(symb) {
var r = {};
var max = -1;
if (symb) {
for (var i = 1; i < symb.length; i++) {
r[caml_jsstring_of_string(symb[i][1])] = symb[i][2];
var idx = symb[i][2];
max = Math.max(max, idx);
r[caml_jsstring_of_string(symb[i][1])] = idx;
}
}
r.next_idx = max + 1;
return r;
}

Expand All @@ -173,7 +177,10 @@ function caml_register_global(n, v, name_opt) {
var nid = caml_global_data.symidx[name];
if (nid >= 0) n = nid;
else {
caml_failwith("caml_register_global: cannot locate " + name);
// The unit is unknown, this can happen when dynlinking a precompiled js,
// let's allocate a fresh idx.
var n = caml_global_data.symidx.next_idx++;
caml_global_data.symidx[name] = n;
}
}
}
Expand Down

0 comments on commit 95e71a2

Please sign in to comment.