feat(napi): improve symbol support

This commit is contained in:
LongYinan 2021-11-15 23:09:44 +08:00
parent c0a89834c2
commit 424c7805c4
No known key found for this signature in database
GPG key ID: C3666B7FC82ADAD7
10 changed files with 101 additions and 29 deletions

View file

@ -35,7 +35,7 @@ Generated by [AVA](https://avajs.dev).
export function listObjKeys(obj: object): Array<string>
export function createObj(): object␊
export function getGlobal(): typeof global␊
export function getUndefined(): JsUndefined␊
export function getUndefined(): void␊
export function getNull(): JsNull␊
export function asyncPlus100(p: Promise<number>): Promise<number>
interface PackageJson {␊
@ -50,6 +50,8 @@ Generated by [AVA](https://avajs.dev).
export function concatStr(s: string): string␊
export function concatUtf16(s: string): string␊
export function concatLatin1(s: string): string␊
export function setSymbolInObj(symbol: symbol): object␊
export function createSymbol(): symbol␊
export function withoutAbortController(a: number, b: number): Promise<number>
export function withAbortController(a: number, b: number, signal: AbortSignal): Promise<number>
export function callThreadsafeFunction(callback: (...args: any[]) => any): void␊

View file

@ -44,6 +44,8 @@ import {
getGlobal,
getUndefined,
getNull,
setSymbolInObj,
createSymbol,
} from '../'
test('number', (t) => {
@ -156,6 +158,16 @@ test('get null', (t) => {
}
})
test('pass symbol in', (t) => {
const sym = Symbol('test')
const obj = setSymbolInObj(sym)
t.is(obj[sym], 'a symbol')
})
test('create symbol', (t) => {
t.is(createSymbol().toString(), 'Symbol(a symbol)')
})
test('Option', (t) => {
t.is(mapOption(null), null)
t.is(mapOption(3), 4)

View file

@ -7,9 +7,7 @@ export function bigintAdd(a: BigInt, b: BigInt): BigInt
export function createBigInt(): BigInt
export function createBigIntI64(): BigInt
export function getCwd(callback: (arg0: string) => void): void
export function readFile(
callback: (arg0: Error | undefined, arg1: string | null) => void,
): void
export function readFile(callback: (arg0: Error | undefined, arg1: string | null) => void): void
export function eitherStringOrNumber(input: string | number): number
export function returnEither(input: number): string | number
export function either3(input: string | number | boolean): number
@ -17,21 +15,8 @@ interface Obj {
v: string | number
}
export function either4(input: string | number | boolean | Obj): number
export enum Kind {
Dog = 0,
Cat = 1,
Duck = 2,
}
export enum CustomNumEnum {
One = 1,
Two = 2,
Three = 3,
Four = 4,
Six = 6,
Eight = 8,
Nine = 9,
Ten = 10,
}
export enum Kind { Dog = 0, Cat = 1, Duck = 2 }
export enum CustomNumEnum { One = 1, Two = 2, Three = 3, Four = 4, Six = 6, Eight = 8, Nine = 9, Ten = 10 }
export function enumToI32(e: CustomNumEnum): number
export function throwError(): void
export function mapOption(val: number | null): number | null
@ -40,7 +25,7 @@ export function fibonacci(n: number): number
export function listObjKeys(obj: object): Array<string>
export function createObj(): object
export function getGlobal(): typeof global
export function getUndefined(): JsUndefined
export function getUndefined(): void
export function getNull(): JsNull
export function asyncPlus100(p: Promise<number>): Promise<number>
interface PackageJson {
@ -55,12 +40,10 @@ export function contains(source: string, target: string): boolean
export function concatStr(s: string): string
export function concatUtf16(s: string): string
export function concatLatin1(s: string): string
export function setSymbolInObj(symbol: symbol): object
export function createSymbol(): symbol
export function withoutAbortController(a: number, b: number): Promise<number>
export function withAbortController(
a: number,
b: number,
signal: AbortSignal,
): Promise<number>
export function withAbortController(a: number, b: number, signal: AbortSignal): Promise<number>
export function callThreadsafeFunction(callback: (...args: any[]) => any): void
export function threadsafeFunctionThrowError(cb: (...args: any[]) => any): void
export function getBuffer(): Buffer
@ -74,10 +57,14 @@ export class Animal {
static getDogKind(): Kind
}
export class Blake2BHasher {
static withKey(key: Blake2bKey): Blake2BHasher
}
export class Blake2BKey {}
export class Blake2BKey {
}
export class Context {
constructor()
static withData(data: string): Context
method(): string

View file

@ -18,6 +18,7 @@ mod object;
mod promise;
mod serde;
mod string;
mod symbol;
mod task;
mod threadsafe_function;
mod typed_array;

View file

@ -0,0 +1,13 @@
use napi::{bindgen_prelude::*, Env, JsObject, JsSymbol};
#[napi]
pub fn set_symbol_in_obj(env: Env, symbol: JsSymbol) -> Result<JsObject> {
let mut obj = env.create_object()?;
obj.set_property(symbol, env.create_string("a symbol")?)?;
Ok(obj)
}
#[napi]
pub fn create_symbol() -> Symbol {
Symbol::new("a symbol".to_owned())
}