arcticfox1919 / LuaDardo

A Lua virtual machine written in Dart
Apache License 2.0
174 stars 33 forks source link

[Question] await/async #7

Open blkfry opened 2 years ago

blkfry commented 2 years ago

Is there any way to handle the async nature of Dart? Is there a way to register an async function? Right now you can only pushDarFunction, any plan to support functions that return a future?

arcticfox1919 commented 2 years ago

Since Lua itself does not have such a mechanism, it may be difficult to implement. Do you have any suggestions for implementation?

blkfry commented 2 years ago

Hey, thanks for the reply.

First of all I have zero experience with LUA, I just learned a bit in the last week. So .. my goal is to run specific LUA scripts on the app and register entire Dart classes to the LUA layer. And I have to do this to be compatible to an old Android native app (which already has the scripts created). I cannot change the base architecture of the system since it's a client requirement.

Sadly I couldn't find any package to fit these needs. So I ended up using another package for the LUA side (LuaVM from pub.dev) which supports await/async and to register Dart classes I created a code generator which generates LUA class code based on Dart annotated code that are loaded on the luavm (overall it's still work in progress).

maks commented 1 year ago

@arcticfox1919 I'm looking into this now and I'm not sure about your comment about Lua not having such a mechanism, surely coroutines while not the same have enough similarity to enable using async functionality from Dart?

I do realise that you haven't yet implemented coroutines in LuaDardo, but by adding coroutines I think it would allow for calling in Dart async functions, as long the calling Lua code yields when it does so, the Lua VM could then suspend the coroutine until the Dart async function call returns and then Lua resumes the calling coroutine.

Does that sound like a reasonable approach? If so I can take a go at trying to implement this, though it may take me a while as I'm still learning my way around the LuaDardo internals and I'm actually pretty new to Lua too.

maks commented 1 year ago

As an example, I have a need to Lua code to sleep for a given amount of time without doing a busy wait (eg. using os.clock() and tying up the Dart Isolates thread its running in. Doing this in Dart is trivial using an async function and Future.delayed() but of course at the moment thats not possible as the typedef for DartFunction is sync and returns an int not Future<int>.

But maybe I'm missing and there is a work around for this possible without needing a full coroutine implementation in LuaDardo?

maks commented 1 year ago

So its been a while since this issue and #9 were opened, but I have a PR now: #27 that adds support for calling async Dart functions. It turned out that this really has nothing to do with adding coroutine support, but it does require breaking change to the existing LuaDardo package api.

maks commented 1 year ago

Actually no, it turns out that my attempt in #27 was incomplete and that following through with that approach will just pollute the entire codebase with async/await 😞 so this will need a different approach.