2018-04-28 17:26:38 +09:00
|
|
|
#[macro_use]
|
2020-07-15 01:43:20 +09:00
|
|
|
extern crate napi;
|
2020-04-26 21:21:45 +09:00
|
|
|
#[macro_use]
|
2020-07-15 01:43:20 +09:00
|
|
|
extern crate napi_derive;
|
2018-04-28 17:26:38 +09:00
|
|
|
|
2020-07-01 21:44:29 +09:00
|
|
|
use napi::{CallContext, Error, JsString, JsUnknown, Module, Result, Status};
|
|
|
|
|
2020-07-03 01:31:50 +09:00
|
|
|
#[cfg(napi4)]
|
|
|
|
mod libuv;
|
2020-07-08 01:59:09 +09:00
|
|
|
#[cfg(napi4)]
|
|
|
|
mod napi4;
|
2020-07-01 21:44:29 +09:00
|
|
|
#[cfg(napi5)]
|
|
|
|
mod napi5;
|
2020-07-08 01:59:09 +09:00
|
|
|
#[cfg(napi4)]
|
|
|
|
mod tokio_rt;
|
2018-04-28 17:26:38 +09:00
|
|
|
|
2020-06-21 20:10:06 +09:00
|
|
|
mod buffer;
|
2020-08-03 00:31:22 +09:00
|
|
|
mod class;
|
2020-07-18 03:00:48 +09:00
|
|
|
mod either;
|
2020-06-21 20:10:06 +09:00
|
|
|
mod external;
|
2020-07-08 01:59:09 +09:00
|
|
|
mod function;
|
|
|
|
mod napi_version;
|
2020-06-21 20:10:06 +09:00
|
|
|
mod symbol;
|
|
|
|
mod task;
|
2018-04-28 17:26:38 +09:00
|
|
|
|
2020-06-21 20:10:06 +09:00
|
|
|
use buffer::{buffer_to_string, get_buffer_length};
|
2020-07-18 03:00:48 +09:00
|
|
|
use either::{dynamic_argument_length, either_number_string};
|
2020-06-21 20:10:06 +09:00
|
|
|
use external::{create_external, get_external_count};
|
2020-07-08 01:59:09 +09:00
|
|
|
use function::{call_function, call_function_with_this};
|
|
|
|
#[cfg(napi4)]
|
|
|
|
use libuv::read_file::uv_read_file;
|
2020-07-03 01:31:50 +09:00
|
|
|
#[cfg(napi4)]
|
|
|
|
use napi4::{test_threadsafe_function, test_tokio_readfile, test_tsfn_error};
|
2020-07-01 21:44:29 +09:00
|
|
|
#[cfg(napi5)]
|
|
|
|
use napi5::is_date::test_object_is_date;
|
2020-07-08 01:59:09 +09:00
|
|
|
use napi_version::get_napi_version;
|
2020-06-21 20:10:06 +09:00
|
|
|
use symbol::{create_named_symbol, create_symbol_from_js_string, create_unnamed_symbol};
|
|
|
|
use task::test_spawn_thread;
|
2020-07-03 01:31:50 +09:00
|
|
|
#[cfg(napi4)]
|
2020-07-08 01:59:09 +09:00
|
|
|
use tokio_rt::{error_from_tokio_future, test_execute_tokio_readfile};
|
2020-05-11 01:28:06 +09:00
|
|
|
|
2020-06-21 20:10:06 +09:00
|
|
|
register_module!(test_module, init);
|
2020-05-11 01:28:06 +09:00
|
|
|
|
2020-06-21 20:10:06 +09:00
|
|
|
fn init(module: &mut Module) -> Result<()> {
|
|
|
|
module.create_named_method("testThrow", test_throw)?;
|
|
|
|
module.create_named_method("testThrowWithReason", test_throw_with_reason)?;
|
|
|
|
module.create_named_method("testSpawnThread", test_spawn_thread)?;
|
|
|
|
module.create_named_method("createExternal", create_external)?;
|
|
|
|
module.create_named_method("getExternalCount", get_external_count)?;
|
|
|
|
module.create_named_method("getBufferLength", get_buffer_length)?;
|
|
|
|
module.create_named_method("bufferToString", buffer_to_string)?;
|
|
|
|
module.create_named_method("createNamedSymbol", create_named_symbol)?;
|
|
|
|
module.create_named_method("createUnnamedSymbol", create_unnamed_symbol)?;
|
|
|
|
module.create_named_method("createSymbolFromJsString", create_symbol_from_js_string)?;
|
2020-07-03 01:31:50 +09:00
|
|
|
module.create_named_method("getNapiVersion", get_napi_version)?;
|
|
|
|
module.create_named_method("testCallFunction", call_function)?;
|
|
|
|
module.create_named_method("testCallFunctionWithThis", call_function_with_this)?;
|
2020-07-18 03:00:48 +09:00
|
|
|
module.create_named_method("eitherNumberString", either_number_string)?;
|
|
|
|
module.create_named_method("dynamicArgumentLength", dynamic_argument_length)?;
|
2020-08-03 00:31:22 +09:00
|
|
|
module.create_named_method("createTestClass", class::create_test_class)?;
|
2020-07-03 01:31:50 +09:00
|
|
|
#[cfg(napi4)]
|
2020-07-08 01:59:09 +09:00
|
|
|
module.create_named_method("testExecuteTokioReadfile", test_execute_tokio_readfile)?;
|
|
|
|
#[cfg(napi4)]
|
2020-06-21 20:10:06 +09:00
|
|
|
module.create_named_method("testTsfnError", test_tsfn_error)?;
|
2020-07-03 01:31:50 +09:00
|
|
|
#[cfg(napi4)]
|
2020-06-21 20:10:06 +09:00
|
|
|
module.create_named_method("testThreadsafeFunction", test_threadsafe_function)?;
|
2020-07-03 01:31:50 +09:00
|
|
|
#[cfg(napi4)]
|
2020-06-21 20:10:06 +09:00
|
|
|
module.create_named_method("testTokioReadfile", test_tokio_readfile)?;
|
2020-07-03 01:31:50 +09:00
|
|
|
#[cfg(napi4)]
|
2020-07-08 01:59:09 +09:00
|
|
|
module.create_named_method("testTokioError", error_from_tokio_future)?;
|
|
|
|
#[cfg(napi4)]
|
2020-07-03 01:31:50 +09:00
|
|
|
module.create_named_method("uvReadFile", uv_read_file)?;
|
2020-07-01 21:44:29 +09:00
|
|
|
#[cfg(napi5)]
|
|
|
|
module.create_named_method("testObjectIsDate", test_object_is_date)?;
|
2020-06-21 20:10:06 +09:00
|
|
|
Ok(())
|
2018-04-28 17:26:38 +09:00
|
|
|
}
|
|
|
|
|
2020-04-21 01:20:35 +09:00
|
|
|
#[js_function]
|
2020-06-21 20:10:06 +09:00
|
|
|
fn test_throw(_ctx: CallContext) -> Result<JsUnknown> {
|
2020-05-06 00:13:23 +09:00
|
|
|
Err(Error::from_status(Status::GenericFailure))
|
2018-04-28 17:26:38 +09:00
|
|
|
}
|
2020-06-15 22:15:37 +09:00
|
|
|
|
2020-06-17 21:33:28 +09:00
|
|
|
#[js_function(1)]
|
2020-06-21 20:10:06 +09:00
|
|
|
fn test_throw_with_reason(ctx: CallContext) -> Result<JsUnknown> {
|
2020-06-17 21:33:28 +09:00
|
|
|
let reason = ctx.get::<JsString>(0)?;
|
2020-06-21 20:10:06 +09:00
|
|
|
Err(Error::new(
|
|
|
|
Status::GenericFailure,
|
|
|
|
reason.as_str()?.to_owned(),
|
|
|
|
))
|
2020-06-19 17:16:28 +09:00
|
|
|
}
|
2020-07-19 17:54:10 +09:00
|
|
|
|
|
|
|
#[js_function]
|
|
|
|
pub fn test_throw_with_panic(_ctx: CallContext) -> Result<JsUnknown> {
|
|
|
|
panic!("don't panic.");
|
|
|
|
}
|