Skip to content

Commit

Permalink
builtins: add Number.isSafeInteger
Browse files Browse the repository at this point in the history
test262: 9.57% (+0.01) | πŸ§ͺ 48642 | 🀠 4654 (+5) | ❌ 106 | πŸ’€ 16 | 🧩 188 | πŸ’₯ 16170 (-6) | πŸ“ 27508 (+1)
  • Loading branch information
CanadaHonk committed Jul 8, 2023
1 parent 5d26bc9 commit d11157c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ see [optimizations](#optimizations) for opts implemented/supported.
- supports i32, i64, and f64 for valtypes
- `NaN` and `Infinity` (f64 only)
- `isNaN` and `isFinite` (f64 only)
- most of `Number` (`MAX_VALUE`, `MIN_VALUE`, `MAX_SAFE_INTEGER`, `MIN_SAFE_INTEGER`, `POSITIVE_INFINITY`, `NEGATIVE_INFINITY`, `EPSILON`, `NaN`, `isNaN`, `isFinite`, `isInteger`) (some f64 only)
- most of `Number` (`MAX_VALUE`, `MIN_VALUE`, `MAX_SAFE_INTEGER`, `MIN_SAFE_INTEGER`, `POSITIVE_INFINITY`, `NEGATIVE_INFINITY`, `EPSILON`, `NaN`, `isNaN`, `isFinite`, `isInteger`, `isSafeInteger`) (some f64 only)
- some `Math` funcs (`Math.sqrt`, `Math.abs`, `Math.floor`, `Math.sign`, `Math.round`, `Math.trunc`) (f64 only)

## soon todo
Expand Down
24 changes: 23 additions & 1 deletion compiler/builtins.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Opcodes, Valtype } from "./wasmSpec.js";
import { Blocktype, Opcodes, Valtype } from "./wasmSpec.js";
import { number, i32x4 } from "./embedding.js";
// import parse from "./parse.js";

Expand Down Expand Up @@ -203,6 +203,28 @@ export const BuiltinFuncs = function() {
]
};

this.__Number_isSafeInteger = {
floatOnly: true,
params: [ valtypeBinary ],
locals: [],
returns: [ valtypeBinary ],
wasm: [
[ Opcodes.local_get, 0 ],
[ Opcodes.local_get, 0 ],
[ Opcodes.f64_trunc ],
[ Opcodes.f64_ne ],
[ Opcodes.if, Blocktype.void ],
...number(0),
[ Opcodes.return ],
[ Opcodes.end ],
[ Opcodes.local_get, 0 ],
[ Opcodes.f64_abs ],
...number(9007199254740991),
[ Opcodes.f64_le ],
Opcodes.i32_from
]
};

this.__Math_sqrt = {
floatOnly: true,
params: [ valtypeBinary ],
Expand Down
11 changes: 11 additions & 0 deletions test/float_issafeinteger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// "1\n1\n1\n1\n1\n0\n0\n"

console.log(Number.isSafeInteger(0));
console.log(Number.isSafeInteger(1));
console.log(Number.isSafeInteger(-1));

console.log(Number.isSafeInteger(Number.MAX_SAFE_INTEGER));
console.log(Number.isSafeInteger(Number.MIN_SAFE_INTEGER));

console.log(Number.isSafeInteger(Number.MAX_SAFE_INTEGER + 1));
console.log(Number.isSafeInteger(Number.MIN_SAFE_INTEGER - 1));

0 comments on commit d11157c

Please sign in to comment.