Skip to content

Commit 5b98a6f

Browse files
authored
Merge pull request #58 from ocaml-wasm/named-value
Fix implementation of caml_register_named_value
2 parents 5b6215e + 89fce85 commit 5b98a6f

File tree

5 files changed

+49
-31
lines changed

5 files changed

+49
-31
lines changed

compiler/tests-jsoo/bin/dune

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
(executables
22
(names error1 error2 error3)
3-
(modes js)
3+
(modes exe js)
4+
(foreign_stubs
5+
(language c)
6+
(names named_value_stubs))
7+
(js_of_ocaml
8+
(javascript_files runtime.js))
49
(libraries js_of_ocaml))
510

611
(rule
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include "caml/mlvalues.h"
2+
3+
CAMLprim value caml_unregister_named_value(value nm) {
4+
return Val_unit;
5+
}

compiler/tests-jsoo/bin/runtime.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//Provides: caml_unregister_named_value (const)
2+
//Requires: caml_named_values, caml_jsbytes_of_string
3+
function caml_unregister_named_value(nm) {
4+
nm = caml_jsbytes_of_string(nm);
5+
delete caml_named_values[nm];
6+
return 0;
7+
}

runtime/stdlib.js

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -131,16 +131,7 @@ var caml_named_values = {};
131131
//Provides: caml_register_named_value (const,mutable)
132132
//Requires: caml_named_values, caml_jsbytes_of_string
133133
function caml_register_named_value(nm,v) {
134-
nm = caml_jsbytes_of_string(nm);
135-
if (!caml_named_values.hasOwnProperty(nm)) caml_named_values[nm] = v;
136-
return 0;
137-
}
138-
139-
//Provides: caml_unregister_named_value (const)
140-
//Requires: caml_named_values, caml_jsbytes_of_string
141-
function caml_unregister_named_value(nm) {
142-
nm = caml_jsbytes_of_string(nm);
143-
delete caml_named_values[nm];
134+
caml_named_values[caml_jsbytes_of_string(nm)] = v;
144135
return 0;
145136
}
146137

runtime/wasm/stdlib.wat

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
(type $assoc
5252
(struct
5353
(field (ref $string))
54-
(field (ref eq))
54+
(field (mut (ref eq)))
5555
(field (mut (ref null $assoc)))))
5656

5757
(type $assoc_array (array (field (mut (ref null $assoc)))))
@@ -62,9 +62,9 @@
6262
(array.new $assoc_array (ref.null $assoc) (global.get $Named_value_size)))
6363

6464
(func $find_named_value
65-
(param $s (ref eq)) (param $l (ref null $assoc)) (result (ref null eq))
65+
(param $s (ref eq)) (param $l (ref null $assoc)) (result (ref null $assoc))
6666
(local $a (ref $assoc))
67-
(block $tail (result (ref null eq))
67+
(block $tail (result (ref null $assoc))
6868
(loop $loop
6969
(local.set $a
7070
(br_on_cast_fail $tail (ref null eq) (ref $assoc) (local.get $l)))
@@ -74,21 +74,26 @@
7474
(local.get $s)
7575
(struct.get $assoc 0 (local.get $a)))))
7676
(then
77-
(return (struct.get $assoc 1 (local.get $a)))))
77+
(return (local.get $a))))
7878
(local.set $l (struct.get $assoc 2 (local.get $a)))
7979
(br $loop))))
8080

8181
(func $caml_named_value (export "caml_named_value")
8282
(param $s (ref $string)) (result (ref null eq))
83-
(return_call $find_named_value
84-
(local.get $s)
85-
(array.get $assoc_array (global.get $named_value_table)
86-
(i32.rem_u
87-
(i31.get_s
88-
(ref.cast (ref i31)
89-
(call $caml_string_hash
90-
(ref.i31 (i32.const 0)) (local.get $s))))
91-
(global.get $Named_value_size)))))
83+
(block $not_found
84+
(return
85+
(struct.get $assoc 1
86+
(br_on_null $not_found
87+
(call $find_named_value
88+
(local.get $s)
89+
(array.get $assoc_array (global.get $named_value_table)
90+
(i32.rem_u
91+
(i31.get_s
92+
(ref.cast (ref i31)
93+
(call $caml_string_hash
94+
(ref.i31 (i32.const 0)) (local.get $s))))
95+
(global.get $Named_value_size))))))))
96+
(return (ref.null eq)))
9297

9398
(func (export "caml_register_named_value")
9499
(param (ref eq)) (param (ref eq)) (result (ref eq))
@@ -104,15 +109,20 @@
104109
(local.set $r
105110
(array.get $assoc_array
106111
(global.get $named_value_table) (local.get $h)))
107-
(if (ref.is_null (call $find_named_value (local.get 0) (local.get $r)))
108-
(then
109-
(array.set $assoc_array
110-
(global.get $named_value_table) (local.get $h)
111-
(struct.new $assoc
112-
(ref.cast (ref $string) (local.get 0))
113-
(local.get 1) (local.get $r)))))
112+
(block $not_found
113+
(struct.set $assoc 1
114+
(br_on_null $not_found
115+
(call $find_named_value (local.get 0) (local.get $r)))
116+
(local.get 1))
117+
(return (ref.i31 (i32.const 0))))
118+
(array.set $assoc_array
119+
(global.get $named_value_table) (local.get $h)
120+
(struct.new $assoc
121+
(ref.cast (ref $string) (local.get 0))
122+
(local.get 1) (local.get $r)))
114123
(ref.i31 (i32.const 0)))
115124

125+
;; Used only for testing (tests-jsoo/bin), but inconvenient to pull out
116126
(func (export "caml_unregister_named_value")
117127
(param $name (ref eq)) (result (ref eq))
118128
(local $h i32)

0 commit comments

Comments
 (0)