Skip to content

Commit 1528d17

Browse files
committed
WIP
1 parent 4197fcb commit 1528d17

File tree

7 files changed

+65
-4
lines changed

7 files changed

+65
-4
lines changed

compiler/lib/flow.ml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,13 @@ module Info = struct
4747
}
4848

4949
let def t x =
50-
match t.info_defs.(Code.Var.idx x) with
51-
| Phi _ | Param -> None
52-
| Expr x -> Some x
50+
let idx = Code.Var.idx x in
51+
if Array.length t.info_defs <= idx
52+
then None
53+
else
54+
match t.info_defs.(idx) with
55+
| Phi _ | Param -> None
56+
| Expr x -> Some x
5357

5458
let possibly_mutable t x = Code.Var.ISet.mem t.info_possibly_mutable x
5559

compiler/lib/generate.ml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1547,6 +1547,17 @@ let rec translate_expr ctx loc x e level : (_ * J.statement_list) Expr_builder.t
15471547
| Some s -> Printf.sprintf ", file %S" s)
15481548
pi.Parse_info.line
15491549
pi.Parse_info.col))
1550+
| Extern "caml_jsoo_runtime", [ Pc (String nm) ] when J.is_ident nm ->
1551+
let prim = Share.get_prim (runtime_fun ctx) nm ctx.Ctx.share in
1552+
return prim
1553+
| Extern "caml_jsoo_runtime", [ (Pc _ | Pv _) ] ->
1554+
failwith
1555+
(Printf.sprintf
1556+
"%scaml_jsoo_runtime expects a string literal."
1557+
(match (loc : J.location) with
1558+
| Pi { name = Some name; col; line; _ } ->
1559+
Printf.sprintf "%s:%d:%d: " name line col
1560+
| Pi _ | N | U -> ""))
15501561
| Extern "%js_array", l ->
15511562
let* args = list_map (fun x -> access' ~ctx x) l in
15521563
return (J.array args)

compiler/lib/specialize_js.ml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,35 @@ let specialize_instrs ~target opt_count info l =
288288
match l with
289289
| [] -> List.rev acc
290290
| i :: r -> (
291+
let i =
292+
match i with
293+
| Let (x, Apply { f; args; exact = false }) -> (
294+
match Info.def info f with
295+
| None -> i
296+
| Some (Prim (Extern "caml_jsoo_runtime", [ name ])) -> (
297+
let name =
298+
match name with
299+
| Pc (String name) -> Some name
300+
| Pc _ -> None
301+
| Pv x -> (
302+
match Info.def info x with
303+
| Some (Constant (String name)) -> Some name
304+
| Some _ | None -> None)
305+
in
306+
match name with
307+
| None -> i
308+
| Some name -> (
309+
let name = Primitive.resolve name in
310+
match Primitive.arity name with
311+
| exception Not_found -> i
312+
| n ->
313+
if List.compare_length_with args ~len:n = 0
314+
then
315+
Let (x, Prim (Extern name, List.map args ~f:(fun x -> Pv x)))
316+
else i))
317+
| Some _ -> i)
318+
| _ -> i
319+
in
291320
(* We make bound checking explicit. Then, we can remove duplicated
292321
bound checks. Also, it appears to be more efficient to inline
293322
the array access. The bound checking function returns the array,

examples/boulderdash/boulderdash.ml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,4 +510,12 @@ let start _ =
510510
Dom.appendChild body div;
511511
Lwt.return ()
512512

513-
let () = Lwt.async start
513+
let () =
514+
let p : Js.js_string Js.t = Jsoo_runtime.Sys.external_ "process" in
515+
let o : _ Js.t = Jsoo_runtime.Sys.external_ "obj" in
516+
let del : 'a -> Jsoo_runtime.Js.t -> unit =
517+
Jsoo_runtime.Sys.external_ "caml_js_delete"
518+
in
519+
del o (Jsoo_runtime.Js.string "process");
520+
print_endline (Js.to_string p);
521+
Lwt.async start

examples/boulderdash/custom.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//Provides: process
2+
var process = "process"
3+
4+
5+
//Provides: obj
6+
var obj = { "process": 42 }

examples/boulderdash/dune

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
(modes js wasm)
55
(js_of_ocaml
66
(compilation_mode separate)
7+
(javascript_files custom.js)
78
(build_runtime_flags :standard --file %{dep:maps.txt} --file maps))
89
(link_deps
910
(glob_files maps/*.map))

lib/runtime/jsoo_runtime.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ module Sys = struct
124124
external restore_channel : out_channel -> redirection -> unit
125125
= "caml_ml_channel_restore"
126126

127+
external external_ : string -> 'a = "caml_jsoo_runtime"
128+
127129
module Config = struct
128130
external use_js_string : unit -> bool = "caml_jsoo_flags_use_js_string"
129131

0 commit comments

Comments
 (0)