napi-rs/test_module/src/lib.rs

59 lines
2.1 KiB
Rust
Raw Normal View History

2018-04-28 17:26:38 +09:00
#[macro_use]
extern crate napi_rs as napi;
#[macro_use]
extern crate napi_rs_derive;
2018-04-28 17:26:38 +09:00
use napi::{CallContext, Env, Error, JsBoolean, JsString, JsUnknown, Module, Result, Status};
2018-04-28 17:26:38 +09:00
mod buffer;
mod external;
mod symbol;
mod task;
mod tsfn;
2018-04-28 17:26:38 +09:00
use buffer::{buffer_to_string, get_buffer_length};
use external::{create_external, get_external_count};
use symbol::{create_named_symbol, create_symbol_from_js_string, create_unnamed_symbol};
use task::test_spawn_thread;
use tsfn::{test_threadsafe_function, test_tokio_readfile, test_tsfn_error};
register_module!(test_module, init);
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("testObjectIsDate", test_object_is_date)?;
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)?;
module.create_named_method("testTsfnError", test_tsfn_error)?;
module.create_named_method("testThreadsafeFunction", test_threadsafe_function)?;
module.create_named_method("testTokioReadfile", test_tokio_readfile)?;
Ok(())
2018-04-28 17:26:38 +09:00
}
2020-04-21 01:20:35 +09:00
#[js_function]
fn test_throw(_ctx: CallContext) -> Result<JsUnknown> {
Err(Error::from_status(Status::GenericFailure))
2018-04-28 17:26:38 +09:00
}
2020-06-15 22:15:37 +09:00
#[js_function(1)]
fn test_throw_with_reason(ctx: CallContext) -> Result<JsUnknown> {
let reason = ctx.get::<JsString>(0)?;
Err(Error::new(
Status::GenericFailure,
reason.as_str()?.to_owned(),
))
}
#[js_function(1)]
fn test_object_is_date(ctx: CallContext) -> Result<JsBoolean> {
let obj = ctx.get::<JsUnknown>(0)?;
Env::get_boolean(ctx.env, obj.is_date()?)
}