Skip to content

Commit

Permalink
fix!: Expression.Return.get_value can be null (#149)
Browse files Browse the repository at this point in the history
* fix!: Expression.Return.get_value can be null

* Add test
  • Loading branch information
ospencer committed Apr 8, 2022
1 parent 1332797 commit 559341d
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 4 deletions.
7 changes: 6 additions & 1 deletion src/binaryen_stubs_expressions.c
Original file line number Diff line number Diff line change
Expand Up @@ -1534,7 +1534,12 @@ CAMLprim value
caml_binaryen_return_get_value(value _exp) {
CAMLparam1(_exp);
BinaryenExpressionRef exp = BinaryenExpressionRef_val(_exp);
CAMLreturn(alloc_BinaryenExpressionRef(BinaryenReturnGetValue(exp)));
BinaryenExpressionRef val = BinaryenReturnGetValue(exp);
if (val == NULL) {
CAMLreturn(Val_none);
} else {
CAMLreturn(caml_alloc_some(alloc_BinaryenExpressionRef(val)));
}
}

CAMLprim value
Expand Down
3 changes: 2 additions & 1 deletion src/binaryen_stubs_expressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -1273,8 +1273,9 @@ function caml_binaryen_drop_set_value(exp, value) {

//Provides: caml_binaryen_return_get_value
//Requires: binaryen
//Requires: to_option
function caml_binaryen_return_get_value(exp) {
return binaryen.Return.getValue(exp);
return to_option(binaryen.Return.getValue(exp));
}

//Provides: caml_binaryen_return_set_value
Expand Down
2 changes: 1 addition & 1 deletion src/expression.ml
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ end

module Return = struct
external make : Module.t -> t -> t = "caml_binaryen_return"
external get_value : t -> t = "caml_binaryen_return_get_value"
external get_value : t -> t option = "caml_binaryen_return_get_value"
external set_value : t -> t -> unit = "caml_binaryen_return_set_value"
end

Expand Down
2 changes: 1 addition & 1 deletion src/expression.mli
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ end

module Return : sig
val make : Module.t -> t -> t
val get_value : t -> t
val get_value : t -> t option
val set_value : t -> t -> unit
end

Expand Down
14 changes: 14 additions & 0 deletions test/test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ let _ = Settings.set_low_memory_unused false
let wasm_mod = Module.create ()
let _ = Module.set_features wasm_mod [ Module.Feature.all ]

(* Testing Return.get_value *)
let _ =
assert (
Option.is_some
(Expression.Return.get_value
(Expression.Return.make wasm_mod
(Expression.Const.make wasm_mod (Literal.int32 0l)))))

let _ =
assert (
Option.is_none
(Expression.Return.get_value
(Expression.Return.make wasm_mod (Expression.Null.make ()))))

(* Create function type for i32 (i32, i32) *)
let params () = Type.create [| Type.int32; Type.int32 |]
let results = Type.int32
Expand Down

0 comments on commit 559341d

Please sign in to comment.