diff options
author | Linus Groh <mail@linusgroh.de> | 2020-05-28 19:12:34 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-05-29 08:00:02 +0200 |
commit | 9755f8d29755362b306d8d2ac90a42a7f855100e (patch) | |
tree | b2073a3173ac54fbfb32e8edc28c42887b627f6e /Libraries/LibJS/Runtime | |
parent | 5c6323c1305e8ff1cebb8a0fc5136834780b1645 (diff) | |
download | serenity-9755f8d29755362b306d8d2ac90a42a7f855100e.zip |
LibJS: Add Object::invoke()
Diffstat (limited to 'Libraries/LibJS/Runtime')
-rw-r--r-- | Libraries/LibJS/Runtime/Object.cpp | 13 | ||||
-rw-r--r-- | Libraries/LibJS/Runtime/Object.h | 3 |
2 files changed, 16 insertions, 0 deletions
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<MarkedValueList> arguments) const +{ + auto& interpreter = const_cast<Object*>(this)->interpreter(); + auto property = get(property_name).value_or(js_undefined()); + if (interpreter.exception()) + return {}; + if (!property.is_function()) { + interpreter.throw_exception<TypeError>(String::format("%s is not a function", property.to_string_without_side_effects().characters())); + return {}; + } + return interpreter.call(property.as_function(), const_cast<Object*>(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 <LibJS/Forward.h> #include <LibJS/Runtime/Cell.h> #include <LibJS/Runtime/IndexedProperties.h> +#include <LibJS/Runtime/MarkedValueList.h> #include <LibJS/Runtime/PrimitiveString.h> #include <LibJS/Runtime/PropertyName.h> #include <LibJS/Runtime/Shape.h> @@ -107,6 +108,8 @@ public: IndexedProperties& indexed_properties() { return m_indexed_properties; } void set_indexed_property_elements(Vector<Value>&& values) { m_indexed_properties = IndexedProperties(move(values)); } + Value invoke(const FlyString& property_name, Optional<MarkedValueList> arguments = {}) const; + private: virtual Value get_by_index(u32 property_index) const; virtual bool put_by_index(u32 property_index, Value); |