Skip to content
Merged
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
34 changes: 4 additions & 30 deletions lib/constructs/interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ class Interface {
generateExport() {
this.str += `
exports.is = value => {
return utils.isObject(value) && utils.hasOwn(value, implSymbol) && value[implSymbol] instanceof Impl.implementation;
return utils.isObject(value) && Object.hasOwn(value, implSymbol) && value[implSymbol] instanceof Impl.implementation;
};
exports.isImpl = value => {
return utils.isObject(value) && value instanceof Impl.implementation;
Expand Down Expand Up @@ -666,7 +666,7 @@ class Interface {
conditions.push(supportsPropertyName(O, P));
}
if (overrideBuiltins) {
conditions.push(`!utils.hasOwn(${O}, ${P})`);
conditions.push(`!Object.hasOwn(${O}, ${P})`);
} else {
// TODO: create a named properties object.
conditions.push(`!(${P} in ${O})`);
Expand Down Expand Up @@ -987,33 +987,7 @@ class Interface {
if (ownDesc === undefined) {
ownDesc = Reflect.getOwnPropertyDescriptor(target, P);
}
if (ownDesc === undefined) {
const parent = Reflect.getPrototypeOf(target);
if (parent !== null) {
return Reflect.set(parent, P, V, receiver);
}
ownDesc = { writable: true, enumerable: true, configurable: true, value: undefined };
}
if (!ownDesc.writable) {
return false;
}
if (!utils.isObject(receiver)) {
return false;
}
const existingDesc = Reflect.getOwnPropertyDescriptor(receiver, P);
let valueDesc;
if (existingDesc !== undefined) {
if (existingDesc.get || existingDesc.set) {
return false;
}
if (!existingDesc.writable) {
return false;
}
valueDesc = { value: V };
} else {
valueDesc = { writable: true, enumerable: true, configurable: true, value: V };
}
return Reflect.defineProperty(receiver, P, valueDesc);
return utils.ordinarySetWithOwnDescriptor(target, P, V, receiver, ownDesc);
}
`;

Expand Down Expand Up @@ -1065,7 +1039,7 @@ class Interface {
}
if (!overrideBuiltins) {
needFallback = true;
this.str += "if (!utils.hasOwn(target, P)) {";
this.str += "if (!Object.hasOwn(target, P)) {";
}

if (!hasNamedSetter) {
Expand Down
58 changes: 54 additions & 4 deletions lib/output/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function isObject(value) {
return (typeof value === "object" && value !== null) || typeof value === "function";
}

const hasOwn = Function.prototype.call.bind(Object.prototype.hasOwnProperty);
const call = Function.call.bind(Function.call);

// Like `Object.assign`, but using `[[GetOwnProperty]]` and `[[DefineOwnProperty]]`
// instead of `[[Get]]` and `[[Set]]` and only allowing objects
Expand Down Expand Up @@ -34,7 +34,7 @@ const ctorRegistrySymbol = Symbol.for("[webidl2js] constructor registry");
const AsyncIteratorPrototype = Object.getPrototypeOf(Object.getPrototypeOf(async function* () {}).prototype);

function initCtorRegistry(globalObject) {
if (hasOwn(globalObject, ctorRegistrySymbol)) {
if (Object.hasOwn(globalObject, ctorRegistrySymbol)) {
return globalObject[ctorRegistrySymbol];
}

Expand Down Expand Up @@ -137,6 +137,56 @@ function iteratorResult([key, value], kind) {
return { value: result, done: false };
}

function ordinarySetWithOwnDescriptor(target, property, value, receiver, ownDesc) {
if (ownDesc === undefined) {
const parent = Reflect.getPrototypeOf(target);
if (parent !== null) {
return Reflect.set(parent, property, value, receiver);
}
ownDesc = { writable: true, enumerable: true, configurable: true, value: undefined };
}
if (isDataDescriptor(ownDesc)) {
if (!ownDesc.writable) {
return false;
}
if (!isObject(receiver)) {
return false;
}
const existingDesc = Reflect.getOwnPropertyDescriptor(receiver, property);
if (existingDesc !== undefined) {
if (isAccessorDescriptor(existingDesc)) {
return false;
}
if (existingDesc.writable === false) {
return false;
}
const valueDesc = { value };
return Reflect.defineOwnProperty(receiver, property, valueDesc);
}

return Reflect.defineOwnProperty(
receiver,
property,
{ value, writable: true, enumerable: true, configurable: true }
);
}

const setter = ownDesc.set;
if (setter === undefined) {
return false;
}
call(setter, receiver, value);
return true;
}

function isDataDescriptor(desc) {
return Object.hasOwn(desc, "value") || Object.hasOwn(desc, "writable");
}

function isAccessorDescriptor(desc) {
return Object.hasOwn(desc, "get") || Object.hasOwn(desc, "set");
}

const supportsPropertyIndex = Symbol("supports property index");
const supportedPropertyIndices = Symbol("supported property indices");
const supportsPropertyName = Symbol("supports property name");
Expand All @@ -156,7 +206,6 @@ const asyncIteratorEOI = Symbol("async iterator end of iteration");

module.exports = exports = {
isObject,
hasOwn,
define,
newObjectInRealm,
wrapperSymbol,
Expand Down Expand Up @@ -186,5 +235,6 @@ module.exports = exports = {
asyncIteratorReturn,
asyncIteratorInit,
asyncIteratorEOI,
iteratorResult
iteratorResult,
ordinarySetWithOwnDescriptor
};
Loading