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

Question: Confused by examples to call JS function from Rust #382

Closed
davedbase opened this issue Dec 27, 2018 · 2 comments
Closed

Question: Confused by examples to call JS function from Rust #382

davedbase opened this issue Dec 27, 2018 · 2 comments

Comments

@davedbase
Copy link

davedbase commented Dec 27, 2018

I'm a bit confused by the example that lets you call a JS function within Rust. Having a heck of a time comprehending the Calling JS functions section located at https://neon-bindings.com/docs/functions. Keep in mind I'm early in my Rust learnings (so bear with me).

I've reviewed the documentation but I'm still VERY fuzzy on how to accomplish my need. What I'd like to do is simply: within JS register a function that Rust can call that processes asynchronously. For example:

class MyLibrary {
    processRequest(jsonValue) {
        console.log(jsonValue);
        return { "success": true };
    }
}

So within rust I want to call MyLibrary.processRequest({ "foo": "bar" });.

The examples unfortunately aren't commented/descriptive enough for me to understand the process of registering the function within Rust and then at some arbitrary point call the function somewhere in my native plugin.

Basically I'm writing an HTTP server binding with Neon, every request that comes into the HTTP server needs to be processed by Node, the response returned and sent back to the user via Rust.

Any suggestions would be helpful!

PS. I don't need to call a method on a class directly, even if I can register a closure and call it on the Rust side, that should be sufficient.

@davedbase davedbase changed the title Question: Confused with example to call JS function from Rust Question: Confused by examples to call JS function from Rust Dec 27, 2018
@kjvalencik
Copy link
Member

kjvalencik commented Dec 27, 2018

Unfortunately, this is not currently possible (#369) since there is no API provided to lock the VM from Rust; it must be initiated by JS. You can work around it by polling (kjvalencik/neon-example@2ef932d).

There are a couple of proposals for implementing this:

If you try the polling approach, keep in mind that this example doesn't include a poll timeout--and it should. If you needed to a create a few instances of this, you would exhaust the libuv threadpool waiting on empty queues.

@davedbase
Copy link
Author

Aha, that makes a heck of a lot of sense. Hoping this gets added as the polling system imposes a dramatic perf limitation for us. I'll have to work it in for now and pray that the Neon team gets something going for it =) thanks for the great work btw.

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