summaryrefslogtreecommitdiff
path: root/Libraries/LibWeb/Bindings
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2020-09-27 18:36:49 +0200
committerAndreas Kling <kling@serenityos.org>2020-09-27 20:26:58 +0200
commit340a115dfe9518eae3d76154fba9092c36526430 (patch)
treeced6db98e45ce6ac110db8e6a2a8ba7d95565cea /Libraries/LibWeb/Bindings
parent1ff9d33131921d97b5de99496f933bcebeb4faaa (diff)
downloadserenity-340a115dfe9518eae3d76154fba9092c36526430.zip
LibJS: Make native function/property callbacks take VM, not Interpreter
More work on decoupling the general runtime from Interpreter. The goal is becoming clearer. Interpreter should be one possible way to execute code inside a VM. In the future we might have other ways :^)
Diffstat (limited to 'Libraries/LibWeb/Bindings')
-rw-r--r--Libraries/LibWeb/Bindings/LocationObject.cpp22
-rw-r--r--Libraries/LibWeb/Bindings/NavigatorObject.cpp2
-rw-r--r--Libraries/LibWeb/Bindings/WindowObject.cpp122
-rw-r--r--Libraries/LibWeb/Bindings/XMLHttpRequestPrototype.cpp24
4 files changed, 85 insertions, 85 deletions
diff --git a/Libraries/LibWeb/Bindings/LocationObject.cpp b/Libraries/LibWeb/Bindings/LocationObject.cpp
index c457e4decd..6d9234fa2f 100644
--- a/Libraries/LibWeb/Bindings/LocationObject.cpp
+++ b/Libraries/LibWeb/Bindings/LocationObject.cpp
@@ -62,14 +62,14 @@ LocationObject::~LocationObject()
JS_DEFINE_NATIVE_GETTER(LocationObject::href_getter)
{
auto& window = static_cast<WindowObject&>(global_object);
- return JS::js_string(interpreter, window.impl().document().url().to_string());
+ return JS::js_string(vm, window.impl().document().url().to_string());
}
JS_DEFINE_NATIVE_SETTER(LocationObject::href_setter)
{
auto& window = static_cast<WindowObject&>(global_object);
- auto new_href = value.to_string(interpreter);
- if (interpreter.exception())
+ auto new_href = value.to_string(global_object);
+ if (vm.exception())
return;
window.impl().did_set_location_href({}, new_href);
}
@@ -77,13 +77,13 @@ JS_DEFINE_NATIVE_SETTER(LocationObject::href_setter)
JS_DEFINE_NATIVE_GETTER(LocationObject::pathname_getter)
{
auto& window = static_cast<WindowObject&>(global_object);
- return JS::js_string(interpreter, window.impl().document().url().path());
+ return JS::js_string(vm, window.impl().document().url().path());
}
JS_DEFINE_NATIVE_GETTER(LocationObject::hostname_getter)
{
auto& window = static_cast<WindowObject&>(global_object);
- return JS::js_string(interpreter, window.impl().document().url().host());
+ return JS::js_string(vm, window.impl().document().url().host());
}
JS_DEFINE_NATIVE_GETTER(LocationObject::host_getter)
@@ -94,7 +94,7 @@ JS_DEFINE_NATIVE_GETTER(LocationObject::host_getter)
builder.append(url.host());
builder.append(':');
builder.appendf("%u", url.port());
- return JS::js_string(interpreter, builder.to_string());
+ return JS::js_string(vm, builder.to_string());
}
JS_DEFINE_NATIVE_GETTER(LocationObject::hash_getter)
@@ -102,11 +102,11 @@ JS_DEFINE_NATIVE_GETTER(LocationObject::hash_getter)
auto& window = static_cast<WindowObject&>(global_object);
auto fragment = window.impl().document().url().fragment();
if (!fragment.length())
- return JS::js_string(interpreter, "");
+ return JS::js_string(vm, "");
StringBuilder builder;
builder.append('#');
builder.append(fragment);
- return JS::js_string(interpreter, builder.to_string());
+ return JS::js_string(vm, builder.to_string());
}
JS_DEFINE_NATIVE_GETTER(LocationObject::search_getter)
@@ -114,11 +114,11 @@ JS_DEFINE_NATIVE_GETTER(LocationObject::search_getter)
auto& window = static_cast<WindowObject&>(global_object);
auto query = window.impl().document().url().query();
if (!query.length())
- return JS::js_string(interpreter, "");
+ return JS::js_string(vm, "");
StringBuilder builder;
builder.append('?');
builder.append(query);
- return JS::js_string(interpreter, builder.to_string());
+ return JS::js_string(vm, builder.to_string());
}
JS_DEFINE_NATIVE_GETTER(LocationObject::protocol_getter)
@@ -127,7 +127,7 @@ JS_DEFINE_NATIVE_GETTER(LocationObject::protocol_getter)
StringBuilder builder;
builder.append(window.impl().document().url().protocol());
builder.append(':');
- return JS::js_string(interpreter, builder.to_string());
+ return JS::js_string(vm, builder.to_string());
}
JS_DEFINE_NATIVE_FUNCTION(LocationObject::reload)
diff --git a/Libraries/LibWeb/Bindings/NavigatorObject.cpp b/Libraries/LibWeb/Bindings/NavigatorObject.cpp
index 9b6432d9de..33e7ab0a21 100644
--- a/Libraries/LibWeb/Bindings/NavigatorObject.cpp
+++ b/Libraries/LibWeb/Bindings/NavigatorObject.cpp
@@ -62,7 +62,7 @@ NavigatorObject::~NavigatorObject()
JS_DEFINE_NATIVE_GETTER(NavigatorObject::user_agent_getter)
{
- return JS::js_string(interpreter, ResourceLoader::the().user_agent());
+ return JS::js_string(vm, ResourceLoader::the().user_agent());
}
}
diff --git a/Libraries/LibWeb/Bindings/WindowObject.cpp b/Libraries/LibWeb/Bindings/WindowObject.cpp
index 25540e6293..77178514b2 100644
--- a/Libraries/LibWeb/Bindings/WindowObject.cpp
+++ b/Libraries/LibWeb/Bindings/WindowObject.cpp
@@ -99,15 +99,15 @@ Origin WindowObject::origin() const
return impl().document().origin();
}
-static DOM::Window* impl_from(JS::Interpreter& interpreter, JS::GlobalObject& global_object)
+static DOM::Window* impl_from(JS::VM& vm, JS::GlobalObject& global_object)
{
- auto* this_object = interpreter.this_value(global_object).to_object(interpreter, global_object);
+ auto* this_object = vm.this_value(global_object).to_object(global_object);
if (!this_object) {
ASSERT_NOT_REACHED();
return nullptr;
}
if (StringView("WindowObject") != this_object->class_name()) {
- interpreter.vm().throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotA, "WindowObject");
+ vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotA, "WindowObject");
return nullptr;
}
return &static_cast<WindowObject*>(this_object)->impl();
@@ -115,13 +115,13 @@ static DOM::Window* impl_from(JS::Interpreter& interpreter, JS::GlobalObject& gl
JS_DEFINE_NATIVE_FUNCTION(WindowObject::alert)
{
- auto* impl = impl_from(interpreter, global_object);
+ auto* impl = impl_from(vm, global_object);
if (!impl)
return {};
String message = "";
- if (interpreter.argument_count()) {
- message = interpreter.argument(0).to_string(interpreter);
- if (interpreter.exception())
+ if (vm.argument_count()) {
+ message = vm.argument(0).to_string(global_object);
+ if (vm.exception())
return {};
}
impl->alert(message);
@@ -130,13 +130,13 @@ JS_DEFINE_NATIVE_FUNCTION(WindowObject::alert)
JS_DEFINE_NATIVE_FUNCTION(WindowObject::confirm)
{
- auto* impl = impl_from(interpreter, global_object);
+ auto* impl = impl_from(vm, global_object);
if (!impl)
return {};
String message = "";
- if (interpreter.argument_count()) {
- message = interpreter.argument(0).to_string(interpreter);
- if (interpreter.exception())
+ if (vm.argument_count()) {
+ message = vm.argument(0).to_string(global_object);
+ if (vm.exception())
return {};
}
return JS::Value(impl->confirm(message));
@@ -144,24 +144,24 @@ JS_DEFINE_NATIVE_FUNCTION(WindowObject::confirm)
JS_DEFINE_NATIVE_FUNCTION(WindowObject::set_interval)
{
- auto* impl = impl_from(interpreter, global_object);
+ auto* impl = impl_from(vm, global_object);
if (!impl)
return {};
- if (!interpreter.argument_count()) {
- interpreter.vm().throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountAtLeastOne, "setInterval");
+ if (!vm.argument_count()) {
+ vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountAtLeastOne, "setInterval");
return {};
}
- auto* callback_object = interpreter.argument(0).to_object(interpreter, global_object);
+ auto* callback_object = vm.argument(0).to_object(global_object);
if (!callback_object)
return {};
if (!callback_object->is_function()) {
- interpreter.vm().throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAFunctionNoParam);
+ vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAFunctionNoParam);
return {};
}
i32 interval = 0;
- if (interpreter.argument_count() >= 2) {
- interval = interpreter.argument(1).to_i32(interpreter);
- if (interpreter.exception())
+ if (vm.argument_count() >= 2) {
+ interval = vm.argument(1).to_i32(global_object);
+ if (vm.exception())
return {};
if (interval < 0)
interval = 0;
@@ -173,24 +173,24 @@ JS_DEFINE_NATIVE_FUNCTION(WindowObject::set_interval)
JS_DEFINE_NATIVE_FUNCTION(WindowObject::set_timeout)
{
- auto* impl = impl_from(interpreter, global_object);
+ auto* impl = impl_from(vm, global_object);
if (!impl)
return {};
- if (!interpreter.argument_count()) {
- interpreter.vm().throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountAtLeastOne, "setTimeout");
+ if (!vm.argument_count()) {
+ vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountAtLeastOne, "setTimeout");
return {};
}
- auto* callback_object = interpreter.argument(0).to_object(interpreter, global_object);
+ auto* callback_object = vm.argument(0).to_object(global_object);
if (!callback_object)
return {};
if (!callback_object->is_function()) {
- interpreter.vm().throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAFunctionNoParam);
+ vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAFunctionNoParam);
return {};
}
i32 interval = 0;
- if (interpreter.argument_count() >= 2) {
- interval = interpreter.argument(1).to_i32(interpreter);
- if (interpreter.exception())
+ if (vm.argument_count() >= 2) {
+ interval = vm.argument(1).to_i32(global_object);
+ if (vm.exception())
return {};
if (interval < 0)
interval = 0;
@@ -202,15 +202,15 @@ JS_DEFINE_NATIVE_FUNCTION(WindowObject::set_timeout)
JS_DEFINE_NATIVE_FUNCTION(WindowObject::clear_timeout)
{
- auto* impl = impl_from(interpreter, global_object);
+ auto* impl = impl_from(vm, global_object);
if (!impl)
return {};
- if (!interpreter.argument_count()) {
- interpreter.vm().throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountAtLeastOne, "clearTimeout");
+ if (!vm.argument_count()) {
+ vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountAtLeastOne, "clearTimeout");
return {};
}
- i32 timer_id = interpreter.argument(0).to_i32(interpreter);
- if (interpreter.exception())
+ i32 timer_id = vm.argument(0).to_i32(global_object);
+ if (vm.exception())
return {};
impl->clear_timeout(timer_id);
return JS::js_undefined();
@@ -218,15 +218,15 @@ JS_DEFINE_NATIVE_FUNCTION(WindowObject::clear_timeout)
JS_DEFINE_NATIVE_FUNCTION(WindowObject::clear_interval)
{
- auto* impl = impl_from(interpreter, global_object);
+ auto* impl = impl_from(vm, global_object);
if (!impl)
return {};
- if (!interpreter.argument_count()) {
- interpreter.vm().throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountAtLeastOne, "clearInterval");
+ if (!vm.argument_count()) {
+ vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountAtLeastOne, "clearInterval");
return {};
}
- i32 timer_id = interpreter.argument(0).to_i32(interpreter);
- if (interpreter.exception())
+ i32 timer_id = vm.argument(0).to_i32(global_object);
+ if (vm.exception())
return {};
impl->clear_timeout(timer_id);
return JS::js_undefined();
@@ -234,18 +234,18 @@ JS_DEFINE_NATIVE_FUNCTION(WindowObject::clear_interval)
JS_DEFINE_NATIVE_FUNCTION(WindowObject::request_animation_frame)
{
- auto* impl = impl_from(interpreter, global_object);
+ auto* impl = impl_from(vm, global_object);
if (!impl)
return {};
- if (!interpreter.argument_count()) {
- interpreter.vm().throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountOne, "requestAnimationFrame");
+ if (!vm.argument_count()) {
+ vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountOne, "requestAnimationFrame");
return {};
}
- auto* callback_object = interpreter.argument(0).to_object(interpreter, global_object);
+ auto* callback_object = vm.argument(0).to_object(global_object);
if (!callback_object)
return {};
if (!callback_object->is_function()) {
- interpreter.vm().throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAFunctionNoParam);
+ vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotAFunctionNoParam);
return {};
}
return JS::Value(impl->request_animation_frame(*static_cast<JS::Function*>(callback_object)));
@@ -253,15 +253,15 @@ JS_DEFINE_NATIVE_FUNCTION(WindowObject::request_animation_frame)
JS_DEFINE_NATIVE_FUNCTION(WindowObject::cancel_animation_frame)
{
- auto* impl = impl_from(interpreter, global_object);
+ auto* impl = impl_from(vm, global_object);
if (!impl)
return {};
- if (!interpreter.argument_count()) {
- interpreter.vm().throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountOne, "cancelAnimationFrame");
+ if (!vm.argument_count()) {
+ vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountOne, "cancelAnimationFrame");
return {};
}
- auto id = interpreter.argument(0).to_i32(interpreter);
- if (interpreter.exception())
+ auto id = vm.argument(0).to_i32(global_object);
+ if (vm.exception())
return {};
impl->cancel_animation_frame(id);
return JS::js_undefined();
@@ -269,52 +269,52 @@ JS_DEFINE_NATIVE_FUNCTION(WindowObject::cancel_animation_frame)
JS_DEFINE_NATIVE_FUNCTION(WindowObject::atob)
{
- auto* impl = impl_from(interpreter, global_object);
+ auto* impl = impl_from(vm, global_object);
if (!impl)
return {};
- if (!interpreter.argument_count()) {
- interpreter.vm().throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountOne, "atob");
+ if (!vm.argument_count()) {
+ vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountOne, "atob");
return {};
}
- auto string = interpreter.argument(0).to_string(interpreter);
- if (interpreter.exception())
+ auto string = vm.argument(0).to_string(global_object);
+ if (vm.exception())
return {};
auto decoded = decode_base64(StringView(string));
// decode_base64() returns a byte string. LibJS uses UTF-8 for strings. Use Latin1Decoder to convert bytes 128-255 to UTF-8.
- return JS::js_string(interpreter, TextCodec::decoder_for("iso-8859-1")->to_utf8(decoded));
+ return JS::js_string(vm, TextCodec::decoder_for("iso-8859-1")->to_utf8(decoded));
}
JS_DEFINE_NATIVE_FUNCTION(WindowObject::btoa)
{
- auto* impl = impl_from(interpreter, global_object);
+ auto* impl = impl_from(vm, global_object);
if (!impl)
return {};
- if (!interpreter.argument_count()) {
- interpreter.vm().throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountOne, "btoa");
+ if (!vm.argument_count()) {
+ vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::BadArgCountOne, "btoa");
return {};
}
- auto string = interpreter.argument(0).to_string(interpreter);
- if (interpreter.exception())
+ auto string = vm.argument(0).to_string(global_object);
+ if (vm.exception())
return {};
Vector<u8> byte_string;
byte_string.ensure_capacity(string.length());
for (u32 code_point : Utf8View(string)) {
if (code_point > 0xff) {
- interpreter.vm().throw_exception<JS::InvalidCharacterError>(global_object, JS::ErrorType::NotAByteString, "btoa");
+ vm.throw_exception<JS::InvalidCharacterError>(global_object, JS::ErrorType::NotAByteString, "btoa");
return {};
}
byte_string.append(code_point);
}
auto encoded = encode_base64(byte_string.span());
- return JS::js_string(interpreter, move(encoded));
+ return JS::js_string(vm, move(encoded));
}
JS_DEFINE_NATIVE_GETTER(WindowObject::document_getter)
{
- auto* impl = impl_from(interpreter, global_object);
+ auto* impl = impl_from(vm, global_object);
if (!impl)
return {};
return wrap(global_object, impl->document());
diff --git a/Libraries/LibWeb/Bindings/XMLHttpRequestPrototype.cpp b/Libraries/LibWeb/Bindings/XMLHttpRequestPrototype.cpp
index d9e3310e1e..7286d10f50 100644
--- a/Libraries/LibWeb/Bindings/XMLHttpRequestPrototype.cpp
+++ b/Libraries/LibWeb/Bindings/XMLHttpRequestPrototype.cpp
@@ -58,13 +58,13 @@ XMLHttpRequestPrototype::~XMLHttpRequestPrototype()
{
}
-static XMLHttpRequest* impl_from(JS::Interpreter& interpreter, JS::GlobalObject& global_object)
+static XMLHttpRequest* impl_from(JS::VM& vm, JS::GlobalObject& global_object)
{
- auto* this_object = interpreter.this_value(global_object).to_object(interpreter, global_object);
+ auto* this_object = vm.this_value(global_object).to_object(global_object);
if (!this_object)
return nullptr;
if (StringView("XMLHttpRequestWrapper") != this_object->class_name()) {
- interpreter.vm().throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotA, "XMLHttpRequest");
+ vm.throw_exception<JS::TypeError>(global_object, JS::ErrorType::NotA, "XMLHttpRequest");
return nullptr;
}
return &static_cast<XMLHttpRequestWrapper*>(this_object)->impl();
@@ -72,14 +72,14 @@ static XMLHttpRequest* impl_from(JS::Interpreter& interpreter, JS::GlobalObject&
JS_DEFINE_NATIVE_FUNCTION(XMLHttpRequestPrototype::open)
{
- auto* impl = impl_from(interpreter, global_object);
+ auto* impl = impl_from(vm, global_object);
if (!impl)
return {};
- auto arg0 = interpreter.argument(0).to_string(interpreter);
- if (interpreter.exception())
+ auto arg0 = vm.argument(0).to_string(global_object);
+ if (vm.exception())
return {};
- auto arg1 = interpreter.argument(1).to_string(interpreter);
- if (interpreter.exception())
+ auto arg1 = vm.argument(1).to_string(global_object);
+ if (vm.exception())
return {};
impl->open(arg0, arg1);
return JS::js_undefined();
@@ -87,7 +87,7 @@ JS_DEFINE_NATIVE_FUNCTION(XMLHttpRequestPrototype::open)
JS_DEFINE_NATIVE_FUNCTION(XMLHttpRequestPrototype::send)
{
- auto* impl = impl_from(interpreter, global_object);
+ auto* impl = impl_from(vm, global_object);
if (!impl)
return {};
impl->send();
@@ -96,7 +96,7 @@ JS_DEFINE_NATIVE_FUNCTION(XMLHttpRequestPrototype::send)
JS_DEFINE_NATIVE_GETTER(XMLHttpRequestPrototype::ready_state_getter)
{
- auto* impl = impl_from(interpreter, global_object);
+ auto* impl = impl_from(vm, global_object);
if (!impl)
return {};
return JS::Value((i32)impl->ready_state());
@@ -104,10 +104,10 @@ JS_DEFINE_NATIVE_GETTER(XMLHttpRequestPrototype::ready_state_getter)
JS_DEFINE_NATIVE_GETTER(XMLHttpRequestPrototype::response_text_getter)
{
- auto* impl = impl_from(interpreter, global_object);
+ auto* impl = impl_from(vm, global_object);
if (!impl)
return {};
- return JS::js_string(interpreter, impl->response_text());
+ return JS::js_string(vm, impl->response_text());
}
}