Skip to content

Commit

Permalink
Merge pull request #780 from neon-bindings/kv/rfc-borrow
Browse files Browse the repository at this point in the history
Borrow RFC
  • Loading branch information
kjvalencik committed Aug 30, 2021
2 parents f2e1960 + 75f4896 commit 24e7d6f
Show file tree
Hide file tree
Showing 28 changed files with 1,239 additions and 293 deletions.
2 changes: 2 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
[alias]
# Neon defines mutually exclusive feature flags which prevents using `cargo clippy --all-features`
# The following aliases simplify linting the entire workspace
check-napi = "check --all-targets --no-default-features -p neon -p neon-runtime -p neon-build -p neon-macros -p electron-tests -p napi-tests --features proc-macros,try-catch-api,napi-experimental"
check-legacy = "check --all-targets --no-default-features -p neon -p neon-runtime -p neon-build -p neon-macros -p tests -p static_tests --features event-handler-api,proc-macros,try-catch-api,legacy-runtime"
clippy-legacy = "clippy --all-targets --no-default-features -p neon -p neon-runtime -p neon-build -p neon-macros -p tests -p static_tests --features event-handler-api,proc-macros,try-catch-api,legacy-runtime -- -A clippy::missing_safety_doc"
clippy-napi = "clippy --all-targets --no-default-features -p neon -p neon-runtime -p neon-build -p neon-macros -p electron-tests -p napi-tests --features proc-macros,try-catch-api,napi-experimental -- -A clippy::missing_safety_doc"
neon-test = "test --no-default-features --features napi-experimental"
Expand Down
41 changes: 21 additions & 20 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,36 @@ on:
# Prevent duplicate runs of this workflow on our own internal PRs.
branches:
- main
- next/*
pull_request:
branches:
- main

- next/*
jobs:
lint:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [14.x]
rust-toolchain: [nightly]
node-version: [ 14.x ]
rust-toolchain: [ nightly ]

steps:
- uses: actions/checkout@v2
- name: Use Rust ${{ matrix.rust-toolchain }}
uses: actions-rs/toolchain@v1
with:
toolchain: ${{ matrix.rust-toolchain }}
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: install build-essential
run: sudo apt-get install -y build-essential
- name: Formatting
run: cargo fmt --all -- --check
- name: Clippy (N-API)
run: cargo clippy-napi
- name: Clippy (Legacy)
run: cargo clippy-legacy
- uses: actions/checkout@v2
- name: Use Rust ${{ matrix.rust-toolchain }}
uses: actions-rs/toolchain@v1
with:
toolchain: ${{ matrix.rust-toolchain }}
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: install build-essential
run: sudo apt-get install -y build-essential
- name: Formatting
run: cargo fmt --all -- --check
- name: Clippy (N-API)
run: cargo clippy-napi
- name: Clippy (Legacy)
run: cargo clippy-legacy
39 changes: 20 additions & 19 deletions .github/workflows/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,35 @@ on:
# Prevent duplicate runs of this workflow on our own internal PRs.
branches:
- main
- next/*
pull_request:
branches:
- main

- next/*
jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [12.x, 14.x, 16.x]
rust-toolchain: [stable, beta, nightly]
node-version: [ 12.x, 14.x, 16.x ]
rust-toolchain: [ stable, beta, nightly ]

steps:
- uses: actions/checkout@v2
- name: Use Rust ${{ matrix.rust-toolchain }}
uses: actions-rs/toolchain@v1
with:
toolchain: ${{ matrix.rust-toolchain }}
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: install build-essential
run: sudo apt-get install -y build-essential
- name: run cargo test
run: xvfb-run --auto-servernum cargo neon-test -- --nocapture
- name: run CLI test
working-directory: ./create-neon
run: npm test
- uses: actions/checkout@v2
- name: Use Rust ${{ matrix.rust-toolchain }}
uses: actions-rs/toolchain@v1
with:
toolchain: ${{ matrix.rust-toolchain }}
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: install build-essential
run: sudo apt-get install -y build-essential
- name: run cargo test
run: xvfb-run --auto-servernum cargo neon-test -- --nocapture
- name: run CLI test
working-directory: ./create-neon
run: npm test
43 changes: 22 additions & 21 deletions .github/workflows/macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,37 @@ on:
# Prevent duplicate runs of this workflow on our own internal PRs.
branches:
- main
- next/*
pull_request:
branches:
- main

- next/*
jobs:
build:

runs-on: macos-latest

strategy:
matrix:
node-version: [12.x, 14.x, 16.x]
rust-toolchain: [stable, beta, nightly]
node-version: [ 12.x, 14.x, 16.x ]
rust-toolchain: [ stable, beta, nightly ]

steps:
- uses: actions/checkout@v2
- name: Use Rust ${{ matrix.rust-toolchain }}
uses: actions-rs/toolchain@v1
with:
toolchain: ${{ matrix.rust-toolchain }}
target: x86_64-apple-darwin
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
# - name: update node-gyp to latest
# # https://github.com/nodejs/node-gyp/issues/1933#issuecomment-586915535
# run: npm install -g node-gyp@latest
- name: run cargo test
run: cargo neon-test
- name: run CLI test
working-directory: ./create-neon
run: npm test
- uses: actions/checkout@v2
- name: Use Rust ${{ matrix.rust-toolchain }}
uses: actions-rs/toolchain@v1
with:
toolchain: ${{ matrix.rust-toolchain }}
target: x86_64-apple-darwin
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
# - name: update node-gyp to latest
# # https://github.com/nodejs/node-gyp/issues/1933#issuecomment-586915535
# run: npm install -g node-gyp@latest
- name: run cargo test
run: cargo neon-test
- name: run CLI test
working-directory: ./create-neon
run: npm test
65 changes: 33 additions & 32 deletions .github/workflows/windows.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,48 @@ on:
# Prevent duplicate runs of this workflow on our own internal PRs.
branches:
- main
- next/*
pull_request:
branches:
- main

- next/*
jobs:
build:

runs-on: windows-latest

strategy:
matrix:
node-version: [12.x, 14.x, 16.x]
rust-toolchain: [stable, beta, nightly]
node-version: [ 12.x, 14.x, 16.x ]
rust-toolchain: [ stable, beta, nightly ]

steps:
- uses: actions/checkout@v2
- name: Use Rust ${{ matrix.rust-toolchain }}
uses: actions-rs/toolchain@v1
with:
toolchain: ${{ matrix.rust-toolchain }}
target: x86_64-pc-windows-msvc
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: Use npm v6
if: ${{ matrix.node-version == '16.x' }}
run: npm install -g npm@6
- name: Install libclang
uses: KyleMayes/install-llvm-action@01144dc97b1e2693196c3056414a44f15180648b
with:
version: "10"
directory: ${{ runner.temp }}/llvm
# - name: update node-gyp to latest
# # https://github.com/nodejs/node-gyp/issues/1933#issuecomment-586915535
# run: npm install -g node-gyp@latest
- run: npm config set msvs_version 2019
- name: run cargo test
run: cargo neon-test
env:
LIBCLANG_PATH: ${{ runner.temp }}/llvm/bin
- name: run CLI test
working-directory: ./create-neon
run: npm test
- uses: actions/checkout@v2
- name: Use Rust ${{ matrix.rust-toolchain }}
uses: actions-rs/toolchain@v1
with:
toolchain: ${{ matrix.rust-toolchain }}
target: x86_64-pc-windows-msvc
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}
- name: Use npm v6
if: ${{ matrix.node-version == '16.x' }}
run: npm install -g npm@6
- name: Install libclang
uses: KyleMayes/install-llvm-action@01144dc97b1e2693196c3056414a44f15180648b
with:
version: "10"
directory: ${{ runner.temp }}/llvm
# - name: update node-gyp to latest
# # https://github.com/nodejs/node-gyp/issues/1933#issuecomment-586915535
# run: npm install -g node-gyp@latest
- run: npm config set msvs_version 2019
- name: run cargo test
run: cargo neon-test
env:
LIBCLANG_PATH: ${{ runner.temp }}/llvm/bin
- name: run CLI test
working-directory: ./create-neon
run: npm test
33 changes: 30 additions & 3 deletions crates/neon-runtime/src/napi/arraybuffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@ use crate::raw::{Env, Local};
use std::mem::MaybeUninit;
use std::os::raw::c_void;
use std::ptr::null_mut;
use std::slice;

use crate::napi::bindings as napi;

pub unsafe fn new(out: &mut Local, env: Env, size: u32) -> bool {
let status = napi::create_arraybuffer(env, size as usize, null_mut(), out as *mut _);
pub unsafe fn new(env: Env, len: usize) -> Result<Local, napi::Status> {
let mut buf = MaybeUninit::uninit();
let status = napi::create_arraybuffer(env, len, null_mut(), buf.as_mut_ptr());

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

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

Ok(buf.assume_init())
}

pub unsafe fn data(env: Env, base_out: &mut *mut c_void, obj: Local) -> usize {
Expand Down Expand Up @@ -48,3 +56,22 @@ where
unsafe extern "C" fn drop_external<T>(_env: Env, _data: *mut c_void, hint: *mut c_void) {
Box::<T>::from_raw(hint as *mut _);
}

/// # Safety
/// * Caller must ensure `env` and `buf` are valid
/// * The lifetime `'a` does not exceed the lifetime of `Env` or `buf`
pub unsafe fn as_mut_slice<'a>(env: Env, buf: Local) -> &'a mut [u8] {
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,
);

if size == 0 {
return &mut [];
}

slice::from_raw_parts_mut(data.assume_init().cast(), size)
}
11 changes: 11 additions & 0 deletions crates/neon-runtime/src/napi/bindings/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ mod napi1 {
fn close_handle_scope(env: Env, scope: HandleScope) -> Status;

fn is_arraybuffer(env: Env, value: Value, result: *mut bool) -> Status;
fn is_typedarray(env: Env, value: Value, result: *mut bool) -> Status;
fn is_buffer(env: Env, value: Value, result: *mut bool) -> Status;
fn is_error(env: Env, value: Value, result: *mut bool) -> Status;
fn is_array(env: Env, value: Value, result: *mut bool) -> Status;
Expand Down Expand Up @@ -90,6 +91,16 @@ mod napi1 {
byte_length: *mut usize,
) -> Status;

fn get_typedarray_info(
env: Env,
typedarray: Value,
typ: *mut TypedArrayType,
length: *mut usize,
data: *mut *mut c_void,
buf: *mut Value,
offset: *mut usize,
) -> Status;

fn create_buffer(
env: Env,
length: usize,
Expand Down
1 change: 1 addition & 0 deletions crates/neon-runtime/src/napi/bindings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ macro_rules! generate {
use std::sync::Once;

pub(crate) use functions::*;
pub use types::TypedArrayType;
pub(crate) use types::*;

mod functions;
Expand Down
17 changes: 17 additions & 0 deletions crates/neon-runtime/src/napi/bindings/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,23 @@ pub(crate) enum ValueType {
BigInt = 9,
}

#[allow(dead_code)]
#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub enum TypedArrayType {
I8 = 0,
U8 = 1,
U8Clamped = 2,
I16 = 3,
U16 = 4,
I32 = 5,
U32 = 6,
F32 = 7,
F64 = 8,
I64 = 9,
U64 = 10,
}

#[allow(dead_code)]
#[repr(u32)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
Expand Down

0 comments on commit 24e7d6f

Please sign in to comment.