zhangyuang / node-ffi-rs

Implement ffi in Node.js by Rust and NAPI
MIT License
189 stars 7 forks source link

Callback not being called #62

Closed thewh1teagle closed 2 months ago

thewh1teagle commented 4 months ago

Current ffi-rs version

Print current Node.js info with the following code

$ cat node_modules/ffi-rs/package.json

{
  "name": "ffi-rs",
  "version": "1.0.86",
  "main": "index.js",
  "types": "index.d.ts",
  "description": "A module written in Rust and N-API provides interface (FFI) features for Node.js",
  "napi": {
    "name": "ffi-rs",
    "triples": {
      "additional": [
        "aarch64-apple-darwin",
        "aarch64-unknown-linux-gnu",
        "aarch64-unknown-linux-musl",
        "i686-pc-windows-msvc",
        "x86_64-unknown-linux-musl",
        "aarch64-pc-windows-msvc"
      ]
    }
  },
  "author": "zhangyuang",
  "homepage": "https://github.com/zhangyuang/node-ffi-rs#readme",
  "repository": {
    "type": "git",
    "url": "git+https://github.com/zhangyuang/node-ffi-rs.git"
  },
  "keywords": [
    "ffi",
    "rust",
    "node.js",
    "napi"
  ],
  "files": [
    "index.js",
    "index.d.ts",
    "README.md"
  ],
  "license": "MIT",
  "dependencies": {},
  "devDependencies": {
    "@napi-rs/cli": "^2.15.2",
    "@types/node": "^20.8.7",
    "benny": "^3.7.1",
    "conventional-changelog-cli": "^4.1.0",
    "esno": "^4.0.0",
    "ffi-napi": "^4.0.3",
    "koa": "^2.14.2",
    "shelljs": "^0.8.5",
    "typescript": "^5.4.5"
  },
  "scripts": {
    "artifacts": "napi artifacts",
    "build:c": "node scripts/compile.js",
    "build:dev": "env=development node scripts/build.js",
    "build": "node scripts/build.js",
    "publish:npm": "node scripts/publish.js",
    "test": "esno ./test.ts",
    "changelog": "conventional-changelog -p angular -i CHANGELOG.md -s -r 0 && git add . && git commit -m \"docs: update changelog.md\" && git push origin master",
    "pub": "npm version patch && git push origin master --tags && npm run changelog",
    "pub:alpha": "npm version prerelease --preid=alpha && git push origin master --tags"
  },
  "optionalDependencies": {
    "@yuuang/ffi-rs-darwin-arm64": "1.0.86",
    "@yuuang/ffi-rs-darwin-x64": "1.0.86",
    "@yuuang/ffi-rs-linux-arm64-gnu": "1.0.86",
    "@yuuang/ffi-rs-linux-arm64-musl": "1.0.86",
    "@yuuang/ffi-rs-linux-x64-gnu": "1.0.86",
    "@yuuang/ffi-rs-linux-x64-musl": "1.0.86",
    "@yuuang/ffi-rs-win32-arm64-msvc": "1.0.86",
    "@yuuang/ffi-rs-win32-ia32-msvc": "1.0.86",
    "@yuuang/ffi-rs-win32-x64-msvc": "1.0.86"
  }
}

$ ls node_modules/@yuuang

ls node_modules/@yuuang ffi-rs-darwin-arm64

Current Node.js arch

Print current Node.js info with the following code

node -v
v20.15.1 ➜ node git:(main) bun -v
1.1.18

$ node -e "console.log(process.arch, process.platform)"

node -e "console.log(process.arch, process.platform)"
arm64 darwin

Descibe your problem in detail

I created bindings using ffi-rs to tauric I have a function called on_ready that accept function pointer and it's being called after run is called and ready. I register it in bindings/node/index.ts#L25. In nodejs it desn't being called. In bun.js when register it I get error:

bun run index.js  
thread '<unnamed>' panicked at src/utils/dataprocess.rs:246:54:
called `Result::unwrap()` on an `Err` value: Error { status: Unknown, reason: "Get field \"needFree\" received Boolean(false) but params type only supported number or object ", maybe_raw: 0x0 }
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
fatal runtime error: failed to initiate panic, error 5
[1]    2267 abort      bun run index.js

You can see that with python bindings it works: bindings/python/main.py

What's your expect result

The callback that passed should being called few moments after run is called.

The reproduction repo address

https://github.com/thewh1teagle/tauric

zhangyuang commented 4 months ago

on_ready function has been called successfully with logs , you have not called the closure callback in your code

#[no_mangle]
pub extern "C" fn on_ready(callback: Option<extern "C" fn()>) {
    println!("xx");
    // Store the callback function in the global variable
    *READY_CALLBACK.lock().unwrap() = callback;
}
thewh1teagle commented 4 months ago

on_ready function has been called successfully with logs , you have not called the closure callback in your code

#[no_mangle]
pub extern "C" fn on_ready(callback: Option<extern "C" fn()>) {
    println!("xx");
    // Store the callback function in the global variable
    *READY_CALLBACK.lock().unwrap() = callback;
}

The on ready register on ready callback. Later I call it here:

https://github.com/thewh1teagle/tauric/blob/main/c-api/src/lib.rs#L105-L110

In the nodejs callback it should print ready. But although I can see that rust call it, nodejs doesn't print ready

zhangyuang commented 4 months ago

https://github.com/tauri-apps/tauri/issues/3172

It looks like tauri.run method blocks the main thread cause callback cannot be executed