LibJS: Change PropertyKey(ByteString) to PropertyKey(String)

...and deal with the fallout.
This commit is contained in:
Andreas Kling 2025-03-16 20:45:02 -05:00 committed by Andreas Kling
parent 3b5032c4b1
commit d7908dbff5
Notes: github-actions[bot] 2025-03-24 22:28:48 +00:00
17 changed files with 78 additions and 78 deletions

View file

@ -47,7 +47,7 @@ void JSONObject::initialize(Realm& realm)
}
// 25.5.2 JSON.stringify ( value [ , replacer [ , space ] ] ), https://tc39.es/ecma262/#sec-json.stringify
ThrowCompletionOr<Optional<ByteString>> JSONObject::stringify_impl(VM& vm, Value value, Value replacer, Value space)
ThrowCompletionOr<Optional<String>> JSONObject::stringify_impl(VM& vm, Value value, Value replacer, Value space)
{
auto& realm = *vm.current_realm();
@ -61,18 +61,18 @@ ThrowCompletionOr<Optional<ByteString>> JSONObject::stringify_impl(VM& vm, Value
if (is_array) {
auto& replacer_object = replacer.as_object();
auto replacer_length = TRY(length_of_array_like(vm, replacer_object));
Vector<ByteString> list;
Vector<String> list;
for (size_t i = 0; i < replacer_length; ++i) {
auto replacer_value = TRY(replacer_object.get(i));
Optional<ByteString> item;
Optional<String> item;
if (replacer_value.is_string()) {
item = replacer_value.as_string().byte_string();
item = replacer_value.as_string().utf8_string();
} else if (replacer_value.is_number()) {
item = MUST(replacer_value.to_byte_string(vm));
item = MUST(replacer_value.to_string(vm));
} else if (replacer_value.is_object()) {
auto& value_object = replacer_value.as_object();
if (is<StringObject>(value_object) || is<NumberObject>(value_object))
item = TRY(replacer_value.to_byte_string(vm));
item = TRY(replacer_value.to_string(vm));
}
if (item.has_value() && !list.contains_slow(*item)) {
list.append(*item);
@ -94,20 +94,20 @@ ThrowCompletionOr<Optional<ByteString>> JSONObject::stringify_impl(VM& vm, Value
if (space.is_number()) {
auto space_mv = MUST(space.to_integer_or_infinity(vm));
space_mv = min(10, space_mv);
state.gap = space_mv < 1 ? ByteString::empty() : ByteString::repeated(' ', space_mv);
state.gap = space_mv < 1 ? String {} : MUST(String::repeated(' ', space_mv));
} else if (space.is_string()) {
auto string = space.as_string().byte_string();
if (string.length() <= 10)
auto string = space.as_string().utf8_string();
if (string.bytes().size() <= 10)
state.gap = string;
else
state.gap = string.substring(0, 10);
state.gap = MUST(string.substring_from_byte_offset(0, 10));
} else {
state.gap = ByteString::empty();
state.gap = String {};
}
auto wrapper = Object::create(realm, realm.intrinsics().object_prototype());
MUST(wrapper->create_data_property_or_throw(ByteString::empty(), value));
return serialize_json_property(vm, state, ByteString::empty(), wrapper);
MUST(wrapper->create_data_property_or_throw(String {}, value));
return serialize_json_property(vm, state, String {}, wrapper);
}
// 25.5.2 JSON.stringify ( value [ , replacer [ , space ] ] ), https://tc39.es/ecma262/#sec-json.stringify
@ -128,7 +128,7 @@ JS_DEFINE_NATIVE_FUNCTION(JSONObject::stringify)
}
// 25.5.2.1 SerializeJSONProperty ( state, key, holder ), https://tc39.es/ecma262/#sec-serializejsonproperty
ThrowCompletionOr<Optional<ByteString>> JSONObject::serialize_json_property(VM& vm, StringifyState& state, PropertyKey const& key, Object* holder)
ThrowCompletionOr<Optional<String>> JSONObject::serialize_json_property(VM& vm, StringifyState& state, PropertyKey const& key, Object* holder)
{
// 1. Let value be ? Get(holder, key).
auto value = TRY(holder->get(key));
@ -179,25 +179,25 @@ ThrowCompletionOr<Optional<ByteString>> JSONObject::serialize_json_property(VM&
// 5. If value is null, return "null".
if (value.is_null())
return "null"sv;
return "null"_string;
// 6. If value is true, return "true".
// 7. If value is false, return "false".
if (value.is_boolean())
return value.as_bool() ? "true"sv : "false"sv;
return value.as_bool() ? "true"_string : "false"_string;
// 8. If Type(value) is String, return QuoteJSONString(value).
if (value.is_string())
return quote_json_string(value.as_string().byte_string());
return quote_json_string(value.as_string().utf8_string());
// 9. If Type(value) is Number, then
if (value.is_number()) {
// a. If value is finite, return ! ToString(value).
if (value.is_finite_number())
return MUST(value.to_byte_string(vm));
return MUST(value.to_string(vm));
// b. Return "null".
return "null"sv;
return "null"_string;
}
// 10. If Type(value) is BigInt, throw a TypeError exception.
@ -218,30 +218,30 @@ ThrowCompletionOr<Optional<ByteString>> JSONObject::serialize_json_property(VM&
}
// 12. Return undefined.
return Optional<ByteString> {};
return Optional<String> {};
}
// 25.5.2.4 SerializeJSONObject ( state, value ), https://tc39.es/ecma262/#sec-serializejsonobject
ThrowCompletionOr<ByteString> JSONObject::serialize_json_object(VM& vm, StringifyState& state, Object& object)
ThrowCompletionOr<String> JSONObject::serialize_json_object(VM& vm, StringifyState& state, Object& object)
{
if (state.seen_objects.contains(&object))
return vm.throw_completion<TypeError>(ErrorType::JsonCircular);
state.seen_objects.set(&object);
ByteString previous_indent = state.indent;
state.indent = ByteString::formatted("{}{}", state.indent, state.gap);
Vector<ByteString> property_strings;
String previous_indent = state.indent;
state.indent = MUST(String::formatted("{}{}", state.indent, state.gap));
Vector<String> property_strings;
auto process_property = [&](PropertyKey const& key) -> ThrowCompletionOr<void> {
if (key.is_symbol())
return {};
auto serialized_property_string = TRY(serialize_json_property(vm, state, key, &object));
if (serialized_property_string.has_value()) {
property_strings.append(ByteString::formatted(
property_strings.append(MUST(String::formatted(
"{}:{}{}",
quote_json_string(key.to_string()),
quote_json_string(MUST(String::from_byte_string(key.to_string()))),
state.gap.is_empty() ? "" : " ",
serialized_property_string));
serialized_property_string)));
}
return {};
};
@ -253,7 +253,7 @@ ThrowCompletionOr<ByteString> JSONObject::serialize_json_object(VM& vm, Stringif
} else {
auto property_list = TRY(object.enumerable_own_property_names(PropertyKind::Key));
for (auto& property : property_list)
TRY(process_property(property.as_string().byte_string()));
TRY(process_property(property.as_string().utf8_string()));
}
StringBuilder builder;
if (property_strings.is_empty()) {
@ -271,7 +271,7 @@ ThrowCompletionOr<ByteString> JSONObject::serialize_json_object(VM& vm, Stringif
} else {
builder.append('\n');
builder.append(state.indent);
auto separator = ByteString::formatted(",\n{}", state.indent);
auto separator = MUST(String::formatted(",\n{}", state.indent));
for (auto& property_string : property_strings) {
if (!first)
builder.append(separator);
@ -286,19 +286,19 @@ ThrowCompletionOr<ByteString> JSONObject::serialize_json_object(VM& vm, Stringif
state.seen_objects.remove(&object);
state.indent = previous_indent;
return builder.to_byte_string();
return builder.to_string_without_validation();
}
// 25.5.2.5 SerializeJSONArray ( state, value ), https://tc39.es/ecma262/#sec-serializejsonarray
ThrowCompletionOr<ByteString> JSONObject::serialize_json_array(VM& vm, StringifyState& state, Object& object)
ThrowCompletionOr<String> JSONObject::serialize_json_array(VM& vm, StringifyState& state, Object& object)
{
if (state.seen_objects.contains(&object))
return vm.throw_completion<TypeError>(ErrorType::JsonCircular);
state.seen_objects.set(&object);
ByteString previous_indent = state.indent;
state.indent = ByteString::formatted("{}{}", state.indent, state.gap);
Vector<ByteString> property_strings;
String previous_indent = state.indent;
state.indent = MUST(String::formatted("{}{}", state.indent, state.gap));
Vector<String> property_strings;
auto length = TRY(length_of_array_like(vm, object));
@ -308,7 +308,7 @@ ThrowCompletionOr<ByteString> JSONObject::serialize_json_array(VM& vm, Stringify
for (size_t i = 0; i < length; ++i) {
auto serialized_property_string = TRY(serialize_json_property(vm, state, i, &object));
if (!serialized_property_string.has_value()) {
property_strings.append("null"sv);
property_strings.append("null"_string);
} else {
property_strings.append(serialized_property_string.release_value());
}
@ -331,7 +331,7 @@ ThrowCompletionOr<ByteString> JSONObject::serialize_json_array(VM& vm, Stringify
} else {
builder.append("[\n"sv);
builder.append(state.indent);
auto separator = ByteString::formatted(",\n{}", state.indent);
auto separator = MUST(String::formatted(",\n{}", state.indent));
bool first = true;
for (auto& property_string : property_strings) {
if (!first)
@ -347,11 +347,11 @@ ThrowCompletionOr<ByteString> JSONObject::serialize_json_array(VM& vm, Stringify
state.seen_objects.remove(&object);
state.indent = previous_indent;
return builder.to_byte_string();
return builder.to_string_without_validation();
}
// 25.5.2.2 QuoteJSONString ( value ), https://tc39.es/ecma262/#sec-quotejsonstring
ByteString JSONObject::quote_json_string(ByteString string)
String JSONObject::quote_json_string(String string)
{
// 1. Let product be the String value consisting solely of the code unit 0x0022 (QUOTATION MARK).
StringBuilder builder;
@ -402,7 +402,7 @@ ByteString JSONObject::quote_json_string(ByteString string)
builder.append('"');
// 4. Return product.
return builder.to_byte_string();
return builder.to_string_without_validation();
}
// 25.5.1 JSON.parse ( text [ , reviver ] ), https://tc39.es/ecma262/#sec-json.parse
@ -410,7 +410,7 @@ JS_DEFINE_NATIVE_FUNCTION(JSONObject::parse)
{
auto& realm = *vm.current_realm();
auto string = TRY(vm.argument(0).to_byte_string(vm));
auto string = TRY(vm.argument(0).to_string(vm));
auto reviver = vm.argument(1);
auto json = JsonValue::from_string(string);
@ -419,7 +419,7 @@ JS_DEFINE_NATIVE_FUNCTION(JSONObject::parse)
Value unfiltered = parse_json_value(vm, json.value());
if (reviver.is_function()) {
auto root = Object::create(realm, realm.intrinsics().object_prototype());
auto root_name = ByteString::empty();
auto root_name = String {};
MUST(root->create_data_property_or_throw(root_name, unfiltered));
return internalize_json_property(vm, root, root_name, reviver.as_function());
}
@ -448,7 +448,7 @@ Object* JSONObject::parse_json_object(VM& vm, JsonObject const& json_object)
auto& realm = *vm.current_realm();
auto object = Object::create(realm, realm.intrinsics().object_prototype());
json_object.for_each_member([&](auto& key, auto& value) {
object->define_direct_property(key.to_byte_string(), parse_json_value(vm, value), default_attributes);
object->define_direct_property(key, parse_json_value(vm, value), default_attributes);
});
return object;
}
@ -488,7 +488,7 @@ ThrowCompletionOr<Value> JSONObject::internalize_json_property(VM& vm, Object* h
} else {
auto property_list = TRY(value_object.enumerable_own_property_names(Object::PropertyKind::Key));
for (auto& property_key : property_list)
TRY(process_property(property_key.as_string().byte_string()));
TRY(process_property(property_key.as_string().utf8_string()));
}
}