From 9755f8d29755362b306d8d2ac90a42a7f855100e Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Thu, 28 May 2020 19:12:34 +0100 Subject: LibJS: Add Object::invoke() --- Libraries/LibJS/Runtime/Object.cpp | 13 +++++++++++++ Libraries/LibJS/Runtime/Object.h | 3 +++ 2 files changed, 16 insertions(+) (limited to 'Libraries/LibJS/Runtime') diff --git a/Libraries/LibJS/Runtime/Object.cpp b/Libraries/LibJS/Runtime/Object.cpp index 6f44638231..eff0d31e12 100644 --- a/Libraries/LibJS/Runtime/Object.cpp +++ b/Libraries/LibJS/Runtime/Object.cpp @@ -662,4 +662,17 @@ Value Object::to_string() const return js_string(heap(), String::format("[object %s]", class_name())); } +Value Object::invoke(const FlyString& property_name, Optional arguments) const +{ + auto& interpreter = const_cast(this)->interpreter(); + auto property = get(property_name).value_or(js_undefined()); + if (interpreter.exception()) + return {}; + if (!property.is_function()) { + interpreter.throw_exception(String::format("%s is not a function", property.to_string_without_side_effects().characters())); + return {}; + } + return interpreter.call(property.as_function(), const_cast(this), move(arguments)); +} + } diff --git a/Libraries/LibJS/Runtime/Object.h b/Libraries/LibJS/Runtime/Object.h index 44be63e506..503ba55857 100644 --- a/Libraries/LibJS/Runtime/Object.h +++ b/Libraries/LibJS/Runtime/Object.h @@ -31,6 +31,7 @@ #include #include #include +#include #include #include #include @@ -107,6 +108,8 @@ public: IndexedProperties& indexed_properties() { return m_indexed_properties; } void set_indexed_property_elements(Vector&& values) { m_indexed_properties = IndexedProperties(move(values)); } + Value invoke(const FlyString& property_name, Optional arguments = {}) const; + private: virtual Value get_by_index(u32 property_index) const; virtual bool put_by_index(u32 property_index, Value); -- cgit v1.2.3