summaryrefslogtreecommitdiff
path: root/Libraries/LibJS/Runtime/Function.h
diff options
context:
space:
mode:
authorAndreas Kling <kling@serenityos.org>2020-04-15 21:58:22 +0200
committerAndreas Kling <kling@serenityos.org>2020-04-15 22:07:20 +0200
commited80952cb6e2b32e2541198134ffc98d282a559d (patch)
tree788ee9cf2e7f1036e41cee395585839cef05c115 /Libraries/LibJS/Runtime/Function.h
parentcea950fd701e98de073e37a4476fc5ca1719dee0 (diff)
downloadserenity-ed80952cb6e2b32e2541198134ffc98d282a559d.zip
LibJS: Introduce LexicalEnvironment
This patch replaces the old variable lookup logic with a new one based on lexical environments. This brings us closer to the way JavaScript is actually specced, and also gives us some basic support for closures. The interpreter's call stack frames now have a pointer to the lexical environment for that frame. Each lexical environment can have a chain of parent environments. Before calling a Function, we first ask it to create_environment(). This gives us a new LexicalEnvironment for that function, which has the function's lexical parent's environment as its parent. This allows inner functions to access variables in their outer function: function foo() { <-- LexicalEnvironment A var x = 1; function() { <-- LexicalEnvironment B (parent: A) console.log(x); } } If we return the result of a function expression from a function, that new function object will keep a reference to its parent environment, which is how we get closures. :^) I'm pretty sure I didn't get everything right here, but it's a pretty good start. This is quite a bit slower than before, but also correcter!
Diffstat (limited to 'Libraries/LibJS/Runtime/Function.h')
-rw-r--r--Libraries/LibJS/Runtime/Function.h1
1 files changed, 1 insertions, 0 deletions
diff --git a/Libraries/LibJS/Runtime/Function.h b/Libraries/LibJS/Runtime/Function.h
index 242f671b9e..3465e02135 100644
--- a/Libraries/LibJS/Runtime/Function.h
+++ b/Libraries/LibJS/Runtime/Function.h
@@ -38,6 +38,7 @@ public:
virtual Value call(Interpreter&) = 0;
virtual Value construct(Interpreter&) = 0;
virtual const FlyString& name() const = 0;
+ virtual LexicalEnvironment* create_environment() = 0;
protected:
Function();