neon-bindings / neon

Rust bindings for writing safe and fast native Node.js modules.
https://www.neon-bindings.com/
Apache License 2.0
8.01k stars 283 forks source link

Having some trouble using rusty_v8 #603

Closed pepsi closed 4 years ago

pepsi commented 4 years ago

As the title says, using rusty_v8 is not working for me, im on Arch linux x86_64, Node v12.18.1, rustc 1.48.0-nightly (fb1dc34a8 2020-09-21) I made a simple project with neon new neonb, Filled it out with a basic skeleton for a discord bot, before adding some rust code to initialize the v8 js engine. it didnt work (Same error as below), so I started stripping away some code, until i was left with just const rusty = require('../native') and

use neon::prelude::*;
use rusty_v8 as v8;

fn init_v8(mut func_contex: FunctionContext) -> JsResult<JsUndefined>{
    Ok(func_contex.undefined())
}

register_module!(mut cx, {
    cx.export_function("init", init_v8)
});

If i remove the use rusty_v8 as v8; the code compiles just fine, but if I keep it, I get the following error on runtime:

#
# Fatal error in ../../../../v8/src/execution/isolate.h, line 556
# Debug check failed: true == isolate_key_created_.load(std::memory_order_relaxed) (1 vs. 0).
#
#
#
#FailureMessage Object: 0x7fff80618db0./.start.sh: line 2: 531776 Aborted                 (core dumped) node -e 'require("./")'

Im not sure if this is a problem, since rusty_v8 is a binding to Chromium's V8 engine, and neon isnt compatable with things of that nature, but any help on this would be greatly appreciated. I can give more information if needed. neon-cli version ^0.4.0 neon-build 0.4.0 neon 0.4.0

kjvalencik commented 4 years ago

Neon is specifically for building modules against node.js and not v8. Neon includes Node header files which in turn include v8 header files. It's likely that these symbol definitions are conflicting.

I am closing this issue because it appears outside the scope of Neon. Please let me know if that's not the case!

pepsi commented 4 years ago

I am building against node.js, Im just testing out a "serverless" type thing for discord bots platform, and for that I need to run some js code. I've found that the easiest way to do that, is with a clean, v8 javscript sandbox. The plan is to have a nodejs script, that calls into a rust script that spawns a v8 instance. If you need further clarification about anything along the way, Ill be glad to explain it :)

kjvalencik commented 4 years ago

I'm not sure if node would support other instances of v8 in the process. There are some globals managed in the threads.

If it is supported, it might be neon-sys that is problematic. Can you try using the napi-runtime backend for Neon? It isn't on feature parity yet, but might help confirm if this direction will work. Add the following to your Cargo.toml:

neon = { version = "0.4.2", default-features = false, features = ["napi-runtime"] } 

An alternative approach is to call into node.js with the EventHandler API and have some glue code in javascript that creates a sandboxed worker to execute the Js.

pepsi commented 4 years ago

Thank you, switching to napi-runtime worked perfectly. Now I can use js to call rust to call js :) Jokes aside, thank you so much for your help :)

kjvalencik commented 4 years ago

Glad I could help! That means the issues may have to do with linking. There a few caveats that likely apply:

pepsi commented 4 years ago

Who needs windows support anyway? And the other stuff, I should be able to work around it, so no problems there