zhangyuang / node-ffi-rs

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

[BUG] Please use ffi-rs.arrayConstrutor to describe array type #84

Open mushan0x0 opened 3 days ago

mushan0x0 commented 3 days ago

Current ffi-rs version

{
  "name": "ffi-rs",
  "version": "1.1.1",
  "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",
        "arm-unknown-linux-gnueabihf",
        "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.1.1",
    "@yuuang/ffi-rs-darwin-x64": "1.1.1",
    "@yuuang/ffi-rs-linux-arm-gnueabihf": "1.1.1",
    "@yuuang/ffi-rs-linux-arm64-gnu": "1.1.1",
    "@yuuang/ffi-rs-linux-arm64-musl": "1.1.1",
    "@yuuang/ffi-rs-linux-x64-gnu": "1.1.1",
    "@yuuang/ffi-rs-linux-x64-musl": "1.1.1",
    "@yuuang/ffi-rs-win32-arm64-msvc": "1.1.1",
    "@yuuang/ffi-rs-win32-ia32-msvc": "1.1.1",
    "@yuuang/ffi-rs-win32-x64-msvc": "1.1.1"
  }
}
ffi-rs-darwin-arm64

Current Node.js arch

arm64 darwin

Descibe your problem in detail

在 mac 平台想要调用 CGEventGetLocation 获取光标位置,报错:

node test.js
thread '<unnamed>' panicked at src/define.rs:176:7:
In the latest ffi-rs version, please use ffi-rs.arrayConstrutor to describe array type
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
fatal runtime error: failed to initiate panic, error 5
zsh: abort      node test.js

What's your expect result

希望返回正确值

The reproduction repo address

// test.js
const { load, open, DataType } = require("ffi-rs");

// 打开 macOS 的 ApplicationServices 框架
open({
  library: "ApplicationServices",
  path: "/System/Library/Frameworks/ApplicationServices.framework/ApplicationServices",
});

// 获取光标位置函数
async function getCursorPosition() {
  // 调用 CGEventCreate 创建一个空事件
  const createEvent = await load({
    library: "ApplicationServices",
    funcName: "CGEventCreate",
    retType: DataType.External, // 返回的是一个指针指向 CGEventRef
    paramsType: [],
    paramsValue: [],
    freeResultMemory: false,
  });

  // 调用 CGEventGetLocation 获取光标位置
  const location = await load({
    library: "ApplicationServices",
    funcName: "CGEventGetLocation",
    retType: DataType.DoubleArray, // 将 CGPoint 转为 Double 数组返回
    paramsType: [DataType.External], // 参数是一个指针(CGEventRef 类型)
    paramsValue: [createEvent], // 传入创建的事件
    freeResultMemory: false, // 不自动释放
  });

  // 转换结果为对象
  if (Array.isArray(location) && location.length === 2) {
    return { x: location[0], y: location[1] };
  }

  throw new Error("无法分析光标位置");
}

// 调用获取光标函数并打印结果
getCursorPosition()
  .then((pos) => {
    console.log(`光标当前位置:x=${pos.x}, y=${pos.y}`);
  })
  .catch((err) => {
    console.error("获取光标位置失败:", err.message);
  });
zhangyuang commented 1 day ago

Use arrayConstructor({type: DataType.DoubleArray, length:2}) to describe retType, but the retType of ffi function is stack struct in fact