prepack
Version:
Execute a JS bundle, serialize global state and side effects to a snapshot that can be quickly restored.
258 lines (202 loc) • 8.87 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = function (realm, obj) {
// ECMA262 23.2.3.1
obj.defineNativeMethod("add", 1, (context, [value]) => {
// 1. Let S be the this value.
let S = context.throwIfNotConcrete();
// 2. If Type(S) is not Object, throw a TypeError exception.
if (!(S instanceof _index.ObjectValue)) {
throw realm.createErrorThrowCompletion(realm.intrinsics.TypeError);
}
// 3. If S does not have a [[SetData]] internal slot, throw a TypeError exception.
if (!S.$SetData) {
throw realm.createErrorThrowCompletion(realm.intrinsics.TypeError);
}
// 4. Let entries be the List that is the value of S's [[SetData]] internal slot.
realm.recordModifiedProperty(S.$SetData_binding);
let entries = S.$SetData;
(0, _invariant2.default)(entries !== undefined);
// 5. Repeat for each e that is an element of entries,
for (let e of entries) {
// a. If e is not empty and SameValueZero(e, value) is true, then
if (e && (0, _index2.SameValueZeroPartial)(realm, e, value)) {
// i. Return S.
return S;
}
}
// 6. If value is -0, let value be +0.
value = value.throwIfNotConcrete();
if (value instanceof _index.NumberValue && Object.is(value.value, -0)) {
value = realm.intrinsics.zero;
}
// 7. Append value as the last element of entries.
entries.push(value);
// 8. Return S.
return S;
});
// ECMA262 23.2.3.2
obj.defineNativeMethod("clear", 0, context => {
// 1. Let S be the this value.
let S = context.throwIfNotConcrete();
// 2. If Type(S) is not Object, throw a TypeError exception.
if (!(S instanceof _index.ObjectValue)) {
throw realm.createErrorThrowCompletion(realm.intrinsics.TypeError);
}
// 3. If S does not have a [[SetData]] internal slot, throw a TypeError exception.
if (!S.$SetData) {
throw realm.createErrorThrowCompletion(realm.intrinsics.TypeError);
}
// All of these steps can be replace with just reseting [[SetData]]
// 4. Let entries be the List that is the value of S's [[SetData]] internal slot.
// 5. Repeat for each e that is an element of entries,
// 5.a Replace the element of entries whose value is e with an element whose value is empty.
realm.recordModifiedProperty(S.$SetData_binding);
S.$SetData = [];
// 6. Return undefined.
return realm.intrinsics.undefined;
});
// ECMA262 23.2.3.4
obj.defineNativeMethod("delete", 1, (context, [value]) => {
// 1. Let S be the this value.
let S = context.throwIfNotConcrete();
// 2. If Type(S) is not Object, throw a TypeError exception.
if (!(S instanceof _index.ObjectValue)) {
throw realm.createErrorThrowCompletion(realm.intrinsics.TypeError);
}
// 3. If S does not have a [[SetData]] internal slot, throw a TypeError exception.
if (!S.$SetData) {
throw realm.createErrorThrowCompletion(realm.intrinsics.TypeError);
}
// 4. Let entries be the List that is the value of S's [[SetData]] internal slot.
realm.recordModifiedProperty(S.$SetData_binding);
let entries = S.$SetData;
(0, _invariant2.default)(entries !== undefined);
// 5. Repeat for each e that is an element of entries,
for (let i = 0; i < entries.length; i++) {
let e = entries[i];
// a. If e is not empty and SameValueZero(e, value) is true, then
if (e !== undefined && (0, _index2.SameValueZeroPartial)(realm, e, value)) {
// i. Replace the element of entries whose value is e with an element whose value is empty.
entries[i] = undefined;
// ii. Return true.
return realm.intrinsics.true;
}
}
// 6. Return false.
return realm.intrinsics.false;
});
// ECMA262 23.2.3.5
obj.defineNativeMethod("entries", 0, context => {
// 1. Let S be the this value.
let S = context;
// 2. Return ? CreateSetIterator(S, "key+value").
return (0, _index2.CreateSetIterator)(realm, S, "key+value");
});
// ECMA262 23.2.3.6
obj.defineNativeMethod("forEach", 1, (context, [callbackfn, thisArg]) => {
// 1. Let S be the this value.
let S = context.throwIfNotConcrete();
// 2. If Type(S) is not Object, throw a TypeError exception.
if (!(S instanceof _index.ObjectValue)) {
throw realm.createErrorThrowCompletion(realm.intrinsics.TypeError);
}
// 3. If S does not have a [[SetData]] internal slot, throw a TypeError exception.
if (!S.$SetData) {
throw realm.createErrorThrowCompletion(realm.intrinsics.TypeError);
}
// 4. If IsCallable(callbackfn) is false, throw a TypeError exception.
if (!(0, _index2.IsCallable)(realm, callbackfn)) {
throw realm.createErrorThrowCompletion(realm.intrinsics.TypeError);
}
// 5. If thisArg was supplied, let T be thisArg; else let T be undefined.
let T = thisArg || realm.intrinsics.undefined;
// 6. Let entries be the List that is the value of S's [[SetData]] internal slot.
let entries = S.$SetData;
(0, _invariant2.default)(entries);
// 7. Repeat for each e that is an element of entries, in original insertion order
for (let e of entries) {
// a. If e is not empty, then
if (e) {
// i. Perform ? Call(callbackfn, T, « e, e, S »).
(0, _index2.Call)(realm, callbackfn, T, [e, e, S]);
}
}
// 8. Return undefined.
return realm.intrinsics.undefined;
});
// ECMA262 23.2.3.7
obj.defineNativeMethod("has", 1, (context, [value]) => {
// 1. Let S be the this value.
let S = context.throwIfNotConcrete();
// 2. If Type(S) is not Object, throw a TypeError exception.
if (!(S instanceof _index.ObjectValue)) {
throw realm.createErrorThrowCompletion(realm.intrinsics.TypeError);
}
// 3. If S does not have a [[SetData]] internal slot, throw a TypeError exception.
if (!S.$SetData) {
throw realm.createErrorThrowCompletion(realm.intrinsics.TypeError);
}
// 4. Let entries be the List that is the value of S's [[SetData]] internal slot.
let entries = S.$SetData;
// 5. Repeat for each e that is an element of entries,
for (let e of entries) {
// a. If e is not empty and SameValueZero(e, value) is true, return true.
if (e && (0, _index2.SameValueZeroPartial)(realm, e, value)) return realm.intrinsics.true;
}
// 6. Return false.
return realm.intrinsics.false;
});
// ECMA262 23.2.3.9 get Set.prototype.size
obj.$DefineOwnProperty("size", {
get: new _index.NativeFunctionValue(realm, undefined, "get size", 0, context => {
// 1. Let S be the this value.
let S = context.throwIfNotConcrete();
// 2. If Type(S) is not Object, throw a TypeError exception.
if (!(S instanceof _index.ObjectValue)) {
throw realm.createErrorThrowCompletion(realm.intrinsics.TypeError);
}
// 3. If S does not have a [[SetData]] internal slot, throw a TypeError exception.
if (!S.$SetData) {
throw realm.createErrorThrowCompletion(realm.intrinsics.TypeError);
}
// 4. Let entries be the List that is the value of S's [[SetData]] internal slot.
let entries = S.$SetData;
// 5. Let count be 0.
let count = 0;
// 6. For each e that is an element of entries
for (let e of entries) {
// a. If e is not empty, set count to count+1.
if (e) count++;
}
// 7. Return count.
return new _index.NumberValue(realm, count);
}),
configurable: true
});
// ECMA262 23.2.3.10
obj.defineNativeMethod("values", 0, context => {
// 1. Let S be the this value.
let S = context;
// 2. Return ? CreateSetIterator(S, "value").
return (0, _index2.CreateSetIterator)(realm, S, "value");
});
// ECMA262 23.2.3.8
let valuesPropertyDescriptor = obj.$GetOwnProperty("values");
(0, _invariant2.default)(valuesPropertyDescriptor);
_singletons.Properties.ThrowIfMightHaveBeenDeleted(valuesPropertyDescriptor.value);
obj.defineNativeProperty("keys", undefined, valuesPropertyDescriptor);
// ECMA262 23.2.3.11
obj.defineNativeProperty(realm.intrinsics.SymbolIterator, undefined, valuesPropertyDescriptor);
// ECMA262 23.2.3.12 Set.prototype [ @@toStringTag ]
obj.defineNativeProperty(realm.intrinsics.SymbolToStringTag, new _index.StringValue(realm, "Set"), { writable: false });
};
var _index = require("../../values/index.js");
var _index2 = require("../../methods/index.js");
var _singletons = require("../../singletons.js");
var _invariant = require("../../invariant.js");
var _invariant2 = _interopRequireDefault(_invariant);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
//# sourceMappingURL=SetPrototype.js.map