Skip to content

Implement the named properties object for global #25

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
104 changes: 103 additions & 1 deletion lib/constructs/interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,105 @@ const IteratorPrototype = Object.create(utils.IteratorPrototype, {
}
});`;
}
}

Interface.prototype.generateNamedPropertiesObject = function () {
if (!utils.isGlobal(this.idl)) {
return;
}

let overrideBuiltins = false;
let parent = this.idl;
while (parent) {
if (utils.getExtAttr(parent.extAttrs, 'OverrideBuiltins')) {
overrideBuiltins = true;
}

const members = parent.members.filter((m) =>
m.type === 'attribute' && utils.getExtAttr(m.extAttrs, 'Unforgeable')
);
const parentInterface = this.opts.interfaces[parent.inheritance];
parent = parentInterface && parentInterface.idl;
}

this.str += `
function namedPropertiesIsVisible(P, O) {
if (!true) { // is supported
return false;
}

if (Object.getOwnPropertyDescriptor(O, P)) {
return false;
}
`;

if (overrideBuiltins) {
this.str += `
return true;`;
}

this.str += `
let prototype = Object.getPrototypeOf(O);
while (prototype) {
if (prototype.constructor.name.endsWith("PropertiesConstructor") && Object.getOwnPropertyDescriptor(prototype, P)) {
return false;
}
prototype = Object.getPrototypeOf(prototype);
}`;
this.str += `
return true;
}`;

this.str += `var ${this.name}PropertiesConstructor = function () {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can tell there's no constructor for the named properties object. It's just a singleton object. So the proxy should have as its target an empty object (probably an Object.create(null) object)

Copy link
Member Author

@Sebmaster Sebmaster May 22, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can do window.__proto__.__proto__.constructor and it'll return a function which throws Illegal constructor.

It'll not be exported on window though, that's right.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, that's EventTargetPrototype.constructor

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, that proxy object inherits the EventTarget constructor. Huh!

throw new TypeError("Illegal constructor");
}\n`;

if (this.idl.inheritance) {
this.str += `${this.name}PropertiesConstructor.prototype = Object.create(${this.idl.inheritance}.interface.prototype);
${this.name}PropertiesConstructor.prototype.constructor = ${this.name}PropertiesConstructor;\n`;
} else if (utils.getExtAttr(this.idl.extAttrs, 'LegacyArrayClass')) {
this.str += `${this.name}PropertiesConstructor.prototype = Object.create(Array.prototype);
${this.name}PropertiesConstructor.prototype.constructor = ${this.name}PropertiesConstructor;\n`;
}

const getter = this.idl.members.filter((m) => m.getter)[0].name;
this.str += `
const ${this.name}Properties = new Proxy(${this.name}PropertiesConstructor.prototype, {
defineProperty() {
return false;
},
deleteProperty() {
return false;
},
getOwnPropertyDescriptor(target, key) {
if (namedPropertiesIsVisible(key, target)) { // is visible
const value = target${getter ? "." + getter : "[utils.anonymousGetter]"}(key);
return {
value,
enumerable: true,
writable: true,
configurable: true
};
} else {
return Object.getOwnPropertyDescriptor(target, key);
}
},
get(target, key) {
if (namedPropertiesIsVisible(key, target)) { // is visible
const value = target${getter ? "." + getter : "[utils.anonymousGetter]"}(key);
return value;
} else {
return target[key];
}
},
has(target, key) {
if (namedPropertiesIsVisible(key, target)) { // is visible
return true;
} else {
return key in target;
}
}
});\n\n`;
};

Interface.prototype.generateConstructor = function () {
Expand Down Expand Up @@ -116,9 +215,11 @@ Interface.prototype.generateConstructor = function () {
}\n`;
}

if (this.idl.inheritance) {
if (this.idl.inheritance && !utils.isGlobal(this.idl)) {
this.str += `${this.name}.prototype = Object.create(${this.idl.inheritance}.interface.prototype);
${this.name}.prototype.constructor = ${this.name};\n`;
} else if (utils.isGlobal(this.idl)) {
this.str += `Object.setPrototypeOf(${this.name}.prototype, ${this.name}Properties);\n`;
}
};

Expand Down Expand Up @@ -406,6 +507,7 @@ module.exports = {
defaultPrivateData = defaultPrivateData === undefined ? {} : defaultPrivateData;\n\n`;
}

this.generateNamedPropertiesObject();
this.generateConstructor();
this.generateMixins();

Expand Down
3 changes: 3 additions & 0 deletions lib/output/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,6 @@ module.exports.tryWrapperForImpl = tryWrapperForImpl;
module.exports.tryImplForWrapper = tryImplForWrapper;
module.exports.iterInternalSymbol = iterInternalSymbol;
module.exports.IteratorPrototype = IteratorPrototype;

module.exports.anonymousGetter = Symbol("anonymousGetter");
module.exports.anonymousSetter = Symbol("anonymousSetter");
3 changes: 2 additions & 1 deletion lib/transformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ class Transformer {
obj = new Interface(instruction, {
implDir: path.resolve(outputDir, file.impl),
implSuffix: this.options.implSuffix,
customTypes
customTypes,
interfaces
});
interfaces[obj.name] = obj;
break;
Expand Down