Skip to content

Commit 03b1921

Browse files
committed
Wasm runtime: additional primitives
1 parent 8b78a78 commit 03b1921

File tree

8 files changed

+198
-0
lines changed

8 files changed

+198
-0
lines changed

runtime/wasm/array.wat

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,90 @@
315315
(br $fill))))
316316
(local.get $a))))
317317

318+
(func (export "caml_float_array_concat") (param (ref eq)) (result (ref eq))
319+
(local $i i32) (local $len i32)
320+
(local $l (ref eq)) (local $v (ref eq))
321+
(local $b (ref $block))
322+
(local $fa (ref $float_array)) (local $fa' (ref $float_array))
323+
(local.set $l (local.get 0))
324+
(local.set $len (i32.const 0))
325+
(loop $compute_length
326+
(drop (block $exit (result (ref eq))
327+
(local.set $b
328+
(br_on_cast_fail $exit (ref eq) (ref $block) (local.get $l)))
329+
(local.set $v (array.get $block (local.get $b) (i32.const 1)))
330+
(local.set $len
331+
(i32.add (local.get $len)
332+
(array.len (ref.cast (ref $float_array) (local.get $v)))))
333+
(local.set $l (array.get $block (local.get $b) (i32.const 2)))
334+
(br $compute_length))))
335+
(local.set $fa
336+
(array.new $float_array (f64.const 0) (local.get $len)))
337+
(local.set $l (local.get 0))
338+
(local.set $i (i32.const 0))
339+
(loop $fill
340+
(drop (block $exit (result (ref eq))
341+
(local.set $b
342+
(br_on_cast_fail $exit (ref eq) (ref $block)
343+
(local.get $l)))
344+
(local.set $l (array.get $block (local.get $b) (i32.const 2)))
345+
(drop (block $not_float (result (ref eq))
346+
(local.set $fa'
347+
(br_on_cast_fail $not_float (ref eq) (ref $float_array)
348+
(array.get $block (local.get $b) (i32.const 1))))
349+
(local.set $len (array.len (local.get $fa')))
350+
(array.copy $float_array $float_array
351+
(local.get $fa) (local.get $i)
352+
(local.get $fa') (i32.const 0)
353+
(local.get $len))
354+
(local.set $i (i32.add (local.get $i) (local.get $len)))
355+
(br $fill)))
356+
(br $fill))))
357+
(local.get $fa))
358+
359+
(func (export "caml_uniform_array_concat") (param (ref eq)) (result (ref eq))
360+
(local $i i32) (local $len i32)
361+
(local $l (ref eq)) (local $v (ref eq))
362+
(local $b (ref $block))
363+
(local $a (ref $block)) (local $a' (ref $block))
364+
(local.set $l (local.get 0))
365+
(local.set $len (i32.const 0))
366+
(loop $compute_length
367+
(drop (block $exit (result (ref eq))
368+
(local.set $b
369+
(br_on_cast_fail $exit (ref eq) (ref $block) (local.get $l)))
370+
(local.set $v (array.get $block (local.get $b) (i32.const 1)))
371+
(local.set $len
372+
(i32.add (local.get $len)
373+
(i32.sub
374+
(array.len (ref.cast (ref $block) (local.get $v)))
375+
(i32.const 1))))
376+
(local.set $l (array.get $block (local.get $b) (i32.const 2)))
377+
(br $compute_length))))
378+
(local.set $a
379+
(array.new $block (ref.i31 (i32.const 0))
380+
(i32.add (local.get $len) (i32.const 1))))
381+
(local.set $l (local.get 0))
382+
(local.set $i (i32.const 1))
383+
(loop $fill
384+
(drop (block $exit (result (ref eq))
385+
(local.set $b
386+
(br_on_cast_fail $exit (ref eq) (ref $block)
387+
(local.get $l)))
388+
(local.set $a'
389+
(ref.cast (ref $block)
390+
(array.get $block (local.get $b) (i32.const 1))))
391+
(local.set $len
392+
(i32.sub (array.len (local.get $a')) (i32.const 1)))
393+
(array.copy $block $block
394+
(local.get $a) (local.get $i)
395+
(local.get $a') (i32.const 1)
396+
(local.get $len))
397+
(local.set $i (i32.add (local.get $i) (local.get $len)))
398+
(local.set $l (array.get $block (local.get $b) (i32.const 2)))
399+
(br $fill))))
400+
(local.get $a))
401+
318402
(func $caml_floatarray_blit (export "caml_floatarray_blit")
319403
(param $a1 (ref eq)) (param $i1 (ref eq))
320404
(param $a2 (ref eq)) (param $i2 (ref eq))

runtime/wasm/domain.wat

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,31 @@
3838
(else
3939
(ref.i31 (i32.const 0)))))
4040

41+
(func (export "caml_atomic_cas_field")
42+
(param $ref (ref eq)) (param $i (ref eq)) (param $o (ref eq))
43+
(param $n (ref eq)) (result (ref eq))
44+
(local $b (ref $block))
45+
(local $j i32)
46+
(local.set $j
47+
(i32.add (i31.get_u (ref.cast (ref i31) (local.get $i))) (i32.const 1)))
48+
(local.set $b (ref.cast (ref $block) (local.get $ref)))
49+
(if (result (ref eq))
50+
(ref.eq (array.get $block (local.get $b) (local.get $j))
51+
(local.get $o))
52+
(then
53+
(array.set $block (local.get $b) (local.get $j) (local.get $n))
54+
(ref.i31 (i32.const 1)))
55+
(else
56+
(ref.i31 (i32.const 0)))))
57+
4158
(func (export "caml_atomic_load") (param (ref eq)) (result (ref eq))
4259
(array.get $block (ref.cast (ref $block) (local.get 0)) (i32.const 1)))
4360

61+
(func (export "caml_atomic_load_field")
62+
(param $b (ref eq)) (param $i (ref eq)) (result (ref eq))
63+
(array.get $block (ref.cast (ref $block) (local.get $b))
64+
(i32.add (i31.get_u (ref.cast (ref i31) (local.get $i))) (i32.const 1))))
65+
4466
(func (export "caml_atomic_fetch_add")
4567
(param $ref (ref eq)) (param $i (ref eq)) (result (ref eq))
4668
(local $b (ref $block))
@@ -52,6 +74,21 @@
5274
(i31.get_s (ref.cast (ref i31) (local.get $i))))))
5375
(local.get $old))
5476

77+
(func (export "caml_atomic_fetch_add_field")
78+
(param $ref (ref eq)) (param $i (ref eq)) (param $n (ref eq))
79+
(result (ref eq))
80+
(local $b (ref $block))
81+
(local $old (ref eq))
82+
(local $j i32)
83+
(local.set $j
84+
(i32.add (i31.get_u (ref.cast (ref i31) (local.get $i))) (i32.const 1)))
85+
(local.set $b (ref.cast (ref $block) (local.get $ref)))
86+
(local.set $old (array.get $block (local.get $b) (local.get $j)))
87+
(array.set $block (local.get $b) (local.get $j)
88+
(ref.i31 (i32.add (i31.get_s (ref.cast (ref i31) (local.get $old)))
89+
(i31.get_s (ref.cast (ref i31) (local.get $n))))))
90+
(local.get $old))
91+
5592
(func (export "caml_atomic_exchange")
5693
(param $ref (ref eq)) (param $v (ref eq)) (result (ref eq))
5794
(local $b (ref $block))
@@ -61,6 +98,19 @@
6198
(array.set $block (local.get $b) (i32.const 1) (local.get $v))
6299
(local.get $r))
63100

101+
(func (export "caml_atomic_exchange_field")
102+
(param $ref (ref eq)) (param $i (ref eq)) (param $v (ref eq))
103+
(result (ref eq))
104+
(local $b (ref $block))
105+
(local $r (ref eq))
106+
(local $j i32)
107+
(local.set $j
108+
(i32.add (i31.get_u (ref.cast (ref i31) (local.get $i))) (i32.const 1)))
109+
(local.set $b (ref.cast (ref $block) (local.get $ref)))
110+
(local.set $r (array.get $block (local.get $b) (local.get $j)))
111+
(array.set $block (local.get $b) (local.get $j) (local.get $v))
112+
(local.get $r))
113+
64114
(func (export "caml_atomic_make_contended")
65115
(param $v (ref eq)) (result (ref eq))
66116
(array.new_fixed $block 2 (ref.i31 (i32.const 0)) (local.get $v)))

runtime/wasm/fs.wat

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616
;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
1717

1818
(module
19+
(import "bindings" "on_windows" (global $on_windows i32))
1920
(import "bindings" "getcwd" (func $getcwd (result anyref)))
2021
(import "bindings" "chdir" (func $chdir (param anyref)))
2122
(import "bindings" "mkdir" (func $mkdir (param anyref) (param i32)))
2223
(import "bindings" "rmdir" (func $rmdir (param anyref)))
2324
(import "bindings" "unlink" (func $unlink (param anyref)))
25+
(import "bindings" "tmpdir" (func $tmpdir (result anyref)))
2426
(import "bindings" "read_dir"
2527
(func $read_dir (param anyref) (result (ref extern))))
2628
(import "bindings" "file_exists"
@@ -172,6 +174,12 @@
172174
(call $caml_handle_sys_error (pop externref))
173175
(return (ref.i31 (i32.const 0))))))
174176

177+
(func (export "caml_sys_temp_dir_name") (param (ref eq)) (result (ref eq))
178+
(if (global.get $on_windows)
179+
(then
180+
(return_call $caml_string_of_jsstring (call $wrap (call $tmpdir)))))
181+
(@string ""))
182+
175183
(func (export "caml_mount_autoload")
176184
(param (ref eq) (ref eq)) (result (ref eq))
177185
(ref.i31 (i32.const 0)))

runtime/wasm/gc.wat

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
1616
;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
1717

1818
(module
19+
(import "obj" "caml_callback_1"
20+
(func $caml_callback_1
21+
(param (ref eq)) (param (ref eq)) (result (ref eq))))
22+
1923
(type $float (struct (field f64)))
2024
(type $block (array (mut (ref eq))))
2125

@@ -118,4 +122,12 @@
118122

119123
(func (export "caml_eventlog_resume") (param (ref eq)) (result (ref eq))
120124
(ref.i31 (i32.const 0)))
125+
126+
(func (export "caml_ml_gc_ramp_up") (param $f (ref eq)) (result (ref eq))
127+
(array.new_fixed $block 3 (ref.i31 (i32.const 0))
128+
(call $caml_callback_1 (local.get $f) (ref.i31 (i32.const 0)))
129+
(ref.i31 (i32.const 0))))
130+
131+
(func (export "caml_ml_gc_ramp_down") (param (ref eq)) (result (ref eq))
132+
(ref.i31 (i32.const 0)))
121133
)

runtime/wasm/io.wat

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,9 @@
260260
(call $caml_handle_sys_error (pop externref))))
261261
(ref.i31 (i32.const 0)))
262262

263+
(func (export "caml_sys_io_buffer_size") (param (ref eq)) (result (ref eq))
264+
(ref.i31 (global.get $IO_BUFFER_SIZE)))
265+
263266
(func (export "caml_ml_set_channel_name")
264267
(param (ref eq)) (param (ref eq)) (result (ref eq))
265268
(ref.i31 (i32.const 0)))

runtime/wasm/obj.wat

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,26 @@
176176
(return (ref.i31 (i32.const 0)))))
177177
(unreachable))
178178

