pinar/
lib.rs

1//#![feature(associated_type_defaults)]
2//#![feature(optin_builtin_traits)]
3//#![feature(specialization)]
4//#![feature(tool_lints)]
5#![feature(core_intrinsics)]
6#![warn(
7     clippy::all,
8     clippy::cargo,
9//     clippy::restriction,
10//     clippy::pedantic,
11//     clippy::nursery,
12)]
13
14use crate::arguments::{Arguments, FromArguments};
15use crate::error::ArgumentsError;
16use crate::module::__pinar_dispatch_function;
17use std::collections::HashMap;
18use napi_sys::*;
19use crate::module::ModuleBuilder;
20use crate::objects::*;
21use crate::env::Env;
22use crate::to_js::ToJs;
23
24mod status;
25mod value;
26mod module;
27mod error;
28mod jsreturn;
29mod classes;
30mod property_descriptor;
31mod external;
32mod objects;
33mod env;
34mod arguments;
35mod function_threadsafe;
36mod to_rust;
37mod multi_js;
38mod to_js;
39
40#[cfg(feature = "pinar-serde")]
41mod pinar_serde;
42
43pub mod prelude {
44    pub use crate::env::Env;
45    pub use crate::multi_js::MultiJs;
46    pub use crate::objects::*;
47    pub use crate::status::Status;
48    pub use crate::to_js::ToJs;
49    pub use crate::to_rust::ToRust;
50    pub use crate::function_threadsafe::JsFunctionThreadSafe;
51    pub use crate::module::ModuleBuilder;
52    pub use crate::property_descriptor::PropertyDescriptor;
53    pub use crate::jsreturn::JsReturn;
54    pub use crate::module::__pinar_dispatch_function;
55    pub use crate::arguments::{FromArguments, Arguments};
56    pub use crate::classes::{JsClass, ClassBuilder};
57    #[cfg(feature = "pinar-serde")]
58    pub use crate::pinar_serde::ser::serialize_to_js;
59    #[cfg(feature = "pinar-serde")]
60    pub use pinar_derive::ToJs;
61}
62
63pub type Result<R> = std::result::Result<R, Error>;
64pub use crate::error::Error;
65
66use crate::pinar_serde::ser::serialize_to_js;
67
68fn testfn(fun: JsFunction) {
69    fun.call((1, "seb")).ok();
70    fun.call(()).ok();
71    fun.call((234, "coucou", vec![1, 2, 3])).ok();
72    fun.call(vec![1, 2, 3]).ok();
73    fun.call("salut").ok();
74    fun.call(10).ok();
75    fun.call(Box::new(91)).ok();
76    fun.call((10, "a", 12, vec![1, 2, 3])).ok();
77    fun.call(ABC { a: 1, b: 2, c: 3, d: TestEnum::B(1), e: None }).ok();
78}
79
80use serde_derive::{Serialize, Deserialize};
81
82use pinar_derive::{ToJs, FromArguments};
83
84#[derive(Debug, Serialize, Deserialize)]
85enum TestEnum {
86    A(String),
87    B(usize),
88    C(Vec<usize>),
89    D(Option<usize>),
90    E(Box<usize>)
91}
92
93#[derive(Debug, Serialize, Deserialize)]
94struct AR {
95    s: String
96}
97
98#[derive(Debug, Serialize, Deserialize, ToJs, FromArguments)]
99struct ABC {
100    a: i32,
101    b: i32,
102    c: i32,
103    d: TestEnum,
104    e: Option<AR>
105}
106
107fn test1(args: (Env, String)) {
108    println!("TEST1 CALLED with {}", args.1);
109}
110
111fn test2(args: (String, i64)) -> i64 {
112    println!("TEST2 CALLED with {} {}", args.0, args.1);
113    100
114}
115
116fn test3(args: (String, i64, Option<String>)) -> String {
117    println!("TEST3 CALLED with {} {} {:?}", args.0, args.1, args.2);
118    "C'est moi le boss".to_owned()
119}
120
121fn test4(args: (String, i64, Option<String>)) -> Option<String> {
122    println!("TEST4 CALLED with {} {} {:?}", args.0, args.1, args.2);
123    None
124}
125
126fn test5(args: (String, i64, Option<String>)) -> Vec<i64> {
127    println!("TEST5 CALLED with {} {} {:?}", args.0, args.1, args.2);
128    vec![1, 2, 3, 4, 5, 6]
129}
130
131fn test6(args: (String, i64, Option<String>)) -> HashMap<i64, String> {
132    println!("TEST6 CALLED with {} {} {:?}", args.0, args.1, args.2);
133    let mut map = HashMap::new();
134    map.insert(1, "coucou".to_owned());
135    map.insert(2, "salut".to_owned());
136    map.insert(3, "oklm".to_owned());
137    map
138}
139
140fn test7(args: i64) -> i64 {
141    args + 1
142}
143
144fn test8(args: Option<i64>) -> Result<Option<i64>> {
145    println!("ARGS: {:?}", args);
146    Ok(args)
147}
148
149fn test9(args: ()) -> ABC {
150    ABC {
151        a: 10,
152        b: 31,
153        c: 22,
154        d: TestEnum::E(Box::new(123)),
155        e: None
156    }
157}
158
159fn test10(_: ()) -> Box<usize> {
160    Box::new(1234)
161}
162
163fn test11<'e>(args: (JsString<'e>, JsObject)) -> JsString<'e> {
164    args.0
165}
166
167fn test12<'e>((env, s1, obj): (Env, JsString, JsObject)) -> JsString<'e> {
168    env.string("weeesh").unwrap()
169}
170
171fn test13<'e>((env, abc): (Env, ABC)) -> ABC {
172    println!("ABC: {:?}", abc);
173    abc
174    //env.string("weeesh").unwrap()
175}
176
177#[macro_export]
178macro_rules! register_module {
179    ($module_name:ident, $init:expr) => {
180        #[no_mangle]
181        #[cfg_attr(target_os = "linux", link_section = ".ctors")]
182        #[cfg_attr(target_os = "macos", link_section = "__DATA,__mod_init_func")]
183        #[cfg_attr(target_os = "windows", link_section = ".CRT$XCU")]
184        pub static __REGISTER_MODULE: extern "C" fn() = {
185            use napi_sys::*;
186
187            extern "C" fn register_module() {
188                unsafe {
189                    static mut MODULE_DESCRIPTOR: napi_module =
190                        napi_module {
191                            nm_version: 1,
192                            nm_flags: 0,
193                            nm_filename: std::ptr::null(),
194                            nm_modname: std::ptr::null(),
195                            nm_register_func: Some(init_module),
196                            nm_priv: 0 as *mut _,
197                            reserved: [0 as *mut _; 4],
198                        };
199                    napi_module_register(&mut MODULE_DESCRIPTOR);
200                }
201
202                extern "C" fn init_module(env: napi_env, export: napi_value) -> napi_value {
203                    match $init(ModuleBuilder::new(env, export)) {
204                        Ok(export) => export,
205                        _ => unreachable!()
206                    }
207                }
208            }
209
210            register_module
211        };
212
213        unsafe extern "C" fn __pinar_callback_function(env: napi_env, info: napi_callback_info) -> napi_value {
214            __pinar_dispatch_function(env, info)
215        }
216    };
217}
218
219use crate::classes::SomeClass;
220use crate::classes::ClassBuilder;
221
222register_module!(sebastien, |module: ModuleBuilder| {
223    module.with_function("test1", test1)
224          .with_function("my_super_function", test2)
225          .with_function("my_other_function", test3)
226          .with_function("test4", test4)
227          .with_function("test5", test5)
228          .with_function("test6", test6)
229          .with_function("test7", test7)
230          .with_function("test8", test8)
231          .with_function("test9", test9)
232          .with_function("test10", test10)
233          .with_function("test11", test11)
234          .with_function("test12", test12)
235          .with_function("test13", test13)
236          .with_class("someclass", || {
237              ClassBuilder::<SomeClass>::start_build()
238                  .with_method("easy", SomeClass::jsfunction)
239                  .with_method("easy2", SomeClass::jsother)
240                  .with_accessor("easy3", SomeClass::jsaccessor)
241                  // .with_accessor("easy4", SomeClass::jsbox)
242          })
243          .build()
244});