ladybird/Libraries/LibJS/Runtime/JSONObject.h

74 lines
2.8 KiB
C
Raw Normal View History

2020-06-10 15:01:00 -03:00
/*
* Copyright (c) 2020, Matthew Olsson <mattco@serenityos.org>
2020-06-10 15:01:00 -03:00
*
* SPDX-License-Identifier: BSD-2-Clause
2020-06-10 15:01:00 -03:00
*/
#pragma once
#include <AK/Utf16StringBuilder.h>
#include <LibJS/Export.h>
#include <LibJS/Runtime/Object.h>
2020-06-10 15:01:00 -03:00
namespace JS {
// A JSON Parse Record is a Record value used to describe the initial state of a
// value parsed from JSON text. https://tc39.es/ecma262/#sec-json-parse-record
struct JSONParseRecord {
// [[Value]]: the value produced by evaluation of [[ParseNode]].
Value value;
// [[Key]]: the property name with which [[Value]] is associated.
Utf16String key;
// NB: In place of the spec's [[ParseNode]] field, we capture the source text
// matched by the parse node up front. It is present only when [[Value]] is
// a primitive, since the reviver context only exposes "source" for those.
Optional<String> source;
// [[Elements]]: if [[Value]] is an Array, the records for its elements; else empty.
Vector<JSONParseRecord> elements;
// [[Entries]]: if [[Value]] is a non-Array Object, the records for its entries; else empty.
Vector<JSONParseRecord> entries;
};
class JS_API JSONObject final : public Object {
JS_OBJECT(JSONObject, Object);
GC_DECLARE_ALLOCATOR(JSONObject);
2020-06-10 15:01:00 -03:00
public:
virtual void initialize(Realm&) override;
virtual ~JSONObject() override = default;
2020-06-10 15:01:00 -03:00
// 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.
static ThrowCompletionOr<Optional<Utf16String>> stringify_impl(VM&, Value value, Value replacer, Value space);
static ThrowCompletionOr<Value> parse_json(VM&, StringView text, JSONParseRecord* root_record = nullptr);
2020-06-10 15:01:00 -03:00
private:
explicit JSONObject(Realm&);
2020-06-10 15:01:00 -03:00
struct StringifyState {
GC::Ptr<FunctionObject> replacer_function;
HashTable<GC::Ptr<Object>> seen_objects;
size_t indent_depth { 0 };
Utf16String gap;
Optional<Vector<Utf16String>> property_list;
Utf16StringBuilder builder;
2020-06-10 15:01:00 -03:00
};
// Stringify helpers
static ThrowCompletionOr<bool> serialize_json_property(VM&, StringifyState&, PropertyKey const& key, Object* holder);
static ThrowCompletionOr<void> serialize_json_object(VM&, StringifyState&, Object&);
static ThrowCompletionOr<void> serialize_json_array(VM&, StringifyState&, Object&);
static void quote_json_string(Utf16StringBuilder&, Utf16View const&);
2020-06-10 15:01:00 -03:00
2020-06-11 03:30:36 -03:00
// Parse helpers
static ThrowCompletionOr<Value> internalize_json_property(VM&, Object* holder, PropertyKey const& name, FunctionObject& reviver, JSONParseRecord const* parse_record);
2020-06-11 03:30:36 -03:00
JS_DECLARE_NATIVE_FUNCTION(stringify);
JS_DECLARE_NATIVE_FUNCTION(parse);
JS_DECLARE_NATIVE_FUNCTION(raw_json);
JS_DECLARE_NATIVE_FUNCTION(is_raw_json);
2020-06-10 15:01:00 -03:00
};
}