Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/core/brsTypes/Coercion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,12 @@ export function tryCoerce(value: BrsType, target: ValueKind): BrsType | undefine
return tryCoerce(value.unbox(), target);
}

// numeric types can be cast between each other
// numeric types can be cast between each other; `Integer` follows `toArrayIndex` (the device rule shared with
// `toIntegerArg`): `Float`/`Double` are truncated with NaN as 0, a `LongInteger` keeps its low 32 bits
if (isNumberKind(target) && isBrsNumber(value)) {
switch (target) {
case ValueKind.Int32:
return new Int32(value.getValue());
return new Int32(toArrayIndex(value) ?? 0);
case ValueKind.Int64:
return new Int64(value.getValue());
case ValueKind.Float:
Expand Down
4 changes: 4 additions & 0 deletions test/brsTypes/TypeCoercion.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ describe("type coercion", () => {
["float", new Float(3.14159), new Int32(3)],
["double", new Double(2.71828), new Int32(2)],
["longinteger", new Int64(2147483647119), new Int32(-881)],
["negative float", new Float(-1.9), new Int32(-1)],
["negative fraction", new Double(-0.5), new Int32(0)],
["NaN float", new Float(Number.NaN), new Int32(0)],
["NaN double", new Double(Number.NaN), new Int32(0)],
])("returns integer for %s target", (_type, input, output) => {
expect(tryCoerce(input, ValueKind.Int32)).toEqual(output);
expect(tryCoerce(input.box(), ValueKind.Int32)).toEqual(output);
Expand Down
Loading