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>
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-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>
2020-12-02 20:49:31 +00:00
# include <LibJS/Runtime/ArrayBufferConstructor.h>
2020-04-04 22:28:21 +02:00
# include <LibJS/Runtime/ArrayConstructor.h>
2020-04-18 13:18:06 +02:00
# include <LibJS/Runtime/ArrayPrototype.h>
2021-11-09 20:39:22 +02:00
# include <LibJS/Runtime/AsyncFunctionConstructor.h>
2021-11-15 01:53:24 +01:00
# include <LibJS/Runtime/AsyncGeneratorFunctionConstructor.h>
2022-05-05 08:47:36 +02:00
# include <LibJS/Runtime/AsyncGeneratorPrototype.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>
2020-04-06 22:51:16 -05:00
# include <LibJS/Runtime/BooleanConstructor.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>
2020-03-30 00:21:56 +01:00
# include <LibJS/Runtime/DateConstructor.h>
2020-04-01 19:42:07 +01:00
# include <LibJS/Runtime/ErrorConstructor.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>
2021-06-15 00:04:08 -07:00
# include <LibJS/Runtime/GeneratorFunctionConstructor.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>
2021-11-18 10:30:31 -05:00
# include <LibJS/Runtime/Intl/DateTimeFormatConstructor.h>
2021-08-24 22:27:05 -04:00
# include <LibJS/Runtime/Intl/DisplayNamesConstructor.h>
2022-06-30 00:58:40 +03:00
# include <LibJS/Runtime/Intl/DurationFormatConstructor.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>
2021-09-02 08:32:43 -04:00
# include <LibJS/Runtime/Intl/LocaleConstructor.h>
2021-09-08 21:34:27 -04:00
# include <LibJS/Runtime/Intl/NumberFormatConstructor.h>
2022-01-28 12:56:04 -05:00
# include <LibJS/Runtime/Intl/PluralRulesConstructor.h>
2022-01-25 10:41:57 -05:00
# include <LibJS/Runtime/Intl/RelativeTimeFormatConstructor.h>
2022-01-29 23:47:29 +02:00
# include <LibJS/Runtime/Intl/SegmenterConstructor.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>
2020-03-21 17:52:12 +01:00
# include <LibJS/Runtime/MathObject.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/Object.h>
2020-03-28 17:23:54 +01:00
# include <LibJS/Runtime/ObjectConstructor.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>
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>
2021-06-09 00:08:47 +03:00
# include <LibJS/Runtime/SetConstructor.h>
2021-10-13 21:09:45 +01:00
# include <LibJS/Runtime/ShadowRealmConstructor.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-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>
2021-07-14 21:01:12 +01:00
# include <LibJS/Runtime/Temporal/CalendarConstructor.h>
2021-07-15 23:20:43 +01:00
# include <LibJS/Runtime/Temporal/DurationConstructor.h>
2021-07-07 17:41:37 +01:00
# include <LibJS/Runtime/Temporal/InstantConstructor.h>
2021-07-19 00:29:26 +03:00
# include <LibJS/Runtime/Temporal/PlainDateConstructor.h>
2021-07-22 19:47:07 +01:00
# include <LibJS/Runtime/Temporal/PlainDateTimeConstructor.h>
2021-08-14 23:54:24 +01:00
# include <LibJS/Runtime/Temporal/PlainMonthDayConstructor.h>
2021-07-28 18:36:52 +01:00
# include <LibJS/Runtime/Temporal/PlainTimeConstructor.h>
2021-08-07 22:40:32 +01:00
# include <LibJS/Runtime/Temporal/PlainYearMonthConstructor.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>
2021-08-01 17:20:11 +01:00
# include <LibJS/Runtime/Temporal/ZonedDateTimeConstructor.h>
2020-12-01 21:05:25 +01:00
# include <LibJS/Runtime/TypedArray.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>
2021-06-12 17:38:34 +03:00
# include <LibJS/Runtime/WeakRefConstructor.h>
2021-06-09 19:23:04 +03:00
# include <LibJS/Runtime/WeakSetConstructor.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-03-12 20:11:35 +01:00
{
2022-08-28 17:33:01 +01:00
ensure_shape_is_unique ( ) ;
Object : : set_prototype ( realm . intrinsics ( ) . object_prototype ( ) ) ;
2020-04-18 13:18:06 +02:00
}
2022-08-27 00:54:55 +01:00
// 9.3.4 SetDefaultGlobalBindings ( realmRec ), https://tc39.es/ecma262/#sec-setdefaultglobalbindings
2022-08-28 17:33:01 +01:00
Object & set_default_global_bindings ( Realm & realm )
2020-04-18 13:18:06 +02:00
{
2022-08-28 17:33:01 +01:00
auto & vm = realm . vm ( ) ;
2022-08-28 14:42:50 +01:00
2022-08-28 17:33:01 +01:00
// 1. Let global be realmRec.[[GlobalObject]].
auto & global = realm . global_object ( ) ;
2020-10-13 23:49:19 +02:00
2022-08-28 17:33:01 +01:00
// 2. For each property of the Global Object specified in clause 19, do
// a. Let name be the String value of the property name.
// b. Let desc be the fully populated data Property Descriptor for the property, containing the specified attributes for the property.
// For properties listed in 19.2, 19.3, or 19.4 the value of the [[Value]] attribute is the corresponding intrinsic object from realmRec.
// c. Perform ? DefinePropertyOrThrow(global, name, desc).
// NOTE: This function is infallible as we set properties directly; property clashes in global object construction are not expected.
2021-07-06 20:39:55 +01:00
2020-04-27 23:05:02 -07:00
u8 attr = Attribute : : Writable | Attribute : : Configurable ;
2022-08-27 00:54:55 +01:00
// 19.2 Function Properties of the Global Object, https://tc39.es/ecma262/#sec-function-properties-of-the-global-object
2022-08-28 17:33:01 +01:00
global . define_direct_property ( vm . names . eval , realm . intrinsics ( ) . eval_function ( ) , attr ) ;
global . define_direct_property ( vm . names . isFinite , realm . intrinsics ( ) . is_finite_function ( ) , attr ) ;
global . define_direct_property ( vm . names . isNaN , realm . intrinsics ( ) . is_nan_function ( ) , attr ) ;
global . define_direct_property ( vm . names . parseFloat , realm . intrinsics ( ) . parse_float_function ( ) , attr ) ;
global . define_direct_property ( vm . names . parseInt , realm . intrinsics ( ) . parse_int_function ( ) , attr ) ;
global . define_direct_property ( vm . names . decodeURI , realm . intrinsics ( ) . decode_uri_function ( ) , attr ) ;
global . define_direct_property ( vm . names . decodeURIComponent , realm . intrinsics ( ) . decode_uri_component_function ( ) , attr ) ;
global . define_direct_property ( vm . names . encodeURI , realm . intrinsics ( ) . encode_uri_function ( ) , attr ) ;
global . define_direct_property ( vm . names . encodeURIComponent , realm . intrinsics ( ) . encode_uri_component_function ( ) , attr ) ;
2020-10-13 23:49:19 +02:00
2022-08-27 00:54:55 +01:00
// 19.1 Value Properties of the Global Object, https://tc39.es/ecma262/#sec-value-properties-of-the-global-object
2022-08-28 17:33:01 +01:00
global . define_direct_property ( vm . names . globalThis , & global , attr ) ;
global . define_direct_property ( vm . names . Infinity , js_infinity ( ) , 0 ) ;
global . define_direct_property ( vm . names . NaN , js_nan ( ) , 0 ) ;
global . define_direct_property ( vm . names . undefined , js_undefined ( ) , 0 ) ;
2020-10-13 23:49:19 +02:00
2022-08-27 00:54:55 +01:00
// 19.3 Constructor Properties of the Global Object, https://tc39.es/ecma262/#sec-constructor-properties-of-the-global-object
2022-08-28 17:33:01 +01:00
global . define_direct_property ( vm . names . AggregateError , realm . intrinsics ( ) . aggregate_error_constructor ( ) , attr ) ;
global . define_direct_property ( vm . names . Array , realm . intrinsics ( ) . array_constructor ( ) , attr ) ;
global . define_direct_property ( vm . names . ArrayBuffer , realm . intrinsics ( ) . array_buffer_constructor ( ) , attr ) ;
global . define_direct_property ( vm . names . BigInt , realm . intrinsics ( ) . bigint_constructor ( ) , attr ) ;
global . define_direct_property ( vm . names . BigInt64Array , realm . intrinsics ( ) . big_int64_array_constructor ( ) , attr ) ;
global . define_direct_property ( vm . names . BigUint64Array , realm . intrinsics ( ) . big_uint64_array_constructor ( ) , attr ) ;
global . define_direct_property ( vm . names . Boolean , realm . intrinsics ( ) . boolean_constructor ( ) , attr ) ;
global . define_direct_property ( vm . names . DataView , realm . intrinsics ( ) . data_view_constructor ( ) , attr ) ;
global . define_direct_property ( vm . names . Date , realm . intrinsics ( ) . date_constructor ( ) , attr ) ;
global . define_direct_property ( vm . names . Error , realm . intrinsics ( ) . error_constructor ( ) , attr ) ;
global . define_direct_property ( vm . names . EvalError , realm . intrinsics ( ) . eval_error_constructor ( ) , attr ) ;
global . define_direct_property ( vm . names . FinalizationRegistry , realm . intrinsics ( ) . finalization_registry_constructor ( ) , attr ) ;
global . define_direct_property ( vm . names . Float32Array , realm . intrinsics ( ) . float32_array_constructor ( ) , attr ) ;
global . define_direct_property ( vm . names . Float64Array , realm . intrinsics ( ) . float64_array_constructor ( ) , attr ) ;
global . define_direct_property ( vm . names . Function , realm . intrinsics ( ) . function_constructor ( ) , attr ) ;
global . define_direct_property ( vm . names . Int8Array , realm . intrinsics ( ) . int8_array_constructor ( ) , attr ) ;
global . define_direct_property ( vm . names . Int16Array , realm . intrinsics ( ) . int16_array_constructor ( ) , attr ) ;
global . define_direct_property ( vm . names . Int32Array , realm . intrinsics ( ) . int32_array_constructor ( ) , attr ) ;
global . define_direct_property ( vm . names . Map , realm . intrinsics ( ) . map_constructor ( ) , attr ) ;
global . define_direct_property ( vm . names . Number , realm . intrinsics ( ) . number_constructor ( ) , attr ) ;
global . define_direct_property ( vm . names . Object , realm . intrinsics ( ) . object_constructor ( ) , attr ) ;
global . define_direct_property ( vm . names . Promise , realm . intrinsics ( ) . promise_constructor ( ) , attr ) ;
global . define_direct_property ( vm . names . Proxy , realm . intrinsics ( ) . proxy_constructor ( ) , attr ) ;
global . define_direct_property ( vm . names . RangeError , realm . intrinsics ( ) . range_error_constructor ( ) , attr ) ;
global . define_direct_property ( vm . names . ReferenceError , realm . intrinsics ( ) . reference_error_constructor ( ) , attr ) ;
global . define_direct_property ( vm . names . RegExp , realm . intrinsics ( ) . regexp_constructor ( ) , attr ) ;
global . define_direct_property ( vm . names . Set , realm . intrinsics ( ) . set_constructor ( ) , attr ) ;
global . define_direct_property ( vm . names . ShadowRealm , realm . intrinsics ( ) . shadow_realm_constructor ( ) , attr ) ;
global . define_direct_property ( vm . names . String , realm . intrinsics ( ) . string_constructor ( ) , attr ) ;
global . define_direct_property ( vm . names . Symbol , realm . intrinsics ( ) . symbol_constructor ( ) , attr ) ;
global . define_direct_property ( vm . names . SyntaxError , realm . intrinsics ( ) . syntax_error_constructor ( ) , attr ) ;
global . define_direct_property ( vm . names . TypeError , realm . intrinsics ( ) . type_error_constructor ( ) , attr ) ;
global . define_direct_property ( vm . names . Uint8Array , realm . intrinsics ( ) . uint8_array_constructor ( ) , attr ) ;
global . define_direct_property ( vm . names . Uint8ClampedArray , realm . intrinsics ( ) . uint8_clamped_array_constructor ( ) , attr ) ;
global . define_direct_property ( vm . names . Uint16Array , realm . intrinsics ( ) . uint16_array_constructor ( ) , attr ) ;
global . define_direct_property ( vm . names . Uint32Array , realm . intrinsics ( ) . uint32_array_constructor ( ) , attr ) ;
global . define_direct_property ( vm . names . URIError , realm . intrinsics ( ) . uri_error_constructor ( ) , attr ) ;
global . define_direct_property ( vm . names . WeakMap , realm . intrinsics ( ) . weak_map_constructor ( ) , attr ) ;
global . define_direct_property ( vm . names . WeakRef , realm . intrinsics ( ) . weak_ref_constructor ( ) , attr ) ;
global . define_direct_property ( vm . names . WeakSet , realm . intrinsics ( ) . weak_set_constructor ( ) , attr ) ;
2022-08-27 00:54:55 +01:00
// 19.4 Other Properties of the Global Object, https://tc39.es/ecma262/#sec-other-properties-of-the-global-object
2022-08-28 17:33:01 +01:00
global . define_direct_property ( vm . names . Atomics , vm . heap ( ) . allocate < AtomicsObject > ( realm , realm ) , attr ) ;
global . define_direct_property ( vm . names . Intl , vm . heap ( ) . allocate < Intl : : Intl > ( realm , realm ) , attr ) ;
global . define_direct_property ( vm . names . JSON , vm . heap ( ) . allocate < JSONObject > ( realm , realm ) , attr ) ;
global . define_direct_property ( vm . names . Math , vm . heap ( ) . allocate < MathObject > ( realm , realm ) , attr ) ;
global . define_direct_property ( vm . names . Reflect , vm . heap ( ) . allocate < ReflectObject > ( realm , realm ) , attr ) ;
global . define_direct_property ( vm . names . Temporal , vm . heap ( ) . allocate < Temporal : : Temporal > ( realm , realm ) , attr ) ;
2020-10-13 23:49:19 +02:00
2022-08-27 00:54:55 +01:00
// B.2.1 Additional Properties of the Global Object, https://tc39.es/ecma262/#sec-additional-properties-of-the-global-object
2022-08-28 17:33:01 +01:00
global . define_direct_property ( vm . names . escape , realm . intrinsics ( ) . escape_function ( ) , attr ) ;
global . define_direct_property ( vm . names . unescape , realm . intrinsics ( ) . unescape_function ( ) , attr ) ;
// Non-standard
global . define_direct_property ( vm . names . InternalError , realm . intrinsics ( ) . internal_error_constructor ( ) , attr ) ;
global . define_direct_property ( vm . names . console , realm . intrinsics ( ) . console_object ( ) , attr ) ;
// 3. Return global.
return global ;
}
void GlobalObject : : initialize ( Realm & realm )
{
Base : : initialize ( realm ) ;
auto & vm = this - > vm ( ) ;
2022-01-16 14:47:14 +01:00
2022-08-27 00:54:55 +01:00
// Non-standard
2022-08-28 17:33:01 +01:00
u8 attr = Attribute : : Writable | Attribute : : Configurable ;
2022-08-27 00:54:55 +01:00
define_native_function ( realm , vm . names . gc , gc , 0 , attr ) ;
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
2021-10-29 17:04:01 -04:00
JS_DEFINE_NATIVE_FUNCTION ( GlobalObject : : gc )
2020-03-28 23:10:37 +01:00
{
2022-10-09 15:23:23 -06:00
# ifdef AK_OS_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
{
2022-08-21 14:00:56 +01:00
return Value ( TRY ( vm . argument ( 0 ) . to_number ( vm ) ) . 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
{
2022-08-21 14:00:56 +01:00
return Value ( TRY ( vm . argument ( 0 ) . to_number ( vm ) ) . 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 ) ;
2022-08-21 14:00:56 +01:00
auto input_string = TRY ( vm . argument ( 0 ) . to_string ( vm ) ) ;
2022-08-21 18:02:29 +01:00
auto trimmed_string = MUST ( trim_string ( vm , 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 ) {
2022-08-21 14:00:56 +01:00
auto number = MUST ( Value ( js_string ( vm , trimmed_string . substring ( 0 , length ) ) ) . to_number ( vm ) ) ;
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).
2022-08-21 14:00:56 +01:00
auto input_string = TRY ( vm . argument ( 0 ) . to_string ( vm ) ) ;
2020-12-05 13:51:09 +01:00
2022-01-16 20:35:26 +02:00
// 2. Let S be ! TrimString(inputString, start).
2022-08-21 18:02:29 +01:00
auto string = MUST ( trim_string ( vm , js_string ( vm , input_string ) , TrimMode : : Left ) ) ;
2022-01-16 20:35:26 +02:00
// 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)).
2022-08-21 14:00:56 +01:00
auto radix = TRY ( vm . argument ( 1 ) . to_i32 ( vm ) ) ;
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-08-21 19:24:32 +01:00
return perform_eval ( vm , 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-08-21 20:38:35 +01:00
static ThrowCompletionOr < String > encode ( VM & vm , String const & string , StringView unescaped_set )
2021-04-13 22:50:29 +03:00
{
2022-01-16 19:06:03 +02:00
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.
2022-09-12 16:31:16 +02:00
if ( code_unit < 0x80 & & unescaped_set . contains ( static_cast < char > ( code_unit ) ) ) {
2022-01-16 19:06:03 +02:00
// 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 )
2022-08-16 20:33:17 +01:00
return vm . throw_completion < URIError > ( ErrorType : : URIMalformed ) ;
2022-01-16 19:06:03 +02:00
// 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-08-21 20:38:35 +01:00
static ThrowCompletionOr < String > decode ( VM & vm , 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 )
2022-08-21 20:38:35 +01:00
return vm . throw_completion < URIError > ( ErrorType : : URIMalformed ) ;
2021-10-29 17:02:06 -04:00
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 ( ) )
2022-08-21 20:38:35 +01:00
return vm . throw_completion < URIError > ( ErrorType : : URIMalformed ) ;
2021-10-29 17:02:06 -04:00
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 )
2022-08-21 20:38:35 +01:00
return vm . throw_completion < URIError > ( ErrorType : : URIMalformed ) ;
2021-10-29 17:02:06 -04:00
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 )
2022-08-21 20:38:35 +01:00
return vm . throw_completion < URIError > ( ErrorType : : URIMalformed ) ;
2021-10-29 17:02:06 -04:00
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 ( ) )
2022-08-21 20:38:35 +01:00
return vm . throw_completion < URIError > ( ErrorType : : URIMalformed ) ;
2021-04-13 22:50:29 +03:00
continue ;
}
2021-10-29 17:02:06 -04:00
2022-09-12 16:31:16 +02:00
if ( decoded_code_unit < 0x80 ) {
if ( reserved_set . contains ( static_cast < char > ( decoded_code_unit ) ) )
2021-04-13 22:50:29 +03:00
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 )
2022-08-21 20:38:35 +01:00
return vm . throw_completion < URIError > ( ErrorType : : URIMalformed ) ;
2021-10-29 17:02:06 -04:00
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 )
2022-08-21 20:38:35 +01:00
return vm . throw_completion < URIError > ( 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
{
2022-08-21 14:00:56 +01:00
auto uri_string = TRY ( vm . argument ( 0 ) . to_string ( vm ) ) ;
2022-08-21 20:38:35 +01:00
auto encoded = TRY ( encode ( vm , 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
{
2022-08-21 14:00:56 +01:00
auto uri_string = TRY ( vm . argument ( 0 ) . to_string ( vm ) ) ;
2022-08-21 20:38:35 +01:00
auto decoded = TRY ( decode ( vm , 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
{
2022-08-21 14:00:56 +01:00
auto uri_string = TRY ( vm . argument ( 0 ) . to_string ( vm ) ) ;
2022-08-21 20:38:35 +01:00
auto encoded = TRY ( encode ( vm , 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
{
2022-08-21 14:00:56 +01:00
auto uri_string = TRY ( vm . argument ( 0 ) . to_string ( vm ) ) ;
2022-08-21 20:38:35 +01:00
auto decoded = TRY ( decode ( vm , 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
{
2022-08-21 14:00:56 +01:00
auto string = TRY ( vm . argument ( 0 ) . to_string ( vm ) ) ;
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 ) {
2022-09-12 16:31:16 +02:00
if ( " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789@*_+-./ " sv . contains ( static_cast < char > ( code_point ) ) )
2021-06-05 19:21:15 +03:00
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
{
2022-08-21 14:00:56 +01:00
auto string = TRY ( vm . argument ( 0 ) . to_string ( vm ) ) ;
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
}