Skip to content

Commit b99f325

Browse files
committed
Wasm runtime: use string syntactic sugar
1 parent c51c220 commit b99f325

25 files changed

+198
-435
lines changed

runtime/wasm/array.wat

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
(type $float (struct (field f64)))
2525
(type $float_array (array (mut f64)))
2626

27-
(data $Array_make "Array.make")
27+
(@string $Array_make "Array.make")
2828

2929
(global $empty_array (ref eq)
3030
(array.new_fixed $block 1 (ref.i31 (i32.const 0))))
@@ -34,10 +34,7 @@
3434
(local $sz i32) (local $b (ref $block)) (local $f f64)
3535
(local.set $sz (i31.get_s (ref.cast (ref i31) (local.get $n))))
3636
(if (i32.ge_u (local.get $sz) (i32.const 0xfffffff))
37-
(then
38-
(call $caml_invalid_argument
39-
(array.new_data $string $Array_make
40-
(i32.const 0) (i32.const 10)))))
37+
(then (call $caml_invalid_argument (global.get $Array_make))))
4138
(if (i32.eqz (local.get $sz)) (then (return (global.get $empty_array))))
4239
(drop (block $not_float (result (ref eq))
4340
(local.set $f
@@ -56,10 +53,7 @@
5653
(local $sz i32) (local $f f64)
5754
(local.set $sz (i31.get_s (ref.cast (ref i31) (local.get $n))))
5855
(if (i32.ge_u (local.get $sz) (i32.const 0x7ffffff))
59-
(then
60-
(call $caml_invalid_argument
61-
(array.new_data $string $Array_make
62-
(i32.const 0) (i32.const 10)))))
56+
(then (call $caml_invalid_argument (global.get $Array_make))))
6357
(if (i32.eqz (local.get $sz)) (then (return (global.get $empty_array))))
6458
(local.set $f
6559
(struct.get $float 0
@@ -73,10 +67,7 @@
7367
(local $sz i32)
7468
(local.set $sz (i31.get_s (ref.cast (ref i31) (local.get $n))))
7569
(if (i32.ge_u (local.get $sz) (i32.const 0x7ffffff))
76-
(then
77-
(call $caml_invalid_argument
78-
(array.new_data $string $Array_make
79-
(i32.const 0) (i32.const 10)))))
70+
(then (call $caml_invalid_argument (global.get $Array_make))))
8071
(if (i32.eqz (local.get $sz)) (then (return (global.get $empty_array))))
8172
(array.new $float_array (f64.const 0) (local.get $sz)))
8273

runtime/wasm/backtrace.wat

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,12 @@
3838
(param (ref eq)) (result (ref eq))
3939
(ref.i31 (i32.const 0)))
4040

41-
(data $raw_backtrace_slot_err
41+
(@string $raw_backtrace_slot_err
4242
"Printexc.get_raw_backtrace_slot: index out of bounds")
4343

4444
(func (export "caml_raw_backtrace_slot")
4545
(param (ref eq) (ref eq)) (result (ref eq))
46-
(call $caml_invalid_argument
47-
(array.new_data $string $raw_backtrace_slot_err
48-
(i32.const 0) (i32.const 52)))
46+
(call $caml_invalid_argument (global.get $raw_backtrace_slot_err))
4947
(ref.i31 (i32.const 0)))
5048

5149
(func (export "caml_convert_raw_backtrace_slot")

runtime/wasm/bigarray.wat

Lines changed: 26 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,7 @@
155155

156156
(global $bigarray_ops (export "bigarray_ops") (ref $custom_operations)
157157
(struct.new $custom_operations
158-
(array.new_fixed $string 9 ;; "_bigarr02"
159-
(i32.const 95) (i32.const 98) (i32.const 105) (i32.const 103)
160-
(i32.const 97) (i32.const 114) (i32.const 114) (i32.const 48)
161-
(i32.const 50))
158+
(@string "_bigarr02")
162159
(ref.func $caml_ba_compare)
163160
(ref.null $compare)
164161
(ref.func $bigarray_hash)
@@ -596,7 +593,7 @@
596593
(i32.mul (i32.add (i32.const 4) (local.get $num_dims)) (i32.const 4))
597594
(i32.mul (i32.add (i32.const 4) (local.get $num_dims)) (i32.const 8))))
598595

599-
(data $intern_overflow
596+
(@string $intern_overflow
600597
"input_value: cannot read bigarray with 64-bit OCaml ints")
601598

602599
(func $bigarray_deserialize
@@ -680,10 +677,7 @@
680677
(br $done))
681678
;; int
682679
(if (call $caml_deserialize_uint_1 (local.get $s))
683-
(then
684-
(call $caml_failwith
685-
(array.new_data $string $intern_overflow
686-
(i32.const 0) (i32.const 56))))))
680+
(then (call $caml_failwith (global.get $intern_overflow)))))
687681
;; int32
688682
(loop $loop
689683
(if (i32.lt_u (local.get $i) (local.get $len))
@@ -792,8 +786,8 @@
792786

793787
(global $CAML_BA_MAX_NUM_DIMS i32 (i32.const 16))
794788

795-
(data $ba_create_bad_dims "Bigarray.create: bad number of dimensions")
796-
(data $ba_create_negative_dim "Bigarray.create: negative dimension")
789+
(@string $ba_create_bad_dims "Bigarray.create: bad number of dimensions")
790+
(@string $ba_create_negative_dim "Bigarray.create: negative dimension")
797791

798792
(func (export "caml_ba_create")
799793
(param $vkind (ref eq)) (param $layout (ref eq)) (param $d (ref eq))
@@ -805,10 +799,7 @@
805799
(local.set $vdim (ref.cast (ref $block) (local.get $d)))
806800
(local.set $num_dims (i32.sub (array.len (local.get $vdim)) (i32.const 1)))
807801
(if (i32.gt_u (local.get $num_dims) (global.get $CAML_BA_MAX_NUM_DIMS))
808-
(then
809-
(call $caml_invalid_argument
810-
(array.new_data $string $ba_create_bad_dims
811-
(i32.const 0) (i32.const 41)))))
802+
(then (call $caml_invalid_argument (global.get $ba_create_bad_dims))))
812803
(local.set $dim
813804
(array.new $int_array (i32.const 0) (local.get $num_dims)))
814805
(local.set $i (i32.const 0))
@@ -823,8 +814,7 @@
823814
(if (i32.lt_s (local.get $n) (i32.const 0))
824815
(then
825816
(call $caml_invalid_argument
826-
(array.new_data $string $ba_create_negative_dim
827-
(i32.const 0) (i32.const 35)))))
817+
(global.get $ba_create_negative_dim))))
828818
(array.set $int_array
829819
(local.get $dim) (local.get $i) (local.get $n))
830820
(local.set $i (i32.add (local.get $i) (i32.const 1)))
@@ -838,8 +828,8 @@
838828
(local.get $kind)
839829
(i31.get_s (ref.cast (ref i31) (local.get $layout)))))
840830

