1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 10:48:11 +00:00

LibJS: Add Object::invoke()

This commit is contained in:
Linus Groh 2020-05-28 19:12:34 +01:00 committed by Andreas Kling
parent 5c6323c130
commit 9755f8d297
2 changed files with 16 additions and 0 deletions

View file

@ -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));
}
}