Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typed Array discoverability #909

Merged
merged 31 commits into from
Sep 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
e5ee685
WIP
dherman Jun 17, 2022
6d136af
- Use aquamarine crate to product the type hierarchy diagram in neon:…
dherman Jun 30, 2022
a50522e
Types diagram cosmetics:
dherman Jun 30, 2022
04f122e
- Divide type hierarchy diagram into two diagrams and add some more c…
dherman Jun 30, 2022
321bb68
Prettier
dherman Jul 1, 2022
5b5da7e
cargo fmt fixes
dherman Jul 1, 2022
bbcd086
Copy nit: avoid the word "number" when describing object types
dherman Jul 1, 2022
9172444
Move `Binary` trait into `crate::types::buffer::private` so it doesn'…
dherman Jul 8, 2022
9db2163
Add missing module.
dherman Jul 8, 2022
ff4e501
Move definition of typed array type aliases into the macro.
dherman Jul 9, 2022
1a49e5b
Elide lifetimes
dherman Jul 9, 2022
84c5fd8
- Change `from_array_buffer()` to `from_buffer_region()` and add more…
dherman Jul 11, 2022
bbf3cda
Add API docs for typed array methods
dherman Jul 11, 2022
45780d5
Make `JsTypedArray` into a wide pointer (like `JsBox`) that caches al…
dherman Jul 12, 2022
e119b79
Docs copy edit
dherman Jul 12, 2022
03c0cf9
`TypedArray::byte_length()` doesn't need an FFI call for `JsTypedArray`
dherman Jul 12, 2022
776ba84
Address all Clippy warnings and errors.
dherman Jul 15, 2022
3f0fc4c
Add tests for detached buffers, and move typed array tests into separ…
dherman Jul 22, 2022
ffb089b
Abstract some boilerplate in the various detaching test cases
dherman Jul 22, 2022
480298c
Test byteOffset as well
dherman Jul 23, 2022
5785b96
Fix the tests by removing the caching of offset and length
dherman Jul 23, 2022
6bc6aaa
Add a `Region` type to represent a typed region of a buffer:
dherman Jul 23, 2022
9cf8c53
rustfmt
dherman Jul 23, 2022
28b6136
prettier
dherman Jul 23, 2022
912b178
fix doc test
dherman Jul 23, 2022
f24f1e7
Update for latest RFC changes:
dherman Aug 1, 2022
74251cb
- API tweak: TypedArray::Item must implement Binary
dherman Aug 1, 2022
326a9d0
prettier
dherman Aug 1, 2022
aa6c655
Address review suggestions:
dherman Aug 28, 2022
f6c2143
Satisfying the linters with ritualistic sacrifices
dherman Aug 28, 2022
7fb1779
Doc build fix: cfg_attr should do a feature test
dherman Sep 14, 2022
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
4 changes: 2 additions & 2 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
# The following aliases simplify linting the entire workspace
neon-check = " check --all --all-targets --features napi-experimental,futures"
neon-clippy = "clippy --all --all-targets --features napi-experimental,futures -- -A clippy::missing_safety_doc"
neon-test = " test --all --features=napi-experimental,futures"
neon-doc = " rustdoc -p neon --features=napi-experimental,futures -- --cfg docsrs"
neon-test = " test --all --features=doc-comment,napi-experimental,futures"
kjvalencik marked this conversation as resolved.
Show resolved Hide resolved
neon-doc = " rustdoc -p neon --features=doc-dependencies,napi-experimental,futures -- --cfg docsrs"
6 changes: 6 additions & 0 deletions crates/neon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ semver = "1"
smallvec = "1.4.2"
once_cell = "1.10.0"
neon-macros = { version = "=1.0.0-alpha.1", path = "../neon-macros" }
aquamarine = { version = "0.1.11", optional = true }
doc-comment = { version = "0.3.3", optional = true }

[dependencies.tokio]
version = "1.18.2"
Expand Down Expand Up @@ -70,9 +72,13 @@ task-api = []
# DEPRECATED: This is always enabled and should be removed.
proc-macros = []

# Enables the optional dependencies that are only used for generating the API docs.
doc-dependencies = ["doc-comment", "aquamarine"]

[package.metadata.docs.rs]
rustdoc-args = ["--cfg", "docsrs"]
features = [
"futures",
"napi-experimental",
"doc-dependencies",
]
4 changes: 2 additions & 2 deletions crates/neon/src/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,12 +322,12 @@ pub trait Context<'a>: ContextInternal<'a> {

/// Convenience method for creating a `JsNull` value.
fn null(&mut self) -> Handle<'a, JsNull> {
return JsNull::new(self);
JsNull::new(self)
}

/// Convenience method for creating a `JsUndefined` value.
fn undefined(&mut self) -> Handle<'a, JsUndefined> {
return JsUndefined::new(self);
JsUndefined::new(self)
}

