diff options
author | Andreas Kling <kling@serenityos.org> | 2020-03-13 10:08:52 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-03-13 11:08:16 +0100 |
commit | d9c70096046b416439243cba7cd45117b4e9a3ed (patch) | |
tree | 520e86a63e56dcc0fb46f31be81028bd51dcf61c /Libraries/LibJS/AST.cpp | |
parent | de6f697ebabfbe998ef2fcb16817131f8a796a0e (diff) | |
download | serenity-d9c70096046b416439243cba7cd45117b4e9a3ed.zip |
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. :^)
Diffstat (limited to 'Libraries/LibJS/AST.cpp')
-rw-r--r-- | Libraries/LibJS/AST.cpp | 29 |
1 files changed, 8 insertions, 21 deletions
diff --git a/Libraries/LibJS/AST.cpp b/Libraries/LibJS/AST.cpp index dce5c0092c..2f51372831 100644 --- a/Libraries/LibJS/AST.cpp +++ b/Libraries/LibJS/AST.cpp @@ -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(); - - 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) }); - } - - if (callee_object->is_function()) - return interpreter.run(static_cast<Function&>(*callee_object).body(), move(passed_arguments), ScopeType::Function); + ASSERT(callee.as_object()->is_function()); + auto* function = static_cast<Function*>(callee.as_object()); - if (callee_object->is_native_function()) { - return static_cast<NativeFunction&>(*callee_object).native_function()(interpreter, move(passed_arguments)); - } + Vector<Value> argument_values; + for (size_t i = 0; i < m_arguments.size(); ++i) + argument_values.append(m_arguments[i].execute(interpreter)); - ASSERT_NOT_REACHED(); + return function->call(interpreter, move(argument_values)); } Value ReturnStatement::execute(Interpreter& interpreter) const |