1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 11:28:12 +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

@ -27,6 +27,7 @@
#pragma once
#include <AK/HashMap.h>
#include <AK/String.h>
#include <AK/Vector.h>
#include <LibJS/Forward.h>
#include <LibJS/Heap.h>
@ -50,12 +51,17 @@ struct ScopeFrame {
HashMap<String, Variable> variables;
};
struct Argument {
String name;
Value value;
};
class Interpreter {
public:
Interpreter();
~Interpreter();
Value run(const ScopeNode&, HashMap<String, Value> scope_variables = {}, ScopeType = ScopeType::Block);
Value run(const ScopeNode&, Vector<Argument> = {}, ScopeType = ScopeType::Block);
Object& global_object() { return *m_global_object; }
const Object& global_object() const { return *m_global_object; }
@ -71,7 +77,7 @@ public:
void collect_roots(Badge<Heap>, HashTable<Cell*>&);
private:
void enter_scope(const ScopeNode&, HashMap<String, Value> scope_variables, ScopeType);
void enter_scope(const ScopeNode&, Vector<Argument>, ScopeType);
void exit_scope(const ScopeNode&);
Heap m_heap;