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>
|
|
|
|
#include <LibJS/Lexer.h>
|
|
|
|
#include <LibJS/Parser.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>
|
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-06-22 17:16:08 +02:00
|
|
|
#include <LibJS/Runtime/GlobalEnvironmentRecord.h>
|
2020-03-16 14:20:30 +01:00
|
|
|
#include <LibJS/Runtime/GlobalObject.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>
|
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-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>
|
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>
|
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
|
|
|
|
2021-06-26 17:05:29 +02:00
|
|
|
m_environment_record = heap().allocate<GlobalEnvironmentRecord>(*this, *this);
|
2021-06-22 17:16:08 +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
|
|
|
|
2020-07-22 17:50:18 +02:00
|
|
|
static_cast<FunctionPrototype*>(m_function_prototype)->initialize(*this);
|
|
|
|
static_cast<ObjectPrototype*>(m_object_prototype)->initialize(*this);
|
2020-04-18 13:18:06 +02:00
|
|
|
|
2021-06-09 00:08:47 +03: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);
|
|
|
|
m_generator_object_prototype->define_property(vm.names.constructor, m_generator_function_constructor, Attribute::Configurable);
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
2020-04-27 23:05:02 -07:00
|
|
|
u8 attr = Attribute::Writable | Attribute::Configurable;
|
2020-10-13 23:49:19 +02: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);
|
2021-06-06 02:27:31 +03:00
|
|
|
define_native_function(vm.names.parseInt, parse_int, 2, attr);
|
2021-03-14 16:20:01 +01:00
|
|
|
define_native_function(vm.names.eval, eval, 1, attr);
|
2021-06-19 20:13:53 -07:00
|
|
|
m_eval_function = &get_without_side_effects(vm.names.eval).as_function();
|
|
|
|
|
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) {
|
|
|
|
vm.throw_exception<TypeError>(global_object, ErrorType::RestrictedFunctionPropertiesAccess);
|
|
|
|
return Value();
|
|
|
|
});
|
|
|
|
m_throw_type_error_function->prevent_extensions();
|
|
|
|
m_throw_type_error_function->define_property_without_transition(vm.names.length, Value(0), 0, false);
|
|
|
|
m_throw_type_error_function->define_property_without_transition(vm.names.name, js_string(vm, ""), 0, false);
|
|
|
|
|
|
|
|
// 10.2.4 AddRestrictedFunctionProperties ( F, realm ), https://tc39.es/ecma262/#sec-addrestrictedfunctionproperties
|
|
|
|
m_function_prototype->define_accessor(vm.names.caller, throw_type_error_function(), throw_type_error_function(), Attribute::Configurable);
|
|
|
|
m_function_prototype->define_accessor(vm.names.arguments, throw_type_error_function(), throw_type_error_function(), Attribute::Configurable);
|
|
|
|
|
2021-04-13 22:50:29 +03: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);
|
2021-06-05 19:21:15 +03:00
|
|
|
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
|
|
|
|
|
|
|
define_property(vm.names.NaN, js_nan(), 0);
|
|
|
|
define_property(vm.names.Infinity, js_infinity(), 0);
|
|
|
|
define_property(vm.names.undefined, js_undefined(), 0);
|
|
|
|
|
|
|
|
define_property(vm.names.globalThis, this, attr);
|
|
|
|
define_property(vm.names.console, heap().allocate<ConsoleObject>(*this, *this), attr);
|
|
|
|
define_property(vm.names.Math, heap().allocate<MathObject>(*this, *this), attr);
|
|
|
|
define_property(vm.names.JSON, heap().allocate<JSONObject>(*this, *this), attr);
|
|
|
|
define_property(vm.names.Reflect, heap().allocate<ReflectObject>(*this, *this), attr);
|
|
|
|
|
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);
|
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
|
|
|
|
m_generator_function_prototype->define_property(vm.names.constructor, m_generator_function_constructor, Attribute::Configurable);
|
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-06-22 17:16:08 +02:00
|
|
|
visitor.visit(m_environment_record);
|
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);
|
2021-06-11 17:54:42 +01:00
|
|
|
JS_ENUMERATE_NATIVE_ERRORS
|
2021-02-05 14:51:18 +01:00
|
|
|
JS_ENUMERATE_BUILTIN_TYPES
|
2020-04-18 11:02:05 +02:00
|
|
|
#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
|
2021-06-19 20:13:53 -07:00
|
|
|
|
|
|
|
visitor.visit(m_eval_function);
|
2021-06-28 03:53:35 +03:00
|
|
|
visitor.visit(m_throw_type_error_function);
|
2020-04-08 11:05:38 +02:00
|
|
|
}
|
|
|
|
|
2020-06-20 13:55:34 +02: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
|
2020-06-20 13:55:34 +02:00
|
|
|
JS_DEFINE_NATIVE_FUNCTION(GlobalObject::is_nan)
|
2020-03-28 23:10:37 +01:00
|
|
|
{
|
2020-09-27 18:36:49 +02:00
|
|
|
auto number = vm.argument(0).to_number(global_object);
|
|
|
|
if (vm.exception())
|
2020-05-18 00:28:00 +01:00
|
|
|
return {};
|
|
|
|
return Value(number.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
|
2020-06-20 13:55:34 +02:00
|
|
|
JS_DEFINE_NATIVE_FUNCTION(GlobalObject::is_finite)
|
2020-04-22 18:07:24 +01:00
|
|
|
{
|
2020-09-27 18:36:49 +02:00
|
|
|
auto number = vm.argument(0).to_number(global_object);
|
|
|
|
if (vm.exception())
|
2020-05-18 00:28:00 +01:00
|
|
|
return {};
|
|
|
|
return Value(number.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
|
2020-06-20 13:55:34 +02: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-06-06 02:32:14 +03:00
|
|
|
auto input_string = vm.argument(0).to_string(global_object);
|
2020-09-27 18:36:49 +02:00
|
|
|
if (vm.exception())
|
2020-05-17 15:12:34 +01:00
|
|
|
return {};
|
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) {
|
2020-05-18 00:28:00 +01:00
|
|
|
// This can't throw, so no exception check is fine.
|
2021-06-06 02:32:14 +03:00
|
|
|
auto number = 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
|
2020-12-05 13:51:09 +01:00
|
|
|
JS_DEFINE_NATIVE_FUNCTION(GlobalObject::parse_int)
|
|
|
|
{
|
|
|
|
auto input_string = vm.argument(0).to_string(global_object);
|
|
|
|
if (vm.exception())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
auto radix = vm.argument(1).to_i32(global_object);
|
|
|
|
if (vm.exception())
|
|
|
|
return {};
|
|
|
|
|
|
|
|
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-03-14 16:20:01 +01:00
|
|
|
JS_DEFINE_NATIVE_FUNCTION(GlobalObject::eval)
|
|
|
|
{
|
2021-06-19 20:13:53 -07: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-04-13 22:50:29 +03:00
|
|
|
static String encode([[maybe_unused]] JS::GlobalObject& global_object, const String& string, StringView unescaped_set)
|
|
|
|
{
|
|
|
|
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-04-13 22:50:29 +03:00
|
|
|
static String decode(JS::GlobalObject& global_object, const String& string, StringView reserved_set)
|
|
|
|
{
|
|
|
|
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 != '%') {
|
|
|
|
if (expected_continuation_bytes > 0) {
|
|
|
|
global_object.vm().throw_exception<URIError>(global_object, ErrorType::URIMalformed);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
decoded_builder.append(code_unit);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (k + 2 >= string.length()) {
|
|
|
|
global_object.vm().throw_exception<URIError>(global_object, ErrorType::URIMalformed);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
auto first_digit = decode_hex_digit(string[k + 1]);
|
|
|
|
if (first_digit >= 16) {
|
|
|
|
global_object.vm().throw_exception<URIError>(global_object, ErrorType::URIMalformed);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
auto second_digit = decode_hex_digit(string[k + 2]);
|
|
|
|
if (second_digit >= 16) {
|
|
|
|
global_object.vm().throw_exception<URIError>(global_object, ErrorType::URIMalformed);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
auto leading_ones = count_trailing_zeroes_32_safe(~decoded_code_unit) - 24;
|
|
|
|
if (leading_ones == 1 || leading_ones > 4) {
|
|
|
|
global_object.vm().throw_exception<URIError>(global_object, ErrorType::URIMalformed);
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
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-04-13 22:50:29 +03:00
|
|
|
JS_DEFINE_NATIVE_FUNCTION(GlobalObject::encode_uri)
|
|
|
|
{
|
|
|
|
auto uri_string = vm.argument(0).to_string(global_object);
|
|
|
|
if (vm.exception())
|
|
|
|
return {};
|
|
|
|
auto encoded = encode(global_object, uri_string, ";/?:@&=+$,abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.!~*'()#"sv);
|
|
|
|
if (vm.exception())
|
|
|
|
return {};
|
|
|
|
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-04-13 22:50:29 +03:00
|
|
|
JS_DEFINE_NATIVE_FUNCTION(GlobalObject::decode_uri)
|
|
|
|
{
|
|
|
|
auto uri_string = vm.argument(0).to_string(global_object);
|
|
|
|
if (vm.exception())
|
|
|
|
return {};
|
|
|
|
auto decoded = decode(global_object, uri_string, ";/?:@&=+$,#"sv);
|
|
|
|
if (vm.exception())
|
|
|
|
return {};
|
|
|
|
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-04-13 22:50:29 +03:00
|
|
|
JS_DEFINE_NATIVE_FUNCTION(GlobalObject::encode_uri_component)
|
|
|
|
{
|
|
|
|
auto uri_string = vm.argument(0).to_string(global_object);
|
|
|
|
if (vm.exception())
|
|
|
|
return {};
|
|
|
|
auto encoded = encode(global_object, uri_string, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.!~*'()"sv);
|
|
|
|
if (vm.exception())
|
|
|
|
return {};
|
|
|
|
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-04-13 22:50:29 +03:00
|
|
|
JS_DEFINE_NATIVE_FUNCTION(GlobalObject::decode_uri_component)
|
|
|
|
{
|
|
|
|
auto uri_string = vm.argument(0).to_string(global_object);
|
|
|
|
if (vm.exception())
|
|
|
|
return {};
|
|
|
|
auto decoded = decode(global_object, uri_string, ""sv);
|
|
|
|
if (vm.exception())
|
|
|
|
return {};
|
|
|
|
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-06-05 19:21:15 +03:00
|
|
|
JS_DEFINE_NATIVE_FUNCTION(GlobalObject::escape)
|
|
|
|
{
|
|
|
|
auto string = vm.argument(0).to_string(global_object);
|
|
|
|
if (vm.exception())
|
|
|
|
return {};
|
|
|
|
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-06-05 19:21:15 +03:00
|
|
|
JS_DEFINE_NATIVE_FUNCTION(GlobalObject::unescape)
|
|
|
|
{
|
|
|
|
auto string = vm.argument(0).to_string(global_object);
|
|
|
|
if (vm.exception())
|
|
|
|
return {};
|
|
|
|
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
|
|
|
}
|