feat(napi-derive): catch_unwind attribute ()

This commit is contained in:
LongYinan 2022-08-19 23:36:36 +08:00 committed by GitHub
parent bc69e15efb
commit b7a3103f0c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 64 additions and 8 deletions

View file

@ -103,6 +103,7 @@ Generated by [AVA](https://avajs.dev).
}␊
export function enumToI32(e: CustomNumEnum): number␊
export function throwError(): void␊
export function panic(): void␊
export function createExternal(size: number): ExternalObject<number>
export function createExternalString(content: string): ExternalObject<string>
export function getExternal(external: ExternalObject<number>): number␊

View file

@ -31,6 +31,7 @@ import {
mapOption,
readFile,
throwError,
panic,
readPackageJson,
getPackageJsonName,
getBuffer,
@ -163,7 +164,7 @@ test('enum', (t) => {
t.is(enumToI32(CustomNumEnum.Eight), 8)
})
test.only('class', (t) => {
test('class', (t) => {
const dog = new Animal(Kind.Dog, '旺财')
t.is(dog.name, '旺财')
@ -371,6 +372,9 @@ test('Option', (t) => {
test('Result', (t) => {
t.throws(() => throwError(), void 0, 'Manual Error')
if (!process.env.SKIP_UNWIND_TEST) {
t.throws(() => panic(), void 0, `Don't panic`)
}
})
test('function ts type override', (t) => {

View file

@ -93,6 +93,7 @@ export const enum CustomNumEnum {
}
export function enumToI32(e: CustomNumEnum): number
export function throwError(): void
export function panic(): void
export function createExternal(size: number): ExternalObject<number>
export function createExternalString(content: string): ExternalObject<string>
export function getExternal(external: ExternalObject<number>): number

View file

@ -1,6 +1,11 @@
use napi::bindgen_prelude::*;
#[napi]
fn throw_error() -> Result<()> {
pub fn throw_error() -> Result<()> {
Err(Error::new(Status::InvalidArg, "Manual Error".to_owned()))
}
#[napi(catch_unwind)]
pub fn panic() {
panic!("Don't panic");
}

View file

@ -1,6 +1,6 @@
#![allow(dead_code)]
#![allow(unreachable_code)]
#![allow(clippy::blacklisted_name)]
#![allow(clippy::disallowed_names)]
#[macro_use]
extern crate napi_derive;