From d11157ce9ab738cef7d085878e510f528569bfb6 Mon Sep 17 00:00:00 2001 From: CanadaHonk Date: Sat, 8 Jul 2023 21:48:54 +0100 Subject: [PATCH] builtins: add Number.isSafeInteger MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit test262: 9.57% (+0.01) | ๐Ÿงช 48642 | ๐Ÿค  4654 (+5) | โŒ 106 | ๐Ÿ’€ 16 | ๐Ÿงฉ 188 | ๐Ÿ’ฅ 16170 (-6) | ๐Ÿ“ 27508 (+1) --- README.md | 2 +- compiler/builtins.js | 24 +++++++++++++++++++++++- test/float_issafeinteger.js | 11 +++++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 test/float_issafeinteger.js diff --git a/README.md b/README.md index 22da1495..ecf8f8af 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/compiler/builtins.js b/compiler/builtins.js index 15509583..69f3bd68 100644 --- a/compiler/builtins.js +++ b/compiler/builtins.js @@ -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"; @@ -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 ], diff --git a/test/float_issafeinteger.js b/test/float_issafeinteger.js new file mode 100644 index 00000000..e0b66996 --- /dev/null +++ b/test/float_issafeinteger.js @@ -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)); \ No newline at end of file