Merge pull request #940 from napi-rs/fix-return-type
fix(napi-derive-backend): Null and Undefined return type
This commit is contained in:
commit
5b83de8a48
6 changed files with 26 additions and 0 deletions
|
@ -98,6 +98,7 @@ static KNOWN_TYPES: Lazy<HashMap<&'static str, &'static str>> = Lazy::new(|| {
|
|||
map.extend([
|
||||
("JsUndefined", "undefined"),
|
||||
("()", "undefined"),
|
||||
("Undefined", "undefined"),
|
||||
("JsNumber", "number"),
|
||||
("i8", "number"),
|
||||
("i16", "number"),
|
||||
|
@ -152,6 +153,7 @@ static KNOWN_TYPES: Lazy<HashMap<&'static str, &'static str>> = Lazy::new(|| {
|
|||
("Either4", "{} | {} | {} | {}"),
|
||||
("Either5", "{} | {} | {} | {} | {}"),
|
||||
("unknown", "unknown"),
|
||||
("Null", "null"),
|
||||
("null", "null"),
|
||||
("Symbol", "symbol"),
|
||||
("JsSymbol", "symbol"),
|
||||
|
|
|
@ -66,6 +66,8 @@ Generated by [AVA](https://avajs.dev).
|
|||
export function tsRename(a: { foo: number }): string[]␊
|
||||
export function xxh64Alias(input: Buffer): BigInt␊
|
||||
export function mapOption(val?: number | undefined | null): number | undefined | null␊
|
||||
export function returnNull(): null␊
|
||||
export function returnUndefined(): void␊
|
||||
export function add(a: number, b: number): number␊
|
||||
export function fibonacci(n: number): number␊
|
||||
export function listObjKeys(obj: object): Array<string>␊
|
||||
|
|
Binary file not shown.
|
@ -65,6 +65,8 @@ import {
|
|||
ALIAS,
|
||||
AliasedStruct,
|
||||
appendBuffer,
|
||||
returnNull,
|
||||
returnUndefined,
|
||||
} from '../'
|
||||
|
||||
test('export const', (t) => {
|
||||
|
@ -192,6 +194,14 @@ test('get null', (t) => {
|
|||
}
|
||||
})
|
||||
|
||||
test('return Null', (t) => {
|
||||
t.is(returnNull(), null)
|
||||
})
|
||||
|
||||
test('return Undefined', (t) => {
|
||||
t.is(returnUndefined(), undefined)
|
||||
})
|
||||
|
||||
test('pass symbol in', (t) => {
|
||||
const sym = Symbol('test')
|
||||
const obj = setSymbolInObj(sym)
|
||||
|
|
2
examples/napi/index.d.ts
vendored
2
examples/napi/index.d.ts
vendored
|
@ -56,6 +56,8 @@ export function mutateExternal(external: ExternalObject<number>, newVal: number)
|
|||
export function tsRename(a: { foo: number }): string[]
|
||||
export function xxh64Alias(input: Buffer): BigInt
|
||||
export function mapOption(val?: number | undefined | null): number | undefined | null
|
||||
export function returnNull(): null
|
||||
export function returnUndefined(): void
|
||||
export function add(a: number, b: number): number
|
||||
export function fibonacci(n: number): number
|
||||
export function listObjKeys(obj: object): Array<string>
|
||||
|
|
|
@ -1,4 +1,14 @@
|
|||
use napi::bindgen_prelude::*;
|
||||
|
||||
#[napi]
|
||||
fn map_option(val: Option<u32>) -> Option<u32> {
|
||||
val.map(|v| v + 1)
|
||||
}
|
||||
|
||||
#[napi]
|
||||
fn return_null() -> Null {
|
||||
Null
|
||||
}
|
||||
|
||||
#[napi]
|
||||
fn return_undefined() -> Undefined {}
|
||||
|
|
Loading…
Reference in a new issue