2020-04-06 11:09:01 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
2021-06-11 17:54:42 +01:00
|
|
|
* Copyright (c) 2020-2021, 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-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>
|
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-09 20:39:22 +02:00
|
|
|
#include <LibJS/Runtime/AsyncFunctionConstructor.h>
|
|
|
|
#include <LibJS/Runtime/AsyncFunctionPrototype.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>
|
|
|
|
#include <LibJS/Runtime/GeneratorObjectPrototype.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-08-24 22:27:05 -04:00
|
|
|
#include <LibJS/Runtime/Intl/DisplayNamesConstructor.h>
|
|
|
|
#include <LibJS/Runtime/Intl/DisplayNamesPrototype.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>
|
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 {
|
|
|
|
|
2020-03-13 11:06:32 +01:00
|
|
|
GlobalObject::GlobalObject()
|
2021-06-22 17:16:08 +02:00
|
|
|
: Object(GlobalObjectTag::Tag)
|
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.
|
2020-10-17 23:47:07 +02:00
|
|
|
m_empty_object_shape = heap().allocate_without_global_object<Shape>(*this);
|
2020-06-20 15:40:48 +02:00
|
|
|
m_object_prototype = heap().allocate_without_global_object<ObjectPrototype>(*this);
|
|
|
|
m_function_prototype = heap().allocate_without_global_object<FunctionPrototype>(*this);
|
2020-04-18 13:18:06 +02:00
|
|
|
|
2020-10-17 23:47:07 +02:00
|
|
|
m_new_object_shape = vm.heap().allocate_without_global_object<Shape>(*this);
|
2020-10-17 23:13:37 +02:00
|
|
|
m_new_object_shape->set_prototype_without_transition(m_object_prototype);
|
|
|
|
|
2021-06-27 22:15:58 +02:00
|
|
|
m_new_ordinary_function_prototype_object_shape = vm.heap().allocate_without_global_object<Shape>(*this);
|
|
|
|
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
|
|
|
|
2021-08-28 17:12:14 +01:00
|
|
|
// Normally Heap::allocate() takes care of this, but these are allocated via allocate_without_global_object().
|
|
|
|
|
2020-07-22 17:50:18 +02:00
|
|
|
static_cast<FunctionPrototype*>(m_function_prototype)->initialize(*this);
|
2021-08-28 17:12:14 +01:00
|
|
|
|
2020-07-22 17:50:18 +02:00
|
|
|
static_cast<ObjectPrototype*>(m_object_prototype)->initialize(*this);
|
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.
|
|
|
|
m_error_prototype = heap().allocate<ErrorPrototype>(*this, *this);
|
|
|
|
|
2020-09-20 19:24:44 +02:00
|
|
|
#define __JS_ENUMERATE(ClassName, snake_name) \
|
|
|
|
if (!m_##snake_name##_prototype) \
|
2020-07-09 14:58:20 -07:00
|
|
|
m_##snake_name##_prototype = heap().allocate<ClassName##Prototype>(*this, *this);
|
|
|
|
JS_ENUMERATE_ITERATOR_PROTOTYPES
|
|
|
|
#undef __JS_ENUMERATE
|
|
|
|
|
2021-06-15 00:04:08 -07:00
|
|
|
// %GeneratorFunction.prototype.prototype% must be initialized separately as it has no
|
|
|
|
// companion constructor
|
|
|
|
m_generator_object_prototype = heap().allocate<GeneratorObjectPrototype>(*this, *this);
|
2021-10-01 02:43:57 +02:00
|
|
|
m_generator_object_prototype->define_direct_property(vm.names.constructor, m_generator_function_constructor, Attribute::Configurable);
|
2021-06-15 00:04:08 -07:00
|
|
|
|
|
|
|
#define __JS_ENUMERATE(ClassName, snake_name, PrototypeName, ConstructorName, ArrayType) \
|
|
|
|
if (!m_##snake_name##_prototype) \
|
|
|
|
m_##snake_name##_prototype = heap().allocate<PrototypeName>(*this, *this);
|
|
|
|
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) \
|
|
|
|
m_intl_##snake_name##_prototype = heap().allocate<Intl::PrototypeName>(*this, *this);
|
|
|
|
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) \
|
|
|
|
m_temporal_##snake_name##_prototype = heap().allocate<Temporal::PrototypeName>(*this, *this);
|
|
|
|
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%
|
|
|
|
m_throw_type_error_function = NativeFunction::create(global_object(), {}, [](VM& vm, GlobalObject& global_object) {
|
2021-10-19 22:53:27 +03:00
|
|
|
return vm.throw_completion<TypeError>(global_object, ErrorType::RestrictedFunctionPropertiesAccess);
|
2021-06-28 03:53:35 +03:00
|
|
|
});
|
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);
|
|
|
|
define_direct_property(vm.names.console, heap().allocate<ConsoleObject>(*this, *this), attr);
|
2021-07-10 22:53:54 -04:00
|
|
|
define_direct_property(vm.names.Atomics, heap().allocate<AtomicsObject>(*this, *this), attr);
|
2021-07-06 02:15:08 +03:00
|
|
|
define_direct_property(vm.names.Math, heap().allocate<MathObject>(*this, *this), attr);
|
|
|
|
define_direct_property(vm.names.JSON, heap().allocate<JSONObject>(*this, *this), attr);
|
|
|
|
define_direct_property(vm.names.Reflect, heap().allocate<ReflectObject>(*this, *this), attr);
|
2021-08-08 18:27:28 +01:00
|
|
|
define_direct_property(vm.names.Intl, heap().allocate<Intl::Intl>(*this, *this), attr);
|
2021-07-06 19:14:47 +01:00
|
|
|
define_direct_property(vm.names.Temporal, heap().allocate<Temporal::Temporal>(*this, *this), 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
|
|
|
|
|
|
|
// The generator constructor cannot be initialized with add_constructor as it has no global binding
|
|
|
|
m_generator_function_constructor = heap().allocate<GeneratorFunctionConstructor>(*this, *this);
|
|
|
|
// 27.3.3.1 GeneratorFunction.prototype.constructor, https://tc39.es/ecma262/#sec-generatorfunction.prototype.constructor
|
2021-10-01 02:43:57 +02:00
|
|
|
m_generator_function_prototype->define_direct_property(vm.names.constructor, m_generator_function_constructor, 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();
|
|
|
|
m_eval_function = &get_without_side_effects(vm.names.eval).as_function();
|
2020-03-12 20:11:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
GlobalObject::~GlobalObject()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
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);
|
2021-06-15 00:04:08 -07:00
|
|
|
visitor.visit(m_generator_object_prototype);
|
2021-07-25 19:37:42 +01:00
|
|
|
visitor.visit(m_array_prototype_values_function);
|
|
|
|
visitor.visit(m_eval_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;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GlobalObject::set_associated_realm(Badge<Realm>, Realm& realm)
|
|
|
|
{
|
|
|
|
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));
|
2021-06-06 02:32:14 +03:00
|
|
|
auto trimmed_string = input_string.trim_whitespace(TrimMode::Left);
|
|
|
|
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
|
|
|
{
|
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
|
|
|
|
|
|
|
// FIXME: There's a bunch of unnecessary string copying here.
|
|
|
|
double sign = 1;
|
|
|
|
auto s = input_string.trim_whitespace(TrimMode::Left);
|
|
|
|
if (!s.is_empty() && s[0] == '-')
|
|
|
|
sign = -1;
|
|
|
|
if (!s.is_empty() && (s[0] == '+' || s[0] == '-'))
|
|
|
|
s = s.substring(1, s.length() - 1);
|
|
|
|
|
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
|
|
|
|
|
|
|
bool strip_prefix = true;
|
|
|
|
if (radix != 0) {
|
|
|
|
if (radix < 2 || radix > 36)
|
|
|
|
return js_nan();
|
|
|
|
if (radix != 16)
|
|
|
|
strip_prefix = false;
|
|
|
|
} else {
|
|
|
|
radix = 10;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strip_prefix) {
|
|
|
|
if (s.length() >= 2 && s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) {
|
|
|
|
s = s.substring(2, s.length() - 2);
|
|
|
|
radix = 16;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-01 10:01:11 +02:00
|
|
|
auto parse_digit = [&](u32 code_point, i32 radix) -> Optional<i32> {
|
2021-06-06 02:29:29 +03:00
|
|
|
if (!is_ascii_alphanumeric(code_point) || radix <= 0)
|
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;
|
2021-06-01 10:01:11 +02:00
|
|
|
for (auto code_point : Utf8View(s)) {
|
|
|
|
auto digit = parse_digit(code_point, radix);
|
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();
|
|
|
|
|
|
|
|
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
|
|
|
{
|
2021-10-29 17:04:01 -04:00
|
|
|
return perform_eval(vm.argument(0), global_object, 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
|
2021-10-29 17:02:06 -04:00
|
|
|
static ThrowCompletionOr<String> encode([[maybe_unused]] JS::GlobalObject& global_object, const String& string, StringView unescaped_set)
|
2021-04-13 22:50:29 +03:00
|
|
|
{
|
|
|
|
StringBuilder encoded_builder;
|
|
|
|
for (unsigned char code_unit : string) {
|
|
|
|
if (unescaped_set.contains(code_unit)) {
|
|
|
|
encoded_builder.append(code_unit);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// FIXME: check for unpaired surrogates and throw URIError
|
|
|
|
encoded_builder.appendff("%{:02X}", code_unit);
|
|
|
|
}
|
|
|
|
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
|
2021-10-29 17:02:06 -04:00
|
|
|
static ThrowCompletionOr<String> decode(JS::GlobalObject& global_object, const String& string, StringView reserved_set)
|
2021-04-13 22:50:29 +03:00
|
|
|
{
|
|
|
|
StringBuilder decoded_builder;
|
|
|
|
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);
|
|
|
|
|
2021-04-13 22:50:29 +03:00
|
|
|
char decoded_code_unit = (first_digit << 4) | second_digit;
|
|
|
|
k += 2;
|
|
|
|
if (expected_continuation_bytes > 0) {
|
|
|
|
decoded_builder.append(decoded_code_unit);
|
|
|
|
expected_continuation_bytes--;
|
|
|
|
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
|
|
|
|
2021-04-13 22:50:29 +03:00
|
|
|
auto leading_ones = count_trailing_zeroes_32_safe(~decoded_code_unit) - 24;
|
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);
|
|
|
|
|
2021-04-13 22:50:29 +03:00
|
|
|
decoded_builder.append(decoded_code_unit);
|
|
|
|
expected_continuation_bytes = leading_ones - 1;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
for (auto code_point : Utf8View(string)) {
|
|
|
|
if (code_point < 256) {
|
|
|
|
if ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789@*_+-./"sv.contains(code_point))
|
|
|
|
escaped.append(code_point);
|
|
|
|
else
|
|
|
|
escaped.appendff("%{:02X}", code_point);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
escaped.appendff("%u{:04X}", code_point); // FIXME: Handle utf-16 surrogate pairs
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|