Serenity Operating System
at master 307 lines 16 kB view raw
1/* 2 * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org> 3 * Copyright (c) 2020-2022, Linus Groh <linusg@serenityos.org> 4 * 5 * SPDX-License-Identifier: BSD-2-Clause 6 */ 7 8#pragma once 9 10#include <AK/Concepts.h> 11#include <AK/Types.h> 12 13#define JS_DECLARE_NATIVE_FUNCTION(name) \ 14 static JS::ThrowCompletionOr<JS::Value> name(JS::VM&) 15 16#define JS_DEFINE_NATIVE_FUNCTION(name) \ 17 JS::ThrowCompletionOr<JS::Value> name([[maybe_unused]] JS::VM& vm) 18 19// NOTE: Proxy is not included here as it doesn't have a prototype - m_proxy_constructor is initialized separately. 20#define JS_ENUMERATE_NATIVE_OBJECTS_EXCLUDING_TEMPLATES \ 21 __JS_ENUMERATE(AggregateError, aggregate_error, AggregateErrorPrototype, AggregateErrorConstructor, void) \ 22 __JS_ENUMERATE(Array, array, ArrayPrototype, ArrayConstructor, void) \ 23 __JS_ENUMERATE(ArrayBuffer, array_buffer, ArrayBufferPrototype, ArrayBufferConstructor, void) \ 24 __JS_ENUMERATE(AsyncFunction, async_function, AsyncFunctionPrototype, AsyncFunctionConstructor, void) \ 25 __JS_ENUMERATE(AsyncGeneratorFunction, async_generator_function, AsyncGeneratorFunctionPrototype, AsyncGeneratorFunctionConstructor, void) \ 26 __JS_ENUMERATE(BigIntObject, bigint, BigIntPrototype, BigIntConstructor, void) \ 27 __JS_ENUMERATE(BooleanObject, boolean, BooleanPrototype, BooleanConstructor, void) \ 28 __JS_ENUMERATE(DataView, data_view, DataViewPrototype, DataViewConstructor, void) \ 29 __JS_ENUMERATE(Date, date, DatePrototype, DateConstructor, void) \ 30 __JS_ENUMERATE(DisposableStack, disposable_stack, DisposableStackPrototype, DisposableStackConstructor, void) \ 31 __JS_ENUMERATE(Error, error, ErrorPrototype, ErrorConstructor, void) \ 32 __JS_ENUMERATE(FinalizationRegistry, finalization_registry, FinalizationRegistryPrototype, FinalizationRegistryConstructor, void) \ 33 __JS_ENUMERATE(FunctionObject, function, FunctionPrototype, FunctionConstructor, void) \ 34 __JS_ENUMERATE(GeneratorFunction, generator_function, GeneratorFunctionPrototype, GeneratorFunctionConstructor, void) \ 35 __JS_ENUMERATE(Map, map, MapPrototype, MapConstructor, void) \ 36 __JS_ENUMERATE(NumberObject, number, NumberPrototype, NumberConstructor, void) \ 37 __JS_ENUMERATE(Object, object, ObjectPrototype, ObjectConstructor, void) \ 38 __JS_ENUMERATE(Promise, promise, PromisePrototype, PromiseConstructor, void) \ 39 __JS_ENUMERATE(RegExpObject, regexp, RegExpPrototype, RegExpConstructor, void) \ 40 __JS_ENUMERATE(Set, set, SetPrototype, SetConstructor, void) \ 41 __JS_ENUMERATE(ShadowRealm, shadow_realm, ShadowRealmPrototype, ShadowRealmConstructor, void) \ 42 __JS_ENUMERATE(StringObject, string, StringPrototype, StringConstructor, void) \ 43 __JS_ENUMERATE(SuppressedError, suppressed_error, SuppressedErrorPrototype, SuppressedErrorConstructor, void) \ 44 __JS_ENUMERATE(SymbolObject, symbol, SymbolPrototype, SymbolConstructor, void) \ 45 __JS_ENUMERATE(WeakMap, weak_map, WeakMapPrototype, WeakMapConstructor, void) \ 46 __JS_ENUMERATE(WeakRef, weak_ref, WeakRefPrototype, WeakRefConstructor, void) \ 47 __JS_ENUMERATE(WeakSet, weak_set, WeakSetPrototype, WeakSetConstructor, void) 48 49#define JS_ENUMERATE_NATIVE_OBJECTS \ 50 JS_ENUMERATE_NATIVE_OBJECTS_EXCLUDING_TEMPLATES \ 51 __JS_ENUMERATE(TypedArray, typed_array, TypedArrayPrototype, TypedArrayConstructor, void) 52 53#define JS_ENUMERATE_NATIVE_ERRORS \ 54 __JS_ENUMERATE(EvalError, eval_error, EvalErrorPrototype, EvalErrorConstructor, void) \ 55 __JS_ENUMERATE(InternalError, internal_error, InternalErrorPrototype, InternalErrorConstructor, void) \ 56 __JS_ENUMERATE(RangeError, range_error, RangeErrorPrototype, RangeErrorConstructor, void) \ 57 __JS_ENUMERATE(ReferenceError, reference_error, ReferenceErrorPrototype, ReferenceErrorConstructor, void) \ 58 __JS_ENUMERATE(SyntaxError, syntax_error, SyntaxErrorPrototype, SyntaxErrorConstructor, void) \ 59 __JS_ENUMERATE(TypeError, type_error, TypeErrorPrototype, TypeErrorConstructor, void) \ 60 __JS_ENUMERATE(URIError, uri_error, URIErrorPrototype, URIErrorConstructor, void) 61 62#define JS_ENUMERATE_TYPED_ARRAYS \ 63 __JS_ENUMERATE(Uint8Array, uint8_array, Uint8ArrayPrototype, Uint8ArrayConstructor, u8) \ 64 __JS_ENUMERATE(Uint8ClampedArray, uint8_clamped_array, Uint8ClampedArrayPrototype, Uint8ClampedArrayConstructor, ClampedU8) \ 65 __JS_ENUMERATE(Uint16Array, uint16_array, Uint16ArrayPrototype, Uint16ArrayConstructor, u16) \ 66 __JS_ENUMERATE(Uint32Array, uint32_array, Uint32ArrayPrototype, Uint32ArrayConstructor, u32) \ 67 __JS_ENUMERATE(BigUint64Array, big_uint64_array, BigUint64ArrayPrototype, BigUint64ArrayConstructor, u64) \ 68 __JS_ENUMERATE(Int8Array, int8_array, Int8ArrayPrototype, Int8ArrayConstructor, i8) \ 69 __JS_ENUMERATE(Int16Array, int16_array, Int16ArrayPrototype, Int16ArrayConstructor, i16) \ 70 __JS_ENUMERATE(Int32Array, int32_array, Int32ArrayPrototype, Int32ArrayConstructor, i32) \ 71 __JS_ENUMERATE(BigInt64Array, big_int64_array, BigInt64ArrayPrototype, BigInt64ArrayConstructor, i64) \ 72 __JS_ENUMERATE(Float32Array, float32_array, Float32ArrayPrototype, Float32ArrayConstructor, float) \ 73 __JS_ENUMERATE(Float64Array, float64_array, Float64ArrayPrototype, Float64ArrayConstructor, double) 74 75#define JS_ENUMERATE_INTL_OBJECTS \ 76 __JS_ENUMERATE(Collator, collator, CollatorPrototype, CollatorConstructor) \ 77 __JS_ENUMERATE(DateTimeFormat, date_time_format, DateTimeFormatPrototype, DateTimeFormatConstructor) \ 78 __JS_ENUMERATE(DisplayNames, display_names, DisplayNamesPrototype, DisplayNamesConstructor) \ 79 __JS_ENUMERATE(DurationFormat, duration_format, DurationFormatPrototype, DurationFormatConstructor) \ 80 __JS_ENUMERATE(ListFormat, list_format, ListFormatPrototype, ListFormatConstructor) \ 81 __JS_ENUMERATE(Locale, locale, LocalePrototype, LocaleConstructor) \ 82 __JS_ENUMERATE(NumberFormat, number_format, NumberFormatPrototype, NumberFormatConstructor) \ 83 __JS_ENUMERATE(PluralRules, plural_rules, PluralRulesPrototype, PluralRulesConstructor) \ 84 __JS_ENUMERATE(RelativeTimeFormat, relative_time_format, RelativeTimeFormatPrototype, RelativeTimeFormatConstructor) \ 85 __JS_ENUMERATE(Segmenter, segmenter, SegmenterPrototype, SegmenterConstructor) 86 87#define JS_ENUMERATE_TEMPORAL_OBJECTS \ 88 __JS_ENUMERATE(Calendar, calendar, CalendarPrototype, CalendarConstructor) \ 89 __JS_ENUMERATE(Duration, duration, DurationPrototype, DurationConstructor) \ 90 __JS_ENUMERATE(Instant, instant, InstantPrototype, InstantConstructor) \ 91 __JS_ENUMERATE(PlainDate, plain_date, PlainDatePrototype, PlainDateConstructor) \ 92 __JS_ENUMERATE(PlainDateTime, plain_date_time, PlainDateTimePrototype, PlainDateTimeConstructor) \ 93 __JS_ENUMERATE(PlainMonthDay, plain_month_day, PlainMonthDayPrototype, PlainMonthDayConstructor) \ 94 __JS_ENUMERATE(PlainTime, plain_time, PlainTimePrototype, PlainTimeConstructor) \ 95 __JS_ENUMERATE(PlainYearMonth, plain_year_month, PlainYearMonthPrototype, PlainYearMonthConstructor) \ 96 __JS_ENUMERATE(TimeZone, time_zone, TimeZonePrototype, TimeZoneConstructor) \ 97 __JS_ENUMERATE(ZonedDateTime, zoned_date_time, ZonedDateTimePrototype, ZonedDateTimeConstructor) 98 99#define JS_ENUMERATE_BUILTIN_NAMESPACE_OBJECTS \ 100 __JS_ENUMERATE(AtomicsObject, atomics) \ 101 __JS_ENUMERATE(ConsoleObject, console) \ 102 __JS_ENUMERATE(Intl::Intl, intl) \ 103 __JS_ENUMERATE(JSONObject, json) \ 104 __JS_ENUMERATE(MathObject, math) \ 105 __JS_ENUMERATE(ReflectObject, reflect) \ 106 __JS_ENUMERATE(Temporal::Temporal, temporal) 107 108#define JS_ENUMERATE_ITERATOR_PROTOTYPES \ 109 __JS_ENUMERATE(Iterator, iterator) \ 110 __JS_ENUMERATE(ArrayIterator, array_iterator) \ 111 __JS_ENUMERATE(AsyncIterator, async_iterator) \ 112 __JS_ENUMERATE(Intl::SegmentIterator, intl_segment_iterator) \ 113 __JS_ENUMERATE(MapIterator, map_iterator) \ 114 __JS_ENUMERATE(RegExpStringIterator, regexp_string_iterator) \ 115 __JS_ENUMERATE(SetIterator, set_iterator) \ 116 __JS_ENUMERATE(StringIterator, string_iterator) 117 118#define JS_ENUMERATE_BUILTIN_TYPES \ 119 JS_ENUMERATE_NATIVE_OBJECTS \ 120 JS_ENUMERATE_NATIVE_ERRORS \ 121 JS_ENUMERATE_TYPED_ARRAYS 122 123#define JS_ENUMERATE_WELL_KNOWN_SYMBOLS \ 124 __JS_ENUMERATE(iterator, iterator) \ 125 __JS_ENUMERATE(asyncIterator, async_iterator) \ 126 __JS_ENUMERATE(match, match) \ 127 __JS_ENUMERATE(matchAll, match_all) \ 128 __JS_ENUMERATE(replace, replace) \ 129 __JS_ENUMERATE(replaceAll, replace_all) \ 130 __JS_ENUMERATE(search, search) \ 131 __JS_ENUMERATE(split, split) \ 132 __JS_ENUMERATE(hasInstance, has_instance) \ 133 __JS_ENUMERATE(isConcatSpreadable, is_concat_spreadable) \ 134 __JS_ENUMERATE(unscopables, unscopables) \ 135 __JS_ENUMERATE(species, species) \ 136 __JS_ENUMERATE(toPrimitive, to_primitive) \ 137 __JS_ENUMERATE(toStringTag, to_string_tag) \ 138 __JS_ENUMERATE(dispose, dispose) 139 140#define JS_ENUMERATE_REGEXP_FLAGS \ 141 __JS_ENUMERATE(hasIndices, has_indices, d) \ 142 __JS_ENUMERATE(global, global, g) \ 143 __JS_ENUMERATE(ignoreCase, ignore_case, i) \ 144 __JS_ENUMERATE(multiline, multiline, m) \ 145 __JS_ENUMERATE(dotAll, dot_all, s) \ 146 __JS_ENUMERATE(unicodeSets, unicode_sets, v) \ 147 __JS_ENUMERATE(unicode, unicode, u) \ 148 __JS_ENUMERATE(sticky, sticky, y) 149 150namespace JS { 151 152class ASTNode; 153class Accessor; 154struct AsyncGeneratorRequest; 155class BigInt; 156class BoundFunction; 157class Cell; 158class CellAllocator; 159class ClassExpression; 160struct ClassFieldDefinition; 161class Completion; 162class Console; 163class DeclarativeEnvironment; 164class DeferGC; 165class ECMAScriptFunctionObject; 166class Environment; 167class Error; 168class ErrorType; 169struct ExecutionContext; 170struct ExportEntry; 171class ExportStatement; 172class Expression; 173class ForStatement; 174class FunctionEnvironment; 175class FunctionNode; 176struct FunctionParameter; 177class GlobalEnvironment; 178class GlobalObject; 179class HandleImpl; 180class Heap; 181class HeapBlock; 182struct ImportEntry; 183class ImportStatement; 184class Interpreter; 185class Intrinsics; 186class MetaProperty; 187class Module; 188struct ModuleRequest; 189class NativeFunction; 190class ObjectEnvironment; 191class Parser; 192struct ParserError; 193class PrimitiveString; 194class Program; 195class PromiseCapability; 196class PromiseReaction; 197class PropertyAttributes; 198class PropertyDescriptor; 199class PropertyKey; 200class Realm; 201class Reference; 202class ScopeNode; 203class Script; 204class Shape; 205class Statement; 206class StringOrSymbol; 207class SourceCode; 208struct SourceRange; 209class SourceTextModule; 210class Symbol; 211class Token; 212class Utf16String; 213class VM; 214class Value; 215class WeakContainer; 216class WrappedFunction; 217enum class DeclarationKind; 218struct AlreadyResolved; 219struct JobCallback; 220struct ModuleRequest; 221 222// Not included in JS_ENUMERATE_NATIVE_OBJECTS due to missing distinct prototype 223class ProxyObject; 224class ProxyConstructor; 225 226// Not included in JS_ENUMERATE_NATIVE_OBJECTS due to missing distinct constructor 227class AsyncFromSyncIteratorPrototype; 228class AsyncGenerator; 229class AsyncGeneratorPrototype; 230class GeneratorPrototype; 231 232class TypedArrayConstructor; 233class TypedArrayPrototype; 234 235class AtomicsObject; 236class ConsoleObject; 237class JSONObject; 238class MathObject; 239class ReflectObject; 240 241// Tag type used to differentiate between u8 as used by Uint8Array and u8 as used by Uint8ClampedArray. 242struct ClampedU8; 243 244#define __JS_ENUMERATE(ClassName, snake_name, ConstructorName, PrototypeName, ArrayType) \ 245 class ClassName; \ 246 class ConstructorName; \ 247 class PrototypeName; 248JS_ENUMERATE_NATIVE_OBJECTS_EXCLUDING_TEMPLATES 249JS_ENUMERATE_NATIVE_ERRORS 250JS_ENUMERATE_TYPED_ARRAYS 251#undef __JS_ENUMERATE 252 253#define __JS_ENUMERATE(ClassName, snake_name) \ 254 class ClassName; \ 255 JS_ENUMERATE_BUILTIN_NAMESPACE_OBJECTS 256#undef __JS_ENUMERATE 257 258namespace Intl { 259#define __JS_ENUMERATE(ClassName, snake_name, ConstructorName, PrototypeName) \ 260 class ClassName; \ 261 class ConstructorName; \ 262 class PrototypeName; 263JS_ENUMERATE_INTL_OBJECTS 264#undef __JS_ENUMERATE 265 266class Intl; 267class MathematicalValue; 268 269// Not included in JS_ENUMERATE_INTL_OBJECTS due to missing distinct constructor 270class Segments; 271class SegmentsPrototype; 272}; 273 274namespace Temporal { 275#define __JS_ENUMERATE(ClassName, snake_name, ConstructorName, PrototypeName) \ 276 class ClassName; \ 277 class ConstructorName; \ 278 class PrototypeName; 279JS_ENUMERATE_TEMPORAL_OBJECTS 280#undef __JS_ENUMERATE 281class Temporal; 282struct DurationRecord; 283struct DateDurationRecord; 284struct TimeDurationRecord; 285struct PartialDurationRecord; 286}; 287 288template<typename T> 289requires(!IsLvalueReference<T>) 290class ThrowCompletionOr; 291 292template<class T> 293class Handle; 294 295template<class T, size_t inline_capacity = 32> 296class MarkedVector; 297 298namespace Bytecode { 299class BasicBlock; 300struct Executable; 301class Generator; 302class Instruction; 303class Interpreter; 304class Register; 305} 306 307}