From 809682b57f78425d61e313fe6ba85ede04f2a8bf Mon Sep 17 00:00:00 2001 From: Thomas Frank Date: Fri, 10 May 2024 16:29:29 +0200 Subject: [PATCH 1/3] fix clippy warnings --- nj-core/src/basic.rs | 11 ----------- nj-core/src/module.rs | 2 +- 2 files changed, 1 insertion(+), 12 deletions(-) diff --git a/nj-core/src/basic.rs b/nj-core/src/basic.rs index 3beb06e7..7b6fd486 100644 --- a/nj-core/src/basic.rs +++ b/nj-core/src/basic.rs @@ -40,17 +40,6 @@ fn napi_value_type_to_string(js_type: napi_valuetype) -> &'static str { } } -#[derive(Clone)] -pub struct JsNapiValue(napi_value); - -unsafe impl Send for JsNapiValue {} - -impl From for JsNapiValue { - fn from(value: napi_value) -> Self { - Self(value) - } -} - #[derive(Clone, Copy, Debug)] pub struct JsEnv(napi_env); diff --git a/nj-core/src/module.rs b/nj-core/src/module.rs index 56a5c1d6..33c840a7 100644 --- a/nj-core/src/module.rs +++ b/nj-core/src/module.rs @@ -84,6 +84,6 @@ fn init_module() { }; unsafe { - napi_module_register(&mut _MODULE); + napi_module_register(ptr::addr_of_mut!(_MODULE)); } } From 86902cf36495ea9abc2a754007104d65c14efded Mon Sep 17 00:00:00 2001 From: Thomas Frank Date: Mon, 13 May 2024 11:36:58 +0200 Subject: [PATCH 2/3] fix more clippy warnings --- examples/class-async/src/lib.rs | 21 ------------- nj-core/src/worker.rs | 52 --------------------------------- nj-derive/src/ast/util.rs | 12 -------- 3 files changed, 85 deletions(-) diff --git a/examples/class-async/src/lib.rs b/examples/class-async/src/lib.rs index dc7f8763..b504488e 100644 --- a/examples/class-async/src/lib.rs +++ b/examples/class-async/src/lib.rs @@ -2,29 +2,8 @@ use std::time::Duration; use fluvio_future::timer::sleep; -use node_bindgen::sys::napi_value; -use node_bindgen::core::NjError; -use node_bindgen::core::val::JsObject; -use node_bindgen::core::val::JsEnv; -use node_bindgen::core::TryIntoJs; use node_bindgen::derive::node_bindgen; -struct MyJson { - val: f64, -} - -impl TryIntoJs for MyJson { - fn try_to_js(self, js_env: &JsEnv) -> Result { - // create JSON - let mut json = JsObject::new(*js_env, js_env.create_object()?); - - let js_val = js_env.create_double(self.val)?; - json.set_property("val", js_val)?; - - json.try_to_js(js_env) - } -} - struct MyObject { val: f64, } diff --git a/nj-core/src/worker.rs b/nj-core/src/worker.rs index 8551284c..2a624626 100644 --- a/nj-core/src/worker.rs +++ b/nj-core/src/worker.rs @@ -3,7 +3,6 @@ use std::ptr; use tracing::error; use tracing::trace; use tracing::debug; -use async_trait::async_trait; use futures_lite::Future; use fluvio_future::task::spawn; @@ -13,9 +12,7 @@ use crate::sys::napi_value; use crate::val::JsEnv; use crate::NjError; use crate::sys::napi_env; -use crate::sys::napi_callback_info; use crate::TryIntoJs; -use crate::IntoJs; use crate::assert_napi; use crate::ThreadSafeFunction; @@ -119,55 +116,6 @@ where } } -#[async_trait] -pub trait JSWorker: Sized + Send + 'static { - type Output: TryIntoJs; - - /// create new worker based on argument based in the callback - /// only need if it is called as method - fn create_worker(_env: &JsEnv, _info: napi_callback_info) -> Result { - Err(NjError::InvalidType( - "worker".to_owned(), - "worker".to_owned(), - )) - } - - /// call by Node to create promise - extern "C" fn start_promise(env: napi_env, info: napi_callback_info) -> napi_value { - let js_env = JsEnv::new(env); - - let result: Result = (|| { - let worker = Self::create_worker(&js_env, info)?; - worker.create_promise(&js_env) - })(); - - result.into_js(&js_env) - } - - /// create promise and schedule work - /// when this is finished it will return result in the main thread - fn create_promise(self, js_env: &JsEnv) -> Result { - let (promise, deferred) = js_env.create_promise()?; - let function_name = format!("async_worker_th_{}", std::any::type_name::()); - let ts_fn = js_env.create_thread_safe_function( - &function_name, - None, - Some(promise_complete::), - )?; - let js_deferred = JsDeferred(deferred); - - spawn(async move { - let result = self.execute().await; - finish_worker(ts_fn, result, js_deferred); - }); - - Ok(promise) - } - - /// execute this in async worker thread - async fn execute(mut self) -> Self::Output; -} - pub trait NjFutureExt: Future { fn try_to_js(self, js_env: &JsEnv) -> Result where diff --git a/nj-derive/src/ast/util.rs b/nj-derive/src/ast/util.rs index 53f9a2e2..29a1883c 100644 --- a/nj-derive/src/ast/util.rs +++ b/nj-derive/src/ast/util.rs @@ -1,4 +1,3 @@ -use syn::ItemFn; use syn::Ident; use syn::TypePath; use syn::ImplItemMethod; @@ -7,17 +6,6 @@ use syn::PathArguments; use syn::Lifetime; use syn::GenericArgument; -/// traits for function item -pub trait FunctionItem { - fn name(&self) -> &Ident; -} - -impl FunctionItem for ItemFn { - fn name(&self) -> &Ident { - &self.sig.ident - } -} - pub trait TypePathUtil { fn name_identifier(&self) -> Option<&Ident>; fn lifetime(&self) -> Option<&Lifetime>; From d5e0f6772aef7f31b132690529a89dad0a8354ed Mon Sep 17 00:00:00 2001 From: Luis Moreno Date: Wed, 5 Jun 2024 13:01:10 -0500 Subject: [PATCH 3/3] chore: add back removed code from examples --- examples/class-async/src/lib.rs | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/examples/class-async/src/lib.rs b/examples/class-async/src/lib.rs index b504488e..3a7b4488 100644 --- a/examples/class-async/src/lib.rs +++ b/examples/class-async/src/lib.rs @@ -2,8 +2,30 @@ use std::time::Duration; use fluvio_future::timer::sleep; +use node_bindgen::sys::napi_value; +use node_bindgen::core::NjError; +use node_bindgen::core::val::JsObject; +use node_bindgen::core::val::JsEnv; +use node_bindgen::core::TryIntoJs; use node_bindgen::derive::node_bindgen; +pub struct MyJson { + val: f64, +} + +impl TryIntoJs for MyJson { + fn try_to_js(self, js_env: &JsEnv) -> Result { + // create JSON + let mut json = JsObject::new(*js_env, js_env.create_object()?); + + let js_val = js_env.create_double(self.val)?; + json.set_property("val", js_val)?; + + json.try_to_js(js_env) + } +} + + struct MyObject { val: f64, }