mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 06:37:35 +00:00
LibJS: Rework how native functions are called to improve |this| value
Native functions now only get the Interpreter& as an argument. They can then extract |this| along with any indexed arguments it wants from it. This forces functions that want |this| to actually deal with calling interpreter.this_value().to_object(), and dealing with the possibility of a non-object |this|. This is still not great but let's keep massaging it forward.
This commit is contained in:
parent
c209ea1985
commit
7c4e53f31e
25 changed files with 145 additions and 102 deletions
|
@ -24,8 +24,9 @@
|
|||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <AK/Function.h>
|
||||
#include <AK/FlyString.h>
|
||||
#include <AK/Function.h>
|
||||
#include <LibJS/Interpreter.h>
|
||||
#include <LibJS/Runtime/PrimitiveString.h>
|
||||
#include <LibJS/Runtime/Value.h>
|
||||
#include <LibWeb/Bindings/CanvasRenderingContext2DWrapper.h>
|
||||
|
@ -50,7 +51,8 @@ CanvasRenderingContext2DWrapper::CanvasRenderingContext2DWrapper(CanvasRendering
|
|||
[this](JS::Object*, JS::Value value) {
|
||||
m_impl->set_fill_style(value.to_string());
|
||||
});
|
||||
put_native_function("fillRect", [this](JS::Object*, const Vector<JS::Value>& arguments) {
|
||||
put_native_function("fillRect", [this](JS::Interpreter& interpreter) {
|
||||
auto& arguments = interpreter.call_frame().arguments;
|
||||
if (arguments.size() >= 4) {
|
||||
m_impl->fill_rect(arguments[0].to_i32(), arguments[1].to_i32(), arguments[2].to_i32(), arguments[3].to_i32());
|
||||
}
|
||||
|
|
|
@ -25,6 +25,7 @@
|
|||
*/
|
||||
|
||||
#include <AK/FlyString.h>
|
||||
#include <LibJS/Interpreter.h>
|
||||
#include <LibJS/Runtime/PrimitiveString.h>
|
||||
#include <LibJS/Runtime/Value.h>
|
||||
#include <LibWeb/Bindings/DocumentWrapper.h>
|
||||
|
@ -37,7 +38,8 @@ namespace Bindings {
|
|||
DocumentWrapper::DocumentWrapper(Document& document)
|
||||
: NodeWrapper(document)
|
||||
{
|
||||
put_native_function("getElementById", [this](JS::Object*, const Vector<JS::Value>& arguments) -> JS::Value {
|
||||
put_native_function("getElementById", [this](JS::Interpreter& interpreter) -> JS::Value {
|
||||
auto& arguments = interpreter.call_frame().arguments;
|
||||
if (arguments.is_empty())
|
||||
return JS::js_null();
|
||||
auto id = arguments[0].to_string();
|
||||
|
|
|
@ -24,8 +24,9 @@
|
|||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <AK/Function.h>
|
||||
#include <AK/FlyString.h>
|
||||
#include <AK/Function.h>
|
||||
#include <LibJS/Interpreter.h>
|
||||
#include <LibJS/Runtime/Function.h>
|
||||
#include <LibWeb/Bindings/EventListenerWrapper.h>
|
||||
#include <LibWeb/Bindings/EventTargetWrapper.h>
|
||||
|
@ -38,7 +39,12 @@ namespace Bindings {
|
|||
EventTargetWrapper::EventTargetWrapper(EventTarget& impl)
|
||||
: m_impl(impl)
|
||||
{
|
||||
put_native_function("addEventListener", [](Object* this_object, const Vector<JS::Value>& arguments) {
|
||||
put_native_function("addEventListener", [](JS::Interpreter& interpreter) -> JS::Value {
|
||||
auto* this_object = interpreter.this_value().to_object(interpreter.heap());
|
||||
if (!this_object)
|
||||
return {};
|
||||
|
||||
auto& arguments = interpreter.call_frame().arguments;
|
||||
if (arguments.size() < 2)
|
||||
return JS::js_undefined();
|
||||
|
||||
|
|
|
@ -24,8 +24,9 @@
|
|||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <AK/Function.h>
|
||||
#include <AK/FlyString.h>
|
||||
#include <AK/Function.h>
|
||||
#include <LibJS/Interpreter.h>
|
||||
#include <LibJS/Runtime/PrimitiveString.h>
|
||||
#include <LibJS/Runtime/Value.h>
|
||||
#include <LibWeb/Bindings/CanvasRenderingContext2DWrapper.h>
|
||||
|
@ -39,7 +40,8 @@ namespace Bindings {
|
|||
HTMLCanvasElementWrapper::HTMLCanvasElementWrapper(HTMLCanvasElement& element)
|
||||
: ElementWrapper(element)
|
||||
{
|
||||
put_native_function("getContext", [this](JS::Object*, const Vector<JS::Value>& arguments) -> JS::Value {
|
||||
put_native_function("getContext", [this](JS::Interpreter& interpreter) -> JS::Value {
|
||||
auto& arguments = interpreter.call_frame().arguments;
|
||||
if (arguments.size() >= 1) {
|
||||
auto* context = node().get_context(arguments[0].to_string());
|
||||
return wrap(heap(), *context);
|
||||
|
|
|
@ -338,14 +338,16 @@ JS::Interpreter& Document::interpreter()
|
|||
if (!m_interpreter) {
|
||||
m_interpreter = make<JS::Interpreter>();
|
||||
|
||||
m_interpreter->global_object().put_native_function("alert", [](JS::Object*, const Vector<JS::Value>& arguments) -> JS::Value {
|
||||
m_interpreter->global_object().put_native_function("alert", [](JS::Interpreter& interpreter) -> JS::Value {
|
||||
auto& arguments = interpreter.call_frame().arguments;
|
||||
if (arguments.size() < 1)
|
||||
return JS::js_undefined();
|
||||
GUI::MessageBox::show(arguments[0].to_string(), "Alert", GUI::MessageBox::Type::Information);
|
||||
return JS::js_undefined();
|
||||
});
|
||||
|
||||
m_interpreter->global_object().put_native_function("setInterval", [this](JS::Object*, const Vector<JS::Value>& arguments) -> JS::Value {
|
||||
m_interpreter->global_object().put_native_function("setInterval", [this](JS::Interpreter& interpreter) -> JS::Value {
|
||||
auto& arguments = interpreter.call_frame().arguments;
|
||||
if (arguments.size() < 2)
|
||||
return JS::js_undefined();
|
||||
ASSERT(arguments[0].is_object());
|
||||
|
@ -355,14 +357,16 @@ JS::Interpreter& Document::interpreter()
|
|||
// FIXME: This timer should not be leaked! It should also be removable with clearInterval()!
|
||||
(void)Core::Timer::construct(
|
||||
arguments[1].to_i32(), [this, callback] {
|
||||
const_cast<JS::Function*>(static_cast<const JS::Function*>(callback.cell()))->call(*m_interpreter, {});
|
||||
// FIXME: Perform the call through Interpreter so it can set up a call frame!
|
||||
const_cast<JS::Function*>(static_cast<const JS::Function*>(callback.cell()))->call(*m_interpreter);
|
||||
})
|
||||
.leak_ref();
|
||||
|
||||
return JS::js_undefined();
|
||||
});
|
||||
|
||||
m_interpreter->global_object().put_native_function("requestAnimationFrame", [this](JS::Object*, const Vector<JS::Value>& arguments) -> JS::Value {
|
||||
m_interpreter->global_object().put_native_function("requestAnimationFrame", [this](JS::Interpreter& interpreter) -> JS::Value {
|
||||
auto& arguments = interpreter.call_frame().arguments;
|
||||
if (arguments.size() < 1)
|
||||
return JS::js_undefined();
|
||||
ASSERT(arguments[0].is_object());
|
||||
|
@ -370,13 +374,15 @@ JS::Interpreter& Document::interpreter()
|
|||
auto callback = make_handle(const_cast<JS::Object*>(arguments[0].as_object()));
|
||||
// FIXME: Don't hand out raw DisplayLink ID's to JavaScript!
|
||||
i32 link_id = GUI::DisplayLink::register_callback([this, callback](i32 link_id) {
|
||||
const_cast<JS::Function*>(static_cast<const JS::Function*>(callback.cell()))->call(*m_interpreter, {});
|
||||
// FIXME: Perform the call through Interpreter so it can set up a call frame!
|
||||
const_cast<JS::Function*>(static_cast<const JS::Function*>(callback.cell()))->call(*m_interpreter);
|
||||
GUI::DisplayLink::unregister_callback(link_id);
|
||||
});
|
||||
return JS::Value(link_id);
|
||||
});
|
||||
|
||||
m_interpreter->global_object().put_native_function("cancelAnimationFrame", [](JS::Object*, const Vector<JS::Value>& arguments) -> JS::Value {
|
||||
m_interpreter->global_object().put_native_function("cancelAnimationFrame", [](JS::Interpreter& interpreter) -> JS::Value {
|
||||
auto& arguments = interpreter.call_frame().arguments;
|
||||
if (arguments.size() < 1)
|
||||
return JS::js_undefined();
|
||||
// FIXME: We should not be passing untrusted numbers to DisplayLink::unregistered_callback()!
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue