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: Expose globalObject on proxyHandler [CEReactions] methods #163

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
80 changes: 67 additions & 13 deletions lib/constructs/interface.js
Expand Up @@ -90,6 +90,7 @@ class Interface {
this.namedSetter = null;
this.namedDeleter = null;
this.stringifier = null;
this.needsPerGlobalProxyHandler = false;

this.iterable = null;
this._analyzed = false;
Expand Down Expand Up @@ -229,11 +230,17 @@ class Interface {
throw new Error(msg + "duplicated indexed setter");
}
this.indexedSetter = member;
if (utils.hasCEReactions(member)) {
this.needsPerGlobalProxyHandler = true;
}
} else if (isNamed(member)) {
if (this.namedSetter) {
throw new Error(msg + "duplicated named setter");
}
this.namedSetter = member;
if (utils.hasCEReactions(member)) {
this.needsPerGlobalProxyHandler = true;
}
} else {
throw new Error(msg + "setter is neither indexed nor named");
}
Expand All @@ -253,6 +260,9 @@ class Interface {
throw new Error(msg + "duplicated named deleter");
}
this.namedDeleter = member;
if (utils.hasCEReactions(member)) {
this.needsPerGlobalProxyHandler = true;
}
} else {
throw new Error(msg + "deleter is not named");
}
Expand Down Expand Up @@ -683,7 +693,6 @@ class Interface {
${conv.body}
`;


let invocation;
if (!this.namedSetter.name) {
invocation = `
Expand All @@ -709,9 +718,21 @@ class Interface {
return prolog + invocation;
};

this.str += `
const proxyHandler = {
`;
let sep = "";
if (this.needsPerGlobalProxyHandler) {
this.str += `
const proxyHandlerCache = new WeakMap();
class ProxyHandler {
constructor(globalObject) {
this._globalObject = globalObject;
}
`;
} else {
this.str += `
const proxyHandler = {
`;
sep = ",";
}

// [[Get]] (necessary because of proxy semantics)
this.str += `
Expand All @@ -735,7 +756,7 @@ class Interface {
return undefined;
}
return Reflect.apply(getter, receiver, []);
},
}${sep}
`;

// [[HasProperty]] (necessary because of proxy semantics)
Expand All @@ -753,7 +774,7 @@ class Interface {
return Reflect.has(parent, P);
}
return false;
},
}${sep}
`;

// [[OwnPropertyKeys]]
Expand Down Expand Up @@ -784,7 +805,7 @@ class Interface {
keys.add(key);
}
return [...keys];
},
}${sep}
`;

// [[GetOwnProperty]]
Expand Down Expand Up @@ -858,7 +879,7 @@ class Interface {
}
this.str += `
return Reflect.getOwnPropertyDescriptor(target, P);
},
}${sep}
`;

// [[Set]]
Expand All @@ -870,6 +891,12 @@ class Interface {
if (target === receiver) {
`;

if (this.needsPerGlobalProxyHandler) {
this.str += `
const globalObject = this._globalObject;
`;
}

if (this.supportsIndexedProperties) {
if (hasIndexedSetter) {
this.str += `
Expand Down Expand Up @@ -966,7 +993,7 @@ class Interface {
valueDesc = { writable: true, enumerable: true, configurable: true, value: V };
}
return Reflect.defineProperty(receiver, P, valueDesc);
},
}${sep}
`;

// [[DefineOwnProperty]]
Expand All @@ -976,6 +1003,13 @@ class Interface {
return Reflect.defineProperty(target, P, desc);
}
`;

if (this.needsPerGlobalProxyHandler) {
this.str += `
const globalObject = this._globalObject;
`;
}

if (this.supportsIndexedProperties) {
this.str += `
if (utils.isArrayIndexPropName(P)) {
Expand Down Expand Up @@ -1054,7 +1088,7 @@ class Interface {
`;
}
this.str += `
},
}${sep}
`;

// [[Delete]]
Expand All @@ -1064,6 +1098,13 @@ class Interface {
return Reflect.deleteProperty(target, P);
}
`;

if (this.needsPerGlobalProxyHandler) {
this.str += `
const globalObject = this._globalObject;
`;
}

if (this.supportsIndexedProperties) {
this.str += `
if (utils.isArrayIndexPropName(P)) {
Expand Down Expand Up @@ -1109,7 +1150,7 @@ class Interface {
}
this.str += `
return Reflect.deleteProperty(target, P);
},
}${sep}
`;

// [[PreventExtensions]]
Expand Down Expand Up @@ -1168,9 +1209,22 @@ class Interface {
`;

if (this.isLegacyPlatformObj) {
this.str += `
if (this.needsPerGlobalProxyHandler) {
this.str += `
{
let proxyHandler = proxyHandlerCache.get(globalObject);
if (proxyHandler === undefined) {
proxyHandler = new ProxyHandler(globalObject);
proxyHandlerCache.set(globalObject, proxyHandler);
}
obj = new Proxy(obj, proxyHandler);
}
`;
} else {
this.str += `
obj = new Proxy(obj, proxyHandler);
`;
`;
}
}

this.str += `
Expand Down