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([
|
map.extend([
|
||||||
("JsUndefined", "undefined"),
|
("JsUndefined", "undefined"),
|
||||||
("()", "undefined"),
|
("()", "undefined"),
|
||||||
|
("Undefined", "undefined"),
|
||||||
("JsNumber", "number"),
|
("JsNumber", "number"),
|
||||||
("i8", "number"),
|
("i8", "number"),
|
||||||
("i16", "number"),
|
("i16", "number"),
|
||||||
|
@ -152,6 +153,7 @@ static KNOWN_TYPES: Lazy<HashMap<&'static str, &'static str>> = Lazy::new(|| {
|
||||||
("Either4", "{} | {} | {} | {}"),
|
("Either4", "{} | {} | {} | {}"),
|
||||||
("Either5", "{} | {} | {} | {} | {}"),
|
("Either5", "{} | {} | {} | {} | {}"),
|
||||||
("unknown", "unknown"),
|
("unknown", "unknown"),
|
||||||
|
("Null", "null"),
|
||||||
("null", "null"),
|
("null", "null"),
|
||||||
("Symbol", "symbol"),
|
("Symbol", "symbol"),
|
||||||
("JsSymbol", "symbol"),
|
("JsSymbol", "symbol"),
|
||||||
|
|
|
@ -66,6 +66,8 @@ Generated by [AVA](https://avajs.dev).
|
||||||
export function tsRename(a: { foo: number }): string[]␊
|
export function tsRename(a: { foo: number }): string[]␊
|
||||||
export function xxh64Alias(input: Buffer): BigInt␊
|
export function xxh64Alias(input: Buffer): BigInt␊
|
||||||
export function mapOption(val?: number | undefined | null): number | undefined | null␊
|
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 add(a: number, b: number): number␊
|
||||||
export function fibonacci(n: number): number␊
|
export function fibonacci(n: number): number␊
|
||||||
export function listObjKeys(obj: object): Array<string>␊
|
export function listObjKeys(obj: object): Array<string>␊
|
||||||
|
|
Binary file not shown.
|
@ -65,6 +65,8 @@ import {
|
||||||
ALIAS,
|
ALIAS,
|
||||||
AliasedStruct,
|
AliasedStruct,
|
||||||
appendBuffer,
|
appendBuffer,
|
||||||
|
returnNull,
|
||||||
|
returnUndefined,
|
||||||
} from '../'
|
} from '../'
|
||||||
|
|
||||||
test('export const', (t) => {
|
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) => {
|
test('pass symbol in', (t) => {
|
||||||
const sym = Symbol('test')
|
const sym = Symbol('test')
|
||||||
const obj = setSymbolInObj(sym)
|
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 tsRename(a: { foo: number }): string[]
|
||||||
export function xxh64Alias(input: Buffer): BigInt
|
export function xxh64Alias(input: Buffer): BigInt
|
||||||
export function mapOption(val?: number | undefined | null): number | undefined | null
|
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 add(a: number, b: number): number
|
||||||
export function fibonacci(n: number): number
|
export function fibonacci(n: number): number
|
||||||
export function listObjKeys(obj: object): Array<string>
|
export function listObjKeys(obj: object): Array<string>
|
||||||
|
|
|
@ -1,4 +1,14 @@
|
||||||
|
use napi::bindgen_prelude::*;
|
||||||
|
|
||||||
#[napi]
|
#[napi]
|
||||||
fn map_option(val: Option<u32>) -> Option<u32> {
|
fn map_option(val: Option<u32>) -> Option<u32> {
|
||||||
val.map(|v| v + 1)
|
val.map(|v| v + 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[napi]
|
||||||
|
fn return_null() -> Null {
|
||||||
|
Null
|
||||||
|
}
|
||||||
|
|
||||||
|
#[napi]
|
||||||
|
fn return_undefined() -> Undefined {}
|
||||||
|
|
Loading…
Reference in a new issue