2020-06-10 11:01:00 -07:00
|
|
|
/*
|
2021-04-22 16:53:07 -07:00
|
|
|
* Copyright (c) 2020, Matthew Olsson <mattco@serenityos.org>
|
2020-06-10 11:01:00 -07:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-06-10 11:01:00 -07:00
|
|
|
*/
|
|
|
|
|
2020-09-28 09:17:33 +02:00
|
|
|
#include <AK/Function.h>
|
2020-06-10 23:30:36 -07:00
|
|
|
#include <AK/JsonArray.h>
|
2020-06-20 16:56:01 +02:00
|
|
|
#include <AK/JsonObject.h>
|
|
|
|
#include <AK/JsonParser.h>
|
2020-06-10 11:01:00 -07:00
|
|
|
#include <AK/StringBuilder.h>
|
2021-07-01 03:24:04 +03:00
|
|
|
#include <AK/Utf8View.h>
|
2021-06-19 21:45:00 +01:00
|
|
|
#include <LibJS/Runtime/AbstractOperations.h>
|
2020-06-10 11:01:00 -07:00
|
|
|
#include <LibJS/Runtime/Array.h>
|
2021-01-01 17:46:39 +01:00
|
|
|
#include <LibJS/Runtime/BigIntObject.h>
|
|
|
|
#include <LibJS/Runtime/BooleanObject.h>
|
2020-06-10 11:01:00 -07:00
|
|
|
#include <LibJS/Runtime/Error.h>
|
|
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
|
|
|
#include <LibJS/Runtime/JSONObject.h>
|
2021-01-01 17:46:39 +01:00
|
|
|
#include <LibJS/Runtime/NumberObject.h>
|
2020-06-10 11:01:00 -07:00
|
|
|
#include <LibJS/Runtime/Object.h>
|
2021-01-01 17:46:39 +01:00
|
|
|
#include <LibJS/Runtime/StringObject.h>
|
2020-06-10 11:01:00 -07:00
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
2020-06-20 17:11:11 +02:00
|
|
|
JSONObject::JSONObject(GlobalObject& global_object)
|
2020-06-23 17:21:53 +02:00
|
|
|
: Object(*global_object.object_prototype())
|
2020-06-20 17:11:11 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-07-22 17:50:18 +02:00
|
|
|
void JSONObject::initialize(GlobalObject& global_object)
|
2020-06-10 11:01:00 -07:00
|
|
|
{
|
2020-10-14 00:03:58 +02:00
|
|
|
auto& vm = this->vm();
|
2020-07-22 17:50:18 +02:00
|
|
|
Object::initialize(global_object);
|
2020-06-10 11:01:00 -07:00
|
|
|
u8 attr = Attribute::Writable | Attribute::Configurable;
|
2020-10-14 00:03:58 +02:00
|
|
|
define_native_function(vm.names.stringify, stringify, 3, attr);
|
|
|
|
define_native_function(vm.names.parse, parse, 2, attr);
|
2021-06-13 00:22:35 +01:00
|
|
|
|
|
|
|
// 25.5.3 JSON [ @@toStringTag ], https://tc39.es/ecma262/#sec-json-@@tostringtag
|
2021-07-06 02:15:08 +03:00
|
|
|
define_direct_property(*vm.well_known_symbol_to_string_tag(), js_string(global_object.heap(), "JSON"), Attribute::Configurable);
|
2020-06-10 11:01:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
JSONObject::~JSONObject()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-07-01 03:24:04 +03:00
|
|
|
// 25.5.2 JSON.stringify ( value [ , replacer [ , space ] ] ), https://tc39.es/ecma262/#sec-json.stringify
|
2020-09-27 18:52:42 +02:00
|
|
|
String JSONObject::stringify_impl(GlobalObject& global_object, Value value, Value replacer, Value space)
|
2020-08-25 22:18:32 +04:30
|
|
|
{
|
2020-09-27 18:52:42 +02:00
|
|
|
auto& vm = global_object.vm();
|
2020-06-10 11:01:00 -07:00
|
|
|
StringifyState state;
|
|
|
|
|
|
|
|
if (replacer.is_object()) {
|
|
|
|
if (replacer.as_object().is_function()) {
|
|
|
|
state.replacer_function = &replacer.as_function();
|
2021-07-01 03:24:04 +03:00
|
|
|
} else {
|
2021-09-23 20:05:34 +03:00
|
|
|
auto is_array = TRY_OR_DISCARD(replacer.is_array(global_object));
|
2021-07-01 03:24:04 +03:00
|
|
|
if (is_array) {
|
|
|
|
auto& replacer_object = replacer.as_object();
|
2021-09-21 21:59:49 +03:00
|
|
|
auto replacer_length = TRY_OR_DISCARD(length_of_array_like(global_object, replacer_object));
|
2021-07-01 03:24:04 +03:00
|
|
|
Vector<String> list;
|
|
|
|
for (size_t i = 0; i < replacer_length; ++i) {
|
|
|
|
auto replacer_value = replacer_object.get(i);
|
2020-09-27 18:52:42 +02:00
|
|
|
if (vm.exception())
|
2020-06-10 11:01:00 -07:00
|
|
|
return {};
|
2021-07-01 03:24:04 +03:00
|
|
|
String item;
|
|
|
|
if (replacer_value.is_string()) {
|
|
|
|
item = replacer_value.as_string().string();
|
|
|
|
} else if (replacer_value.is_number()) {
|
|
|
|
item = replacer_value.to_string(global_object);
|
|
|
|
} else if (replacer_value.is_object()) {
|
|
|
|
auto& value_object = replacer_value.as_object();
|
|
|
|
if (is<StringObject>(value_object) || is<NumberObject>(value_object)) {
|
|
|
|
item = replacer_value.to_string(global_object);
|
|
|
|
if (vm.exception())
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!item.is_null() && !list.contains_slow(item)) {
|
|
|
|
list.append(item);
|
2020-06-10 11:01:00 -07:00
|
|
|
}
|
|
|
|
}
|
2021-07-01 03:24:04 +03:00
|
|
|
state.property_list = list;
|
2020-06-10 11:01:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (space.is_object()) {
|
2021-07-01 03:24:04 +03:00
|
|
|
auto& space_object = space.as_object();
|
|
|
|
if (is<NumberObject>(space_object)) {
|
|
|
|
space = space.to_number(global_object);
|
|
|
|
if (vm.exception())
|
|
|
|
return {};
|
|
|
|
} else if (is<StringObject>(space_object)) {
|
|
|
|
space = space.to_primitive_string(global_object);
|
|
|
|
if (vm.exception())
|
|
|
|
return {};
|
|
|
|
}
|
2020-06-10 11:01:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (space.is_number()) {
|
2021-07-01 03:24:04 +03:00
|
|
|
auto space_mv = space.to_integer_or_infinity(global_object);
|
|
|
|
space_mv = min(10, space_mv);
|
|
|
|
state.gap = space_mv < 1 ? String::empty() : String::repeated(' ', space_mv);
|
2020-06-10 11:01:00 -07:00
|
|
|
} else if (space.is_string()) {
|
|
|
|
auto string = space.as_string().string();
|
2021-07-01 03:24:04 +03:00
|
|
|
if (string.length() <= 10)
|
2020-06-10 11:01:00 -07:00
|
|
|
state.gap = string;
|
2021-07-01 03:24:04 +03:00
|
|
|
else
|
2020-06-10 11:01:00 -07:00
|
|
|
state.gap = string.substring(0, 10);
|
|
|
|
} else {
|
|
|
|
state.gap = String::empty();
|
|
|
|
}
|
|
|
|
|
2021-06-16 20:52:30 +01:00
|
|
|
auto* wrapper = Object::create(global_object, global_object.object_prototype());
|
LibJS: Rewrite most of Object for spec compliance :^)
This is a huge patch, I know. In hindsight this perhaps could've been
done slightly more incremental, but I started and then fixed everything
until it worked, and here we are. I tried splitting of some completely
unrelated changes into separate commits, however. Anyway.
This is a rewrite of most of Object, and by extension large parts of
Array, Proxy, Reflect, String, TypedArray, and some other things.
What we already had worked fine for about 90% of things, but getting the
last 10% right proved to be increasingly difficult with the current code
that sort of grew organically and is only very loosely based on the
spec - this became especially obvious when we started fixing a large
number of test262 failures.
Key changes include:
- 1:1 matching function names and parameters of all object-related
functions, to avoid ambiguity. Previously we had things like put(),
which the spec doesn't have - as a result it wasn't always clear which
need to be used.
- Better separation between object abstract operations and internal
methods - the former are always the same, the latter can be overridden
(and are therefore virtual). The internal methods (i.e. [[Foo]] in the
spec) are now prefixed with 'internal_' for clarity - again, it was
previously not always clear which AO a certain method represents,
get() could've been both Get and [[Get]] (I don't know which one it
was closer to right now).
Note that some of the old names have been kept until all code relying
on them is updated, but they are now simple wrappers around the
closest matching standard abstract operation.
- Simplifications of the storage layer: functions that write values to
storage are now prefixed with 'storage_' to make their purpose clear,
and as they are not part of the spec they should not contain any steps
specified by it. Much functionality is now covered by the layers above
it and was removed (e.g. handling of accessors, attribute checks).
- PropertyAttributes has been greatly simplified, and is being replaced
by PropertyDescriptor - a concept similar to the current
implementation, but more aligned with the actual spec. See the commit
message of the previous commit where it was introduced for details.
- As a bonus, and since I had to look at the spec a whole lot anyway, I
introduced more inline comments with the exact steps from the spec -
this makes it super easy to verify correctness.
- East-const all the things.
As a result of all of this, things are much more correct but a bit
slower now. Retaining speed wasn't a consideration at all, I have done
no profiling of the new code - there might be low hanging fruits, which
we can then harvest separately.
Special thanks to Idan for helping me with this by tracking down bugs,
updating everything outside of LibJS to work with these changes (LibWeb,
Spreadsheet, HackStudio), as well as providing countless patches to fix
regressions I introduced - there still are very few (we got it down to
5), but we also get many new passing test262 tests in return. :^)
Co-authored-by: Idan Horowitz <idan.horowitz@gmail.com>
2021-07-04 18:14:16 +01:00
|
|
|
wrapper->create_data_property_or_throw(String::empty(), value);
|
2020-09-27 18:52:42 +02:00
|
|
|
auto result = serialize_json_property(global_object, state, String::empty(), wrapper);
|
|
|
|
if (vm.exception())
|
2020-06-10 11:01:00 -07:00
|
|
|
return {};
|
2020-07-04 11:37:50 -07:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
// 25.5.2 JSON.stringify ( value [ , replacer [ , space ] ] ), https://tc39.es/ecma262/#sec-json.stringify
|
2020-07-04 11:37:50 -07:00
|
|
|
JS_DEFINE_NATIVE_FUNCTION(JSONObject::stringify)
|
|
|
|
{
|
2020-09-27 18:36:49 +02:00
|
|
|
if (!vm.argument_count())
|
2020-07-04 11:37:50 -07:00
|
|
|
return js_undefined();
|
|
|
|
|
2020-09-27 18:36:49 +02:00
|
|
|
auto value = vm.argument(0);
|
|
|
|
auto replacer = vm.argument(1);
|
|
|
|
auto space = vm.argument(2);
|
2020-07-04 11:37:50 -07:00
|
|
|
|
2020-09-27 18:52:42 +02:00
|
|
|
auto string = stringify_impl(global_object, value, replacer, space);
|
2020-07-04 11:37:50 -07:00
|
|
|
if (string.is_null())
|
2020-06-10 11:01:00 -07:00
|
|
|
return js_undefined();
|
2020-07-04 11:37:50 -07:00
|
|
|
|
2020-09-27 18:36:49 +02:00
|
|
|
return js_string(vm, string);
|
2020-06-10 11:01:00 -07:00
|
|
|
}
|
|
|
|
|
2021-07-01 03:24:04 +03:00
|
|
|
// 25.5.2.1 SerializeJSONProperty ( state, key, holder ), https://tc39.es/ecma262/#sec-serializejsonproperty
|
2020-09-27 18:52:42 +02:00
|
|
|
String JSONObject::serialize_json_property(GlobalObject& global_object, StringifyState& state, const PropertyName& key, Object* holder)
|
2020-06-10 11:01:00 -07:00
|
|
|
{
|
2020-09-27 18:52:42 +02:00
|
|
|
auto& vm = global_object.vm();
|
2021-07-04 22:55:45 +01:00
|
|
|
auto value = holder->get(key);
|
2020-09-27 18:52:42 +02:00
|
|
|
if (vm.exception())
|
2020-09-19 12:57:31 +01:00
|
|
|
return {};
|
2021-07-01 03:24:04 +03:00
|
|
|
if (value.is_object() || value.is_bigint()) {
|
|
|
|
auto* value_object = value.to_object(global_object);
|
|
|
|
if (vm.exception())
|
|
|
|
return {};
|
|
|
|
auto to_json = value_object->get(vm.names.toJSON);
|
2020-09-27 18:52:42 +02:00
|
|
|
if (vm.exception())
|
2020-06-10 11:01:00 -07:00
|
|
|
return {};
|
2021-09-23 20:56:28 +03:00
|
|
|
if (to_json.is_function())
|
|
|
|
value = TRY_OR_DISCARD(vm.call(to_json.as_function(), value, js_string(vm, key.to_string())));
|
2020-06-10 11:01:00 -07:00
|
|
|
}
|
|
|
|
|
2021-09-23 20:56:28 +03:00
|
|
|
if (state.replacer_function)
|
|
|
|
value = TRY_OR_DISCARD(vm.call(*state.replacer_function, holder, js_string(vm, key.to_string()), value));
|
2020-06-10 11:01:00 -07:00
|
|
|
|
|
|
|
if (value.is_object()) {
|
|
|
|
auto& value_object = value.as_object();
|
2021-07-01 03:24:04 +03:00
|
|
|
if (is<NumberObject>(value_object)) {
|
|
|
|
value = value.to_number(global_object);
|
|
|
|
if (vm.exception())
|
|
|
|
return {};
|
|
|
|
} else if (is<StringObject>(value_object)) {
|
|
|
|
value = value.to_primitive_string(global_object);
|
|
|
|
if (vm.exception())
|
|
|
|
return {};
|
|
|
|
} else if (is<BooleanObject>(value_object)) {
|
|
|
|
value = static_cast<BooleanObject&>(value_object).value_of();
|
|
|
|
} else if (is<BigIntObject>(value_object)) {
|
|
|
|
value = static_cast<BigIntObject&>(value_object).value_of();
|
|
|
|
}
|
2020-06-10 11:01:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (value.is_null())
|
|
|
|
return "null";
|
|
|
|
if (value.is_boolean())
|
|
|
|
return value.as_bool() ? "true" : "false";
|
|
|
|
if (value.is_string())
|
|
|
|
return quote_json_string(value.as_string().string());
|
|
|
|
if (value.is_number()) {
|
|
|
|
if (value.is_finite_number())
|
2020-09-27 18:36:49 +02:00
|
|
|
return value.to_string(global_object);
|
2020-06-10 11:01:00 -07:00
|
|
|
return "null";
|
|
|
|
}
|
2021-07-01 03:24:04 +03:00
|
|
|
if (value.is_bigint()) {
|
|
|
|
vm.throw_exception<TypeError>(global_object, ErrorType::JsonBigInt);
|
|
|
|
return {};
|
|
|
|
}
|
2020-06-10 11:01:00 -07:00
|
|
|
if (value.is_object() && !value.is_function()) {
|
2021-09-23 20:05:34 +03:00
|
|
|
auto is_array = TRY_OR_DISCARD(value.is_array(global_object));
|
2021-07-01 03:24:04 +03:00
|
|
|
if (is_array) {
|
|
|
|
auto result = serialize_json_array(global_object, state, static_cast<Array&>(value.as_object()));
|
|
|
|
if (vm.exception())
|
|
|
|
return {};
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
auto result = serialize_json_object(global_object, state, value.as_object());
|
2021-06-08 21:53:36 +01:00
|
|
|
if (vm.exception())
|
|
|
|
return {};
|
2021-07-01 03:24:04 +03:00
|
|
|
return result;
|
2020-06-10 11:01:00 -07:00
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2021-07-01 03:24:04 +03:00
|
|
|
// 25.5.2.4 SerializeJSONObject ( state, value ), https://tc39.es/ecma262/#sec-serializejsonobject
|
2020-09-27 18:52:42 +02:00
|
|
|
String JSONObject::serialize_json_object(GlobalObject& global_object, StringifyState& state, Object& object)
|
2020-06-10 11:01:00 -07:00
|
|
|
{
|
2020-09-27 18:52:42 +02:00
|
|
|
auto& vm = global_object.vm();
|
2020-06-10 11:01:00 -07:00
|
|
|
if (state.seen_objects.contains(&object)) {
|
2020-09-27 18:52:42 +02:00
|
|
|
vm.throw_exception<TypeError>(global_object, ErrorType::JsonCircular);
|
2020-06-10 11:01:00 -07:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
state.seen_objects.set(&object);
|
|
|
|
String previous_indent = state.indent;
|
2020-10-04 15:18:52 +01:00
|
|
|
state.indent = String::formatted("{}{}", state.indent, state.gap);
|
2020-06-10 11:01:00 -07:00
|
|
|
Vector<String> property_strings;
|
|
|
|
|
|
|
|
auto process_property = [&](const PropertyName& key) {
|
2021-04-16 13:02:51 +03:00
|
|
|
if (key.is_symbol())
|
|
|
|
return;
|
2020-09-27 18:52:42 +02:00
|
|
|
auto serialized_property_string = serialize_json_property(global_object, state, key, &object);
|
|
|
|
if (vm.exception())
|
2020-06-10 11:01:00 -07:00
|
|
|
return;
|
|
|
|
if (!serialized_property_string.is_null()) {
|
2020-10-04 15:18:52 +01:00
|
|
|
property_strings.append(String::formatted(
|
|
|
|
"{}:{}{}",
|
|
|
|
quote_json_string(key.to_string()),
|
2020-06-10 11:01:00 -07:00
|
|
|
state.gap.is_empty() ? "" : " ",
|
2020-10-04 15:18:52 +01:00
|
|
|
serialized_property_string));
|
2020-06-10 11:01:00 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (state.property_list.has_value()) {
|
|
|
|
auto property_list = state.property_list.value();
|
|
|
|
for (auto& property : property_list) {
|
|
|
|
process_property(property);
|
2020-09-27 18:52:42 +02:00
|
|
|
if (vm.exception())
|
2020-06-10 11:01:00 -07:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
} else {
|
LibJS: Rewrite most of Object for spec compliance :^)
This is a huge patch, I know. In hindsight this perhaps could've been
done slightly more incremental, but I started and then fixed everything
until it worked, and here we are. I tried splitting of some completely
unrelated changes into separate commits, however. Anyway.
This is a rewrite of most of Object, and by extension large parts of
Array, Proxy, Reflect, String, TypedArray, and some other things.
What we already had worked fine for about 90% of things, but getting the
last 10% right proved to be increasingly difficult with the current code
that sort of grew organically and is only very loosely based on the
spec - this became especially obvious when we started fixing a large
number of test262 failures.
Key changes include:
- 1:1 matching function names and parameters of all object-related
functions, to avoid ambiguity. Previously we had things like put(),
which the spec doesn't have - as a result it wasn't always clear which
need to be used.
- Better separation between object abstract operations and internal
methods - the former are always the same, the latter can be overridden
(and are therefore virtual). The internal methods (i.e. [[Foo]] in the
spec) are now prefixed with 'internal_' for clarity - again, it was
previously not always clear which AO a certain method represents,
get() could've been both Get and [[Get]] (I don't know which one it
was closer to right now).
Note that some of the old names have been kept until all code relying
on them is updated, but they are now simple wrappers around the
closest matching standard abstract operation.
- Simplifications of the storage layer: functions that write values to
storage are now prefixed with 'storage_' to make their purpose clear,
and as they are not part of the spec they should not contain any steps
specified by it. Much functionality is now covered by the layers above
it and was removed (e.g. handling of accessors, attribute checks).
- PropertyAttributes has been greatly simplified, and is being replaced
by PropertyDescriptor - a concept similar to the current
implementation, but more aligned with the actual spec. See the commit
message of the previous commit where it was introduced for details.
- As a bonus, and since I had to look at the spec a whole lot anyway, I
introduced more inline comments with the exact steps from the spec -
this makes it super easy to verify correctness.
- East-const all the things.
As a result of all of this, things are much more correct but a bit
slower now. Retaining speed wasn't a consideration at all, I have done
no profiling of the new code - there might be low hanging fruits, which
we can then harvest separately.
Special thanks to Idan for helping me with this by tracking down bugs,
updating everything outside of LibJS to work with these changes (LibWeb,
Spreadsheet, HackStudio), as well as providing countless patches to fix
regressions I introduced - there still are very few (we got it down to
5), but we also get many new passing test262 tests in return. :^)
Co-authored-by: Idan Horowitz <idan.horowitz@gmail.com>
2021-07-04 18:14:16 +01:00
|
|
|
auto property_list = object.enumerable_own_property_names(PropertyKind::Key);
|
2021-07-01 03:24:04 +03:00
|
|
|
if (vm.exception())
|
|
|
|
return {};
|
|
|
|
for (auto& property : property_list) {
|
|
|
|
process_property(property.as_string().string());
|
2020-09-27 18:52:42 +02:00
|
|
|
if (vm.exception())
|
2020-06-10 11:01:00 -07:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
StringBuilder builder;
|
|
|
|
if (property_strings.is_empty()) {
|
|
|
|
builder.append("{}");
|
|
|
|
} else {
|
|
|
|
bool first = true;
|
|
|
|
builder.append('{');
|
|
|
|
if (state.gap.is_empty()) {
|
|
|
|
for (auto& property_string : property_strings) {
|
|
|
|
if (!first)
|
|
|
|
builder.append(',');
|
|
|
|
first = false;
|
|
|
|
builder.append(property_string);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
builder.append('\n');
|
|
|
|
builder.append(state.indent);
|
2020-10-04 15:18:52 +01:00
|
|
|
auto separator = String::formatted(",\n{}", state.indent);
|
2020-06-10 11:01:00 -07:00
|
|
|
for (auto& property_string : property_strings) {
|
|
|
|
if (!first)
|
|
|
|
builder.append(separator);
|
|
|
|
first = false;
|
|
|
|
builder.append(property_string);
|
|
|
|
}
|
|
|
|
builder.append('\n');
|
|
|
|
builder.append(previous_indent);
|
|
|
|
}
|
|
|
|
builder.append('}');
|
|
|
|
}
|
|
|
|
|
|
|
|
state.seen_objects.remove(&object);
|
|
|
|
state.indent = previous_indent;
|
|
|
|
return builder.to_string();
|
|
|
|
}
|
|
|
|
|
2021-07-01 03:24:04 +03:00
|
|
|
// 25.5.2.5 SerializeJSONArray ( state, value ), https://tc39.es/ecma262/#sec-serializejsonarray
|
2020-09-27 18:52:42 +02:00
|
|
|
String JSONObject::serialize_json_array(GlobalObject& global_object, StringifyState& state, Object& object)
|
2020-06-10 11:01:00 -07:00
|
|
|
{
|
2020-09-27 18:52:42 +02:00
|
|
|
auto& vm = global_object.vm();
|
2020-06-10 11:01:00 -07:00
|
|
|
if (state.seen_objects.contains(&object)) {
|
2020-09-27 18:52:42 +02:00
|
|
|
vm.throw_exception<TypeError>(global_object, ErrorType::JsonCircular);
|
2020-06-10 11:01:00 -07:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
state.seen_objects.set(&object);
|
|
|
|
String previous_indent = state.indent;
|
2020-10-04 15:18:52 +01:00
|
|
|
state.indent = String::formatted("{}{}", state.indent, state.gap);
|
2020-06-10 11:01:00 -07:00
|
|
|
Vector<String> property_strings;
|
|
|
|
|
2021-09-21 21:59:49 +03:00
|
|
|
auto length = TRY_OR_DISCARD(length_of_array_like(global_object, object));
|
2021-07-01 03:24:04 +03:00
|
|
|
|
|
|
|
// Optimization
|
|
|
|
property_strings.ensure_capacity(length);
|
|
|
|
|
2020-06-10 11:01:00 -07:00
|
|
|
for (size_t i = 0; i < length; ++i) {
|
2020-09-27 18:52:42 +02:00
|
|
|
if (vm.exception())
|
2020-06-10 11:01:00 -07:00
|
|
|
return {};
|
2020-09-27 18:52:42 +02:00
|
|
|
auto serialized_property_string = serialize_json_property(global_object, state, i, &object);
|
|
|
|
if (vm.exception())
|
2020-06-10 11:01:00 -07:00
|
|
|
return {};
|
|
|
|
if (serialized_property_string.is_null()) {
|
|
|
|
property_strings.append("null");
|
|
|
|
} else {
|
|
|
|
property_strings.append(serialized_property_string);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
StringBuilder builder;
|
|
|
|
if (property_strings.is_empty()) {
|
|
|
|
builder.append("[]");
|
|
|
|
} else {
|
|
|
|
if (state.gap.is_empty()) {
|
|
|
|
builder.append('[');
|
|
|
|
bool first = true;
|
|
|
|
for (auto& property_string : property_strings) {
|
|
|
|
if (!first)
|
|
|
|
builder.append(',');
|
|
|
|
first = false;
|
|
|
|
builder.append(property_string);
|
|
|
|
}
|
|
|
|
builder.append(']');
|
|
|
|
} else {
|
|
|
|
builder.append("[\n");
|
|
|
|
builder.append(state.indent);
|
2020-10-04 15:18:52 +01:00
|
|
|
auto separator = String::formatted(",\n{}", state.indent);
|
2020-06-10 11:01:00 -07:00
|
|
|
bool first = true;
|
|
|
|
for (auto& property_string : property_strings) {
|
|
|
|
if (!first)
|
|
|
|
builder.append(separator);
|
|
|
|
first = false;
|
|
|
|
builder.append(property_string);
|
|
|
|
}
|
|
|
|
builder.append('\n');
|
|
|
|
builder.append(previous_indent);
|
|
|
|
builder.append(']');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
state.seen_objects.remove(&object);
|
|
|
|
state.indent = previous_indent;
|
|
|
|
return builder.to_string();
|
|
|
|
}
|
|
|
|
|
2021-07-01 03:24:04 +03:00
|
|
|
// 25.5.2.2 QuoteJSONString ( value ), https://tc39.es/ecma262/#sec-quotejsonstring
|
2020-06-10 11:01:00 -07:00
|
|
|
String JSONObject::quote_json_string(String string)
|
|
|
|
{
|
|
|
|
// FIXME: Handle UTF16
|
|
|
|
StringBuilder builder;
|
|
|
|
builder.append('"');
|
2021-07-01 03:24:04 +03:00
|
|
|
auto utf_view = Utf8View(string);
|
|
|
|
for (auto code_point : utf_view) {
|
|
|
|
switch (code_point) {
|
2020-06-10 11:01:00 -07:00
|
|
|
case '\b':
|
|
|
|
builder.append("\\b");
|
|
|
|
break;
|
|
|
|
case '\t':
|
|
|
|
builder.append("\\t");
|
|
|
|
break;
|
|
|
|
case '\n':
|
|
|
|
builder.append("\\n");
|
|
|
|
break;
|
|
|
|
case '\f':
|
|
|
|
builder.append("\\f");
|
|
|
|
break;
|
|
|
|
case '\r':
|
|
|
|
builder.append("\\r");
|
|
|
|
break;
|
|
|
|
case '"':
|
|
|
|
builder.append("\\\"");
|
|
|
|
break;
|
|
|
|
case '\\':
|
|
|
|
builder.append("\\\\");
|
|
|
|
break;
|
|
|
|
default:
|
2021-07-01 03:24:04 +03:00
|
|
|
if (code_point < 0x20) {
|
|
|
|
builder.appendff("\\u{:04x}", code_point);
|
2020-06-10 11:01:00 -07:00
|
|
|
} else {
|
2021-07-01 03:24:04 +03:00
|
|
|
builder.append_code_point(code_point);
|
2020-06-10 11:01:00 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
builder.append('"');
|
|
|
|
return builder.to_string();
|
|
|
|
}
|
|
|
|
|
2021-06-13 00:22:35 +01:00
|
|
|
// 25.5.1 JSON.parse ( text [ , reviver ] ), https://tc39.es/ecma262/#sec-json.parse
|
2020-06-20 13:55:34 +02:00
|
|
|
JS_DEFINE_NATIVE_FUNCTION(JSONObject::parse)
|
2020-06-10 11:01:00 -07:00
|
|
|
{
|
2020-09-27 18:36:49 +02:00
|
|
|
auto string = vm.argument(0).to_string(global_object);
|
|
|
|
if (vm.exception())
|
2020-06-10 23:30:36 -07:00
|
|
|
return {};
|
2020-09-27 18:36:49 +02:00
|
|
|
auto reviver = vm.argument(1);
|
2020-06-10 23:30:36 -07:00
|
|
|
|
|
|
|
auto json = JsonValue::from_string(string);
|
|
|
|
if (!json.has_value()) {
|
2020-09-27 18:36:49 +02:00
|
|
|
vm.throw_exception<SyntaxError>(global_object, ErrorType::JsonMalformed);
|
2020-06-10 23:30:36 -07:00
|
|
|
return {};
|
|
|
|
}
|
2021-07-01 03:54:24 +03:00
|
|
|
Value unfiltered = parse_json_value(global_object, json.value());
|
2020-06-10 23:30:36 -07:00
|
|
|
if (reviver.is_function()) {
|
2021-06-16 20:52:30 +01:00
|
|
|
auto* root = Object::create(global_object, global_object.object_prototype());
|
|
|
|
auto root_name = String::empty();
|
LibJS: Rewrite most of Object for spec compliance :^)
This is a huge patch, I know. In hindsight this perhaps could've been
done slightly more incremental, but I started and then fixed everything
until it worked, and here we are. I tried splitting of some completely
unrelated changes into separate commits, however. Anyway.
This is a rewrite of most of Object, and by extension large parts of
Array, Proxy, Reflect, String, TypedArray, and some other things.
What we already had worked fine for about 90% of things, but getting the
last 10% right proved to be increasingly difficult with the current code
that sort of grew organically and is only very loosely based on the
spec - this became especially obvious when we started fixing a large
number of test262 failures.
Key changes include:
- 1:1 matching function names and parameters of all object-related
functions, to avoid ambiguity. Previously we had things like put(),
which the spec doesn't have - as a result it wasn't always clear which
need to be used.
- Better separation between object abstract operations and internal
methods - the former are always the same, the latter can be overridden
(and are therefore virtual). The internal methods (i.e. [[Foo]] in the
spec) are now prefixed with 'internal_' for clarity - again, it was
previously not always clear which AO a certain method represents,
get() could've been both Get and [[Get]] (I don't know which one it
was closer to right now).
Note that some of the old names have been kept until all code relying
on them is updated, but they are now simple wrappers around the
closest matching standard abstract operation.
- Simplifications of the storage layer: functions that write values to
storage are now prefixed with 'storage_' to make their purpose clear,
and as they are not part of the spec they should not contain any steps
specified by it. Much functionality is now covered by the layers above
it and was removed (e.g. handling of accessors, attribute checks).
- PropertyAttributes has been greatly simplified, and is being replaced
by PropertyDescriptor - a concept similar to the current
implementation, but more aligned with the actual spec. See the commit
message of the previous commit where it was introduced for details.
- As a bonus, and since I had to look at the spec a whole lot anyway, I
introduced more inline comments with the exact steps from the spec -
this makes it super easy to verify correctness.
- East-const all the things.
As a result of all of this, things are much more correct but a bit
slower now. Retaining speed wasn't a consideration at all, I have done
no profiling of the new code - there might be low hanging fruits, which
we can then harvest separately.
Special thanks to Idan for helping me with this by tracking down bugs,
updating everything outside of LibJS to work with these changes (LibWeb,
Spreadsheet, HackStudio), as well as providing countless patches to fix
regressions I introduced - there still are very few (we got it down to
5), but we also get many new passing test262 tests in return. :^)
Co-authored-by: Idan Horowitz <idan.horowitz@gmail.com>
2021-07-04 18:14:16 +01:00
|
|
|
root->create_data_property_or_throw(root_name, unfiltered);
|
2021-07-01 03:54:24 +03:00
|
|
|
auto result = internalize_json_property(global_object, root, root_name, reviver.as_function());
|
2020-09-27 18:36:49 +02:00
|
|
|
if (vm.exception())
|
2020-06-10 23:30:36 -07:00
|
|
|
return {};
|
2021-07-01 03:54:24 +03:00
|
|
|
return result;
|
2020-06-10 23:30:36 -07:00
|
|
|
}
|
2021-07-01 03:54:24 +03:00
|
|
|
return unfiltered;
|
2020-06-10 23:30:36 -07:00
|
|
|
}
|
|
|
|
|
2020-09-27 18:52:42 +02:00
|
|
|
Value JSONObject::parse_json_value(GlobalObject& global_object, const JsonValue& value)
|
2020-06-10 23:30:36 -07:00
|
|
|
{
|
|
|
|
if (value.is_object())
|
2020-09-27 18:52:42 +02:00
|
|
|
return Value(parse_json_object(global_object, value.as_object()));
|
2020-06-10 23:30:36 -07:00
|
|
|
if (value.is_array())
|
2020-09-27 18:52:42 +02:00
|
|
|
return Value(parse_json_array(global_object, value.as_array()));
|
2020-06-10 23:30:36 -07:00
|
|
|
if (value.is_null())
|
|
|
|
return js_null();
|
|
|
|
if (value.is_double())
|
|
|
|
return Value(value.as_double());
|
|
|
|
if (value.is_number())
|
|
|
|
return Value(value.to_i32(0));
|
|
|
|
if (value.is_string())
|
2020-09-27 18:52:42 +02:00
|
|
|
return js_string(global_object.heap(), value.to_string());
|
2020-06-10 23:30:36 -07:00
|
|
|
if (value.is_bool())
|
|
|
|
return Value(static_cast<bool>(value.as_bool()));
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-06-10 23:30:36 -07:00
|
|
|
}
|
|
|
|
|
2020-09-27 18:52:42 +02:00
|
|
|
Object* JSONObject::parse_json_object(GlobalObject& global_object, const JsonObject& json_object)
|
2020-06-10 23:30:36 -07:00
|
|
|
{
|
2021-06-16 20:52:30 +01:00
|
|
|
auto* object = Object::create(global_object, global_object.object_prototype());
|
2020-06-20 16:56:01 +02:00
|
|
|
json_object.for_each_member([&](auto& key, auto& value) {
|
2021-07-06 02:15:08 +03:00
|
|
|
object->define_direct_property(key, parse_json_value(global_object, value), JS::default_attributes);
|
2020-06-10 23:30:36 -07:00
|
|
|
});
|
|
|
|
return object;
|
|
|
|
}
|
|
|
|
|
2020-09-27 18:52:42 +02:00
|
|
|
Array* JSONObject::parse_json_array(GlobalObject& global_object, const JsonArray& json_array)
|
2020-06-10 23:30:36 -07:00
|
|
|
{
|
2021-07-04 01:36:44 +03:00
|
|
|
auto* array = Array::create(global_object, 0);
|
2020-06-10 23:30:36 -07:00
|
|
|
size_t index = 0;
|
2020-06-20 16:56:01 +02:00
|
|
|
json_array.for_each([&](auto& value) {
|
2021-07-06 02:15:08 +03:00
|
|
|
array->define_direct_property(index++, parse_json_value(global_object, value), JS::default_attributes);
|
2020-06-10 23:30:36 -07:00
|
|
|
});
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
2021-06-16 20:52:30 +01:00
|
|
|
// 25.5.1.1 InternalizeJSONProperty ( holder, name, reviver ), https://tc39.es/ecma262/#sec-internalizejsonproperty
|
2021-06-27 21:48:34 +02:00
|
|
|
Value JSONObject::internalize_json_property(GlobalObject& global_object, Object* holder, PropertyName const& name, FunctionObject& reviver)
|
2020-06-10 23:30:36 -07:00
|
|
|
{
|
2020-09-27 18:52:42 +02:00
|
|
|
auto& vm = global_object.vm();
|
2021-07-04 22:55:45 +01:00
|
|
|
auto value = holder->get(name);
|
2020-09-27 18:52:42 +02:00
|
|
|
if (vm.exception())
|
2020-06-10 23:30:36 -07:00
|
|
|
return {};
|
|
|
|
if (value.is_object()) {
|
2021-09-23 20:05:34 +03:00
|
|
|
auto is_array = TRY_OR_DISCARD(value.is_array(global_object));
|
2020-06-10 23:30:36 -07:00
|
|
|
|
2021-07-01 03:54:24 +03:00
|
|
|
auto& value_object = value.as_object();
|
2020-06-10 23:30:36 -07:00
|
|
|
auto process_property = [&](const PropertyName& key) {
|
2020-09-27 18:52:42 +02:00
|
|
|
auto element = internalize_json_property(global_object, &value_object, key, reviver);
|
|
|
|
if (vm.exception())
|
2020-06-10 23:30:36 -07:00
|
|
|
return;
|
2021-07-01 03:54:24 +03:00
|
|
|
if (element.is_undefined())
|
LibJS: Rewrite most of Object for spec compliance :^)
This is a huge patch, I know. In hindsight this perhaps could've been
done slightly more incremental, but I started and then fixed everything
until it worked, and here we are. I tried splitting of some completely
unrelated changes into separate commits, however. Anyway.
This is a rewrite of most of Object, and by extension large parts of
Array, Proxy, Reflect, String, TypedArray, and some other things.
What we already had worked fine for about 90% of things, but getting the
last 10% right proved to be increasingly difficult with the current code
that sort of grew organically and is only very loosely based on the
spec - this became especially obvious when we started fixing a large
number of test262 failures.
Key changes include:
- 1:1 matching function names and parameters of all object-related
functions, to avoid ambiguity. Previously we had things like put(),
which the spec doesn't have - as a result it wasn't always clear which
need to be used.
- Better separation between object abstract operations and internal
methods - the former are always the same, the latter can be overridden
(and are therefore virtual). The internal methods (i.e. [[Foo]] in the
spec) are now prefixed with 'internal_' for clarity - again, it was
previously not always clear which AO a certain method represents,
get() could've been both Get and [[Get]] (I don't know which one it
was closer to right now).
Note that some of the old names have been kept until all code relying
on them is updated, but they are now simple wrappers around the
closest matching standard abstract operation.
- Simplifications of the storage layer: functions that write values to
storage are now prefixed with 'storage_' to make their purpose clear,
and as they are not part of the spec they should not contain any steps
specified by it. Much functionality is now covered by the layers above
it and was removed (e.g. handling of accessors, attribute checks).
- PropertyAttributes has been greatly simplified, and is being replaced
by PropertyDescriptor - a concept similar to the current
implementation, but more aligned with the actual spec. See the commit
message of the previous commit where it was introduced for details.
- As a bonus, and since I had to look at the spec a whole lot anyway, I
introduced more inline comments with the exact steps from the spec -
this makes it super easy to verify correctness.
- East-const all the things.
As a result of all of this, things are much more correct but a bit
slower now. Retaining speed wasn't a consideration at all, I have done
no profiling of the new code - there might be low hanging fruits, which
we can then harvest separately.
Special thanks to Idan for helping me with this by tracking down bugs,
updating everything outside of LibJS to work with these changes (LibWeb,
Spreadsheet, HackStudio), as well as providing countless patches to fix
regressions I introduced - there still are very few (we got it down to
5), but we also get many new passing test262 tests in return. :^)
Co-authored-by: Idan Horowitz <idan.horowitz@gmail.com>
2021-07-04 18:14:16 +01:00
|
|
|
value_object.internal_delete(key);
|
2021-07-01 03:54:24 +03:00
|
|
|
else
|
LibJS: Rewrite most of Object for spec compliance :^)
This is a huge patch, I know. In hindsight this perhaps could've been
done slightly more incremental, but I started and then fixed everything
until it worked, and here we are. I tried splitting of some completely
unrelated changes into separate commits, however. Anyway.
This is a rewrite of most of Object, and by extension large parts of
Array, Proxy, Reflect, String, TypedArray, and some other things.
What we already had worked fine for about 90% of things, but getting the
last 10% right proved to be increasingly difficult with the current code
that sort of grew organically and is only very loosely based on the
spec - this became especially obvious when we started fixing a large
number of test262 failures.
Key changes include:
- 1:1 matching function names and parameters of all object-related
functions, to avoid ambiguity. Previously we had things like put(),
which the spec doesn't have - as a result it wasn't always clear which
need to be used.
- Better separation between object abstract operations and internal
methods - the former are always the same, the latter can be overridden
(and are therefore virtual). The internal methods (i.e. [[Foo]] in the
spec) are now prefixed with 'internal_' for clarity - again, it was
previously not always clear which AO a certain method represents,
get() could've been both Get and [[Get]] (I don't know which one it
was closer to right now).
Note that some of the old names have been kept until all code relying
on them is updated, but they are now simple wrappers around the
closest matching standard abstract operation.
- Simplifications of the storage layer: functions that write values to
storage are now prefixed with 'storage_' to make their purpose clear,
and as they are not part of the spec they should not contain any steps
specified by it. Much functionality is now covered by the layers above
it and was removed (e.g. handling of accessors, attribute checks).
- PropertyAttributes has been greatly simplified, and is being replaced
by PropertyDescriptor - a concept similar to the current
implementation, but more aligned with the actual spec. See the commit
message of the previous commit where it was introduced for details.
- As a bonus, and since I had to look at the spec a whole lot anyway, I
introduced more inline comments with the exact steps from the spec -
this makes it super easy to verify correctness.
- East-const all the things.
As a result of all of this, things are much more correct but a bit
slower now. Retaining speed wasn't a consideration at all, I have done
no profiling of the new code - there might be low hanging fruits, which
we can then harvest separately.
Special thanks to Idan for helping me with this by tracking down bugs,
updating everything outside of LibJS to work with these changes (LibWeb,
Spreadsheet, HackStudio), as well as providing countless patches to fix
regressions I introduced - there still are very few (we got it down to
5), but we also get many new passing test262 tests in return. :^)
Co-authored-by: Idan Horowitz <idan.horowitz@gmail.com>
2021-07-04 18:14:16 +01:00
|
|
|
value_object.create_data_property(key, element);
|
2020-06-10 23:30:36 -07:00
|
|
|
};
|
|
|
|
|
2021-07-01 03:54:24 +03:00
|
|
|
if (is_array) {
|
2021-09-21 21:59:49 +03:00
|
|
|
auto length = TRY_OR_DISCARD(length_of_array_like(global_object, value_object));
|
2020-06-10 23:30:36 -07:00
|
|
|
for (size_t i = 0; i < length; ++i) {
|
|
|
|
process_property(i);
|
2020-09-27 18:52:42 +02:00
|
|
|
if (vm.exception())
|
2020-06-10 23:30:36 -07:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
} else {
|
LibJS: Rewrite most of Object for spec compliance :^)
This is a huge patch, I know. In hindsight this perhaps could've been
done slightly more incremental, but I started and then fixed everything
until it worked, and here we are. I tried splitting of some completely
unrelated changes into separate commits, however. Anyway.
This is a rewrite of most of Object, and by extension large parts of
Array, Proxy, Reflect, String, TypedArray, and some other things.
What we already had worked fine for about 90% of things, but getting the
last 10% right proved to be increasingly difficult with the current code
that sort of grew organically and is only very loosely based on the
spec - this became especially obvious when we started fixing a large
number of test262 failures.
Key changes include:
- 1:1 matching function names and parameters of all object-related
functions, to avoid ambiguity. Previously we had things like put(),
which the spec doesn't have - as a result it wasn't always clear which
need to be used.
- Better separation between object abstract operations and internal
methods - the former are always the same, the latter can be overridden
(and are therefore virtual). The internal methods (i.e. [[Foo]] in the
spec) are now prefixed with 'internal_' for clarity - again, it was
previously not always clear which AO a certain method represents,
get() could've been both Get and [[Get]] (I don't know which one it
was closer to right now).
Note that some of the old names have been kept until all code relying
on them is updated, but they are now simple wrappers around the
closest matching standard abstract operation.
- Simplifications of the storage layer: functions that write values to
storage are now prefixed with 'storage_' to make their purpose clear,
and as they are not part of the spec they should not contain any steps
specified by it. Much functionality is now covered by the layers above
it and was removed (e.g. handling of accessors, attribute checks).
- PropertyAttributes has been greatly simplified, and is being replaced
by PropertyDescriptor - a concept similar to the current
implementation, but more aligned with the actual spec. See the commit
message of the previous commit where it was introduced for details.
- As a bonus, and since I had to look at the spec a whole lot anyway, I
introduced more inline comments with the exact steps from the spec -
this makes it super easy to verify correctness.
- East-const all the things.
As a result of all of this, things are much more correct but a bit
slower now. Retaining speed wasn't a consideration at all, I have done
no profiling of the new code - there might be low hanging fruits, which
we can then harvest separately.
Special thanks to Idan for helping me with this by tracking down bugs,
updating everything outside of LibJS to work with these changes (LibWeb,
Spreadsheet, HackStudio), as well as providing countless patches to fix
regressions I introduced - there still are very few (we got it down to
5), but we also get many new passing test262 tests in return. :^)
Co-authored-by: Idan Horowitz <idan.horowitz@gmail.com>
2021-07-04 18:14:16 +01:00
|
|
|
auto property_list = value_object.enumerable_own_property_names(Object::PropertyKind::Key);
|
2021-07-06 01:30:02 +01:00
|
|
|
if (vm.exception())
|
|
|
|
return {};
|
2021-07-01 03:54:24 +03:00
|
|
|
for (auto& property_name : property_list) {
|
|
|
|
process_property(property_name.as_string().string());
|
2020-09-27 18:52:42 +02:00
|
|
|
if (vm.exception())
|
2020-06-10 23:30:36 -07:00
|
|
|
return {};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-25 22:18:32 +04:30
|
|
|
|
2021-09-23 20:56:28 +03:00
|
|
|
return TRY_OR_DISCARD(vm.call(reviver, Value(holder), js_string(vm, name.to_string()), value));
|
2020-06-10 11:01:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|