fix(napi-derive-backend): Null and Undefined return type

This commit is contained in:
LongYinan 2021-12-18 13:36:45 +08:00
parent 3f2e44d3db
commit 4ec4400703
No known key found for this signature in database
GPG key ID: C3666B7FC82ADAD7
6 changed files with 26 additions and 0 deletions

View file

@ -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"),

View file

@ -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>

View file

@ -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)

View file

@ -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>

View file

@ -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 {}