From feabcd7f16c8a7a0d19f45ca73bb497ddc14b605 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Mon, 6 Nov 2023 11:03:44 +0800 Subject: [PATCH] test(napi): add tests for napi9 features (#1784) --- .github/workflows/test-release.yaml | 2 +- crates/napi/Cargo.toml | 6 +- crates/napi/src/env.rs | 36 ++- .../__snapshots__/typegen.spec.ts.md | 4 + .../__snapshots__/typegen.spec.ts.snap | Bin 4091 -> 4126 bytes examples/napi/__tests__/values.spec.ts | 24 ++ examples/napi/index.d.ts | 4 + examples/napi/index.wasi.mjs | 247 +++++++++--------- examples/napi/src/env.rs | 16 ++ examples/napi/src/lib.rs | 8 +- 10 files changed, 191 insertions(+), 156 deletions(-) create mode 100644 examples/napi/src/env.rs diff --git a/.github/workflows/test-release.yaml b/.github/workflows/test-release.yaml index 142f884a..5a800cd5 100644 --- a/.github/workflows/test-release.yaml +++ b/.github/workflows/test-release.yaml @@ -523,7 +523,6 @@ jobs: runs-on: ubuntu-latest name: Test latest bun timeout-minutes: 10 - continue-on-error: true steps: - uses: actions/checkout@v4 - uses: oven-sh/setup-bun@v1 @@ -554,6 +553,7 @@ jobs: bun run build bun run build:test - name: Test + continue-on-error: true run: bun run test:bun release-npm: diff --git a/crates/napi/Cargo.toml b/crates/napi/Cargo.toml index 05c53efc..469d0501 100644 --- a/crates/napi/Cargo.toml +++ b/crates/napi/Cargo.toml @@ -37,7 +37,7 @@ napi5 = ["napi4", "napi-sys/napi5"] napi6 = ["napi5", "napi-sys/napi6"] napi7 = ["napi6", "napi-sys/napi7"] napi8 = ["napi7", "napi-sys/napi8"] -napi9 = ["napi8", "napi-sys/napi9", "byteorder"] +napi9 = ["napi8", "napi-sys/napi9"] noop = [] serde-json = ["serde", "serde_json"] tokio_fs = ["tokio/fs"] @@ -75,10 +75,6 @@ version = "0.8" optional = true version = "0.4" -[dependencies.byteorder] -optional = true -version = "1.5" - [target.'cfg(target_os = "wasi")'.dependencies] napi-derive = { path = "../macro", version = "2.10.1", default-features = false } tokio = { version = "1", optional = true, features = ["rt", "sync"] } diff --git a/crates/napi/src/env.rs b/crates/napi/src/env.rs index 1954885c..754d1744 100644 --- a/crates/napi/src/env.rs +++ b/crates/napi/src/env.rs @@ -673,16 +673,19 @@ impl Env { /// This API throws a JavaScript SyntaxError with the text provided. #[cfg(feature = "napi9")] - pub fn throw_syntax_error(&self, msg: &str, code: Option<&str>) -> Result<()> { - let code = code.and_then(|s| CString::new(s).ok()); - let msg = CString::new(msg)?; - check_status!(unsafe { - sys::node_api_throw_syntax_error( - self.0, - code.map(|s| s.as_ptr()).unwrap_or(ptr::null_mut()), - msg.as_ptr(), - ) - }) + pub fn throw_syntax_error, C: AsRef>(&self, msg: S, code: Option) { + use crate::check_status_or_throw; + + let code = code.as_ref().map(|c| c.as_ref()).unwrap_or(""); + let c_code = CString::new(code).expect("code must be a valid utf-8 string"); + let code_ptr = c_code.as_ptr(); + let msg: CString = CString::new(msg.as_ref()).expect("msg must be a valid utf-8 string"); + let msg_ptr = msg.as_ptr(); + check_status_or_throw!( + self.0, + unsafe { sys::node_api_throw_syntax_error(self.0, code_ptr, msg_ptr,) }, + "Throw syntax error failed" + ); } #[allow(clippy::expect_fun_call)] @@ -1286,17 +1289,8 @@ impl Env { // SAFETY: This is safe because `char_ptr` is guaranteed to not be `null`, and point to // null-terminated string data. let module_filename = unsafe { std::ffi::CStr::from_ptr(char_ptr) }; - #[cfg(windows)] - { - let byte_len = module_filename.to_bytes().len(); - let mut utf16_buf = vec![0u16; byte_len / 2]; - byteorder::LittleEndian::read_u16_into(module_filename.to_bytes(), &mut utf16_buf); - Ok(String::from_utf16_lossy(utf16_buf)) - } - #[cfg(not(windows))] - { - Ok(module_filename.to_string_lossy().into_owned()) - } + + Ok(module_filename.to_string_lossy().into_owned()) } /// ### Serialize `Rust Struct` into `JavaScript Value` diff --git a/examples/napi/__tests__/__snapshots__/typegen.spec.ts.md b/examples/napi/__tests__/__snapshots__/typegen.spec.ts.md index 7951d2ec..45c24471 100644 --- a/examples/napi/__tests__/__snapshots__/typegen.spec.ts.md +++ b/examples/napi/__tests__/__snapshots__/typegen.spec.ts.md @@ -372,6 +372,8 @@ Generated by [AVA](https://avajs.dev). ␊ export function getMapping(): Record␊ ␊ + export function getModuleFileName(): string␊ + ␊ export function getNestedNumArr(): number[][][]␊ ␊ export function getNull(): null␊ @@ -528,6 +530,8 @@ Generated by [AVA](https://avajs.dev). ␊ export function throwError(): void␊ ␊ + export function throwSyntaxError(error: string, code?: string | undefined | null): void␊ + ␊ export function toJsObj(): object␊ ␊ export function tsfnAsyncCall(func: (...args: any[]) => any): Promise␊ diff --git a/examples/napi/__tests__/__snapshots__/typegen.spec.ts.snap b/examples/napi/__tests__/__snapshots__/typegen.spec.ts.snap index 92bf03121d9090d6e4567587ca57537a00318de9..3801ad873926c9c1b57785d2631a8492475655fd 100644 GIT binary patch literal 4126 zcmV+(5aI7ZRzVguZeEpLF^)Rn2NvDT7ZuDz+1U6L$qF6eVnTqHCyuHvZ*ZUUC6zo{p5X2c(%k?^kOaw**SEX&l3;hE0W^O7 z2bjOjXQ_y;<)8n~RiaY<;XmY36lu&BJmYz&IEzG{^GGqZS#uWiIh!XbPh=LZfl|po z|1FL~75?_%|9<(!zkPxK{Ni7~_|?DsrkAsKpu)G)r|jcB#&RCUB1<>y{{1mE zG@6Pm`9r~(+^l9IrO$7^ADh>=V>X4R+k3a5z*qO!SL~-qWK!itq(uG`6Kj=-jLk*P ze$uT!f$%>RNzNq;(-cO}(^xVk*o?DvF-wzZgkpqR^86-|oT+fZlGPeWGZsqbh)23# zzk!)c0;hH7?L8cNZQzGlvI^KX$T70JVb^KTXD4%v6duNsFH}?Bx;Ew9BDgaGXS7ka7D6% z9V;DKZDX@nq((zSYBM5fn;iLCKU`WzN&UJiQk6UayTY-rRy9o2;SV|*pvCG3Cy z`j`Kvb{xpfQiQ9drl31|JFg7sMEaFmgsb#2`*?(YNTt?v<_Kn$r>1M3Se)s0>$J?)eH~#X=kGkQu97@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>vrIDh$=%EUtiKk(bA# zkc$^J4-UI;n}{x6E~j&B)43mqqh@C`k1_$-lRnU+qmLE`H&^ws!&rr>1_yIkZm#g^ za9jI0ynqRgP{4c`Vh%Mt##e zECMO0YOVyC>zqwOF)Q|xy*4qt7P%+vG}hajnA`IrMYBR)P$4`VAI1=jg+Zla(N^Sb5~1PA zNUDOe6Q^sk!Nvnv%lWEB?lg=0BcKN(q`xc{eWj(&BOocG!r@_zxt1y-nqp$TrdoJr zwY-i~d+T(+NR3qKj;K#lNL*Ci?}2%KfVt0`#1GJLpx^BDj+-t~My4-cGSxV_El zae~81i)?0ccu-F?rSUCj1`;THR_vt&x`hj`=JwIi?uHWusi&~}LqQQCy<3as+yS|S zGh-h5R;D_ia`V*Ay7YaoJ#}tnZV%k>)T=I*t~eZFfp#>!Q+b5`cO(?gYA=72z~qu* z5+%dh%gEr^$g=3LS!(iHEIG!IVjNSyc)qxF$(HE^!cY#mLUUeuOEVByShP-pZ*h}m z>Ai$nCwR7)Wb*&j7a55TQ1JcEyEx-?5z@!d$GWjZA&f!*t0-6?-9TVfWN+%n6OF!D zt<)J9&yA1vomkrj?142!W!rE6=l*kwes@xneva-?5N)ieloyUS5V$_*m*oJAe)3M;f) zs%*XfS-MZQY<`OfIP4XZ)z&fod}C(_P54udG>IZuQP+jPE)(H|>bykVr&00kcPD*%VE4ZJ(I= 4 ? test : test.skip @@ -1069,3 +1071,25 @@ const Napi9Test = Number(process.versions.napi) >= 9 ? test : test.skip Napi9Test('create symbol for', (t) => { t.is(createSymbolFor('foo'), Symbol.for('foo')) }) + +Napi9Test('get module file name', (t) => { + if (isWasiTest) { + t.pass() + return + } + console.info(getModuleFileName()) + t.true(getModuleFileName().includes('examples/napi/index.node')) +}) + +test('throw syntax error', (t) => { + const message = `Syntax Error: Unexpected token '}'` + const code = 'InvalidCharacterError' + t.throws( + () => throwSyntaxError(message, code), + { + code, + instanceOf: SyntaxError, + }, + message, + ) +}) diff --git a/examples/napi/index.d.ts b/examples/napi/index.d.ts index eb7075d9..bd950724 100644 --- a/examples/napi/index.d.ts +++ b/examples/napi/index.d.ts @@ -362,6 +362,8 @@ export function getGlobal(): typeof global export function getMapping(): Record +export function getModuleFileName(): string + export function getNestedNumArr(): number[][][] export function getNull(): null @@ -518,6 +520,8 @@ export function throwAsyncError(): Promise export function throwError(): void +export function throwSyntaxError(error: string, code?: string | undefined | null): void + export function toJsObj(): object export function tsfnAsyncCall(func: (...args: any[]) => any): Promise diff --git a/examples/napi/index.wasi.mjs b/examples/napi/index.wasi.mjs index 0d61c845..ad9212c9 100644 --- a/examples/napi/index.wasi.mjs +++ b/examples/napi/index.wasi.mjs @@ -1,4 +1,3 @@ -/* tslint:disable */ /* eslint-disable */ /* prettier-ignore */ @@ -140,130 +139,132 @@ function __napi_rs_initialize_modules(__napiInstance) { __napiInstance.exports['__napi_register__CustomNumEnum_83']() __napiInstance.exports['__napi_register__enum_to_i32_84']() __napiInstance.exports['__napi_register__SkippedEnums_85']() - __napiInstance.exports['__napi_register__throw_error_86']() - __napiInstance.exports['__napi_register__panic_87']() - __napiInstance.exports['__napi_register__receive_string_88']() - __napiInstance.exports['__napi_register__custom_status_code_89']() - __napiInstance.exports['__napi_register__throw_async_error_90']() - __napiInstance.exports['__napi_register__create_external_91']() - __napiInstance.exports['__napi_register__create_external_string_92']() - __napiInstance.exports['__napi_register__get_external_93']() - __napiInstance.exports['__napi_register__mutate_external_94']() - __napiInstance.exports['__napi_register__validate_array_95']() - __napiInstance.exports['__napi_register__validate_buffer_96']() - __napiInstance.exports['__napi_register__validate_typed_array_97']() - __napiInstance.exports['__napi_register__validate_bigint_98']() - __napiInstance.exports['__napi_register__validate_boolean_99']() - __napiInstance.exports['__napi_register__validate_date_100']() - __napiInstance.exports['__napi_register__validate_date_time_101']() - __napiInstance.exports['__napi_register__validate_external_102']() - __napiInstance.exports['__napi_register__validate_function_103']() - __napiInstance.exports['__napi_register__validate_hash_map_104']() - __napiInstance.exports['__napi_register__validate_null_105']() - __napiInstance.exports['__napi_register__validate_undefined_106']() - __napiInstance.exports['__napi_register__validate_number_107']() - __napiInstance.exports['__napi_register__validate_promise_108']() - __napiInstance.exports['__napi_register__validate_string_109']() - __napiInstance.exports['__napi_register__validate_symbol_110']() - __napiInstance.exports['__napi_register__validate_optional_111']() - __napiInstance.exports['__napi_register__return_undefined_if_invalid_112']() - __napiInstance.exports['__napi_register__return_undefined_if_invalid_promise_113']() - __napiInstance.exports['__napi_register__ts_rename_114']() - __napiInstance.exports['__napi_register__override_individual_arg_on_function_115']() - __napiInstance.exports['__napi_register__override_individual_arg_on_function_with_cb_arg_116']() - __napiInstance.exports['__napi_register__Fib_struct_117']() - __napiInstance.exports['__napi_register__Fib_impl_118']() - __napiInstance.exports['__napi_register__Fib_impl_119']() - __napiInstance.exports['__napi_register__Fib2_struct_120']() - __napiInstance.exports['__napi_register__Fib2_impl_121']() - __napiInstance.exports['__napi_register__Fib2_impl_122']() - __napiInstance.exports['__napi_register__Fib3_struct_123']() - __napiInstance.exports['__napi_register__Fib3_impl_124']() - __napiInstance.exports['__napi_register__ALIGNMENT_125']() - __napiInstance.exports['__napi_register__xxh64_126']() - __napiInstance.exports['__napi_register__xxh128_127']() - __napiInstance.exports['__napi_register__Xxh3_struct_128']() - __napiInstance.exports['__napi_register__Xxh3_impl_129']() - __napiInstance.exports['__napi_register__xxh2_plus_130']() - __napiInstance.exports['__napi_register__xxh3_xxh64_alias_131']() - __napiInstance.exports['__napi_register__xxh64_alias_132']() - __napiInstance.exports['__napi_register__get_mapping_133']() - __napiInstance.exports['__napi_register__sum_mapping_134']() - __napiInstance.exports['__napi_register__map_option_135']() - __napiInstance.exports['__napi_register__return_null_136']() - __napiInstance.exports['__napi_register__return_undefined_137']() - __napiInstance.exports['__napi_register__add_138']() - __napiInstance.exports['__napi_register__fibonacci_139']() - __napiInstance.exports['__napi_register__list_obj_keys_140']() - __napiInstance.exports['__napi_register__create_obj_141']() - __napiInstance.exports['__napi_register__get_global_142']() - __napiInstance.exports['__napi_register__get_undefined_143']() - __napiInstance.exports['__napi_register__get_null_144']() - __napiInstance.exports['__napi_register__receive_all_optional_object_145']() - __napiInstance.exports['__napi_register__AliasedEnum_146']() - __napiInstance.exports['__napi_register__fn_received_aliased_147']() - __napiInstance.exports['__napi_register__receive_strict_object_148']() - __napiInstance.exports['__napi_register__get_str_from_object_149']() - __napiInstance.exports['__napi_register__create_obj_with_property_150']() - __napiInstance.exports['__napi_register__getter_from_obj_151']() - __napiInstance.exports['__napi_register__receive_object_only_from_js_152']() - __napiInstance.exports['__napi_register__async_plus_100_153']() - __napiInstance.exports['__napi_register__JsRepo_struct_154']() - __napiInstance.exports['__napi_register__JsRepo_impl_155']() - __napiInstance.exports['__napi_register__JsRemote_struct_156']() - __napiInstance.exports['__napi_register__JsRemote_impl_157']() - __napiInstance.exports['__napi_register__CSSRuleList_struct_158']() - __napiInstance.exports['__napi_register__CSSRuleList_impl_159']() - __napiInstance.exports['__napi_register__CSSStyleSheet_struct_160']() - __napiInstance.exports['__napi_register__AnotherCSSStyleSheet_struct_161']() - __napiInstance.exports['__napi_register__AnotherCSSStyleSheet_impl_162']() - __napiInstance.exports['__napi_register__CSSStyleSheet_impl_163']() - __napiInstance.exports['__napi_register__read_package_json_164']() - __napiInstance.exports['__napi_register__get_package_json_name_165']() - __napiInstance.exports['__napi_register__test_serde_roundtrip_166']() - __napiInstance.exports['__napi_register__test_serde_big_number_precision_167']() - __napiInstance.exports['__napi_register__return_from_shared_crate_168']() - __napiInstance.exports['__napi_register__contains_169']() - __napiInstance.exports['__napi_register__concat_str_170']() - __napiInstance.exports['__napi_register__concat_utf16_171']() - __napiInstance.exports['__napi_register__concat_latin1_172']() - __napiInstance.exports['__napi_register__roundtrip_str_173']() - __napiInstance.exports['__napi_register__set_symbol_in_obj_174']() - __napiInstance.exports['__napi_register__create_symbol_175']() - __napiInstance.exports['__napi_register__create_symbol_for_176']() - __napiInstance.exports['__napi_register__DelaySum_impl_177']() - __napiInstance.exports['__napi_register__without_abort_controller_178']() - __napiInstance.exports['__napi_register__with_abort_controller_179']() - __napiInstance.exports['__napi_register__call_threadsafe_function_180']() - __napiInstance.exports['__napi_register__call_long_threadsafe_function_181']() - __napiInstance.exports['__napi_register__threadsafe_function_throw_error_182']() - __napiInstance.exports['__napi_register__threadsafe_function_fatal_mode_183']() - __napiInstance.exports['__napi_register__threadsafe_function_fatal_mode_error_184']() - __napiInstance.exports['__napi_register__threadsafe_function_closure_capture_185']() - __napiInstance.exports['__napi_register__tsfn_call_with_callback_186']() - __napiInstance.exports['__napi_register__tsfn_async_call_187']() - __napiInstance.exports['__napi_register__accept_threadsafe_function_188']() - __napiInstance.exports['__napi_register__accept_threadsafe_function_fatal_189']() - __napiInstance.exports['__napi_register__accept_threadsafe_function_tuple_args_190']() - __napiInstance.exports['__napi_register__tsfn_return_promise_191']() - __napiInstance.exports['__napi_register__tsfn_return_promise_timeout_192']() - __napiInstance.exports['__napi_register__tsfn_throw_from_js_193']() - __napiInstance.exports['__napi_register__get_buffer_194']() - __napiInstance.exports['__napi_register__append_buffer_195']() - __napiInstance.exports['__napi_register__get_empty_buffer_196']() - __napiInstance.exports['__napi_register__convert_u32_array_197']() - __napiInstance.exports['__napi_register__create_external_typed_array_198']() - __napiInstance.exports['__napi_register__mutate_typed_array_199']() - __napiInstance.exports['__napi_register__deref_uint8_array_200']() - __napiInstance.exports['__napi_register__buffer_pass_through_201']() - __napiInstance.exports['__napi_register__array_buffer_pass_through_202']() - __napiInstance.exports['__napi_register__AsyncBuffer_impl_203']() - __napiInstance.exports['__napi_register__async_reduce_buffer_204']() - __napiInstance.exports['__napi_register__run_script_205']() + __napiInstance.exports['__napi_register__run_script_86']() + __napiInstance.exports['__napi_register__get_module_file_name_87']() + __napiInstance.exports['__napi_register__throw_syntax_error_88']() + __napiInstance.exports['__napi_register__throw_error_89']() + __napiInstance.exports['__napi_register__panic_90']() + __napiInstance.exports['__napi_register__receive_string_91']() + __napiInstance.exports['__napi_register__custom_status_code_92']() + __napiInstance.exports['__napi_register__throw_async_error_93']() + __napiInstance.exports['__napi_register__create_external_94']() + __napiInstance.exports['__napi_register__create_external_string_95']() + __napiInstance.exports['__napi_register__get_external_96']() + __napiInstance.exports['__napi_register__mutate_external_97']() + __napiInstance.exports['__napi_register__validate_array_98']() + __napiInstance.exports['__napi_register__validate_buffer_99']() + __napiInstance.exports['__napi_register__validate_typed_array_100']() + __napiInstance.exports['__napi_register__validate_bigint_101']() + __napiInstance.exports['__napi_register__validate_boolean_102']() + __napiInstance.exports['__napi_register__validate_date_103']() + __napiInstance.exports['__napi_register__validate_date_time_104']() + __napiInstance.exports['__napi_register__validate_external_105']() + __napiInstance.exports['__napi_register__validate_function_106']() + __napiInstance.exports['__napi_register__validate_hash_map_107']() + __napiInstance.exports['__napi_register__validate_null_108']() + __napiInstance.exports['__napi_register__validate_undefined_109']() + __napiInstance.exports['__napi_register__validate_number_110']() + __napiInstance.exports['__napi_register__validate_promise_111']() + __napiInstance.exports['__napi_register__validate_string_112']() + __napiInstance.exports['__napi_register__validate_symbol_113']() + __napiInstance.exports['__napi_register__validate_optional_114']() + __napiInstance.exports['__napi_register__return_undefined_if_invalid_115']() + __napiInstance.exports['__napi_register__return_undefined_if_invalid_promise_116']() + __napiInstance.exports['__napi_register__ts_rename_117']() + __napiInstance.exports['__napi_register__override_individual_arg_on_function_118']() + __napiInstance.exports['__napi_register__override_individual_arg_on_function_with_cb_arg_119']() + __napiInstance.exports['__napi_register__Fib_struct_120']() + __napiInstance.exports['__napi_register__Fib_impl_121']() + __napiInstance.exports['__napi_register__Fib_impl_122']() + __napiInstance.exports['__napi_register__Fib2_struct_123']() + __napiInstance.exports['__napi_register__Fib2_impl_124']() + __napiInstance.exports['__napi_register__Fib2_impl_125']() + __napiInstance.exports['__napi_register__Fib3_struct_126']() + __napiInstance.exports['__napi_register__Fib3_impl_127']() + __napiInstance.exports['__napi_register__ALIGNMENT_128']() + __napiInstance.exports['__napi_register__xxh64_129']() + __napiInstance.exports['__napi_register__xxh128_130']() + __napiInstance.exports['__napi_register__Xxh3_struct_131']() + __napiInstance.exports['__napi_register__Xxh3_impl_132']() + __napiInstance.exports['__napi_register__xxh2_plus_133']() + __napiInstance.exports['__napi_register__xxh3_xxh64_alias_134']() + __napiInstance.exports['__napi_register__xxh64_alias_135']() + __napiInstance.exports['__napi_register__get_mapping_136']() + __napiInstance.exports['__napi_register__sum_mapping_137']() + __napiInstance.exports['__napi_register__map_option_138']() + __napiInstance.exports['__napi_register__return_null_139']() + __napiInstance.exports['__napi_register__return_undefined_140']() + __napiInstance.exports['__napi_register__add_141']() + __napiInstance.exports['__napi_register__fibonacci_142']() + __napiInstance.exports['__napi_register__list_obj_keys_143']() + __napiInstance.exports['__napi_register__create_obj_144']() + __napiInstance.exports['__napi_register__get_global_145']() + __napiInstance.exports['__napi_register__get_undefined_146']() + __napiInstance.exports['__napi_register__get_null_147']() + __napiInstance.exports['__napi_register__receive_all_optional_object_148']() + __napiInstance.exports['__napi_register__AliasedEnum_149']() + __napiInstance.exports['__napi_register__fn_received_aliased_150']() + __napiInstance.exports['__napi_register__receive_strict_object_151']() + __napiInstance.exports['__napi_register__get_str_from_object_152']() + __napiInstance.exports['__napi_register__create_obj_with_property_153']() + __napiInstance.exports['__napi_register__getter_from_obj_154']() + __napiInstance.exports['__napi_register__receive_object_only_from_js_155']() + __napiInstance.exports['__napi_register__async_plus_100_156']() + __napiInstance.exports['__napi_register__JsRepo_struct_157']() + __napiInstance.exports['__napi_register__JsRepo_impl_158']() + __napiInstance.exports['__napi_register__JsRemote_struct_159']() + __napiInstance.exports['__napi_register__JsRemote_impl_160']() + __napiInstance.exports['__napi_register__CSSRuleList_struct_161']() + __napiInstance.exports['__napi_register__CSSRuleList_impl_162']() + __napiInstance.exports['__napi_register__CSSStyleSheet_struct_163']() + __napiInstance.exports['__napi_register__AnotherCSSStyleSheet_struct_164']() + __napiInstance.exports['__napi_register__AnotherCSSStyleSheet_impl_165']() + __napiInstance.exports['__napi_register__CSSStyleSheet_impl_166']() + __napiInstance.exports['__napi_register__read_package_json_167']() + __napiInstance.exports['__napi_register__get_package_json_name_168']() + __napiInstance.exports['__napi_register__test_serde_roundtrip_169']() + __napiInstance.exports['__napi_register__test_serde_big_number_precision_170']() + __napiInstance.exports['__napi_register__return_from_shared_crate_171']() + __napiInstance.exports['__napi_register__contains_172']() + __napiInstance.exports['__napi_register__concat_str_173']() + __napiInstance.exports['__napi_register__concat_utf16_174']() + __napiInstance.exports['__napi_register__concat_latin1_175']() + __napiInstance.exports['__napi_register__roundtrip_str_176']() + __napiInstance.exports['__napi_register__set_symbol_in_obj_177']() + __napiInstance.exports['__napi_register__create_symbol_178']() + __napiInstance.exports['__napi_register__create_symbol_for_179']() + __napiInstance.exports['__napi_register__DelaySum_impl_180']() + __napiInstance.exports['__napi_register__without_abort_controller_181']() + __napiInstance.exports['__napi_register__with_abort_controller_182']() + __napiInstance.exports['__napi_register__call_threadsafe_function_183']() + __napiInstance.exports['__napi_register__call_long_threadsafe_function_184']() + __napiInstance.exports['__napi_register__threadsafe_function_throw_error_185']() + __napiInstance.exports['__napi_register__threadsafe_function_fatal_mode_186']() + __napiInstance.exports['__napi_register__threadsafe_function_fatal_mode_error_187']() + __napiInstance.exports['__napi_register__threadsafe_function_closure_capture_188']() + __napiInstance.exports['__napi_register__tsfn_call_with_callback_189']() + __napiInstance.exports['__napi_register__tsfn_async_call_190']() + __napiInstance.exports['__napi_register__accept_threadsafe_function_191']() + __napiInstance.exports['__napi_register__accept_threadsafe_function_fatal_192']() + __napiInstance.exports['__napi_register__accept_threadsafe_function_tuple_args_193']() + __napiInstance.exports['__napi_register__tsfn_return_promise_194']() + __napiInstance.exports['__napi_register__tsfn_return_promise_timeout_195']() + __napiInstance.exports['__napi_register__tsfn_throw_from_js_196']() + __napiInstance.exports['__napi_register__get_buffer_197']() + __napiInstance.exports['__napi_register__append_buffer_198']() + __napiInstance.exports['__napi_register__get_empty_buffer_199']() + __napiInstance.exports['__napi_register__convert_u32_array_200']() + __napiInstance.exports['__napi_register__create_external_typed_array_201']() + __napiInstance.exports['__napi_register__mutate_typed_array_202']() + __napiInstance.exports['__napi_register__deref_uint8_array_203']() + __napiInstance.exports['__napi_register__buffer_pass_through_204']() + __napiInstance.exports['__napi_register__array_buffer_pass_through_205']() + __napiInstance.exports['__napi_register__AsyncBuffer_impl_206']() + __napiInstance.exports['__napi_register__async_reduce_buffer_207']() } const binding = __napiModule.exports -const { Animal, AnimalWithDefaultConstructor, AnotherClassForEither, AnotherCssStyleSheet, AnotherCSSStyleSheet, Asset, JsAsset, Assets, JsAssets, Bird, Blake2BHasher, Blake2bHasher, Blake2BKey, Blake2bKey, ClassWithFactory, Context, CssRuleList, CSSRuleList, CssStyleSheet, CSSStyleSheet, CustomFinalize, Dog, Fib, Fib2, Fib3, GetterSetterWithClosures, JsClassForEither, JsRemote, JsRepo, NinjaTurtle, NotWritableClass, Optional, Selector, Width, acceptThreadsafeFunction, acceptThreadsafeFunctionFatal, acceptThreadsafeFunctionTupleArgs, add, ALIAS, AliasedEnum, appendBuffer, arrayBufferPassThrough, asyncMultiTwo, asyncPlus100, asyncReduceBuffer, bigintAdd, bigintFromI128, bigintFromI64, bigintGetU64AsString, bufferPassThrough, callbackReturnPromise, callbackReturnPromiseAndSpawn, callLongThreadsafeFunction, callThreadsafeFunction, captureErrorInCallback, chronoDateAdd1Minute, chronoDateToMillis, chronoNativeDateTime, chronoNativeDateTimeReturn, concatLatin1, concatStr, concatUtf16, contains, convertU32Array, createBigInt, createBigIntI64, createExternal, createExternalString, createExternalTypedArray, createObj, createObjectWithClassField, createObjWithProperty, createSymbol, createSymbolFor, CustomNumEnum, customStatusCode, dateToNumber, DEFAULT_COST, derefUint8Array, either3, either4, eitherBoolOrFunction, eitherFromObjects, eitherFromOption, eitherStringOrNumber, Empty, enumToI32, fibonacci, fnReceivedAliased, getBuffer, getCwd, getEmptyBuffer, getExternal, getGlobal, getMapping, getNestedNumArr, getNull, getNumArr, getNums, getPackageJsonName, getStrFromObject, getterFromObj, getUndefined, getWords, Kind, listObjKeys, mapOption, mutateExternal, mutateTypedArray, optionEnd, optionOnly, optionStart, optionStartEnd, overrideIndividualArgOnFunction, overrideIndividualArgOnFunctionWithCbArg, panic, plusOne, promiseInEither, readFile, readFileAsync, readPackageJson, receiveAllOptionalObject, receiveClassOrNumber, receiveDifferentClass, receiveMutClassOrNumber, receiveObjectOnlyFromJs, receiveObjectWithClassField, receiveStrictObject, receiveString, returnEither, returnEitherClass, returnFromSharedCrate, returnJsFunction, returnNull, returnUndefined, returnUndefinedIfInvalid, returnUndefinedIfInvalidPromise, roundtripStr, runScript, setSymbolInObj, Status, sumMapping, sumNums, testSerdeBigNumberPrecision, testSerdeRoundtrip, threadsafeFunctionClosureCapture, threadsafeFunctionFatalMode, threadsafeFunctionFatalModeError, threadsafeFunctionThrowError, throwAsyncError, throwError, toJsObj, tsfnAsyncCall, tsfnCallWithCallback, tsfnReturnPromise, tsfnReturnPromiseTimeout, tsfnThrowFromJs, tsRename, validateArray, validateBigint, validateBoolean, validateBuffer, validateDate, validateDateTime, validateExternal, validateFunction, validateHashMap, validateNull, validateNumber, validateOptional, validatePromise, validateString, validateSymbol, validateTypedArray, validateUndefined, withAbortController, withoutAbortController, xxh64Alias, xxh2, xxh3 } = binding +const { Animal, AnimalWithDefaultConstructor, AnotherClassForEither, AnotherCssStyleSheet, AnotherCSSStyleSheet, Asset, JsAsset, Assets, JsAssets, Bird, Blake2BHasher, Blake2bHasher, Blake2BKey, Blake2bKey, ClassWithFactory, Context, CssRuleList, CSSRuleList, CssStyleSheet, CSSStyleSheet, CustomFinalize, Dog, Fib, Fib2, Fib3, GetterSetterWithClosures, JsClassForEither, JsRemote, JsRepo, NinjaTurtle, NotWritableClass, Optional, Selector, Width, acceptThreadsafeFunction, acceptThreadsafeFunctionFatal, acceptThreadsafeFunctionTupleArgs, add, ALIAS, AliasedEnum, appendBuffer, arrayBufferPassThrough, asyncMultiTwo, asyncPlus100, asyncReduceBuffer, bigintAdd, bigintFromI128, bigintFromI64, bigintGetU64AsString, bufferPassThrough, callbackReturnPromise, callbackReturnPromiseAndSpawn, callLongThreadsafeFunction, callThreadsafeFunction, captureErrorInCallback, chronoDateAdd1Minute, chronoDateToMillis, chronoNativeDateTime, chronoNativeDateTimeReturn, concatLatin1, concatStr, concatUtf16, contains, convertU32Array, createBigInt, createBigIntI64, createExternal, createExternalString, createExternalTypedArray, createObj, createObjectWithClassField, createObjWithProperty, createSymbol, createSymbolFor, CustomNumEnum, customStatusCode, dateToNumber, DEFAULT_COST, derefUint8Array, either3, either4, eitherBoolOrFunction, eitherFromObjects, eitherFromOption, eitherStringOrNumber, Empty, enumToI32, fibonacci, fnReceivedAliased, getBuffer, getCwd, getEmptyBuffer, getExternal, getGlobal, getMapping, getModuleFileName, getNestedNumArr, getNull, getNumArr, getNums, getPackageJsonName, getStrFromObject, getterFromObj, getUndefined, getWords, Kind, listObjKeys, mapOption, mutateExternal, mutateTypedArray, optionEnd, optionOnly, optionStart, optionStartEnd, overrideIndividualArgOnFunction, overrideIndividualArgOnFunctionWithCbArg, panic, plusOne, promiseInEither, readFile, readFileAsync, readPackageJson, receiveAllOptionalObject, receiveClassOrNumber, receiveDifferentClass, receiveMutClassOrNumber, receiveObjectOnlyFromJs, receiveObjectWithClassField, receiveStrictObject, receiveString, returnEither, returnEitherClass, returnFromSharedCrate, returnJsFunction, returnNull, returnUndefined, returnUndefinedIfInvalid, returnUndefinedIfInvalidPromise, roundtripStr, runScript, setSymbolInObj, Status, sumMapping, sumNums, testSerdeBigNumberPrecision, testSerdeRoundtrip, threadsafeFunctionClosureCapture, threadsafeFunctionFatalMode, threadsafeFunctionFatalModeError, threadsafeFunctionThrowError, throwAsyncError, throwError, throwSyntaxError, toJsObj, tsfnAsyncCall, tsfnCallWithCallback, tsfnReturnPromise, tsfnReturnPromiseTimeout, tsfnThrowFromJs, tsRename, validateArray, validateBigint, validateBoolean, validateBuffer, validateDate, validateDateTime, validateExternal, validateFunction, validateHashMap, validateNull, validateNumber, validateOptional, validatePromise, validateString, validateSymbol, validateTypedArray, validateUndefined, withAbortController, withoutAbortController, xxh64Alias, xxh2, xxh3 } = binding export { Animal, AnimalWithDefaultConstructor, @@ -360,6 +361,7 @@ export { getExternal, getGlobal, getMapping, + getModuleFileName, getNestedNumArr, getNull, getNumArr, @@ -416,6 +418,7 @@ export { threadsafeFunctionThrowError, throwAsyncError, throwError, + throwSyntaxError, toJsObj, tsfnAsyncCall, tsfnCallWithCallback, diff --git a/examples/napi/src/env.rs b/examples/napi/src/env.rs new file mode 100644 index 00000000..cb153424 --- /dev/null +++ b/examples/napi/src/env.rs @@ -0,0 +1,16 @@ +use napi::{bindgen_prelude::*, Env}; + +#[napi] +pub fn run_script(env: Env, script: String) -> Result { + env.run_script(script) +} + +#[napi] +pub fn get_module_file_name(env: Env) -> Result { + env.get_module_file_name() +} + +#[napi] +pub fn throw_syntax_error(env: Env, error: String, code: Option) { + env.throw_syntax_error(error, code); +} diff --git a/examples/napi/src/lib.rs b/examples/napi/src/lib.rs index cb2b24da..ec552067 100644 --- a/examples/napi/src/lib.rs +++ b/examples/napi/src/lib.rs @@ -3,8 +3,6 @@ #![allow(clippy::disallowed_names)] #![allow(clippy::uninlined_format_args)] -use napi::{Env, JsUnknown}; - #[macro_use] extern crate napi_derive; #[macro_use] @@ -31,6 +29,7 @@ mod constructor; mod date; mod either; mod r#enum; +mod env; mod error; mod external; mod fn_strict; @@ -50,8 +49,3 @@ mod symbol; mod task; mod threadsafe_function; mod typed_array; - -#[napi] -pub fn run_script(env: Env, script: String) -> napi::Result { - env.run_script(script) -}