1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-03 00:52:12 +00:00

LibJS: Allow the choice of a scope of declaration for a variable (#1408)

Previously, we were assuming all declared variables were bound to a
block scope, now, with the addition of declaration types, we can bind
a variable to a block scope using `let`, or a function scope (the scope
of the inner-most enclosing function of a `var` declaration) using
`var`.
This commit is contained in:
0xtechnobabble 2020-03-11 21:09:20 +02:00 committed by GitHub
parent 542108421e
commit df40c85f80
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 85 additions and 16 deletions

View file

@ -33,7 +33,13 @@
namespace JS {
enum class ScopeType {
Function,
Block,
};
struct ScopeFrame {
ScopeType type;
const ScopeNode& scope_node;
HashMap<String, Value> variables;
};
@ -43,7 +49,7 @@ public:
Interpreter();
~Interpreter();
Value run(const ScopeNode&);
Value run(const ScopeNode&, ScopeType = ScopeType::Block);
Object& global_object() { return *m_global_object; }
const Object& global_object() const { return *m_global_object; }
@ -54,12 +60,12 @@ public:
Value get_variable(const String& name);
void set_variable(String name, Value);
void declare_variable(String name);
void declare_variable(String name, DeclarationType);
void collect_roots(Badge<Heap>, HashTable<Cell*>&);
private:
void enter_scope(const ScopeNode&);
void enter_scope(const ScopeNode&, ScopeType);
void exit_scope(const ScopeNode&);
Heap m_heap;