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:
parent
542108421e
commit
df40c85f80
6 changed files with 85 additions and 16 deletions
|
@ -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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue