mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 19:37:35 +00:00
LibJS: Add Object::invoke()
This commit is contained in:
parent
5c6323c130
commit
9755f8d297
2 changed files with 16 additions and 0 deletions
|
@ -662,4 +662,17 @@ Value Object::to_string() const
|
||||||
return js_string(heap(), String::format("[object %s]", class_name()));
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,6 +31,7 @@
|
||||||
#include <LibJS/Forward.h>
|
#include <LibJS/Forward.h>
|
||||||
#include <LibJS/Runtime/Cell.h>
|
#include <LibJS/Runtime/Cell.h>
|
||||||
#include <LibJS/Runtime/IndexedProperties.h>
|
#include <LibJS/Runtime/IndexedProperties.h>
|
||||||
|
#include <LibJS/Runtime/MarkedValueList.h>
|
||||||
#include <LibJS/Runtime/PrimitiveString.h>
|
#include <LibJS/Runtime/PrimitiveString.h>
|
||||||
#include <LibJS/Runtime/PropertyName.h>
|
#include <LibJS/Runtime/PropertyName.h>
|
||||||
#include <LibJS/Runtime/Shape.h>
|
#include <LibJS/Runtime/Shape.h>
|
||||||
|
@ -107,6 +108,8 @@ public:
|
||||||
IndexedProperties& indexed_properties() { return m_indexed_properties; }
|
IndexedProperties& indexed_properties() { return m_indexed_properties; }
|
||||||
void set_indexed_property_elements(Vector<Value>&& values) { m_indexed_properties = IndexedProperties(move(values)); }
|
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:
|
private:
|
||||||
virtual Value get_by_index(u32 property_index) const;
|
virtual Value get_by_index(u32 property_index) const;
|
||||||
virtual bool put_by_index(u32 property_index, Value);
|
virtual bool put_by_index(u32 property_index, Value);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue