mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 19:12:43 +00:00 
			
		
		
		
	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:
		
							parent
							
								
									cea950fd70
								
							
						
					
					
						commit
						ed80952cb6
					
				
					 11 changed files with 228 additions and 34 deletions
				
			
		|  | @ -48,14 +48,14 @@ Value ScopeNode::execute(Interpreter& interpreter) const | |||
| 
 | ||||
| Value FunctionDeclaration::execute(Interpreter& interpreter) const | ||||
| { | ||||
|     auto* function = interpreter.heap().allocate<ScriptFunction>(name(), body(), parameters()); | ||||
|     auto* function = interpreter.heap().allocate<ScriptFunction>(name(), body(), parameters(), interpreter.current_environment()); | ||||
|     interpreter.set_variable(name(), function); | ||||
|     return js_undefined(); | ||||
| } | ||||
| 
 | ||||
| Value FunctionExpression::execute(Interpreter& interpreter) const | ||||
| { | ||||
|     return interpreter.heap().allocate<ScriptFunction>(name(), body(), parameters()); | ||||
|     return interpreter.heap().allocate<ScriptFunction>(name(), body(), parameters(), interpreter.current_environment()); | ||||
| } | ||||
| 
 | ||||
| Value ExpressionStatement::execute(Interpreter& interpreter) const | ||||
|  | @ -119,6 +119,7 @@ Value CallExpression::execute(Interpreter& interpreter) const | |||
|     auto& call_frame = interpreter.push_call_frame(); | ||||
|     call_frame.function_name = function.name(); | ||||
|     call_frame.arguments = move(arguments); | ||||
|     call_frame.environment = function.create_environment(); | ||||
| 
 | ||||
|     Object* new_object = nullptr; | ||||
|     Value result; | ||||
|  | @ -134,11 +135,11 @@ Value CallExpression::execute(Interpreter& interpreter) const | |||
|         result = function.call(interpreter); | ||||
|     } | ||||
| 
 | ||||
|     interpreter.pop_call_frame(); | ||||
| 
 | ||||
|     if (interpreter.exception()) | ||||
|         return {}; | ||||
| 
 | ||||
|     interpreter.pop_call_frame(); | ||||
| 
 | ||||
|     if (is_new_expression()) { | ||||
|         if (result.is_object()) | ||||
|             return result; | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Andreas Kling
						Andreas Kling