12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010 |
- 'use strict';
- /**
- * __ ___ ____ _ _ ___ _ _ ____
- * \ \ / / \ | _ \| \ | |_ _| \ | |/ ___|
- * \ \ /\ / / _ \ | |_) | \| || || \| | | _
- * \ V V / ___ \| _ <| |\ || || |\ | |_| |
- * \_/\_/_/ \_\_| \_\_| \_|___|_| \_|\____|
- *
- * This file is critical for vm2. It implements the bridge between the host and the sandbox.
- * If you do not know exactly what you are doing, you should NOT edit this file.
- *
- * The file is loaded in the host and sandbox to handle objects in both directions.
- * This is done to ensure that RangeErrors are from the correct context.
- * The boundary between the sandbox and host might throw RangeErrors from both contexts.
- * Therefore, thisFromOther and friends can handle objects from both domains.
- *
- * Method parameters have comments to tell from which context they came.
- *
- */
- const globalsList = [
- 'Number',
- 'String',
- 'Boolean',
- 'Date',
- 'RegExp',
- 'Map',
- 'WeakMap',
- 'Set',
- 'WeakSet',
- 'Promise',
- 'Function'
- ];
- const errorsList = [
- 'RangeError',
- 'ReferenceError',
- 'SyntaxError',
- 'TypeError',
- 'EvalError',
- 'URIError',
- 'Error'
- ];
- const OPNA = 'Operation not allowed on contextified object.';
- const thisGlobalPrototypes = {
- __proto__: null,
- Object: Object.prototype,
- Array: Array.prototype
- };
- for (let i = 0; i < globalsList.length; i++) {
- const key = globalsList[i];
- const g = global[key];
- if (g) thisGlobalPrototypes[key] = g.prototype;
- }
- for (let i = 0; i < errorsList.length; i++) {
- const key = errorsList[i];
- const g = global[key];
- if (g) thisGlobalPrototypes[key] = g.prototype;
- }
- const {
- getPrototypeOf: thisReflectGetPrototypeOf,
- setPrototypeOf: thisReflectSetPrototypeOf,
- defineProperty: thisReflectDefineProperty,
- deleteProperty: thisReflectDeleteProperty,
- getOwnPropertyDescriptor: thisReflectGetOwnPropertyDescriptor,
- isExtensible: thisReflectIsExtensible,
- preventExtensions: thisReflectPreventExtensions,
- apply: thisReflectApply,
- construct: thisReflectConstruct,
- set: thisReflectSet,
- get: thisReflectGet,
- has: thisReflectHas,
- ownKeys: thisReflectOwnKeys,
- enumerate: thisReflectEnumerate,
- } = Reflect;
- const thisObject = Object;
- const {
- freeze: thisObjectFreeze,
- prototype: thisObjectPrototype
- } = thisObject;
- const thisObjectHasOwnProperty = thisObjectPrototype.hasOwnProperty;
- const ThisProxy = Proxy;
- const ThisWeakMap = WeakMap;
- const {
- get: thisWeakMapGet,
- set: thisWeakMapSet
- } = ThisWeakMap.prototype;
- const ThisMap = Map;
- const thisMapGet = ThisMap.prototype.get;
- const thisMapSet = ThisMap.prototype.set;
- const thisFunction = Function;
- const thisFunctionBind = thisFunction.prototype.bind;
- const thisArrayIsArray = Array.isArray;
- const thisErrorCaptureStackTrace = Error.captureStackTrace;
- const thisSymbolToString = Symbol.prototype.toString;
- const thisSymbolToStringTag = Symbol.toStringTag;
- const thisSymbolIterator = Symbol.iterator;
- const thisSymbolNodeJSUtilInspectCustom = Symbol.for('nodejs.util.inspect.custom');
- /**
- * VMError.
- *
- * @public
- * @extends {Error}
- */
- class VMError extends Error {
- /**
- * Create VMError instance.
- *
- * @public
- * @param {string} message - Error message.
- * @param {string} code - Error code.
- */
- constructor(message, code) {
- super(message);
- this.name = 'VMError';
- this.code = code;
- thisErrorCaptureStackTrace(this, this.constructor);
- }
- }
- thisGlobalPrototypes['VMError'] = VMError.prototype;
- function thisUnexpected() {
- return new VMError('Unexpected');
- }
- if (!thisReflectSetPrototypeOf(exports, null)) throw thisUnexpected();
- function thisSafeGetOwnPropertyDescriptor(obj, key) {
- const desc = thisReflectGetOwnPropertyDescriptor(obj, key);
- if (!desc) return desc;
- if (!thisReflectSetPrototypeOf(desc, null)) throw thisUnexpected();
- return desc;
- }
- function thisThrowCallerCalleeArgumentsAccess(key) {
- 'use strict';
- thisThrowCallerCalleeArgumentsAccess[key];
- return thisUnexpected();
- }
- function thisIdMapping(factory, other) {
- return other;
- }
- const thisThrowOnKeyAccessHandler = thisObjectFreeze({
- __proto__: null,
- get(target, key, receiver) {
- if (typeof key === 'symbol') {
- key = thisReflectApply(thisSymbolToString, key, []);
- }
- throw new VMError(`Unexpected access to key '${key}'`);
- }
- });
- const emptyForzenObject = thisObjectFreeze({
- __proto__: null
- });
- const thisThrowOnKeyAccess = new ThisProxy(emptyForzenObject, thisThrowOnKeyAccessHandler);
- function SafeBase() {}
- if (!thisReflectDefineProperty(SafeBase, 'prototype', {
- __proto__: null,
- value: thisThrowOnKeyAccess
- })) throw thisUnexpected();
- function SHARED_FUNCTION() {}
- const TEST_PROXY_HANDLER = thisObjectFreeze({
- __proto__: thisThrowOnKeyAccess,
- construct() {
- return this;
- }
- });
- function thisIsConstructor(obj) {
- // Note: obj@any(unsafe)
- const Func = new ThisProxy(obj, TEST_PROXY_HANDLER);
- try {
- // eslint-disable-next-line no-new
- new Func();
- return true;
- } catch (e) {
- return false;
- }
- }
- function thisCreateTargetObject(obj, proto) {
- // Note: obj@any(unsafe) proto@any(unsafe) returns@this(unsafe) throws@this(unsafe)
- let base;
- if (typeof obj === 'function') {
- if (thisIsConstructor(obj)) {
- // Bind the function since bound functions do not have a prototype property.
- base = thisReflectApply(thisFunctionBind, SHARED_FUNCTION, [null]);
- } else {
- base = () => {};
- }
- } else if (thisArrayIsArray(obj)) {
- base = [];
- } else {
- return {__proto__: proto};
- }
- if (!thisReflectSetPrototypeOf(base, proto)) throw thisUnexpected();
- return base;
- }
- function createBridge(otherInit, registerProxy) {
- const mappingOtherToThis = new ThisWeakMap();
- const protoMappings = new ThisMap();
- const protoName = new ThisMap();
- function thisAddProtoMapping(proto, other, name) {
- // Note: proto@this(unsafe) other@other(unsafe) name@this(unsafe) throws@this(unsafe)
- thisReflectApply(thisMapSet, protoMappings, [proto, thisIdMapping]);
- thisReflectApply(thisMapSet, protoMappings, [other,
- (factory, object) => thisProxyOther(factory, object, proto)]);
- if (name) thisReflectApply(thisMapSet, protoName, [proto, name]);
- }
- function thisAddProtoMappingFactory(protoFactory, other, name) {
- // Note: protoFactory@this(unsafe) other@other(unsafe) name@this(unsafe) throws@this(unsafe)
- let proto;
- thisReflectApply(thisMapSet, protoMappings, [other,
- (factory, object) => {
- if (!proto) {
- proto = protoFactory();
- thisReflectApply(thisMapSet, protoMappings, [proto, thisIdMapping]);
- if (name) thisReflectApply(thisMapSet, protoName, [proto, name]);
- }
- return thisProxyOther(factory, object, proto);
- }]);
- }
- const result = {
- __proto__: null,
- globalPrototypes: thisGlobalPrototypes,
- safeGetOwnPropertyDescriptor: thisSafeGetOwnPropertyDescriptor,
- fromArguments: thisFromOtherArguments,
- from: thisFromOther,
- fromWithFactory: thisFromOtherWithFactory,
- ensureThis: thisEnsureThis,
- mapping: mappingOtherToThis,
- connect: thisConnect,
- reflectSet: thisReflectSet,
- reflectGet: thisReflectGet,
- reflectDefineProperty: thisReflectDefineProperty,
- reflectDeleteProperty: thisReflectDeleteProperty,
- reflectApply: thisReflectApply,
- reflectConstruct: thisReflectConstruct,
- reflectHas: thisReflectHas,
- reflectOwnKeys: thisReflectOwnKeys,
- reflectEnumerate: thisReflectEnumerate,
- reflectGetPrototypeOf: thisReflectGetPrototypeOf,
- reflectIsExtensible: thisReflectIsExtensible,
- reflectPreventExtensions: thisReflectPreventExtensions,
- objectHasOwnProperty: thisObjectHasOwnProperty,
- weakMapSet: thisWeakMapSet,
- addProtoMapping: thisAddProtoMapping,
- addProtoMappingFactory: thisAddProtoMappingFactory,
- defaultFactory,
- protectedFactory,
- readonlyFactory,
- VMError
- };
- const isHost = typeof otherInit !== 'object';
- if (isHost) {
- otherInit = otherInit(result, registerProxy);
- }
- result.other = otherInit;
- const {
- globalPrototypes: otherGlobalPrototypes,
- safeGetOwnPropertyDescriptor: otherSafeGetOwnPropertyDescriptor,
- fromArguments: otherFromThisArguments,
- from: otherFromThis,
- mapping: mappingThisToOther,
- reflectSet: otherReflectSet,
- reflectGet: otherReflectGet,
- reflectDefineProperty: otherReflectDefineProperty,
- reflectDeleteProperty: otherReflectDeleteProperty,
- reflectApply: otherReflectApply,
- reflectConstruct: otherReflectConstruct,
- reflectHas: otherReflectHas,
- reflectOwnKeys: otherReflectOwnKeys,
- reflectEnumerate: otherReflectEnumerate,
- reflectGetPrototypeOf: otherReflectGetPrototypeOf,
- reflectIsExtensible: otherReflectIsExtensible,
- reflectPreventExtensions: otherReflectPreventExtensions,
- objectHasOwnProperty: otherObjectHasOwnProperty,
- weakMapSet: otherWeakMapSet
- } = otherInit;
- function thisOtherHasOwnProperty(object, key) {
- // Note: object@other(safe) key@prim throws@this(unsafe)
- try {
- return otherReflectApply(otherObjectHasOwnProperty, object, [key]) === true;
- } catch (e) { // @other(unsafe)
- throw thisFromOtherForThrow(e);
- }
- }
- function thisDefaultGet(handler, object, key, desc) {
- // Note: object@other(unsafe) key@prim desc@other(safe)
- let ret; // @other(unsafe)
- if (desc.get || desc.set) {
- const getter = desc.get;
- if (!getter) return undefined;
- try {
- ret = otherReflectApply(getter, object, [key]);
- } catch (e) {
- throw thisFromOtherForThrow(e);
- }
- } else {
- ret = desc.value;
- }
- return handler.fromOtherWithContext(ret);
- }
- function otherFromThisIfAvailable(to, from, key) {
- // Note: to@other(safe) from@this(safe) key@prim throws@this(unsafe)
- if (!thisReflectApply(thisObjectHasOwnProperty, from, [key])) return false;
- try {
- to[key] = otherFromThis(from[key]);
- } catch (e) { // @other(unsafe)
- throw thisFromOtherForThrow(e);
- }
- return true;
- }
- class BaseHandler extends SafeBase {
- constructor(object) {
- // Note: object@other(unsafe) throws@this(unsafe)
- super();
- this.objectWrapper = () => object;
- }
- getObject() {
- return this.objectWrapper();
- }
- getFactory() {
- return defaultFactory;
- }
- fromOtherWithContext(other) {
- // Note: other@other(unsafe) throws@this(unsafe)
- return thisFromOtherWithFactory(this.getFactory(), other);
- }
- doPreventExtensions(target, object, factory) {
- // Note: target@this(unsafe) object@other(unsafe) throws@this(unsafe)
- let keys; // @other(safe-array-of-prim)
- try {
- keys = otherReflectOwnKeys(object);
- } catch (e) { // @other(unsafe)
- throw thisFromOtherForThrow(e);
- }
- for (let i = 0; i < keys.length; i++) {
- const key = keys[i]; // @prim
- let desc;
- try {
- desc = otherSafeGetOwnPropertyDescriptor(object, key);
- } catch (e) { // @other(unsafe)
- throw thisFromOtherForThrow(e);
- }
- if (!desc) continue;
- if (!desc.configurable) {
- const current = thisSafeGetOwnPropertyDescriptor(target, key);
- if (current && !current.configurable) continue;
- if (desc.get || desc.set) {
- desc.get = this.fromOtherWithContext(desc.get);
- desc.set = this.fromOtherWithContext(desc.set);
- } else if (typeof object === 'function' && (key === 'caller' || key === 'callee' || key === 'arguments')) {
- desc.value = null;
- } else {
- desc.value = this.fromOtherWithContext(desc.value);
- }
- } else {
- if (desc.get || desc.set) {
- desc = {
- __proto__: null,
- configurable: true,
- enumerable: desc.enumerable,
- writable: true,
- value: null
- };
- } else {
- desc.value = null;
- }
- }
- if (!thisReflectDefineProperty(target, key, desc)) throw thisUnexpected();
- }
- if (!thisReflectPreventExtensions(target)) throw thisUnexpected();
- }
- get(target, key, receiver) {
- // Note: target@this(unsafe) key@prim receiver@this(unsafe) throws@this(unsafe)
- const object = this.getObject(); // @other(unsafe)
- switch (key) {
- case 'constructor': {
- const desc = otherSafeGetOwnPropertyDescriptor(object, key);
- if (desc) return thisDefaultGet(this, object, key, desc);
- const proto = thisReflectGetPrototypeOf(target);
- return proto === null ? undefined : proto.constructor;
- }
- case '__proto__': {
- const desc = otherSafeGetOwnPropertyDescriptor(object, key);
- if (desc) return thisDefaultGet(this, object, key, desc);
- return thisReflectGetPrototypeOf(target);
- }
- case thisSymbolToStringTag:
- if (!thisOtherHasOwnProperty(object, thisSymbolToStringTag)) {
- const proto = thisReflectGetPrototypeOf(target);
- const name = thisReflectApply(thisMapGet, protoName, [proto]);
- if (name) return name;
- }
- break;
- case 'arguments':
- case 'caller':
- case 'callee':
- if (typeof object === 'function' && thisOtherHasOwnProperty(object, key)) {
- throw thisThrowCallerCalleeArgumentsAccess(key);
- }
- break;
- }
- let ret; // @other(unsafe)
- try {
- ret = otherReflectGet(object, key);
- } catch (e) { // @other(unsafe)
- throw thisFromOtherForThrow(e);
- }
- return this.fromOtherWithContext(ret);
- }
- set(target, key, value, receiver) {
- // Note: target@this(unsafe) key@prim value@this(unsafe) receiver@this(unsafe) throws@this(unsafe)
- const object = this.getObject(); // @other(unsafe)
- if (key === '__proto__' && !thisOtherHasOwnProperty(object, key)) {
- return this.setPrototypeOf(target, value);
- }
- try {
- value = otherFromThis(value);
- return otherReflectSet(object, key, value) === true;
- } catch (e) { // @other(unsafe)
- throw thisFromOtherForThrow(e);
- }
- }
- getPrototypeOf(target) {
- // Note: target@this(unsafe)
- return thisReflectGetPrototypeOf(target);
- }
- setPrototypeOf(target, value) {
- // Note: target@this(unsafe) throws@this(unsafe)
- throw new VMError(OPNA);
- }
- apply(target, context, args) {
- // Note: target@this(unsafe) context@this(unsafe) args@this(safe-array) throws@this(unsafe)
- const object = this.getObject(); // @other(unsafe)
- let ret; // @other(unsafe)
- try {
- context = otherFromThis(context);
- args = otherFromThisArguments(args);
- ret = otherReflectApply(object, context, args);
- } catch (e) { // @other(unsafe)
- throw thisFromOtherForThrow(e);
- }
- return thisFromOther(ret);
- }
- construct(target, args, newTarget) {
- // Note: target@this(unsafe) args@this(safe-array) newTarget@this(unsafe) throws@this(unsafe)
- const object = this.getObject(); // @other(unsafe)
- let ret; // @other(unsafe)
- try {
- args = otherFromThisArguments(args);
- ret = otherReflectConstruct(object, args);
- } catch (e) { // @other(unsafe)
- throw thisFromOtherForThrow(e);
- }
- return thisFromOtherWithFactory(this.getFactory(), ret, thisFromOther(object));
- }
- getOwnPropertyDescriptorDesc(target, prop, desc) {
- // Note: target@this(unsafe) prop@prim desc@other{safe} throws@this(unsafe)
- const object = this.getObject(); // @other(unsafe)
- if (desc && typeof object === 'function' && (prop === 'arguments' || prop === 'caller' || prop === 'callee')) desc.value = null;
- return desc;
- }
- getOwnPropertyDescriptor(target, prop) {
- // Note: target@this(unsafe) prop@prim throws@this(unsafe)
- const object = this.getObject(); // @other(unsafe)
- let desc; // @other(safe)
- try {
- desc = otherSafeGetOwnPropertyDescriptor(object, prop);
- } catch (e) { // @other(unsafe)
- throw thisFromOtherForThrow(e);
- }
- desc = this.getOwnPropertyDescriptorDesc(target, prop, desc);
- if (!desc) return undefined;
- let thisDesc;
- if (desc.get || desc.set) {
- thisDesc = {
- __proto__: null,
- get: this.fromOtherWithContext(desc.get),
- set: this.fromOtherWithContext(desc.set),
- enumerable: desc.enumerable === true,
- configurable: desc.configurable === true
- };
- } else {
- thisDesc = {
- __proto__: null,
- value: this.fromOtherWithContext(desc.value),
- writable: desc.writable === true,
- enumerable: desc.enumerable === true,
- configurable: desc.configurable === true
- };
- }
- if (!thisDesc.configurable) {
- const oldDesc = thisSafeGetOwnPropertyDescriptor(target, prop);
- if (!oldDesc || oldDesc.configurable || oldDesc.writable !== thisDesc.writable) {
- if (!thisReflectDefineProperty(target, prop, thisDesc)) throw thisUnexpected();
- }
- }
- return thisDesc;
- }
- definePropertyDesc(target, prop, desc) {
- // Note: target@this(unsafe) prop@prim desc@this(safe) throws@this(unsafe)
- return desc;
- }
- defineProperty(target, prop, desc) {
- // Note: target@this(unsafe) prop@prim desc@this(unsafe) throws@this(unsafe)
- const object = this.getObject(); // @other(unsafe)
- if (!thisReflectSetPrototypeOf(desc, null)) throw thisUnexpected();
- desc = this.definePropertyDesc(target, prop, desc);
- if (!desc) return false;
- let otherDesc = {__proto__: null};
- let hasFunc = true;
- let hasValue = true;
- let hasBasic = true;
- hasFunc &= otherFromThisIfAvailable(otherDesc, desc, 'get');
- hasFunc &= otherFromThisIfAvailable(otherDesc, desc, 'set');
- hasValue &= otherFromThisIfAvailable(otherDesc, desc, 'value');
- hasValue &= otherFromThisIfAvailable(otherDesc, desc, 'writable');
- hasBasic &= otherFromThisIfAvailable(otherDesc, desc, 'enumerable');
- hasBasic &= otherFromThisIfAvailable(otherDesc, desc, 'configurable');
- try {
- if (!otherReflectDefineProperty(object, prop, otherDesc)) return false;
- if (otherDesc.configurable !== true && (!hasBasic || !(hasFunc || hasValue))) {
- otherDesc = otherSafeGetOwnPropertyDescriptor(object, prop);
- }
- } catch (e) { // @other(unsafe)
- throw thisFromOtherForThrow(e);
- }
- if (!otherDesc.configurable) {
- let thisDesc;
- if (otherDesc.get || otherDesc.set) {
- thisDesc = {
- __proto__: null,
- get: this.fromOtherWithContext(otherDesc.get),
- set: this.fromOtherWithContext(otherDesc.set),
- enumerable: otherDesc.enumerable,
- configurable: otherDesc.configurable
- };
- } else {
- thisDesc = {
- __proto__: null,
- value: this.fromOtherWithContext(otherDesc.value),
- writable: otherDesc.writable,
- enumerable: otherDesc.enumerable,
- configurable: otherDesc.configurable
- };
- }
- if (!thisReflectDefineProperty(target, prop, thisDesc)) throw thisUnexpected();
- }
- return true;
- }
- deleteProperty(target, prop) {
- // Note: target@this(unsafe) prop@prim throws@this(unsafe)
- const object = this.getObject(); // @other(unsafe)
- try {
- return otherReflectDeleteProperty(object, prop) === true;
- } catch (e) { // @other(unsafe)
- throw thisFromOtherForThrow(e);
- }
- }
- has(target, key) {
- // Note: target@this(unsafe) key@prim throws@this(unsafe)
- const object = this.getObject(); // @other(unsafe)
- try {
- return otherReflectHas(object, key) === true;
- } catch (e) { // @other(unsafe)
- throw thisFromOtherForThrow(e);
- }
- }
- isExtensible(target) {
- // Note: target@this(unsafe) throws@this(unsafe)
- const object = this.getObject(); // @other(unsafe)
- try {
- if (otherReflectIsExtensible(object)) return true;
- } catch (e) { // @other(unsafe)
- throw thisFromOtherForThrow(e);
- }
- if (thisReflectIsExtensible(target)) {
- this.doPreventExtensions(target, object, this);
- }
- return false;
- }
- ownKeys(target) {
- // Note: target@this(unsafe) throws@this(unsafe)
- const object = this.getObject(); // @other(unsafe)
- let res; // @other(unsafe)
- try {
- res = otherReflectOwnKeys(object);
- } catch (e) { // @other(unsafe)
- throw thisFromOtherForThrow(e);
- }
- return thisFromOther(res);
- }
- preventExtensions(target) {
- // Note: target@this(unsafe) throws@this(unsafe)
- const object = this.getObject(); // @other(unsafe)
- try {
- if (!otherReflectPreventExtensions(object)) return false;
- } catch (e) { // @other(unsafe)
- throw thisFromOtherForThrow(e);
- }
- if (thisReflectIsExtensible(target)) {
- this.doPreventExtensions(target, object, this);
- }
- return true;
- }
- enumerate(target) {
- // Note: target@this(unsafe) throws@this(unsafe)
- const object = this.getObject(); // @other(unsafe)
- let res; // @other(unsafe)
- try {
- res = otherReflectEnumerate(object);
- } catch (e) { // @other(unsafe)
- throw thisFromOtherForThrow(e);
- }
- return this.fromOtherWithContext(res);
- }
- }
- BaseHandler.prototype[thisSymbolNodeJSUtilInspectCustom] = undefined;
- BaseHandler.prototype[thisSymbolToStringTag] = 'VM2 Wrapper';
- BaseHandler.prototype[thisSymbolIterator] = undefined;
- function defaultFactory(object) {
- // Note: other@other(unsafe) returns@this(unsafe) throws@this(unsafe)
- return new BaseHandler(object);
- }
- class ProtectedHandler extends BaseHandler {
- getFactory() {
- return protectedFactory;
- }
- set(target, key, value, receiver) {
- // Note: target@this(unsafe) key@prim value@this(unsafe) receiver@this(unsafe) throws@this(unsafe)
- if (typeof value === 'function') {
- return thisReflectDefineProperty(receiver, key, {
- __proto__: null,
- value: value,
- writable: true,
- enumerable: true,
- configurable: true
- }) === true;
- }
- return super.set(target, key, value, receiver);
- }
- definePropertyDesc(target, prop, desc) {
- // Note: target@this(unsafe) prop@prim desc@this(safe) throws@this(unsafe)
- if (desc && (desc.set || desc.get || typeof desc.value === 'function')) return undefined;
- return desc;
- }
- }
- function protectedFactory(object) {
- // Note: other@other(unsafe) returns@this(unsafe) throws@this(unsafe)
- return new ProtectedHandler(object);
- }
- class ReadOnlyHandler extends BaseHandler {
- getFactory() {
- return readonlyFactory;
- }
- set(target, key, value, receiver) {
- // Note: target@this(unsafe) key@prim value@this(unsafe) receiver@this(unsafe) throws@this(unsafe)
- return thisReflectDefineProperty(receiver, key, {
- __proto__: null,
- value: value,
- writable: true,
- enumerable: true,
- configurable: true
- });
- }
- setPrototypeOf(target, value) {
- // Note: target@this(unsafe) throws@this(unsafe)
- return false;
- }
- defineProperty(target, prop, desc) {
- // Note: target@this(unsafe) prop@prim desc@this(unsafe) throws@this(unsafe)
- return false;
- }
- deleteProperty(target, prop) {
- // Note: target@this(unsafe) prop@prim throws@this(unsafe)
- return false;
- }
- isExtensible(target) {
- // Note: target@this(unsafe) throws@this(unsafe)
- return false;
- }
- preventExtensions(target) {
- // Note: target@this(unsafe) throws@this(unsafe)
- return false;
- }
- }
- function readonlyFactory(object) {
- // Note: other@other(unsafe) returns@this(unsafe) throws@this(unsafe)
- return new ReadOnlyHandler(object);
- }
- class ReadOnlyMockHandler extends ReadOnlyHandler {
- constructor(object, mock) {
- // Note: object@other(unsafe) mock:this(unsafe) throws@this(unsafe)
- super(object);
- this.mock = mock;
- }
- get(target, key, receiver) {
- // Note: target@this(unsafe) key@prim receiver@this(unsafe) throws@this(unsafe)
- const object = this.getObject(); // @other(unsafe)
- const mock = this.mock;
- if (thisReflectApply(thisObjectHasOwnProperty, mock, key) && !thisOtherHasOwnProperty(object, key)) {
- return mock[key];
- }
- return super.get(target, key, receiver);
- }
- }
- function thisFromOther(other) {
- // Note: other@other(unsafe) returns@this(unsafe) throws@this(unsafe)
- return thisFromOtherWithFactory(defaultFactory, other);
- }
- function thisProxyOther(factory, other, proto) {
- const target = thisCreateTargetObject(other, proto);
- const handler = factory(other);
- const proxy = new ThisProxy(target, handler);
- try {
- otherReflectApply(otherWeakMapSet, mappingThisToOther, [proxy, other]);
- registerProxy(proxy, handler);
- } catch (e) {
- throw new VMError('Unexpected error');
- }
- if (!isHost) {
- thisReflectApply(thisWeakMapSet, mappingOtherToThis, [other, proxy]);
- return proxy;
- }
- const proxy2 = new ThisProxy(proxy, emptyForzenObject);
- try {
- otherReflectApply(otherWeakMapSet, mappingThisToOther, [proxy2, other]);
- registerProxy(proxy2, handler);
- } catch (e) {
- throw new VMError('Unexpected error');
- }
- thisReflectApply(thisWeakMapSet, mappingOtherToThis, [other, proxy2]);
- return proxy2;
- }
- function thisEnsureThis(other) {
- const type = typeof other;
- switch (type) {
- case 'object':
- if (other === null) {
- return null;
- }
- // fallthrough
- case 'function':
- let proto = thisReflectGetPrototypeOf(other);
- if (!proto) {
- return other;
- }
- while (proto) {
- const mapping = thisReflectApply(thisMapGet, protoMappings, [proto]);
- if (mapping) {
- const mapped = thisReflectApply(thisWeakMapGet, mappingOtherToThis, [other]);
- if (mapped) return mapped;
- return mapping(defaultFactory, other);
- }
- proto = thisReflectGetPrototypeOf(proto);
- }
- return other;
- case 'undefined':
- case 'string':
- case 'number':
- case 'boolean':
- case 'symbol':
- case 'bigint':
- return other;
- default: // new, unknown types can be dangerous
- throw new VMError(`Unknown type '${type}'`);
- }
- }
- function thisFromOtherForThrow(other) {
- for (let loop = 0; loop < 10; loop++) {
- const type = typeof other;
- switch (type) {
- case 'object':
- if (other === null) {
- return null;
- }
- // fallthrough
- case 'function':
- const mapped = thisReflectApply(thisWeakMapGet, mappingOtherToThis, [other]);
- if (mapped) return mapped;
- let proto;
- try {
- proto = otherReflectGetPrototypeOf(other);
- } catch (e) { // @other(unsafe)
- other = e;
- break;
- }
- if (!proto) {
- return thisProxyOther(defaultFactory, other, null);
- }
- for (;;) {
- const mapping = thisReflectApply(thisMapGet, protoMappings, [proto]);
- if (mapping) return mapping(defaultFactory, other);
- try {
- proto = otherReflectGetPrototypeOf(proto);
- } catch (e) { // @other(unsafe)
- other = e;
- break;
- }
- if (!proto) return thisProxyOther(defaultFactory, other, thisObjectPrototype);
- }
- break;
- case 'undefined':
- case 'string':
- case 'number':
- case 'boolean':
- case 'symbol':
- case 'bigint':
- return other;
- default: // new, unknown types can be dangerous
- throw new VMError(`Unknown type '${type}'`);
- }
- }
- throw new VMError('Exception recursion depth');
- }
- function thisFromOtherWithFactory(factory, other, proto) {
- const type = typeof other;
- switch (type) {
- case 'object':
- if (other === null) {
- return null;
- }
- // fallthrough
- case 'function':
- const mapped = thisReflectApply(thisWeakMapGet, mappingOtherToThis, [other]);
- if (mapped) return mapped;
- if (proto) {
- return thisProxyOther(factory, other, proto);
- }
- try {
- proto = otherReflectGetPrototypeOf(other);
- } catch (e) { // @other(unsafe)
- throw thisFromOtherForThrow(e);
- }
- if (!proto) {
- return thisProxyOther(factory, other, null);
- }
- do {
- const mapping = thisReflectApply(thisMapGet, protoMappings, [proto]);
- if (mapping) return mapping(factory, other);
- try {
- proto = otherReflectGetPrototypeOf(proto);
- } catch (e) { // @other(unsafe)
- throw thisFromOtherForThrow(e);
- }
- } while (proto);
- return thisProxyOther(factory, other, thisObjectPrototype);
- case 'undefined':
- case 'string':
- case 'number':
- case 'boolean':
- case 'symbol':
- case 'bigint':
- return other;
- default: // new, unknown types can be dangerous
- throw new VMError(`Unknown type '${type}'`);
- }
- }
- function thisFromOtherArguments(args) {
- // Note: args@other(safe-array) returns@this(safe-array) throws@this(unsafe)
- const arr = [];
- for (let i = 0; i < args.length; i++) {
- const value = thisFromOther(args[i]);
- thisReflectDefineProperty(arr, i, {
- __proto__: null,
- value: value,
- writable: true,
- enumerable: true,
- configurable: true
- });
- }
- return arr;
- }
- function thisConnect(obj, other) {
- // Note: obj@this(unsafe) other@other(unsafe) throws@this(unsafe)
- try {
- otherReflectApply(otherWeakMapSet, mappingThisToOther, [obj, other]);
- } catch (e) {
- throw new VMError('Unexpected error');
- }
- thisReflectApply(thisWeakMapSet, mappingOtherToThis, [other, obj]);
- }
- thisAddProtoMapping(thisGlobalPrototypes.Object, otherGlobalPrototypes.Object);
- thisAddProtoMapping(thisGlobalPrototypes.Array, otherGlobalPrototypes.Array);
- for (let i = 0; i < globalsList.length; i++) {
- const key = globalsList[i];
- const tp = thisGlobalPrototypes[key];
- const op = otherGlobalPrototypes[key];
- if (tp && op) thisAddProtoMapping(tp, op, key);
- }
- for (let i = 0; i < errorsList.length; i++) {
- const key = errorsList[i];
- const tp = thisGlobalPrototypes[key];
- const op = otherGlobalPrototypes[key];
- if (tp && op) thisAddProtoMapping(tp, op, 'Error');
- }
- thisAddProtoMapping(thisGlobalPrototypes.VMError, otherGlobalPrototypes.VMError, 'Error');
- result.BaseHandler = BaseHandler;
- result.ProtectedHandler = ProtectedHandler;
- result.ReadOnlyHandler = ReadOnlyHandler;
- result.ReadOnlyMockHandler = ReadOnlyMockHandler;
- return result;
- }
- exports.createBridge = createBridge;
- exports.VMError = VMError;
|