Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
516 changes: 218 additions & 298 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions crates/neon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ homepage = "https://www.neon-bindings.com"
repository = "https://github.com/neon-bindings/neon"
license = "MIT/Apache-2.0"
exclude = ["neon.jpg", "doc/**/*"]
edition = "2021"
edition = "2024"

[dev-dependencies]
itertools = "0.10.5"
Expand All @@ -27,7 +27,7 @@ nodejs-sys = "0.15.0"
either = "1.13.0"
getrandom = { version = "0.2.11", optional = true }
libloading = "0.8.1"
linkme = "0.3.25"
linkme = "0.3.32"
semver = "1.0.20"
smallvec = "1.11.2"
once_cell = "1.18.0"
Expand Down
40 changes: 23 additions & 17 deletions crates/neon/src/context/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
handle::Handle,
result::NeonResult,
sys::{self, raw},
types::{private::ValueInternal, JsObject},
types::{JsObject, private::ValueInternal},
};

#[repr(C)]
Expand Down Expand Up @@ -36,12 +36,16 @@ impl Env {
let result = f();
let mut local: MaybeUninit<raw::Local> = MaybeUninit::zeroed();

if sys::error::catch_error(self.to_raw(), local.as_mut_ptr()) {
Err(local.assume_init())
} else if let Ok(result) = result {
Ok(result)
} else {
panic!("try_catch: unexpected Err(Throw) when VM is not in a throwing state");
unsafe {
if sys::error::catch_error(self.to_raw(), local.as_mut_ptr()) {
Err(local.assume_init())
} else {
if let Ok(result) = result {
Ok(result)
} else {
panic!("try_catch: unexpected Err(Throw) when VM is not in a throwing state");
}
}
}
}
}
Expand Down Expand Up @@ -72,19 +76,21 @@ fn init(cx: ModuleContext) -> NeonResult<()> {
}
}

#[no_mangle]
#[unsafe(no_mangle)]
unsafe extern "C" fn napi_register_module_v1(env: *mut c_void, m: *mut c_void) -> *mut c_void {
let env = env.cast();
unsafe {
let env = env.cast();

sys::setup(env);
sys::setup(env);

IS_RUNNING.with(|v| {
*v.borrow_mut() = true;
});
IS_RUNNING.with(|v| {
*v.borrow_mut() = true;
});

let env = Env(env);
let exports = Handle::new_internal(JsObject::from_local(env, m.cast()));
let _ = ModuleContext::with(env, exports, init);
let env = Env(env);
let exports = Handle::new_internal(JsObject::from_local(env, m.cast()));
let _ = ModuleContext::with(env, exports, init);

m
m
}
}
22 changes: 12 additions & 10 deletions crates/neon/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@
//! .bind(&mut cx)
//! .this(iterator)?
//! .call()?;
//! numbers.push(obj.prop(&mut cx, "value").get()?); // temporary number
//! let num: Handle<JsNumber> = // temporary number
//! obj.prop(&mut cx, "value").get()?;
//! numbers.push(num);
//! obj.prop(&mut cx, "done").get() // temporary boolean
//! })?;
//! }
Expand Down Expand Up @@ -180,12 +182,12 @@ use crate::{
scope::{EscapableHandleScope, HandleScope},
},
types::{
Deferred, JsArray, JsArrayBuffer, JsBoolean, JsBuffer, JsFunction, JsNull, JsNumber,
JsObject, JsPromise, JsString, JsUndefined, JsValue, StringResult, Value,
boxed::{Finalize, JsBox},
error::JsError,
extract::{FromArgs, TryFromJs},
private::ValueInternal,
Deferred, JsArray, JsArrayBuffer, JsBoolean, JsBuffer, JsFunction, JsNull, JsNumber,
JsObject, JsPromise, JsString, JsUndefined, JsValue, StringResult, Value,
},
};

