|
@@ -1272,6 +1272,9 @@ static bool gravity_vm_exec (gravity_vm *vm) {
|
|
// create closure
|
|
// create closure
|
|
gravity_closure_t *closure = gravity_closure_new(vm, f);
|
|
gravity_closure_t *closure = gravity_closure_new(vm, f);
|
|
|
|
|
|
|
|
+ // save current context (if any)
|
|
|
|
+ closure->context = gravity_value_isobject(STACK_GET(0)) ? VALUE_AS_OBJECT(STACK_GET(0)) : NULL;
|
|
|
|
+
|
|
// loop for each upvalue setup instruction
|
|
// loop for each upvalue setup instruction
|
|
for (uint16_t i=0; i<f->nupvalues; ++i) {
|
|
for (uint16_t i=0; i<f->nupvalues; ++i) {
|
|
// code is generated by the compiler so op must be MOVE
|
|
// code is generated by the compiler so op must be MOVE
|
|
@@ -1470,7 +1473,7 @@ void gravity_vm_loadclosure (gravity_vm *vm, gravity_closure_t *closure) {
|
|
gravity_vm_exec(vm);
|
|
gravity_vm_exec(vm);
|
|
}
|
|
}
|
|
|
|
|
|
-bool gravity_vm_runclosure (gravity_vm *vm, gravity_closure_t *closure, gravity_value_t selfvalue, gravity_value_t params[], uint16_t nparams) {
|
|
|
|
|
|
+bool gravity_vm_runclosure (gravity_vm *vm, gravity_closure_t *closure, gravity_value_t sender, gravity_value_t params[], uint16_t nparams) {
|
|
if (!vm || !closure || vm->aborted) return false;
|
|
if (!vm || !closure || vm->aborted) return false;
|
|
|
|
|
|
// do not waste cycles on empty functions
|
|
// do not waste cycles on empty functions
|
|
@@ -1484,6 +1487,11 @@ bool gravity_vm_runclosure (gravity_vm *vm, gravity_closure_t *closure, gravity_
|
|
|
|
|
|
DEBUG_STACK();
|
|
DEBUG_STACK();
|
|
|
|
|
|
|
|
+ // self value is default to the context where the closure has been created
|
|
|
|
+ gravity_value_t selfvalue = (closure->context) ? VALUE_FROM_OBJECT(closure->context) : sender;
|
|
|
|
+
|
|
|
|
+ // we need a way to give user the ability to access the sender value from a closure
|
|
|
|
+
|
|
// if fiber->nframes is not zero it means that this event has been recursively called
|
|
// if fiber->nframes is not zero it means that this event has been recursively called
|
|
// from somewhere inside the main function so we need to protect and correctly setup
|
|
// from somewhere inside the main function so we need to protect and correctly setup
|
|
// the new activation frame
|
|
// the new activation frame
|