179+
(func (export "caml_alloc_dummy_lazy") (param (ref eq)) (result (ref eq))
180+
(array.new_fixed $block 2 (ref.i31 (i32.const 0)) (ref.i31 (i32.const 0))))
181+
182+
(func (export "caml_update_dummy_lazy")
183+
(param $dummy (ref eq)) (param $newval (ref eq)) (result (ref eq))
184+
(local $tag i32)
185+
(local $b (ref $block))
186+
(local.set $tag
187+
(i31.get_s (ref.cast (ref i31) (call $caml_obj_tag (local.get $newval)))))
188+
(block $update
189+
(br_if $update (i32.eq (local.get $tag) (global.get $lazy_tag)))
190+
(br_if $update (i32.eq (local.get $tag) (global.get $forcing_tag)))
191+
(br_if $update (i32.eq (local.get $tag) (global.get $forward_tag)))
192+
(local.set $b (ref.cast (ref $block) (local.get $dummy)))
193+
(array.set $block (local.get $b) (i32.const 0)
194+
(ref.i31 (global.get $forward_tag)))
195+
(array.set $block (local.get $b) (i32.const 1) (local.get $newval))
196+
(return (ref.i31 (i32.const 0))))
197+
(return_call $caml_update_dummy (local.get $dummy) (local.get $newval)))
198+
179199
(func $caml_obj_dup (export "caml_obj_dup")
180200
(param (ref eq)) (result (ref eq))
181201
(local $orig (ref $block)) (local $res (ref $block))

runtime/wasm/runtime.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,7 @@
538538
}
539539
fs.renameSync(o, n);
540540
},
541+
tmpdir: () => require("node:os").tmpdir(),
541542
start_fiber: (x) => start_fiber(x),
542543
suspend_fiber: make_suspending((f, env) => new Promise((k) => f(k, env))),
543544
resume_fiber: (k, v) => k(v),

