test(napi): add tests for napi9 features (#1784)

This commit is contained in:
LongYinan 2023-11-06 11:03:44 +08:00 committed by GitHub
parent 4e4d891ad8
commit feabcd7f16
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 191 additions and 156 deletions

View file

@ -523,7 +523,6 @@ jobs:
runs-on: ubuntu-latest runs-on: ubuntu-latest
name: Test latest bun name: Test latest bun
timeout-minutes: 10 timeout-minutes: 10
continue-on-error: true
steps: steps:
- uses: actions/checkout@v4 - uses: actions/checkout@v4
- uses: oven-sh/setup-bun@v1 - uses: oven-sh/setup-bun@v1
@ -554,6 +553,7 @@ jobs:
bun run build bun run build
bun run build:test bun run build:test
- name: Test - name: Test
continue-on-error: true
run: bun run test:bun run: bun run test:bun
release-npm: release-npm:

View file

@ -37,7 +37,7 @@ napi5 = ["napi4", "napi-sys/napi5"]
napi6 = ["napi5", "napi-sys/napi6"] napi6 = ["napi5", "napi-sys/napi6"]
napi7 = ["napi6", "napi-sys/napi7"] napi7 = ["napi6", "napi-sys/napi7"]
napi8 = ["napi7", "napi-sys/napi8"] napi8 = ["napi7", "napi-sys/napi8"]
napi9 = ["napi8", "napi-sys/napi9", "byteorder"] napi9 = ["napi8", "napi-sys/napi9"]
noop = [] noop = []
serde-json = ["serde", "serde_json"] serde-json = ["serde", "serde_json"]
tokio_fs = ["tokio/fs"] tokio_fs = ["tokio/fs"]
@ -75,10 +75,6 @@ version = "0.8"
optional = true optional = true
version = "0.4" version = "0.4"
[dependencies.byteorder]
optional = true
version = "1.5"
[target.'cfg(target_os = "wasi")'.dependencies] [target.'cfg(target_os = "wasi")'.dependencies]
napi-derive = { path = "../macro", version = "2.10.1", default-features = false } napi-derive = { path = "../macro", version = "2.10.1", default-features = false }
tokio = { version = "1", optional = true, features = ["rt", "sync"] } tokio = { version = "1", optional = true, features = ["rt", "sync"] }

View file

@ -673,16 +673,19 @@ impl Env {
/// This API throws a JavaScript SyntaxError with the text provided. /// This API throws a JavaScript SyntaxError with the text provided.
#[cfg(feature = "napi9")] #[cfg(feature = "napi9")]
pub fn throw_syntax_error(&self, msg: &str, code: Option<&str>) -> Result<()> { pub fn throw_syntax_error<S: AsRef<str>, C: AsRef<str>>(&self, msg: S, code: Option<C>) {
let code = code.and_then(|s| CString::new(s).ok()); use crate::check_status_or_throw;
let msg = CString::new(msg)?;
check_status!(unsafe { let code = code.as_ref().map(|c| c.as_ref()).unwrap_or("");
sys::node_api_throw_syntax_error( 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, self.0,
code.map(|s| s.as_ptr()).unwrap_or(ptr::null_mut()), unsafe { sys::node_api_throw_syntax_error(self.0, code_ptr, msg_ptr,) },
msg.as_ptr(), "Throw syntax error failed"
) );
})
} }
#[allow(clippy::expect_fun_call)] #[allow(clippy::expect_fun_call)]
@ -1286,18 +1289,9 @@ impl Env {
// SAFETY: This is safe because `char_ptr` is guaranteed to not be `null`, and point to // SAFETY: This is safe because `char_ptr` is guaranteed to not be `null`, and point to
// null-terminated string data. // null-terminated string data.
let module_filename = unsafe { std::ffi::CStr::from_ptr(char_ptr) }; 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` /// ### Serialize `Rust Struct` into `JavaScript Value`
/// ///

View file

@ -372,6 +372,8 @@ Generated by [AVA](https://avajs.dev).
export function getMapping(): Record<string, number> export function getMapping(): Record<string, number>
export function getModuleFileName(): string␊
export function getNestedNumArr(): number[][][]␊ export function getNestedNumArr(): number[][][]␊
export function getNull(): null␊ export function getNull(): null␊
@ -528,6 +530,8 @@ Generated by [AVA](https://avajs.dev).
export function throwError(): void␊ export function throwError(): void␊
export function throwSyntaxError(error: string, code?: string | undefined | null): void␊
export function toJsObj(): object␊ export function toJsObj(): object␊
export function tsfnAsyncCall(func: (...args: any[]) => any): Promise<void> export function tsfnAsyncCall(func: (...args: any[]) => any): Promise<void>

View file

@ -139,6 +139,8 @@ const {
chronoNativeDateTime, chronoNativeDateTime,
chronoNativeDateTimeReturn, chronoNativeDateTimeReturn,
throwAsyncError, throwAsyncError,
getModuleFileName,
throwSyntaxError,
} = (await import('../index.js')).default } = (await import('../index.js')).default
const Napi4Test = Number(process.versions.napi) >= 4 ? test : test.skip const Napi4Test = Number(process.versions.napi) >= 4 ? test : test.skip
@ -1069,3 +1071,25 @@ const Napi9Test = Number(process.versions.napi) >= 9 ? test : test.skip
Napi9Test('create symbol for', (t) => { Napi9Test('create symbol for', (t) => {
t.is(createSymbolFor('foo'), Symbol.for('foo')) 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,
)
})

View file

@ -362,6 +362,8 @@ export function getGlobal(): typeof global
export function getMapping(): Record<string, number> export function getMapping(): Record<string, number>
export function getModuleFileName(): string
export function getNestedNumArr(): number[][][] export function getNestedNumArr(): number[][][]
export function getNull(): null export function getNull(): null
@ -518,6 +520,8 @@ export function throwAsyncError(): Promise<void>
export function throwError(): void export function throwError(): void
export function throwSyntaxError(error: string, code?: string | undefined | null): void
export function toJsObj(): object export function toJsObj(): object
export function tsfnAsyncCall(func: (...args: any[]) => any): Promise<void> export function tsfnAsyncCall(func: (...args: any[]) => any): Promise<void>

View file

@ -1,4 +1,3 @@
/* tslint:disable */
/* eslint-disable */ /* eslint-disable */
/* prettier-ignore */ /* prettier-ignore */
@ -140,130 +139,132 @@ function __napi_rs_initialize_modules(__napiInstance) {
__napiInstance.exports['__napi_register__CustomNumEnum_83']() __napiInstance.exports['__napi_register__CustomNumEnum_83']()
__napiInstance.exports['__napi_register__enum_to_i32_84']() __napiInstance.exports['__napi_register__enum_to_i32_84']()
__napiInstance.exports['__napi_register__SkippedEnums_85']() __napiInstance.exports['__napi_register__SkippedEnums_85']()
__napiInstance.exports['__napi_register__throw_error_86']() __napiInstance.exports['__napi_register__run_script_86']()
__napiInstance.exports['__napi_register__panic_87']() __napiInstance.exports['__napi_register__get_module_file_name_87']()
__napiInstance.exports['__napi_register__receive_string_88']() __napiInstance.exports['__napi_register__throw_syntax_error_88']()
__napiInstance.exports['__napi_register__custom_status_code_89']() __napiInstance.exports['__napi_register__throw_error_89']()
__napiInstance.exports['__napi_register__throw_async_error_90']() __napiInstance.exports['__napi_register__panic_90']()
__napiInstance.exports['__napi_register__create_external_91']() __napiInstance.exports['__napi_register__receive_string_91']()
__napiInstance.exports['__napi_register__create_external_string_92']() __napiInstance.exports['__napi_register__custom_status_code_92']()
__napiInstance.exports['__napi_register__get_external_93']() __napiInstance.exports['__napi_register__throw_async_error_93']()
__napiInstance.exports['__napi_register__mutate_external_94']() __napiInstance.exports['__napi_register__create_external_94']()
__napiInstance.exports['__napi_register__validate_array_95']() __napiInstance.exports['__napi_register__create_external_string_95']()
__napiInstance.exports['__napi_register__validate_buffer_96']() __napiInstance.exports['__napi_register__get_external_96']()
__napiInstance.exports['__napi_register__validate_typed_array_97']() __napiInstance.exports['__napi_register__mutate_external_97']()
__napiInstance.exports['__napi_register__validate_bigint_98']() __napiInstance.exports['__napi_register__validate_array_98']()
__napiInstance.exports['__napi_register__validate_boolean_99']() __napiInstance.exports['__napi_register__validate_buffer_99']()
__napiInstance.exports['__napi_register__validate_date_100']() __napiInstance.exports['__napi_register__validate_typed_array_100']()
__napiInstance.exports['__napi_register__validate_date_time_101']() __napiInstance.exports['__napi_register__validate_bigint_101']()
__napiInstance.exports['__napi_register__validate_external_102']() __napiInstance.exports['__napi_register__validate_boolean_102']()
__napiInstance.exports['__napi_register__validate_function_103']() __napiInstance.exports['__napi_register__validate_date_103']()
__napiInstance.exports['__napi_register__validate_hash_map_104']() __napiInstance.exports['__napi_register__validate_date_time_104']()
__napiInstance.exports['__napi_register__validate_null_105']() __napiInstance.exports['__napi_register__validate_external_105']()
__napiInstance.exports['__napi_register__validate_undefined_106']() __napiInstance.exports['__napi_register__validate_function_106']()
__napiInstance.exports['__napi_register__validate_number_107']() __napiInstance.exports['__napi_register__validate_hash_map_107']()
__napiInstance.exports['__napi_register__validate_promise_108']() __napiInstance.exports['__napi_register__validate_null_108']()
__napiInstance.exports['__napi_register__validate_string_109']() __napiInstance.exports['__napi_register__validate_undefined_109']()
__napiInstance.exports['__napi_register__validate_symbol_110']() __napiInstance.exports['__napi_register__validate_number_110']()
__napiInstance.exports['__napi_register__validate_optional_111']() __napiInstance.exports['__napi_register__validate_promise_111']()
__napiInstance.exports['__napi_register__return_undefined_if_invalid_112']() __napiInstance.exports['__napi_register__validate_string_112']()
__napiInstance.exports['__napi_register__return_undefined_if_invalid_promise_113']() __napiInstance.exports['__napi_register__validate_symbol_113']()
__napiInstance.exports['__napi_register__ts_rename_114']() __napiInstance.exports['__napi_register__validate_optional_114']()
__napiInstance.exports['__napi_register__override_individual_arg_on_function_115']() __napiInstance.exports['__napi_register__return_undefined_if_invalid_115']()
__napiInstance.exports['__napi_register__override_individual_arg_on_function_with_cb_arg_116']() __napiInstance.exports['__napi_register__return_undefined_if_invalid_promise_116']()
__napiInstance.exports['__napi_register__Fib_struct_117']() __napiInstance.exports['__napi_register__ts_rename_117']()
__napiInstance.exports['__napi_register__Fib_impl_118']() __napiInstance.exports['__napi_register__override_individual_arg_on_function_118']()
__napiInstance.exports['__napi_register__Fib_impl_119']() __napiInstance.exports['__napi_register__override_individual_arg_on_function_with_cb_arg_119']()
__napiInstance.exports['__napi_register__Fib2_struct_120']() __napiInstance.exports['__napi_register__Fib_struct_120']()
__napiInstance.exports['__napi_register__Fib2_impl_121']() __napiInstance.exports['__napi_register__Fib_impl_121']()
__napiInstance.exports['__napi_register__Fib2_impl_122']() __napiInstance.exports['__napi_register__Fib_impl_122']()
__napiInstance.exports['__napi_register__Fib3_struct_123']() __napiInstance.exports['__napi_register__Fib2_struct_123']()
__napiInstance.exports['__napi_register__Fib3_impl_124']() __napiInstance.exports['__napi_register__Fib2_impl_124']()
__napiInstance.exports['__napi_register__ALIGNMENT_125']() __napiInstance.exports['__napi_register__Fib2_impl_125']()
__napiInstance.exports['__napi_register__xxh64_126']() __napiInstance.exports['__napi_register__Fib3_struct_126']()
__napiInstance.exports['__napi_register__xxh128_127']() __napiInstance.exports['__napi_register__Fib3_impl_127']()
__napiInstance.exports['__napi_register__Xxh3_struct_128']() __napiInstance.exports['__napi_register__ALIGNMENT_128']()
__napiInstance.exports['__napi_register__Xxh3_impl_129']() __napiInstance.exports['__napi_register__xxh64_129']()
__napiInstance.exports['__napi_register__xxh2_plus_130']() __napiInstance.exports['__napi_register__xxh128_130']()
__napiInstance.exports['__napi_register__xxh3_xxh64_alias_131']() __napiInstance.exports['__napi_register__Xxh3_struct_131']()
__napiInstance.exports['__napi_register__xxh64_alias_132']() __napiInstance.exports['__napi_register__Xxh3_impl_132']()
__napiInstance.exports['__napi_register__get_mapping_133']() __napiInstance.exports['__napi_register__xxh2_plus_133']()
__napiInstance.exports['__napi_register__sum_mapping_134']() __napiInstance.exports['__napi_register__xxh3_xxh64_alias_134']()
__napiInstance.exports['__napi_register__map_option_135']() __napiInstance.exports['__napi_register__xxh64_alias_135']()
__napiInstance.exports['__napi_register__return_null_136']() __napiInstance.exports['__napi_register__get_mapping_136']()
__napiInstance.exports['__napi_register__return_undefined_137']() __napiInstance.exports['__napi_register__sum_mapping_137']()
__napiInstance.exports['__napi_register__add_138']() __napiInstance.exports['__napi_register__map_option_138']()
__napiInstance.exports['__napi_register__fibonacci_139']() __napiInstance.exports['__napi_register__return_null_139']()
__napiInstance.exports['__napi_register__list_obj_keys_140']() __napiInstance.exports['__napi_register__return_undefined_140']()
__napiInstance.exports['__napi_register__create_obj_141']() __napiInstance.exports['__napi_register__add_141']()
__napiInstance.exports['__napi_register__get_global_142']() __napiInstance.exports['__napi_register__fibonacci_142']()
__napiInstance.exports['__napi_register__get_undefined_143']() __napiInstance.exports['__napi_register__list_obj_keys_143']()
__napiInstance.exports['__napi_register__get_null_144']() __napiInstance.exports['__napi_register__create_obj_144']()
__napiInstance.exports['__napi_register__receive_all_optional_object_145']() __napiInstance.exports['__napi_register__get_global_145']()
__napiInstance.exports['__napi_register__AliasedEnum_146']() __napiInstance.exports['__napi_register__get_undefined_146']()
__napiInstance.exports['__napi_register__fn_received_aliased_147']() __napiInstance.exports['__napi_register__get_null_147']()
__napiInstance.exports['__napi_register__receive_strict_object_148']() __napiInstance.exports['__napi_register__receive_all_optional_object_148']()
__napiInstance.exports['__napi_register__get_str_from_object_149']() __napiInstance.exports['__napi_register__AliasedEnum_149']()
__napiInstance.exports['__napi_register__create_obj_with_property_150']() __napiInstance.exports['__napi_register__fn_received_aliased_150']()
__napiInstance.exports['__napi_register__getter_from_obj_151']() __napiInstance.exports['__napi_register__receive_strict_object_151']()
__napiInstance.exports['__napi_register__receive_object_only_from_js_152']() __napiInstance.exports['__napi_register__get_str_from_object_152']()
__napiInstance.exports['__napi_register__async_plus_100_153']() __napiInstance.exports['__napi_register__create_obj_with_property_153']()
__napiInstance.exports['__napi_register__JsRepo_struct_154']() __napiInstance.exports['__napi_register__getter_from_obj_154']()
__napiInstance.exports['__napi_register__JsRepo_impl_155']() __napiInstance.exports['__napi_register__receive_object_only_from_js_155']()
__napiInstance.exports['__napi_register__JsRemote_struct_156']() __napiInstance.exports['__napi_register__async_plus_100_156']()
__napiInstance.exports['__napi_register__JsRemote_impl_157']() __napiInstance.exports['__napi_register__JsRepo_struct_157']()
__napiInstance.exports['__napi_register__CSSRuleList_struct_158']() __napiInstance.exports['__napi_register__JsRepo_impl_158']()
__napiInstance.exports['__napi_register__CSSRuleList_impl_159']() __napiInstance.exports['__napi_register__JsRemote_struct_159']()
__napiInstance.exports['__napi_register__CSSStyleSheet_struct_160']() __napiInstance.exports['__napi_register__JsRemote_impl_160']()
__napiInstance.exports['__napi_register__AnotherCSSStyleSheet_struct_161']() __napiInstance.exports['__napi_register__CSSRuleList_struct_161']()
__napiInstance.exports['__napi_register__AnotherCSSStyleSheet_impl_162']() __napiInstance.exports['__napi_register__CSSRuleList_impl_162']()
__napiInstance.exports['__napi_register__CSSStyleSheet_impl_163']() __napiInstance.exports['__napi_register__CSSStyleSheet_struct_163']()
__napiInstance.exports['__napi_register__read_package_json_164']() __napiInstance.exports['__napi_register__AnotherCSSStyleSheet_struct_164']()
__napiInstance.exports['__napi_register__get_package_json_name_165']() __napiInstance.exports['__napi_register__AnotherCSSStyleSheet_impl_165']()
__napiInstance.exports['__napi_register__test_serde_roundtrip_166']() __napiInstance.exports['__napi_register__CSSStyleSheet_impl_166']()
__napiInstance.exports['__napi_register__test_serde_big_number_precision_167']() __napiInstance.exports['__napi_register__read_package_json_167']()
__napiInstance.exports['__napi_register__return_from_shared_crate_168']() __napiInstance.exports['__napi_register__get_package_json_name_168']()
__napiInstance.exports['__napi_register__contains_169']() __napiInstance.exports['__napi_register__test_serde_roundtrip_169']()
__napiInstance.exports['__napi_register__concat_str_170']() __napiInstance.exports['__napi_register__test_serde_big_number_precision_170']()
__napiInstance.exports['__napi_register__concat_utf16_171']() __napiInstance.exports['__napi_register__return_from_shared_crate_171']()
__napiInstance.exports['__napi_register__concat_latin1_172']() __napiInstance.exports['__napi_register__contains_172']()
__napiInstance.exports['__napi_register__roundtrip_str_173']() __napiInstance.exports['__napi_register__concat_str_173']()
__napiInstance.exports['__napi_register__set_symbol_in_obj_174']() __napiInstance.exports['__napi_register__concat_utf16_174']()
__napiInstance.exports['__napi_register__create_symbol_175']() __napiInstance.exports['__napi_register__concat_latin1_175']()
__napiInstance.exports['__napi_register__create_symbol_for_176']() __napiInstance.exports['__napi_register__roundtrip_str_176']()
__napiInstance.exports['__napi_register__DelaySum_impl_177']() __napiInstance.exports['__napi_register__set_symbol_in_obj_177']()
__napiInstance.exports['__napi_register__without_abort_controller_178']() __napiInstance.exports['__napi_register__create_symbol_178']()
__napiInstance.exports['__napi_register__with_abort_controller_179']() __napiInstance.exports['__napi_register__create_symbol_for_179']()
__napiInstance.exports['__napi_register__call_threadsafe_function_180']() __napiInstance.exports['__napi_register__DelaySum_impl_180']()
__napiInstance.exports['__napi_register__call_long_threadsafe_function_181']() __napiInstance.exports['__napi_register__without_abort_controller_181']()
__napiInstance.exports['__napi_register__threadsafe_function_throw_error_182']() __napiInstance.exports['__napi_register__with_abort_controller_182']()
__napiInstance.exports['__napi_register__threadsafe_function_fatal_mode_183']() __napiInstance.exports['__napi_register__call_threadsafe_function_183']()
__napiInstance.exports['__napi_register__threadsafe_function_fatal_mode_error_184']() __napiInstance.exports['__napi_register__call_long_threadsafe_function_184']()
__napiInstance.exports['__napi_register__threadsafe_function_closure_capture_185']() __napiInstance.exports['__napi_register__threadsafe_function_throw_error_185']()
__napiInstance.exports['__napi_register__tsfn_call_with_callback_186']() __napiInstance.exports['__napi_register__threadsafe_function_fatal_mode_186']()
__napiInstance.exports['__napi_register__tsfn_async_call_187']() __napiInstance.exports['__napi_register__threadsafe_function_fatal_mode_error_187']()
__napiInstance.exports['__napi_register__accept_threadsafe_function_188']() __napiInstance.exports['__napi_register__threadsafe_function_closure_capture_188']()
__napiInstance.exports['__napi_register__accept_threadsafe_function_fatal_189']() __napiInstance.exports['__napi_register__tsfn_call_with_callback_189']()
__napiInstance.exports['__napi_register__accept_threadsafe_function_tuple_args_190']() __napiInstance.exports['__napi_register__tsfn_async_call_190']()
__napiInstance.exports['__napi_register__tsfn_return_promise_191']() __napiInstance.exports['__napi_register__accept_threadsafe_function_191']()
__napiInstance.exports['__napi_register__tsfn_return_promise_timeout_192']() __napiInstance.exports['__napi_register__accept_threadsafe_function_fatal_192']()
__napiInstance.exports['__napi_register__tsfn_throw_from_js_193']() __napiInstance.exports['__napi_register__accept_threadsafe_function_tuple_args_193']()
__napiInstance.exports['__napi_register__get_buffer_194']() __napiInstance.exports['__napi_register__tsfn_return_promise_194']()
__napiInstance.exports['__napi_register__append_buffer_195']() __napiInstance.exports['__napi_register__tsfn_return_promise_timeout_195']()
__napiInstance.exports['__napi_register__get_empty_buffer_196']() __napiInstance.exports['__napi_register__tsfn_throw_from_js_196']()
__napiInstance.exports['__napi_register__convert_u32_array_197']() __napiInstance.exports['__napi_register__get_buffer_197']()
__napiInstance.exports['__napi_register__create_external_typed_array_198']() __napiInstance.exports['__napi_register__append_buffer_198']()
__napiInstance.exports['__napi_register__mutate_typed_array_199']() __napiInstance.exports['__napi_register__get_empty_buffer_199']()
__napiInstance.exports['__napi_register__deref_uint8_array_200']() __napiInstance.exports['__napi_register__convert_u32_array_200']()
__napiInstance.exports['__napi_register__buffer_pass_through_201']() __napiInstance.exports['__napi_register__create_external_typed_array_201']()
__napiInstance.exports['__napi_register__array_buffer_pass_through_202']() __napiInstance.exports['__napi_register__mutate_typed_array_202']()
__napiInstance.exports['__napi_register__AsyncBuffer_impl_203']() __napiInstance.exports['__napi_register__deref_uint8_array_203']()
__napiInstance.exports['__napi_register__async_reduce_buffer_204']() __napiInstance.exports['__napi_register__buffer_pass_through_204']()
__napiInstance.exports['__napi_register__run_script_205']() __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 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 { export {
Animal, Animal,
AnimalWithDefaultConstructor, AnimalWithDefaultConstructor,
@ -360,6 +361,7 @@ export {
getExternal, getExternal,
getGlobal, getGlobal,
getMapping, getMapping,
getModuleFileName,
getNestedNumArr, getNestedNumArr,
getNull, getNull,
getNumArr, getNumArr,
@ -416,6 +418,7 @@ export {
threadsafeFunctionThrowError, threadsafeFunctionThrowError,
throwAsyncError, throwAsyncError,
throwError, throwError,
throwSyntaxError,
toJsObj, toJsObj,
tsfnAsyncCall, tsfnAsyncCall,
tsfnCallWithCallback, tsfnCallWithCallback,

16
examples/napi/src/env.rs Normal file
View file

@ -0,0 +1,16 @@
use napi::{bindgen_prelude::*, Env};
#[napi]
pub fn run_script(env: Env, script: String) -> Result<Unknown> {
env.run_script(script)
}
#[napi]
pub fn get_module_file_name(env: Env) -> Result<String> {
env.get_module_file_name()
}
#[napi]
pub fn throw_syntax_error(env: Env, error: String, code: Option<String>) {
env.throw_syntax_error(error, code);
}

View file

@ -3,8 +3,6 @@
#![allow(clippy::disallowed_names)] #![allow(clippy::disallowed_names)]
#![allow(clippy::uninlined_format_args)] #![allow(clippy::uninlined_format_args)]
use napi::{Env, JsUnknown};
#[macro_use] #[macro_use]
extern crate napi_derive; extern crate napi_derive;
#[macro_use] #[macro_use]
@ -31,6 +29,7 @@ mod constructor;
mod date; mod date;
mod either; mod either;
mod r#enum; mod r#enum;
mod env;
mod error; mod error;
mod external; mod external;
mod fn_strict; mod fn_strict;
@ -50,8 +49,3 @@ mod symbol;
mod task; mod task;
mod threadsafe_function; mod threadsafe_function;
mod typed_array; mod typed_array;
#[napi]
pub fn run_script(env: Env, script: String) -> napi::Result<JsUnknown> {
env.run_script(script)
}