/// Convenience method for creating an empty `JsObject` value.
Expand Down
2 changes: 1 addition & 1 deletion crates/neon/src/handle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ pub struct Handle<'a, T: Managed + 'a> {
impl<'a, T: Managed> Clone for Handle<'a, T> {
fn clone(&self) -> Self {
Self {
value: self.value.clone(),
value: self.value,
phantom: PhantomData,
}
}
Expand Down
8 changes: 7 additions & 1 deletion crates/neon/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,13 @@ pub mod result;
mod sys;
#[cfg(feature = "napi-6")]
pub mod thread;
pub mod types;
// To use the #[aquamarine] attribute on the top-level neon::types module docs, we have to
// use this hack so we can keep the module docs in a separate file.
// See: https://github.com/mersinvald/aquamarine/issues/5#issuecomment-1168816499
mod types_docs;
dherman marked this conversation as resolved.
Show resolved Hide resolved
mod types_impl;

pub use types_docs::exports as types;

#[doc(hidden)]
pub mod macro_internal;
Expand Down
8 changes: 3 additions & 5 deletions crates/neon/src/lifecycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ impl LocalCell {
// Kick off a new transaction and drop it before getting the result.
{
let mut tx = TryInitTransaction::new(cx, id);
tx.run(|cx| Ok(f(cx)?))?;
tx.run(|cx| f(cx))?;
}

// If we're here, the transaction has succeeded, so get the result.
Expand Down Expand Up @@ -195,11 +195,9 @@ impl<'cx, 'a, C: Context<'cx>> TryInitTransaction<'cx, 'a, C> {
InstanceData::locals(self.cx).get(self.id)
}

#[allow(clippy::wrong_self_convention)]
fn is_trying(&mut self) -> bool {
match self.cell() {
LocalCell::Trying => true,
_ => false,
}
matches!(self.cell(), LocalCell::Trying)
}
}

Expand Down
6 changes: 4 additions & 2 deletions crates/neon/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ pub use crate::{
result::{JsResult, NeonResult, ResultExt as NeonResultExt},
types::{
boxed::{Finalize, JsBox},
JsArray, JsArrayBuffer, JsBoolean, JsBuffer, JsError, JsFunction, JsNull, JsNumber,
JsObject, JsPromise, JsString, JsTypedArray, JsUndefined, JsValue, Value,
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,
},
};

Expand Down
14 changes: 14 additions & 0 deletions crates/neon/src/sys/arraybuffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,17 @@ pub unsafe fn as_mut_slice<'a>(env: Env, buf: Local) -> &'a mut [u8] {

slice::from_raw_parts_mut(data.assume_init().cast(), size)
}

/// # Safety
/// * Caller must ensure `env` and `buf` are valid
pub unsafe fn size(env: Env, buf: Local) -> usize {
let mut data = MaybeUninit::uninit();
let mut size = 0usize;

assert_eq!(
napi::get_arraybuffer_info(env, buf, data.as_mut_ptr(), &mut size as *mut _),
napi::Status::Ok,
);

size
}
9 changes: 9 additions & 0 deletions crates/neon/src/sys/bindings/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ mod napi1 {
byte_length: *mut usize,
) -> Status;

fn create_typedarray(
env: Env,
type_: TypedArrayType,
length: usize,
arraybuffer: Value,
byte_offset: usize,
result: *mut Value,
) -> Status;

fn get_typedarray_info(
env: Env,
typedarray: Value,
Expand Down
2 changes: 1 addition & 1 deletion crates/neon/src/sys/bindings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! These types are manually copied from bindings generated from `bindgen`. To
//! update, use the following approach:
//!
//! * Run `cargo build` with `--cfg neon=dev` at least once to install `nodejs-sys`
//! * Run a debug build of Neon at least once to install `nodejs-sys`
//! * Open the generated bindings at `target/debug/build/nodejs-sys-*/out/bindings.rs`
//! * Copy the types needed into `types.rs` and `functions.rs`
//! * Modify to match Rust naming conventions:
Expand Down
14 changes: 14 additions & 0 deletions crates/neon/src/sys/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,17 @@ pub unsafe fn as_mut_slice<'a>(env: Env, buf: Local) -> &'a mut [u8] {

slice::from_raw_parts_mut(data.assume_init().cast(), size)
}

/// # Safety
/// * Caller must ensure `env` and `buf` are valid
pub unsafe fn size(env: Env, buf: Local) -> usize {
let mut data = MaybeUninit::uninit();
let mut size = 0usize;

assert_eq!(
napi::get_buffer_info(env, buf, data.as_mut_ptr(), &mut size as *mut _),
napi::Status::Ok,
);

size
}
8 changes: 3 additions & 5 deletions crates/neon/src/sys/no_panic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,13 +210,11 @@ unsafe fn error_from_message(env: Env, msg: &str) -> Local {

let status = napi::create_error(env, ptr::null_mut(), msg, err.as_mut_ptr());

let err = if status == napi::Status::Ok {
if status == napi::Status::Ok {
err.assume_init()
} else {
fatal_error("Failed to create an Error");
};

err
}
}

#[track_caller]
Expand Down Expand Up @@ -246,7 +244,7 @@ unsafe fn panic_msg(panic: &Panic) -> Option<&str> {
if let Some(msg) = panic.downcast_ref::<&str>() {
Some(msg)
} else if let Some(msg) = panic.downcast_ref::<String>() {
Some(&msg)
Some(msg)
} else {
None
}
Expand Down
2 changes: 1 addition & 1 deletion crates/neon/src/sys/tsfn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl<T: Send + 'static> ThreadsafeFunction<T> {

Self {
tsfn: Tsfn(result.assume_init()),
is_finalized: is_finalized,
is_finalized,
callback,
}
}
Expand Down
19 changes: 19 additions & 0 deletions crates/neon/src/sys/typedarray.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,22 @@ pub unsafe fn info(env: Env, value: Local) -> TypedArrayInfo {

info.assume_init()
}

pub unsafe fn new(
env: Env,
typ: TypedArrayType,
buffer: Local,
offset: usize,
len: usize,
) -> Result<Local, napi::Status> {
let mut array = MaybeUninit::uninit();
let status = napi::create_typedarray(env, typ, len, buffer, offset, array.as_mut_ptr());

if status == napi::Status::PendingException {
return Err(status);
}

assert_eq!(status, napi::Status::Ok);

Ok(array.assume_init())
}
157 changes: 0 additions & 157 deletions crates/neon/src/types/buffer/mod.rs

This file was deleted.

Loading