Allow context switch from C using `mrb_fiber_resume()`.
But you still cannot cross C function boundary.
MODIFY
mrbgems/mruby-fiber/src/fiber.c
mrbgems/mruby-fiber/src/fiber.c
@@ -219,8 +219,11 @@ fiber_switch(mrb_state *mrb, mrb_value self, mrb_int len, const mrb_value *a, mr
while (b<e) {
*b++ = *a++;
}
+ if (vmexec) {
+ c->ci--; /* pop dummy callinfo */
+ }
c->cibase->argc = (int)len;
- value = c->stack[0] = MRB_PROC_ENV(c->ci->proc)->stack[0];
+ value = c->stack[0] = MRB_PROC_ENV(c->cibase->proc)->stack[0];
}
else {
value = fiber_result(mrb, a, len);
@@ -228,7 +231,7 @@ fiber_switch(mrb_state *mrb, mrb_value self, mrb_int len, const mrb_value *a, mr
if (vmexec) {
c->vmexec = TRUE;
- value = mrb_vm_exec(mrb, c->ci[-1].proc, c->ci->pc);
+ value = mrb_vm_exec(mrb, c->ci->proc, c->ci->pc);
mrb->c = old_c;
}
else {
@@ -354,6 +357,7 @@ mrb_fiber_yield(mrb_state *mrb, mrb_int len, const mrb_value *a)
if (c->vmexec) {
c->vmexec = FALSE;
mrb->c->ci->acc = CI_ACC_RESUMED;
+ c->ci--; /* pop callinfo for yield */
}
MARK_CONTEXT_MODIFY(mrb->c);
return fiber_result(mrb, a, len);

MODIFY
src/vm.c
src/vm.c
@@ -2074,7 +2074,7 @@ RETRY_TRY_BLOCK:
regs[irep->nlocals] = v;
goto CHECKPOINT_LABEL_MAKE(RBREAK_TAG_STOP);
}
- if (c->prev->ci == c->prev->cibase) {
+ if (!c->vmexec && c->prev->ci == c->prev->cibase) {
mrb_value exc = mrb_exc_new_lit(mrb, E_FIBER_ERROR, "double resume");
mrb_exc_set(mrb, exc);
goto L_RAISE;
@@ -2089,8 +2089,14 @@ RETRY_TRY_BLOCK:
/* automatic yield at the end */
c->status = MRB_FIBER_TERMINATED;
mrb->c = c->prev;
- c->prev = NULL;
mrb->c->status = MRB_FIBER_RUNNING;
+ c->prev = NULL;
+ if (c->vmexec) {
+ mrb_gc_arena_restore(mrb, ai);
+ c->vmexec = FALSE;
+ mrb->jmp = prev_jmp;
+ return v;
+ }
ci = mrb->c->ci;
}
CHECKPOINT_RESTORE(RBREAK_TAG_RETURN) {

Allow context switch from C using `mrb_fiber_resume()`.

But you still cannot cross C function boundary.