You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When wrapping a native struct in a js object, the struct never seems to be dropped. No matter if the node process crashed or exits gracefully, a impl Dropon a struct that was wrapped never gets called.
I can provide a test case - would be useful though if before the changes from @cztomsik could be pulled in because I worked upon these.
we've decided to go with totally different approach (ffi + messaging) + node-ffi library
- so take any code you want from my fork but I'm too busy to be actively working on this
if you're interested, this is a commit cztomsik/graffiti@3df1bb0 implementing basic poc of such messaging (we're going to use bincode instead of json)
shallow api (just send(Msg) where Msg is like Union of all possible message types) has some benefits:
less binding code (routing is just ordinary rust, it's not specific to node)
theoretically, we can receive messages from anything (ipc, network), not just ffi
it's easy to do cross-cutting thing in one place (logging, profiling)
we wanted to batch multiple calls to one (avoid js <-> rust crossing)
it could be done with napi-rs too, but we would need to introduce arraybuffers and in overall node-ffi felt like better choice (it works with callbacks too which was my original motivation for my own fork of napi-rs)
@Frando Without any further information. If your finalize_cb callback is setup correctly, it’s probably due to some GC optimization. Since when the process exits everything gets freed already and presumably faster by the OS.
If it helps, personally I’ve been messing around with an image processing library, although it doesn’t use this specific library. Heres a snippet using the NAPI extern feature:
pub fn to_extern<T>(env: napi_env, data: T) -> napi_value {
pub unsafe extern "C" fn finalize_value<T>(
env: napi_env,
finalize_data: *mut ::std::os::raw::c_void,
finalize_hint: *mut ::std::os::raw::c_void,
) {
println!("finalize called!");
assert!(!finalize_data.is_null());
let data = Box::from_raw(finalize_data as *mut T);
std::mem::drop(data);
println!("finalize done");
}
unsafe {
let data = Box::new(data);
let data = Box::into_raw(data);
let mut output = std::mem::uninitialized();
let status = napi_create_external(
env,
data as *mut libc::c_void,
Some(finalize_value::<T>),
std::ptr::null_mut(),
&mut output,
);
assert!(status == napi_status_napi_ok);
assert!(!output.is_null());
output
}
}
To test that finalize_value runs correctly, you can invoke node with node --expose_gc …, then in your JS code manually trigger the GC with something like this:
let img = image.open(x);
img = null;
global.gc();
This is the best I’ve figured out so far…
One thing that’s been on my mind. From the docs, I’ve noticed that NAPI exposes a function called napi_adjust_external_memory that quote “will trigger global garbage collections more often than it would otherwise”.
Activity
cztomsik commentedon Feb 20, 2019
Frando commentedon Feb 20, 2019
Thanks for the reply, and interesting approach.
What's the advantage - the codegen from typescript interfaces?
cztomsik commentedon Feb 20, 2019
shallow api (just
send(Msg)whereMsgis like Union of all possible message types) has some benefits:it could be done with
napi-rstoo, but we would need to introduce arraybuffers and in overallnode-ffifelt like better choice (it works with callbacks too which was my original motivation for my own fork ofnapi-rs)colbyn commentedon Oct 13, 2019
@Frando Without any further information. If your
finalize_cbcallback is setup correctly, it’s probably due to some GC optimization. Since when the process exits everything gets freed already and presumably faster by the OS.colbyn commentedon Oct 13, 2019
If it helps, personally I’ve been messing around with an image processing library, although it doesn’t use this specific library. Heres a snippet using the NAPI extern feature:
To test that
finalize_valueruns correctly, you can invoke node withnode --expose_gc …, then in your JS code manually trigger the GC with something like this:This is the best I’ve figured out so far…
One thing that’s been on my mind. From the docs, I’ve noticed that NAPI exposes a function called
napi_adjust_external_memorythat quote “will trigger global garbage collections more often than it would otherwise”.feat(napi): add wrap_and_tag helper + stamp the manual-wrap escape ha…
feat: unforgeable `#[napi]` class identity via Node object type tags (#…