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

LibJS: Switch is_array to ThrowCompletionOr

This commit is contained in:
Idan Horowitz 2021-09-23 20:05:34 +03:00
parent 0b9e633482
commit 1db7e096e2
6 changed files with 15 additions and 26 deletions

View file

@ -213,7 +213,7 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayConstructor::from)
JS_DEFINE_NATIVE_FUNCTION(ArrayConstructor::is_array) JS_DEFINE_NATIVE_FUNCTION(ArrayConstructor::is_array)
{ {
auto value = vm.argument(0); auto value = vm.argument(0);
return Value(value.is_array(global_object)); return Value(TRY_OR_DISCARD(value.is_array(global_object)));
} }
// 23.1.2.3 Array.of ( ...items ), https://tc39.es/ecma262/#sec-array.of // 23.1.2.3 Array.of ( ...items ), https://tc39.es/ecma262/#sec-array.of

View file

@ -107,9 +107,7 @@ static Object* array_species_create(GlobalObject& global_object, Object& origina
{ {
auto& vm = global_object.vm(); auto& vm = global_object.vm();
auto is_array = Value(&original_array).is_array(global_object); auto is_array = TRY_OR_DISCARD(Value(&original_array).is_array(global_object));
if (vm.exception())
return {};
if (!is_array) { if (!is_array) {
auto array = Array::create(global_object, length); auto array = Array::create(global_object, length);
@ -593,7 +591,7 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::concat)
size_t n = 0; size_t n = 0;
// 23.1.3.1.1 IsConcatSpreadable ( O ), https://tc39.es/ecma262/#sec-isconcatspreadable // 23.1.3.1.1 IsConcatSpreadable ( O ), https://tc39.es/ecma262/#sec-isconcatspreadable
auto is_concat_spreadable = [&vm, &global_object](Value const& val) { auto is_concat_spreadable = [&vm, &global_object](Value const& val) -> bool {
if (!val.is_object()) { if (!val.is_object()) {
return false; return false;
} }
@ -608,7 +606,7 @@ JS_DEFINE_NATIVE_FUNCTION(ArrayPrototype::concat)
if (!spreadable.is_undefined()) if (!spreadable.is_undefined())
return spreadable.to_boolean(); return spreadable.to_boolean();
return val.is_array(global_object); return TRY_OR_DISCARD(val.is_array(global_object));
}; };
auto append_to_new_array = [&vm, &is_concat_spreadable, &new_array, &global_object, &n](Value arg) { auto append_to_new_array = [&vm, &is_concat_spreadable, &new_array, &global_object, &n](Value arg) {
@ -1895,7 +1893,7 @@ static size_t flatten_into_array(GlobalObject& global_object, Object& new_array,
return {}; return {};
} }
if (depth > 0 && value.is_array(global_object)) { if (depth > 0 && TRY_OR_DISCARD(value.is_array(global_object))) {
if (vm.did_reach_stack_space_limit()) { if (vm.did_reach_stack_space_limit()) {
vm.throw_exception<Error>(global_object, ErrorType::CallStackSizeExceeded); vm.throw_exception<Error>(global_object, ErrorType::CallStackSizeExceeded);
return {}; return {};

View file

@ -54,9 +54,7 @@ String JSONObject::stringify_impl(GlobalObject& global_object, Value value, Valu
if (replacer.as_object().is_function()) { if (replacer.as_object().is_function()) {
state.replacer_function = &replacer.as_function(); state.replacer_function = &replacer.as_function();
} else { } else {
auto is_array = replacer.is_array(global_object); auto is_array = TRY_OR_DISCARD(replacer.is_array(global_object));
if (vm.exception())
return {};
if (is_array) { if (is_array) {
auto& replacer_object = replacer.as_object(); auto& replacer_object = replacer.as_object();
auto replacer_length = TRY_OR_DISCARD(length_of_array_like(global_object, replacer_object)); auto replacer_length = TRY_OR_DISCARD(length_of_array_like(global_object, replacer_object));
@ -200,9 +198,7 @@ String JSONObject::serialize_json_property(GlobalObject& global_object, Stringif
return {}; return {};
} }
if (value.is_object() && !value.is_function()) { if (value.is_object() && !value.is_function()) {
auto is_array = value.is_array(global_object); auto is_array = TRY_OR_DISCARD(value.is_array(global_object));
if (vm.exception())
return {};
if (is_array) { if (is_array) {
auto result = serialize_json_array(global_object, state, static_cast<Array&>(value.as_object())); auto result = serialize_json_array(global_object, state, static_cast<Array&>(value.as_object()));
if (vm.exception()) if (vm.exception())
@ -479,9 +475,7 @@ Value JSONObject::internalize_json_property(GlobalObject& global_object, Object*
if (vm.exception()) if (vm.exception())
return {}; return {};
if (value.is_object()) { if (value.is_object()) {
auto is_array = value.is_array(global_object); auto is_array = TRY_OR_DISCARD(value.is_array(global_object));
if (vm.exception())
return {};
auto& value_object = value.as_object(); auto& value_object = value.as_object();
auto process_property = [&](const PropertyName& key) { auto process_property = [&](const PropertyName& key) {

View file

@ -87,9 +87,7 @@ JS_DEFINE_NATIVE_FUNCTION(ObjectPrototype::to_string)
VERIFY(object); VERIFY(object);
// 4. Let isArray be ? IsArray(O). // 4. Let isArray be ? IsArray(O).
auto is_array = Value(object).is_array(global_object); auto is_array = TRY_OR_DISCARD(Value(object).is_array(global_object));
if (vm.exception())
return {};
String builtin_tag; String builtin_tag;

View file

@ -192,8 +192,10 @@ static String double_to_string(double d)
} }
// 7.2.2 IsArray ( argument ), https://tc39.es/ecma262/#sec-isarray // 7.2.2 IsArray ( argument ), https://tc39.es/ecma262/#sec-isarray
bool Value::is_array(GlobalObject& global_object) const ThrowCompletionOr<bool> Value::is_array(GlobalObject& global_object) const
{ {
auto& vm = global_object.vm();
if (!is_object()) if (!is_object())
return false; return false;
auto& object = as_object(); auto& object = as_object();
@ -201,11 +203,8 @@ bool Value::is_array(GlobalObject& global_object) const
return true; return true;
if (is<ProxyObject>(object)) { if (is<ProxyObject>(object)) {
auto& proxy = static_cast<ProxyObject const&>(object); auto& proxy = static_cast<ProxyObject const&>(object);
if (proxy.is_revoked()) { if (proxy.is_revoked())
auto& vm = global_object.vm(); return vm.throw_completion<TypeError>(global_object, ErrorType::ProxyRevoked);
vm.throw_exception<TypeError>(global_object, ErrorType::ProxyRevoked);
return false;
}
return Value(&proxy.target()).is_array(global_object); return Value(&proxy.target()).is_array(global_object);
} }
return false; return false;

View file

@ -62,7 +62,7 @@ public:
bool is_bigint() const { return m_type == Type::BigInt; }; bool is_bigint() const { return m_type == Type::BigInt; };
bool is_nullish() const { return is_null() || is_undefined(); } bool is_nullish() const { return is_null() || is_undefined(); }
bool is_cell() const { return is_string() || is_accessor() || is_object() || is_bigint() || is_symbol(); } bool is_cell() const { return is_string() || is_accessor() || is_object() || is_bigint() || is_symbol(); }
bool is_array(GlobalObject&) const; ThrowCompletionOr<bool> is_array(GlobalObject&) const;
bool is_function() const; bool is_function() const;
bool is_constructor() const; bool is_constructor() const;
bool is_regexp(GlobalObject&) const; bool is_regexp(GlobalObject&) const;