prepack
Version:
Execute a JS bundle, serialize global state and side effects to a snapshot that can be quickly restored.
187 lines (147 loc) • 6.5 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = function (realm, obj) {
// ECMA262 23.3.3.6
obj.defineNativeProperty(realm.intrinsics.SymbolToStringTag, new _index.StringValue(realm, "WeakMap"), { writable: false });
// ECMA262 23.3.3.2
obj.defineNativeMethod("delete", 1, (context, [key]) => {
// 1. Let M be the this value.
let M = context.throwIfNotConcrete();
// 2. If Type(M) is not Object, throw a TypeError exception.
if (!(M instanceof _index.ObjectValue)) {
throw realm.createErrorThrowCompletion(realm.intrinsics.TypeError);
}
// 3. If M does not have a [[WeakMapData]] internal slot, throw a TypeError exception.
if (!M.$WeakMapData) {
throw realm.createErrorThrowCompletion(realm.intrinsics.TypeError);
}
// 4. Let entries be the List that is the value of M's [[WeakMapData]] internal slot.
let entries = M.$WeakMapData;
realm.recordModifiedProperty(M.$WeakMapData_binding);
(0, _invariant2.default)(entries !== undefined);
// 5. If Type(key) is not Object, return false.
key = key.throwIfNotConcrete();
if (!(key instanceof _index.ObjectValue)) {
return realm.intrinsics.false;
}
// 6. Repeat for each Record {[[Key]], [[Value]]} p that is an element of entries,
for (let p of entries) {
// a. If p.[[Key]] is not empty and SameValue(p.[[Key]], key) is true, then
if (p.$Key !== undefined && (0, _index2.SameValuePartial)(realm, p.$Key, key)) {
// i. Set p.[[Key]] to empty.
p.$Key = undefined;
// ii. Set p.[[Value]] to empty.
p.$Value = undefined;
// iii. Return true.
return realm.intrinsics.true;
}
}
// 7. Return false.
return realm.intrinsics.false;
});
// ECMA262 23.3.3.3
obj.defineNativeMethod("get", 1, (context, [key]) => {
// 1. Let M be the this value.
let M = context.throwIfNotConcrete();
// 2. If Type(M) is not Object, throw a TypeError exception.
if (!(M instanceof _index.ObjectValue)) {
throw realm.createErrorThrowCompletion(realm.intrinsics.TypeError);
}
// 3. If M does not have a [[WeakMapData]] internal slot, throw a TypeError exception.
if (!M.$WeakMapData) {
throw realm.createErrorThrowCompletion(realm.intrinsics.TypeError);
}
// 4. Let entries be the List that is the value of M's [[WeakMapData]] internal slot.
let entries = M.$WeakMapData;
(0, _invariant2.default)(entries !== undefined);
// 5. If Type(key) is not Object, return undefined.
key = key.throwIfNotConcrete();
if (!(key instanceof _index.ObjectValue)) {
return realm.intrinsics.undefined;
}
// 6. Repeat for each Record {[[Key]], [[Value]]} p that is an element of entries,
for (let p of entries) {
// a. If p.[[Key]] is not empty and SameValue(p.[[Key]], key) is true, return p.[[Value]].
if (p.$Key !== undefined && (0, _index2.SameValuePartial)(realm, p.$Key, key)) {
(0, _invariant2.default)(p.$Value !== undefined);
return p.$Value;
}
}
// 7. Return undefined.
return realm.intrinsics.undefined;
});
// ECMA262 23.3.3.4
obj.defineNativeMethod("has", 1, (context, [key]) => {
// 1. Let M be the this value.
let M = context.throwIfNotConcrete();
// 2. If Type(M) is not Object, throw a TypeError exception.
if (!(M instanceof _index.ObjectValue)) {
throw realm.createErrorThrowCompletion(realm.intrinsics.TypeError);
}
// 3. If M does not have a [[WeakMapData]] internal slot, throw a TypeError exception.
if (!M.$WeakMapData) {
throw realm.createErrorThrowCompletion(realm.intrinsics.TypeError);
}
// 4. Let entries be the List that is the value of M's [[WeakMapData]] internal slot.
let entries = M.$WeakMapData;
(0, _invariant2.default)(entries !== undefined);
// 5. If Type(key) is not Object, return false.
key = key.throwIfNotConcrete();
if (!(key instanceof _index.ObjectValue)) {
return realm.intrinsics.false;
}
// 6. Repeat for each Record {[[Key]], [[Value]]} p that is an element of entries,
for (let p of entries) {
// a. If p.[[Key]] is not empty and SameValue(p.[[Key]], key) is true, return true.
if (p.$Key !== undefined && (0, _index2.SameValuePartial)(realm, p.$Key, key)) return realm.intrinsics.true;
}
// 7. Return false.
return realm.intrinsics.false;
});
// ECMA262 23.3.3.5
obj.defineNativeMethod("set", 2, (context, [key, value]) => {
// 1. Let M be the this value.
let M = context.throwIfNotConcrete();
// 2. If Type(M) is not Object, throw a TypeError exception.
if (!(M instanceof _index.ObjectValue)) {
throw realm.createErrorThrowCompletion(realm.intrinsics.TypeError);
}
// 3. If M does not have a [[WeakMapData]] internal slot, throw a TypeError exception.
if (!M.$WeakMapData) {
throw realm.createErrorThrowCompletion(realm.intrinsics.TypeError);
}
// 4. Let entries be the List that is the value of M's [[WeakMapData]] internal slot.
realm.recordModifiedProperty(M.$WeakMapData_binding);
let entries = M.$WeakMapData;
(0, _invariant2.default)(entries !== undefined);
// 5. If Type(key) is not Object, throw a TypeError exception.
key = key.throwIfNotConcrete();
if (!(key instanceof _index.ObjectValue)) {
throw realm.createErrorThrowCompletion(realm.intrinsics.TypeError);
}
// 6. Repeat for each Record {[[Key]], [[Value]]} p that is an element of entries,
for (let p of entries) {
// a. If p.[[Key]] is not empty and SameValue(p.[[Key]], key) is true, then
if (p.$Key !== undefined && (0, _index2.SameValuePartial)(realm, p.$Key, key)) {
// i. Set p.[[Value]] to value.
p.$Value = value;
// ii. Return M.
return M;
}
}
// 7. Let p be the Record {[[Key]]: key, [[Value]]: value}.
let p = { $Key: key, $Value: value };
// 8. Append p as the last element of entries.
entries.push(p);
// 9. Return M.
return M;
});
};
var _index = require("../../values/index.js");
var _index2 = require("../../methods/index.js");
var _invariant = require("../../invariant.js");
var _invariant2 = _interopRequireDefault(_invariant);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
//# sourceMappingURL=WeakMapPrototype.js.map