1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 19:27:44 +00:00

LibJS: Add basic support for (scoped) variables

It's now possible to assign expressions to variables. The variables are
put into the current scope of the interpreter.

Variable lookup follows the scope chain, ending in the global object.
This commit is contained in:
Andreas Kling 2020-03-09 21:13:55 +01:00
parent ac3c19b91c
commit 1382dbc5e1
5 changed files with 224 additions and 19 deletions

View file

@ -26,6 +26,7 @@
#pragma once
#include <AK/HashMap.h>
#include <AK/Vector.h>
#include <LibJS/Forward.h>
#include <LibJS/Heap.h>
@ -34,6 +35,7 @@ namespace JS {
struct ScopeFrame {
const ScopeNode& scope_node;
HashMap<String, Value> variables;
};
class Interpreter {
@ -50,6 +52,10 @@ public:
void do_return();
Value get_variable(const String& name);
void set_variable(String name, Value);
void declare_variable(String name);
private:
void enter_scope(const ScopeNode&);
void exit_scope(const ScopeNode&);