Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to deal with callables that are async in nature? #1105

Open
thumbert opened this issue Jan 28, 2023 · 1 comment
Open

How to deal with callables that are async in nature? #1105

thumbert opened this issue Jan 28, 2023 · 1 comment

Comments

@thumbert
Copy link

Hi,

I don't see a clear way to implement callables that are async. In my case I'm using a Dart implementation and I'm even willing to restrict the async functions to the globals (so all are predefined), say to get some data from a database. However, the visitCallExpr will return a Future that I can't await properly to preserve the sequential nature of a Lox script.

For example, a global async function:

    globals.define(
      'get42',
      JuiceCallable(0, (interpreter, arguments) {
        return Future.delayed(Duration(seconds: 5), () => 42);
      }),
    );

called from the Lox script

var x = get42();
print x;

will print Instance of Future<int>, etc.

If you can share any tips for a possible implementation it would be much appreciated.

Thank you!

@ratchetfreak
Copy link

if you want the async nature of the call to be hidden from lox then you will need to implement some coroutine functionality

So you will need to be able to yield from the interpreter where the stack gets preserved and restored on the next resume.

Or you can use the built in async/await you need to detect whether the return of a function is a Future and instead of returning it you need to await it (and make the entire interpreter callstack stack up to that point async)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants