wren icon indicating copy to clipboard operation
wren copied to clipboard

[RFC] Provide wrenGetCurrentFiber() API

Open joshgoebel opened this issue 4 years ago • 4 comments

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

joshgoebel avatar May 02 '21 17:05 joshgoebel

Just noting that the signature should probably be:

void wrenGetCurrentFiber(WrenVM* vm, int slot)

ChayimFriedman2 avatar May 02 '21 17:05 ChayimFriedman2

Trivially correct.

mhermier avatar May 05 '21 06:05 mhermier

Would a PR for this be welcome?

joshgoebel avatar May 18 '21 18:05 joshgoebel

Bumping this since I could also use it for the same purpose, implementing async/await event loop model like JS in Wren

graphitemaster avatar May 30 '21 00:05 graphitemaster