Expand Down Expand Up @@ -239,9 +241,11 @@ impl<'cx> Cx<'cx> {
#[cfg(feature = "sys")]
#[cfg_attr(docsrs, doc(cfg(feature = "sys")))]
pub unsafe fn from_raw(env: sys::Env) -> Self {
Self {
env: env.into(),
_phantom_inner: PhantomData,
unsafe {
Self {
env: env.into(),
_phantom_inner: PhantomData,
}
}
}

Expand Down Expand Up @@ -411,9 +415,7 @@ pub trait Context<'a>: ContextInternal<'a> {

let escapee = unsafe { scope.escape(f(cx)?.to_local()) };

Ok(Handle::new_internal(unsafe {
V::from_local(self.env(), escapee)
}))
Ok(unsafe { Handle::new_internal(V::from_local(self.env(), escapee)) })
}

fn try_catch<T, F>(&mut self, f: F) -> Result<T, Handle<'a, JsValue>>
Expand Down Expand Up @@ -799,7 +801,7 @@ impl<'cx> FunctionContext<'cx> {
};

argv.get(i)
.map(|v| Handle::new_internal(unsafe { JsValue::from_local(self.env(), v) }))
.map(|v| unsafe { Handle::new_internal(JsValue::from_local(self.env(), v)) })
}

/// Produces the `i`th argument and casts it to the type `V`, or throws an exception if `i` is greater than or equal to `self.len()` or cannot be cast to `V`.
Expand Down
4 changes: 2 additions & 2 deletions crates/neon/src/event/channel.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use std::{
error, fmt,
sync::{
atomic::{AtomicUsize, Ordering},
Arc,
atomic::{AtomicUsize, Ordering},
},
};

use crate::{
context::{internal::Env, Context, Cx},
context::{Context, Cx, internal::Env},
result::{NeonResult, ResultExt, Throw},
sys::{self, tsfn::ThreadsafeFunction},
};
Expand Down
2 changes: 1 addition & 1 deletion crates/neon/src/event/task.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{panic::resume_unwind, thread};

use crate::{
context::{internal::Env, Context, Cx},
context::{Context, Cx, internal::Env},
handle::Handle,
result::{JsResult, NeonResult},
sys::{async_work, raw},
Expand Down
2 changes: 1 addition & 1 deletion crates/neon/src/handle/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl NapiRef {
/// # Safety
/// Must only be used from the same module context that created the reference
pub(crate) unsafe fn unref(self, env: raw::Env) {
reference::unreference(env, self.0.cast());
unsafe { reference::unreference(env, self.0.cast()) }
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/neon/src/lifecycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use std::{
any::Any,
marker::PhantomData,
sync::{
atomic::{AtomicU32, Ordering},
Arc,
atomic::{AtomicU32, Ordering},
},
};

Expand Down Expand Up @@ -108,7 +108,7 @@ impl LocalCell {
{
let cell = InstanceData::locals(cx).get(id);
match cell {
LocalCell::Init(ref mut b) => Some(b),
LocalCell::Init(b) => Some(b),
_ => None,
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/neon/src/macro_internal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
context::{Context, Cx, ModuleContext},
handle::Handle,
result::{JsResult, NeonResult},
types::{extract::TryIntoJs, JsValue},
types::{JsValue, extract::TryIntoJs},
};

#[cfg(feature = "serde")]
Expand Down
17 changes: 8 additions & 9 deletions crates/neon/src/object/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,16 @@
use smallvec::smallvec;

use crate::{
context::{internal::ContextInternal, Context, Cx},
context::{Context, Cx, internal::ContextInternal},
handle::{Handle, Root},
result::{NeonResult, Throw},
sys::{self, raw},
types::{
build,
JsFunction, JsUndefined, JsValue, Value, build,
extract::{TryFromJs, TryIntoJs},
function::{BindOptions, CallOptions},
private::ValueInternal,
utf8::Utf8,
JsFunction, JsUndefined, JsValue, Value,
},
};

Expand Down Expand Up @@ -76,7 +75,7 @@ impl PropertyKey for u32 {
out: &mut raw::Local,
obj: raw::Local,
) -> bool {
sys::object::get_index(out, cx.env().to_raw(), obj, self)
unsafe { sys::object::get_index(out, cx.env().to_raw(), obj, self) }
}

unsafe fn set_from<'c, C: Context<'c>>(
Expand All @@ -86,7 +85,7 @@ impl PropertyKey for u32 {
obj: raw::Local,
val: raw::Local,
) -> bool {
sys::object::set_index(out, cx.env().to_raw(), obj, self, val)
unsafe { sys::object::set_index(out, cx.env().to_raw(), obj, self, val) }
}
}

Expand All @@ -99,7 +98,7 @@ impl<'a, K: Value> PropertyKey for Handle<'a, K> {
) -> bool {
let env = cx.env().to_raw();

sys::object::get(out, env, obj, self.to_local())
unsafe { sys::object::get(out, env, obj, self.to_local()) }
}

unsafe fn set_from<'c, C: Context<'c>>(
Expand All @@ -111,7 +110,7 @@ impl<'a, K: Value> PropertyKey for Handle<'a, K> {
) -> bool {
let env = cx.env().to_raw();

sys::object::set(out, env, obj, self.to_local(), val)
unsafe { sys::object::set(out, env, obj, self.to_local(), val) }
}
}

Expand All @@ -125,7 +124,7 @@ impl<'a> PropertyKey for &'a str {
let (ptr, len) = Utf8::from(self).into_small_unwrap().lower();
let env = cx.env().to_raw();

sys::object::get_string(env, out, obj, ptr, len)
unsafe { sys::object::get_string(env, out, obj, ptr, len) }
}

unsafe fn set_from<'c, C: Context<'c>>(
Expand All @@ -138,7 +137,7 @@ impl<'a> PropertyKey for &'a str {
let (ptr, len) = Utf8::from(self).into_small_unwrap().lower();
let env = cx.env().to_raw();

sys::object::set_string(env, out, obj, ptr, len, val)
unsafe { sys::object::set_string(env, out, obj, ptr, len, val) }
}
}

Expand Down
8 changes: 4 additions & 4 deletions crates/neon/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ pub use crate::{
object::Object,
result::{JsResult, NeonResult, ResultExt as NeonResultExt},
types::{
boxed::{Finalize, JsBox},
JsArray, JsArrayBuffer, JsBigInt64Array, JsBigUint64Array, JsBoolean, JsBuffer, JsError,
JsFloat32Array, JsFloat64Array, JsFunction, JsInt16Array, JsInt32Array, JsInt8Array,
JsNull, JsNumber, JsObject, JsPromise, JsString, JsTypedArray, JsUint16Array,
JsUint32Array, JsUint8Array, JsUndefined, JsValue, Value,
JsFloat32Array, JsFloat64Array, JsFunction, JsInt8Array, JsInt16Array, JsInt32Array,
JsNull, JsNumber, JsObject, JsPromise, JsString, JsTypedArray, JsUint8Array, JsUint16Array,
JsUint32Array, JsUndefined, JsValue, Value,
boxed::{Finalize, JsBox},
},
};

Expand Down
2 changes: 1 addition & 1 deletion crates/neon/src/reflect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
context::Context,
handle::Handle,
result::JsResult,
types::{build, private::ValueInternal, JsString, JsValue},
types::{JsString, JsValue, build, private::ValueInternal},
};

pub fn eval<'a, 'b, C: Context<'a>>(
Expand Down
8 changes: 6 additions & 2 deletions crates/neon/src/sys/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use super::{
};

pub unsafe fn new(out: &mut Local, env: Env, length: usize) {
napi::create_array_with_length(env, length, out as *mut _).unwrap();
unsafe {
napi::create_array_with_length(env, length, out as *mut _).unwrap();
}
}

/// Gets the length of a `napi_value` containing a JavaScript Array.
Expand All @@ -16,6 +18,8 @@ pub unsafe fn new(out: &mut Local, env: Env, length: usize) {
/// exception.
pub unsafe fn len(env: Env, array: Local) -> u32 {
let mut len = 0;
napi::get_array_length(env, array, &mut len as *mut _).unwrap();
unsafe {
napi::get_array_length(env, array, &mut len as *mut _).unwrap();
}
len
}
Loading
Loading