From 309bc50489668ea3f79baa7fb1c94adc5bf1a50f Mon Sep 17 00:00:00 2001 From: Sehyo Chang Date: Tue, 26 Sep 2023 01:13:31 +0000 Subject: [PATCH] fix: misalignment crash (#259) resolves #255 The underlying issue was that the reference to wrap was removed just after the constructor was called. The code was there probably due to a workaround bug in the previous version of node. May need to clean up reference to wrap when finalizer is invoked --- .github/workflows/ci.yml | 2 +- Cargo.lock | 109 +++++++++++------- examples/Cargo.lock | 162 ++++++++++++--------------- examples/Cargo.toml | 3 +- examples/async-cb/Cargo.toml | 2 +- examples/async-cb/src/lib.rs | 2 +- examples/class-simple/test.js | 1 + examples/js-env/src/lib.rs | 1 - examples/json/src/lib.rs | 28 ++--- examples/logging/Cargo.toml | 3 +- examples/logging/src/lib.rs | 6 +- nj-core/src/basic.rs | 34 ++++-- nj-core/src/class.rs | 41 +++---- nj-core/src/lib.rs | 2 +- nj-core/src/property.rs | 2 +- nj-derive/src/generator/class/mod.rs | 3 +- nj-derive/src/generator/function.rs | 2 + nj-derive/src/generator/napi.rs | 2 + 18 files changed, 210 insertions(+), 195 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8a71032f..c514e710 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -60,7 +60,7 @@ jobs: fail-fast: false matrix: # os: [ubuntu-latest, windows-latest, macOS-latest] - os: [ubuntu-latest, macOS-latest] + os: [ubuntu-latest, macOS-13] rust: [stable] node-version: [ '16', '18', '20' ] steps: diff --git a/Cargo.lock b/Cargo.lock index a23052a7..d0376212 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -28,20 +28,20 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "81953c529336010edd6d8e358f886d9581267795c61b19475b71314bffa46d35" dependencies = [ "concurrent-queue", - "event-listener", + "event-listener 2.5.3", "futures-core", ] [[package]] name = "async-executor" -version = "1.5.1" +version = "1.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6fa3dc5f2a8564f07759c008b9109dc0d39de92a88d5588b8a5036d286383afb" +checksum = "78f2db9467baa66a700abce2a18c5ad793f6f83310aca1284796fc3921d113fd" dependencies = [ "async-lock", "async-task", "concurrent-queue", - "fastrand 1.9.0", + "fastrand 2.0.1", "futures-lite", "slab", ] @@ -87,24 +87,42 @@ version = "2.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "287272293e9d8c41773cec55e365490fe034813a2f172f502d6ddcf75b2f582b" dependencies = [ - "event-listener", + "event-listener 2.5.3", ] [[package]] name = "async-process" -version = "1.7.0" +version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a9d28b1d97e08915212e2e45310d47854eafa69600756fc735fb788f75199c9" +checksum = "bf012553ce51eb7aa6dc2143804cc8252bd1cb681a1c5cb7fa94ca88682dee1d" dependencies = [ "async-io", "async-lock", - "autocfg", + "async-signal", "blocking", "cfg-if", - "event-listener", + "event-listener 3.0.0", "futures-lite", - "rustix 0.37.23", - "signal-hook", + "rustix 0.38.14", + "windows-sys", +] + +[[package]] +name = "async-signal" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4af361a844928cb7d36590d406709473a1b574f443094422ef166daa3b493208" +dependencies = [ + "async-io", + "async-lock", + "atomic-waker", + "cfg-if", + "concurrent-queue", + "futures-core", + "futures-io", + "libc", + "signal-hook-registry", + "slab", "windows-sys", ] @@ -137,9 +155,9 @@ dependencies = [ [[package]] name = "async-task" -version = "4.4.0" +version = "4.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ecc7ab41815b3c653ccd2978ec3255c81349336702dfdf62ee6f7069b12a3aae" +checksum = "b9441c6b2fe128a7c2bf680a44c34d0df31ce09e5b7e401fcca3faa483dbc921" [[package]] name = "async-trait" @@ -165,9 +183,9 @@ dependencies = [ [[package]] name = "atomic-waker" -version = "1.1.1" +version = "1.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1181e1e0d1fce796a03db1ae795d67167da795f9cf4a39c37589e85ef57f26d3" +checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" [[package]] name = "autocfg" @@ -189,17 +207,18 @@ checksum = "b4682ae6287fcf752ecaabbfcc7b6f9b72aa33933dc23a554d853aea8eea8635" [[package]] name = "blocking" -version = "1.3.1" +version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "77231a1c8f801696fc0123ec6150ce92cffb8e164a02afb9c8ddee0e9b65ad65" +checksum = "94c4ef1f913d78636d78d538eec1f18de81e481f44b1be0a81060090530846e1" dependencies = [ "async-channel", "async-lock", "async-task", - "atomic-waker", - "fastrand 1.9.0", + "fastrand 2.0.1", + "futures-io", "futures-lite", - "log", + "piper", + "tracing", ] [[package]] @@ -268,9 +287,9 @@ dependencies = [ [[package]] name = "concurrent-queue" -version = "2.2.0" +version = "2.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "62ec6771ecfa0762d24683ee5a32ad78487a3d3afdc0fb8cae19d2c5deb50b7c" +checksum = "f057a694a54f12365049b0958a1685bb52d567f5593b355fbf685838e873d400" dependencies = [ "crossbeam-utils", ] @@ -353,6 +372,17 @@ version = "2.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0206175f82b8d6bf6652ff7d71a1e27fd2e4efde587fd368662814d6ec1d9ce0" +[[package]] +name = "event-listener" +version = "3.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29e56284f00d94c1bc7fd3c77027b4623c88c1f53d8d2394c6199f2921dea325" +dependencies = [ + "concurrent-queue", + "parking", + "pin-project-lite", +] + [[package]] name = "fastrand" version = "1.9.0" @@ -364,9 +394,9 @@ dependencies = [ [[package]] name = "fastrand" -version = "2.0.0" +version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6999dc1837253364c2ebb0704ba97994bd874e8f195d665c50b7548f6ea92764" +checksum = "25cbce373ec4653f1a01a31e8a5e5ec0c622dc27ff9c4e6606eefef5cbbed4a5" [[package]] name = "fluvio-future" @@ -898,9 +928,9 @@ checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39" [[package]] name = "parking" -version = "2.1.0" +version = "2.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "14f2252c834a40ed9bb5422029649578e63aa341ac401f74e719dd1afda8394e" +checksum = "e52c774a4c39359c1d1c52e43f73dd91a75a614652c825408eec30c95a9b2067" [[package]] name = "parking_lot" @@ -969,6 +999,17 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" +[[package]] +name = "piper" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "668d31b1c4eba19242f2088b2bf3316b82ca31082a8335764db4e083db7485d4" +dependencies = [ + "atomic-waker", + "fastrand 2.0.1", + "futures-io", +] + [[package]] name = "pkg-config" version = "0.3.27" @@ -1239,16 +1280,6 @@ dependencies = [ "lazy_static", ] -[[package]] -name = "signal-hook" -version = "0.3.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8621587d4798caf8eb44879d42e56b9a93ea5dcd315a6487c357130095b62801" -dependencies = [ - "libc", - "signal-hook-registry", -] - [[package]] name = "signal-hook-registry" version = "1.4.1" @@ -1336,7 +1367,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cb94d2f3cc536af71caac6b6fcebf65860b347e7ce0cc9ebe8f70d3e521054ef" dependencies = [ "cfg-if", - "fastrand 2.0.0", + "fastrand 2.0.1", "redox_syscall 0.3.5", "rustix 0.38.14", "windows-sys", @@ -1567,9 +1598,9 @@ checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" [[package]] name = "waker-fn" -version = "1.1.0" +version = "1.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9d5b2c62b4012a3e1eca5a7e077d13b3bf498c4073e33ccd58626607748ceeca" +checksum = "f3c4517f54858c779bbcbf228f4fca63d121bf85fbecb2dc578cdf4a39395690" [[package]] name = "wasm-bindgen" diff --git a/examples/Cargo.lock b/examples/Cargo.lock index 70beba9e..13e31f2c 100644 --- a/examples/Cargo.lock +++ b/examples/Cargo.lock @@ -28,20 +28,20 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "81953c529336010edd6d8e358f886d9581267795c61b19475b71314bffa46d35" dependencies = [ "concurrent-queue", - "event-listener", + "event-listener 2.5.3", "futures-core", ] [[package]] name = "async-executor" -version = "1.5.1" +version = "1.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6fa3dc5f2a8564f07759c008b9109dc0d39de92a88d5588b8a5036d286383afb" +checksum = "78f2db9467baa66a700abce2a18c5ad793f6f83310aca1284796fc3921d113fd" dependencies = [ "async-lock", "async-task", "concurrent-queue", - "fastrand 1.9.0", + "fastrand 2.0.1", "futures-lite", "slab", ] @@ -87,24 +87,42 @@ version = "2.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "287272293e9d8c41773cec55e365490fe034813a2f172f502d6ddcf75b2f582b" dependencies = [ - "event-listener", + "event-listener 2.5.3", ] [[package]] name = "async-process" -version = "1.7.0" +version = "1.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a9d28b1d97e08915212e2e45310d47854eafa69600756fc735fb788f75199c9" +checksum = "bf012553ce51eb7aa6dc2143804cc8252bd1cb681a1c5cb7fa94ca88682dee1d" dependencies = [ "async-io", "async-lock", - "autocfg", + "async-signal", "blocking", "cfg-if", - "event-listener", + "event-listener 3.0.0", "futures-lite", - "rustix 0.37.23", - "signal-hook", + "rustix 0.38.14", + "windows-sys", +] + +[[package]] +name = "async-signal" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4af361a844928cb7d36590d406709473a1b574f443094422ef166daa3b493208" +dependencies = [ + "async-io", + "async-lock", + "atomic-waker", + "cfg-if", + "concurrent-queue", + "futures-core", + "futures-io", + "libc", + "signal-hook-registry", + "slab", "windows-sys", ] @@ -137,9 +155,9 @@ dependencies = [ [[package]] name = "async-task" -version = "4.4.0" +version = "4.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ecc7ab41815b3c653ccd2978ec3255c81349336702dfdf62ee6f7069b12a3aae" +checksum = "b9441c6b2fe128a7c2bf680a44c34d0df31ce09e5b7e401fcca3faa483dbc921" [[package]] name = "async-trait" @@ -165,9 +183,9 @@ dependencies = [ [[package]] name = "atomic-waker" -version = "1.1.1" +version = "1.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1181e1e0d1fce796a03db1ae795d67167da795f9cf4a39c37589e85ef57f26d3" +checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" [[package]] name = "autocfg" @@ -189,17 +207,18 @@ checksum = "b4682ae6287fcf752ecaabbfcc7b6f9b72aa33933dc23a554d853aea8eea8635" [[package]] name = "blocking" -version = "1.3.1" +version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "77231a1c8f801696fc0123ec6150ce92cffb8e164a02afb9c8ddee0e9b65ad65" +checksum = "94c4ef1f913d78636d78d538eec1f18de81e481f44b1be0a81060090530846e1" dependencies = [ "async-channel", "async-lock", "async-task", - "atomic-waker", - "fastrand 1.9.0", + "fastrand 2.0.1", + "futures-io", "futures-lite", - "log", + "piper", + "tracing", ] [[package]] @@ -225,9 +244,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "concurrent-queue" -version = "2.2.0" +version = "2.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "62ec6771ecfa0762d24683ee5a32ad78487a3d3afdc0fb8cae19d2c5deb50b7c" +checksum = "f057a694a54f12365049b0958a1685bb52d567f5593b355fbf685838e873d400" dependencies = [ "crossbeam-utils", ] @@ -277,19 +296,6 @@ dependencies = [ "syn 2.0.37", ] -[[package]] -name = "env_logger" -version = "0.10.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "85cdab6a89accf66733ad5a1693a4dcced6aeff64602b634530dd73c1f3ee9f0" -dependencies = [ - "humantime", - "is-terminal", - "log", - "regex", - "termcolor", -] - [[package]] name = "errno" version = "0.3.3" @@ -317,6 +323,17 @@ version = "2.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0206175f82b8d6bf6652ff7d71a1e27fd2e4efde587fd368662814d6ec1d9ce0" +[[package]] +name = "event-listener" +version = "3.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29e56284f00d94c1bc7fd3c77027b4623c88c1f53d8d2394c6199f2921dea325" +dependencies = [ + "concurrent-queue", + "parking", + "pin-project-lite", +] + [[package]] name = "fastrand" version = "1.9.0" @@ -328,9 +345,9 @@ dependencies = [ [[package]] name = "fastrand" -version = "2.0.0" +version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6999dc1837253364c2ebb0704ba97994bd874e8f195d665c50b7548f6ea92764" +checksum = "25cbce373ec4653f1a01a31e8a5e5ec0c622dc27ff9c4e6606eefef5cbbed4a5" [[package]] name = "fluvio-future" @@ -524,12 +541,6 @@ dependencies = [ "unicase", ] -[[package]] -name = "humantime" -version = "2.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" - [[package]] name = "instant" version = "0.1.12" @@ -575,17 +586,6 @@ dependencies = [ "windows-sys", ] -[[package]] -name = "is-terminal" -version = "0.4.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cb0889898416213fab133e1d33a0e5858a48177452750691bde3666d0fdbaf8b" -dependencies = [ - "hermit-abi", - "rustix 0.38.14", - "windows-sys", -] - [[package]] name = "itoa" version = "1.0.9" @@ -734,8 +734,8 @@ name = "nj-example-async-cb" version = "0.0.0" dependencies = [ "fluvio-future", - "log", "node-bindgen", + "tracing", ] [[package]] @@ -816,9 +816,8 @@ dependencies = [ name = "nj-example-logging" version = "0.0.0" dependencies = [ - "env_logger", - "log", "node-bindgen", + "tracing", ] [[package]] @@ -972,9 +971,9 @@ checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39" [[package]] name = "parking" -version = "2.1.0" +version = "2.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "14f2252c834a40ed9bb5422029649578e63aa341ac401f74e719dd1afda8394e" +checksum = "e52c774a4c39359c1d1c52e43f73dd91a75a614652c825408eec30c95a9b2067" [[package]] name = "parking_lot" @@ -1043,6 +1042,17 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" +[[package]] +name = "piper" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "668d31b1c4eba19242f2088b2bf3316b82ca31082a8335764db4e083db7485d4" +dependencies = [ + "atomic-waker", + "fastrand 2.0.1", + "futures-io", +] + [[package]] name = "pkg-config" version = "0.3.27" @@ -1277,16 +1287,6 @@ dependencies = [ "lazy_static", ] -[[package]] -name = "signal-hook" -version = "0.3.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8621587d4798caf8eb44879d42e56b9a93ea5dcd315a6487c357130095b62801" -dependencies = [ - "libc", - "signal-hook-registry", -] - [[package]] name = "signal-hook-registry" version = "1.4.1" @@ -1350,21 +1350,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cb94d2f3cc536af71caac6b6fcebf65860b347e7ce0cc9ebe8f70d3e521054ef" dependencies = [ "cfg-if", - "fastrand 2.0.0", + "fastrand 2.0.1", "redox_syscall 0.3.5", "rustix 0.38.14", "windows-sys", ] -[[package]] -name = "termcolor" -version = "1.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6093bad37da69aab9d123a8091e4be0aa4a03e4d601ec641c327398315f62b64" -dependencies = [ - "winapi-util", -] - [[package]] name = "test-electron" version = "0.0.0" @@ -1511,9 +1502,9 @@ checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" [[package]] name = "waker-fn" -version = "1.1.0" +version = "1.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9d5b2c62b4012a3e1eca5a7e077d13b3bf498c4073e33ccd58626607748ceeca" +checksum = "f3c4517f54858c779bbcbf228f4fca63d121bf85fbecb2dc578cdf4a39395690" [[package]] name = "wasm-bindgen" @@ -1607,15 +1598,6 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" -[[package]] -name = "winapi-util" -version = "0.1.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f29e6f9198ba0d26b4c9f07dbe6f9ed633e1f3d5b8b414090084349e46a52596" -dependencies = [ - "winapi", -] - [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" diff --git a/examples/Cargo.toml b/examples/Cargo.toml index 2392f2dd..c6dffba5 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -25,12 +25,11 @@ members = [ [workspace.dependencies] -log = "0.4.8" -env_logger = "0.10.0" serde = { version = "1.0.110", features = ["derive"] } serde_json = "1.0.53" futures-lite = "1.7.0" uuid = "1.1.0" +tracing = "0.1.37" node-bindgen = { path = "..", features = ["default"]} fluvio-future = { version = "0.6.0", features = ["timer"] } \ No newline at end of file diff --git a/examples/async-cb/Cargo.toml b/examples/async-cb/Cargo.toml index fa108134..53b88b49 100644 --- a/examples/async-cb/Cargo.toml +++ b/examples/async-cb/Cargo.toml @@ -9,7 +9,7 @@ publish = false crate-type = ["cdylib"] [dependencies] -log = { workspace = true } +tracing = { workspace = true } node-bindgen = { workspace = true} fluvio-future = { workspace = true} diff --git a/examples/async-cb/src/lib.rs b/examples/async-cb/src/lib.rs index f4c9c9d7..3c6ba052 100644 --- a/examples/async-cb/src/lib.rs +++ b/examples/async-cb/src/lib.rs @@ -16,4 +16,4 @@ async fn hello(seconds: i32, cb: F) { // println!("woke from time"); cb(10.0, "hello world".to_string()); -} \ No newline at end of file +} diff --git a/examples/class-simple/test.js b/examples/class-simple/test.js index 2066356c..bb508617 100644 --- a/examples/class-simple/test.js +++ b/examples/class-simple/test.js @@ -7,6 +7,7 @@ let obj = new addon.MyObject(10,2); assert.equal(obj.value,10,"verify value works"); assert.equal(obj.plusOne(),11); assert.equal(obj.value2,2); +//console.print("first"); obj.changeValue(100); assert.equal(obj.value,100); diff --git a/examples/js-env/src/lib.rs b/examples/js-env/src/lib.rs index e15d28b2..9497a368 100644 --- a/examples/js-env/src/lib.rs +++ b/examples/js-env/src/lib.rs @@ -21,4 +21,3 @@ impl TryIntoJs for EnvInterceptor { js_env.create_double(self.0 * 2.0) } } - diff --git a/examples/json/src/lib.rs b/examples/json/src/lib.rs index c9e69020..384e562d 100644 --- a/examples/json/src/lib.rs +++ b/examples/json/src/lib.rs @@ -12,12 +12,12 @@ use serde_json::map::Map; #[node_bindgen] struct StandardJson { some_name: String, - a_number: i64 + a_number: i64, } #[node_bindgen] struct Outer { - val: Inner + val: Inner, } #[node_bindgen] @@ -29,19 +29,17 @@ struct UnitStruct; #[node_bindgen] enum ErrorType { WithMessage(String, usize), - WithFields { - val: usize - }, - UnitError + WithFields { val: usize }, + UnitError, } #[node_bindgen] struct WithSerdeJson { - val: Value + val: Value, } struct CustomJson { - val: f64 + val: f64, } impl TryIntoJs for CustomJson { @@ -67,14 +65,14 @@ fn custom_json() -> CustomJson { fn standard_json() -> StandardJson { StandardJson { some_name: "John".to_owned(), - a_number: 1337 + a_number: 1337, } } #[node_bindgen] fn multilevel_json() -> Outer { Outer { - val: Inner("hello".to_owned()) + val: Inner("hello".to_owned()), } } @@ -90,9 +88,7 @@ fn with_message() -> ErrorType { #[node_bindgen] fn with_fields() -> ErrorType { - ErrorType::WithFields { - val: 123 - } + ErrorType::WithFields { val: 123 } } #[node_bindgen] @@ -102,9 +98,7 @@ fn with_unit() -> ErrorType { #[node_bindgen] fn failed_result_with_fields() -> Result<(), ErrorType> { - Err(ErrorType::WithFields { - val: 987 - }) + Err(ErrorType::WithFields { val: 987 }) } #[node_bindgen] @@ -119,6 +113,6 @@ fn with_serde_json() -> WithSerdeJson { map.insert("second".to_owned(), Value::String("hello".to_owned())); WithSerdeJson { - val: Value::Object(map) + val: Value::Object(map), } } diff --git a/examples/logging/Cargo.toml b/examples/logging/Cargo.toml index cff57109..6035d6a0 100644 --- a/examples/logging/Cargo.toml +++ b/examples/logging/Cargo.toml @@ -12,8 +12,7 @@ crate-type = ["cdylib"] [dependencies] node-bindgen = { workspace = true} -log = { workspace = true } -env_logger = { workspace = true } +tracing = { workspace = true } [build-dependencies] node-bindgen = { path = "../../", default-features = false, features = ["build"] } diff --git a/examples/logging/src/lib.rs b/examples/logging/src/lib.rs index d4b6cfcc..e5ffead1 100644 --- a/examples/logging/src/lib.rs +++ b/examples/logging/src/lib.rs @@ -1,11 +1,13 @@ +use tracing::{info, warn}; + use node_bindgen::derive::node_bindgen; use node_bindgen::init::node_bindgen_init_once; -use log::{info, warn}; + #[node_bindgen_init_once] fn init_logging() { // initialize logging framework - env_logger::init(); + // logging is initialized already info!("logging initialized"); } diff --git a/nj-core/src/basic.rs b/nj-core/src/basic.rs index 2fa59b41..5196fc1a 100644 --- a/nj-core/src/basic.rs +++ b/nj-core/src/basic.rs @@ -264,6 +264,7 @@ impl JsEnv { /// get callback information #[allow(clippy::not_unsafe_ptr_arg_deref)] + #[instrument] pub fn get_cb_info( &self, info: napi_callback_info, @@ -284,6 +285,7 @@ impl JsEnv { ptr::null_mut() ))?; + trace!(argc, "actual argc"); // truncate arg to actual received count args.resize(argc, ptr::null_mut()); @@ -291,20 +293,16 @@ impl JsEnv { } /// define classes + #[instrument(skip(properties))] pub fn define_class( &self, name: &str, constructor: napi_callback_raw, properties: PropertiesBuilder, ) -> Result { + debug!(?properties, "defining class",); let mut js_constructor = ptr::null_mut(); let mut raw_properties = properties.as_raw_properties(); - - debug!( - "defining class: {} with {} properties", - name, - raw_properties.len() - ); napi_call_result!(crate::sys::napi_define_class( self.0, name.as_ptr() as *const ::std::os::raw::c_char, @@ -340,14 +338,16 @@ impl JsEnv { } #[allow(clippy::not_unsafe_ptr_arg_deref)] + #[instrument] pub fn get_new_target(&self, info: napi_callback_info) -> Result { let mut result = ptr::null_mut(); napi_call_result!(crate::sys::napi_get_new_target(self.0, info, &mut result))?; - + debug!(?result, "got new target"); Ok(result) } #[allow(clippy::not_unsafe_ptr_arg_deref)] + #[instrument] pub fn wrap( &self, js_object: napi_value, @@ -356,6 +356,7 @@ impl JsEnv { ) -> Result { let mut result = ptr::null_mut(); + debug!("napi wrap"); napi_call_result!(crate::sys::napi_wrap( self.0, js_object, @@ -369,23 +370,34 @@ impl JsEnv { } #[allow(clippy::not_unsafe_ptr_arg_deref)] + #[instrument] pub fn unwrap(&self, js_this: napi_value) -> Result<&'static T, NjError> { let mut result: *mut ::std::os::raw::c_void = ptr::null_mut(); napi_call_result!(crate::sys::napi_unwrap(self.0, js_this, &mut result))?; Ok(unsafe { + debug!(?result, "got back raw pointer"); + if result.is_null() { + return Err(NjError::Other("unwrap got null pointer".to_string())); + } let rust_ref: &T = &mut *(result as *mut T); rust_ref }) } #[allow(clippy::not_unsafe_ptr_arg_deref)] + #[instrument] pub fn unwrap_mut(&self, js_this: napi_value) -> Result<&'static mut T, NjError> { let mut result: *mut ::std::os::raw::c_void = ptr::null_mut(); + debug!(env = ?self.0,"napi unwrap"); napi_call_result!(crate::sys::napi_unwrap(self.0, js_this, &mut result))?; - Ok(unsafe { - let rust_ref: &mut T = &mut *(result as *mut T); + debug!(?result, "got back raw pointer"); + if result.is_null() { + return Err(NjError::Other("unwrap mut null pointer".to_string())); + } + let ptr = result as *mut T; + let rust_ref: &mut T = &mut *(ptr); rust_ref }) } @@ -396,7 +408,7 @@ impl JsEnv { constructor: napi_value, mut args: Vec, ) -> Result { - trace!("napi new instance: {}", args.len()); + trace!(args = args.len(), "napi new instance"); let mut result = ptr::null_mut(); napi_call_result!(crate::sys::napi_new_instance( self.0, @@ -760,6 +772,7 @@ impl JsCallback { } } + #[instrument] pub fn unwrap_mut(&self) -> Result<&'static mut T, NjError> { Ok(self .env @@ -886,6 +899,7 @@ impl ExtractArgFromJs<'_> for JsEnv { } } +#[derive(Debug)] pub struct JsExports { inner: napi_value, env: JsEnv, diff --git a/nj-core/src/class.rs b/nj-core/src/class.rs index 6af7ef69..a2319681 100644 --- a/nj-core/src/class.rs +++ b/nj-core/src/class.rs @@ -1,6 +1,7 @@ use std::ptr; use tracing::debug; +use tracing::instrument; use crate::sys::napi_value; use crate::sys::napi_env; @@ -34,10 +35,11 @@ where { /// wrap myself in the JS instance /// and saved the reference + #[instrument(skip(self))] fn wrap(self, js_env: &JsEnv, js_cb: JsCallback) -> Result { let boxed_self = Box::new(self); let raw_ptr = Box::into_raw(boxed_self); // rust no longer manages this struct - + debug!(?raw_ptr, "box into raw"); let wrap = js_env.wrap(js_cb.this(), raw_ptr as *mut u8, T::js_finalize)?; unsafe { @@ -46,15 +48,6 @@ where rust_ref.wrapper = wrap; } - // Finally, remove the reference in response to finalize callback - // See footnote on `napi_wrap` documentation: https://nodejs.org/api/n-api.html#n_api_napi_wrap - // - // "Caution: The optional returned reference (if obtained) should be deleted via napi_delete_reference - // ONLY in response to the finalize callback invocation. If it is deleted before then, - // then the finalize callback may never be invoked. Therefore, when obtaining a reference a - // finalize callback is also required in order to enable correct disposal of the reference." - js_env.delete_reference(wrap)?; - Ok(js_cb.this_owned()) } } @@ -73,13 +66,15 @@ pub trait JSClass: Sized { fn get_constructor() -> napi_ref; /// new instance + #[instrument] fn new_instance(js_env: &JsEnv, js_args: Vec) -> Result { - debug!("new instance with args: {:#?}", js_args); + debug!("new instance"); let constructor = js_env.get_reference_value(Self::get_constructor())?; js_env.new_instance(constructor, js_args) } /// given instance, return my object + #[instrument] fn unwrap_mut(js_env: &JsEnv, instance: napi_value) -> Result<&'static mut Self, NjError> { Ok(js_env .unwrap_mut::>(instance)? @@ -95,14 +90,18 @@ pub trait JSClass: Sized { } /// define class and properties under exports + #[instrument] fn js_init(js_exports: &mut JsExports) -> Result<(), NjError> { let js_constructor = js_exports .env() .define_class(Self::CLASS_NAME, Self::js_new, Self::properties())?; + debug!(?js_constructor, "class defined with constructor"); + // save the constructor reference, we need this later in order to instantiate let js_ref = js_exports.env().create_reference(js_constructor, 1)?; + debug!(?js_constructor, "created reference to constructor"); Self::set_constructor(js_ref); js_exports.set_name_property(Self::CLASS_NAME, js_constructor)?; @@ -111,23 +110,23 @@ pub trait JSClass: Sized { /// call when Javascript class constructor is called /// For example: new Car(...) + #[instrument] extern "C" fn js_new(env: napi_env, info: napi_callback_info) -> napi_value { let js_env = JsEnv::new(env); let result: Result = (|| { - debug!( - "Class constructor called: {:#?}", - std::any::type_name::() - ); + debug!(clas = std::any::type_name::(), "getting new target"); let target = js_env.get_new_target(info)?; if target.is_null() { + debug!("no target"); Err(NjError::NoPlainConstructor) } else { - debug!("invoked as constructor"); + debug!(?target, "invoked as constructor"); let (rust_obj, js_cb) = Self::create_from_js(&js_env, info)?; + debug!(?js_cb, "created rust object"); let my_obj = JSObjectWrapper { inner: rust_obj, wrapper: ptr::null_mut(), @@ -140,16 +139,6 @@ pub trait JSClass: Sized { result.into_js(&js_env) } - /* - /// convert my self as JS object - fn as_js_instance(self,js_env: &JsEnv,js_args: Vec) -> napi_value { - - let new_instance = Self::new_instance(js_env,args); - - // unwrap the actual inner - } - */ - extern "C" fn js_finalize( _env: napi_env, finalize_data: *mut ::std::os::raw::c_void, diff --git a/nj-core/src/lib.rs b/nj-core/src/lib.rs index 36e00328..a2bc037c 100644 --- a/nj-core/src/lib.rs +++ b/nj-core/src/lib.rs @@ -41,7 +41,7 @@ pub mod val { } pub mod log { - pub use ::tracing::*; + pub use tracing::*; } /// call napi and assert diff --git a/nj-core/src/property.rs b/nj-core/src/property.rs index bb9f4dcb..29e27c46 100644 --- a/nj-core/src/property.rs +++ b/nj-core/src/property.rs @@ -53,7 +53,7 @@ impl Property { } } -#[derive(Default)] +#[derive(Default, Debug)] pub struct PropertiesBuilder(Vec); impl PropertiesBuilder { diff --git a/nj-derive/src/generator/class/mod.rs b/nj-derive/src/generator/class/mod.rs index ae2b6a58..3956de55 100644 --- a/nj-derive/src/generator/class/mod.rs +++ b/nj-derive/src/generator/class/mod.rs @@ -68,8 +68,8 @@ fn generate_class_helper(class: Class) -> TokenStream { impl node_bindgen::core::JSClass for #impl_for_block { const CLASS_NAME: &'static str = #class_type_lit; - fn set_constructor(constructor: node_bindgen::sys::napi_ref) { + node_bindgen::core::log::trace!("set constructor"); unsafe { CLASS_CONSTRUCTOR = constructor; } @@ -97,6 +97,7 @@ fn generate_class_helper(class: Class) -> TokenStream { #[node_bindgen::core::ctor] fn register_class() { + node_bindgen::core::log::debug!(class = stringify!(#type_name),"registering class"); submit_register_callback(#type_name::js_init); } } diff --git a/nj-derive/src/generator/function.rs b/nj-derive/src/generator/function.rs index 0b288039..70ce8b57 100644 --- a/nj-derive/src/generator/function.rs +++ b/nj-derive/src/generator/function.rs @@ -97,6 +97,7 @@ pub fn generate_rust_invocation(ctx: &FnGeneratorCtx, cb_args: &mut CbArgs) -> T let async_name = format!("{}_ft", ctx.fn_name()); let async_lit = LitStr::new(&async_name, Span::call_site()); quote! { + node_bindgen::core::log::debug!("creating JSPromiseFuture"); (node_bindgen::core::JsPromiseFuture::new( #rust_invoke, #async_lit )).try_to_js(&js_env) @@ -109,6 +110,7 @@ pub fn generate_rust_invocation(ctx: &FnGeneratorCtx, cb_args: &mut CbArgs) -> T let receiver = if ctx.is_method() { quote! { + node_bindgen::core::log::debug!("unwrapping receiver for method"); let receiver = (js_cb.unwrap_mut::()?); } } else { diff --git a/nj-derive/src/generator/napi.rs b/nj-derive/src/generator/napi.rs index 187c013a..316feddf 100644 --- a/nj-derive/src/generator/napi.rs +++ b/nj-derive/src/generator/napi.rs @@ -50,6 +50,8 @@ fn raw_napi_function_template( use node_bindgen::core::IntoJs; use node_bindgen::core::val::JsCallbackFunction; + node_bindgen::core::log::debug!( napi_fn = stringify!(#ident_n_api_fn),"invoking napi function"); + #input_fn #(#rust_args_struct)*