Skip to content

Commit

Permalink
Use bound functions instead of anonymous ones
Browse files Browse the repository at this point in the history
  • Loading branch information
XmiliaH committed Apr 20, 2021
1 parent a4c5b17 commit e95165b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
16 changes: 11 additions & 5 deletions lib/contextify.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ local.Reflect.isExtensible = Reflect.isExtensible;
local.Reflect.preventExtensions = Reflect.preventExtensions;
local.Reflect.getOwnPropertyDescriptor = Reflect.getOwnPropertyDescriptor;

function curry(func) {
return (thiz, args) => local.Reflect.apply(func, thiz, args);
}

const FunctionBind = curry(Function.prototype.bind);

// global is originally prototype of host.Object so it can be used to climb up from the sandbox.
Object.setPrototypeOf(global, Object.prototype);

Expand Down Expand Up @@ -51,10 +57,10 @@ const Contextified = new host.WeakMap();
const Decontextified = new host.WeakMap();

// We can't use host's hasInstance method
const hasInstance = local.Object[Symbol.hasInstance];
const ObjectHasInstance = curry(local.Object[Symbol.hasInstance]);
function instanceOf(value, construct) {
try {
return host.Reflect.apply(hasInstance, construct, [value]);
return ObjectHasInstance(construct, [value]);
} catch (ex) {
// Never pass the handled exception through!
throw new VMError('Unable to perform instanceOf check.');
Expand All @@ -63,6 +69,7 @@ function instanceOf(value, construct) {
}

const SHARED_OBJECT = {__proto__: null};
function SHARED_FUNCTION() {}

function createBaseObject(obj) {
let base;
Expand All @@ -75,9 +82,8 @@ function createBaseObject(obj) {
return this;
}
})();
// eslint-disable-next-line func-names
base = function() {};
base.prototype = null;
// Bind the function since bound functions do not have a prototype property.
base = FunctionBind(SHARED_FUNCTION, [null]);
} catch (e) {
base = () => {};
}
Expand Down
7 changes: 7 additions & 0 deletions test/vm.js
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,13 @@ describe('VM', () => {
});
}

it('proxy trap errors', () => {
const vm2 = new VM();
assert.doesNotThrow(() => {
Reflect.ownKeys(vm2.run('(function(){}).bind(null)'));
});
});

it('frozen unconfigurable access', () => {
const vm2 = new VM();
const obj = {};
Expand Down

0 comments on commit e95165b

Please sign in to comment.