
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>
199 lines
10 KiB
C++
199 lines
10 KiB
C++
/*
|
|
* Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#define JS_DECLARE_NATIVE_FUNCTION(name) \
|
|
static JS::Value name(JS::VM&, JS::GlobalObject&)
|
|
|
|
#define JS_DECLARE_NATIVE_GETTER(name) \
|
|
static JS::Value name(JS::VM&, JS::GlobalObject&)
|
|
|
|
#define JS_DECLARE_NATIVE_SETTER(name) \
|
|
static void name(JS::VM&, JS::GlobalObject&, JS::Value)
|
|
|
|
#define JS_DEFINE_NATIVE_FUNCTION(name) \
|
|
JS::Value name([[maybe_unused]] JS::VM& vm, [[maybe_unused]] JS::GlobalObject& global_object)
|
|
|
|
#define JS_DEFINE_NATIVE_GETTER(name) \
|
|
JS::Value name([[maybe_unused]] JS::VM& vm, [[maybe_unused]] JS::GlobalObject& global_object)
|
|
|
|
#define JS_DEFINE_NATIVE_SETTER(name) \
|
|
void name([[maybe_unused]] JS::VM& vm, [[maybe_unused]] JS::GlobalObject& global_object, [[maybe_unused]] JS::Value value)
|
|
|
|
// NOTE: Proxy is not included here as it doesn't have a prototype - m_proxy_constructor is initialized separately.
|
|
#define JS_ENUMERATE_NATIVE_OBJECTS_EXCLUDING_TEMPLATES \
|
|
__JS_ENUMERATE(AggregateError, aggregate_error, AggregateErrorPrototype, AggregateErrorConstructor, void) \
|
|
__JS_ENUMERATE(Array, array, ArrayPrototype, ArrayConstructor, void) \
|
|
__JS_ENUMERATE(ArrayBuffer, array_buffer, ArrayBufferPrototype, ArrayBufferConstructor, void) \
|
|
__JS_ENUMERATE(BigIntObject, bigint, BigIntPrototype, BigIntConstructor, void) \
|
|
__JS_ENUMERATE(BooleanObject, boolean, BooleanPrototype, BooleanConstructor, void) \
|
|
__JS_ENUMERATE(DataView, data_view, DataViewPrototype, DataViewConstructor, void) \
|
|
__JS_ENUMERATE(Date, date, DatePrototype, DateConstructor, void) \
|
|
__JS_ENUMERATE(Error, error, ErrorPrototype, ErrorConstructor, void) \
|
|
__JS_ENUMERATE(FinalizationRegistry, finalization_registry, FinalizationRegistryPrototype, FinalizationRegistryConstructor, void) \
|
|
__JS_ENUMERATE(FunctionObject, function, FunctionPrototype, FunctionConstructor, void) \
|
|
__JS_ENUMERATE(GeneratorFunction, generator_function, GeneratorFunctionPrototype, GeneratorFunctionConstructor, void) \
|
|
__JS_ENUMERATE(Map, map, MapPrototype, MapConstructor, void) \
|
|
__JS_ENUMERATE(NumberObject, number, NumberPrototype, NumberConstructor, void) \
|
|
__JS_ENUMERATE(Object, object, ObjectPrototype, ObjectConstructor, void) \
|
|
__JS_ENUMERATE(Promise, promise, PromisePrototype, PromiseConstructor, void) \
|
|
__JS_ENUMERATE(RegExpObject, regexp, RegExpPrototype, RegExpConstructor, void) \
|
|
__JS_ENUMERATE(Set, set, SetPrototype, SetConstructor, void) \
|
|
__JS_ENUMERATE(StringObject, string, StringPrototype, StringConstructor, void) \
|
|
__JS_ENUMERATE(SymbolObject, symbol, SymbolPrototype, SymbolConstructor, void) \
|
|
__JS_ENUMERATE(WeakMap, weak_map, WeakMapPrototype, WeakMapConstructor, void) \
|
|
__JS_ENUMERATE(WeakRef, weak_ref, WeakRefPrototype, WeakRefConstructor, void) \
|
|
__JS_ENUMERATE(WeakSet, weak_set, WeakSetPrototype, WeakSetConstructor, void)
|
|
|
|
#define JS_ENUMERATE_NATIVE_OBJECTS \
|
|
JS_ENUMERATE_NATIVE_OBJECTS_EXCLUDING_TEMPLATES \
|
|
__JS_ENUMERATE(TypedArray, typed_array, TypedArrayPrototype, TypedArrayConstructor, void)
|
|
|
|
#define JS_ENUMERATE_NATIVE_ERRORS \
|
|
__JS_ENUMERATE(EvalError, eval_error, EvalErrorPrototype, EvalErrorConstructor, void) \
|
|
__JS_ENUMERATE(InternalError, internal_error, InternalErrorPrototype, InternalErrorConstructor, void) \
|
|
__JS_ENUMERATE(InvalidCharacterError, invalid_character_error, InvalidCharacterErrorPrototype, InvalidCharacterErrorConstructor, void) \
|
|
__JS_ENUMERATE(RangeError, range_error, RangeErrorPrototype, RangeErrorConstructor, void) \
|
|
__JS_ENUMERATE(ReferenceError, reference_error, ReferenceErrorPrototype, ReferenceErrorConstructor, void) \
|
|
__JS_ENUMERATE(SyntaxError, syntax_error, SyntaxErrorPrototype, SyntaxErrorConstructor, void) \
|
|
__JS_ENUMERATE(TypeError, type_error, TypeErrorPrototype, TypeErrorConstructor, void) \
|
|
__JS_ENUMERATE(URIError, uri_error, URIErrorPrototype, URIErrorConstructor, void)
|
|
|
|
#define JS_ENUMERATE_TYPED_ARRAYS \
|
|
__JS_ENUMERATE(Uint8Array, uint8_array, Uint8ArrayPrototype, Uint8ArrayConstructor, u8) \
|
|
__JS_ENUMERATE(Uint8ClampedArray, uint8_clamped_array, Uint8ClampedArrayPrototype, Uint8ClampedArrayConstructor, ClampedU8) \
|
|
__JS_ENUMERATE(Uint16Array, uint16_array, Uint16ArrayPrototype, Uint16ArrayConstructor, u16) \
|
|
__JS_ENUMERATE(Uint32Array, uint32_array, Uint32ArrayPrototype, Uint32ArrayConstructor, u32) \
|
|
__JS_ENUMERATE(BigUint64Array, big_uint64_array, BigUint64ArrayPrototype, BigUint64ArrayConstructor, u64) \
|
|
__JS_ENUMERATE(Int8Array, int8_array, Int8ArrayPrototype, Int8ArrayConstructor, i8) \
|
|
__JS_ENUMERATE(Int16Array, int16_array, Int16ArrayPrototype, Int16ArrayConstructor, i16) \
|
|
__JS_ENUMERATE(Int32Array, int32_array, Int32ArrayPrototype, Int32ArrayConstructor, i32) \
|
|
__JS_ENUMERATE(BigInt64Array, big_int64_array, BigInt64ArrayPrototype, BigInt64ArrayConstructor, i64) \
|
|
__JS_ENUMERATE(Float32Array, float32_array, Float32ArrayPrototype, Float32ArrayConstructor, float) \
|
|
__JS_ENUMERATE(Float64Array, float64_array, Float64ArrayPrototype, Float64ArrayConstructor, double)
|
|
|
|
#define JS_ENUMERATE_ITERATOR_PROTOTYPES \
|
|
__JS_ENUMERATE(Iterator, iterator) \
|
|
__JS_ENUMERATE(ArrayIterator, array_iterator) \
|
|
__JS_ENUMERATE(MapIterator, map_iterator) \
|
|
__JS_ENUMERATE(SetIterator, set_iterator) \
|
|
__JS_ENUMERATE(StringIterator, string_iterator)
|
|
|
|
#define JS_ENUMERATE_BUILTIN_TYPES \
|
|
JS_ENUMERATE_NATIVE_OBJECTS \
|
|
JS_ENUMERATE_NATIVE_ERRORS \
|
|
JS_ENUMERATE_TYPED_ARRAYS
|
|
|
|
#define JS_ENUMERATE_WELL_KNOWN_SYMBOLS \
|
|
__JS_ENUMERATE(iterator, iterator) \
|
|
__JS_ENUMERATE(asyncIterator, async_iterator) \
|
|
__JS_ENUMERATE(match, match) \
|
|
__JS_ENUMERATE(matchAll, match_all) \
|
|
__JS_ENUMERATE(replace, replace) \
|
|
__JS_ENUMERATE(search, search) \
|
|
__JS_ENUMERATE(split, split) \
|
|
__JS_ENUMERATE(hasInstance, has_instance) \
|
|
__JS_ENUMERATE(isConcatSpreadable, is_concat_spreadable) \
|
|
__JS_ENUMERATE(unscopables, unscopables) \
|
|
__JS_ENUMERATE(species, species) \
|
|
__JS_ENUMERATE(toPrimitive, to_primitive) \
|
|
__JS_ENUMERATE(toStringTag, to_string_tag)
|
|
|
|
#define JS_ENUMERATE_REGEXP_FLAGS \
|
|
__JS_ENUMERATE(global, global, g, Global) \
|
|
__JS_ENUMERATE(ignoreCase, ignore_case, i, Insensitive) \
|
|
__JS_ENUMERATE(multiline, multiline, m, Multiline) \
|
|
__JS_ENUMERATE(dotAll, dot_all, s, SingleLine) \
|
|
__JS_ENUMERATE(unicode, unicode, u, Unicode) \
|
|
__JS_ENUMERATE(sticky, sticky, y, Sticky)
|
|
|
|
namespace JS {
|
|
|
|
class ASTNode;
|
|
class Accessor;
|
|
class BigInt;
|
|
class BoundFunction;
|
|
class Cell;
|
|
class CellAllocator;
|
|
class ClassExpression;
|
|
class Console;
|
|
class DeclarativeEnvironment;
|
|
class DeferGC;
|
|
class Environment;
|
|
class Error;
|
|
class ErrorType;
|
|
class Exception;
|
|
class Expression;
|
|
class FunctionEnvironment;
|
|
class FunctionNode;
|
|
class GlobalEnvironment;
|
|
class GlobalObject;
|
|
class HandleImpl;
|
|
class Heap;
|
|
class HeapBlock;
|
|
class Interpreter;
|
|
class MarkedValueList;
|
|
class NativeFunction;
|
|
class NativeProperty;
|
|
class ObjectEnvironment;
|
|
class PrimitiveString;
|
|
class PromiseReaction;
|
|
class PromiseReactionJob;
|
|
class PromiseResolveThenableJob;
|
|
class PropertyAttributes;
|
|
class PropertyDescriptor;
|
|
class PropertyName;
|
|
class Reference;
|
|
class ScopeNode;
|
|
class Shape;
|
|
class Statement;
|
|
class StringOrSymbol;
|
|
class Symbol;
|
|
class Token;
|
|
class VM;
|
|
class Value;
|
|
class WeakContainer;
|
|
enum class DeclarationKind;
|
|
struct AlreadyResolved;
|
|
struct JobCallback;
|
|
struct PromiseCapability;
|
|
|
|
// Not included in JS_ENUMERATE_NATIVE_OBJECTS due to missing distinct prototype
|
|
class ProxyObject;
|
|
class ProxyConstructor;
|
|
|
|
// Not included in JS_ENUMERATE_NATIVE_OBJECTS due to missing distinct constructor
|
|
class GeneratorObjectPrototype;
|
|
|
|
class TypedArrayConstructor;
|
|
class TypedArrayPrototype;
|
|
|
|
// Tag type used to differentiate between u8 as used by Uint8Array and u8 as used by Uint8ClampedArray.
|
|
struct ClampedU8;
|
|
|
|
#define __JS_ENUMERATE(ClassName, snake_name, ConstructorName, PrototypeName, ArrayType) \
|
|
class ClassName; \
|
|
class ConstructorName; \
|
|
class PrototypeName;
|
|
JS_ENUMERATE_NATIVE_OBJECTS_EXCLUDING_TEMPLATES
|
|
JS_ENUMERATE_NATIVE_ERRORS
|
|
JS_ENUMERATE_TYPED_ARRAYS
|
|
#undef __JS_ENUMERATE
|
|
|
|
template<class T>
|
|
class Handle;
|
|
|
|
namespace Bytecode {
|
|
class BasicBlock;
|
|
struct Executable;
|
|
class Generator;
|
|
class Instruction;
|
|
class Interpreter;
|
|
class Register;
|
|
}
|
|
|
|
}
|