Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to a Hashmap implementation for Map #76

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
142 changes: 141 additions & 1 deletion compiler/builtins.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as GeneratedBuiltins from './generated_builtins.js';
import { Blocktype, Opcodes, Valtype, ValtypeSize } from './wasmSpec.js';
import { number } from './embedding.js';
import { TYPES, TYPE_NAMES } from './types.js';
import { TYPES, TYPE_NAMES, TYPE_FLAGS } from './types.js';
import Prefs from './prefs.js';

export const importedFuncs = [
Expand Down Expand Up @@ -1253,5 +1253,145 @@ export const BuiltinFuncs = function() {
]
};

this.__ecma262_SameValueZero = {
params: [ Valtype.f64, Valtype.i32, Valtype.f64, Valtype.i32 ],
locals: [],
localNames: [ 'x', 'xType', 'y', 'yType' ],
returns: [ Valtype.i32 ],
returnType: TYPES.boolean,
wasm: (scope, { compareStrings }) => [
// 1. If Type(x) is not Type(y), return false.
[ Opcodes.local_get, 1 ],
...number(TYPE_FLAGS.parity, Valtype.i32),
[ Opcodes.i32_or ],
[ Opcodes.local_get, 3 ],
...number(TYPE_FLAGS.parity, Valtype.i32),
[ Opcodes.i32_or ],
[ Opcodes.i32_ne ],
[ Opcodes.if, Blocktype.void ],
...number(0, Valtype.i32),
[ Opcodes.return ],
[ Opcodes.end ],

// 2. If x is a Number, then
[ Opcodes.local_get, 1 ],
...number(TYPES.number, Valtype.i32),
[ Opcodes.i32_eq ],
[ Opcodes.if, Blocktype.void ],
// 1. If x is NaN and y is NaN, return true.
[ Opcodes.local_get, 0 ],
[ Opcodes.local_get, 0 ],
[ Opcodes.f64_ne ],

[ Opcodes.local_get, 2 ],
[ Opcodes.local_get, 2 ],
[ Opcodes.f64_ne ],

[ Opcodes.i32_and ],
[ Opcodes.if, Blocktype.void ],
...number(1, Valtype.i32),
[ Opcodes.return ],
[ Opcodes.end ],

// 2. If x is +0𝔽 and y is -0𝔽, return true.
// 3. If x is -0𝔽 and y is +0𝔽, return true.
// 4. If x is y, return true.

// note: IEEE 754 equal does not differentiate between signed zeros, so this is okay
[ Opcodes.local_get, 0 ],
[ Opcodes.local_get, 2 ],
[ Opcodes.f64_eq ],
[ Opcodes.if, Blocktype.void ],
...number(1, Valtype.i32),
[ Opcodes.return ],
[ Opcodes.end ],

// 5. Return false.
...number(0, Valtype.i32),
[ Opcodes.return ],
[ Opcodes.end],

// 3. Return SameValueNonNumber(x, y).
// 1. Assert: Type(x) is Type(y).
// 2. If x is either null or undefined, return true.
[ Opcodes.local_get, 0 ],
...number(0, Valtype.f64),
[ Opcodes.f64_eq ],

[ Opcodes.local_get, 1 ],
...number(TYPES.object, Valtype.i32),
[ Opcodes.i32_eq ],
[ Opcodes.local_get, 1 ],
...number(TYPES.undefined, Valtype.i32),
[ Opcodes.i32_eq ],

[ Opcodes.i32_or ],
[ Opcodes.i32_and ],
[ Opcodes.if, Blocktype.void ],
...number(1, Valtype.i32),
[ Opcodes.return ],
[ Opcodes.end ],

// 3. If x is a BigInt, then
// a. Return BigInt::equal(x, y).
// todo: we currently don't have bigints

// 4. If x is a String, then
// a. If x and y have the same length and the same code units in the same positions, return true; otherwise, return false
[ Opcodes.local_get, 1 ],
...number(TYPES.bytestring, Valtype.i32),
[ Opcodes.i32_eq ],
[ Opcodes.local_get, 3 ],
...number(TYPES.bytestring, Valtype.i32),
[ Opcodes.i32_eq ],
[ Opcodes.i32_and ],
[ Opcodes.if, Blocktype.void ],
...compareStrings(scope, [[ Opcodes.local_get, 0 ]], [[ Opcodes.local_get, 2 ]], [[ Opcodes.local_get, 1 ]], [[ Opcodes.local_get, 3 ]], true),
[ Opcodes.return ],
[ Opcodes.end ],

[ Opcodes.local_get, 1 ],
...number(TYPES.string, Valtype.i32),
[ Opcodes.i32_eq ],
[ Opcodes.local_get, 1 ],
...number(TYPES.bytestring, Valtype.i32),
[ Opcodes.i32_eq ],
[ Opcodes.i32_or ],
[ Opcodes.if, Blocktype.void ],
...compareStrings(scope, [[ Opcodes.local_get, 0 ]], [[ Opcodes.local_get, 2 ]], [[ Opcodes.local_get, 1 ]], [[ Opcodes.local_get, 3 ]]),
[ Opcodes.return ],
[ Opcodes.end ],

// 5. If x is a Boolean, then
// a. If x and y are both true or both false, return true; otherwise, return false.
[ Opcodes.local_get, 1 ],
...number(TYPES.boolean, Valtype.i32),
[ Opcodes.i32_eq ],
[ Opcodes.if, Blocktype.void ],
[ Opcodes.local_get, 0 ],
[ Opcodes.local_get, 2 ],
[ Opcodes.f64_eq ],
[ Opcodes.if, Blocktype.void ],
...number(1, Valtype.i32),
[ Opcodes.return ],
[ Opcodes.end ],
...number(0, Valtype.i32),
[ Opcodes.return ],
[ Opcodes.end ],

// 7. If x is y, return true; otherwise, return false.
[ Opcodes.local_get, 0 ],
[ Opcodes.local_get, 2 ],
[ Opcodes.f64_eq ],
[ Opcodes.if, Blocktype.void ],
...number(1, Valtype.i32),
[ Opcodes.return ],
[ Opcodes.end ],

...number(0, Valtype.i32),
[ Opcodes.return ]
]
}

GeneratedBuiltins.BuiltinFuncs.call(this);
};
Loading