Skip to content

Commit

Permalink
Use preprocessor to manage runtime changes between OCaml versions
Browse files Browse the repository at this point in the history
  • Loading branch information
vouillon committed Jan 30, 2025
1 parent 8e47ba1 commit 114a561
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 179 deletions.
47 changes: 47 additions & 0 deletions runtime/wasm/domain.wat
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

(module
(import "obj" "caml_callback_1"
(func $caml_callback_1
(param (ref eq)) (param (ref eq)) (result (ref eq))))
(import "sync" "caml_ml_mutex_unlock"
(func $caml_ml_mutex_unlock (param (ref eq)) (result (ref eq))))

(type $block (array (mut (ref eq))))
(type $function_1 (func (param (ref eq) (ref eq)) (result (ref eq))))
(type $closure (sub (struct (;(field i32);) (field (ref $function_1)))))
Expand Down Expand Up @@ -95,6 +101,47 @@
(global $caml_domain_latest_id (export "caml_domain_latest_id") (mut i32)
(i32.const 1))

(@if (>= ocaml_version (5 2 0))
(@then
(func (export "caml_domain_spawn")
(param $f (ref eq)) (param $term_sync_v (ref eq)) (result (ref eq))
(local $id i32) (local $old i32) (local $ts (ref $block)) (local $res (ref eq))
(local.set $id (global.get $caml_domain_latest_id))
(global.set $caml_domain_latest_id
(i32.add (local.get $id) (i32.const 1)))
(local.set $old (global.get $caml_domain_id))
(local.set $res
(call $caml_callback_1 (local.get $f) (ref.i31 (i32.const 0))))
(global.set $caml_domain_id (local.get $old))
(local.set $ts (ref.cast (ref $block) (local.get $term_sync_v)))
(drop (call $caml_ml_mutex_unlock (array.get $block (local.get $ts) (i32.const 2))))
;; TODO: fix exn case
(array.set
$block
(local.get $ts)
(i32.const 1)
(array.new_fixed
$block
2
(ref.i31 (i32.const 0))
(array.new_fixed $block 2 (ref.i31 (i32.const 0)) (local.get $res))))
(ref.i31 (local.get $id)))
)
(@else
(func (export "caml_domain_spawn")
(param $f (ref eq)) (param $mutex (ref eq)) (result (ref eq))
(local $id i32) (local $old i32)
(local.set $id (global.get $caml_domain_latest_id))
(global.set $caml_domain_latest_id
(i32.add (local.get $id) (i32.const 1)))
(local.set $old (global.get $caml_domain_id))
(drop (call $caml_callback_1 (local.get $f) (ref.i31 (i32.const 0))))
(global.set $caml_domain_id (local.get $old))
(drop (call $caml_ml_mutex_unlock (local.get $mutex)))
(ref.i31 (local.get $id)))
))


(func (export "caml_ml_domain_id") (export "caml_ml_domain_index")
(param (ref eq)) (result (ref eq))
(ref.i31 (global.get $caml_domain_id)))
Expand Down
26 changes: 0 additions & 26 deletions runtime/wasm/dune
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,6 @@
(package wasm_of_ocaml-compiler)
(files runtime.wasm runtime.js))

(rule
(target version-dependent.wat)
(deps version-dependent/post-5.2.wat)
(enabled_if
(>= %{ocaml_version} 5.2.0))
(action
(copy %{deps} %{target})))

(rule
(target version-dependent.wat)
(deps version-dependent/post-5.1.wat)
(enabled_if
(and
(>= %{ocaml_version} 5.1.0)
(< %{ocaml_version} 5.2.0)))
(action
(copy %{deps} %{target})))

(rule
(target version-dependent.wat)
(deps version-dependent/pre-5.1.wat)
(enabled_if
(< %{ocaml_version} 5.1.0))
(action
(copy %{deps} %{target})))

(rule
(target runtime.wasm)
(deps
Expand Down
12 changes: 10 additions & 2 deletions runtime/wasm/marshal.wat
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@
(import "custom" "caml_find_custom_operations"
(func $caml_find_custom_operations
(param (ref $string)) (result (ref null $custom_operations))))
(import "version-dependent" "caml_marshal_header_size"
(global $caml_marshal_header_size i32))

(global $input_val_from_string (ref $string)
(array.new_fixed $string 21
Expand Down Expand Up @@ -723,6 +721,16 @@

(data $marshal_data_size "Marshal.data_size")

(@if (>= ocaml_version (5 1 0))
(@then
(global $caml_marshal_header_size (export "caml_marshal_header_size") i32
(i32.const 16))
)
(@else
(global $caml_marshal_header_size (export "caml_marshal_header_size") i32
(i32.const 20))
))

(func (export "caml_marshal_data_size")
(param $buf (ref eq)) (param $ofs (ref eq)) (result (ref eq))
(local $s (ref $intern_state))
Expand Down
12 changes: 12 additions & 0 deletions runtime/wasm/runtime_events.wat
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@
(local.get $evtag)
(local.get $evtype)))

(@if (>= ocaml_version (5 2 0))
(@then
(func (export "caml_runtime_events_user_write")
(param (ref eq)) (param (ref eq)) (param (ref eq)) (result (ref eq))
(ref.i31 (i32.const 0)))
)
(@else
(func (export "caml_runtime_events_user_write")
(param (ref eq)) (param (ref eq)) (result (ref eq))
(ref.i31 (i32.const 0)))
))

(func (export "caml_runtime_events_user_resolve")
(param (ref eq)) (param (ref eq)) (param (ref eq)) (result (ref eq))
(ref.i31 (i32.const 0)))
Expand Down
46 changes: 0 additions & 46 deletions runtime/wasm/version-dependent/post-5.1.wat

This file was deleted.

59 changes: 0 additions & 59 deletions runtime/wasm/version-dependent/post-5.2.wat

This file was deleted.

46 changes: 0 additions & 46 deletions runtime/wasm/version-dependent/pre-5.1.wat

This file was deleted.

0 comments on commit 114a561

Please sign in to comment.