From 6cf59213fe3e15d20ef6f1f4b4b0024ded3a64a3 Mon Sep 17 00:00:00 2001 From: "K.J. Valencik" Date: Wed, 6 Jan 2021 12:09:38 -0500 Subject: [PATCH 1/2] Remove APIs that will not be implemented in N-API --- crates/neon-runtime/src/napi/call.rs | 2 -- crates/neon-runtime/src/napi/handler.rs | 7 ------- crates/neon-runtime/src/napi/mod.rs | 2 -- crates/neon-runtime/src/napi/object.rs | 5 ----- crates/neon-runtime/src/napi/primitive.rs | 10 ---------- crates/neon-runtime/src/napi/task.rs | 7 ------- src/context/internal.rs | 5 ----- src/context/mod.rs | 1 + src/lib.rs | 1 + src/prelude.rs | 1 + 10 files changed, 3 insertions(+), 38 deletions(-) delete mode 100644 crates/neon-runtime/src/napi/handler.rs delete mode 100644 crates/neon-runtime/src/napi/task.rs diff --git a/crates/neon-runtime/src/napi/call.rs b/crates/neon-runtime/src/napi/call.rs index 021b6a0f6..13e121bc2 100644 --- a/crates/neon-runtime/src/napi/call.rs +++ b/crates/neon-runtime/src/napi/call.rs @@ -26,8 +26,6 @@ pub unsafe extern "C" fn set_return(_info: FunctionCallbackInfo, _value: Local) } -pub unsafe extern "C" fn get_isolate(_info: FunctionCallbackInfo) -> Env { unimplemented!() } - // FIXME: Remove. This will never be implemented pub unsafe extern "C" fn current_isolate() -> Env { panic!("current_isolate won't be implemented in n-api") } diff --git a/crates/neon-runtime/src/napi/handler.rs b/crates/neon-runtime/src/napi/handler.rs deleted file mode 100644 index dc2f9546a..000000000 --- a/crates/neon-runtime/src/napi/handler.rs +++ /dev/null @@ -1,7 +0,0 @@ -use crate::raw::Local; -use std::os::raw::c_void; - -pub unsafe extern "C" fn new(_isolate: *mut c_void, _this: Local, _callback: Local) -> *mut c_void { unimplemented!() } -pub unsafe extern "C" fn schedule(_thread_safe_cb: *mut c_void, _rust_callback: *mut c_void, - _complete: unsafe extern fn(Local, Local, *mut c_void)) { unimplemented!() } -pub unsafe extern "C" fn delete(_thread_safe_cb: *mut c_void) { unimplemented!() } diff --git a/crates/neon-runtime/src/napi/mod.rs b/crates/neon-runtime/src/napi/mod.rs index 475b68f08..87c7b5e79 100644 --- a/crates/neon-runtime/src/napi/mod.rs +++ b/crates/neon-runtime/src/napi/mod.rs @@ -14,8 +14,6 @@ pub mod raw; pub mod scope; pub mod string; pub mod tag; -pub mod task; -pub mod handler; pub mod reference; #[cfg(feature = "napi-4")] pub mod tsfn; diff --git a/crates/neon-runtime/src/napi/object.rs b/crates/neon-runtime/src/napi/object.rs index ea5b90181..5c9efec22 100644 --- a/crates/neon-runtime/src/napi/object.rs +++ b/crates/neon-runtime/src/napi/object.rs @@ -30,11 +30,6 @@ pub unsafe extern "C" fn get_own_property_names(out: &mut Local, env: Env, objec true } -// Unused. -pub unsafe extern "C" fn get_isolate(_obj: Local) -> Env { - unimplemented!() -} - /// Mutate the `out` argument to refer to the value at `index` in the given `object`. Returns `false` if the value couldn't be retrieved. pub unsafe extern "C" fn get_index(out: &mut Local, env: Env, object: Local, index: u32) -> bool { let status = napi::get_element(env, object, index, out as *mut _); diff --git a/crates/neon-runtime/src/napi/primitive.rs b/crates/neon-runtime/src/napi/primitive.rs index 74e591e33..4423d1b52 100644 --- a/crates/neon-runtime/src/napi/primitive.rs +++ b/crates/neon-runtime/src/napi/primitive.rs @@ -24,16 +24,6 @@ pub unsafe extern "C" fn boolean_value(env: Env, p: Local) -> bool { value } -// DEPRECATE(0.2) -pub unsafe extern "C" fn integer(_out: &mut Local, _isolate: Env, _x: i32) { unimplemented!() } - -pub unsafe extern "C" fn is_u32(_p: Local) -> bool { unimplemented!() } - -pub unsafe extern "C" fn is_i32(_p: Local) -> bool { unimplemented!() } - -// DEPRECATE(0.2) -pub unsafe extern "C" fn integer_value(_p: Local) -> i64 { unimplemented!() } - /// Mutates the `out` argument provided to refer to a newly created `Local` containing a /// JavaScript number. pub unsafe extern "C" fn number(out: &mut Local, env: Env, v: f64) { diff --git a/crates/neon-runtime/src/napi/task.rs b/crates/neon-runtime/src/napi/task.rs deleted file mode 100644 index a784df4a0..000000000 --- a/crates/neon-runtime/src/napi/task.rs +++ /dev/null @@ -1,7 +0,0 @@ -use crate::raw::Local; -use std::os::raw::c_void; - -pub unsafe extern "C" fn schedule(_task: *mut c_void, - _perform: unsafe extern fn(*mut c_void) -> *mut c_void, - _complete: unsafe extern fn(*mut c_void, *mut c_void, &mut Local), - _callback: Local) { unimplemented!() } diff --git a/src/context/internal.rs b/src/context/internal.rs index 448d4cc8e..7e3fdc025 100644 --- a/src/context/internal.rs +++ b/src/context/internal.rs @@ -59,11 +59,6 @@ impl Env { unsafe { std::mem::transmute(ptr) } } - #[cfg(feature = "napi-1")] - pub(crate) fn current() -> Env { - panic!("Context::current() will not implemented with n-api") - } - #[cfg(feature = "legacy-runtime")] pub(crate) fn current() -> Env { unsafe { diff --git a/src/context/mod.rs b/src/context/mod.rs index 81e476868..44487c06c 100644 --- a/src/context/mod.rs +++ b/src/context/mod.rs @@ -598,6 +598,7 @@ pub struct TaskContext<'a> { } impl<'a> TaskContext<'a> { + #[cfg(feature = "legacy-runtime")] pub(crate) fn with FnOnce(TaskContext<'b>) -> T>(f: F) -> T { let env = Env::current(); Scope::with(env, |scope| { diff --git a/src/lib.rs b/src/lib.rs index 30bcc60c6..9b06d3143 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -18,6 +18,7 @@ pub mod types; pub mod object; pub mod borrow; pub mod result; +#[cfg(feature = "legacy-runtime")] pub mod task; #[cfg(any(feature = "event-handler-api", all(feature = "napi-4", feature = "event-queue-api")))] pub mod event; diff --git a/src/prelude.rs b/src/prelude.rs index 86f067398..cb54a6103 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -6,6 +6,7 @@ pub use object::{Object, Class}; pub use borrow::{Borrow, BorrowMut}; pub use context::{CallKind, Context, ModuleContext, ExecuteContext, ComputeContext, CallContext, FunctionContext, MethodContext, TaskContext}; pub use result::{NeonResult, JsResult, JsResultExt}; +#[cfg(feature = "legacy-runtime")] pub use task::Task; #[cfg(all(not(feature = "napi-1"), feature = "event-handler-api"))] pub use event::EventHandler; From 0734bf4a07d3bf3ea6fbe6e3131d72cc94dfc9a0 Mon Sep 17 00:00:00 2001 From: "K.J. Valencik" Date: Wed, 6 Jan 2021 12:29:57 -0500 Subject: [PATCH 2/2] Remove classes from N-API --- crates/neon-runtime/src/napi/call.rs | 7 ----- crates/neon-runtime/src/napi/class.rs | 39 -------------------------- crates/neon-runtime/src/napi/fun.rs | 4 --- crates/neon-runtime/src/napi/mod.rs | 1 - src/context/internal.rs | 6 ++-- src/context/mod.rs | 3 ++ src/lib.rs | 4 +++ src/macro_internal/mod.rs | 1 + src/object/class/mod.rs | 40 ++------------------------- src/object/mod.rs | 2 ++ src/prelude.rs | 8 ++++-- src/types/internal.rs | 37 ++++++++++++++++++++++++- src/types/mod.rs | 2 +- 13 files changed, 59 insertions(+), 95 deletions(-) delete mode 100644 crates/neon-runtime/src/napi/class.rs diff --git a/crates/neon-runtime/src/napi/call.rs b/crates/neon-runtime/src/napi/call.rs index 13e121bc2..988b96d4a 100644 --- a/crates/neon-runtime/src/napi/call.rs +++ b/crates/neon-runtime/src/napi/call.rs @@ -22,13 +22,6 @@ impl Default for CCallback { } } -pub unsafe extern "C" fn set_return(_info: FunctionCallbackInfo, _value: Local) { - -} - -// FIXME: Remove. This will never be implemented -pub unsafe extern "C" fn current_isolate() -> Env { panic!("current_isolate won't be implemented in n-api") } - pub unsafe extern "C" fn is_construct(env: Env, info: FunctionCallbackInfo) -> bool { let mut target: MaybeUninit = MaybeUninit::zeroed(); diff --git a/crates/neon-runtime/src/napi/class.rs b/crates/neon-runtime/src/napi/class.rs deleted file mode 100644 index 9e7244075..000000000 --- a/crates/neon-runtime/src/napi/class.rs +++ /dev/null @@ -1,39 +0,0 @@ -use std::os::raw::c_void; -use crate::call::CCallback; -use crate::raw::{Env, Local}; - -pub unsafe extern "C" fn get_class_map(_isolate: Env) -> *mut c_void { unimplemented!() } - -pub unsafe extern "C" fn set_class_map(_isolate: Env, _map: *mut c_void, _free_map: *mut c_void) { unimplemented!() } - -pub unsafe extern "C" fn create_base(_isolate: Env, - _allocate: CCallback, - _construct: CCallback, - _call: CCallback, - _drop: extern "C" fn(*mut c_void)) -> *mut c_void { unimplemented!() } - -pub unsafe extern "C" fn get_name<'a>(_base_out: &'a mut *mut u8, _isolate: Env, _metadata: *const c_void) -> usize { unimplemented!() } - -pub unsafe extern "C" fn set_name(_isolate: Env, _metadata: *mut c_void, _name: *const u8, _byte_length: u32) -> bool { unimplemented!() } - -pub unsafe extern "C" fn throw_call_error(_isolate: Env, _metadata: *mut c_void) { unimplemented!() } - -pub unsafe extern "C" fn throw_this_error(_isolate: Env, _metadata: *mut c_void) { unimplemented!() } - -pub unsafe extern "C" fn add_method(_isolate: Env, _metadata: *mut c_void, _name: *const u8, _byte_length: u32, _method: Local) -> bool { unimplemented!() } - -pub unsafe extern "C" fn metadata_to_constructor(_out: &mut Local, _isolate: Env, _metadata: *mut c_void) -> bool { unimplemented!() } - -// FIXME: get rid of all the "kernel" nomenclature - -pub unsafe extern "C" fn get_allocate_kernel(_data: *mut c_void) -> *mut c_void { unimplemented!() } - -pub unsafe extern "C" fn get_construct_kernel(_data: *mut c_void) -> *mut c_void { unimplemented!() } - -pub unsafe extern "C" fn get_call_kernel(_data: *mut c_void) -> *mut c_void { unimplemented!() } - -pub unsafe extern "C" fn constructor(_out: &mut Local, _ft: Local) -> bool { unimplemented!() } - -pub unsafe extern "C" fn has_instance(_metadata: *mut c_void, _v: Local) -> bool { unimplemented!() } - -pub unsafe extern "C" fn get_instance_internals(_obj: Local) -> *mut c_void { unimplemented!() } diff --git a/crates/neon-runtime/src/napi/fun.rs b/crates/neon-runtime/src/napi/fun.rs index 1f6af65a9..321a2062c 100644 --- a/crates/neon-runtime/src/napi/fun.rs +++ b/crates/neon-runtime/src/napi/fun.rs @@ -22,10 +22,6 @@ pub unsafe extern "C" fn new(out: &mut Local, env: Env, callback: CCallback) -> status == napi::Status::Ok } -pub unsafe extern "C" fn new_template(_out: &mut Local, _env: Env, _callback: CCallback) -> bool { - unimplemented!() -} - pub unsafe extern "C" fn get_dynamic_callback(_env: Env, data: *mut c_void) -> *mut c_void { data } diff --git a/crates/neon-runtime/src/napi/mod.rs b/crates/neon-runtime/src/napi/mod.rs index 87c7b5e79..39ea25dc3 100644 --- a/crates/neon-runtime/src/napi/mod.rs +++ b/crates/neon-runtime/src/napi/mod.rs @@ -2,7 +2,6 @@ pub mod array; pub mod arraybuffer; pub mod buffer; pub mod call; -pub mod class; pub mod convert; pub mod error; pub mod external; diff --git a/src/context/internal.rs b/src/context/internal.rs index 7e3fdc025..771d9de77 100644 --- a/src/context/internal.rs +++ b/src/context/internal.rs @@ -1,9 +1,8 @@ -use std; #[cfg(feature = "legacy-runtime")] use std::any::Any; -use std::boxed::Box; use std::cell::Cell; use std::mem::MaybeUninit; +#[cfg(feature = "legacy-runtime")] use std::os::raw::c_void; #[cfg(feature = "legacy-runtime")] use std::panic::{AssertUnwindSafe, catch_unwind, resume_unwind}; @@ -14,6 +13,7 @@ use neon_runtime::scope::Root; use neon_runtime::try_catch::TryCatchControl; use types::{JsObject, JsValue}; use handle::Handle; +#[cfg(feature = "legacy-runtime")] use object::class::ClassMap; use result::NeonResult; use super::ModuleContext; @@ -28,6 +28,7 @@ pub struct Env(raw::Isolate); #[derive(Clone, Copy)] pub struct Env(raw::Env); +#[cfg(feature = "legacy-runtime")] extern "C" fn drop_class_map(map: Box) { std::mem::drop(map); } @@ -45,6 +46,7 @@ impl Env { ptr } + #[cfg(feature = "legacy-runtime")] pub(crate) fn class_map(&mut self) -> &mut ClassMap { let mut ptr: *mut c_void = unsafe { neon_runtime::class::get_class_map(self.to_raw()) }; if ptr.is_null() { diff --git a/src/context/mod.rs b/src/context/mod.rs index 44487c06c..09aaf315a 100644 --- a/src/context/mod.rs +++ b/src/context/mod.rs @@ -22,6 +22,7 @@ use types::boxed::{Finalize, JsBox}; use types::binary::{JsArrayBuffer, JsBuffer}; use types::error::JsError; use object::{Object, This}; +#[cfg(feature = "legacy-runtime")] use object::class::Class; use result::{NeonResult, JsResult, Throw}; #[cfg(feature = "napi-1")] @@ -47,6 +48,7 @@ impl CallbackInfo<'_> { CallContext::::with(env, self, f) } + #[cfg(feature = "legacy-runtime")] pub fn set_return<'a, 'b, T: Value>(&'a self, value: Handle<'b, T>) { unsafe { neon_runtime::call::set_return(self.info, value.to_raw()) @@ -421,6 +423,7 @@ impl<'a> ModuleContext<'a> { Ok(()) } + #[cfg(feature = "legacy-runtime")] /// Convenience method for exporting a Neon class constructor from a module. pub fn export_class(&mut self, key: &str) -> NeonResult<()> { let constructor = T::constructor(self)?; diff --git a/src/lib.rs b/src/lib.rs index 9b06d3143..a555000ed 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -172,6 +172,7 @@ macro_rules! register_module { } } +#[cfg(feature = "legacy-runtime")] #[doc(hidden)] #[macro_export(local_inner_macros)] macro_rules! class_definition { @@ -261,6 +262,7 @@ macro_rules! class_definition { }; } +#[cfg(feature = "legacy-runtime")] #[doc(hidden)] #[macro_export(local_inner_macros)] macro_rules! impl_managed { @@ -281,6 +283,7 @@ macro_rules! impl_managed { } } +#[cfg(feature = "legacy-runtime")] /// Declare custom native JavaScript types with Rust implementations. /// /// Example: @@ -363,6 +366,7 @@ macro_rules! declare_types { { } => { }; } +#[cfg(feature = "legacy-runtime")] #[doc(hidden)] #[macro_export] macro_rules! neon_stringify { diff --git a/src/macro_internal/mod.rs b/src/macro_internal/mod.rs index db753fd96..fdc125ce7 100644 --- a/src/macro_internal/mod.rs +++ b/src/macro_internal/mod.rs @@ -1,6 +1,7 @@ //! Internals needed by macros. These have to be exported for the macros to work /// but are subject to change and should never be explicitly used. +#[cfg(feature = "legacy-runtime")] // Used by the class macro. pub use object::class::internal::{AllocateCallback, ConstructCallback, ConstructorCallCallback, MethodCallback}; pub use context::internal::{initialize_module, Env}; diff --git a/src/object/class/mod.rs b/src/object/class/mod.rs index 6fd661cd9..3008cbb23 100644 --- a/src/object/class/mod.rs +++ b/src/object/class/mod.rs @@ -9,14 +9,13 @@ use std::slice; use std::collections::HashMap; use neon_runtime; use neon_runtime::raw; -use neon_runtime::call::CCallback; -use context::{Context, Lock, CallbackInfo}; +use context::{Context, Lock}; use context::internal::Env; use result::{NeonResult, JsResult, Throw}; use borrow::{Borrow, BorrowMut, Ref, RefMut, LoanError}; use handle::{Handle, Managed}; use types::{Value, JsFunction, JsValue, build}; -use types::internal::ValueInternal; +use types::internal::{Callback, ValueInternal}; use object::{Object, This}; use self::internal::{ClassMetadata, MethodCallback, ConstructorCallCallback, AllocateCallback, ConstructCallback}; @@ -260,38 +259,3 @@ impl<'a, T: Class> BorrowMut for &'a mut T { } } } - -/// A dynamically computed callback that can be passed through C to the engine. -/// This type makes it possible to export a dynamically computed Rust function -/// as a pair of 1) a raw pointer to the dynamically computed function, and 2) -/// a static function that knows how to transmute that raw pointer and call it. -pub(crate) trait Callback: Sized { - /// Extracts the computed Rust function and invokes it. The Neon runtime - /// ensures that the computed function is provided as the extra data field, - /// wrapped as a V8 External, in the `CallbackInfo` argument. - extern "C" fn invoke(env: Env, info: CallbackInfo<'_>) -> T; - - /// See `invoke`. This is used by the non-n-api implementation, so that every impl for this - /// trait doesn't need to provide two versions of `invoke`. - #[cfg(feature = "legacy-runtime")] - #[doc(hidden)] - extern "C" fn invoke_compat(info: CallbackInfo<'_>) -> T { - Self::invoke(Env::current(), info) - } - - /// Converts the callback to a raw void pointer. - fn as_ptr(self) -> *mut c_void; - - /// Exports the callback as a pair consisting of the static `Self::invoke` - /// method and the computed callback, both converted to raw void pointers. - fn into_c_callback(self) -> CCallback { - #[cfg(feature = "napi-1")] - let invoke = Self::invoke; - #[cfg(feature = "legacy-runtime")] - let invoke = Self::invoke_compat; - CCallback { - static_callback: unsafe { mem::transmute(invoke as usize) }, - dynamic_callback: self.as_ptr() - } - } -} diff --git a/src/object/mod.rs b/src/object/mod.rs index 715c84b36..02225524b 100644 --- a/src/object/mod.rs +++ b/src/object/mod.rs @@ -1,7 +1,9 @@ //! Traits for working with JavaScript objects. +#[cfg(feature = "legacy-runtime")] pub(crate) mod class; +#[cfg(feature = "legacy-runtime")] pub use self::class::{Class, ClassDescriptor}; pub use self::traits::*; diff --git a/src/prelude.rs b/src/prelude.rs index cb54a6103..adba29f89 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -2,7 +2,9 @@ pub use handle::Handle; pub use types::{JsBuffer, JsArrayBuffer, BinaryData, JsError, Value, JsValue, JsUndefined, JsNull, JsBoolean, JsString, JsNumber, JsObject, JsArray, JsFunction}; -pub use object::{Object, Class}; +pub use object::Object; +#[cfg(feature = "legacy-runtime")] +pub use object::Class; pub use borrow::{Borrow, BorrowMut}; pub use context::{CallKind, Context, ModuleContext, ExecuteContext, ComputeContext, CallContext, FunctionContext, MethodContext, TaskContext}; pub use result::{NeonResult, JsResult, JsResultExt}; @@ -10,7 +12,9 @@ pub use result::{NeonResult, JsResult, JsResultExt}; pub use task::Task; #[cfg(all(not(feature = "napi-1"), feature = "event-handler-api"))] pub use event::EventHandler; -pub use crate::{register_module, declare_types}; +pub use crate::register_module; +#[cfg(feature = "legacy-runtime")] +pub use crate::declare_types; #[cfg(feature = "napi-1")] pub use crate::{ handle::Root, diff --git a/src/types/internal.rs b/src/types/internal.rs index af643b5a9..96f7348f4 100644 --- a/src/types/internal.rs +++ b/src/types/internal.rs @@ -1,13 +1,13 @@ use std::mem; use std::os::raw::c_void; use neon_runtime; +use neon_runtime::call::CCallback; use neon_runtime::raw; use context::{CallbackInfo, FunctionContext}; use context::internal::Env; use types::error::convert_panics; use types::{JsObject, Handle, Managed}; use result::JsResult; -use object::class::Callback; use super::Value; pub trait ValueInternal: Managed + 'static { @@ -78,3 +78,38 @@ impl Callback for FunctionCallback { unsafe { mem::transmute(self.0) } } } + +/// A dynamically computed callback that can be passed through C to the engine. +/// This type makes it possible to export a dynamically computed Rust function +/// as a pair of 1) a raw pointer to the dynamically computed function, and 2) +/// a static function that knows how to transmute that raw pointer and call it. +pub(crate) trait Callback: Sized { + /// Extracts the computed Rust function and invokes it. The Neon runtime + /// ensures that the computed function is provided as the extra data field, + /// wrapped as a V8 External, in the `CallbackInfo` argument. + extern "C" fn invoke(env: Env, info: CallbackInfo<'_>) -> T; + + /// See `invoke`. This is used by the non-n-api implementation, so that every impl for this + /// trait doesn't need to provide two versions of `invoke`. + #[cfg(feature = "legacy-runtime")] + #[doc(hidden)] + extern "C" fn invoke_compat(info: CallbackInfo<'_>) -> T { + Self::invoke(Env::current(), info) + } + + /// Converts the callback to a raw void pointer. + fn as_ptr(self) -> *mut c_void; + + /// Exports the callback as a pair consisting of the static `Self::invoke` + /// method and the computed callback, both converted to raw void pointers. + fn into_c_callback(self) -> CCallback { + #[cfg(feature = "napi-1")] + let invoke = Self::invoke; + #[cfg(feature = "legacy-runtime")] + let invoke = Self::invoke_compat; + CCallback { + static_callback: unsafe { mem::transmute(invoke as usize) }, + dynamic_callback: self.as_ptr() + } + } +} diff --git a/src/types/mod.rs b/src/types/mod.rs index e91ece0ab..6108992d7 100644 --- a/src/types/mod.rs +++ b/src/types/mod.rs @@ -18,9 +18,9 @@ use context::{Context, FunctionContext}; use context::internal::Env; use result::{NeonResult, JsResult, Throw, JsResultExt}; use object::{Object, This}; -use object::class::Callback; use handle::{Handle, Managed}; use handle::internal::SuperType; +use types::internal::Callback; use smallvec::SmallVec; use self::internal::{ValueInternal, FunctionCallback}; use self::utf8::Utf8;