Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Always use correct prototype for ordinary objects #67

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions proxy.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 57 additions & 10 deletions src/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,53 @@ module.exports = function proxyPolyfill() {
return o ? (typeof o === 'object' || typeof o === 'function') : false;
}

const $Object = Object;
ExE-Boss marked this conversation as resolved.
Show resolved Hide resolved

let canCreateNullProtoObjects = true;
const objectCreate =
$Object.create ||
((canCreateNullProtoObjects = !({ __proto__: null } instanceof $Object))
ExE-Boss marked this conversation as resolved.
Show resolved Hide resolved
? function create(proto) {
if (proto !== null && !isObject(proto)) {
throw new TypeError('Object prototype may only be an Object or null: ' + proto);
}

return { __proto__: proto };
}
: function create(proto) {
if (proto !== null && !isObject(proto)) {
throw new TypeError('Object prototype may only be an Object or null: ' + proto);
}

if (proto === null) {
throw new SyntaxError('Native Object.create is required to create objects with null prototype');
}

// nb. cast to convince Closure compiler that this is a constructor
var T = /** @type {!Function} */ (function T() {});
T.prototype = proto;
return new T();
});

const getProto =
$Object.getPrototypeOf ||
([].__proto__ === Array.prototype
? function getPrototypeOf(O) {
return O.__proto__;
}
: null);

/**
* @constructor
* @param {!Object} target
* @param {{apply, construct, get, set}} handler
*/
ProxyPolyfill = function(target, handler) {
const newTarget = this && this instanceof ProxyPolyfill ? this.constructor : undefined;
if (newTarget === undefined) {
throw new TypeError("Constructor Proxy requires 'new'");
}

if (!isObject(target) || !isObject(handler)) {
throw new TypeError('Cannot create proxy with a non-object as target or handler');
}
Expand Down Expand Up @@ -67,9 +108,10 @@ module.exports = function proxyPolyfill() {
handler.apply = unsafeHandler.apply.bind(unsafeHandler);
}

// Define proxy as this, or a Function (if either it's callable, or apply is set).
// TODO(samthor): Closure compiler doesn't know about 'construct', attempts to rename it.
let proxy = this;
// Define proxy as an object that extends target.[[Prototype]],
// or a Function (if either it's callable, or apply is set).
const proto = getProto ? getProto(target) : null;
let proxy;
let isMethod = false;
let isArray = false;
if (typeof target === 'function') {
Expand All @@ -78,6 +120,7 @@ module.exports = function proxyPolyfill() {
const args = Array.prototype.slice.call(arguments);
throwRevoked(usingNew ? 'construct' : 'apply');

// TODO(samthor): Closure compiler doesn't know about 'construct', attempts to rename it.
if (usingNew && handler['construct']) {
return handler['construct'].call(this, target, args);
} else if (!usingNew && handler.apply) {
Expand All @@ -98,6 +141,8 @@ module.exports = function proxyPolyfill() {
} else if (target instanceof Array) {
proxy = [];
isArray = true;
} else {
proxy = proto !== null || canCreateNullProtoObjects ? objectCreate(proto) : {};
}

// Create default getters/setters. Create different code paths as handler.get/handler.set can't
Expand Down Expand Up @@ -143,12 +188,14 @@ module.exports = function proxyPolyfill() {
// TODO(samthor): We don't allow prototype methods to be set. It's (even more) awkward.
// An alternative here would be to _just_ clone methods to keep behavior consistent.
let prototypeOk = true;
if (Object.setPrototypeOf) {
Object.setPrototypeOf(proxy, Object.getPrototypeOf(target));
} else if (proxy.__proto__) {
proxy.__proto__ = target.__proto__;
} else {
prototypeOk = false;
if (isMethod || isArray) {
if (Object.setPrototypeOf) {
Object.setPrototypeOf(proxy, proto);
} else if (proxy.__proto__) {
proxy.__proto__ = proto;
} else {
prototypeOk = false;
}
}
if (handler.get || !prototypeOk) {
for (let k in target) {
Expand All @@ -172,4 +219,4 @@ module.exports = function proxyPolyfill() {
};

return ProxyPolyfill;
}
}