2020-04-06 11:09:01 +02:00
/*
* Copyright ( c ) 2020 , Andreas Kling < kling @ serenityos . org >
2022-04-11 22:47:50 +01:00
* Copyright ( c ) 2020 - 2022 , Linus Groh < linusg @ serenityos . org >
2020-04-06 11:09:01 +02:00
*
2021-04-22 01:24:48 -07:00
* SPDX - License - Identifier : BSD - 2 - Clause
2020-04-06 11:09:01 +02:00
*/
2021-12-19 15:46:55 -06:00
# include <AK/BuiltinWrappers.h>
2021-06-01 21:18:08 +02:00
# include <AK/CharacterTypes.h>
2021-04-13 22:50:29 +03:00
# include <AK/Hex.h>
# include <AK/Platform.h>
2022-01-16 19:06:03 +02:00
# include <AK/UnicodeUtils.h>
2022-01-13 19:45:11 -05:00
# include <AK/Utf16View.h>
2020-12-05 13:51:09 +01:00
# include <AK/Utf8View.h>
2020-09-29 21:15:06 +02:00
# include <LibJS/Console.h>
2020-09-20 19:24:44 +02:00
# include <LibJS/Heap/DeferGC.h>
2021-03-14 16:20:01 +01:00
# include <LibJS/Interpreter.h>
2021-06-19 20:13:53 -07:00
# include <LibJS/Runtime/AbstractOperations.h>
2021-06-11 18:06:20 +01:00
# include <LibJS/Runtime/AggregateErrorConstructor.h>
# include <LibJS/Runtime/AggregateErrorPrototype.h>
2020-12-02 20:49:31 +00:00
# include <LibJS/Runtime/ArrayBufferConstructor.h>
# include <LibJS/Runtime/ArrayBufferPrototype.h>
2020-04-04 22:28:21 +02:00
# include <LibJS/Runtime/ArrayConstructor.h>
2020-07-09 14:58:20 -07:00
# include <LibJS/Runtime/ArrayIteratorPrototype.h>
2020-04-18 13:18:06 +02:00
# include <LibJS/Runtime/ArrayPrototype.h>
2021-11-23 15:01:35 +01:00
# include <LibJS/Runtime/AsyncFromSyncIteratorPrototype.h>
2021-11-09 20:39:22 +02:00
# include <LibJS/Runtime/AsyncFunctionConstructor.h>
# include <LibJS/Runtime/AsyncFunctionPrototype.h>
2021-11-15 01:53:24 +01:00
# include <LibJS/Runtime/AsyncGeneratorFunctionConstructor.h>
# include <LibJS/Runtime/AsyncGeneratorFunctionPrototype.h>
2022-05-05 08:47:36 +02:00
# include <LibJS/Runtime/AsyncGeneratorPrototype.h>
2021-11-23 14:53:02 +01:00
# include <LibJS/Runtime/AsyncIteratorPrototype.h>
2021-07-10 22:53:54 -04:00
# include <LibJS/Runtime/AtomicsObject.h>
2020-06-06 01:14:10 +01:00
# include <LibJS/Runtime/BigIntConstructor.h>
# include <LibJS/Runtime/BigIntPrototype.h>
2020-04-06 22:51:16 -05:00
# include <LibJS/Runtime/BooleanConstructor.h>
2020-04-18 13:18:06 +02:00
# include <LibJS/Runtime/BooleanPrototype.h>
2020-03-16 14:20:30 +01:00
# include <LibJS/Runtime/ConsoleObject.h>
2021-06-14 01:47:08 +03:00
# include <LibJS/Runtime/DataViewConstructor.h>
# include <LibJS/Runtime/DataViewPrototype.h>
2020-03-30 00:21:56 +01:00
# include <LibJS/Runtime/DateConstructor.h>
2020-04-18 13:18:06 +02:00
# include <LibJS/Runtime/DatePrototype.h>
2020-04-01 19:42:07 +01:00
# include <LibJS/Runtime/ErrorConstructor.h>
2020-04-18 13:18:06 +02:00
# include <LibJS/Runtime/ErrorPrototype.h>
2021-06-15 22:16:17 +03:00
# include <LibJS/Runtime/FinalizationRegistryConstructor.h>
# include <LibJS/Runtime/FinalizationRegistryPrototype.h>
2020-04-04 14:34:31 +01:00
# include <LibJS/Runtime/FunctionConstructor.h>
2020-04-18 13:18:06 +02:00
# include <LibJS/Runtime/FunctionPrototype.h>
2021-06-15 00:04:08 -07:00
# include <LibJS/Runtime/GeneratorFunctionConstructor.h>
# include <LibJS/Runtime/GeneratorFunctionPrototype.h>
2022-01-16 14:14:42 +01:00
# include <LibJS/Runtime/GeneratorPrototype.h>
2021-07-01 12:24:46 +02:00
# include <LibJS/Runtime/GlobalEnvironment.h>
2020-03-16 14:20:30 +01:00
# include <LibJS/Runtime/GlobalObject.h>
2021-09-06 22:18:48 -04:00
# include <LibJS/Runtime/Intl/CollatorConstructor.h>
# include <LibJS/Runtime/Intl/CollatorPrototype.h>
2021-11-18 10:30:31 -05:00
# include <LibJS/Runtime/Intl/DateTimeFormatConstructor.h>
# include <LibJS/Runtime/Intl/DateTimeFormatPrototype.h>
2021-08-24 22:27:05 -04:00
# include <LibJS/Runtime/Intl/DisplayNamesConstructor.h>
# include <LibJS/Runtime/Intl/DisplayNamesPrototype.h>
2022-06-30 00:58:40 +03:00
# include <LibJS/Runtime/Intl/DurationFormatConstructor.h>
# include <LibJS/Runtime/Intl/DurationFormatPrototype.h>
2021-08-08 18:27:28 +01:00
# include <LibJS/Runtime/Intl/Intl.h>
2021-09-05 23:05:16 -04:00
# include <LibJS/Runtime/Intl/ListFormatConstructor.h>
# include <LibJS/Runtime/Intl/ListFormatPrototype.h>
2021-09-02 08:32:43 -04:00
# include <LibJS/Runtime/Intl/LocaleConstructor.h>
# include <LibJS/Runtime/Intl/LocalePrototype.h>
2021-09-08 21:34:27 -04:00
# include <LibJS/Runtime/Intl/NumberFormatConstructor.h>
# include <LibJS/Runtime/Intl/NumberFormatPrototype.h>
2022-01-28 12:56:04 -05:00
# include <LibJS/Runtime/Intl/PluralRulesConstructor.h>
# include <LibJS/Runtime/Intl/PluralRulesPrototype.h>
2022-01-25 10:41:57 -05:00
# include <LibJS/Runtime/Intl/RelativeTimeFormatConstructor.h>
# include <LibJS/Runtime/Intl/RelativeTimeFormatPrototype.h>
2022-01-30 02:18:47 +02:00
# include <LibJS/Runtime/Intl/SegmentIteratorPrototype.h>
2022-01-29 23:47:29 +02:00
# include <LibJS/Runtime/Intl/SegmenterConstructor.h>
# include <LibJS/Runtime/Intl/SegmenterPrototype.h>
2022-01-30 01:08:42 +02:00
# include <LibJS/Runtime/Intl/SegmentsPrototype.h>
2020-07-09 14:58:20 -07:00
# include <LibJS/Runtime/IteratorPrototype.h>
2020-06-10 11:01:00 -07:00
# include <LibJS/Runtime/JSONObject.h>
2021-06-12 23:54:40 +03:00
# include <LibJS/Runtime/MapConstructor.h>
2021-06-12 23:58:03 +03:00
# include <LibJS/Runtime/MapIteratorPrototype.h>
2021-06-12 23:54:40 +03:00
# include <LibJS/Runtime/MapPrototype.h>
2020-03-21 17:52:12 +01:00
# include <LibJS/Runtime/MathObject.h>
2020-03-16 14:20:30 +01:00
# include <LibJS/Runtime/NativeFunction.h>
2020-04-07 16:17:23 +01:00
# include <LibJS/Runtime/NumberConstructor.h>
2020-04-18 13:18:06 +02:00
# include <LibJS/Runtime/NumberPrototype.h>
# include <LibJS/Runtime/Object.h>
2020-03-28 17:23:54 +01:00
# include <LibJS/Runtime/ObjectConstructor.h>
2020-04-18 13:18:06 +02:00
# include <LibJS/Runtime/ObjectPrototype.h>
LibJS: Add initial support for Promises
Almost a year after first working on this, it's finally done: an
implementation of Promises for LibJS! :^)
The core functionality is working and closely following the spec [1].
I mostly took the pseudo code and transformed it into C++ - if you read
and understand it, you will know how the spec implements Promises; and
if you read the spec first, the code will look very familiar.
Implemented functions are:
- Promise() constructor
- Promise.prototype.then()
- Promise.prototype.catch()
- Promise.prototype.finally()
- Promise.resolve()
- Promise.reject()
For the tests I added a new function to test-js's global object,
runQueuedPromiseJobs(), which calls vm.run_queued_promise_jobs().
By design, queued jobs normally only run after the script was fully
executed, making it improssible to test handlers in individual test()
calls by default [2].
Subsequent commits include integrations into LibWeb and js(1) -
pretty-printing, running queued promise jobs when necessary.
This has an unusual amount of dbgln() statements, all hidden behind the
PROMISE_DEBUG flag - I'm leaving them in for now as they've been very
useful while debugging this, things can get quite complex with so many
asynchronously executed functions.
I've not extensively explored use of these APIs for promise-based
functionality in LibWeb (fetch(), Notification.requestPermission()
etc.), but we'll get there in due time.
[1]: https://tc39.es/ecma262/#sec-promise-objects
[2]: https://tc39.es/ecma262/#sec-jobs-and-job-queues
2021-04-01 22:13:29 +02:00
# include <LibJS/Runtime/PromiseConstructor.h>
# include <LibJS/Runtime/PromisePrototype.h>
2020-06-03 14:34:52 -07:00
# include <LibJS/Runtime/ProxyConstructor.h>
2021-10-14 12:04:10 +01:00
# include <LibJS/Runtime/Realm.h>
2020-05-01 11:06:27 +01:00
# include <LibJS/Runtime/ReflectObject.h>
2020-06-03 16:05:49 -07:00
# include <LibJS/Runtime/RegExpConstructor.h>
# include <LibJS/Runtime/RegExpPrototype.h>
2021-07-15 08:58:27 -04:00
# include <LibJS/Runtime/RegExpStringIteratorPrototype.h>
2021-06-09 00:08:47 +03:00
# include <LibJS/Runtime/SetConstructor.h>
2021-06-09 00:17:17 +03:00
# include <LibJS/Runtime/SetIteratorPrototype.h>
2021-06-09 00:08:47 +03:00
# include <LibJS/Runtime/SetPrototype.h>
2021-10-13 21:09:45 +01:00
# include <LibJS/Runtime/ShadowRealmConstructor.h>
# include <LibJS/Runtime/ShadowRealmPrototype.h>
2020-04-18 13:18:06 +02:00
# include <LibJS/Runtime/Shape.h>
2020-04-10 14:14:02 +02:00
# include <LibJS/Runtime/StringConstructor.h>
2020-07-11 20:23:01 -07:00
# include <LibJS/Runtime/StringIteratorPrototype.h>
2020-04-18 13:18:06 +02:00
# include <LibJS/Runtime/StringPrototype.h>
2020-04-29 23:25:21 -07:00
# include <LibJS/Runtime/SymbolConstructor.h>
# include <LibJS/Runtime/SymbolPrototype.h>
2021-07-14 21:01:12 +01:00
# include <LibJS/Runtime/Temporal/CalendarConstructor.h>
# include <LibJS/Runtime/Temporal/CalendarPrototype.h>
2021-07-15 23:20:43 +01:00
# include <LibJS/Runtime/Temporal/DurationConstructor.h>
# include <LibJS/Runtime/Temporal/DurationPrototype.h>
2021-07-07 17:41:37 +01:00
# include <LibJS/Runtime/Temporal/InstantConstructor.h>
# include <LibJS/Runtime/Temporal/InstantPrototype.h>
2021-07-19 00:29:26 +03:00
# include <LibJS/Runtime/Temporal/PlainDateConstructor.h>
# include <LibJS/Runtime/Temporal/PlainDatePrototype.h>
2021-07-22 19:47:07 +01:00
# include <LibJS/Runtime/Temporal/PlainDateTimeConstructor.h>
# include <LibJS/Runtime/Temporal/PlainDateTimePrototype.h>
2021-08-14 23:54:24 +01:00
# include <LibJS/Runtime/Temporal/PlainMonthDayConstructor.h>
# include <LibJS/Runtime/Temporal/PlainMonthDayPrototype.h>
2021-07-28 18:36:52 +01:00
# include <LibJS/Runtime/Temporal/PlainTimeConstructor.h>
# include <LibJS/Runtime/Temporal/PlainTimePrototype.h>
2021-08-07 22:40:32 +01:00
# include <LibJS/Runtime/Temporal/PlainYearMonthConstructor.h>
# include <LibJS/Runtime/Temporal/PlainYearMonthPrototype.h>
2021-07-06 19:14:47 +01:00
# include <LibJS/Runtime/Temporal/Temporal.h>
2021-07-06 23:53:27 +01:00
# include <LibJS/Runtime/Temporal/TimeZoneConstructor.h>
# include <LibJS/Runtime/Temporal/TimeZonePrototype.h>
2021-08-01 17:20:11 +01:00
# include <LibJS/Runtime/Temporal/ZonedDateTimeConstructor.h>
# include <LibJS/Runtime/Temporal/ZonedDateTimePrototype.h>
2020-12-01 21:05:25 +01:00
# include <LibJS/Runtime/TypedArray.h>
2020-12-02 00:23:40 +00:00
# include <LibJS/Runtime/TypedArrayConstructor.h>
# include <LibJS/Runtime/TypedArrayPrototype.h>
2020-03-16 14:20:30 +01:00
# include <LibJS/Runtime/Value.h>
2021-06-12 05:28:30 +03:00
# include <LibJS/Runtime/WeakMapConstructor.h>
# include <LibJS/Runtime/WeakMapPrototype.h>
2021-06-12 17:38:34 +03:00
# include <LibJS/Runtime/WeakRefConstructor.h>
# include <LibJS/Runtime/WeakRefPrototype.h>
2021-06-09 19:23:04 +03:00
# include <LibJS/Runtime/WeakSetConstructor.h>
# include <LibJS/Runtime/WeakSetPrototype.h>
2020-03-12 20:11:35 +01:00
namespace JS {
2022-08-01 20:27:20 +02:00
GlobalObject : : GlobalObject ( Realm & realm )
: Object ( GlobalObjectTag : : Tag , realm )
2020-09-29 21:15:06 +02:00
, m_console ( make < Console > ( * this ) )
2020-03-12 20:11:35 +01:00
{
2020-04-18 13:18:06 +02:00
}
2021-03-17 16:52:26 +01:00
void GlobalObject : : initialize_global_object ( )
2020-04-18 13:18:06 +02:00
{
2020-10-13 23:49:19 +02:00
auto & vm = this - > vm ( ) ;
2020-10-04 22:56:45 +02:00
ensure_shape_is_unique ( ) ;
2020-04-18 13:18:06 +02:00
// These are done first since other prototypes depend on their presence.
2022-08-01 20:27:20 +02:00
VERIFY ( associated_realm ( ) ) ;
auto & realm = * associated_realm ( ) ;
2022-08-16 00:20:50 +01:00
m_empty_object_shape = heap ( ) . allocate_without_realm < Shape > ( realm ) ;
m_object_prototype = heap ( ) . allocate_without_realm < ObjectPrototype > ( realm ) ;
m_function_prototype = heap ( ) . allocate_without_realm < FunctionPrototype > ( realm ) ;
2020-04-18 13:18:06 +02:00
2022-08-16 00:20:50 +01:00
m_new_object_shape = vm . heap ( ) . allocate_without_realm < Shape > ( realm ) ;
2020-10-17 23:13:37 +02:00
m_new_object_shape - > set_prototype_without_transition ( m_object_prototype ) ;
2022-08-16 00:20:50 +01:00
m_new_ordinary_function_prototype_object_shape = vm . heap ( ) . allocate_without_realm < Shape > ( realm ) ;
2021-06-27 22:15:58 +02:00
m_new_ordinary_function_prototype_object_shape - > set_prototype_without_transition ( m_object_prototype ) ;
m_new_ordinary_function_prototype_object_shape - > add_property_without_transition ( vm . names . constructor , Attribute : : Writable | Attribute : : Configurable ) ;
2020-10-17 23:13:37 +02:00
2022-08-16 00:20:50 +01:00
// Normally Heap::allocate() takes care of this, but these are allocated via allocate_without_realm().
2022-08-16 00:20:49 +01:00
static_cast < FunctionPrototype * > ( m_function_prototype ) - > initialize ( realm ) ;
static_cast < ObjectPrototype * > ( m_object_prototype ) - > initialize ( realm ) ;
2020-04-18 13:18:06 +02:00
2021-10-02 14:32:37 +01:00
Object : : set_prototype ( m_object_prototype ) ;
2020-11-06 18:56:54 +00:00
2021-06-11 18:06:20 +01:00
// This must be initialized before allocating AggregateErrorPrototype, which uses ErrorPrototype as its prototype.
2022-08-16 00:20:50 +01:00
m_error_prototype = heap ( ) . allocate < ErrorPrototype > ( realm , realm ) ;
2021-06-11 18:06:20 +01:00
2020-09-20 19:24:44 +02:00
# define __JS_ENUMERATE(ClassName, snake_name) \
if ( ! m_ # # snake_name # # _prototype ) \
2022-08-16 00:20:50 +01:00
m_ # # snake_name # # _prototype = heap ( ) . allocate < ClassName # # Prototype > ( realm , realm ) ;
2020-07-09 14:58:20 -07:00
JS_ENUMERATE_ITERATOR_PROTOTYPES
# undef __JS_ENUMERATE
2022-05-05 08:47:36 +02:00
// These must be initialized separately as they have no companion constructor
2022-08-16 00:20:50 +01:00
m_async_from_sync_iterator_prototype = heap ( ) . allocate < AsyncFromSyncIteratorPrototype > ( realm , realm ) ;
m_async_generator_prototype = heap ( ) . allocate < AsyncGeneratorPrototype > ( realm , realm ) ;
m_generator_prototype = heap ( ) . allocate < GeneratorPrototype > ( realm , realm ) ;
m_intl_segments_prototype = heap ( ) . allocate < Intl : : SegmentsPrototype > ( realm , realm ) ;
2022-01-30 01:08:42 +02:00
2021-06-15 00:04:08 -07:00
# define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \
if ( ! m_ # # snake_name # # _prototype ) \
2022-08-16 00:20:50 +01:00
m_ # # snake_name # # _prototype = heap ( ) . allocate < PrototypeName > ( realm , realm ) ;
2021-06-15 00:04:08 -07:00
JS_ENUMERATE_BUILTIN_TYPES
# undef __JS_ENUMERATE
2021-08-08 18:35:29 +01:00
# define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \
if ( ! m_intl_ # # snake_name # # _prototype ) \
2022-08-16 00:20:50 +01:00
m_intl_ # # snake_name # # _prototype = heap ( ) . allocate < Intl : : PrototypeName > ( realm , realm ) ;
2021-08-08 18:35:29 +01:00
JS_ENUMERATE_INTL_OBJECTS
# undef __JS_ENUMERATE
// Must be allocated before `Intl::Intl` below.
# define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \
initialize_constructor ( vm . names . ClassName , m_intl_ # # snake_name # # _constructor , m_intl_ # # snake_name # # _prototype ) ;
JS_ENUMERATE_INTL_OBJECTS
# undef __JS_ENUMERATE
2021-07-06 20:39:55 +01:00
# define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \
if ( ! m_temporal_ # # snake_name # # _prototype ) \
2022-08-16 00:20:50 +01:00
m_temporal_ # # snake_name # # _prototype = heap ( ) . allocate < Temporal : : PrototypeName > ( realm , realm ) ;
2021-07-06 20:39:55 +01:00
JS_ENUMERATE_TEMPORAL_OBJECTS
# undef __JS_ENUMERATE
// Must be allocated before `Temporal::Temporal` below.
# define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \
initialize_constructor ( vm . names . ClassName , m_temporal_ # # snake_name # # _constructor , m_temporal_ # # snake_name # # _prototype ) ;
JS_ENUMERATE_TEMPORAL_OBJECTS
# undef __JS_ENUMERATE
2020-04-27 23:05:02 -07:00
u8 attr = Attribute : : Writable | Attribute : : Configurable ;
2021-10-29 17:04:01 -04:00
define_native_function ( vm . names . gc , gc , 0 , attr ) ;
define_native_function ( vm . names . isNaN , is_nan , 1 , attr ) ;
define_native_function ( vm . names . isFinite , is_finite , 1 , attr ) ;
define_native_function ( vm . names . parseFloat , parse_float , 1 , attr ) ;
define_native_function ( vm . names . parseInt , parse_int , 2 , attr ) ;
define_native_function ( vm . names . eval , eval , 1 , attr ) ;
2021-06-19 20:13:53 -07:00
2021-06-28 03:53:35 +03:00
// 10.2.4.1 %ThrowTypeError% ( ), https://tc39.es/ecma262/#sec-%throwtypeerror%
2022-08-16 00:20:49 +01:00
m_throw_type_error_function = NativeFunction : : create (
realm , [ ] ( VM & vm , GlobalObject & global_object ) {
return vm . throw_completion < TypeError > ( global_object , ErrorType : : RestrictedFunctionPropertiesAccess ) ;
} ,
0 , " " , & realm ) ;
2021-10-01 02:43:57 +02:00
m_throw_type_error_function - > define_direct_property ( vm . names . length , Value ( 0 ) , 0 ) ;
m_throw_type_error_function - > define_direct_property ( vm . names . name , js_string ( vm , " " ) , 0 ) ;
2021-10-03 15:57:26 +01:00
MUST ( m_throw_type_error_function - > internal_prevent_extensions ( ) ) ;
2021-06-28 03:53:35 +03:00
// 10.2.4 AddRestrictedFunctionProperties ( F, realm ), https://tc39.es/ecma262/#sec-addrestrictedfunctionproperties
2021-10-01 02:43:57 +02:00
m_function_prototype - > define_direct_accessor ( vm . names . caller , m_throw_type_error_function , m_throw_type_error_function , Attribute : : Configurable ) ;
m_function_prototype - > define_direct_accessor ( vm . names . arguments , m_throw_type_error_function , m_throw_type_error_function , Attribute : : Configurable ) ;
2021-06-28 03:53:35 +03:00
2021-10-29 17:04:01 -04:00
define_native_function ( vm . names . encodeURI , encode_uri , 1 , attr ) ;
define_native_function ( vm . names . decodeURI , decode_uri , 1 , attr ) ;
define_native_function ( vm . names . encodeURIComponent , encode_uri_component , 1 , attr ) ;
define_native_function ( vm . names . decodeURIComponent , decode_uri_component , 1 , attr ) ;
define_native_function ( vm . names . escape , escape , 1 , attr ) ;
define_native_function ( vm . names . unescape , unescape , 1 , attr ) ;
2020-10-13 23:49:19 +02:00
2021-07-06 02:15:08 +03:00
define_direct_property ( vm . names . NaN , js_nan ( ) , 0 ) ;
define_direct_property ( vm . names . Infinity , js_infinity ( ) , 0 ) ;
define_direct_property ( vm . names . undefined , js_undefined ( ) , 0 ) ;
2020-10-13 23:49:19 +02:00
2021-07-06 02:15:08 +03:00
define_direct_property ( vm . names . globalThis , this , attr ) ;
2022-08-16 00:20:50 +01:00
define_direct_property ( vm . names . console , heap ( ) . allocate < ConsoleObject > ( realm , realm ) , attr ) ;
define_direct_property ( vm . names . Atomics , heap ( ) . allocate < AtomicsObject > ( realm , realm ) , attr ) ;
define_direct_property ( vm . names . Math , heap ( ) . allocate < MathObject > ( realm , realm ) , attr ) ;
define_direct_property ( vm . names . JSON , heap ( ) . allocate < JSONObject > ( realm , realm ) , attr ) ;
define_direct_property ( vm . names . Reflect , heap ( ) . allocate < ReflectObject > ( realm , realm ) , attr ) ;
define_direct_property ( vm . names . Intl , heap ( ) . allocate < Intl : : Intl > ( realm , realm ) , attr ) ;
define_direct_property ( vm . names . Temporal , heap ( ) . allocate < Temporal : : Temporal > ( realm , realm ) , attr ) ;
2020-10-13 23:49:19 +02:00
2021-06-11 18:06:20 +01:00
// This must be initialized before allocating AggregateErrorConstructor, which uses ErrorConstructor as its prototype.
initialize_constructor ( vm . names . Error , m_error_constructor , m_error_prototype ) ;
add_constructor ( vm . names . AggregateError , m_aggregate_error_constructor , m_aggregate_error_prototype ) ;
2020-11-30 22:50:43 +00:00
add_constructor ( vm . names . Array , m_array_constructor , m_array_prototype ) ;
2020-12-02 20:49:31 +00:00
add_constructor ( vm . names . ArrayBuffer , m_array_buffer_constructor , m_array_buffer_prototype ) ;
2020-11-30 22:50:43 +00:00
add_constructor ( vm . names . BigInt , m_bigint_constructor , m_bigint_prototype ) ;
add_constructor ( vm . names . Boolean , m_boolean_constructor , m_boolean_prototype ) ;
2021-06-14 01:47:08 +03:00
add_constructor ( vm . names . DataView , m_data_view_constructor , m_data_view_prototype ) ;
2020-11-30 22:50:43 +00:00
add_constructor ( vm . names . Date , m_date_constructor , m_date_prototype ) ;
add_constructor ( vm . names . Error , m_error_constructor , m_error_prototype ) ;
2021-06-15 22:16:17 +03:00
add_constructor ( vm . names . FinalizationRegistry , m_finalization_registry_constructor , m_finalization_registry_prototype ) ;
2020-11-30 22:50:43 +00:00
add_constructor ( vm . names . Function , m_function_constructor , m_function_prototype ) ;
2021-06-12 23:54:40 +03:00
add_constructor ( vm . names . Map , m_map_constructor , m_map_prototype ) ;
2020-11-30 22:50:43 +00:00
add_constructor ( vm . names . Number , m_number_constructor , m_number_prototype ) ;
add_constructor ( vm . names . Object , m_object_constructor , m_object_prototype ) ;
LibJS: Add initial support for Promises
Almost a year after first working on this, it's finally done: an
implementation of Promises for LibJS! :^)
The core functionality is working and closely following the spec [1].
I mostly took the pseudo code and transformed it into C++ - if you read
and understand it, you will know how the spec implements Promises; and
if you read the spec first, the code will look very familiar.
Implemented functions are:
- Promise() constructor
- Promise.prototype.then()
- Promise.prototype.catch()
- Promise.prototype.finally()
- Promise.resolve()
- Promise.reject()
For the tests I added a new function to test-js's global object,
runQueuedPromiseJobs(), which calls vm.run_queued_promise_jobs().
By design, queued jobs normally only run after the script was fully
executed, making it improssible to test handlers in individual test()
calls by default [2].
Subsequent commits include integrations into LibWeb and js(1) -
pretty-printing, running queued promise jobs when necessary.
This has an unusual amount of dbgln() statements, all hidden behind the
PROMISE_DEBUG flag - I'm leaving them in for now as they've been very
useful while debugging this, things can get quite complex with so many
asynchronously executed functions.
I've not extensively explored use of these APIs for promise-based
functionality in LibWeb (fetch(), Notification.requestPermission()
etc.), but we'll get there in due time.
[1]: https://tc39.es/ecma262/#sec-promise-objects
[2]: https://tc39.es/ecma262/#sec-jobs-and-job-queues
2021-04-01 22:13:29 +02:00
add_constructor ( vm . names . Promise , m_promise_constructor , m_promise_prototype ) ;
2020-11-30 22:50:43 +00:00
add_constructor ( vm . names . Proxy , m_proxy_constructor , nullptr ) ;
add_constructor ( vm . names . RegExp , m_regexp_constructor , m_regexp_prototype ) ;
2021-06-09 00:08:47 +03:00
add_constructor ( vm . names . Set , m_set_constructor , m_set_prototype ) ;
2021-10-13 21:09:45 +01:00
add_constructor ( vm . names . ShadowRealm , m_shadow_realm_constructor , m_shadow_realm_prototype ) ;
2020-11-30 22:50:43 +00:00
add_constructor ( vm . names . String , m_string_constructor , m_string_prototype ) ;
add_constructor ( vm . names . Symbol , m_symbol_constructor , m_symbol_prototype ) ;
2021-06-12 05:28:30 +03:00
add_constructor ( vm . names . WeakMap , m_weak_map_constructor , m_weak_map_prototype ) ;
2021-06-12 17:38:34 +03:00
add_constructor ( vm . names . WeakRef , m_weak_ref_constructor , m_weak_ref_prototype ) ;
2021-06-09 19:23:04 +03:00
add_constructor ( vm . names . WeakSet , m_weak_set_constructor , m_weak_set_prototype ) ;
2020-04-10 12:42:33 +02:00
2020-12-02 00:23:40 +00:00
initialize_constructor ( vm . names . TypedArray , m_typed_array_constructor , m_typed_array_prototype ) ;
2020-12-01 21:05:25 +01:00
# define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \
2020-11-30 22:50:43 +00:00
add_constructor ( vm . names . ClassName , m_ # # snake_name # # _constructor , m_ # # snake_name # # _prototype ) ;
2021-06-11 17:54:42 +01:00
JS_ENUMERATE_NATIVE_ERRORS
2020-12-01 21:05:25 +01:00
JS_ENUMERATE_TYPED_ARRAYS
2020-04-10 14:06:52 +02:00
# undef __JS_ENUMERATE
2021-06-15 00:04:08 -07:00
2022-01-19 19:42:45 +00:00
// NOTE: These constructors cannot be initialized with add_constructor as they have no global binding.
2022-02-19 17:49:25 +02:00
initialize_constructor ( vm . names . GeneratorFunction , m_generator_function_constructor , m_generator_function_prototype , Attribute : : Configurable ) ;
initialize_constructor ( vm . names . AsyncGeneratorFunction , m_async_generator_function_constructor , m_async_generator_function_prototype , Attribute : : Configurable ) ;
initialize_constructor ( vm . names . AsyncFunction , m_async_function_constructor , m_async_function_prototype , Attribute : : Configurable ) ;
2021-11-15 01:53:24 +01:00
2022-01-16 14:47:14 +01:00
// 27.5.1.1 Generator.prototype.constructor, https://tc39.es/ecma262/#sec-generator.prototype.constructor
m_generator_prototype - > define_direct_property ( vm . names . constructor , m_generator_function_prototype , Attribute : : Configurable ) ;
2022-05-05 08:47:36 +02:00
// 27.6.1.1 AsyncGenerator.prototype.constructor, https://tc39.es/ecma262/#sec-asyncgenerator-prototype-constructor
m_async_generator_prototype - > define_direct_property ( vm . names . constructor , m_async_generator_function_prototype , Attribute : : Configurable ) ;
2021-07-25 19:37:42 +01:00
m_array_prototype_values_function = & m_array_prototype - > get_without_side_effects ( vm . names . values ) . as_function ( ) ;
2021-12-04 18:56:44 -05:00
m_date_constructor_now_function = & m_date_constructor - > get_without_side_effects ( vm . names . now ) . as_function ( ) ;
2021-07-25 19:37:42 +01:00
m_eval_function = & get_without_side_effects ( vm . names . eval ) . as_function ( ) ;
2022-01-27 14:49:55 +01:00
m_json_parse_function = & get_without_side_effects ( vm . names . JSON ) . as_object ( ) . get_without_side_effects ( vm . names . parse ) . as_function ( ) ;
2022-04-11 22:47:50 +01:00
m_object_prototype_to_string_function = & m_object_prototype - > get_without_side_effects ( vm . names . toString ) . as_function ( ) ;
2020-03-12 20:11:35 +01:00
}
2022-03-14 10:25:06 -06:00
GlobalObject : : ~ GlobalObject ( ) = default ;
2020-03-12 20:11:35 +01:00
2020-11-28 14:33:36 +01:00
void GlobalObject : : visit_edges ( Visitor & visitor )
2020-04-08 11:05:38 +02:00
{
2020-11-28 16:02:27 +01:00
Base : : visit_edges ( visitor ) ;
2020-04-08 11:05:38 +02:00
2020-04-18 13:56:13 +02:00
visitor . visit ( m_empty_object_shape ) ;
2020-10-17 23:13:37 +02:00
visitor . visit ( m_new_object_shape ) ;
2021-06-27 22:15:58 +02:00
visitor . visit ( m_new_ordinary_function_prototype_object_shape ) ;
2020-12-08 13:32:15 +01:00
visitor . visit ( m_proxy_constructor ) ;
2022-01-30 00:33:15 +02:00
visitor . visit ( m_async_from_sync_iterator_prototype ) ;
2022-05-05 08:47:36 +02:00
visitor . visit ( m_async_generator_prototype ) ;
visitor . visit ( m_generator_prototype ) ;
2022-01-30 01:08:42 +02:00
visitor . visit ( m_intl_segments_prototype ) ;
2021-07-25 19:37:42 +01:00
visitor . visit ( m_array_prototype_values_function ) ;
2021-12-04 18:56:44 -05:00
visitor . visit ( m_date_constructor_now_function ) ;
2021-07-25 19:37:42 +01:00
visitor . visit ( m_eval_function ) ;
2022-01-27 14:49:55 +01:00
visitor . visit ( m_json_parse_function ) ;
2022-04-11 22:47:50 +01:00
visitor . visit ( m_object_prototype_to_string_function ) ;
visitor . visit ( m_throw_type_error_function ) ;
2020-04-18 13:56:13 +02:00
2020-12-01 21:05:25 +01:00
# define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \
2021-02-05 14:51:18 +01:00
visitor . visit ( m_ # # snake_name # # _constructor ) ; \
visitor . visit ( m_ # # snake_name # # _prototype ) ;
JS_ENUMERATE_BUILTIN_TYPES
2020-04-18 11:02:05 +02:00
# undef __JS_ENUMERATE
2021-07-06 20:39:55 +01:00
2021-08-08 18:35:29 +01:00
# define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \
visitor . visit ( m_intl_ # # snake_name # # _constructor ) ; \
visitor . visit ( m_intl_ # # snake_name # # _prototype ) ;
JS_ENUMERATE_INTL_OBJECTS
# undef __JS_ENUMERATE
2021-07-06 20:39:55 +01:00
# define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName) \
visitor . visit ( m_temporal_ # # snake_name # # _constructor ) ; \
visitor . visit ( m_temporal_ # # snake_name # # _prototype ) ;
JS_ENUMERATE_TEMPORAL_OBJECTS
# undef __JS_ENUMERATE
2020-09-08 15:37:39 +02:00
# define __JS_ENUMERATE(ClassName, snake_name) \
visitor . visit ( m_ # # snake_name # # _prototype ) ;
JS_ENUMERATE_ITERATOR_PROTOTYPES
# undef __JS_ENUMERATE
2020-04-08 11:05:38 +02:00
}
2021-10-14 12:04:10 +01:00
Realm * GlobalObject : : associated_realm ( )
{
return m_associated_realm ;
}
2022-08-01 20:27:20 +02:00
void GlobalObject : : set_associated_realm ( Realm & realm )
2021-10-14 12:04:10 +01:00
{
2022-08-01 20:27:20 +02:00
VERIFY ( & realm = = & shape ( ) . realm ( ) ) ;
2021-10-14 12:04:10 +01:00
m_associated_realm = & realm ;
}
2021-10-29 17:04:01 -04:00
JS_DEFINE_NATIVE_FUNCTION ( GlobalObject : : gc )
2020-03-28 23:10:37 +01:00
{
2021-06-12 01:09:45 +01:00
# ifdef __serenity__
2020-12-06 16:55:19 +00:00
dbgln ( " Forced garbage collection requested! " ) ;
2021-06-12 01:09:45 +01:00
# endif
2020-09-27 18:36:49 +02:00
vm . heap ( ) . collect_garbage ( ) ;
2020-03-28 23:10:37 +01:00
return js_undefined ( ) ;
}
2021-06-13 00:22:35 +01:00
// 19.2.3 isNaN ( number ), https://tc39.es/ecma262/#sec-isnan-number
2021-10-29 17:04:01 -04:00
JS_DEFINE_NATIVE_FUNCTION ( GlobalObject : : is_nan )
2020-03-28 23:10:37 +01:00
{
2021-10-29 17:04:01 -04:00
return Value ( TRY ( vm . argument ( 0 ) . to_number ( global_object ) ) . is_nan ( ) ) ;
2020-03-28 23:10:37 +01:00
}
2021-06-13 00:22:35 +01:00
// 19.2.2 isFinite ( number ), https://tc39.es/ecma262/#sec-isfinite-number
2021-10-29 17:04:01 -04:00
JS_DEFINE_NATIVE_FUNCTION ( GlobalObject : : is_finite )
2020-04-22 18:07:24 +01:00
{
2021-10-29 17:04:01 -04:00
return Value ( TRY ( vm . argument ( 0 ) . to_number ( global_object ) ) . is_finite_number ( ) ) ;
2020-04-22 18:07:24 +01:00
}
2021-06-13 00:22:35 +01:00
// 19.2.4 parseFloat ( string ), https://tc39.es/ecma262/#sec-parsefloat-string
2021-10-29 17:04:01 -04:00
JS_DEFINE_NATIVE_FUNCTION ( GlobalObject : : parse_float )
2020-05-17 15:12:34 +01:00
{
2020-09-27 18:36:49 +02:00
if ( vm . argument ( 0 ) . is_number ( ) )
return vm . argument ( 0 ) ;
2021-10-29 17:04:01 -04:00
auto input_string = TRY ( vm . argument ( 0 ) . to_string ( global_object ) ) ;
2022-01-16 20:37:22 +02:00
auto trimmed_string = MUST ( trim_string ( global_object , js_string ( vm , input_string ) , TrimMode : : Left ) ) ;
2021-06-06 02:32:14 +03:00
for ( size_t length = trimmed_string . length ( ) ; length > 0 ; - - length ) {
2021-10-17 23:20:05 +03:00
auto number = MUST ( Value ( js_string ( vm , trimmed_string . substring ( 0 , length ) ) ) . to_number ( global_object ) ) ;
2020-05-17 15:12:34 +01:00
if ( ! number . is_nan ( ) )
return number ;
}
return js_nan ( ) ;
}
2021-06-13 00:22:35 +01:00
// 19.2.5 parseInt ( string, radix ), https://tc39.es/ecma262/#sec-parseint-string-radix
2021-10-29 17:04:01 -04:00
JS_DEFINE_NATIVE_FUNCTION ( GlobalObject : : parse_int )
2020-12-05 13:51:09 +01:00
{
2022-01-16 20:35:26 +02:00
// 1. Let inputString be ? ToString(string).
2021-10-29 17:04:01 -04:00
auto input_string = TRY ( vm . argument ( 0 ) . to_string ( global_object ) ) ;
2020-12-05 13:51:09 +01:00
2022-01-16 20:35:26 +02:00
// 2. Let S be ! TrimString(inputString, start).
auto string = MUST ( trim_string ( global_object , js_string ( vm , input_string ) , TrimMode : : Left ) ) ;
// 3. Let sign be 1.
auto sign = 1 ;
// 4. If S is not empty and the first code unit of S is the code unit 0x002D (HYPHEN-MINUS), set sign to -1.
if ( ! string . is_empty ( ) & & string [ 0 ] = = 0x2D )
2020-12-05 13:51:09 +01:00
sign = - 1 ;
2022-01-16 20:35:26 +02:00
// 5. If S is not empty and the first code unit of S is the code unit 0x002B (PLUS SIGN) or the code unit 0x002D (HYPHEN-MINUS), remove the first code unit from S.
auto trimmed_view = string . view ( ) ;
if ( ! string . is_empty ( ) & & ( string [ 0 ] = = 0x2B | | string [ 0 ] = = 0x2D ) )
trimmed_view = trimmed_view . substring_view ( 1 ) ;
2020-12-05 13:51:09 +01:00
2022-01-16 20:35:26 +02:00
// 6. Let R be ℝ (? ToInt32(radix)).
2021-10-29 17:04:01 -04:00
auto radix = TRY ( vm . argument ( 1 ) . to_i32 ( global_object ) ) ;
2020-12-05 13:51:09 +01:00
2022-01-16 20:35:26 +02:00
// 7. Let stripPrefix be true.
auto strip_prefix = true ;
// 8. If R ≠ 0, then
2020-12-05 13:51:09 +01:00
if ( radix ! = 0 ) {
2022-01-16 20:35:26 +02:00
// a. If R < 2 or R > 36, return NaN.
2020-12-05 13:51:09 +01:00
if ( radix < 2 | | radix > 36 )
return js_nan ( ) ;
2022-01-16 20:35:26 +02:00
// b. If R ≠ 16, set stripPrefix to false.
2020-12-05 13:51:09 +01:00
if ( radix ! = 16 )
strip_prefix = false ;
2022-01-16 20:35:26 +02:00
}
// 9. Else,
else {
// a. Set R to 10.
2020-12-05 13:51:09 +01:00
radix = 10 ;
}
2022-01-16 20:35:26 +02:00
// 10. If stripPrefix is true, then
2020-12-05 13:51:09 +01:00
if ( strip_prefix ) {
2022-01-16 20:35:26 +02:00
// a. If the length of S is at least 2 and the first two code units of S are either "0x" or "0X", then
if ( trimmed_view . length ( ) > = 2 & & trimmed_view . substring_view ( 0 , 2 ) . equals_ignoring_case ( " 0x " sv ) ) {
// i. Remove the first two code units from S.
trimmed_view = trimmed_view . substring_view ( 2 ) ;
// ii. Set R to 16.
2020-12-05 13:51:09 +01:00
radix = 16 ;
}
}
2022-01-16 20:35:26 +02:00
// 11. If S contains a code unit that is not a radix-R digit, let end be the index within S of the first such code unit; otherwise, let end be the length of S.
// 12. Let Z be the substring of S from 0 to end.
// 13. If Z is empty, return NaN.
// 14. Let mathInt be the integer value that is represented by Z in radix-R notation, using the letters A-Z and a-z for digits with values 10 through 35. (However, if R is 10 and Z contains more than 20 significant digits, every significant digit after the 20th may be replaced by a 0 digit, at the option of the implementation; and if R is not 2, 4, 8, 10, 16, or 32, then mathInt may be an implementation-approximated integer representing the integer value denoted by Z in radix-R notation.)
auto parse_digit = [ & ] ( u32 code_point ) - > Optional < u32 > {
if ( ! is_ascii_alphanumeric ( code_point ) )
2021-06-01 21:18:08 +02:00
return { } ;
2021-06-06 02:29:29 +03:00
auto digit = parse_ascii_base36_digit ( code_point ) ;
2021-06-01 21:18:08 +02:00
if ( digit > = ( u32 ) radix )
2020-12-05 13:51:09 +01:00
return { } ;
return digit ;
} ;
bool had_digits = false ;
double number = 0 ;
2022-01-16 20:35:26 +02:00
for ( auto code_point : Utf8View ( trimmed_view ) ) {
auto digit = parse_digit ( code_point ) ;
2020-12-05 13:51:09 +01:00
if ( ! digit . has_value ( ) )
break ;
had_digits = true ;
number * = radix ;
number + = digit . value ( ) ;
}
if ( ! had_digits )
return js_nan ( ) ;
2022-01-16 20:35:26 +02:00
// 15. If mathInt = 0, then
// a. If sign = -1, return -0𝔽 .
// b. Return +0𝔽 .
// 16. Return 𝔽 (sign × mathInt).
2020-12-05 13:51:09 +01:00
return Value ( sign * number ) ;
}
2021-06-13 00:22:35 +01:00
// 19.2.1 eval ( x ), https://tc39.es/ecma262/#sec-eval-x
2021-10-29 17:04:01 -04:00
JS_DEFINE_NATIVE_FUNCTION ( GlobalObject : : eval )
2021-03-14 16:20:01 +01:00
{
2022-04-22 19:10:27 +01:00
return perform_eval ( global_object , vm . argument ( 0 ) , CallerMode : : NonStrict , EvalMode : : Indirect ) ;
2021-03-14 16:20:01 +01:00
}
2021-06-13 00:22:35 +01:00
// 19.2.6.1.1 Encode ( string, unescapedSet ), https://tc39.es/ecma262/#sec-encode
2022-04-03 15:19:33 +01:00
static ThrowCompletionOr < String > encode ( GlobalObject & global_object , String const & string , StringView unescaped_set )
2021-04-13 22:50:29 +03:00
{
2022-01-16 19:06:03 +02:00
auto & vm = global_object . vm ( ) ;
auto utf16_string = Utf16String ( string ) ;
2022-05-03 21:37:01 +02:00
// 1. Let strLen be the length of string.
2022-01-16 19:06:03 +02:00
auto string_length = utf16_string . length_in_code_units ( ) ;
// 2. Let R be the empty String.
2021-04-13 22:50:29 +03:00
StringBuilder encoded_builder ;
2022-01-16 19:06:03 +02:00
// 3. Let k be 0.
auto k = 0u ;
// 4. Repeat,
while ( k < string_length ) {
// a. If k = strLen, return R.
// Handled below
// b. Let C be the code unit at index k within string.
auto code_unit = utf16_string . code_unit_at ( k ) ;
// c. If C is in unescapedSet, then
// NOTE: We assume the unescaped set only contains ascii characters as unescaped_set is a StringView.
if ( code_unit < 0x80 & & unescaped_set . contains ( code_unit ) ) {
// i. Set k to k + 1.
k + + ;
// ii. Set R to the string-concatenation of R and C.
2021-04-13 22:50:29 +03:00
encoded_builder . append ( code_unit ) ;
}
2022-01-16 19:06:03 +02:00
// d. Else,
else {
2022-05-02 20:54:39 +02:00
// i. Let cp be CodePointAt(string, k).
2022-01-16 19:06:03 +02:00
auto code_point = code_point_at ( utf16_string . view ( ) , k ) ;
// ii. If cp.[[IsUnpairedSurrogate]] is true, throw a URIError exception.
if ( code_point . is_unpaired_surrogate )
return vm . throw_completion < URIError > ( global_object , ErrorType : : URIMalformed ) ;
// iii. Set k to k + cp.[[CodeUnitCount]].
k + = code_point . code_unit_count ;
// iv. Let Octets be the List of octets resulting by applying the UTF-8 transformation to cp.[[CodePoint]].
// v. For each element octet of Octets, do
auto nwritten = AK : : UnicodeUtils : : code_point_to_utf8 ( code_point . code_point , [ & encoded_builder ] ( u8 octet ) {
// 1. Set R to the string-concatenation of:
// * R
// * "%"
// * the String representation of octet, formatted as a two-digit uppercase hexadecimal number, padded to the left with a zero if necessary
encoded_builder . appendff ( " %{:02X} " , octet ) ;
} ) ;
VERIFY ( nwritten > 0 ) ;
}
2021-04-13 22:50:29 +03:00
}
return encoded_builder . build ( ) ;
}
2021-06-13 00:22:35 +01:00
// 19.2.6.1.2 Decode ( string, reservedSet ), https://tc39.es/ecma262/#sec-decode
2022-04-03 15:19:33 +01:00
static ThrowCompletionOr < String > decode ( GlobalObject & global_object , String const & string , StringView reserved_set )
2021-04-13 22:50:29 +03:00
{
StringBuilder decoded_builder ;
2022-01-16 19:04:34 +02:00
auto code_point_start_offset = 0u ;
2021-04-13 22:50:29 +03:00
auto expected_continuation_bytes = 0 ;
for ( size_t k = 0 ; k < string . length ( ) ; k + + ) {
auto code_unit = string [ k ] ;
if ( code_unit ! = ' % ' ) {
2021-10-29 17:02:06 -04:00
if ( expected_continuation_bytes > 0 )
return global_object . vm ( ) . throw_completion < URIError > ( global_object , ErrorType : : URIMalformed ) ;
2021-04-13 22:50:29 +03:00
decoded_builder . append ( code_unit ) ;
continue ;
}
2021-10-29 17:02:06 -04:00
if ( k + 2 > = string . length ( ) )
return global_object . vm ( ) . throw_completion < URIError > ( global_object , ErrorType : : URIMalformed ) ;
2021-04-13 22:50:29 +03:00
auto first_digit = decode_hex_digit ( string [ k + 1 ] ) ;
2021-10-29 17:02:06 -04:00
if ( first_digit > = 16 )
return global_object . vm ( ) . throw_completion < URIError > ( global_object , ErrorType : : URIMalformed ) ;
2021-04-13 22:50:29 +03:00
auto second_digit = decode_hex_digit ( string [ k + 2 ] ) ;
2021-10-29 17:02:06 -04:00
if ( second_digit > = 16 )
return global_object . vm ( ) . throw_completion < URIError > ( global_object , ErrorType : : URIMalformed ) ;
2022-01-16 19:04:34 +02:00
u8 decoded_code_unit = ( first_digit < < 4 ) | second_digit ;
2021-04-13 22:50:29 +03:00
k + = 2 ;
if ( expected_continuation_bytes > 0 ) {
decoded_builder . append ( decoded_code_unit ) ;
expected_continuation_bytes - - ;
2022-01-16 19:04:34 +02:00
if ( expected_continuation_bytes = = 0 & & ! Utf8View ( decoded_builder . string_view ( ) . substring_view ( code_point_start_offset ) ) . validate ( ) )
return global_object . vm ( ) . throw_completion < URIError > ( global_object , ErrorType : : URIMalformed ) ;
2021-04-13 22:50:29 +03:00
continue ;
}
2021-10-29 17:02:06 -04:00
2021-04-13 22:50:29 +03:00
if ( ( decoded_code_unit & 0x80 ) = = 0 ) {
if ( reserved_set . contains ( decoded_code_unit ) )
decoded_builder . append ( string . substring_view ( k - 2 , 3 ) ) ;
else
decoded_builder . append ( decoded_code_unit ) ;
continue ;
}
2021-10-29 17:02:06 -04:00
2022-01-16 19:04:34 +02:00
auto leading_ones = count_leading_zeroes_safe ( static_cast < u8 > ( ~ decoded_code_unit ) ) ;
2021-10-29 17:02:06 -04:00
if ( leading_ones = = 1 | | leading_ones > 4 )
return global_object . vm ( ) . throw_completion < URIError > ( global_object , ErrorType : : URIMalformed ) ;
2022-01-16 19:04:34 +02:00
code_point_start_offset = decoded_builder . length ( ) ;
2021-04-13 22:50:29 +03:00
decoded_builder . append ( decoded_code_unit ) ;
expected_continuation_bytes = leading_ones - 1 ;
}
2022-01-16 19:04:34 +02:00
if ( expected_continuation_bytes > 0 )
return global_object . vm ( ) . throw_completion < URIError > ( global_object , ErrorType : : URIMalformed ) ;
2021-04-13 22:50:29 +03:00
return decoded_builder . build ( ) ;
}
2021-06-13 00:22:35 +01:00
// 19.2.6.4 encodeURI ( uri ), https://tc39.es/ecma262/#sec-encodeuri-uri
2021-10-29 17:04:01 -04:00
JS_DEFINE_NATIVE_FUNCTION ( GlobalObject : : encode_uri )
2021-04-13 22:50:29 +03:00
{
2021-10-29 17:04:01 -04:00
auto uri_string = TRY ( vm . argument ( 0 ) . to_string ( global_object ) ) ;
auto encoded = TRY ( encode ( global_object , uri_string , " ;/?:@&=+$,abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.!~*'()# " sv ) ) ;
2021-04-13 22:50:29 +03:00
return js_string ( vm , move ( encoded ) ) ;
}
2021-06-13 00:22:35 +01:00
// 19.2.6.2 decodeURI ( encodedURI ), https://tc39.es/ecma262/#sec-decodeuri-encodeduri
2021-10-29 17:04:01 -04:00
JS_DEFINE_NATIVE_FUNCTION ( GlobalObject : : decode_uri )
2021-04-13 22:50:29 +03:00
{
2021-10-29 17:04:01 -04:00
auto uri_string = TRY ( vm . argument ( 0 ) . to_string ( global_object ) ) ;
auto decoded = TRY ( decode ( global_object , uri_string , " ;/?:@&=+$,# " sv ) ) ;
2021-04-13 22:50:29 +03:00
return js_string ( vm , move ( decoded ) ) ;
}
2021-06-13 00:22:35 +01:00
// 19.2.6.5 encodeURIComponent ( uriComponent ), https://tc39.es/ecma262/#sec-encodeuricomponent-uricomponent
2021-10-29 17:04:01 -04:00
JS_DEFINE_NATIVE_FUNCTION ( GlobalObject : : encode_uri_component )
2021-04-13 22:50:29 +03:00
{
2021-10-29 17:04:01 -04:00
auto uri_string = TRY ( vm . argument ( 0 ) . to_string ( global_object ) ) ;
auto encoded = TRY ( encode ( global_object , uri_string , " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.!~*'() " sv ) ) ;
2021-04-13 22:50:29 +03:00
return js_string ( vm , move ( encoded ) ) ;
}
2021-06-13 00:22:35 +01:00
// 19.2.6.3 decodeURIComponent ( encodedURIComponent ), https://tc39.es/ecma262/#sec-decodeuricomponent-encodeduricomponent
2021-10-29 17:04:01 -04:00
JS_DEFINE_NATIVE_FUNCTION ( GlobalObject : : decode_uri_component )
2021-04-13 22:50:29 +03:00
{
2021-10-29 17:04:01 -04:00
auto uri_string = TRY ( vm . argument ( 0 ) . to_string ( global_object ) ) ;
auto decoded = TRY ( decode ( global_object , uri_string , " " sv ) ) ;
2021-04-13 22:50:29 +03:00
return js_string ( vm , move ( decoded ) ) ;
}
2021-06-13 00:22:35 +01:00
// B.2.1.1 escape ( string ), https://tc39.es/ecma262/#sec-escape-string
2021-10-29 17:04:01 -04:00
JS_DEFINE_NATIVE_FUNCTION ( GlobalObject : : escape )
2021-06-05 19:21:15 +03:00
{
2021-10-29 17:04:01 -04:00
auto string = TRY ( vm . argument ( 0 ) . to_string ( global_object ) ) ;
2021-06-05 19:21:15 +03:00
StringBuilder escaped ;
2022-01-13 19:45:11 -05:00
for ( auto code_point : utf8_to_utf16 ( string ) ) {
2021-06-05 19:21:15 +03:00
if ( code_point < 256 ) {
if ( " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789@*_+-./ " sv . contains ( code_point ) )
escaped . append ( code_point ) ;
else
escaped . appendff ( " %{:02X} " , code_point ) ;
continue ;
}
2022-01-13 19:45:11 -05:00
escaped . appendff ( " %u{:04X} " , code_point ) ;
2021-06-05 19:21:15 +03:00
}
return js_string ( vm , escaped . build ( ) ) ;
}
2021-06-13 00:22:35 +01:00
// B.2.1.2 unescape ( string ), https://tc39.es/ecma262/#sec-unescape-string
2021-10-29 17:04:01 -04:00
JS_DEFINE_NATIVE_FUNCTION ( GlobalObject : : unescape )
2021-06-05 19:21:15 +03:00
{
2021-10-29 17:04:01 -04:00
auto string = TRY ( vm . argument ( 0 ) . to_string ( global_object ) ) ;
2021-06-05 19:21:15 +03:00
ssize_t length = string . length ( ) ;
StringBuilder unescaped ( length ) ;
for ( auto k = 0 ; k < length ; + + k ) {
u32 code_point = string [ k ] ;
if ( code_point = = ' % ' ) {
if ( k < = length - 6 & & string [ k + 1 ] = = ' u ' & & is_ascii_hex_digit ( string [ k + 2 ] ) & & is_ascii_hex_digit ( string [ k + 3 ] ) & & is_ascii_hex_digit ( string [ k + 4 ] ) & & is_ascii_hex_digit ( string [ k + 5 ] ) ) {
code_point = ( parse_ascii_hex_digit ( string [ k + 2 ] ) < < 12 ) | ( parse_ascii_hex_digit ( string [ k + 3 ] ) < < 8 ) | ( parse_ascii_hex_digit ( string [ k + 4 ] ) < < 4 ) | parse_ascii_hex_digit ( string [ k + 5 ] ) ;
k + = 5 ;
} else if ( k < = length - 3 & & is_ascii_hex_digit ( string [ k + 1 ] ) & & is_ascii_hex_digit ( string [ k + 2 ] ) ) {
code_point = ( parse_ascii_hex_digit ( string [ k + 1 ] ) < < 4 ) | parse_ascii_hex_digit ( string [ k + 2 ] ) ;
k + = 2 ;
}
}
unescaped . append_code_point ( code_point ) ;
}
return js_string ( vm , unescaped . build ( ) ) ;
}
2020-03-12 20:11:35 +01:00
}