diff --git a/examples/napi/__test__/typegen.spec.ts.md b/examples/napi/__test__/typegen.spec.ts.md index f2a985cc..54bd26ac 100644 --- a/examples/napi/__test__/typegen.spec.ts.md +++ b/examples/napi/__test__/typegen.spec.ts.md @@ -68,6 +68,8 @@ Generated by [AVA](https://avajs.dev). export function mutateExternal(external: ExternalObject, newVal: number): void␊ export function tsRename(a: { foo: number }): string[]␊ export function xxh64Alias(input: Buffer): BigInt␊ + export function getMapping(): Record␊ + export function sumMapping(nums: Record): number␊ export function mapOption(val?: number | undefined | null): number | undefined | null␊ export function returnNull(): null␊ export function returnUndefined(): void␊ diff --git a/examples/napi/__test__/typegen.spec.ts.snap b/examples/napi/__test__/typegen.spec.ts.snap index 7132e138..2249df9d 100644 Binary files a/examples/napi/__test__/typegen.spec.ts.snap and b/examples/napi/__test__/typegen.spec.ts.snap differ diff --git a/examples/napi/__test__/values.spec.ts b/examples/napi/__test__/values.spec.ts index b080e2d5..a61b4d42 100644 --- a/examples/napi/__test__/values.spec.ts +++ b/examples/napi/__test__/values.spec.ts @@ -14,6 +14,8 @@ import { getNums, getWords, sumNums, + getMapping, + sumMapping, getCwd, Animal, Kind, @@ -104,6 +106,11 @@ test('array', (t) => { t.is(sumNums([1, 2, 3, 4, 5]), 15) }) +test('map', (t) => { + t.deepEqual(getMapping(), { a: 101, b: 102 }) + t.is(sumMapping({ a: 101, b: 102 }), 203) +}) + test('enum', (t) => { t.deepEqual([Kind.Dog, Kind.Cat, Kind.Duck], [0, 1, 2]) t.is(enumToI32(CustomNumEnum.Eight), 8) diff --git a/examples/napi/index.d.ts b/examples/napi/index.d.ts index da358ea6..6282cf8f 100644 --- a/examples/napi/index.d.ts +++ b/examples/napi/index.d.ts @@ -58,6 +58,8 @@ export function getExternal(external: ExternalObject): number export function mutateExternal(external: ExternalObject, newVal: number): void export function tsRename(a: { foo: number }): string[] export function xxh64Alias(input: Buffer): BigInt +export function getMapping(): Record +export function sumMapping(nums: Record): number export function mapOption(val?: number | undefined | null): number | undefined | null export function returnNull(): null export function returnUndefined(): void diff --git a/examples/napi/src/lib.rs b/examples/napi/src/lib.rs index 047c5add..5e0333b2 100644 --- a/examples/napi/src/lib.rs +++ b/examples/napi/src/lib.rs @@ -22,6 +22,7 @@ mod error; mod external; mod fn_ts_override; mod js_mod; +mod map; mod nullable; mod number; mod object; diff --git a/examples/napi/src/map.rs b/examples/napi/src/map.rs new file mode 100644 index 00000000..c4d0bd43 --- /dev/null +++ b/examples/napi/src/map.rs @@ -0,0 +1,14 @@ +use std::collections::HashMap; + +#[napi] +fn get_mapping() -> HashMap { + let mut map = HashMap::new(); + map.insert("a".to_string(), 101); + map.insert("b".to_string(), 102); + map +} + +#[napi] +fn sum_mapping(nums: HashMap) -> u32 { + nums.into_values().sum() +}