Skip to content

Commit 114a561

Browse files
committed
Use preprocessor to manage runtime changes between OCaml versions
1 parent 8e47ba1 commit 114a561

File tree

7 files changed

+69
-179
lines changed

7 files changed

+69
-179
lines changed

runtime/wasm/domain.wat

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
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+
(import "sync" "caml_ml_mutex_unlock"
23+
(func $caml_ml_mutex_unlock (param (ref eq)) (result (ref eq))))
24+
1925
(type $block (array (mut (ref eq))))
2026
(type $function_1 (func (param (ref eq) (ref eq)) (result (ref eq))))
2127
(type $closure (sub (struct (;(field i32);) (field (ref $function_1)))))
@@ -95,6 +101,47 @@
95101
(global $caml_domain_latest_id (export "caml_domain_latest_id") (mut i32)
96102
(i32.const 1))
97103

104+
(@if (>= ocaml_version (5 2 0))
105+
(@then
106+
(func (export "caml_domain_spawn")
107+
(param $f (ref eq)) (param $term_sync_v (ref eq)) (result (ref eq))
108+
(local $id i32) (local $old i32) (local $ts (ref $block)) (local $res (ref eq))
109+
(local.set $id (global.get $caml_domain_latest_id))
110+
(global.set $caml_domain_latest_id
111+
(i32.add (local.get $id) (i32.const 1)))
112+
(local.set $old (global.get $caml_domain_id))
113+
(local.set $res
114+
(call $caml_callback_1 (local.get $f) (ref.i31 (i32.const 0))))
115+
(global.set $caml_domain_id (local.get $old))
116+
(local.set $ts (ref.cast (ref $block) (local.get $term_sync_v)))
117+
(drop (call $caml_ml_mutex_unlock (array.get $block (local.get $ts) (i32.const 2))))
118+
;; TODO: fix exn case
119+
(array.set
120+
$block
121+
(local.get $ts)
122+
(i32.const 1)
123+
(array.new_fixed
124+
$block
125+
2
126+
(ref.i31 (i32.const 0))
127+
(array.new_fixed $block 2 (ref.i31 (i32.const 0)) (local.get $res))))
128+
(ref.i31 (local.get $id)))
129+
)
130+
(@else
131+
(func (export "caml_domain_spawn")
132+
(param $f (ref eq)) (param $mutex (ref eq)) (result (ref eq))
133+
(local $id i32) (local $old i32)
134+
(local.set $id (global.get $caml_domain_latest_id))
135+
(global.set $caml_domain_latest_id
136+
(i32.add (local.get $id) (i32.const 1)))
137+
(local.set $old (global.get $caml_domain_id))
138+
(drop (call $caml_callback_1 (local.get $f) (ref.i31 (i32.const 0))))
139+
(global.set $caml_domain_id (local.get $old))
140+
(drop (call $caml_ml_mutex_unlock (local.get $mutex)))
141+
(ref.i31 (local.get $id)))
142+
))
143+
144+
98145
(func (export "caml_ml_domain_id") (export "caml_ml_domain_index")
99146
(param (ref eq)) (result (ref eq))
100147
(ref.i31 (global.get $caml_domain_id)))

runtime/wasm/dune

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,6 @@
33
(package wasm_of_ocaml-compiler)
44
(files runtime.wasm runtime.js))
55

6-
(rule
7-
(target version-dependent.wat)
8-
(deps version-dependent/post-5.2.wat)
9-
(enabled_if
10-
(>= %{ocaml_version} 5.2.0))
11-
(action
12-
(copy %{deps} %{target})))
13-
14-
(rule
15-
(target version-dependent.wat)
16-
(deps version-dependent/post-5.1.wat)
17-
(enabled_if
18-
(and
19-
(>= %{ocaml_version} 5.1.0)
20-
(< %{ocaml_version} 5.2.0)))
21-
(action
22-
(copy %{deps} %{target})))
23-
24-
(rule
25-
(target version-dependent.wat)
26-
(deps version-dependent/pre-5.1.wat)
27-
(enabled_if
28-
(< %{ocaml_version} 5.1.0))
29-
(action
30-
(copy %{deps} %{target})))
31-
326
(rule
337
(target runtime.wasm)
348
(deps

runtime/wasm/marshal.wat

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@
5050
(import "custom" "caml_find_custom_operations"
5151
(func $caml_find_custom_operations
5252
(param (ref $string)) (result (ref null $custom_operations))))
53-
(import "version-dependent" "caml_marshal_header_size"
54-
(global $caml_marshal_header_size i32))
5553

5654
(global $input_val_from_string (ref $string)
5755
(array.new_fixed $string 21
@@ -723,6 +721,16 @@
723721

724722
(data $marshal_data_size "Marshal.data_size")
725723

724+
(@if (>= ocaml_version (5 1 0))
725+
(@then
726+
(global $caml_marshal_header_size (export "caml_marshal_header_size") i32
727+
(i32.const 16))
728+
)
729+
(@else
730+
(global $caml_marshal_header_size (export "caml_marshal_header_size") i32
731+
(i32.const 20))
732+
))
733+
726734
(func (export "caml_marshal_data_size")
727735
(param $buf (ref eq)) (param $ofs (ref eq)) (result (ref eq))
728736
(local $s (ref $intern_state))

runtime/wasm/runtime_events.wat

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,18 @@
3333
(local.get $evtag)
3434
(local.get $evtype)))
3535

36+
(@if (>= ocaml_version (5 2 0))
37+
(@then
38+
(func (export "caml_runtime_events_user_write")
39+
(param (ref eq)) (param (ref eq)) (param (ref eq)) (result (ref eq))
40+
(ref.i31 (i32.const 0)))
41+
)
42+
(@else
43+
(func (export "caml_runtime_events_user_write")
44+
(param (ref eq)) (param (ref eq)) (result (ref eq))
45+
(ref.i31 (i32.const 0)))
46+
))
47+
3648
(func (export "caml_runtime_events_user_resolve")
3749
(param (ref eq)) (param (ref eq)) (param (ref eq)) (result (ref eq))
3850
(ref.i31 (i32.const 0)))

runtime/wasm/version-dependent/post-5.1.wat

Lines changed: 0 additions & 46 deletions
This file was deleted.

runtime/wasm/version-dependent/post-5.2.wat

Lines changed: 0 additions & 59 deletions
This file was deleted.

runtime/wasm/version-dependent/pre-5.1.wat

Lines changed: 0 additions & 46 deletions
This file was deleted.

0 commit comments

Comments
 (0)