Retoor software development Projects Research SearchXNG
Register Sign In

20 lines
391 B
Plaintext
Raw Normal View History

feat: implement closures with upvalue support and statement/expression separation Add closure objects that wrap functions with captured upvalues, enabling first-class closures that close over local variables from enclosing scopes. Introduce new bytecode instructions (CODE_CLOSURE, CODE_LOAD_UPVALUE, CODE_STORE_UPVALUE, CODE_CLOSE_UPVALUE, CODE_RETURN) and a CompilerUpvalue tracking system with a MAX_UPVALUES limit of 256. Refactor the grammar to strictly separate statements from expressions, fixing a bug where block expressions incorrectly popped locals while temporaries remained on the stack. Rename internal functions (wrenCallFunction, wrenDebugDumpInstruction, wrenDebugDumpStack) and add Upvalue allocation, closure creation, and debug dump support for the new instructions.
2013-12-04 16:43:50 +01:00
{
var f = null
{
var a = "a"
f = fn io.write(a)
}
{
// Since a is out of scope, the local slot will be reused by b. Make sure
// that f still closes over a.
var b = "b"
f.call // expect: a
}
}
// TODO(bob): Closing over this.
// TODO(bob): Close over fn/method parameter.
// TODO(bob): Maximum number of closed-over variables (directly and/or indirect).
Reference in New Issue Copy Permalink
ffc58724d7
wren/test/closure/reuse_closure_slot.wren
Response time: 46ms
Mail Licenses API