Skip to content

Commit c1390f7

Browse files
authored
Merge pull request #113 from ocaml-wasm/caml-make-array
Fix creation of float arrays
2 parents fb4d189 + 0310c5c commit c1390f7

File tree

4 files changed

+85
-5
lines changed

4 files changed

+85
-5
lines changed

compiler/lib/wasm/wa_generate.ml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,10 +1148,7 @@ end
11481148

11491149
let init () =
11501150
let l =
1151-
[ "caml_make_array", "%identity"
1152-
; "caml_ensure_stack_capacity", "%identity"
1153-
; "caml_callback", "caml_trampoline"
1154-
]
1151+
[ "caml_ensure_stack_capacity", "%identity"; "caml_callback", "caml_trampoline" ]
11551152
in
11561153

11571154
let l =

compiler/tests-wasm_of_ocaml/dune

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
(executables
2-
(names gh38 gh46 gh107)
2+
(names gh38 gh46 gh107 gh112)
33
(modes js)
44
(js_of_ocaml
55
(flags :standard --disable optcall --no-inline)))
@@ -33,3 +33,13 @@
3333
(with-outputs-to
3434
%{target}
3535
(run node %{dep:gh107.bc.js}))))
36+
37+
(rule
38+
(target gh112.actual)
39+
(enabled_if
40+
(= %{profile} wasm))
41+
(alias runtest)
42+
(action
43+
(with-outputs-to
44+
%{target}
45+
(run node %{dep:gh112.bc.js}))))
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
let construct x = [| x |]
2+
3+
let get (x : float array) = x.(0)
4+
5+
let get_ (x : _ array) = x.(0)
6+
7+
let set (x : float array) e = x.(0) <- e
8+
9+
let set_ (x : _ array) e = x.(0) <- e
10+
11+
let a = construct 1.0
12+
13+
let _ = set a 2.0
14+
15+
let _ = assert (Float.equal (get a) 2.0)
16+
17+
let _ = assert (Float.equal (get_ a) 2.0)
18+
19+
let _ = set_ a 3.0
20+
21+
let _ = assert (Float.equal (get a) 3.0)
22+
23+
let _ = assert (Float.equal (get_ a) 3.0)
24+
25+
let b = [| 1.0 |]
26+
27+
let _ = set b 2.0
28+
29+
let _ = assert (Float.equal (get b) 2.0)
30+
31+
let _ = assert (Float.equal (get_ b) 2.0)
32+
33+
let _ = set_ b 3.0
34+
35+
let _ = assert (Float.equal (get b) 3.0)
36+
37+
let _ = assert (Float.equal (get_ b) 3.0)
38+
39+
let construct2 x = [| x; x |]
40+
41+
let c = construct2 1.
42+
43+
let _ = assert (Float.equal c.(0) 1. && Float.equal c.(1) 1.)
44+
45+
let _ = c.(1) <- 2.
46+
47+
let _ = assert (Array.length c = 2)
48+
49+
let _ = assert (Float.equal c.(0) 1. && Float.equal c.(1) 2.)

runtime/wasm/array.wat

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,30 @@
6464
(if (i32.eqz (local.get $sz)) (then (return (global.get $empty_array))))
6565
(array.new $float_array (f64.const 0) (local.get $sz)))
6666

67+
(func (export "caml_make_array") (param $vinit (ref eq)) (result (ref eq))
68+
(local $init (ref $block)) (local $res (ref $float_array))
69+
(local $size i32) (local $i i32)
70+
(local.set $init (ref.cast (ref $block) (local.get $vinit)))
71+
(local.set $size (array.len (local.get $init)))
72+
(if (i32.ne (local.get $size) (i32.const 1))
73+
(then
74+
(if (ref.test (ref $float)
75+
(array.get $block (local.get $init) (i32.const 1)))
76+
(then
77+
(local.set $size (i32.sub (local.get $size) (i32.const 1)))
78+
(local.set $res
79+
(array.new $float_array (f64.const 0) (local.get $size)))
80+
(loop $loop
81+
(array.set $float_array (local.get $res) (local.get $i)
82+
(struct.get $float 0
83+
(ref.cast (ref $float)
84+
(array.get $block (local.get $init)
85+
(i32.add (local.get $i) (i32.const 1))))))
86+
(local.set $i (i32.add (local.get $i) (i32.const 1)))
87+
(br_if $loop (i32.lt_u (local.get $i) (local.get $size))))
88+
(return (local.get $res))))))
89+
(return (local.get $init)))
90+
6791
(func (export "caml_floatarray_unsafe_get")
6892
(param $a (ref eq)) (param $i (ref eq)) (result (ref eq))
6993
(struct.new $float

0 commit comments

Comments
 (0)