diff --git a/Libraries/LibJS/Runtime/ArrayPrototype.cpp b/Libraries/LibJS/Runtime/ArrayPrototype.cpp index 007b03452f..0d3db3d188 100644 --- a/Libraries/LibJS/Runtime/ArrayPrototype.cpp +++ b/Libraries/LibJS/Runtime/ArrayPrototype.cpp @@ -35,36 +35,43 @@ namespace JS { ArrayPrototype::ArrayPrototype() { - put_native_function("shift", [](Interpreter& interpreter) -> Value { - auto* this_object = interpreter.this_value().to_object(interpreter.heap()); - if (!this_object) - return {}; - ASSERT(this_object->is_array()); - return static_cast(this_object)->shift(); - }); - - put_native_function("pop", [](Interpreter& interpreter) -> Value { - auto* this_object = interpreter.this_value().to_object(interpreter.heap()); - if (!this_object) - return {}; - ASSERT(this_object->is_array()); - return static_cast(this_object)->pop(); - }); - - put_native_function("push", [](Interpreter& interpreter) -> Value { - auto* this_object = interpreter.this_value().to_object(interpreter.heap()); - if (!this_object) - return {}; - ASSERT(this_object->is_array()); - if (interpreter.call_frame().arguments.is_empty()) - return js_undefined(); - static_cast(this_object)->push(interpreter.call_frame().arguments[0]); - return Value(static_cast(this_object)->length()); - }); + put_native_function("shift", shift); + put_native_function("pop", pop); + put_native_function("push", push); } ArrayPrototype::~ArrayPrototype() { } +Value ArrayPrototype::push(Interpreter& interpreter) +{ + auto* this_object = interpreter.this_value().to_object(interpreter.heap()); + if (!this_object) + return {}; + ASSERT(this_object->is_array()); + if (interpreter.call_frame().arguments.is_empty()) + return js_undefined(); + static_cast(this_object)->push(interpreter.call_frame().arguments[0]); + return Value(static_cast(this_object)->length()); +} + +Value ArrayPrototype::pop(Interpreter& interpreter) +{ + auto* this_object = interpreter.this_value().to_object(interpreter.heap()); + if (!this_object) + return {}; + ASSERT(this_object->is_array()); + return static_cast(this_object)->pop(); +} + +Value ArrayPrototype::shift(Interpreter& interpreter) +{ + auto* this_object = interpreter.this_value().to_object(interpreter.heap()); + if (!this_object) + return {}; + ASSERT(this_object->is_array()); + return static_cast(this_object)->shift(); +} + } diff --git a/Libraries/LibJS/Runtime/ArrayPrototype.h b/Libraries/LibJS/Runtime/ArrayPrototype.h index 2ada6dee17..9828512b61 100644 --- a/Libraries/LibJS/Runtime/ArrayPrototype.h +++ b/Libraries/LibJS/Runtime/ArrayPrototype.h @@ -37,6 +37,10 @@ public: private: virtual const char* class_name() const override { return "ArrayPrototype"; } + + static Value push(Interpreter&); + static Value shift(Interpreter&); + static Value pop(Interpreter&); }; } diff --git a/Libraries/LibJS/Runtime/ConsoleObject.cpp b/Libraries/LibJS/Runtime/ConsoleObject.cpp index b496d11240..ee359d29df 100644 --- a/Libraries/LibJS/Runtime/ConsoleObject.cpp +++ b/Libraries/LibJS/Runtime/ConsoleObject.cpp @@ -34,19 +34,22 @@ namespace JS { ConsoleObject::ConsoleObject() { - put_native_function("log", [](Interpreter& interpreter) -> Value { - for (size_t i = 0; i < interpreter.call_frame().arguments.size(); ++i) { - printf("%s", interpreter.call_frame().arguments[i].to_string().characters()); - if (i != interpreter.call_frame().arguments.size() - 1) - putchar(' '); - } - putchar('\n'); - return js_undefined(); - }); + put_native_function("log", log); } ConsoleObject::~ConsoleObject() { } +Value ConsoleObject::log(Interpreter& interpreter) +{ + for (size_t i = 0; i < interpreter.call_frame().arguments.size(); ++i) { + printf("%s", interpreter.call_frame().arguments[i].to_string().characters()); + if (i != interpreter.call_frame().arguments.size() - 1) + putchar(' '); + } + putchar('\n'); + return js_undefined(); +} + } diff --git a/Libraries/LibJS/Runtime/ConsoleObject.h b/Libraries/LibJS/Runtime/ConsoleObject.h index 635e6f11c0..f0959fd520 100644 --- a/Libraries/LibJS/Runtime/ConsoleObject.h +++ b/Libraries/LibJS/Runtime/ConsoleObject.h @@ -37,6 +37,8 @@ public: private: virtual const char* class_name() const override { return "ConsoleObject"; } + + static Value log(Interpreter&); }; } diff --git a/Libraries/LibJS/Runtime/GlobalObject.cpp b/Libraries/LibJS/Runtime/GlobalObject.cpp index 2872585dcb..b637b2e23b 100644 --- a/Libraries/LibJS/Runtime/GlobalObject.cpp +++ b/Libraries/LibJS/Runtime/GlobalObject.cpp @@ -13,17 +13,10 @@ namespace JS { GlobalObject::GlobalObject() { + put_native_function("gc", gc); + put_native_function("isNaN", is_nan); + put("console", heap().allocate()); - put_native_function("gc", [](Interpreter& interpreter) -> Value { - dbg() << "Forced garbage collection requested!"; - interpreter.heap().collect_garbage(); - return js_undefined(); - }); - put_native_function("isNaN", [](Interpreter& interpreter) -> Value { - if (interpreter.call_frame().arguments.size() < 1) - return js_undefined(); - return Value(interpreter.call_frame().arguments[0].to_number().is_nan()); - }); put("Math", heap().allocate()); put("Object", heap().allocate()); } @@ -32,4 +25,18 @@ GlobalObject::~GlobalObject() { } +Value GlobalObject::gc(Interpreter& interpreter) +{ + dbg() << "Forced garbage collection requested!"; + interpreter.heap().collect_garbage(); + return js_undefined(); +} + +Value GlobalObject::is_nan(Interpreter& interpreter) +{ + if (interpreter.call_frame().arguments.size() < 1) + return js_undefined(); + return Value(interpreter.call_frame().arguments[0].to_number().is_nan()); +} + } diff --git a/Libraries/LibJS/Runtime/GlobalObject.h b/Libraries/LibJS/Runtime/GlobalObject.h index 3aabb4d3e2..c5efc058b9 100644 --- a/Libraries/LibJS/Runtime/GlobalObject.h +++ b/Libraries/LibJS/Runtime/GlobalObject.h @@ -11,6 +11,9 @@ public: private: virtual const char* class_name() const override { return "GlobalObject"; } + + static Value gc(Interpreter&); + static Value is_nan(Interpreter&); }; } diff --git a/Libraries/LibJS/Runtime/MathObject.cpp b/Libraries/LibJS/Runtime/MathObject.cpp index be65fdc3cc..96218036d7 100644 --- a/Libraries/LibJS/Runtime/MathObject.cpp +++ b/Libraries/LibJS/Runtime/MathObject.cpp @@ -32,18 +32,21 @@ namespace JS { MathObject::MathObject() { - put_native_function("random", [](Interpreter&) { -#ifdef __serenity__ - double r = (double)arc4random() / (double)UINT32_MAX; -#else - double r = (double)rand() / (double)RAND_MAX; -#endif - return Value(r); - }); + put_native_function("random", random); } MathObject::~MathObject() { } +Value MathObject::random(Interpreter&) +{ +#ifdef __serenity__ + double r = (double)arc4random() / (double)UINT32_MAX; +#else + double r = (double)rand() / (double)RAND_MAX; +#endif + return Value(r); +} + } diff --git a/Libraries/LibJS/Runtime/MathObject.h b/Libraries/LibJS/Runtime/MathObject.h index a52aadc68d..5d5e079321 100644 --- a/Libraries/LibJS/Runtime/MathObject.h +++ b/Libraries/LibJS/Runtime/MathObject.h @@ -37,6 +37,8 @@ public: private: virtual const char* class_name() const override { return "MathObject"; } + + static Value random(Interpreter&); }; } diff --git a/Libraries/LibJS/Runtime/ObjectPrototype.cpp b/Libraries/LibJS/Runtime/ObjectPrototype.cpp index def5b891b7..96eaa42602 100644 --- a/Libraries/LibJS/Runtime/ObjectPrototype.cpp +++ b/Libraries/LibJS/Runtime/ObjectPrototype.cpp @@ -37,32 +37,39 @@ ObjectPrototype::ObjectPrototype() { set_prototype(nullptr); - put_native_function("hasOwnProperty", [](Interpreter& interpreter) -> Value { - auto* this_object = interpreter.this_value().to_object(interpreter.heap()); - if (!this_object) - return {}; - if (interpreter.call_frame().arguments.is_empty()) - return js_undefined(); - return Value(this_object->has_own_property(interpreter.call_frame().arguments[0].to_string())); - }); - - put_native_function("toString", [](Interpreter& interpreter) -> Value { - auto* this_object = interpreter.this_value().to_object(interpreter.heap()); - if (!this_object) - return {}; - return Value(this_object->to_string()); - }); - - put_native_function("valueOf", [](Interpreter& interpreter) -> Value { - auto* this_object = interpreter.this_value().to_object(interpreter.heap()); - if (!this_object) - return {}; - return this_object->value_of(); - }); + put_native_function("hasOwnProperty", has_own_property); + put_native_function("toString", to_string); + put_native_function("valueOf", value_of); } ObjectPrototype::~ObjectPrototype() { } +Value ObjectPrototype::has_own_property(Interpreter& interpreter) +{ + auto* this_object = interpreter.this_value().to_object(interpreter.heap()); + if (!this_object) + return {}; + if (interpreter.call_frame().arguments.is_empty()) + return js_undefined(); + return Value(this_object->has_own_property(interpreter.call_frame().arguments[0].to_string())); +} + +Value ObjectPrototype::to_string(Interpreter& interpreter) +{ + auto* this_object = interpreter.this_value().to_object(interpreter.heap()); + if (!this_object) + return {}; + return Value(this_object->to_string()); +} + +Value ObjectPrototype::value_of(Interpreter& interpreter) +{ + auto* this_object = interpreter.this_value().to_object(interpreter.heap()); + if (!this_object) + return {}; + return this_object->value_of(); +} + } diff --git a/Libraries/LibJS/Runtime/ObjectPrototype.h b/Libraries/LibJS/Runtime/ObjectPrototype.h index 704e0322e3..f398c7b91c 100644 --- a/Libraries/LibJS/Runtime/ObjectPrototype.h +++ b/Libraries/LibJS/Runtime/ObjectPrototype.h @@ -37,6 +37,10 @@ public: private: virtual const char* class_name() const override { return "ObjectPrototype"; } + + static Value has_own_property(Interpreter&); + static Value value_of(Interpreter&); + static Value to_string(Interpreter&); }; } diff --git a/Libraries/LibJS/Runtime/StringPrototype.cpp b/Libraries/LibJS/Runtime/StringPrototype.cpp index d17185cf99..ffdaddd4de 100644 --- a/Libraries/LibJS/Runtime/StringPrototype.cpp +++ b/Libraries/LibJS/Runtime/StringPrototype.cpp @@ -44,42 +44,48 @@ StringPrototype::StringPrototype() return Value((i32) static_cast(this_object)->primitive_string()->string().length()); }, nullptr); - put_native_function("charAt", [](Interpreter& interpreter) -> Value { - auto* this_object = interpreter.this_value().to_object(interpreter.heap()); - if (!this_object) - return {}; - i32 index = 0; - if (!interpreter.call_frame().arguments.is_empty()) - index = interpreter.call_frame().arguments[0].to_i32(); - ASSERT(this_object->is_string_object()); - auto underlying_string = static_cast(this_object)->primitive_string()->string(); - if (index < 0 || index >= static_cast(underlying_string.length())) - return js_string(this_object->heap(), String::empty()); - return js_string(this_object->heap(), underlying_string.substring(index, 1)); - }); - put_native_function("repeat", [](Interpreter& interpreter) -> Value { - auto* this_object = interpreter.this_value().to_object(interpreter.heap()); - if (!this_object) - return {}; - ASSERT(this_object->is_string_object()); - if (interpreter.call_frame().arguments.is_empty()) - return js_string(this_object->heap(), String::empty()); - i32 count = 0; - count = interpreter.call_frame().arguments[0].to_i32(); - if (count < 0) { - // FIXME: throw RangeError - return js_undefined(); - } - auto* string_object = static_cast(this_object); - StringBuilder builder; - for (i32 i = 0; i < count; ++i) - builder.append(string_object->primitive_string()->string()); - return js_string(this_object->heap(), builder.to_string()); - }); + put_native_function("charAt", char_at); + put_native_function("repeat", repeat); } StringPrototype::~StringPrototype() { } +Value StringPrototype::char_at(Interpreter& interpreter) +{ + auto* this_object = interpreter.this_value().to_object(interpreter.heap()); + if (!this_object) + return {}; + i32 index = 0; + if (!interpreter.call_frame().arguments.is_empty()) + index = interpreter.call_frame().arguments[0].to_i32(); + ASSERT(this_object->is_string_object()); + auto underlying_string = static_cast(this_object)->primitive_string()->string(); + if (index < 0 || index >= static_cast(underlying_string.length())) + return js_string(interpreter.heap(), String::empty()); + return js_string(interpreter.heap(), underlying_string.substring(index, 1)); +} + +Value StringPrototype::repeat(Interpreter& interpreter) +{ + auto* this_object = interpreter.this_value().to_object(interpreter.heap()); + if (!this_object) + return {}; + ASSERT(this_object->is_string_object()); + if (interpreter.call_frame().arguments.is_empty()) + return js_string(interpreter.heap(), String::empty()); + i32 count = 0; + count = interpreter.call_frame().arguments[0].to_i32(); + if (count < 0) { + // FIXME: throw RangeError + return js_undefined(); + } + auto* string_object = static_cast(this_object); + StringBuilder builder; + for (i32 i = 0; i < count; ++i) + builder.append(string_object->primitive_string()->string()); + return js_string(interpreter.heap(), builder.to_string()); +} + } diff --git a/Libraries/LibJS/Runtime/StringPrototype.h b/Libraries/LibJS/Runtime/StringPrototype.h index 51cdedd73b..74beb46bdd 100644 --- a/Libraries/LibJS/Runtime/StringPrototype.h +++ b/Libraries/LibJS/Runtime/StringPrototype.h @@ -37,6 +37,9 @@ public: private: virtual const char* class_name() const override { return "StringPrototype"; } + + static Value char_at(Interpreter&); + static Value repeat(Interpreter&); }; } diff --git a/Libraries/LibWeb/Bindings/CanvasRenderingContext2DWrapper.cpp b/Libraries/LibWeb/Bindings/CanvasRenderingContext2DWrapper.cpp index 3041bcd1f6..4e620d646b 100644 --- a/Libraries/LibWeb/Bindings/CanvasRenderingContext2DWrapper.cpp +++ b/Libraries/LibWeb/Bindings/CanvasRenderingContext2DWrapper.cpp @@ -51,18 +51,25 @@ CanvasRenderingContext2DWrapper::CanvasRenderingContext2DWrapper(CanvasRendering [this](JS::Object*, JS::Value value) { m_impl->set_fill_style(value.to_string()); }); - 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()); - } - return JS::js_undefined(); - }); + put_native_function("fillRect", fill_rect); } CanvasRenderingContext2DWrapper::~CanvasRenderingContext2DWrapper() { } +JS::Value CanvasRenderingContext2DWrapper::fill_rect(JS::Interpreter& interpreter) +{ + auto* this_object = interpreter.this_value().to_object(interpreter.heap()); + if (!this_object) + return {}; + // FIXME: Verify that it's a CanvasRenderingContext2DWrapper somehow! + auto& impl = static_cast(this_object)->impl(); + auto& arguments = interpreter.call_frame().arguments; + if (arguments.size() >= 4) + impl.fill_rect(arguments[0].to_i32(), arguments[1].to_i32(), arguments[2].to_i32(), arguments[3].to_i32()); + return JS::js_undefined(); +} + } } diff --git a/Libraries/LibWeb/Bindings/CanvasRenderingContext2DWrapper.h b/Libraries/LibWeb/Bindings/CanvasRenderingContext2DWrapper.h index 569e5952e0..a2e6c50fa1 100644 --- a/Libraries/LibWeb/Bindings/CanvasRenderingContext2DWrapper.h +++ b/Libraries/LibWeb/Bindings/CanvasRenderingContext2DWrapper.h @@ -42,6 +42,8 @@ public: private: virtual const char* class_name() const override { return "CanvasRenderingContext2DWrapper"; } + static JS::Value fill_rect(JS::Interpreter&); + NonnullRefPtr m_impl; }; diff --git a/Libraries/LibWeb/Bindings/DocumentWrapper.cpp b/Libraries/LibWeb/Bindings/DocumentWrapper.cpp index 04f95713e4..1f0926c1f1 100644 --- a/Libraries/LibWeb/Bindings/DocumentWrapper.cpp +++ b/Libraries/LibWeb/Bindings/DocumentWrapper.cpp @@ -38,16 +38,7 @@ namespace Bindings { DocumentWrapper::DocumentWrapper(Document& document) : NodeWrapper(document) { - 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(); - auto* element = node().get_element_by_id(id); - if (!element) - return JS::js_null(); - return wrap(heap(), const_cast(*element)); - }); + put_native_function("getElementById", get_element_by_id); } DocumentWrapper::~DocumentWrapper() @@ -64,5 +55,22 @@ const Document& DocumentWrapper::node() const return static_cast(NodeWrapper::node()); } +JS::Value DocumentWrapper::get_element_by_id(JS::Interpreter& interpreter) +{ + auto* this_object = interpreter.this_value().to_object(interpreter.heap()); + if (!this_object) + return {}; + // FIXME: Verify that it's a DocumentWrapper somehow! + auto& node = static_cast(this_object)->node(); + auto& arguments = interpreter.call_frame().arguments; + if (arguments.is_empty()) + return JS::js_null(); + auto id = arguments[0].to_string(); + auto* element = node.get_element_by_id(id); + if (!element) + return JS::js_null(); + return wrap(interpreter.heap(), const_cast(*element)); +} + } } diff --git a/Libraries/LibWeb/Bindings/DocumentWrapper.h b/Libraries/LibWeb/Bindings/DocumentWrapper.h index dcf8e48348..dbde01798e 100644 --- a/Libraries/LibWeb/Bindings/DocumentWrapper.h +++ b/Libraries/LibWeb/Bindings/DocumentWrapper.h @@ -41,6 +41,8 @@ public: private: virtual const char* class_name() const override { return "DocumentWrapper"; } + + static JS::Value get_element_by_id(JS::Interpreter&); }; } diff --git a/Libraries/LibWeb/Bindings/EventTargetWrapper.cpp b/Libraries/LibWeb/Bindings/EventTargetWrapper.cpp index fb29a8ed3c..d161c5aeea 100644 --- a/Libraries/LibWeb/Bindings/EventTargetWrapper.cpp +++ b/Libraries/LibWeb/Bindings/EventTargetWrapper.cpp @@ -39,28 +39,29 @@ namespace Bindings { EventTargetWrapper::EventTargetWrapper(EventTarget& impl) : m_impl(impl) { - 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(); - - auto event_name = arguments[0].to_string(); - ASSERT(arguments[1].is_object()); - ASSERT(arguments[1].as_object()->is_function()); - auto* function = static_cast(const_cast(arguments[1].as_object())); - auto listener = adopt(*new EventListener(JS::make_handle(function))); - static_cast(this_object)->impl().add_event_listener(event_name, move(listener)); - return JS::js_undefined(); - }); + put_native_function("addEventListener", add_event_listener); } EventTargetWrapper::~EventTargetWrapper() { } +JS::Value EventTargetWrapper::add_event_listener(JS::Interpreter& interpreter) +{ + 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(); + auto event_name = arguments[0].to_string(); + ASSERT(arguments[1].is_object()); + ASSERT(arguments[1].as_object()->is_function()); + auto* function = static_cast(const_cast(arguments[1].as_object())); + auto listener = adopt(*new EventListener(JS::make_handle(function))); + static_cast(this_object)->impl().add_event_listener(event_name, move(listener)); + return JS::js_undefined(); +} + } } diff --git a/Libraries/LibWeb/Bindings/EventTargetWrapper.h b/Libraries/LibWeb/Bindings/EventTargetWrapper.h index 2ce45e707c..f07aef6007 100644 --- a/Libraries/LibWeb/Bindings/EventTargetWrapper.h +++ b/Libraries/LibWeb/Bindings/EventTargetWrapper.h @@ -42,6 +42,8 @@ public: private: virtual const char* class_name() const override { return "EventTargetWrapper"; } + static JS::Value add_event_listener(JS::Interpreter&); + NonnullRefPtr m_impl; }; diff --git a/Libraries/LibWeb/Bindings/HTMLCanvasElementWrapper.cpp b/Libraries/LibWeb/Bindings/HTMLCanvasElementWrapper.cpp index 2c78580280..0e50bd1bf4 100644 --- a/Libraries/LibWeb/Bindings/HTMLCanvasElementWrapper.cpp +++ b/Libraries/LibWeb/Bindings/HTMLCanvasElementWrapper.cpp @@ -40,14 +40,8 @@ namespace Bindings { HTMLCanvasElementWrapper::HTMLCanvasElementWrapper(HTMLCanvasElement& element) : ElementWrapper(element) { - 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); - } - return JS::js_undefined(); - }); + put_native_function("getContext", get_context); + put_native_property( "width", [this](JS::Object*) { @@ -76,5 +70,20 @@ const HTMLCanvasElement& HTMLCanvasElementWrapper::node() const return static_cast(NodeWrapper::node()); } +JS::Value HTMLCanvasElementWrapper::get_context(JS::Interpreter& interpreter) +{ + auto* this_object = interpreter.this_value().to_object(interpreter.heap()); + if (!this_object) + return {}; + // FIXME: Verify that it's an HTMLCanvasElementWrapper somehow! + auto& node = static_cast(this_object)->node(); + auto& arguments = interpreter.call_frame().arguments; + if (arguments.size() >= 1) { + auto* context = node.get_context(arguments[0].to_string()); + return wrap(interpreter.heap(), *context); + } + return JS::js_undefined(); +} + } } diff --git a/Libraries/LibWeb/Bindings/HTMLCanvasElementWrapper.h b/Libraries/LibWeb/Bindings/HTMLCanvasElementWrapper.h index d82342c668..fb27d0c6b1 100644 --- a/Libraries/LibWeb/Bindings/HTMLCanvasElementWrapper.h +++ b/Libraries/LibWeb/Bindings/HTMLCanvasElementWrapper.h @@ -41,6 +41,8 @@ public: private: virtual const char* class_name() const override { return "HTMLCanvasElementWrapper"; } + + static JS::Value get_context(JS::Interpreter&); }; }