841-
(data $ta_unsupported_kind "Typed_array.to_genarray: unsupported kind")
842-
(data $ta_too_large "Typed_array.to_genarray: too large")
831+
(@string $ta_unsupported_kind "Typed_array.to_genarray: unsupported kind")
832+
(@string $ta_too_large "Typed_array.to_genarray: too large")
843833

844834
(func (export "caml_ba_from_typed_array") (param (ref eq)) (result (ref eq))
845835
(local $data (ref extern))
@@ -850,18 +840,12 @@
850840
(ref.as_non_null (extern.convert_any (call $unwrap (local.get 0))))))
851841
(local.set $kind (call $ta_kind (local.get $data)))
852842
(if (i32.lt_s (local.get $kind) (i32.const 0))
853-
(then
854-
(call $caml_invalid_argument
855-
(array.new_data $string $ta_unsupported_kind
856-
(i32.const 0) (i32.const 41)))))
843+
(then (call $caml_invalid_argument (global.get $ta_unsupported_kind))))
857844
(if (i32.eq (local.get $kind) (i32.const 14)) ;; Uint8ClampedArray
858845
(then (local.set $kind (i32.const 3))))
859846
(local.set $len (call $ta_length (local.get $data)))
860847
(if (i32.lt_s (local.get $len) (i32.const 0))
861-
(then
862-
(call $caml_invalid_argument
863-
(array.new_data $string $ta_too_large
864-
(i32.const 0) (i32.const 34)))))
848+
(then (call $caml_invalid_argument (global.get $ta_too_large))))
865849
(struct.new $bigarray
866850
(global.get $bigarray_ops)
867851
(local.get $data)
@@ -1050,7 +1034,7 @@
10501034
(struct.get $float 0 (ref.cast (ref $float) (local.get $v))))
10511035
(return))
10521036

