summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS
diff options
context:
space:
mode:
authorIdan Horowitz <idan.horowitz@gmail.com>2021-10-16 22:20:23 +0300
committerLinus Groh <mail@linusgroh.de>2021-10-17 12:12:35 +0100
commitc488f5a59d0bddfaa225b26e8ef7ab188f2ef4f2 (patch)
tree4b1cd6c54b6d9577af98faf4ea0a7696d2820a6e /Userland/Libraries/LibJS
parent1639ed7e0a9bc063599ec193b7b6af48f7d5faba (diff)
downloadserenity-c488f5a59d0bddfaa225b26e8ef7ab188f2ef4f2.zip
LibJS: Convert to_property_key() to ThrowCompletionOr
Diffstat (limited to 'Userland/Libraries/LibJS')
-rw-r--r--Userland/Libraries/LibJS/AST.cpp10
-rw-r--r--Userland/Libraries/LibJS/Bytecode/Op.cpp17
-rw-r--r--Userland/Libraries/LibJS/Runtime/ObjectConstructor.cpp18
-rw-r--r--Userland/Libraries/LibJS/Runtime/ObjectPrototype.cpp24
-rw-r--r--Userland/Libraries/LibJS/Runtime/ProxyObject.cpp3
-rw-r--r--Userland/Libraries/LibJS/Runtime/ReflectObject.cpp24
-rw-r--r--Userland/Libraries/LibJS/Runtime/VM.cpp5
-rw-r--r--Userland/Libraries/LibJS/Runtime/Value.cpp12
-rw-r--r--Userland/Libraries/LibJS/Runtime/Value.h2
9 files changed, 42 insertions, 73 deletions
diff --git a/Userland/Libraries/LibJS/AST.cpp b/Userland/Libraries/LibJS/AST.cpp
index 5a33244f75..e1e81f749a 100644
--- a/Userland/Libraries/LibJS/AST.cpp
+++ b/Userland/Libraries/LibJS/AST.cpp
@@ -1043,7 +1043,7 @@ Reference MemberExpression::to_reference(Interpreter& interpreter, GlobalObject&
if (interpreter.exception())
return {};
// 5. Let propertyKey be ? ToPropertyKey(propertyNameValue).
- property_key = property_name_value.to_property_key(global_object);
+ property_key = TRY_OR_DISCARD(property_name_value.to_property_key(global_object));
} else {
// SuperProperty : super . IdentifierName
@@ -1273,9 +1273,7 @@ ThrowCompletionOr<Value> ClassExpression::class_definition_evaluation(Interprete
if (auto* exception = interpreter.exception())
return throw_completion(exception->value());
- auto property_key = key.to_property_key(global_object);
- if (auto* exception = interpreter.exception())
- return throw_completion(exception->value());
+ auto property_key = TRY(key.to_property_key(global_object));
auto& target = method.is_static() ? *class_constructor : class_prototype.as_object();
method_function.set_home_object(&target);
@@ -1302,9 +1300,7 @@ ThrowCompletionOr<Value> ClassExpression::class_definition_evaluation(Interprete
if (auto* exception = interpreter.exception())
return throw_completion(exception->value());
- auto property_key = key.to_property_key(global_object);
- if (auto* exception = interpreter.exception())
- return throw_completion(exception->value());
+ auto property_key = TRY(key.to_property_key(global_object));
ECMAScriptFunctionObject* initializer = nullptr;
if (field.initializer()) {
diff --git a/Userland/Libraries/LibJS/Bytecode/Op.cpp b/Userland/Libraries/LibJS/Bytecode/Op.cpp
index 6a3fc3202a..b072a0ad6f 100644
--- a/Userland/Libraries/LibJS/Bytecode/Op.cpp
+++ b/Userland/Libraries/LibJS/Bytecode/Op.cpp
@@ -201,7 +201,10 @@ void CopyObjectExcludingProperties::execute_impl(Bytecode::Interpreter& interpre
for (auto& key : own_keys) {
if (!excluded_names.contains(key)) {
- auto property_name = PropertyName(key.to_property_key(interpreter.global_object()));
+ auto property_name_or_error = key.to_property_key(interpreter.global_object());
+ if (property_name_or_error.is_error())
+ return;
+ PropertyName property_name = property_name_or_error.release_value();
auto property_value_or_error = from_object->get(property_name);
if (property_value_or_error.is_error())
return;
@@ -443,10 +446,10 @@ void GetByValue::execute_impl(Bytecode::Interpreter& interpreter) const
if (object_or_error.is_error())
return;
auto* object = object_or_error.release_value();
- auto property_key = interpreter.accumulator().to_property_key(interpreter.global_object());
- if (interpreter.vm().exception())
+ auto property_key_or_error = interpreter.accumulator().to_property_key(interpreter.global_object());
+ if (property_key_or_error.is_error())
return;
- auto value_or_error = object->get(property_key);
+ auto value_or_error = object->get(property_key_or_error.release_value());
if (value_or_error.is_error())
return;
interpreter.accumulator() = value_or_error.release_value();
@@ -458,10 +461,10 @@ void PutByValue::execute_impl(Bytecode::Interpreter& interpreter) const
if (object_or_error.is_error())
return;
auto* object = object_or_error.release_value();
- auto property_key = interpreter.reg(m_property).to_property_key(interpreter.global_object());
- if (interpreter.vm().exception())
+ auto property_key_or_error = interpreter.reg(m_property).to_property_key(interpreter.global_object());
+ if (property_key_or_error.is_error())
return;
- MUST(object->set(property_key, interpreter.accumulator(), Object::ShouldThrowExceptions::Yes));
+ MUST(object->set(property_key_or_error.release_value(), interpreter.accumulator(), Object::ShouldThrowExceptions::Yes));
}
void GetIterator::execute_impl(Bytecode::Interpreter& interpreter) const
diff --git a/Userland/Libraries/LibJS/Runtime/ObjectConstructor.cpp b/Userland/Libraries/LibJS/Runtime/ObjectConstructor.cpp
index f7cf300c4c..8a40e2b17f 100644
--- a/Userland/Libraries/LibJS/Runtime/ObjectConstructor.cpp
+++ b/Userland/Libraries/LibJS/Runtime/ObjectConstructor.cpp
@@ -247,10 +247,10 @@ JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::from_entries)
if (value_or_error.is_error())
return IterationDecision::Break;
auto value = value_or_error.release_value();
- auto property_key = key.to_property_key(global_object);
- if (vm.exception())
+ auto property_key_or_error = key.to_property_key(global_object);
+ if (property_key_or_error.is_error())
return IterationDecision::Break;
- auto result_or_error = object->create_data_property_or_throw(property_key, value);
+ auto result_or_error = object->create_data_property_or_throw(property_key_or_error.release_value(), value);
if (result_or_error.is_error())
return IterationDecision::Break;
return IterationDecision::Continue;
@@ -278,9 +278,7 @@ JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::seal)
JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::get_own_property_descriptor)
{
auto* object = TRY_OR_DISCARD(vm.argument(0).to_object(global_object));
- auto key = vm.argument(1).to_property_key(global_object);
- if (vm.exception())
- return {};
+ auto key = TRY_OR_DISCARD(vm.argument(1).to_property_key(global_object));
auto descriptor = TRY_OR_DISCARD(object->internal_get_own_property(key));
return from_property_descriptor(global_object, descriptor);
}
@@ -323,9 +321,7 @@ JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::define_property)
vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, vm.argument(0).to_string_without_side_effects());
return {};
}
- auto key = vm.argument(1).to_property_key(global_object);
- if (vm.exception())
- return {};
+ auto key = TRY_OR_DISCARD(vm.argument(1).to_property_key(global_object));
auto descriptor = TRY_OR_DISCARD(to_property_descriptor(global_object, vm.argument(2)));
TRY_OR_DISCARD(vm.argument(0).as_object().define_property_or_throw(key, descriptor));
return vm.argument(0);
@@ -409,9 +405,7 @@ JS_DEFINE_NATIVE_FUNCTION(ObjectConstructor::has_own)
auto* object = TRY_OR_DISCARD(vm.argument(0).to_object(global_object));
// 2. Let key be ? ToPropertyKey(P).
- auto key = vm.argument(1).to_property_key(global_object);
- if (vm.exception())
- return {};
+ auto key = TRY_OR_DISCARD(vm.argument(1).to_property_key(global_object));
// 3. Return ? HasOwnProperty(obj, key).
return Value(TRY_OR_DISCARD(object->has_own_property(key)));
diff --git a/Userland/Libraries/LibJS/Runtime/ObjectPrototype.cpp b/Userland/Libraries/LibJS/Runtime/ObjectPrototype.cpp
index 40c730dc19..d1afa56849 100644
--- a/Userland/Libraries/LibJS/Runtime/ObjectPrototype.cpp
+++ b/Userland/Libraries/LibJS/Runtime/ObjectPrototype.cpp
@@ -62,9 +62,7 @@ ThrowCompletionOr<bool> ObjectPrototype::internal_set_prototype_of(Object* proto
// 20.1.3.2 Object.prototype.hasOwnProperty ( V ), https://tc39.es/ecma262/#sec-object.prototype.hasownproperty
JS_DEFINE_NATIVE_FUNCTION(ObjectPrototype::has_own_property)
{
- auto property_key = vm.argument(0).to_property_key(global_object);
- if (vm.exception())
- return {};
+ auto property_key = TRY_OR_DISCARD(vm.argument(0).to_property_key(global_object));
auto* this_object = TRY_OR_DISCARD(vm.this_value(global_object).to_object(global_object));
return Value(TRY_OR_DISCARD(this_object->has_own_property(property_key)));
}
@@ -154,9 +152,7 @@ JS_DEFINE_NATIVE_FUNCTION(ObjectPrototype::value_of)
JS_DEFINE_NATIVE_FUNCTION(ObjectPrototype::property_is_enumerable)
{
// 1. Let P be ? ToPropertyKey(V).
- auto property_key = vm.argument(0).to_property_key(global_object);
- if (vm.exception())
- return {};
+ auto property_key = TRY_OR_DISCARD(vm.argument(0).to_property_key(global_object));
// 2. Let O be ? ToObject(this value).
auto* this_object = TRY_OR_DISCARD(vm.this_value(global_object).to_object(global_object));
// 3. Let desc be ? O.[[GetOwnProperty]](P).
@@ -199,9 +195,7 @@ JS_DEFINE_NATIVE_FUNCTION(ObjectPrototype::define_getter)
auto descriptor = PropertyDescriptor { .get = &getter.as_function(), .enumerable = true, .configurable = true };
- auto key = vm.argument(0).to_property_key(global_object);
- if (vm.exception())
- return {};
+ auto key = TRY_OR_DISCARD(vm.argument(0).to_property_key(global_object));
TRY_OR_DISCARD(object->define_property_or_throw(key, descriptor));
@@ -221,9 +215,7 @@ JS_DEFINE_NATIVE_FUNCTION(ObjectPrototype::define_setter)
auto descriptor = PropertyDescriptor { .set = &setter.as_function(), .enumerable = true, .configurable = true };
- auto key = vm.argument(0).to_property_key(global_object);
- if (vm.exception())
- return {};
+ auto key = TRY_OR_DISCARD(vm.argument(0).to_property_key(global_object));
TRY_OR_DISCARD(object->define_property_or_throw(key, descriptor));
@@ -235,9 +227,7 @@ JS_DEFINE_NATIVE_FUNCTION(ObjectPrototype::lookup_getter)
{
auto* object = TRY_OR_DISCARD(vm.this_value(global_object).to_object(global_object));
- auto key = vm.argument(0).to_property_key(global_object);
- if (vm.exception())
- return {};
+ auto key = TRY_OR_DISCARD(vm.argument(0).to_property_key(global_object));
while (object) {
auto desc = TRY_OR_DISCARD(object->internal_get_own_property(key));
@@ -257,9 +247,7 @@ JS_DEFINE_NATIVE_FUNCTION(ObjectPrototype::lookup_setter)
{
auto* object = TRY_OR_DISCARD(vm.this_value(global_object).to_object(global_object));
- auto key = vm.argument(0).to_property_key(global_object);
- if (vm.exception())
- return {};
+ auto key = TRY_OR_DISCARD(vm.argument(0).to_property_key(global_object));
while (object) {
auto desc = TRY_OR_DISCARD(object->internal_get_own_property(key));
diff --git a/Userland/Libraries/LibJS/Runtime/ProxyObject.cpp b/Userland/Libraries/LibJS/Runtime/ProxyObject.cpp
index 8953a2c0ca..2a47c2909f 100644
--- a/Userland/Libraries/LibJS/Runtime/ProxyObject.cpp
+++ b/Userland/Libraries/LibJS/Runtime/ProxyObject.cpp
@@ -673,8 +673,7 @@ ThrowCompletionOr<MarkedValueList> ProxyObject::internal_own_property_keys() con
auto& vm = global_object.vm();
if (!value.is_string() && !value.is_symbol())
return vm.throw_completion<TypeError>(global_object, ErrorType::ProxyOwnPropertyKeysNotStringOrSymbol);
- auto property_key = value.to_property_key(global_object);
- VERIFY(!vm.exception());
+ auto property_key = MUST(value.to_property_key(global_object));
unique_keys.set(property_key, AK::HashSetExistingEntryBehavior::Keep);
return {};
}));
diff --git a/Userland/Libraries/LibJS/Runtime/ReflectObject.cpp b/Userland/Libraries/LibJS/Runtime/ReflectObject.cpp
index 6013a6ecbd..dec045be2f 100644
--- a/Userland/Libraries/LibJS/Runtime/ReflectObject.cpp
+++ b/Userland/Libraries/LibJS/Runtime/ReflectObject.cpp
@@ -112,9 +112,7 @@ JS_DEFINE_NATIVE_FUNCTION(ReflectObject::define_property)
}
// 2. Let key be ? ToPropertyKey(propertyKey).
- auto key = property_key.to_property_key(global_object);
- if (vm.exception())
- return {};
+ auto key = TRY_OR_DISCARD(property_key.to_property_key(global_object));
// 3. Let desc be ? ToPropertyDescriptor(attributes).
auto descriptor = TRY_OR_DISCARD(to_property_descriptor(global_object, attributes));
@@ -136,9 +134,7 @@ JS_DEFINE_NATIVE_FUNCTION(ReflectObject::delete_property)
}
// 2. Let key be ? ToPropertyKey(propertyKey).
- auto key = property_key.to_property_key(global_object);
- if (vm.exception())
- return {};
+ auto key = TRY_OR_DISCARD(property_key.to_property_key(global_object));
// 3. Return ? target.[[Delete]](key).
return Value(TRY_OR_DISCARD(target.as_object().internal_delete(key)));
@@ -158,9 +154,7 @@ JS_DEFINE_NATIVE_FUNCTION(ReflectObject::get)
}
// 2. Let key be ? ToPropertyKey(propertyKey).
- auto key = property_key.to_property_key(global_object);
- if (vm.exception())
- return {};
+ auto key = TRY_OR_DISCARD(property_key.to_property_key(global_object));
// 3. If receiver is not present, then
if (vm.argument_count() < 3) {
@@ -185,9 +179,7 @@ JS_DEFINE_NATIVE_FUNCTION(ReflectObject::get_own_property_descriptor)
}
// 2. Let key be ? ToPropertyKey(propertyKey).
- auto key = property_key.to_property_key(global_object);
- if (vm.exception())
- return {};
+ auto key = TRY_OR_DISCARD(property_key.to_property_key(global_object));
// 3. Let desc be ? target.[[GetOwnProperty]](key).
auto descriptor = TRY_OR_DISCARD(target.as_object().internal_get_own_property(key));
@@ -224,9 +216,7 @@ JS_DEFINE_NATIVE_FUNCTION(ReflectObject::has)
}
// 2. Let key be ? ToPropertyKey(propertyKey).
- auto key = property_key.to_property_key(global_object);
- if (vm.exception())
- return {};
+ auto key = TRY_OR_DISCARD(property_key.to_property_key(global_object));
// 3. Return ? target.[[HasProperty]](key).
return Value(TRY_OR_DISCARD(target.as_object().internal_has_property(key)));
@@ -295,9 +285,7 @@ JS_DEFINE_NATIVE_FUNCTION(ReflectObject::set)
}
// 2. Let key be ? ToPropertyKey(propertyKey).
- auto key = property_key.to_property_key(global_object);
- if (vm.exception())
- return {};
+ auto key = TRY_OR_DISCARD(property_key.to_property_key(global_object));
// 3. If receiver is not present, then
if (vm.argument_count() < 4) {
diff --git a/Userland/Libraries/LibJS/Runtime/VM.cpp b/Userland/Libraries/LibJS/Runtime/VM.cpp
index 9e3bd8ec75..37d53cf582 100644
--- a/Userland/Libraries/LibJS/Runtime/VM.cpp
+++ b/Userland/Libraries/LibJS/Runtime/VM.cpp
@@ -263,7 +263,10 @@ ThrowCompletionOr<void> VM::property_binding_initialization(BindingPattern const
auto result = expression->execute(interpreter(), global_object);
if (exception())
return;
- name = result.to_property_key(global_object);
+ auto name_or_error = result.to_property_key(global_object);
+ if (name_or_error.is_error())
+ return;
+ name = name_or_error.release_value();
});
if (auto* thrown_exception = exception())
diff --git a/Userland/Libraries/LibJS/Runtime/Value.cpp b/Userland/Libraries/LibJS/Runtime/Value.cpp
index 025b853c81..34dee02f8a 100644
--- a/Userland/Libraries/LibJS/Runtime/Value.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Value.cpp
@@ -568,12 +568,12 @@ ThrowCompletionOr<double> Value::to_double(GlobalObject& global_object) const
}
// 7.1.19 ToPropertyKey ( argument ), https://tc39.es/ecma262/#sec-topropertykey
-StringOrSymbol Value::to_property_key(GlobalObject& global_object) const
+ThrowCompletionOr<StringOrSymbol> Value::to_property_key(GlobalObject& global_object) const
{
- auto key = TRY_OR_DISCARD(to_primitive(global_object, PreferredType::String));
+ auto key = TRY(to_primitive(global_object, PreferredType::String));
if (key.is_symbol())
- return &key.as_symbol();
- return TRY_OR_DISCARD(key.to_string(global_object));
+ return StringOrSymbol { &key.as_symbol() };
+ return StringOrSymbol { TRY(key.to_string(global_object)) };
}
i32 Value::to_i32_slow_case(GlobalObject& global_object) const
@@ -1181,9 +1181,7 @@ Value in(GlobalObject& global_object, Value lhs, Value rhs)
global_object.vm().throw_exception<TypeError>(global_object, ErrorType::InOperatorWithObject);
return {};
}
- auto lhs_property_key = lhs.to_property_key(global_object);
- if (global_object.vm().exception())
- return {};
+ auto lhs_property_key = TRY_OR_DISCARD(lhs.to_property_key(global_object));
return Value(TRY_OR_DISCARD(rhs.as_object().has_property(lhs_property_key)));
}
diff --git a/Userland/Libraries/LibJS/Runtime/Value.h b/Userland/Libraries/LibJS/Runtime/Value.h
index 5cf3846741..84ab67189c 100644
--- a/Userland/Libraries/LibJS/Runtime/Value.h
+++ b/Userland/Libraries/LibJS/Runtime/Value.h
@@ -314,7 +314,7 @@ public:
ThrowCompletionOr<i64> to_bigint_int64(GlobalObject&) const;
ThrowCompletionOr<u64> to_bigint_uint64(GlobalObject&) const;
ThrowCompletionOr<double> to_double(GlobalObject&) const;
- StringOrSymbol to_property_key(GlobalObject&) const;
+ ThrowCompletionOr<StringOrSymbol> to_property_key(GlobalObject&) const;
i32 to_i32(GlobalObject& global_object) const
{
if (m_type == Type::Int32)