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([ 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"),

View file

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

View file

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

View file

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

View file

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