wren
wren copied to clipboard
[RFC] Provide wrenGetCurrentFiber() API
Suggest adding:
void wrenGetCurrentFiber(WrenVM* vm, int slot)
This would take Fiber.current and place a reference to it into the requested slot.
This would be helpful for schedulers in general but more specifically would solve this problem of always needing Fiber.current inside C for callbacks - but never having it... resulting in code like:
Scheduler.await_ { startTimer_(milliseconds, Fiber.current) }
It would be desirable to be able to simplify:
Scheduler.await_ { startTimer_(milliseconds) }
This can be done now fairly easily by accessing private API but would be fragile in the future and subject to breakage:
WrenHandle* getFiberCurrent(WrenVM* vm) {
return wrenMakeHandle(vm, OBJ_VAL(vm->fiber));
}
It's also possible for the Scheduler to just push this information down into C itself:
static await_(fn) {
preserveFiberCurrent_(Fiber.current)
fn.call()
return Scheduler.runNextScheduled_()
}
Having the API already available in C to achieve this seems a much more elegant solution though. If this would be welcome I can create a PR.
Original context:
Another thing we can do, however, is to expose a
wrenGetCurrentFiber()method in the API (because it's useful for schedulers in general).
Originally posted by @ChayimFriedman2 in https://github.com/wren-lang/wren-cli/issues/102#issuecomment-830837805
Just noting that the signature should probably be:
void wrenGetCurrentFiber(WrenVM* vm, int slot)
Trivially correct.
Would a PR for this be welcome?
Bumping this since I could also use it for the same purpose, implementing async/await event loop model like JS in Wren