Open
Description
Feature suggestion
#798 and #1240 has discussed lots of thing about closure, but maybe there are some outdated information.
So I want to start to discuss it in some new page.
Project State
- function object will be compiled as static object in data area.
- function object store function index in table and null env.
High Level Requirement For Closure
- Every function object from the same FunctionExpression will have different env.
- When call function object, function can use (read / write) the variable in env and upper function env.
- (Optional) If function is not a closure function, supporting closure has no or small influence this function in the perspective of code size and spend.
What should we do to support closure
For better code review, I think maybe we can support it step by step. I will post everything we need to implement closure. Please correct my mistakes and omissions.
let upper_function = () => {
let variable_in_upper_function: i32 = 0;
let current_function = () => {
let variable_in_current_function: i32 = 0; // used in lower function.
variable_in_upper_function = 1; // variable declared in upper function.
let lower_function = () => {
variable_in_current_function = 2;
}
}
)
code gen
- create function instance for closure function expression. feat: support first class function via builtin function #2752
- get upper function env.
- read/write variable which used by lower function in env instead of local variable.
semantic
- we need a way in compiler to create / modify class definition.
- analyze which function expression should enable closure supporting.
- analyze which local variable is used in lower function.
- analyze which identifier is declared in upper function. (already support in resolver.ts)