2020-04-01 18:53:28 +02:00
|
|
|
/*
|
2021-01-18 09:50:00 +01:00
|
|
|
* Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
|
2020-04-01 18:53:28 +02:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-04-01 18:53:28 +02:00
|
|
|
*/
|
|
|
|
|
|
2020-06-20 16:44:27 +02:00
|
|
|
#include <AK/Base64.h>
|
|
|
|
|
#include <AK/String.h>
|
2020-07-27 14:16:33 +02:00
|
|
|
#include <AK/Utf8View.h>
|
2020-04-01 18:53:28 +02:00
|
|
|
#include <LibJS/Runtime/Error.h>
|
2021-06-27 21:48:34 +02:00
|
|
|
#include <LibJS/Runtime/FunctionObject.h>
|
2020-04-27 23:05:02 -07:00
|
|
|
#include <LibJS/Runtime/Shape.h>
|
2020-07-22 11:39:39 -04:00
|
|
|
#include <LibTextCodec/Decoder.h>
|
2020-04-01 18:53:28 +02:00
|
|
|
#include <LibWeb/Bindings/DocumentWrapper.h>
|
2021-01-18 09:50:00 +01:00
|
|
|
#include <LibWeb/Bindings/EventTargetConstructor.h>
|
|
|
|
|
#include <LibWeb/Bindings/EventTargetPrototype.h>
|
2020-11-21 18:32:39 +00:00
|
|
|
#include <LibWeb/Bindings/EventWrapper.h>
|
2020-11-27 13:54:58 +01:00
|
|
|
#include <LibWeb/Bindings/EventWrapperFactory.h>
|
2020-05-18 21:42:40 +02:00
|
|
|
#include <LibWeb/Bindings/LocationObject.h>
|
2020-04-03 18:10:20 +02:00
|
|
|
#include <LibWeb/Bindings/NavigatorObject.h>
|
2020-06-20 22:09:38 +02:00
|
|
|
#include <LibWeb/Bindings/NodeWrapperFactory.h>
|
2020-09-29 18:19:18 +02:00
|
|
|
#include <LibWeb/Bindings/PerformanceWrapper.h>
|
2021-04-04 00:14:39 +02:00
|
|
|
#include <LibWeb/Bindings/ScreenWrapper.h>
|
2020-04-01 18:53:28 +02:00
|
|
|
#include <LibWeb/Bindings/WindowObject.h>
|
|
|
|
|
#include <LibWeb/DOM/Document.h>
|
2020-11-21 18:32:39 +00:00
|
|
|
#include <LibWeb/DOM/Event.h>
|
2020-04-01 18:53:28 +02:00
|
|
|
#include <LibWeb/DOM/Window.h>
|
2020-09-22 18:24:49 +02:00
|
|
|
#include <LibWeb/Origin.h>
|
2021-05-30 12:36:53 +02:00
|
|
|
#include <LibWeb/Page/BrowsingContext.h>
|
2021-05-17 00:16:44 +04:30
|
|
|
#include <LibWeb/WebAssembly/WebAssemblyObject.h>
|
2020-04-01 18:53:28 +02:00
|
|
|
|
2021-01-18 09:50:00 +01:00
|
|
|
#include <LibWeb/Bindings/WindowObjectHelper.h>
|
|
|
|
|
|
|
|
|
|
namespace Web::Bindings {
|
2020-04-01 18:53:28 +02:00
|
|
|
|
2020-07-26 19:37:56 +02:00
|
|
|
WindowObject::WindowObject(DOM::Window& impl)
|
2020-04-01 18:53:28 +02:00
|
|
|
: m_impl(impl)
|
|
|
|
|
{
|
2020-06-20 17:45:27 +02:00
|
|
|
impl.set_wrapper({}, *this);
|
2020-04-18 13:18:06 +02:00
|
|
|
}
|
|
|
|
|
|
2021-03-17 16:52:26 +01:00
|
|
|
void WindowObject::initialize_global_object()
|
2020-04-18 13:18:06 +02:00
|
|
|
{
|
2021-03-17 16:52:26 +01:00
|
|
|
Base::initialize_global_object();
|
2020-04-18 13:18:06 +02:00
|
|
|
|
LibJS: Rewrite most of Object for spec compliance :^)
This is a huge patch, I know. In hindsight this perhaps could've been
done slightly more incremental, but I started and then fixed everything
until it worked, and here we are. I tried splitting of some completely
unrelated changes into separate commits, however. Anyway.
This is a rewrite of most of Object, and by extension large parts of
Array, Proxy, Reflect, String, TypedArray, and some other things.
What we already had worked fine for about 90% of things, but getting the
last 10% right proved to be increasingly difficult with the current code
that sort of grew organically and is only very loosely based on the
spec - this became especially obvious when we started fixing a large
number of test262 failures.
Key changes include:
- 1:1 matching function names and parameters of all object-related
functions, to avoid ambiguity. Previously we had things like put(),
which the spec doesn't have - as a result it wasn't always clear which
need to be used.
- Better separation between object abstract operations and internal
methods - the former are always the same, the latter can be overridden
(and are therefore virtual). The internal methods (i.e. [[Foo]] in the
spec) are now prefixed with 'internal_' for clarity - again, it was
previously not always clear which AO a certain method represents,
get() could've been both Get and [[Get]] (I don't know which one it
was closer to right now).
Note that some of the old names have been kept until all code relying
on them is updated, but they are now simple wrappers around the
closest matching standard abstract operation.
- Simplifications of the storage layer: functions that write values to
storage are now prefixed with 'storage_' to make their purpose clear,
and as they are not part of the spec they should not contain any steps
specified by it. Much functionality is now covered by the layers above
it and was removed (e.g. handling of accessors, attribute checks).
- PropertyAttributes has been greatly simplified, and is being replaced
by PropertyDescriptor - a concept similar to the current
implementation, but more aligned with the actual spec. See the commit
message of the previous commit where it was introduced for details.
- As a bonus, and since I had to look at the spec a whole lot anyway, I
introduced more inline comments with the exact steps from the spec -
this makes it super easy to verify correctness.
- East-const all the things.
As a result of all of this, things are much more correct but a bit
slower now. Retaining speed wasn't a consideration at all, I have done
no profiling of the new code - there might be low hanging fruits, which
we can then harvest separately.
Special thanks to Idan for helping me with this by tracking down bugs,
updating everything outside of LibJS to work with these changes (LibWeb,
Spreadsheet, HackStudio), as well as providing countless patches to fix
regressions I introduced - there still are very few (we got it down to
5), but we also get many new passing test262 tests in return. :^)
Co-authored-by: Idan Horowitz <idan.horowitz@gmail.com>
2021-07-04 18:14:16 +01:00
|
|
|
auto success = Object::internal_set_prototype_of(&ensure_web_prototype<EventTargetPrototype>("EventTarget"));
|
|
|
|
|
VERIFY(success);
|
2021-01-18 12:15:02 +01:00
|
|
|
|
LibJS: Simplify and normalize publicly-exposed Object functions
Previously, the Object class had many different types of functions for
each action. For example: get_by_index, get(PropertyName),
get(FlyString). This is a bit verbose, so these methods have been
shortened to simply use the PropertyName structure. The methods then
internally call _by_index if necessary. Note that the _by_index
have been made private to enforce this change.
Secondly, a clear distinction has been made between "putting" and
"defining" an object property. "Putting" should mean modifying a
(potentially) already existing property. This is akin to doing "a.b =
'foo'".
This implies two things about put operations:
- They will search the prototype chain for setters and call them, if
necessary.
- If no property exists with a particular key, the put operation
should create a new property with the default attributes
(configurable, writable, and enumerable).
In contrast, "defining" a property should completely overwrite any
existing value without calling setters (if that property is
configurable, of course).
Thus, all of the many JS objects have had any "put" calls changed to
"define_property" calls. Additionally, "put_native_function" and
"put_native_property" have had their "put" replaced with "define".
Finally, "put_own_property" has been made private, as all necessary
functionality should be exposed with the put and define_property
methods.
2020-05-26 21:33:37 -07:00
|
|
|
define_property("window", this, JS::Attribute::Enumerable);
|
2020-08-31 14:08:10 +01:00
|
|
|
define_property("frames", this, JS::Attribute::Enumerable);
|
|
|
|
|
define_property("self", this, JS::Attribute::Enumerable);
|
2021-04-11 22:34:57 +01:00
|
|
|
define_native_property("top", top_getter, nullptr, JS::Attribute::Enumerable);
|
|
|
|
|
define_native_property("parent", parent_getter, nullptr, JS::Attribute::Enumerable);
|
2021-04-04 00:49:19 +01:00
|
|
|
define_native_property("document", document_getter, nullptr, JS::Attribute::Enumerable);
|
2020-09-29 18:19:18 +02:00
|
|
|
define_native_property("performance", performance_getter, nullptr, JS::Attribute::Enumerable);
|
2021-04-04 00:14:39 +02:00
|
|
|
define_native_property("screen", screen_getter, nullptr, JS::Attribute::Enumerable);
|
2021-03-16 17:22:01 +01:00
|
|
|
define_native_property("innerWidth", inner_width_getter, nullptr, JS::Attribute::Enumerable);
|
|
|
|
|
define_native_property("innerHeight", inner_height_getter, nullptr, JS::Attribute::Enumerable);
|
LibJS: Simplify and normalize publicly-exposed Object functions
Previously, the Object class had many different types of functions for
each action. For example: get_by_index, get(PropertyName),
get(FlyString). This is a bit verbose, so these methods have been
shortened to simply use the PropertyName structure. The methods then
internally call _by_index if necessary. Note that the _by_index
have been made private to enforce this change.
Secondly, a clear distinction has been made between "putting" and
"defining" an object property. "Putting" should mean modifying a
(potentially) already existing property. This is akin to doing "a.b =
'foo'".
This implies two things about put operations:
- They will search the prototype chain for setters and call them, if
necessary.
- If no property exists with a particular key, the put operation
should create a new property with the default attributes
(configurable, writable, and enumerable).
In contrast, "defining" a property should completely overwrite any
existing value without calling setters (if that property is
configurable, of course).
Thus, all of the many JS objects have had any "put" calls changed to
"define_property" calls. Additionally, "put_native_function" and
"put_native_property" have had their "put" replaced with "define".
Finally, "put_own_property" has been made private, as all necessary
functionality should be exposed with the put and define_property
methods.
2020-05-26 21:33:37 -07:00
|
|
|
define_native_function("alert", alert);
|
|
|
|
|
define_native_function("confirm", confirm);
|
2021-02-20 12:05:18 +01:00
|
|
|
define_native_function("prompt", prompt);
|
LibJS: Simplify and normalize publicly-exposed Object functions
Previously, the Object class had many different types of functions for
each action. For example: get_by_index, get(PropertyName),
get(FlyString). This is a bit verbose, so these methods have been
shortened to simply use the PropertyName structure. The methods then
internally call _by_index if necessary. Note that the _by_index
have been made private to enforce this change.
Secondly, a clear distinction has been made between "putting" and
"defining" an object property. "Putting" should mean modifying a
(potentially) already existing property. This is akin to doing "a.b =
'foo'".
This implies two things about put operations:
- They will search the prototype chain for setters and call them, if
necessary.
- If no property exists with a particular key, the put operation
should create a new property with the default attributes
(configurable, writable, and enumerable).
In contrast, "defining" a property should completely overwrite any
existing value without calling setters (if that property is
configurable, of course).
Thus, all of the many JS objects have had any "put" calls changed to
"define_property" calls. Additionally, "put_native_function" and
"put_native_property" have had their "put" replaced with "define".
Finally, "put_own_property" has been made private, as all necessary
functionality should be exposed with the put and define_property
methods.
2020-05-26 21:33:37 -07:00
|
|
|
define_native_function("setInterval", set_interval, 1);
|
|
|
|
|
define_native_function("setTimeout", set_timeout, 1);
|
2020-06-27 18:30:29 +02:00
|
|
|
define_native_function("clearInterval", clear_interval, 1);
|
|
|
|
|
define_native_function("clearTimeout", clear_timeout, 1);
|
LibJS: Simplify and normalize publicly-exposed Object functions
Previously, the Object class had many different types of functions for
each action. For example: get_by_index, get(PropertyName),
get(FlyString). This is a bit verbose, so these methods have been
shortened to simply use the PropertyName structure. The methods then
internally call _by_index if necessary. Note that the _by_index
have been made private to enforce this change.
Secondly, a clear distinction has been made between "putting" and
"defining" an object property. "Putting" should mean modifying a
(potentially) already existing property. This is akin to doing "a.b =
'foo'".
This implies two things about put operations:
- They will search the prototype chain for setters and call them, if
necessary.
- If no property exists with a particular key, the put operation
should create a new property with the default attributes
(configurable, writable, and enumerable).
In contrast, "defining" a property should completely overwrite any
existing value without calling setters (if that property is
configurable, of course).
Thus, all of the many JS objects have had any "put" calls changed to
"define_property" calls. Additionally, "put_native_function" and
"put_native_property" have had their "put" replaced with "define".
Finally, "put_own_property" has been made private, as all necessary
functionality should be exposed with the put and define_property
methods.
2020-05-26 21:33:37 -07:00
|
|
|
define_native_function("requestAnimationFrame", request_animation_frame, 1);
|
|
|
|
|
define_native_function("cancelAnimationFrame", cancel_animation_frame, 1);
|
2020-06-20 16:44:27 +02:00
|
|
|
define_native_function("atob", atob, 1);
|
|
|
|
|
define_native_function("btoa", btoa, 1);
|
2020-04-03 18:10:20 +02:00
|
|
|
|
2020-11-21 18:32:39 +00:00
|
|
|
// Legacy
|
|
|
|
|
define_native_property("event", event_getter, nullptr, JS::Attribute::Enumerable);
|
|
|
|
|
|
2020-06-20 17:28:13 +02:00
|
|
|
define_property("navigator", heap().allocate<NavigatorObject>(*this, *this), JS::Attribute::Enumerable | JS::Attribute::Configurable);
|
|
|
|
|
define_property("location", heap().allocate<LocationObject>(*this, *this), JS::Attribute::Enumerable | JS::Attribute::Configurable);
|
2020-04-08 21:11:51 +02:00
|
|
|
|
2021-05-17 00:16:44 +04:30
|
|
|
// WebAssembly "namespace"
|
|
|
|
|
define_property("WebAssembly", heap().allocate<WebAssemblyObject>(*this, *this), JS::Attribute::Enumerable | JS::Attribute::Configurable);
|
|
|
|
|
|
2021-01-18 09:50:00 +01:00
|
|
|
ADD_WINDOW_OBJECT_INTERFACES;
|
2020-04-01 18:53:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
WindowObject::~WindowObject()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-28 14:33:36 +01:00
|
|
|
void WindowObject::visit_edges(Visitor& visitor)
|
2020-04-08 21:11:51 +02:00
|
|
|
{
|
2020-11-28 14:33:36 +01:00
|
|
|
GlobalObject::visit_edges(visitor);
|
2021-01-18 09:50:00 +01:00
|
|
|
for (auto& it : m_prototypes)
|
|
|
|
|
visitor.visit(it.value);
|
|
|
|
|
for (auto& it : m_constructors)
|
|
|
|
|
visitor.visit(it.value);
|
2020-04-08 21:11:51 +02:00
|
|
|
}
|
|
|
|
|
|
2020-09-22 18:24:49 +02:00
|
|
|
Origin WindowObject::origin() const
|
|
|
|
|
{
|
|
|
|
|
return impl().document().origin();
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-27 18:36:49 +02:00
|
|
|
static DOM::Window* impl_from(JS::VM& vm, JS::GlobalObject& global_object)
|
2020-04-01 18:53:28 +02:00
|
|
|
{
|
2020-09-27 18:36:49 +02:00
|
|
|
auto* this_object = vm.this_value(global_object).to_object(global_object);
|
2020-04-01 18:53:28 +02:00
|
|
|
if (!this_object) {
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY_NOT_REACHED();
|
2020-04-01 18:53:28 +02:00
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
if (StringView("WindowObject") != this_object->class_name()) {
|
2020-09-27 18:36:49 +02:00
|
|
|
vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotA, "WindowObject");
|
2020-04-01 18:53:28 +02:00
|
|
|
return nullptr;
|
|
|
|
|
}
|
|
|
|
|
return &static_cast<WindowObject*>(this_object)->impl();
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-20 13:55:34 +02:00
|
|
|
JS_DEFINE_NATIVE_FUNCTION(WindowObject::alert)
|
2020-04-01 18:53:28 +02:00
|
|
|
{
|
2021-02-20 12:05:18 +01:00
|
|
|
// https://html.spec.whatwg.org/multipage/timers-and-user-prompts.html#simple-dialogs
|
|
|
|
|
// Note: This method is defined using two overloads, instead of using an optional argument,
|
|
|
|
|
// for historical reasons. The practical impact of this is that alert(undefined) is
|
|
|
|
|
// treated as alert("undefined"), but alert() is treated as alert("").
|
2020-09-27 18:36:49 +02:00
|
|
|
auto* impl = impl_from(vm, global_object);
|
2020-04-01 18:53:28 +02:00
|
|
|
if (!impl)
|
|
|
|
|
return {};
|
2020-04-15 23:14:03 +01:00
|
|
|
String message = "";
|
2020-09-27 18:36:49 +02:00
|
|
|
if (vm.argument_count()) {
|
|
|
|
|
message = vm.argument(0).to_string(global_object);
|
|
|
|
|
if (vm.exception())
|
2020-05-15 13:39:24 +02:00
|
|
|
return {};
|
|
|
|
|
}
|
2020-04-15 23:14:03 +01:00
|
|
|
impl->alert(message);
|
2020-04-06 20:24:45 +02:00
|
|
|
return JS::js_undefined();
|
2020-04-01 18:53:28 +02:00
|
|
|
}
|
|
|
|
|
|
2020-06-20 13:55:34 +02:00
|
|
|
JS_DEFINE_NATIVE_FUNCTION(WindowObject::confirm)
|
2020-04-16 21:29:26 -04:00
|
|
|
{
|
2020-09-27 18:36:49 +02:00
|
|
|
auto* impl = impl_from(vm, global_object);
|
2020-04-16 21:29:26 -04:00
|
|
|
if (!impl)
|
|
|
|
|
return {};
|
2020-04-21 13:49:33 -04:00
|
|
|
String message = "";
|
2021-02-20 12:05:18 +01:00
|
|
|
if (!vm.argument(0).is_undefined()) {
|
2020-09-27 18:36:49 +02:00
|
|
|
message = vm.argument(0).to_string(global_object);
|
|
|
|
|
if (vm.exception())
|
2020-05-15 13:39:24 +02:00
|
|
|
return {};
|
|
|
|
|
}
|
2020-04-21 13:49:33 -04:00
|
|
|
return JS::Value(impl->confirm(message));
|
2020-04-16 21:29:26 -04:00
|
|
|
}
|
|
|
|
|
|
2021-02-20 12:05:18 +01:00
|
|
|
JS_DEFINE_NATIVE_FUNCTION(WindowObject::prompt)
|
|
|
|
|
{
|
|
|
|
|
auto* impl = impl_from(vm, global_object);
|
|
|
|
|
if (!impl)
|
|
|
|
|
return {};
|
|
|
|
|
String message = "";
|
|
|
|
|
String default_ = "";
|
|
|
|
|
if (!vm.argument(0).is_undefined()) {
|
|
|
|
|
message = vm.argument(0).to_string(global_object);
|
|
|
|
|
if (vm.exception())
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
if (!vm.argument(1).is_undefined()) {
|
|
|
|
|
default_ = vm.argument(1).to_string(global_object);
|
|
|
|
|
if (vm.exception())
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
auto response = impl->prompt(message, default_);
|
|
|
|
|
if (response.is_null())
|
|
|
|
|
return JS::js_null();
|
|
|
|
|
return JS::js_string(vm, response);
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-20 13:55:34 +02:00
|
|
|
JS_DEFINE_NATIVE_FUNCTION(WindowObject::set_interval)
|
2020-04-01 18:53:28 +02:00
|
|
|
{
|
2020-09-27 18:36:49 +02:00
|
|
|
auto* impl = impl_from(vm, global_object);
|
2020-04-01 18:53:28 +02:00
|
|
|
if (!impl)
|
|
|
|
|
return {};
|
2020-09-27 18:36:49 +02:00
|
|
|
if (!vm.argument_count()) {
|
|
|
|
|
vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountAtLeastOne, "setInterval");
|
2020-08-25 12:52:32 +02:00
|
|
|
return {};
|
|
|
|
|
}
|
2020-09-27 18:36:49 +02:00
|
|
|
auto* callback_object = vm.argument(0).to_object(global_object);
|
2020-04-01 18:53:28 +02:00
|
|
|
if (!callback_object)
|
|
|
|
|
return {};
|
2020-08-25 12:52:32 +02:00
|
|
|
if (!callback_object->is_function()) {
|
2020-09-27 18:36:49 +02:00
|
|
|
vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAFunctionNoParam);
|
2020-08-25 12:52:32 +02:00
|
|
|
return {};
|
|
|
|
|
}
|
2020-05-21 01:10:44 +01:00
|
|
|
i32 interval = 0;
|
2020-09-27 18:36:49 +02:00
|
|
|
if (vm.argument_count() >= 2) {
|
|
|
|
|
interval = vm.argument(1).to_i32(global_object);
|
|
|
|
|
if (vm.exception())
|
2020-05-21 01:10:44 +01:00
|
|
|
return {};
|
2020-05-21 01:12:33 +01:00
|
|
|
if (interval < 0)
|
|
|
|
|
interval = 0;
|
2020-05-21 01:10:44 +01:00
|
|
|
}
|
|
|
|
|
|
2021-06-27 21:48:34 +02:00
|
|
|
auto timer_id = impl->set_interval(*static_cast<JS::FunctionObject*>(callback_object), interval);
|
2020-06-27 18:30:29 +02:00
|
|
|
return JS::Value(timer_id);
|
2020-04-01 18:53:28 +02:00
|
|
|
}
|
|
|
|
|
|
2020-06-20 13:55:34 +02:00
|
|
|
JS_DEFINE_NATIVE_FUNCTION(WindowObject::set_timeout)
|
2020-04-05 00:56:16 +02:00
|
|
|
{
|
2020-09-27 18:36:49 +02:00
|
|
|
auto* impl = impl_from(vm, global_object);
|
2020-04-05 00:56:16 +02:00
|
|
|
if (!impl)
|
|
|
|
|
return {};
|
2020-09-27 18:36:49 +02:00
|
|
|
if (!vm.argument_count()) {
|
|
|
|
|
vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountAtLeastOne, "setTimeout");
|
2020-08-25 12:52:32 +02:00
|
|
|
return {};
|
|
|
|
|
}
|
2020-09-27 18:36:49 +02:00
|
|
|
auto* callback_object = vm.argument(0).to_object(global_object);
|
2020-04-05 00:56:16 +02:00
|
|
|
if (!callback_object)
|
|
|
|
|
return {};
|
2020-08-25 12:52:32 +02:00
|
|
|
if (!callback_object->is_function()) {
|
2020-09-27 18:36:49 +02:00
|
|
|
vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAFunctionNoParam);
|
2020-08-25 12:52:32 +02:00
|
|
|
return {};
|
|
|
|
|
}
|
2020-04-05 19:04:00 +02:00
|
|
|
i32 interval = 0;
|
2020-09-27 18:36:49 +02:00
|
|
|
if (vm.argument_count() >= 2) {
|
|
|
|
|
interval = vm.argument(1).to_i32(global_object);
|
|
|
|
|
if (vm.exception())
|
2020-05-18 00:28:00 +01:00
|
|
|
return {};
|
2020-05-21 01:12:33 +01:00
|
|
|
if (interval < 0)
|
|
|
|
|
interval = 0;
|
2020-05-18 00:28:00 +01:00
|
|
|
}
|
2020-04-05 19:04:00 +02:00
|
|
|
|
2021-06-27 21:48:34 +02:00
|
|
|
auto timer_id = impl->set_timeout(*static_cast<JS::FunctionObject*>(callback_object), interval);
|
2020-06-27 18:30:29 +02:00
|
|
|
return JS::Value(timer_id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
JS_DEFINE_NATIVE_FUNCTION(WindowObject::clear_timeout)
|
|
|
|
|
{
|
2020-09-27 18:36:49 +02:00
|
|
|
auto* impl = impl_from(vm, global_object);
|
2020-06-27 18:30:29 +02:00
|
|
|
if (!impl)
|
|
|
|
|
return {};
|
2020-09-27 18:36:49 +02:00
|
|
|
if (!vm.argument_count()) {
|
|
|
|
|
vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountAtLeastOne, "clearTimeout");
|
2020-08-25 12:52:32 +02:00
|
|
|
return {};
|
|
|
|
|
}
|
2020-09-27 18:36:49 +02:00
|
|
|
i32 timer_id = vm.argument(0).to_i32(global_object);
|
|
|
|
|
if (vm.exception())
|
2020-06-27 18:30:29 +02:00
|
|
|
return {};
|
|
|
|
|
impl->clear_timeout(timer_id);
|
|
|
|
|
return JS::js_undefined();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
JS_DEFINE_NATIVE_FUNCTION(WindowObject::clear_interval)
|
|
|
|
|
{
|
2020-09-27 18:36:49 +02:00
|
|
|
auto* impl = impl_from(vm, global_object);
|
2020-06-27 18:30:29 +02:00
|
|
|
if (!impl)
|
|
|
|
|
return {};
|
2020-09-27 18:36:49 +02:00
|
|
|
if (!vm.argument_count()) {
|
|
|
|
|
vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountAtLeastOne, "clearInterval");
|
2020-08-25 12:52:32 +02:00
|
|
|
return {};
|
|
|
|
|
}
|
2020-09-27 18:36:49 +02:00
|
|
|
i32 timer_id = vm.argument(0).to_i32(global_object);
|
|
|
|
|
if (vm.exception())
|
2020-06-27 18:30:29 +02:00
|
|
|
return {};
|
|
|
|
|
impl->clear_timeout(timer_id);
|
2020-04-06 20:24:45 +02:00
|
|
|
return JS::js_undefined();
|
2020-04-05 00:56:16 +02:00
|
|
|
}
|
|
|
|
|
|
2020-06-20 13:55:34 +02:00
|
|
|
JS_DEFINE_NATIVE_FUNCTION(WindowObject::request_animation_frame)
|
2020-04-01 18:53:28 +02:00
|
|
|
{
|
2020-09-27 18:36:49 +02:00
|
|
|
auto* impl = impl_from(vm, global_object);
|
2020-04-01 18:53:28 +02:00
|
|
|
if (!impl)
|
|
|
|
|
return {};
|
2020-09-27 18:36:49 +02:00
|
|
|
if (!vm.argument_count()) {
|
|
|
|
|
vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountOne, "requestAnimationFrame");
|
2020-08-25 12:52:32 +02:00
|
|
|
return {};
|
|
|
|
|
}
|
2020-09-27 18:36:49 +02:00
|
|
|
auto* callback_object = vm.argument(0).to_object(global_object);
|
2020-04-01 18:53:28 +02:00
|
|
|
if (!callback_object)
|
|
|
|
|
return {};
|
2020-08-25 12:52:32 +02:00
|
|
|
if (!callback_object->is_function()) {
|
2020-09-27 18:36:49 +02:00
|
|
|
vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAFunctionNoParam);
|
2020-08-25 12:52:32 +02:00
|
|
|
return {};
|
|
|
|
|
}
|
2021-06-27 21:48:34 +02:00
|
|
|
return JS::Value(impl->request_animation_frame(*static_cast<JS::FunctionObject*>(callback_object)));
|
2020-04-01 18:53:28 +02:00
|
|
|
}
|
|
|
|
|
|
2020-06-20 13:55:34 +02:00
|
|
|
JS_DEFINE_NATIVE_FUNCTION(WindowObject::cancel_animation_frame)
|
2020-04-01 18:53:28 +02:00
|
|
|
{
|
2020-09-27 18:36:49 +02:00
|
|
|
auto* impl = impl_from(vm, global_object);
|
2020-04-01 18:53:28 +02:00
|
|
|
if (!impl)
|
|
|
|
|
return {};
|
2020-09-27 18:36:49 +02:00
|
|
|
if (!vm.argument_count()) {
|
|
|
|
|
vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountOne, "cancelAnimationFrame");
|
2020-08-25 12:52:32 +02:00
|
|
|
return {};
|
|
|
|
|
}
|
2020-09-27 18:36:49 +02:00
|
|
|
auto id = vm.argument(0).to_i32(global_object);
|
|
|
|
|
if (vm.exception())
|
2020-05-18 00:28:00 +01:00
|
|
|
return {};
|
|
|
|
|
impl->cancel_animation_frame(id);
|
2020-04-06 20:24:45 +02:00
|
|
|
return JS::js_undefined();
|
2020-04-01 18:53:28 +02:00
|
|
|
}
|
|
|
|
|
|
2020-06-20 16:44:27 +02:00
|
|
|
JS_DEFINE_NATIVE_FUNCTION(WindowObject::atob)
|
|
|
|
|
{
|
2020-09-27 18:36:49 +02:00
|
|
|
auto* impl = impl_from(vm, global_object);
|
2020-06-20 16:44:27 +02:00
|
|
|
if (!impl)
|
|
|
|
|
return {};
|
2020-09-27 18:36:49 +02:00
|
|
|
if (!vm.argument_count()) {
|
|
|
|
|
vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountOne, "atob");
|
2020-08-25 12:52:32 +02:00
|
|
|
return {};
|
|
|
|
|
}
|
2020-09-27 18:36:49 +02:00
|
|
|
auto string = vm.argument(0).to_string(global_object);
|
|
|
|
|
if (vm.exception())
|
2020-06-20 16:44:27 +02:00
|
|
|
return {};
|
|
|
|
|
auto decoded = decode_base64(StringView(string));
|
2020-07-22 11:39:39 -04:00
|
|
|
|
|
|
|
|
// decode_base64() returns a byte string. LibJS uses UTF-8 for strings. Use Latin1Decoder to convert bytes 128-255 to UTF-8.
|
2020-11-13 11:16:28 +00:00
|
|
|
auto decoder = TextCodec::decoder_for("windows-1252");
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(decoder);
|
2020-11-13 11:16:28 +00:00
|
|
|
return JS::js_string(vm, decoder->to_utf8(decoded));
|
2020-06-20 16:44:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
JS_DEFINE_NATIVE_FUNCTION(WindowObject::btoa)
|
|
|
|
|
{
|
2020-09-27 18:36:49 +02:00
|
|
|
auto* impl = impl_from(vm, global_object);
|
2020-06-20 16:44:27 +02:00
|
|
|
if (!impl)
|
|
|
|
|
return {};
|
2020-09-27 18:36:49 +02:00
|
|
|
if (!vm.argument_count()) {
|
|
|
|
|
vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountOne, "btoa");
|
2020-08-25 12:52:32 +02:00
|
|
|
return {};
|
|
|
|
|
}
|
2020-09-27 18:36:49 +02:00
|
|
|
auto string = vm.argument(0).to_string(global_object);
|
|
|
|
|
if (vm.exception())
|
2020-06-20 16:44:27 +02:00
|
|
|
return {};
|
2020-07-22 11:39:39 -04:00
|
|
|
|
|
|
|
|
Vector<u8> byte_string;
|
|
|
|
|
byte_string.ensure_capacity(string.length());
|
2020-08-05 16:31:20 -04:00
|
|
|
for (u32 code_point : Utf8View(string)) {
|
2020-08-25 12:52:32 +02:00
|
|
|
if (code_point > 0xff) {
|
2020-09-27 18:36:49 +02:00
|
|
|
vm.throw_exception<JS::InvalidCharacterError>(global_object, JS::ErrorType::NotAByteString, "btoa");
|
2020-08-25 12:52:32 +02:00
|
|
|
return {};
|
|
|
|
|
}
|
2020-08-05 16:31:20 -04:00
|
|
|
byte_string.append(code_point);
|
2020-07-22 11:39:39 -04:00
|
|
|
}
|
|
|
|
|
|
2020-07-27 14:16:33 +02:00
|
|
|
auto encoded = encode_base64(byte_string.span());
|
2020-09-27 18:36:49 +02:00
|
|
|
return JS::js_string(vm, move(encoded));
|
2020-06-20 16:44:27 +02:00
|
|
|
}
|
|
|
|
|
|
2021-05-31 10:10:05 +01:00
|
|
|
// https://html.spec.whatwg.org/multipage/browsers.html#dom-top
|
2021-04-07 11:19:51 +02:00
|
|
|
JS_DEFINE_NATIVE_GETTER(WindowObject::top_getter)
|
|
|
|
|
{
|
|
|
|
|
auto* impl = impl_from(vm, global_object);
|
|
|
|
|
if (!impl)
|
|
|
|
|
return {};
|
2021-05-31 10:10:05 +01:00
|
|
|
|
2021-05-30 12:36:53 +02:00
|
|
|
auto* this_browsing_context = impl->document().browsing_context();
|
2021-05-31 10:10:05 +01:00
|
|
|
if (!this_browsing_context)
|
|
|
|
|
return JS::js_null();
|
|
|
|
|
|
2021-05-30 12:36:53 +02:00
|
|
|
VERIFY(this_browsing_context->top_level_browsing_context().document());
|
|
|
|
|
auto& top_window = this_browsing_context->top_level_browsing_context().document()->window();
|
2021-04-07 11:19:51 +02:00
|
|
|
return top_window.wrapper();
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-31 10:10:05 +01:00
|
|
|
// https://html.spec.whatwg.org/multipage/browsers.html#dom-parent
|
2021-04-11 22:34:57 +01:00
|
|
|
JS_DEFINE_NATIVE_GETTER(WindowObject::parent_getter)
|
|
|
|
|
{
|
|
|
|
|
auto* impl = impl_from(vm, global_object);
|
|
|
|
|
if (!impl)
|
|
|
|
|
return {};
|
2021-05-31 10:10:05 +01:00
|
|
|
|
2021-05-30 12:36:53 +02:00
|
|
|
auto* this_browsing_context = impl->document().browsing_context();
|
2021-05-31 10:10:05 +01:00
|
|
|
if (!this_browsing_context)
|
|
|
|
|
return JS::js_null();
|
|
|
|
|
|
2021-05-30 12:36:53 +02:00
|
|
|
if (this_browsing_context->parent()) {
|
|
|
|
|
VERIFY(this_browsing_context->parent()->document());
|
|
|
|
|
auto& parent_window = this_browsing_context->parent()->document()->window();
|
2021-04-11 22:34:57 +01:00
|
|
|
return parent_window.wrapper();
|
|
|
|
|
}
|
2021-05-30 12:36:53 +02:00
|
|
|
VERIFY(this_browsing_context == &this_browsing_context->top_level_browsing_context());
|
2021-04-11 22:34:57 +01:00
|
|
|
return impl->wrapper();
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-20 13:55:34 +02:00
|
|
|
JS_DEFINE_NATIVE_GETTER(WindowObject::document_getter)
|
2020-04-01 18:53:28 +02:00
|
|
|
{
|
2020-09-27 18:36:49 +02:00
|
|
|
auto* impl = impl_from(vm, global_object);
|
2020-04-01 18:53:28 +02:00
|
|
|
if (!impl)
|
|
|
|
|
return {};
|
2020-06-23 16:57:39 +02:00
|
|
|
return wrap(global_object, impl->document());
|
2020-04-01 18:53:28 +02:00
|
|
|
}
|
|
|
|
|
|
2020-09-29 18:19:18 +02:00
|
|
|
JS_DEFINE_NATIVE_GETTER(WindowObject::performance_getter)
|
|
|
|
|
{
|
|
|
|
|
auto* impl = impl_from(vm, global_object);
|
|
|
|
|
if (!impl)
|
|
|
|
|
return {};
|
|
|
|
|
return wrap(global_object, impl->performance());
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-04 00:14:39 +02:00
|
|
|
JS_DEFINE_NATIVE_GETTER(WindowObject::screen_getter)
|
|
|
|
|
{
|
|
|
|
|
auto* impl = impl_from(vm, global_object);
|
|
|
|
|
if (!impl)
|
|
|
|
|
return {};
|
|
|
|
|
return wrap(global_object, impl->screen());
|
|
|
|
|
}
|
|
|
|
|
|
2020-11-21 18:32:39 +00:00
|
|
|
JS_DEFINE_NATIVE_GETTER(WindowObject::event_getter)
|
|
|
|
|
{
|
|
|
|
|
auto* impl = impl_from(vm, global_object);
|
|
|
|
|
if (!impl)
|
|
|
|
|
return {};
|
|
|
|
|
if (!impl->current_event())
|
|
|
|
|
return JS::js_undefined();
|
|
|
|
|
return wrap(global_object, const_cast<DOM::Event&>(*impl->current_event()));
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-16 17:22:01 +01:00
|
|
|
JS_DEFINE_NATIVE_GETTER(WindowObject::inner_width_getter)
|
|
|
|
|
{
|
|
|
|
|
auto* impl = impl_from(vm, global_object);
|
|
|
|
|
if (!impl)
|
|
|
|
|
return {};
|
|
|
|
|
return JS::Value(impl->inner_width());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
JS_DEFINE_NATIVE_GETTER(WindowObject::inner_height_getter)
|
|
|
|
|
{
|
|
|
|
|
auto* impl = impl_from(vm, global_object);
|
|
|
|
|
if (!impl)
|
|
|
|
|
return {};
|
|
|
|
|
return JS::Value(impl->inner_height());
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-01 18:53:28 +02:00
|
|
|
}
|