Skip to content

Commit

Permalink
fix(stdlib): Make toNumber functions respect Number invariants (#1347)
Browse files Browse the repository at this point in the history
  • Loading branch information
ospencer authored Jun 28, 2022
1 parent f640ec2 commit 78db882
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 18 deletions.
4 changes: 4 additions & 0 deletions compiler/test/stdlib/bigint.test.gr
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,10 @@ assert eq(
let (==) = equals
let (+) = origAdd

// Regression #1339
let arr = [> 1, 2, 3]
assert arr[toNumber(1t)] == 2

// Tests generated with https://gist.github.com/peblair/b5a463a03e40bc870a80869eea148b4b
// BEGIN RANDOMLY GENERATED TESTS
//print("Running random test suite (30 tests per category)")
Expand Down
4 changes: 4 additions & 0 deletions compiler/test/stdlib/int32.test.gr
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,7 @@ assert ne(5l, 55l)
assert !ne(5l, 5l)
assert eqz(0l)
assert !eqz(-42l)

// Regression #1339
let arr = [> 1, 2, 3]
assert arr[toNumber(1l)] == 2
4 changes: 4 additions & 0 deletions compiler/test/stdlib/int64.test.gr
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,7 @@ assert ne(5L, 55L)
assert !ne(5L, 5L)
assert eqz(0L)
assert !eqz(-42L)

// Regression #1339
let arr = [> 1, 2, 3]
assert arr[toNumber(1L)] == 2
42 changes: 24 additions & 18 deletions stdlib/runtime/numbers.gr
Original file line number Diff line number Diff line change
Expand Up @@ -2070,42 +2070,48 @@ export let coerceNumberToFloat64 = (x: Number) => {

@unsafe
export let coerceInt32ToNumber = (x: Int32) => {
let x = WasmI32.fromGrain(x)
// incRef x to reuse it via WasmI32.toGrain
Memory.incRef(x)
WasmI32.toGrain(x): Number
WasmI32.toGrain(
reducedInteger(WasmI64.extendI32S(boxedInt32Number(WasmI32.fromGrain(x))))
): Number
}

@unsafe
export let coerceInt64ToNumber = (x: Int64) => {
let x = WasmI32.fromGrain(x)
// incRef x to reuse it via WasmI32.toGrain
Memory.incRef(x)
WasmI32.toGrain(x): Number
WasmI32.toGrain(
reducedInteger(boxedInt64Number(WasmI32.fromGrain(x)))
): Number
}

@unsafe
export let coerceBigIntToNumber = (x: BigInt) => {
let x = WasmI32.fromGrain(x)
// incRef x to reuse it via WasmI32.toGrain
// reducedBigInteger assumes that the bigint is dead,
// but in our case, it is not
Memory.incRef(x)
WasmI32.toGrain(x): Number
WasmI32.toGrain(reducedBigInteger(x)): Number
}

@unsafe
export let coerceRationalToNumber = (x: Rational) => {
let x = WasmI32.fromGrain(x)
// incRef x to reuse it via WasmI32.toGrain
Memory.incRef(x)
WasmI32.toGrain(x): Number
if (WasmI32.eq(boxedRationalDenominator(WasmI32.fromGrain(x)), 1n)) {
WasmI32.toGrain(
reducedInteger(
WasmI64.extendI32S(boxedRationalNumerator(WasmI32.fromGrain(x)))
)
): Number
} else {
let x = WasmI32.fromGrain(x)
// incRef x to reuse it via WasmI32.toGrain
Memory.incRef(x)
WasmI32.toGrain(x): Number
}
}

@unsafe
export let coerceFloat32ToNumber = (x: Float32) => {
let x = WasmI32.fromGrain(x)
// incRef x to reuse it via WasmI32.toGrain
Memory.incRef(x)
WasmI32.toGrain(x): Number
WasmI32.toGrain(
newFloat64(WasmF64.promoteF32(boxedFloat32Number(WasmI32.fromGrain(x))))
): Number
}

@unsafe
Expand Down

0 comments on commit 78db882

Please sign in to comment.