1053-
(data $Bigarray_dim "Bigarray.dim")
1037+
(@string $Bigarray_dim "Bigarray.dim")
10541038

10551039
(func $caml_ba_dim (export "caml_ba_dim")
10561040
(param (ref eq)) (param (ref eq)) (result (ref eq))
@@ -1061,9 +1045,7 @@
10611045
(ref.cast (ref $bigarray) (local.get 0))))
10621046
(local.set $i (i31.get_s (ref.cast (ref i31) (local.get 1))))
10631047
(if (i32.ge_u (local.get $i) (array.len (local.get $dim)))
1064-
(then (call $caml_invalid_argument
1065-
(array.new_data $string $Bigarray_dim
1066-
(i32.const 0) (i32.const 12)))))
1048+
(then (call $caml_invalid_argument (global.get $Bigarray_dim))))
10671049
(ref.i31 (array.get $int_array (local.get $dim) (local.get $i))))
10681050

10691051
(func (export "caml_ba_dim_1") (param (ref eq)) (result (ref eq))
@@ -1409,7 +1391,7 @@
14091391
(local.get $v))
14101392
(ref.i31 (i32.const 0)))
14111393

1412-
(data $too_many_indices "Bigarray.slice: too many indices")
1394+
(@string $too_many_indices "Bigarray.slice: too many indices")
14131395

14141396
(func (export "caml_ba_slice")
14151397
(param $vb (ref eq)) (param $vind (ref eq)) (result (ref eq))
@@ -1425,10 +1407,7 @@
14251407
(local.set $num_dims (struct.get $bigarray $ba_num_dims (local.get $b)))
14261408
(if (i32.gt_u (local.get $num_inds)
14271409
(struct.get $bigarray $ba_num_dims (local.get $b)))
1428-
(then
1429-
(call $caml_invalid_argument
1430-
(array.new_data $string $too_many_indices
1431-
(i32.const 0) (i32.const 32)))))
1410+
(then (call $caml_invalid_argument (global.get $too_many_indices))))
14321411
(local.set $sub_dim
14331412
(array.new $int_array (i32.const 0)
14341413
(i32.sub (local.get $num_dims) (local.get $num_inds))))
@@ -1492,7 +1471,7 @@
14921471
(struct.get $bigarray $ba_kind (local.get $b))
14931472
(struct.get $bigarray $ba_layout (local.get $b))))
14941473

1495-
(data $bad_subarray "Bigarray.sub: bad sub-array")
1474+
(@string $bad_subarray "Bigarray.sub: bad sub-array")
14961475

14971476
(func (export "caml_ba_sub")
14981477
(param $vba (ref eq)) (param $vofs (ref eq)) (param $vlen (ref eq))
@@ -1542,10 +1521,7 @@
15421521
(i32.gt_s (i32.add (local.get $ofs) (local.get $len))
15431522
(array.get $int_array (local.get $dim)
15441523
(local.get $changed_dim))))
1545-
(then
1546-
(call $caml_invalid_argument
1547-
(array.new_data $string $bad_subarray
1548-
(i32.const 0) (i32.const 27)))))
1524+
(then (call $caml_invalid_argument (global.get $bad_subarray))))
15491525
(local.set $new_dim
15501526
(array.new $int_array (i32.const 0) (local.get $num_dims)))
15511527
(array.copy $int_array $int_array
@@ -1658,7 +1634,7 @@
16581634
(struct.get $float 0 (ref.cast (ref $float) (local.get $v))))
16591635
(return (ref.i31 (i32.const 0))))
16601636

