Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bug] exported function has incorrect signature #2060

Open
naskya opened this issue Apr 19, 2024 · 0 comments
Open

[bug] exported function has incorrect signature #2060

naskya opened this issue Apr 19, 2024 · 0 comments

Comments

@naskya
Copy link
Contributor

naskya commented Apr 19, 2024

Problem

napi-exported functions can have incorrect signatures.

Example

https://github.com/naskya/napi-rs-sig-example

Setup

src
├── bar.rs
├── foo.rs
└── lib.rs
/// bar.rs
#[napi_derive::napi(object, js_name = "Bar")]
pub struct Thing {
    pub id: u32,
}

/// foo.rs
#[napi_derive::napi(object, js_name = "Foo")]
pub struct Thing {
    pub name: String,
}

/// lib.rs
pub mod bar;
pub mod foo;

#[napi_derive::napi]
pub fn do_something(bar: bar::Thing) {}

Expected behavior

doSomething should take Bar as an argument.

export interface Bar {
  id: number
}
export interface Foo {
  name: string
}
export function doSomething(bar: Bar): void

Actual behavior

doSomething takes Foo instead of Bar.

export interface Bar {
  id: number
}
export interface Foo {
  name: string
}
export function doSomething(bar: Foo): void

Workaround 1

In this particular case, napi-rs generates the correct function if you swap pub mod foo; and pub mod bar; in lib.rs

/// lib.rs
pub mod foo;
pub mod bar;

#[napi_derive::napi]
pub fn do_something(bar: bar::Thing) {}
export interface Foo {
  name: string
}
export interface Bar {
  id: number
}
export function doSomething(bar: Bar): void

Workaround 2

Give it a type alias.

/// lib.rs
pub mod bar;
pub mod foo;

type Bar = bar::Thing;

#[napi_derive::napi]
pub fn do_something(bar: Bar) {}
export interface Foo {
  name: string
}
export interface Bar {
  id: number
}
export function doSomething(bar: Bar): void
CGsama pushed a commit to CGsama/calckey that referenced this issue Apr 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant