A framework for building compiled Node.js add-ons in Rust via Node-API
OTHER License
Bot releases are hidden (Show)
Published by Brooooooklyn almost 3 years ago
[email protected]
⚠️ There is no official Node.js build for Windows ARM64 for now: https://github.com/nodejs/build/issues/2450
So if you want build library for Windows ARM64, you can not use experimental
features for now.
Published by Brooooooklyn almost 3 years ago
#[napi]
fn convert_u32_array(input: Uint32Array) -> Vec<u32> {
// AsRef<[u32]>, AsMut<[u32]>, Deref<[u32]>, DerefMut<u32>
input.to_vec()
}
↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
export function convertU32Array(input: Uint32Array): Array<number>
Full Changelog: https://github.com/napi-rs/napi-rs/compare/[email protected]@2.0.0-beta.0
Published by Brooooooklyn almost 3 years ago
Published by Brooooooklyn almost 3 years ago
Published by Brooooooklyn almost 3 years ago
/// `constructor` option for `struct` requires all fields to be public,
/// otherwise tag impl fn as constructor
/// #[napi(constructor)]
#[napi]
pub struct Animal {
#[napi(readonly)]
/// Kind of animal
pub kind: Kind,
name: String,
}
#[napi]
impl Animal {
/// This is the constructor
#[napi(constructor)]
pub fn new(kind: Kind, name: String) -> Self {
Animal { kind, name }
}
/// This is a factory method
#[napi(factory)]
pub fn with_kind(kind: Kind) -> Self {
Animal {
kind,
name: "Default".to_owned(),
}
}
#[napi(getter)]
pub fn get_name(&self) -> &str {
self.name.as_str()
}
#[napi(setter)]
pub fn set_name(&mut self, name: String) {
self.name = name;
}
/// This is a
/// multi-line comment
/// with an emoji 🚀
#[napi]
pub fn whoami(&self) -> String {
match self.kind {
Kind::Dog => {
format!("Dog: {}", self.name)
}
Kind::Cat => format!("Cat: {}", self.name),
Kind::Duck => format!("Duck: {}", self.name),
}
}
#[napi]
/// This is static...
pub fn get_dog_kind() -> Kind {
Kind::Dog
}
}
⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️
/**
* `constructor` option for `struct` requires all fields to be public,
* otherwise tag impl fn as constructor
* #[napi(constructor)]
*/
export class Animal {
/** Kind of animal */
readonly kind: Kind
/** This is the constructor */
constructor(kind: Kind, name: string)
/** This is a factory method */
static withKind(kind: Kind): Animal
get name(): string
set name(name: string)
/**
* This is a
* multi-line comment
* with an emoji 🚀
*/
whoami(): string
/** This is static... */
static getDogKind(): Kind
}
Published by Brooooooklyn almost 3 years ago
threadsafe_function
related types now is under the napi4
feature flag @Brooooooklynexperimental
flag and node_api_get_module_file_name
function @BrooooooklynPublished by Brooooooklyn almost 3 years ago
Full Changelog: https://github.com/napi-rs/napi-rs/compare/[email protected]@1.8.0
Published by Brooooooklyn almost 3 years ago
use napi::bindgen_prelude::*;
use napi_derive::napi;
#[napi(ts_args_type = "a: { foo: number }", ts_return_type = "string[]")]
fn ts_rename(a: Object) -> Result<Object> {
a.get_property_names()
}
⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️
export function tsRename(a: { foo: number }): string[]
Published by Brooooooklyn almost 3 years ago
Published by Brooooooklyn almost 3 years ago
rust mod
as JavaScript Object
#[napi]
mod xxh3 {
use napi::bindgen_prelude::{BigInt, Buffer};
#[napi]
pub const ALIGNMENT: u32 = 16;
#[napi(js_name = "xxh3_64")]
pub fn xxh64(input: Buffer) -> u64 {
let mut h: u64 = 0;
for i in input.as_ref() {
h = h.wrapping_add(*i as u64);
}
h
}
}
⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️
export namespace xxh3 {
export const ALIGNMENT: number
export function xxh3_64(input: Buffer): BigInt
export function xxh128(input: Buffer): BigInt
}
&Self
for method chain#[napi]
pub struct ClassWithFactory {
pub name: String,
}
#[napi]
impl ClassWithFactory {
#[napi(factory)]
pub fn with_name(name: String) -> Self {
Self { name }
}
#[napi]
pub fn set_name(&mut self, name: String) -> &Self {
self.name = name;
self
}
}
⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️
export class ClassWithFactory {
name: string
static withName(name: string): ClassWithFactory
setName(name: string): this
}
Full Changelog: https://github.com/napi-rs/napi-rs/compare/[email protected]@2.0.0-alpha.7
Published by Brooooooklyn almost 3 years ago
Published by Brooooooklyn almost 3 years ago
Full Changelog: https://github.com/napi-rs/napi-rs/compare/@napi-rs/[email protected]@2.0.0-alpha.6
Published by Brooooooklyn almost 3 years ago
Full Changelog: https://github.com/napi-rs/napi-rs/compare/[email protected]@1.7.11
Published by Brooooooklyn almost 3 years ago
Published by Brooooooklyn almost 3 years ago
Published by Brooooooklyn almost 3 years ago
Published by Brooooooklyn almost 3 years ago
#[napi]
pub const DEFAULT_COST: u32 = 12;
⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️
export const DEFAULT_COST: number
⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️
import { DEFAULT_COST } from './index.js'
console.log(DEFAULT_COST) // 12
#[napi]
pub fn create_external(size: u32) -> External<u32> {
External::new(size)
}
#[napi]
pub fn get_external(external: External<u32>) -> u32 {
*external.as_ref()
}
#[napi]
pub fn mutate_external(mut external: External<u32>, new_val: u32) {
*external.as_mut() = new_val;
}
#[napi]
pub fn create_external_string(content: String) -> External<String> {
External::new(content)
}
⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️
export class ExternalObject<T> {
private readonly __type: unique symbol;
[val: unique symbol]: T
}
export function createExternal(size: number): ExternalObject<number>
export function getExternal(external: ExternalObject<number>): number
export function mutateExternal(external: ExternalObject<number>, newVal: number): void
export function createExternalString(content: string): ExternalObject<string>
⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️⬇️
import test from 'ava'
import { createExternal, getExternal, mutateExternal } from './index.js'
test('external', (t) => {
const FX = 42
const ext = createExternal(FX)
t.is(getExternal(ext), FX)
mutateExternal(ext, FX + 1)
t.is(getExternal(ext), FX + 1)
// Can not pass non `ExternalObject` type to functions accept `ExternalObject`
// @ts-expect-error
t.throws(() => getExternal({}))
const ext2 = createExternalString('wtf')
// Can not assign `ExternalObject<string>` to `ExternalObject<number>`
// @ts-expect-error
const e = t.throws(() => getExternal(ext2))
t.is(e.message, 'T on `get_value_external` is not the type of wrapped object')
})
Full Changelog: https://github.com/napi-rs/napi-rs/compare/[email protected]@2.0.0-alpha.5
Published by Brooooooklyn almost 3 years ago
Published by Brooooooklyn almost 3 years ago
set_property
on JsObject
should accept any JsValue
as key https://github.com/napi-rs/napi-rs/commit/bfbe2c0d915789d986594d9a5a4d4805d0083745 @BrooooooklynPublished by Brooooooklyn almost 3 years ago
windows
crate from winapi
by @Brooooooklyn in https://github.com/napi-rs/napi-rs/pull/861
Full Changelog: https://github.com/napi-rs/napi-rs/compare/@napi-rs/[email protected]@2.0.0-alpha.4