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!
This commit is contained in:
Andreas Kling 2020-04-15 21:58:22 +02:00
parent cea950fd70
commit ed80952cb6
Notes: sideshowbarker 2024-07-19 07:33:56 +09:00
11 changed files with 228 additions and 34 deletions

View file

@ -33,11 +33,17 @@
namespace JS {
ScriptFunction::ScriptFunction(const FlyString& name, const Statement& body, Vector<FlyString> parameters)
ScriptFunction::ScriptFunction(const FlyString& name, const Statement& body, Vector<FlyString> parameters, LexicalEnvironment* parent_environment)
: m_name(name)
, m_body(body)
, m_parameters(move(parameters))
, m_parent_environment(parent_environment)
{
HashMap<FlyString, Variable> variables;
for (auto& parameter : parameters) {
variables.set(parameter, {});
}
put("prototype", heap().allocate<Object>());
put_native_property("length", length_getter, length_setter);
}
@ -46,6 +52,30 @@ ScriptFunction::~ScriptFunction()
{
}
void ScriptFunction::visit_children(Visitor& visitor)
{
Function::visit_children(visitor);
visitor.visit(m_parent_environment);
}
LexicalEnvironment* ScriptFunction::create_environment()
{
HashMap<FlyString, Variable> variables;
for (auto& parameter : m_parameters) {
variables.set(parameter, { js_undefined(), DeclarationKind::Var });
}
if (body().is_scope_node()) {
for (auto& declaration : static_cast<const ScopeNode&>(body()).variables()) {
for (auto& declarator : declaration.declarations()) {
variables.set(declarator.id().string(), { js_undefined(), DeclarationKind::Var });
}
}
}
return heap().allocate<LexicalEnvironment>(move(variables), m_parent_environment);
}
Value ScriptFunction::call(Interpreter& interpreter)
{
auto& argument_values = interpreter.call_frame().arguments;
@ -55,7 +85,8 @@ Value ScriptFunction::call(Interpreter& interpreter)
auto value = js_undefined();
if (i < argument_values.size())
value = argument_values[i];
arguments.append({ move(name), move(value) });
arguments.append({ name, value });
interpreter.current_environment()->set(name, { value, DeclarationKind::Var });
}
return interpreter.run(m_body, arguments, ScopeType::Function);
}