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