From 413a55545fa953ba2754c0493a5fbd4817d0da75 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Wed, 10 Nov 2021 13:15:54 +0800 Subject: [PATCH] fix(napi-derive-backend): wrong restrict on Result return type --- README.md | 2 +- bench/package.json | 2 +- crates/backend/src/codegen/fn.rs | 11 ++++++----- 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index ab04ada4..9562378e 100644 --- a/README.md +++ b/README.md @@ -89,7 +89,7 @@ extern crate napi; /// import the preludes use napi::bindgen_prelude::*; -/// module registerion is done by the runtime, no need to explicitly do it now. +/// module registration is done by the runtime, no need to explicitly do it now. #[napi] fn fibonacci(n: u32) -> u32 { match n { diff --git a/bench/package.json b/bench/package.json index 6c601ba0..f146d7d4 100644 --- a/bench/package.json +++ b/bench/package.json @@ -2,6 +2,6 @@ "name": "test-module", "version": "1.0.0", "scripts": { - "build": "node ../cli/scripts/index.js build --release" + "build": "node ../cli/scripts/index.js build --js false --release" } } diff --git a/crates/backend/src/codegen/fn.rs b/crates/backend/src/codegen/fn.rs index a5b869a2..07acba9c 100644 --- a/crates/backend/src/codegen/fn.rs +++ b/crates/backend/src/codegen/fn.rs @@ -262,11 +262,12 @@ impl NapiFn { } } else { quote! { - if #ret.is_ok() { - as ToNapiValue>::to_napi_value(env, #ret) - } else { - JsError::from(#ret.unwrap_err()).throw_into(env); - Ok(std::ptr::null_mut()) + match #ret { + Ok(value) => ToNapiValue::to_napi_value(env, value), + Err(err) => { + JsError::from(err).throw_into(env); + Ok(std::ptr::null_mut()) + }, } } }