Bromeon/js-sandbox

calling async functions?

rscarson opened this issue · 5 comments

Trying to find a way to call async functions in a script

I tried calling it as 'async nameoffunction' instead of just 'nameoffunction', but that results in await is only valid in async functions and the top level bodies of modules

the .then syntax does not work as the sandbox can't know to wait for it

callbacks also don't seem to be supported.

Any ideas how this can be done today, and if not, I would request a call_async function that awaits the response

Could you elaborate your use case a bit?

The reason why I'm asking is that I'd like to keep this library as simple as possible for its primary use case, as stated in the ReadMe:

This library's primary focus is embedding JS as a scripting language into Rust. It does not provide all possible integrations between the two languages, and is not tailored to JS's biggest domain as a client/server side language of the web.

Instead, js-sandbox focuses on calling standalone JS code from Rust, and tries to remain as simple as possible in doing so.

Not saying that async and callbacks are not useful, but they are probably not as critical as e.g. module support (#2). So to see if this fits the scope, it would be good to understand the exact problem we're trying to solve 🙂

I'm porting a project of mine to rust, and one of the features allows users to extend the functionality. Previously I used python for this, but want to move to JS.

Some of the extensions that I am moving from python to JS use some level of asynchronous code.

Since most of those are FS or network operations, security requirements demand those no longer work, the issue is not immediate - HOWEVER once #2 is available, making use of many modules would need some level of support for JS promises.

Allowing async functions to resolve would I believe be the simplest way to add support.

bump

I'm afraid that for the rest of 2022, I likely won't have much time to work on js-sandbox, as almost all my time goes into godot-rust (maintaining the existing library and working on the next version). So if async functions are something you really need, you may want to look into implementing them yourself, or consider an alternative library to run JS from Rust.

Sorry if this is not the answer that directly solves your issue, but I think it's better to have a realistic outlook, so I hope this helps at least in some regard! 🙂

Implemented in #15.