mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 09:38:11 +00:00
LibJS: Split Function into subclasses NativeFunction and ScriptFunction
Both types of functions are now Function and implement calling via: virtual Value call(Interpreter&, Vector<Value> arguments); This removes the need for CallExpression::execute() to care about which kind of function it's calling. :^)
This commit is contained in:
parent
de6f697eba
commit
d9c7009604
9 changed files with 133 additions and 42 deletions
|
@ -27,10 +27,9 @@
|
|||
#include <AK/HashMap.h>
|
||||
#include <AK/StringBuilder.h>
|
||||
#include <LibJS/AST.h>
|
||||
#include <LibJS/Function.h>
|
||||
#include <LibJS/Interpreter.h>
|
||||
#include <LibJS/NativeFunction.h>
|
||||
#include <LibJS/PrimitiveString.h>
|
||||
#include <LibJS/ScriptFunction.h>
|
||||
#include <LibJS/Value.h>
|
||||
#include <stdio.h>
|
||||
|
||||
|
@ -43,7 +42,7 @@ Value ScopeNode::execute(Interpreter& interpreter) const
|
|||
|
||||
Value FunctionDeclaration::execute(Interpreter& interpreter) const
|
||||
{
|
||||
auto* function = interpreter.heap().allocate<Function>(name(), body(), parameters());
|
||||
auto* function = interpreter.heap().allocate<ScriptFunction>(body(), parameters());
|
||||
interpreter.set_variable(m_name, function);
|
||||
return function;
|
||||
}
|
||||
|
@ -57,26 +56,14 @@ Value CallExpression::execute(Interpreter& interpreter) const
|
|||
{
|
||||
auto callee = interpreter.get_variable(name());
|
||||
ASSERT(callee.is_object());
|
||||
auto* callee_object = callee.as_object();
|
||||
ASSERT(callee.as_object()->is_function());
|
||||
auto* function = static_cast<Function*>(callee.as_object());
|
||||
|
||||
Vector<Argument> passed_arguments;
|
||||
for (size_t i = 0; i < m_arguments.size(); ++i) {
|
||||
String name;
|
||||
if (callee_object->is_function())
|
||||
name = static_cast<Function&>(*callee_object).parameters()[i];
|
||||
auto value = m_arguments[i].execute(interpreter);
|
||||
dbg() << name << ": " << value;
|
||||
passed_arguments.append({ move(name), move(value) });
|
||||
}
|
||||
Vector<Value> argument_values;
|
||||
for (size_t i = 0; i < m_arguments.size(); ++i)
|
||||
argument_values.append(m_arguments[i].execute(interpreter));
|
||||
|
||||
if (callee_object->is_function())
|
||||
return interpreter.run(static_cast<Function&>(*callee_object).body(), move(passed_arguments), ScopeType::Function);
|
||||
|
||||
if (callee_object->is_native_function()) {
|
||||
return static_cast<NativeFunction&>(*callee_object).native_function()(interpreter, move(passed_arguments));
|
||||
}
|
||||
|
||||
ASSERT_NOT_REACHED();
|
||||
return function->call(interpreter, move(argument_values));
|
||||
}
|
||||
|
||||
Value ReturnStatement::execute(Interpreter& interpreter) const
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue