Skip to content

Commit 982edcb

Browse files
committed
Use preprocessor to manage runtime changes between OCaml versions
1 parent 82fdbd8 commit 982edcb

File tree

8 files changed

+84
-180
lines changed

8 files changed

+84
-180
lines changed

manual/wasm_runtime.wiki

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,21 @@ You can import the following functions to access or allocate integers of type in
4646
(func $caml_copy_int64 (param i64) (result (ref eq))))
4747
}}}
4848

49-
== Implementing primitives
49+
== Preprocessor ==
50+
51+
The Wasm text files are passed through a preprocessor. You can run the processor manually: {{{wasm_of_ocaml pp test.wasm}}}.
52+
53+
This preprocessing step allows optional compilations of pieces of code depending on the version of the compiler.
54+
{{{
55+
(@if (>= ocaml_version (5 2 0))
56+
(@then ...)
57+
(@else ...))
58+
}}}
59+
To form conditional expressions, the following operators are available:
60+
- comparisons: {{{=}}}, {{{>}}}, {{{>=}}}, {{{<}}}, {{{<=}}}, {{{<>}}};
61+
- boolean operators: {{{and}}}, {{{or}}}, {{{not}}}
62+
63+
== Implementing primitives ==
5064

5165
You define a primitive by exporting a Wasm function with parameters and return value of type {{{(ref eq)}}}.
5266
{{{

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)))))
@@ -99,6 +105,47 @@
99105
(global $caml_domain_latest_id (export "caml_domain_latest_id") (mut i32)
100106
(i32.const 1))
101107

108+
(@if (>= ocaml_version (5 2 0))
109+
(@then
110+
(func (export "caml_domain_spawn")
111+
(param $f (ref eq)) (param $term_sync_v (ref eq)) (result (ref eq))
112+
(local $id i32) (local $old i32) (local $ts (ref $block)) (local $res (ref eq))
113+
(local.set $id (global.get $caml_domain_latest_id))
114+
(global.set $caml_domain_latest_id
115+
(i32.add (local.get $id) (i32.const 1)))
116+
(local.set $old (global.get $caml_domain_id))
117+
(local.set $res
118+
(call $caml_callback_1 (local.get $f) (ref.i31 (i32.const 0))))
119+
(global.set $caml_domain_id (local.get $old))
120+
(local.set $ts (ref.cast (ref $block) (local.get $term_sync_v)))
121+
(drop (call $caml_ml_mutex_unlock (array.get $block (local.get $ts) (i32.const 2))))
122+
;; TODO: fix exn case
123+
(array.set
124+
$block
125+
(local.get $ts)
126+
(i32.const 1)
127+
(array.new_fixed
128+
$block
129+
2
130+
(ref.i31 (i32.const 0))
131+
(array.new_fixed $block 2 (ref.i31 (i32.const 0)) (local.get $res))))
132+
(ref.i31 (local.get $id)))
133+
)
134+
(@else
135+
(func (export "caml_domain_spawn")
136+
(param $f (ref eq)) (param $mutex (ref eq)) (result (ref eq))
137+
(local $id i32) (local $old i32)
138+
(local.set $id (global.get $caml_domain_latest_id))
139+
(global.set $caml_domain_latest_id
140+
(i32.add (local.get $id) (i32.const 1)))
141+
(local.set $old (global.get $caml_domain_id))
142+
(drop (call $caml_callback_1 (local.get $f) (ref.i31 (i32.const 0))))
143+
(global.set $caml_domain_id (local.get $old))
144+
(drop (call $caml_ml_mutex_unlock (local.get $mutex)))
145+
(ref.i31 (local.get $id)))
146+
))
147+
148+
102149
(func (export "caml_ml_domain_id") (export "caml_ml_domain_index")
103150
(param (ref eq)) (result (ref eq))
104151
(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
@@ -49,8 +49,6 @@
4949
(import "custom" "caml_find_custom_operations"
5050
(func $caml_find_custom_operations
5151
(param (ref $bytes)) (result (ref null $custom_operations))))
52-
(import "version-dependent" "caml_marshal_header_size"
53-
(global $caml_marshal_header_size i32))
5452

5553
(global $input_val_from_string (ref $bytes)
5654
(array.new_fixed $bytes 21
@@ -722,6 +720,16 @@
722720

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

723+
(@if (>= ocaml_version (5 1 0))
724+
(@then
725+
(global $caml_marshal_header_size (export "caml_marshal_header_size") i32
726+
(i32.const 16))
727+
)
728+
(@else
729+
(global $caml_marshal_header_size (export "caml_marshal_header_size") i32
730+
(i32.const 20))
731+
))
732+
725733
(func (export "caml_marshal_data_size")
726734
(param $buf (ref eq)) (param $ofs (ref eq)) (result (ref eq))
727735
(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)