runtime/wasm/sys.wat

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,18 @@
7878
(call $caml_raise_not_found)))
7979
(return_call $caml_string_of_jsstring (call $wrap (local.get $res))))
8080

81+
(func (export "caml_sys_getenv_opt")
82+
(param (ref eq)) (result (ref eq))
83+
(local $res anyref)
84+
(local.set $res
85+
(call $getenv
86+
(call $unwrap (call $caml_jsstring_of_string (local.get 0)))))
87+
(if (i32.eqz (call $jsstring_test (local.get $res)))
88+
(then
89+
(return (ref.i31 (i32.const 0)))))
90+
(array.new_fixed $block 2 (ref.i31 (i32.const 0))
91+
(call $caml_string_of_jsstring (call $wrap (local.get $res)))))
92+
8193
(func (export "caml_sys_argv") (param (ref eq)) (result (ref eq))
8294
;; ZZZ
8395
(call $caml_js_to_string_array (call $argv)))
@@ -179,6 +191,14 @@
179191
(param (ref eq) (ref eq)) (result (ref eq))
180192
(ref.i31 (i32.const 0)))
181193

194+
(func (export "caml_sys_convert_signal_number")
195+
(param $signo (ref eq)) (result (ref eq))
196+
(local.get $signo))
197+
198+
(func (export "caml_sys_rev_convert_signal_number")
199+
(param $signo (ref eq)) (result (ref eq))
200+
(local.get $signo))
201+
182202
(global $caml_runtime_warnings (mut i32) (i32.const 0))
183203

184204
(func (export "caml_ml_enable_runtime_warnings")

0 commit comments

Comments
 (0)