1661-
(data $dim_mismatch "Bigarray.blit: dimension mismatch")
1637+
(@string $dim_mismatch "Bigarray.blit: dimension mismatch")
16621638

16631639
(func (export "caml_ba_blit")
16641640
(param $vsrc (ref eq)) (param $vdst (ref eq)) (result (ref eq))
@@ -1672,10 +1648,7 @@
16721648
(local.set $len (struct.get $bigarray $ba_num_dims (local.get $dst)))
16731649
(if (i32.ne (local.get $len)
16741650
(struct.get $bigarray $ba_num_dims (local.get $src)))
1675-
(then
1676-
(call $caml_invalid_argument
1677-
(array.new_data $string $dim_mismatch
1678-
(i32.const 0) (i32.const 33)))))
1651+
(then (call $caml_invalid_argument (global.get $dim_mismatch))))
16791652
(local.set $sdim (struct.get $bigarray $ba_dim (local.get $src)))
16801653
(local.set $ddim (struct.get $bigarray $ba_dim (local.get $dst)))
16811654
(loop $loop
@@ -1685,19 +1658,17 @@
16851658
(array.get $int_array (local.get $sdim) (local.get $i))
16861659
(array.get $int_array (local.get $ddim) (local.get $i)))
16871660
(then
1688-
(call $caml_invalid_argument
1689-
(array.new_data $string $dim_mismatch
1690-
(i32.const 0) (i32.const 33)))))
1661+
(call $caml_invalid_argument (global.get $dim_mismatch))))
16911662
(local.set $i (i32.add (local.get $i) (i32.const 1)))
16921663
(br $loop))))
16931664
(call $ta_blit
16941665
(struct.get $bigarray $ba_data (local.get $src))
16951666
(struct.get $bigarray $ba_data (local.get $dst)))
16961667
(ref.i31 (i32.const 0)))
16971668

1698-
(data $bad_number_dim "Bigarray.reshape: bad number of dimensions")
1699-
(data $negative_dim "Bigarray.reshape: negative dimension")
1700-
(data $size_mismatch "Bigarray.reshape: size mismatch")
1669+
(@string $bad_number_dim "Bigarray.reshape: bad number of dimensions")
1670+
(@string $negative_dim "Bigarray.reshape: negative dimension")
1671+
(@string $size_mismatch "Bigarray.reshape: size mismatch")
17011672

17021673
(func (export "caml_ba_reshape")
17031674
(param $vb (ref eq)) (param $vd (ref eq)) (result (ref eq))
@@ -1709,10 +1680,7 @@
17091680
(local.set $num_dims (i32.sub (array.len (local.get $vdim)) (i32.const 1)))
17101681
(local.set $b (ref.cast (ref $bigarray) (local.get $vb)))
17111682
(if (i32.gt_u (local.get $num_dims) (global.get $CAML_BA_MAX_NUM_DIMS))
1712-
(then
1713-
(call $caml_invalid_argument
1714-
(array.new_data $string $bad_number_dim
1715-
(i32.const 0) (i32.const 42)))))
1683+
(then (call $caml_invalid_argument (global.get $bad_number_dim))))
17161684
(local.set $num_elts (i64.const 1))
17171685
(local.set $dim (array.new $int_array (i32.const 0) (local.get $num_dims)))
17181686
(loop $loop
@@ -1725,9 +1693,7 @@
17251693
(i32.add (local.get $i) (i32.const 1))))))
17261694
(if (i32.lt_s (local.get $d) (i32.const 0))
17271695
(then
1728-
(call $caml_invalid_argument
1729-
(array.new_data $string $negative_dim
1730-
(i32.const 0) (i32.const 36)))))
1696+
(call $caml_invalid_argument (global.get $negative_dim))))
17311697
(array.set $int_array (local.get $dim) (local.get $i)
17321698
(local.get $d))
17331699
(local.set $num_elts
@@ -1741,10 +1707,7 @@
17411707
(if (i32.ne (i32.wrap_i64 (local.get $num_elts))
17421708
(call $caml_ba_get_size
17431709
(struct.get $bigarray $ba_dim (local.get $b))))
1744-
(then
1745-
(call $caml_invalid_argument
1746-
(array.new_data $string $size_mismatch
1747-
(i32.const 0) (i32.const 31)))))
1710+
(then (call $caml_invalid_argument (global.get $size_mismatch))))
17481711
(struct.new $bigarray
17491712
(global.get $bigarray_ops)
17501713
(struct.get $bigarray $ba_data (local.get $b))

runtime/wasm/bigstring.wat

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,13 @@
9898
(local.set $h (call $caml_hash_mix_int (local.get $h) (local.get $w))))
9999
(i32.xor (local.get $h) (local.get $len)))
100100

