Starlight-JS / starlight

JS engine in Rust
https://teletype.in/@starlight-js
Mozilla Public License 2.0
511 stars 9 forks source link

how does async work / how is async going to work in starlight #48

Closed andrieshiemstra closed 3 years ago

andrieshiemstra commented 3 years ago

Based on what you said in #44 i had a look at function.rs and generator.rs and i don't quite understand how that is going to work async

JavaScript engines typically rely on running in an EventLoop and provide a way to expose jobs which should be added to that loop.

QuickJS has an internal vec of pending jobs accessed by calling JS_IsJobPending() and then JS_ExecutePendingJob() (the downside of this is that you have call those after everything you do with the quickjs runtime).

Spidermonkey has a more elegant solution, you can add a hook (fn/closure) for when an async job has to run (SetJobQueue())

For quickjs I implemented an generic EventLoop here: https://github.com/HiRoFa/utils/blob/master/src/eventloop.rs

For Promises in starlight i was planning to add a simple hook (vm.registerJobAddHookThingy(Fn(Job)) which in turn adds the job to the EventLoop so it wil run async (but in the same thread) but i'm wondering if that's the way you want to go or have an other solution for async jobs...?

playXE commented 3 years ago

Based on what you said in #44 i had a look at function.rs and generator.rs and i don't quite understand how that is going to work async

I was talking on how async function state should be saved/restored and not the actual impl.

I don't actually know what is the best way to implement promises and async fns for JS engine actually. All my experience with coroutines is all in greenie which implements green threads and nothing more.

andrieshiemstra commented 3 years ago

i'll see what I can come up with and get back to you with a suggestion later ok?

playXE commented 3 years ago

That would be awesome! I still haven't finished exams so I do not have much time to work on such complex things like async. :(

andrieshiemstra commented 3 years ago

That would be awesome! I still haven't finished exams so I do not have much time to work on such complex things like async. :(

Well get back to studying then! i don't want to be the one that distracted you from your exams! ;)

Good luck with your exams!

playXE commented 3 years ago

Good luck with your exams!

Thank you!

Btw if you have questions about internals of Starlight I guess GH Discussions is better place. :)