2021-11-15 19:56:06 +09:00
|
|
|
use napi::{bindgen_prelude::*, JsGlobal, JsNull, JsUndefined};
|
2021-09-23 02:29:09 +09:00
|
|
|
|
|
|
|
#[napi]
|
|
|
|
fn list_obj_keys(obj: Object) -> Vec<String> {
|
|
|
|
Object::keys(&obj).unwrap()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[napi]
|
|
|
|
fn create_obj(env: Env) -> Object {
|
|
|
|
let mut obj = env.create_object().unwrap();
|
2021-09-24 18:01:54 +09:00
|
|
|
obj.set("test", 1).unwrap();
|
2021-09-23 02:29:09 +09:00
|
|
|
|
|
|
|
obj
|
|
|
|
}
|
2021-11-15 19:56:06 +09:00
|
|
|
|
|
|
|
#[napi]
|
|
|
|
fn get_global(env: Env) -> Result<JsGlobal> {
|
|
|
|
env.get_global()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[napi]
|
|
|
|
fn get_undefined(env: Env) -> Result<JsUndefined> {
|
|
|
|
env.get_undefined()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[napi]
|
|
|
|
fn get_null(env: Env) -> Result<JsNull> {
|
|
|
|
env.get_null()
|
|
|
|
}
|
2021-12-02 16:47:59 +09:00
|
|
|
|
|
|
|
#[napi(object)]
|
|
|
|
struct AllOptionalObject {
|
|
|
|
pub name: Option<String>,
|
|
|
|
pub age: Option<u32>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[napi]
|
|
|
|
fn receive_all_optional_object(obj: Option<AllOptionalObject>) -> Result<()> {
|
2021-12-02 17:31:57 +09:00
|
|
|
if obj.is_some() {
|
2021-12-02 16:47:59 +09:00
|
|
|
assert!(obj.as_ref().unwrap().name.is_none());
|
|
|
|
assert!(obj.as_ref().unwrap().age.is_none());
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-12-02 17:25:20 +09:00
|
|
|
|
|
|
|
#[napi(js_name = "ALIAS")]
|
|
|
|
pub enum AliasedEnum {
|
|
|
|
A,
|
|
|
|
B,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[napi(object, js_name = "AliasedStruct")]
|
|
|
|
pub struct StructContainsAliasedEnum {
|
|
|
|
pub a: AliasedEnum,
|
|
|
|
pub b: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[napi]
|
|
|
|
fn fn_received_aliased(mut s: StructContainsAliasedEnum, e: AliasedEnum) {
|
|
|
|
s.a = e;
|
|
|
|
}
|
2022-01-06 16:56:14 +09:00
|
|
|
|
|
|
|
#[napi(object)]
|
|
|
|
pub struct StrictObject {
|
|
|
|
pub name: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[napi]
|
|
|
|
pub fn receive_strict_object(strict_object: StrictObject) {
|
|
|
|
assert_eq!(strict_object.name, "strict");
|
|
|
|
}
|
2022-01-12 16:19:05 +09:00
|
|
|
|
|
|
|
#[napi]
|
|
|
|
pub fn get_str_from_object(env: Env) {
|
|
|
|
let mut obj = env.create_object().unwrap();
|
|
|
|
obj.set("name", "value").unwrap();
|
|
|
|
assert_eq!(obj.get("name").unwrap(), Some("value"));
|
|
|
|
}
|