1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-28 14:15:07 +00:00

LibJS: Add NativeFunction, a callable wrapper around a C++ lambda

This can be used to implement arbitrary functionality, callable from
JavaScript.

To make this work, I had to change the way CallExpression passes
arguments to the callee. Instead of a HashMap<String, Value>, we now
pass an ordered list of Argument { String name; Value value; }.

This patch includes a native "print(argument)" function. :^)
This commit is contained in:
Andreas Kling 2020-03-12 19:53:31 +01:00
parent cc8e3048bc
commit 7912f33ea0
9 changed files with 129 additions and 17 deletions

View file

@ -29,6 +29,7 @@
#include <LibJS/AST.h>
#include <LibJS/Function.h>
#include <LibJS/Interpreter.h>
#include <LibJS/NativeFunction.h>
#include <LibJS/PrimitiveString.h>
#include <LibJS/Value.h>
#include <stdio.h>
@ -62,20 +63,25 @@ Value CallExpression::execute(Interpreter& interpreter) const
auto callee = interpreter.get_variable(name());
ASSERT(callee.is_object());
auto* callee_object = callee.as_object();
ASSERT(callee_object->is_function());
auto& function = static_cast<Function&>(*callee_object);
const size_t arguments_size = m_arguments.size();
ASSERT(function.parameters().size() == arguments_size);
HashMap<String, Value> passed_parameters;
for (size_t i = 0; i < arguments_size; ++i) {
auto name = function.parameters()[i];
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_parameters.set(move(name), move(value));
passed_arguments.append({ move(name), move(value) });
}
return interpreter.run(function.body(), move(passed_parameters), ScopeType::Function);
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()(move(passed_arguments));
}
ASSERT_NOT_REACHED();
}
Value ReturnStatement::execute(Interpreter& interpreter) const