Skip to content

Commit a60db88

Browse files
committed
Lib: Give access to js runtime value
1 parent 02d2403 commit a60db88

File tree

16 files changed

+269
-0
lines changed

16 files changed

+269
-0
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* Compiler/wasm: make the type of some Wasm primitives more precise (#2100)
88
* Compiler: reference unboxing (#1958)
99
* Runtime: improved handling of NaNs (#2110)
10+
* Lib: allow to reference values from the runtime (#2086)
1011

1112
## Bug fixes
1213
* Compiler: fix purity of comparison functions (again) (#2092)

compiler/lib-wasm/gc_target.ml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1932,6 +1932,22 @@ let internal_primitives =
19321932
in
19331933
let l = List.map ~f:transl_prim_arg vl in
19341934
JavaScript.invoke_fragment name l);
1935+
register "caml_jsoo_runtime" (fun _ l ->
1936+
match l with
1937+
| [ Pc (String name) ] when J.is_ident name ->
1938+
let* x =
1939+
register_import
1940+
~import_module:"js"
1941+
~name
1942+
(Global { mut = false; typ = JavaScript.anyref })
1943+
in
1944+
let* wrap =
1945+
register_import
1946+
~name:"wrap"
1947+
(Fun { params = [ JavaScript.anyref ]; result = [ Type.value ] })
1948+
in
1949+
return (W.Call (wrap, [ GlobalGet x ]))
1950+
| _ -> failwith "Jsoo_runtime.Sys.external_ expects a string literal.");
19351951
!l
19361952

19371953
let externref = W.Ref { nullable = true; typ = Extern }

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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,11 @@ let specialize_instr opt_count ~target info i =
188188
incr opt_count;
189189
Let (x, Prim (Extern "%direct_int_mod", [ y; z ]))
190190
| _ -> i)
191+
| Let (x, Prim (Extern "caml_jsoo_runtime", [ nm ])), _ -> (
192+
match the_string_of info nm with
193+
| Some nm when Javascript.is_ident nm ->
194+
Let (x, Prim (Extern "caml_jsoo_runtime", [ Pc (String nm) ]))
195+
| _ -> i)
191196
| _, _ -> i
192197

193198
let skip_event cont (Event _ :: l | l) = cont l

compiler/tests-check-prim/main.4.14.output

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ caml_int64_or_native
1414
caml_int64_sub_native
1515
caml_int64_xor_native
1616
caml_int_as_pointer
17+
caml_jsoo_runtime
1718
caml_reset_afl_instrumentation
1819
debugger
1920

compiler/tests-check-prim/main.5.4.output

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ From main.bc:
55
caml_assume_no_perform
66
caml_continuation_use
77
caml_int_as_pointer
8+
caml_jsoo_runtime
89
caml_reset_afl_instrumentation
910
debugger
1011

compiler/tests-check-prim/unix-Unix.4.14.output

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ caml_int64_or_native
1414
caml_int64_sub_native
1515
caml_int64_xor_native
1616
caml_int_as_pointer
17+
caml_jsoo_runtime
1718
caml_reset_afl_instrumentation
1819
caml_unix_map_file_bytecode
1920
debugger

compiler/tests-check-prim/unix-Unix.5.4.output

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ From unix.bc:
55
caml_assume_no_perform
66
caml_continuation_use
77
caml_int_as_pointer
8+
caml_jsoo_runtime
89
caml_reset_afl_instrumentation
910
caml_unix_accept
1011
caml_unix_alarm

compiler/tests-check-prim/unix-Win32.4.14.output

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ caml_int64_or_native
1414
caml_int64_sub_native
1515
caml_int64_xor_native
1616
caml_int_as_pointer
17+
caml_jsoo_runtime
1718
caml_reset_afl_instrumentation
1819
caml_unix_map_file_bytecode
1920
debugger
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
Missing
2+
-------
3+
4+
From unix.bc:
5+
caml_assume_no_perform
6+
caml_continuation_use
7+
caml_int_as_pointer
8+
caml_jsoo_runtime
9+
caml_reset_afl_instrumentation
10+
caml_unix_accept
11+
caml_unix_bind
12+
caml_unix_clear_close_on_exec
13+
caml_unix_clear_nonblock
14+
caml_unix_connect
15+
caml_unix_create_process
16+
caml_unix_dup
17+
caml_unix_dup2
18+
caml_unix_environment
19+
caml_unix_execv
20+
caml_unix_execve
21+
caml_unix_execvp
22+
caml_unix_execvpe
23+
caml_unix_filedescr_of_channel
24+
caml_unix_getaddrinfo
25+
caml_unix_gethostbyaddr
26+
caml_unix_gethostbyname
27+
caml_unix_gethostname
28+
caml_unix_getnameinfo
29+
caml_unix_getpeername
30+
caml_unix_getpid
31+
caml_unix_getprotobyname
32+
caml_unix_getprotobynumber
33+
caml_unix_getservbyname
34+
caml_unix_getservbyport
35+
caml_unix_getsockname
36+
caml_unix_getsockopt
37+
caml_unix_listen
38+
caml_unix_lockf
39+
caml_unix_map_file_bytecode
40+
caml_unix_pipe
41+
caml_unix_putenv
42+
caml_unix_realpath
43+
caml_unix_recv
44+
caml_unix_recvfrom
45+
caml_unix_select
46+
caml_unix_send
47+
caml_unix_sendto
48+
caml_unix_set_close_on_exec
49+
caml_unix_set_nonblock
50+
caml_unix_setsockopt
51+
caml_unix_shutdown
52+
caml_unix_sleep
53+
caml_unix_socket
54+
caml_unix_socketpair
55+
caml_unix_string_of_inet_addr
56+
caml_unix_system
57+
caml_unix_terminate_process
58+
caml_unix_waitpid
59+
debugger
60+
61+
Unused
62+
-------
63+
64+
From +array.js:
65+
caml_check_bound
66+
67+
From +bigarray.js:
68+
caml_ba_create_from (deprecated)
69+
caml_ba_init
70+
71+
From +bigstring.js:
72+
caml_bigstring_blit_ba_to_ba
73+
caml_bigstring_blit_ba_to_bytes
74+
caml_bigstring_blit_bytes_to_ba
75+
caml_bigstring_blit_string_to_ba
76+
caml_bigstring_memcmp
77+
caml_hash_mix_bigstring
78+
79+
From +effect.js:
80+
jsoo_effect_not_supported
81+
82+
From +fs.js:
83+
caml_ba_map_file
84+
caml_ba_map_file_bytecode
85+
caml_fs_init
86+
jsoo_create_file
87+
jsoo_create_file_extern
88+
89+
From +graphics.js:
90+
caml_gr_arc_aux
91+
caml_gr_blit_image
92+
caml_gr_clear_graph
93+
caml_gr_close_graph
94+
caml_gr_close_subwindow
95+
caml_gr_create_image
96+
caml_gr_current_x
97+
caml_gr_current_y
98+
caml_gr_display_mode
99+
caml_gr_doc_of_state
100+
caml_gr_draw_arc
101+
caml_gr_draw_char
102+
caml_gr_draw_image
103+
caml_gr_draw_rect
104+
caml_gr_draw_str
105+
caml_gr_draw_string
106+
caml_gr_dump_image
107+
caml_gr_fill_arc
108+
caml_gr_fill_poly
109+
caml_gr_fill_rect
110+
caml_gr_lineto
111+
caml_gr_make_image
112+
caml_gr_moveto
113+
caml_gr_open_graph
114+
caml_gr_open_subwindow
115+
caml_gr_plot
116+
caml_gr_point_color
117+
caml_gr_remember_mode
118+
caml_gr_resize_window
119+
caml_gr_set_color
120+
caml_gr_set_font
121+
caml_gr_set_line_width
122+
caml_gr_set_text_size
123+
caml_gr_set_window_title
124+
caml_gr_sigio_handler
125+
caml_gr_sigio_signal
126+
caml_gr_size_x
127+
caml_gr_size_y
128+
caml_gr_state
129+
caml_gr_state_create
130+
caml_gr_state_get
131+
caml_gr_state_init
132+
caml_gr_state_set
133+
caml_gr_synchronize
134+
caml_gr_text_size
135+
caml_gr_wait_event
136+
caml_gr_window_id
137+
138+
From +hash.js:
139+
caml_hash_mix_int64
140+
141+
From +ints.js:
142+
caml_div
143+
caml_mod
144+
145+
From +jslib.js:
146+
caml_is_js
147+
caml_trampoline
148+
caml_trampoline_return
149+
caml_wrap_exception
150+
151+
From +marshal.js:
152+
caml_marshal_constants
153+
154+
From +mlBytes.js:
155+
caml_array_of_bytes (deprecated)
156+
caml_array_of_string (deprecated)
157+
caml_bytes_of_utf16_jsstring
158+
caml_new_string (deprecated)
159+
caml_string_concat
160+
caml_to_js_string (deprecated)
161+
162+
From +runtime_events.js:
163+
caml_runtime_events_create_cursor
164+
caml_runtime_events_free_cursor
165+
caml_runtime_events_read_poll
166+
caml_runtime_events_user_resolve
167+
168+
From +stdlib.js:
169+
caml_is_printable
170+
caml_maybe_print_stats
171+
172+
From +sys.js:
173+
caml_fatal_uncaught_exception
174+
caml_format_exception
175+
caml_is_special_exception
176+
caml_set_static_env
177+
caml_sys_const_naked_pointers_checked
178+
179+
From +toplevel.js:
180+
jsoo_get_runtime_aliases
181+
jsoo_toplevel_init_compile
182+
jsoo_toplevel_init_reloc
183+
184+
From +unix.js:
185+
caml_strerror
186+
caml_unix_fchmod
187+
caml_unix_getegid
188+
caml_unix_geteuid
189+
caml_unix_getgid
190+
caml_unix_getpwnam
191+
caml_unix_getuid
192+
caml_unix_rewinddir
193+
unix_error_message
194+

0 commit comments

Comments
 (0)