2022-06-04 02:07:39 +09:00
|
|
|
use napi::{
|
2022-07-06 20:08:34 +09:00
|
|
|
bindgen_prelude::{Buffer, ClassInstance, This, Uint8Array},
|
2022-06-04 02:07:39 +09:00
|
|
|
Env, Result,
|
|
|
|
};
|
2021-09-23 02:29:09 +09:00
|
|
|
|
|
|
|
use crate::r#enum::Kind;
|
|
|
|
|
2021-09-24 14:00:35 +09:00
|
|
|
/// `constructor` option for `struct` requires all fields to be public,
|
|
|
|
/// otherwise tag impl fn as constructor
|
|
|
|
/// #[napi(constructor)]
|
|
|
|
#[napi]
|
2021-09-23 02:29:09 +09:00
|
|
|
pub struct Animal {
|
|
|
|
#[napi(readonly)]
|
2021-11-29 13:54:45 +09:00
|
|
|
/// Kind of animal
|
2021-09-23 02:29:09 +09:00
|
|
|
pub kind: Kind,
|
2021-11-29 13:54:45 +09:00
|
|
|
|
2021-09-24 14:00:35 +09:00
|
|
|
name: String,
|
2021-09-23 02:29:09 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
#[napi]
|
|
|
|
impl Animal {
|
2021-11-29 13:54:45 +09:00
|
|
|
/// This is the constructor
|
2021-09-24 14:00:35 +09:00
|
|
|
#[napi(constructor)]
|
2021-09-23 02:29:09 +09:00
|
|
|
pub fn new(kind: Kind, name: String) -> Self {
|
|
|
|
Animal { kind, name }
|
|
|
|
}
|
|
|
|
|
2021-11-29 13:54:45 +09:00
|
|
|
/// This is a factory method
|
2021-11-05 19:31:36 +09:00
|
|
|
#[napi(factory)]
|
|
|
|
pub fn with_kind(kind: Kind) -> Self {
|
|
|
|
Animal {
|
|
|
|
kind,
|
|
|
|
name: "Default".to_owned(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-24 14:00:35 +09:00
|
|
|
#[napi(getter)]
|
|
|
|
pub fn get_name(&self) -> &str {
|
|
|
|
self.name.as_str()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[napi(setter)]
|
|
|
|
pub fn set_name(&mut self, name: String) {
|
|
|
|
self.name = name;
|
|
|
|
}
|
|
|
|
|
2022-05-13 13:55:54 +09:00
|
|
|
#[napi(getter, js_name = "type")]
|
|
|
|
pub fn kind(&self) -> Kind {
|
|
|
|
self.kind
|
|
|
|
}
|
|
|
|
|
|
|
|
#[napi(setter, js_name = "type")]
|
|
|
|
pub fn set_kind(&mut self, kind: Kind) {
|
|
|
|
self.kind = kind;
|
|
|
|
}
|
|
|
|
|
2021-11-29 13:54:45 +09:00
|
|
|
/// This is a
|
|
|
|
/// multi-line comment
|
|
|
|
/// with an emoji 🚀
|
2021-09-23 02:29:09 +09:00
|
|
|
#[napi]
|
|
|
|
pub fn whoami(&self) -> String {
|
|
|
|
match self.kind {
|
|
|
|
Kind::Dog => {
|
|
|
|
format!("Dog: {}", self.name)
|
|
|
|
}
|
|
|
|
Kind::Cat => format!("Cat: {}", self.name),
|
|
|
|
Kind::Duck => format!("Duck: {}", self.name),
|
|
|
|
}
|
|
|
|
}
|
2021-09-24 21:19:54 +09:00
|
|
|
|
|
|
|
#[napi]
|
2021-11-29 13:54:45 +09:00
|
|
|
/// This is static...
|
2021-09-24 21:19:54 +09:00
|
|
|
pub fn get_dog_kind() -> Kind {
|
|
|
|
Kind::Dog
|
|
|
|
}
|
2021-12-23 15:07:35 +09:00
|
|
|
|
|
|
|
#[napi]
|
2022-01-17 13:38:16 +09:00
|
|
|
/// Here are some characters and character sequences
|
|
|
|
/// that should be escaped correctly:
|
|
|
|
/// \[]{}/\:""
|
2021-12-23 15:07:35 +09:00
|
|
|
pub fn return_other_class(&self) -> Dog {
|
|
|
|
Dog {
|
|
|
|
name: "Doge".to_owned(),
|
|
|
|
}
|
|
|
|
}
|
2021-12-24 19:46:10 +09:00
|
|
|
|
|
|
|
#[napi]
|
|
|
|
pub fn return_other_class_with_custom_constructor(&self) -> Bird {
|
|
|
|
Bird::new("parrot".to_owned())
|
|
|
|
}
|
2022-05-22 14:43:11 +09:00
|
|
|
|
|
|
|
#[napi]
|
|
|
|
pub fn override_individual_arg_on_method(
|
|
|
|
&self,
|
|
|
|
normal_ty: String,
|
|
|
|
#[napi(ts_arg_type = "{n: string}")] overridden_ty: napi::JsObject,
|
|
|
|
) -> Bird {
|
|
|
|
let obj = overridden_ty.coerce_to_object().unwrap();
|
|
|
|
let the_n: Option<String> = obj.get("n").unwrap();
|
|
|
|
|
|
|
|
Bird::new(format!("{}-{}", normal_ty, the_n.unwrap()))
|
|
|
|
}
|
2021-12-23 15:07:35 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
#[napi(constructor)]
|
|
|
|
pub struct Dog {
|
|
|
|
pub name: String,
|
2021-09-23 02:29:09 +09:00
|
|
|
}
|
2021-11-06 12:53:53 +09:00
|
|
|
|
2021-12-24 19:46:10 +09:00
|
|
|
#[napi]
|
|
|
|
pub struct Bird {
|
|
|
|
pub name: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[napi]
|
|
|
|
impl Bird {
|
|
|
|
#[napi(constructor)]
|
|
|
|
pub fn new(name: String) -> Self {
|
|
|
|
Bird { name }
|
|
|
|
}
|
2021-12-24 23:56:16 +09:00
|
|
|
|
|
|
|
#[napi]
|
|
|
|
pub fn get_count(&self) -> u32 {
|
|
|
|
1234
|
|
|
|
}
|
2021-12-24 19:46:10 +09:00
|
|
|
}
|
|
|
|
|
2021-11-06 12:53:53 +09:00
|
|
|
/// Smoking test for type generation
|
|
|
|
#[napi]
|
|
|
|
#[repr(transparent)]
|
|
|
|
pub struct Blake2bHasher(u32);
|
|
|
|
|
|
|
|
#[napi]
|
|
|
|
impl Blake2bHasher {
|
|
|
|
#[napi(factory)]
|
|
|
|
pub fn with_key(key: &Blake2bKey) -> Self {
|
|
|
|
Blake2bHasher(key.get_inner())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-20 16:59:47 +09:00
|
|
|
#[napi]
|
|
|
|
impl Blake2bHasher {
|
|
|
|
#[napi]
|
|
|
|
pub fn update(&mut self, data: Buffer) {
|
|
|
|
self.0 += data.len() as u32;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-06 12:53:53 +09:00
|
|
|
#[napi]
|
|
|
|
pub struct Blake2bKey(u32);
|
|
|
|
|
|
|
|
impl Blake2bKey {
|
|
|
|
fn get_inner(&self) -> u32 {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
}
|
2021-11-06 22:48:18 +09:00
|
|
|
|
|
|
|
#[napi]
|
|
|
|
pub struct Context {
|
|
|
|
data: String,
|
2021-11-16 14:02:40 +09:00
|
|
|
pub maybe_need: Option<bool>,
|
2022-07-06 20:08:34 +09:00
|
|
|
pub buffer: Uint8Array,
|
2021-11-06 22:48:18 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test for return `napi::Result` and `Result`
|
|
|
|
#[napi]
|
|
|
|
impl Context {
|
|
|
|
#[napi(constructor)]
|
|
|
|
pub fn new() -> napi::Result<Self> {
|
|
|
|
Ok(Self {
|
|
|
|
data: "not empty".into(),
|
2021-11-16 14:02:40 +09:00
|
|
|
maybe_need: None,
|
2022-07-06 20:08:34 +09:00
|
|
|
buffer: Uint8Array::new(vec![0, 1, 2, 3]),
|
2021-11-06 22:48:18 +09:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[napi(factory)]
|
|
|
|
pub fn with_data(data: String) -> Result<Self> {
|
2021-11-16 14:02:40 +09:00
|
|
|
Ok(Self {
|
|
|
|
data,
|
|
|
|
maybe_need: Some(true),
|
2022-07-06 20:08:34 +09:00
|
|
|
buffer: Uint8Array::new(vec![0, 1, 2, 3]),
|
2021-11-16 14:02:40 +09:00
|
|
|
})
|
2021-11-06 22:48:18 +09:00
|
|
|
}
|
|
|
|
|
2022-07-06 20:08:34 +09:00
|
|
|
#[napi(factory)]
|
|
|
|
pub fn with_buffer(buf: Uint8Array) -> Self {
|
|
|
|
Self {
|
|
|
|
data: "not empty".into(),
|
|
|
|
maybe_need: None,
|
|
|
|
buffer: buf,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-06 22:48:18 +09:00
|
|
|
#[napi]
|
|
|
|
pub fn method(&self) -> String {
|
|
|
|
self.data.clone()
|
|
|
|
}
|
|
|
|
}
|
2021-12-08 18:32:16 +09:00
|
|
|
|
|
|
|
#[napi(constructor)]
|
|
|
|
pub struct AnimalWithDefaultConstructor {
|
|
|
|
pub name: String,
|
|
|
|
pub kind: u32,
|
|
|
|
}
|
2021-12-19 21:17:54 +09:00
|
|
|
|
|
|
|
// Test for skip_typescript
|
|
|
|
#[napi]
|
|
|
|
pub struct NinjaTurtle {
|
|
|
|
pub name: String,
|
|
|
|
#[napi(skip_typescript)]
|
|
|
|
pub mask_color: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[napi]
|
|
|
|
impl NinjaTurtle {
|
|
|
|
/// Create your ninja turtle! 🐢
|
|
|
|
#[napi(factory)]
|
|
|
|
pub fn new_raph() -> Self {
|
|
|
|
Self {
|
|
|
|
name: "Raphael".to_owned(),
|
|
|
|
mask_color: "Red".to_owned(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// We are not going to expose this character, so we just skip it...
|
|
|
|
#[napi(factory, skip_typescript)]
|
|
|
|
pub fn new_leo() -> Self {
|
|
|
|
Self {
|
|
|
|
name: "Leonardo".to_owned(),
|
|
|
|
mask_color: "Blue".to_owned(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[napi]
|
|
|
|
pub fn get_mask_color(&self) -> &str {
|
|
|
|
self.mask_color.as_str()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[napi]
|
|
|
|
pub fn get_name(&self) -> &str {
|
|
|
|
self.name.as_str()
|
|
|
|
}
|
2022-07-06 00:09:40 +09:00
|
|
|
|
|
|
|
#[napi]
|
|
|
|
pub fn return_this(&self, this: This) -> This {
|
|
|
|
this
|
|
|
|
}
|
2021-12-19 21:17:54 +09:00
|
|
|
}
|
2021-12-25 18:35:47 +09:00
|
|
|
|
|
|
|
#[napi(js_name = "Assets")]
|
|
|
|
pub struct JsAssets {}
|
|
|
|
|
|
|
|
#[napi]
|
|
|
|
impl JsAssets {
|
|
|
|
#[napi(constructor)]
|
2022-03-05 22:29:57 +09:00
|
|
|
#[allow(clippy::new_without_default)]
|
2021-12-25 18:35:47 +09:00
|
|
|
pub fn new() -> Self {
|
|
|
|
JsAssets {}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[napi]
|
|
|
|
pub fn get(&mut self, _id: u32) -> Option<JsAsset> {
|
|
|
|
Some(JsAsset {})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[napi(js_name = "Asset")]
|
|
|
|
pub struct JsAsset {}
|
|
|
|
|
|
|
|
#[napi]
|
|
|
|
impl JsAsset {
|
|
|
|
#[napi(constructor)]
|
2022-03-05 22:29:57 +09:00
|
|
|
#[allow(clippy::new_without_default)]
|
2021-12-25 18:35:47 +09:00
|
|
|
pub fn new() -> Self {
|
|
|
|
Self {}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[napi(getter)]
|
|
|
|
pub fn get_file_path(&self) -> u32 {
|
2022-02-06 11:49:16 +09:00
|
|
|
1
|
2021-12-25 18:35:47 +09:00
|
|
|
}
|
|
|
|
}
|
2022-01-21 07:39:22 +09:00
|
|
|
|
|
|
|
#[napi]
|
|
|
|
pub struct Optional {}
|
|
|
|
|
|
|
|
#[napi]
|
|
|
|
impl Optional {
|
|
|
|
#[napi]
|
|
|
|
pub fn option_end(required: String, optional: Option<String>) -> String {
|
|
|
|
match optional {
|
|
|
|
None => required,
|
|
|
|
Some(optional) => format!("{} {}", required, optional),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[napi]
|
|
|
|
pub fn option_start(optional: Option<String>, required: String) -> String {
|
|
|
|
match optional {
|
|
|
|
None => required,
|
|
|
|
Some(optional) => format!("{} {}", optional, required),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[napi]
|
|
|
|
pub fn option_start_end(
|
|
|
|
optional1: Option<String>,
|
|
|
|
required: String,
|
|
|
|
optional2: Option<String>,
|
|
|
|
) -> String {
|
|
|
|
match (optional1, optional2) {
|
|
|
|
(None, None) => required,
|
|
|
|
(None, Some(optional2)) => format!("{} {}", required, optional2),
|
|
|
|
(Some(optional1), None) => format!("{} {}", optional1, required),
|
|
|
|
(Some(optional1), Some(optional2)) => format!("{} {} {}", optional1, required, optional2),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[napi]
|
|
|
|
pub fn option_only(optional: Option<String>) -> String {
|
|
|
|
match optional {
|
|
|
|
None => "".to_string(),
|
2022-02-06 11:49:16 +09:00
|
|
|
Some(optional) => optional,
|
2022-01-21 07:39:22 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-06-04 02:07:39 +09:00
|
|
|
|
|
|
|
#[napi(object)]
|
|
|
|
pub struct ObjectFieldClassInstance {
|
|
|
|
pub bird: ClassInstance<Bird>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[napi]
|
|
|
|
pub fn create_object_with_class_field(env: Env) -> Result<ObjectFieldClassInstance> {
|
|
|
|
Ok(ObjectFieldClassInstance {
|
|
|
|
bird: Bird {
|
|
|
|
name: "Carolyn".to_owned(),
|
|
|
|
}
|
|
|
|
.into_instance(env)?,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[napi]
|
|
|
|
pub fn receive_object_with_class_field(
|
|
|
|
object: ObjectFieldClassInstance,
|
|
|
|
) -> Result<ClassInstance<Bird>> {
|
|
|
|
Ok(object.bird)
|
|
|
|
}
|
2022-08-06 22:54:58 +09:00
|
|
|
|
|
|
|
#[napi(constructor)]
|
|
|
|
pub struct NotWritableClass {
|
|
|
|
#[napi(writable = false)]
|
|
|
|
pub name: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[napi]
|
|
|
|
impl NotWritableClass {
|
|
|
|
#[napi(writable = false)]
|
|
|
|
pub fn set_name(&mut self, name: String) {
|
|
|
|
self.name = name;
|
|
|
|
}
|
|
|
|
}
|