Skip to content

Commit

Permalink
Merge pull request #413 from XmiliaH/fix-409
Browse files Browse the repository at this point in the history
Bump ecmaVersion to latest
  • Loading branch information
XmiliaH committed Feb 24, 2022
2 parents 925e3e6 + 48a93e5 commit 5c2e13b
Show file tree
Hide file tree
Showing 7 changed files with 184 additions and 88 deletions.
158 changes: 99 additions & 59 deletions lib/bridge.js
Expand Up @@ -311,7 +311,7 @@ function createBridge(otherInit, registerProxy) {
try {
return otherReflectApply(otherObjectHasOwnProperty, object, [key]) === true;
} catch (e) { // @other(unsafe)
throw thisFromOther(e);
throw thisFromOtherForThrow(e);
}
}

Expand All @@ -324,7 +324,7 @@ function createBridge(otherInit, registerProxy) {
try {
ret = otherReflectApply(getter, object, [key]);
} catch (e) {
throw thisFromOther(e);
throw thisFromOtherForThrow(e);
}
} else {
ret = desc.value;
Expand All @@ -338,7 +338,7 @@ function createBridge(otherInit, registerProxy) {
try {
to[key] = otherFromThis(from[key]);
} catch (e) { // @other(unsafe)
throw thisFromOther(e);
throw thisFromOtherForThrow(e);
}
return true;
}
Expand Down Expand Up @@ -366,15 +366,15 @@ function createBridge(otherInit, registerProxy) {
try {
keys = otherReflectOwnKeys(object);
} catch (e) { // @other(unsafe)
throw thisFromOther(e);
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 thisFromOther(e);
throw thisFromOtherForThrow(e);
}
if (!desc) continue;
if (!desc.configurable) {
Expand Down Expand Up @@ -440,7 +440,7 @@ function createBridge(otherInit, registerProxy) {
try {
ret = otherReflectGet(object, key);
} catch (e) { // @other(unsafe)
throw thisFromOther(e);
throw thisFromOtherForThrow(e);
}
return this.fromOtherWithContext(ret);
}
Expand All @@ -455,7 +455,7 @@ function createBridge(otherInit, registerProxy) {
value = otherFromThis(value);
return otherReflectSet(object, key, value) === true;
} catch (e) { // @other(unsafe)
throw thisFromOther(e);
throw thisFromOtherForThrow(e);
}
}

Expand All @@ -478,7 +478,7 @@ function createBridge(otherInit, registerProxy) {
args = otherFromThisArguments(args);
ret = otherReflectApply(object, context, args);
} catch (e) { // @other(unsafe)
throw thisFromOther(e);
throw thisFromOtherForThrow(e);
}
return thisFromOther(ret);
}
Expand All @@ -491,7 +491,7 @@ function createBridge(otherInit, registerProxy) {
args = otherFromThisArguments(args);
ret = otherReflectConstruct(object, args);
} catch (e) { // @other(unsafe)
throw thisFromOther(e);
throw thisFromOtherForThrow(e);
}
return thisFromOtherWithFactory(this.getFactory(), ret, thisFromOther(object));
}
Expand All @@ -510,7 +510,7 @@ function createBridge(otherInit, registerProxy) {
try {
desc = otherSafeGetOwnPropertyDescriptor(object, prop);
} catch (e) { // @other(unsafe)
throw thisFromOther(e);
throw thisFromOtherForThrow(e);
}

desc = this.getOwnPropertyDescriptorDesc(target, prop, desc);
Expand Down Expand Up @@ -575,7 +575,7 @@ function createBridge(otherInit, registerProxy) {
otherDesc = otherSafeGetOwnPropertyDescriptor(object, prop);
}
} catch (e) { // @other(unsafe)
throw thisFromOther(e);
throw thisFromOtherForThrow(e);
}

