Skip to content

Commit

Permalink
fix: Correct JS conversion of i64 & float64_bits literals (#96)
Browse files Browse the repository at this point in the history
Co-authored-by: Blaine Bublitz <blaine.bublitz@gmail.com>
  • Loading branch information
ospencer and phated committed Apr 2, 2021
1 parent c9a33da commit 00b7093
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 13 deletions.
9 changes: 4 additions & 5 deletions js/expression.ml
Expand Up @@ -115,18 +115,17 @@ let const wasm_mod lit =
| Int32 value ->
let scope = get wasm_mod "i32" in
meth_call scope "const" [| inject value |]
| Int64 value ->
| Int64 (low, high) ->
let scope = get wasm_mod "i64" in
meth_call scope "const" [| inject value |]
meth_call scope "const" [| inject low; inject high |]
| Float32Bits value ->
let scope = get wasm_mod "f32" in
meth_call scope "const_bits" [| inject value |]
| Float64Bits value ->
| Float64Bits (low, high) ->
let scope = get wasm_mod "f64" in
meth_call scope "const_bits" [| inject value |]
meth_call scope "const_bits" [| inject low; inject high |]
| Float32 value ->
let scope = get wasm_mod "f32" in
(* TODO: Investigate if this needs the Int32 conversion stuff *)
meth_call scope "const" [| inject value |]
| Float64 value ->
let scope = get wasm_mod "f64" in
Expand Down
12 changes: 8 additions & 4 deletions js/literal.ml
@@ -1,21 +1,25 @@
(* This is a hack around Binaryen's stack allocations *)
type jsoo =
| Int32 of int32
| Int64 of int64
| Int64 of int32 * int32
| Float32Bits of int32
| Float64Bits of int64
| Float64Bits of int32 * int32
| Float32 of float
| Float64 of float

type t = jsoo

let int32 value = Int32 value

let int64 value = Int64 value
let int64 value =
Int64
(Int64.to_int32 value, Int64.to_int32 (Int64.shift_right_logical value 32))

let float32_bits value = Float32Bits value

let float64_bits value = Float64Bits value
let float64_bits value =
Float64Bits
(Int64.to_int32 value, Int64.to_int32 (Int64.shift_right_logical value 32))

let float32 value = Float32 value

Expand Down
4 changes: 2 additions & 2 deletions src/literal.ml
Expand Up @@ -15,9 +15,9 @@ external float64 : float -> t = "caml_binaryen_literal_float64"
(* Hacks for Binaryen.js stack allocations, Don't use in binaryen.native *)
type jsoo =
| Int32 of int32
| Int64 of int64
| Int64 of int32 * int32
| Float32Bits of int32
| Float64Bits of int64
| Float64Bits of int32 * int32
| Float32 of float
| Float64 of float

Expand Down
2 changes: 2 additions & 0 deletions test/test.expected
Expand Up @@ -4,6 +4,8 @@
(memory $0 1)
(table $table 1 1 funcref)
(elem (i32.const 0) $adder)
(global $max_int64 i64 (i64.const 9223372036854775807))
(global $test_float64_bits f64 (f64.const 1.23))
(export "adder" (func $adder))
(export "memory" (memory $0))
(start $start)
Expand Down
8 changes: 8 additions & 0 deletions test/test.ml
Expand Up @@ -48,6 +48,14 @@ let start =

let _ = Export.add_function_export wasm_mod "adder" "adder"

let _ =
Global.add_global wasm_mod "max_int64" Type.int64 false
(Expression.const wasm_mod (Literal.int64 Int64.max_int))

let _ =
Global.add_global wasm_mod "test_float64_bits" Type.float64 false
(Expression.const wasm_mod (Literal.float64_bits 0x3FF3AE147AE147AEL))

let _ =
Table.add_table wasm_mod "table" 1 1 [ "adder" ]
(Expression.const wasm_mod (Literal.int32 0l))
Expand Down
4 changes: 2 additions & 2 deletions virtual/literal.mli
Expand Up @@ -15,9 +15,9 @@ val float64 : float -> t
(* Hacks for Binaryen.js stack allocations *)
type jsoo =
| Int32 of int32
| Int64 of int64
| Int64 of int32 * int32
| Float32Bits of int32
| Float64Bits of int64
| Float64Bits of int32 * int32
| Float32 of float
| Float64 of float

Expand Down

0 comments on commit 00b7093

Please sign in to comment.