101-
(data $buffer "buffer")
101+
(@string $buffer "buffer")
102102

103103
(func (export "bigstring_to_array_buffer")
104104
(param $bs (ref eq)) (result (ref eq))
105105
(return_call $caml_js_get
106106
(call $caml_ba_to_typed_array (local.get $bs))
107-
(array.new_data $string $buffer (i32.const 0) (i32.const 6))))
107+
(global.get $buffer)))
108108

109109
(export "bigstring_to_typed_array" (func $caml_ba_to_typed_array))
110110

runtime/wasm/compare.wat

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -215,9 +215,9 @@
215215
(call $clear_compare_stack)
216216
(local.get $res))
217217

218-
(data $abstract_value "compare: abstract value")
219-
(data $functional_value "compare: functional value")
220-
(data $continuation_value "compare: continuation value")
218+
(@string $abstract_value "compare: abstract value")
219+
(@string $functional_value "compare: functional value")
220+
(@string $continuation_value "compare: continuation value")
221221

222222
(func $do_compare_val
223223
(param $stack (ref $compare_stack))
@@ -477,9 +477,7 @@
477477
(br_if $next_item (i32.eqz (local.get $res)))
478478
(return (local.get $res)))
479479
(call $clear_compare_stack)
480-
(call $caml_invalid_argument
481-
(array.new_data $string $abstract_value
482-
(i32.const 0) (i32.const 23)))
480+
(call $caml_invalid_argument (global.get $abstract_value))
483481
(ref.i31 (i32.const 0))))
484482
(drop (block $v1_not_js (result (ref eq))
485483
(local.set $js1
@@ -514,17 +512,15 @@
514512
(i32.eqz (call $caml_is_closure (local.get $v2)))))
515513
(call $clear_compare_stack)
516514
(call $caml_invalid_argument
517-
(array.new_data $string $functional_value
518-
(i32.const 0) (i32.const 25)))))
515+
(global.get $functional_value))))
519516
(if (call $caml_is_continuation (local.get $v1))
520517
(then
521518
(drop (br_if $heterogeneous(ref.i31 (i32.const 0))
522519
(i32.eqz
523520
(call $caml_is_continuation (local.get $v2)))))
524521
(call $clear_compare_stack)
525522
(call $caml_invalid_argument
526-
(array.new_data $string $continuation_value
527-
(i32.const 0) (i32.const 27)))))
523+
(global.get $continuation_value))))
528524
(ref.i31 (i32.const 0)))) ;; fall through
529525
;; heterogeneous comparison
530526
(local.set $t1
@@ -549,9 +545,7 @@
549545
(if (i32.eqz (local.get $res))
550546
(then
551547
(call $clear_compare_stack)
552-
(call $caml_invalid_argument
553-
(array.new_data $string $abstract_value
554-
(i32.const 0) (i32.const 23)))))
548+
(call $caml_invalid_argument (global.get $abstract_value))))
555549
(return (local.get $res)))
556550
(if (call $compare_stack_is_not_empty (local.get $stack))
557551
(then

0 commit comments

Comments
 (0)