1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 16:07:47 +00:00

LibJS: Add ErrorTypes for Promise ownKeys trap invariant violations

This commit is contained in:
Linus Groh 2021-10-20 17:57:01 +01:00
parent ca09f20dcf
commit cf109533de
3 changed files with 65 additions and 3 deletions

View file

@ -152,6 +152,12 @@
"the type of each result list element is either String or Symbol") \ "the type of each result list element is either String or Symbol") \
M(ProxyOwnPropertyKeysDuplicates, "Proxy handler's ownKeys trap violates invariant: " \ M(ProxyOwnPropertyKeysDuplicates, "Proxy handler's ownKeys trap violates invariant: " \
"the result list may not contain duplicate elements") \ "the result list may not contain duplicate elements") \
M(ProxyOwnPropertyKeysSkippedNonconfigurableProperty, "Proxy handler's ownKeys trap violates invariant: " \
"cannot skip non-configurable property '{}'") \
M(ProxyOwnPropertyKeysNonExtensibleSkippedProperty, "Proxy handler's ownKeys trap violates invariant: " \
"cannot skip property '{}' of non-extensible object") \
M(ProxyOwnPropertyKeysNonExtensibleNewProperty, "Proxy handler's ownKeys trap violates invariant: " \
"cannot report new property '{}' of non-extensible object") \
M(ProxyPreventExtensionsReturn, "Proxy handler's preventExtensions trap violates " \ M(ProxyPreventExtensionsReturn, "Proxy handler's preventExtensions trap violates " \
"invariant: cannot return true if the target object is extensible") \ "invariant: cannot return true if the target object is extensible") \
M(ProxyRevoked, "An operation was performed on a revoked Proxy object") \ M(ProxyRevoked, "An operation was performed on a revoked Proxy object") \

View file

@ -728,7 +728,7 @@ ThrowCompletionOr<MarkedValueList> ProxyObject::internal_own_property_keys() con
for (auto& key : target_nonconfigurable_keys) { for (auto& key : target_nonconfigurable_keys) {
// a. If key is not an element of uncheckedResultKeys, throw a TypeError exception. // a. If key is not an element of uncheckedResultKeys, throw a TypeError exception.
if (!unchecked_result_keys.contains_slow(key)) if (!unchecked_result_keys.contains_slow(key))
return vm.throw_completion<TypeError>(global_object, ErrorType::FixmeAddAnErrorString); return vm.throw_completion<TypeError>(global_object, ErrorType::ProxyOwnPropertyKeysSkippedNonconfigurableProperty, key.to_string_without_side_effects());
// b. Remove key from uncheckedResultKeys. // b. Remove key from uncheckedResultKeys.
unchecked_result_keys.remove_first_matching([&](auto& value) { unchecked_result_keys.remove_first_matching([&](auto& value) {
@ -744,7 +744,7 @@ ThrowCompletionOr<MarkedValueList> ProxyObject::internal_own_property_keys() con
for (auto& key : target_configurable_keys) { for (auto& key : target_configurable_keys) {
// a. If key is not an element of uncheckedResultKeys, throw a TypeError exception. // a. If key is not an element of uncheckedResultKeys, throw a TypeError exception.
if (!unchecked_result_keys.contains_slow(key)) if (!unchecked_result_keys.contains_slow(key))
return vm.throw_completion<TypeError>(global_object, ErrorType::FixmeAddAnErrorString); return vm.throw_completion<TypeError>(global_object, ErrorType::ProxyOwnPropertyKeysNonExtensibleSkippedProperty, key.to_string_without_side_effects());
// b. Remove key from uncheckedResultKeys. // b. Remove key from uncheckedResultKeys.
unchecked_result_keys.remove_first_matching([&](auto& value) { unchecked_result_keys.remove_first_matching([&](auto& value) {
@ -754,7 +754,7 @@ ThrowCompletionOr<MarkedValueList> ProxyObject::internal_own_property_keys() con
// 22. If uncheckedResultKeys is not empty, throw a TypeError exception. // 22. If uncheckedResultKeys is not empty, throw a TypeError exception.
if (!unchecked_result_keys.is_empty()) if (!unchecked_result_keys.is_empty())
return vm.throw_completion<TypeError>(global_object, ErrorType::FixmeAddAnErrorString); return vm.throw_completion<TypeError>(global_object, ErrorType::ProxyOwnPropertyKeysNonExtensibleNewProperty, unchecked_result_keys[0].to_string_without_side_effects());
// 23. Return trapResult. // 23. Return trapResult.
return { move(trap_result) }; return { move(trap_result) };

View file

@ -0,0 +1,56 @@
// TODO: Add "[[OwnPropertyKeys]] trap normal behavior" tests
describe("[[OwnPropertyKeys]] invariants", () => {
// TODO: Add tests for other [[OwnPropertyKeys]] trap invariants
test("cannot report new property of non-extensible object", () => {
const target = Object.preventExtensions({});
const handler = {
ownKeys() {
return ["foo", "bar", "baz"];
},
};
const proxy = new Proxy(target, handler);
expect(() => {
Reflect.ownKeys(proxy);
}).toThrowWithMessage(
TypeError,
"Proxy handler's ownKeys trap violates invariant: cannot report new property 'foo' of non-extensible object"
);
});
test("cannot skip property of non-extensible object", () => {
const target = Object.preventExtensions({ foo: null });
const handler = {
ownKeys() {
return ["bar", "baz"];
},
};
const proxy = new Proxy(target, handler);
expect(() => {
Reflect.ownKeys(proxy);
}).toThrowWithMessage(
TypeError,
"Proxy handler's ownKeys trap violates invariant: cannot skip property 'foo' of non-extensible object"
);
});
test("cannot skip non-configurable property", () => {
const target = Object.defineProperty({}, "foo", { configurable: false });
const handler = {
ownKeys() {
return ["bar", "baz"];
},
};
const proxy = new Proxy(target, handler);
expect(() => {
Reflect.ownKeys(proxy);
}).toThrowWithMessage(
TypeError,
"Proxy handler's ownKeys trap violates invariant: cannot skip non-configurable property 'foo'"
);
});
});