1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-19 06:27:34 +00:00

LibJS: Convert JSONObject functions to ThrowCompletionOr

This commit is contained in:
Idan Horowitz 2021-10-29 00:27:25 +03:00
parent 6b954b9e76
commit e7a134a346
3 changed files with 66 additions and 102 deletions

View file

@ -33,8 +33,8 @@ void JSONObject::initialize(GlobalObject& global_object)
auto& vm = this->vm(); auto& vm = this->vm();
Object::initialize(global_object); Object::initialize(global_object);
u8 attr = Attribute::Writable | Attribute::Configurable; u8 attr = Attribute::Writable | Attribute::Configurable;
define_old_native_function(vm.names.stringify, stringify, 3, attr); define_native_function(vm.names.stringify, stringify, 3, attr);
define_old_native_function(vm.names.parse, parse, 2, attr); define_native_function(vm.names.parse, parse, 2, attr);
// 25.5.3 JSON [ @@toStringTag ], https://tc39.es/ecma262/#sec-json-@@tostringtag // 25.5.3 JSON [ @@toStringTag ], https://tc39.es/ecma262/#sec-json-@@tostringtag
define_direct_property(*vm.well_known_symbol_to_string_tag(), js_string(global_object.heap(), "JSON"), Attribute::Configurable); define_direct_property(*vm.well_known_symbol_to_string_tag(), js_string(global_object.heap(), "JSON"), Attribute::Configurable);
@ -45,22 +45,21 @@ JSONObject::~JSONObject()
} }
// 25.5.2 JSON.stringify ( value [ , replacer [ , space ] ] ), https://tc39.es/ecma262/#sec-json.stringify // 25.5.2 JSON.stringify ( value [ , replacer [ , space ] ] ), https://tc39.es/ecma262/#sec-json.stringify
String JSONObject::stringify_impl(GlobalObject& global_object, Value value, Value replacer, Value space) ThrowCompletionOr<String> JSONObject::stringify_impl(GlobalObject& global_object, Value value, Value replacer, Value space)
{ {
auto& vm = global_object.vm();
StringifyState state; StringifyState state;
if (replacer.is_object()) { if (replacer.is_object()) {
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 = TRY_OR_DISCARD(replacer.is_array(global_object)); auto is_array = TRY(replacer.is_array(global_object));
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(length_of_array_like(global_object, replacer_object));
Vector<String> list; Vector<String> list;
for (size_t i = 0; i < replacer_length; ++i) { for (size_t i = 0; i < replacer_length; ++i) {
auto replacer_value = TRY_OR_DISCARD(replacer_object.get(i)); auto replacer_value = TRY(replacer_object.get(i));
String item; String item;
if (replacer_value.is_string()) { if (replacer_value.is_string()) {
item = replacer_value.as_string().string(); item = replacer_value.as_string().string();
@ -69,7 +68,7 @@ String JSONObject::stringify_impl(GlobalObject& global_object, Value value, Valu
} else if (replacer_value.is_object()) { } else if (replacer_value.is_object()) {
auto& value_object = replacer_value.as_object(); auto& value_object = replacer_value.as_object();
if (is<StringObject>(value_object) || is<NumberObject>(value_object)) if (is<StringObject>(value_object) || is<NumberObject>(value_object))
item = TRY_OR_DISCARD(replacer_value.to_string(global_object)); item = TRY(replacer_value.to_string(global_object));
} }
if (!item.is_null() && !list.contains_slow(item)) { if (!item.is_null() && !list.contains_slow(item)) {
list.append(item); list.append(item);
@ -83,9 +82,9 @@ String JSONObject::stringify_impl(GlobalObject& global_object, Value value, Valu
if (space.is_object()) { if (space.is_object()) {
auto& space_object = space.as_object(); auto& space_object = space.as_object();
if (is<NumberObject>(space_object)) if (is<NumberObject>(space_object))
space = TRY_OR_DISCARD(space.to_number(global_object)); space = TRY(space.to_number(global_object));
else if (is<StringObject>(space_object)) else if (is<StringObject>(space_object))
space = TRY_OR_DISCARD(space.to_primitive_string(global_object)); space = TRY(space.to_primitive_string(global_object));
} }
if (space.is_number()) { if (space.is_number()) {
@ -104,15 +103,11 @@ String JSONObject::stringify_impl(GlobalObject& global_object, Value value, Valu
auto* wrapper = Object::create(global_object, global_object.object_prototype()); auto* wrapper = Object::create(global_object, global_object.object_prototype());
MUST(wrapper->create_data_property_or_throw(String::empty(), value)); MUST(wrapper->create_data_property_or_throw(String::empty(), value));
auto result = serialize_json_property(global_object, state, String::empty(), wrapper); return serialize_json_property(global_object, state, String::empty(), wrapper);
if (vm.exception())
return {};
return result;
} }
// 25.5.2 JSON.stringify ( value [ , replacer [ , space ] ] ), https://tc39.es/ecma262/#sec-json.stringify // 25.5.2 JSON.stringify ( value [ , replacer [ , space ] ] ), https://tc39.es/ecma262/#sec-json.stringify
JS_DEFINE_OLD_NATIVE_FUNCTION(JSONObject::stringify) JS_DEFINE_NATIVE_FUNCTION(JSONObject::stringify)
{ {
if (!vm.argument_count()) if (!vm.argument_count())
return js_undefined(); return js_undefined();
@ -121,7 +116,7 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(JSONObject::stringify)
auto replacer = vm.argument(1); auto replacer = vm.argument(1);
auto space = vm.argument(2); auto space = vm.argument(2);
auto string = stringify_impl(global_object, value, replacer, space); auto string = TRY(stringify_impl(global_object, value, replacer, space));
if (string.is_null()) if (string.is_null())
return js_undefined(); return js_undefined();
@ -129,26 +124,26 @@ JS_DEFINE_OLD_NATIVE_FUNCTION(JSONObject::stringify)
} }
// 25.5.2.1 SerializeJSONProperty ( state, key, holder ), https://tc39.es/ecma262/#sec-serializejsonproperty // 25.5.2.1 SerializeJSONProperty ( state, key, holder ), https://tc39.es/ecma262/#sec-serializejsonproperty
String JSONObject::serialize_json_property(GlobalObject& global_object, StringifyState& state, const PropertyKey& key, Object* holder) ThrowCompletionOr<String> JSONObject::serialize_json_property(GlobalObject& global_object, StringifyState& state, const PropertyKey& key, Object* holder)
{ {
auto& vm = global_object.vm(); auto& vm = global_object.vm();
auto value = TRY_OR_DISCARD(holder->get(key)); auto value = TRY(holder->get(key));
if (value.is_object() || value.is_bigint()) { if (value.is_object() || value.is_bigint()) {
auto* value_object = TRY_OR_DISCARD(value.to_object(global_object)); auto* value_object = TRY(value.to_object(global_object));
auto to_json = TRY_OR_DISCARD(value_object->get(vm.names.toJSON)); auto to_json = TRY(value_object->get(vm.names.toJSON));
if (to_json.is_function()) if (to_json.is_function())
value = TRY_OR_DISCARD(vm.call(to_json.as_function(), value, js_string(vm, key.to_string()))); value = TRY(vm.call(to_json.as_function(), value, js_string(vm, key.to_string())));
} }
if (state.replacer_function) if (state.replacer_function)
value = TRY_OR_DISCARD(vm.call(*state.replacer_function, holder, js_string(vm, key.to_string()), value)); value = TRY(vm.call(*state.replacer_function, holder, js_string(vm, key.to_string()), value));
if (value.is_object()) { if (value.is_object()) {
auto& value_object = value.as_object(); auto& value_object = value.as_object();
if (is<NumberObject>(value_object)) if (is<NumberObject>(value_object))
value = TRY_OR_DISCARD(value.to_number(global_object)); value = TRY(value.to_number(global_object));
else if (is<StringObject>(value_object)) else if (is<StringObject>(value_object))
value = TRY_OR_DISCARD(value.to_primitive_string(global_object)); value = TRY(value.to_primitive_string(global_object));
else if (is<BooleanObject>(value_object)) else if (is<BooleanObject>(value_object))
value = static_cast<BooleanObject&>(value_object).value_of(); value = static_cast<BooleanObject&>(value_object).value_of();
else if (is<BigIntObject>(value_object)) else if (is<BigIntObject>(value_object))
@ -166,46 +161,33 @@ String JSONObject::serialize_json_property(GlobalObject& global_object, Stringif
return MUST(value.to_string(global_object)); return MUST(value.to_string(global_object));
return "null"; return "null";
} }
if (value.is_bigint()) { if (value.is_bigint())
vm.throw_exception<TypeError>(global_object, ErrorType::JsonBigInt); return vm.throw_completion<TypeError>(global_object, ErrorType::JsonBigInt);
return {};
}
if (value.is_object() && !value.is_function()) { if (value.is_object() && !value.is_function()) {
auto is_array = TRY_OR_DISCARD(value.is_array(global_object)); auto is_array = TRY(value.is_array(global_object));
if (is_array) { if (is_array)
auto result = serialize_json_array(global_object, state, static_cast<Array&>(value.as_object())); return serialize_json_array(global_object, state, static_cast<Array&>(value.as_object()));
if (vm.exception()) return serialize_json_object(global_object, state, value.as_object());
return {};
return result;
} }
auto result = serialize_json_object(global_object, state, value.as_object()); return String {};
if (vm.exception())
return {};
return result;
}
return {};
} }
// 25.5.2.4 SerializeJSONObject ( state, value ), https://tc39.es/ecma262/#sec-serializejsonobject // 25.5.2.4 SerializeJSONObject ( state, value ), https://tc39.es/ecma262/#sec-serializejsonobject
String JSONObject::serialize_json_object(GlobalObject& global_object, StringifyState& state, Object& object) ThrowCompletionOr<String> JSONObject::serialize_json_object(GlobalObject& global_object, StringifyState& state, Object& object)
{ {
auto& vm = global_object.vm(); auto& vm = global_object.vm();
if (state.seen_objects.contains(&object)) { if (state.seen_objects.contains(&object))
vm.throw_exception<TypeError>(global_object, ErrorType::JsonCircular); return vm.throw_completion<TypeError>(global_object, ErrorType::JsonCircular);
return {};
}
state.seen_objects.set(&object); state.seen_objects.set(&object);
String previous_indent = state.indent; String previous_indent = state.indent;
state.indent = String::formatted("{}{}", state.indent, state.gap); state.indent = String::formatted("{}{}", state.indent, state.gap);
Vector<String> property_strings; Vector<String> property_strings;
auto process_property = [&](const PropertyKey& key) { auto process_property = [&](const PropertyKey& key) -> ThrowCompletionOr<void> {
if (key.is_symbol()) if (key.is_symbol())
return; return {};
auto serialized_property_string = serialize_json_property(global_object, state, key, &object); auto serialized_property_string = TRY(serialize_json_property(global_object, state, key, &object));
if (vm.exception())
return;
if (!serialized_property_string.is_null()) { if (!serialized_property_string.is_null()) {
property_strings.append(String::formatted( property_strings.append(String::formatted(
"{}:{}{}", "{}:{}{}",
@ -213,22 +195,17 @@ String JSONObject::serialize_json_object(GlobalObject& global_object, StringifyS
state.gap.is_empty() ? "" : " ", state.gap.is_empty() ? "" : " ",
serialized_property_string)); serialized_property_string));
} }
return {};
}; };
if (state.property_list.has_value()) { if (state.property_list.has_value()) {
auto property_list = state.property_list.value(); auto property_list = state.property_list.value();
for (auto& property : property_list) { for (auto& property : property_list)
process_property(property); TRY(process_property(property));
if (vm.exception())
return {};
}
} else { } else {
auto property_list = TRY_OR_DISCARD(object.enumerable_own_property_names(PropertyKind::Key)); auto property_list = TRY(object.enumerable_own_property_names(PropertyKind::Key));
for (auto& property : property_list) { for (auto& property : property_list)
process_property(property.as_string().string()); TRY(process_property(property.as_string().string()));
if (vm.exception())
return {};
}
} }
StringBuilder builder; StringBuilder builder;
if (property_strings.is_empty()) { if (property_strings.is_empty()) {
@ -265,30 +242,24 @@ String JSONObject::serialize_json_object(GlobalObject& global_object, StringifyS
} }
// 25.5.2.5 SerializeJSONArray ( state, value ), https://tc39.es/ecma262/#sec-serializejsonarray // 25.5.2.5 SerializeJSONArray ( state, value ), https://tc39.es/ecma262/#sec-serializejsonarray
String JSONObject::serialize_json_array(GlobalObject& global_object, StringifyState& state, Object& object) ThrowCompletionOr<String> JSONObject::serialize_json_array(GlobalObject& global_object, StringifyState& state, Object& object)
{ {
auto& vm = global_object.vm(); auto& vm = global_object.vm();
if (state.seen_objects.contains(&object)) { if (state.seen_objects.contains(&object))
vm.throw_exception<TypeError>(global_object, ErrorType::JsonCircular); return vm.throw_completion<TypeError>(global_object, ErrorType::JsonCircular);
return {};
}
state.seen_objects.set(&object); state.seen_objects.set(&object);
String previous_indent = state.indent; String previous_indent = state.indent;
state.indent = String::formatted("{}{}", state.indent, state.gap); state.indent = String::formatted("{}{}", state.indent, state.gap);
Vector<String> property_strings; Vector<String> property_strings;
auto length = TRY_OR_DISCARD(length_of_array_like(global_object, object)); auto length = TRY(length_of_array_like(global_object, object));
// Optimization // Optimization
property_strings.ensure_capacity(length); property_strings.ensure_capacity(length);
for (size_t i = 0; i < length; ++i) { for (size_t i = 0; i < length; ++i) {
if (vm.exception()) auto serialized_property_string = TRY(serialize_json_property(global_object, state, i, &object));
return {};
auto serialized_property_string = serialize_json_property(global_object, state, i, &object);
if (vm.exception())
return {};
if (serialized_property_string.is_null()) { if (serialized_property_string.is_null()) {
property_strings.append("null"); property_strings.append("null");
} else { } else {
@ -375,25 +346,20 @@ String JSONObject::quote_json_string(String string)
} }
// 25.5.1 JSON.parse ( text [ , reviver ] ), https://tc39.es/ecma262/#sec-json.parse // 25.5.1 JSON.parse ( text [ , reviver ] ), https://tc39.es/ecma262/#sec-json.parse
JS_DEFINE_OLD_NATIVE_FUNCTION(JSONObject::parse) JS_DEFINE_NATIVE_FUNCTION(JSONObject::parse)
{ {
auto string = TRY_OR_DISCARD(vm.argument(0).to_string(global_object)); auto string = TRY(vm.argument(0).to_string(global_object));
auto reviver = vm.argument(1); auto reviver = vm.argument(1);
auto json = JsonValue::from_string(string); auto json = JsonValue::from_string(string);
if (!json.has_value()) { if (!json.has_value())
vm.throw_exception<SyntaxError>(global_object, ErrorType::JsonMalformed); return vm.throw_completion<SyntaxError>(global_object, ErrorType::JsonMalformed);
return {};
}
Value unfiltered = parse_json_value(global_object, json.value()); Value unfiltered = parse_json_value(global_object, json.value());
if (reviver.is_function()) { if (reviver.is_function()) {
auto* root = Object::create(global_object, global_object.object_prototype()); auto* root = Object::create(global_object, global_object.object_prototype());
auto root_name = String::empty(); auto root_name = String::empty();
MUST(root->create_data_property_or_throw(root_name, unfiltered)); MUST(root->create_data_property_or_throw(root_name, unfiltered));
auto result = internalize_json_property(global_object, root, root_name, reviver.as_function()); return internalize_json_property(global_object, root, root_name, reviver.as_function());
if (vm.exception())
return {};
return result;
} }
return unfiltered; return unfiltered;
} }
@ -437,18 +403,16 @@ Array* JSONObject::parse_json_array(GlobalObject& global_object, const JsonArray
} }
// 25.5.1.1 InternalizeJSONProperty ( holder, name, reviver ), https://tc39.es/ecma262/#sec-internalizejsonproperty // 25.5.1.1 InternalizeJSONProperty ( holder, name, reviver ), https://tc39.es/ecma262/#sec-internalizejsonproperty
Value JSONObject::internalize_json_property(GlobalObject& global_object, Object* holder, PropertyKey const& name, FunctionObject& reviver) ThrowCompletionOr<Value> JSONObject::internalize_json_property(GlobalObject& global_object, Object* holder, PropertyKey const& name, FunctionObject& reviver)
{ {
auto& vm = global_object.vm(); auto& vm = global_object.vm();
auto value = TRY_OR_DISCARD(holder->get(name)); auto value = TRY(holder->get(name));
if (value.is_object()) { if (value.is_object()) {
auto is_array = TRY_OR_DISCARD(value.is_array(global_object)); auto is_array = TRY(value.is_array(global_object));
auto& value_object = value.as_object(); auto& value_object = value.as_object();
auto process_property = [&](const PropertyKey& key) -> ThrowCompletionOr<void> { auto process_property = [&](const PropertyKey& key) -> ThrowCompletionOr<void> {
auto element = internalize_json_property(global_object, &value_object, key, reviver); auto element = TRY(internalize_json_property(global_object, &value_object, key, reviver));
if (auto* exception = vm.exception())
return throw_completion(exception->value());
if (element.is_undefined()) if (element.is_undefined())
TRY(value_object.internal_delete(key)); TRY(value_object.internal_delete(key));
else else
@ -457,17 +421,17 @@ Value JSONObject::internalize_json_property(GlobalObject& global_object, Object*
}; };
if (is_array) { if (is_array) {
auto length = TRY_OR_DISCARD(length_of_array_like(global_object, value_object)); auto length = TRY(length_of_array_like(global_object, value_object));
for (size_t i = 0; i < length; ++i) for (size_t i = 0; i < length; ++i)
TRY_OR_DISCARD(process_property(i)); TRY(process_property(i));
} else { } else {
auto property_list = TRY_OR_DISCARD(value_object.enumerable_own_property_names(Object::PropertyKind::Key)); auto property_list = TRY(value_object.enumerable_own_property_names(Object::PropertyKind::Key));
for (auto& property_name : property_list) for (auto& property_name : property_list)
TRY_OR_DISCARD(process_property(property_name.as_string().string())); TRY(process_property(property_name.as_string().string()));
} }
} }
return TRY_OR_DISCARD(vm.call(reviver, Value(holder), js_string(vm, name.to_string()), value)); return TRY(vm.call(reviver, Value(holder), js_string(vm, name.to_string()), value));
} }
} }

View file

@ -20,7 +20,7 @@ public:
// The base implementation of stringify is exposed because it is used by // The base implementation of stringify is exposed because it is used by
// test-js to communicate between the JS tests and the C++ test runner. // test-js to communicate between the JS tests and the C++ test runner.
static String stringify_impl(GlobalObject&, Value value, Value replacer, Value space); static ThrowCompletionOr<String> stringify_impl(GlobalObject&, Value value, Value replacer, Value space);
static Value parse_json_value(GlobalObject&, const JsonValue&); static Value parse_json_value(GlobalObject&, const JsonValue&);
@ -34,18 +34,18 @@ private:
}; };
// Stringify helpers // Stringify helpers
static String serialize_json_property(GlobalObject&, StringifyState&, const PropertyKey& key, Object* holder); static ThrowCompletionOr<String> serialize_json_property(GlobalObject&, StringifyState&, const PropertyKey& key, Object* holder);
static String serialize_json_object(GlobalObject&, StringifyState&, Object&); static ThrowCompletionOr<String> serialize_json_object(GlobalObject&, StringifyState&, Object&);
static String serialize_json_array(GlobalObject&, StringifyState&, Object&); static ThrowCompletionOr<String> serialize_json_array(GlobalObject&, StringifyState&, Object&);
static String quote_json_string(String); static String quote_json_string(String);
// Parse helpers // Parse helpers
static Object* parse_json_object(GlobalObject&, const JsonObject&); static Object* parse_json_object(GlobalObject&, const JsonObject&);
static Array* parse_json_array(GlobalObject&, const JsonArray&); static Array* parse_json_array(GlobalObject&, const JsonArray&);
static Value internalize_json_property(GlobalObject&, Object* holder, PropertyKey const& name, FunctionObject& reviver); static ThrowCompletionOr<Value> internalize_json_property(GlobalObject&, Object* holder, PropertyKey const& name, FunctionObject& reviver);
JS_DECLARE_OLD_NATIVE_FUNCTION(stringify); JS_DECLARE_NATIVE_FUNCTION(stringify);
JS_DECLARE_OLD_NATIVE_FUNCTION(parse); JS_DECLARE_NATIVE_FUNCTION(parse);
}; };
} }

View file

@ -239,7 +239,7 @@ inline AK::Result<NonnullRefPtr<JS::SourceTextModule>, ParserError> parse_module
inline Optional<JsonValue> get_test_results(JS::Interpreter& interpreter) inline Optional<JsonValue> get_test_results(JS::Interpreter& interpreter)
{ {
auto results = MUST(interpreter.global_object().get("__TestResults__")); auto results = MUST(interpreter.global_object().get("__TestResults__"));
auto json_string = JS::JSONObject::stringify_impl(interpreter.global_object(), results, JS::js_undefined(), JS::js_undefined()); auto json_string = TRY_OR_DISCARD(JS::JSONObject::stringify_impl(interpreter.global_object(), results, JS::js_undefined(), JS::js_undefined()));
auto json = JsonValue::from_string(json_string); auto json = JsonValue::from_string(json_string);
if (!json.has_value()) if (!json.has_value())