diff options
author | Andreas Kling <kling@serenityos.org> | 2020-04-15 21:58:22 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-04-15 22:07:20 +0200 |
commit | ed80952cb6e2b32e2541198134ffc98d282a559d (patch) | |
tree | 788ee9cf2e7f1036e41cee395585839cef05c115 /Libraries/LibJS/Makefile | |
parent | cea950fd701e98de073e37a4476fc5ca1719dee0 (diff) | |
download | serenity-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/Makefile')
-rw-r--r-- | Libraries/LibJS/Makefile | 1 |
1 files changed, 1 insertions, 0 deletions
diff --git a/Libraries/LibJS/Makefile b/Libraries/LibJS/Makefile index e7c7cc0f70..3e924b8dac 100644 --- a/Libraries/LibJS/Makefile +++ b/Libraries/LibJS/Makefile @@ -25,6 +25,7 @@ OBJS = \ Runtime/FunctionConstructor.o \ Runtime/FunctionPrototype.o \ Runtime/GlobalObject.o \ + Runtime/LexicalEnvironment.o \ Runtime/MathObject.o \ Runtime/NativeFunction.o \ Runtime/NativeProperty.o \ |