Skip to content

Commit

Permalink
feat: Add i31 expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
phated committed Jul 15, 2023
1 parent c1e481b commit c72d453
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/expression.c
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,25 @@ caml_binaryen_pop(value _module, value _ty) {
CAMLreturn(alloc_BinaryenExpressionRef(exp));
}

CAMLprim value
caml_binaryen_i31_new(value _module, value _val) {
CAMLparam2(_module, _val);
BinaryenModuleRef module = BinaryenModuleRef_val(_module);
BinaryenExpressionRef val = BinaryenExpressionRef_val(_val);
BinaryenExpressionRef exp = BinaryenI31New(module, val);
CAMLreturn(alloc_BinaryenExpressionRef(exp));
}

CAMLprim value
caml_binaryen_i31_get(value _module, value _val, value _signed) {
CAMLparam3(_module, _val, _signed);
BinaryenModuleRef module = BinaryenModuleRef_val(_module);
BinaryenExpressionRef val = BinaryenExpressionRef_val(_val);
bool isSigned = Bool_val(_signed);
BinaryenExpressionRef exp = BinaryenI31Get(module, val, isSigned);
CAMLreturn(alloc_BinaryenExpressionRef(exp));
}

CAMLprim value
caml_binaryen_expression_id_invalid(value unit) {
CAMLparam1(unit);
Expand Down
14 changes: 14 additions & 0 deletions src/expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,20 @@ function caml_binaryen_pop(wasm_mod, typ) {
return Binaryen._BinaryenPop(wasm_mod, typ);
}

//Provides: caml_binaryen_i31_new
function caml_binaryen_i31_new(wasm_mod, typ) {
return wasm_mod.i31.new(typ);
}

//Provides: caml_binaryen_i31_get
function caml_binaryen_i31_get(wasm_mod, typ, signed) {
if (signed) {
return wasm_mod.i31.get_s(typ);
} else {
return wasm_mod.i31.get_u(typ);
}
}

//Provides: caml_binaryen_expression_id_invalid
//Requires: Binaryen
function caml_binaryen_expression_id_invalid() {
Expand Down
8 changes: 8 additions & 0 deletions src/expression.ml
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,14 @@ module Pop = struct
(** Module, type *)
end

module I31 = struct
external make : Module.t -> t -> t = "caml_binaryen_i31_new"
(** Module, value *)

external get : Module.t -> t -> bool -> t = "caml_binaryen_i31_get"
(** Module, i31, is_signed *)
end

module Null = struct
external make : unit -> t = "caml_binaryen_null_expression"
(** A null reference. *)
Expand Down
8 changes: 8 additions & 0 deletions src/expression.mli
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,14 @@ module Pop : sig
val make : Module.t -> Type.t -> t
end

module I31 : sig
val make : Module.t -> t -> t
(** Module, value *)

val get : Module.t -> t -> bool -> t
(** Module, i31, is_signed *)
end

module Null : sig
val make : unit -> t
end
Expand Down

0 comments on commit c72d453

Please sign in to comment.