if (!otherDesc.configurable) {
Expand Down Expand Up @@ -608,7 +608,7 @@ function createBridge(otherInit, registerProxy) {
try {
return otherReflectDeleteProperty(object, prop) === true;
} catch (e) { // @other(unsafe)
throw thisFromOther(e);
throw thisFromOtherForThrow(e);
}
}

Expand All @@ -618,7 +618,7 @@ function createBridge(otherInit, registerProxy) {
try {
return otherReflectHas(object, key) === true;
} catch (e) { // @other(unsafe)
throw thisFromOther(e);
throw thisFromOtherForThrow(e);
}
}

Expand All @@ -628,7 +628,7 @@ function createBridge(otherInit, registerProxy) {
try {
if (otherReflectIsExtensible(object)) return true;
} catch (e) { // @other(unsafe)
throw thisFromOther(e);
throw thisFromOtherForThrow(e);
}
if (thisReflectIsExtensible(target)) {
this.doPreventExtensions(target, object, this);
Expand All @@ -643,7 +643,7 @@ function createBridge(otherInit, registerProxy) {
try {
res = otherReflectOwnKeys(object);
} catch (e) { // @other(unsafe)
throw thisFromOther(e);
throw thisFromOtherForThrow(e);
}
return thisFromOther(res);
}
Expand All @@ -654,7 +654,7 @@ function createBridge(otherInit, registerProxy) {
try {
if (!otherReflectPreventExtensions(object)) return false;
} catch (e) { // @other(unsafe)
throw thisFromOther(e);
throw thisFromOtherForThrow(e);
}
if (thisReflectIsExtensible(target)) {
this.doPreventExtensions(target, object, this);
Expand All @@ -669,7 +669,7 @@ function createBridge(otherInit, registerProxy) {
try {
res = otherReflectEnumerate(object);
} catch (e) { // @other(unsafe)
throw thisFromOther(e);
throw thisFromOtherForThrow(e);
}
return this.fromOtherWithContext(res);
}
Expand Down Expand Up @@ -817,26 +817,25 @@ function createBridge(otherInit, registerProxy) {
const type = typeof other;
switch (type) {
case 'object':
case 'function':
if (other === null) {
return null;
} else {
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);
}
}
// 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':
Expand All @@ -850,42 +849,40 @@ function createBridge(otherInit, registerProxy) {
}
}

function thisFromOtherWithFactory(factory, other, proto) {
function thisFromOtherForThrow(other) {
for (let loop = 0; loop < 10; loop++) {
const type = typeof other;
switch (type) {
case 'object':
case 'function':
if (other === null) {
return null;
} else {
const mapped = thisReflectApply(thisWeakMapGet, mappingOtherToThis, [other]);
if (mapped) return mapped;
if (proto) {
return thisProxyOther(factory, other, proto);
}
}
// 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(other);
proto = otherReflectGetPrototypeOf(proto);
} catch (e) { // @other(unsafe)
other = e;
break;
}
if (!proto) {
return thisProxyOther(factory, other, null);
}
while (proto) {
const mapping = thisReflectApply(thisMapGet, protoMappings, [proto]);
if (mapping) return mapping(factory, other);
try {
proto = otherReflectGetPrototypeOf(proto);
} catch (e) { // @other(unsafe)
other = e;
break;
}
}
return thisProxyOther(factory, other, thisObjectPrototype);
if (!proto) return thisProxyOther(defaultFactory, other, thisObjectPrototype);
}

break;
case 'undefined':
case 'string':
case 'number':
Expand All @@ -897,12 +894,55 @@ function createBridge(otherInit, registerProxy) {
default: // new, unknown types can be dangerous
throw new VMError(`Unknown type '${type}'`);
}
factory = defaultFactory;
proto = undefined;
}
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 = [];
Expand Down
2 changes: 1 addition & 1 deletion lib/nodevm.js
Expand Up @@ -410,7 +410,7 @@ class NodeVM extends VM {
}
const prefix = strict ? STRICT_MODULE_PREFIX : MODULE_PREFIX;
let scriptCode = this._compiler(code, unresolvedFilename);
scriptCode = transformer(null, scriptCode, false, false).code;
scriptCode = transformer(null, scriptCode, false, false, unresolvedFilename).code;
script = new Script(prefix + scriptCode + MODULE_SUFFIX, {
__proto__: null,
filename: unresolvedFilename,
Expand Down
2 changes: 1 addition & 1 deletion lib/resolver-compat.js
Expand Up @@ -296,7 +296,7 @@ function resolverFromOptions(vm, options, override, compiler) {
return checkedRootPaths.some(path => {
if (!filename.startsWith(path)) return false;
const len = path.length;
if (filename.length === len) return true;
if (filename.length === len || (len > 0 && path[len-1] === pa.sep)) return true;
const sep = filename[len];
return sep === '/' || sep === pa.sep;
});
Expand Down
2 changes: 1 addition & 1 deletion lib/script.js
Expand Up @@ -309,7 +309,7 @@ class VMScript {
getCompiledCode() {
if (!this._compiledCode) {
const comp = this._compiler(this._prefix + removeShebang(this._code) + this._suffix, this.filename);
const res = transformer(null, comp, false, false);
const res = transformer(null, comp, false, false, this.filename);
this._compiledCode = res.code;
this._hasAsync = res.hasAsync;
}
Expand Down
3 changes: 2 additions & 1 deletion lib/setup-sandbox.js
Expand Up @@ -308,7 +308,8 @@ const withProxy = localObjectFreeze({
const interanState = localObjectFreeze({
__proto__: null,
wrapWith(x) {
return new LocalProxy(x, withProxy);
if (x === null || x === undefined) return x;
return new LocalProxy(localObject(x), withProxy);
},
handleException: ensureThis,
import(what) {
Expand Down

0 comments on commit 5c2e13b

Please sign in to comment.