A framework for building compiled Node.js add-ons in Rust via Node-API
OTHER License
Bot releases are visible (Hide)
Published by Brooooooklyn about 2 years ago
impl<T: Into<Vec<u8>>> From<T> for Uint8Array
by @usrtax in https://github.com/napi-rs/napi-rs/pull/1317
Vec<(std::string::String, u16)>
and some other small change by @usrtax in https://github.com/napi-rs/napi-rs/pull/1320
Full Changelog: https://github.com/napi-rs/napi-rs/compare/[email protected]@2.10.0
Published by Brooooooklyn about 2 years ago
Buffer
by @seritools in https://github.com/napi-rs/napi-rs/pull/1294
Full Changelog: https://github.com/napi-rs/napi-rs/compare/[email protected]@2.9.1
Published by Brooooooklyn about 2 years ago
Full Changelog: https://github.com/napi-rs/napi-rs/compare/[email protected]@2.9.1
Published by Brooooooklyn about 2 years ago
as_object
for ClassInstance
You can use the ClassInstance
as Object
on the Rust side and manipulate it.
#[napi]
impl CanvasElement {
#[napi(constructor)]
pub fn new(mut env: Env, mut this: This, width: u32, height: u32) -> Result<Self> {
let ctx = CanvasRenderingContext2D::into_instance(
CanvasRenderingContext2D {
context: Context::new(width, height, ColorSpace::default())?,
},
env,
)?;
ctx.as_object(env).define_properties(&[
Property::new(FILL_STYLE_HIDDEN_NAME)?
.with_value(&env.create_string("#000")?)
.with_property_attributes(PropertyAttributes::Writable | PropertyAttributes::Configurable),
Property::new(STROKE_STYLE_HIDDEN_NAME)?
.with_value(&env.create_string("#000")?)
.with_property_attributes(PropertyAttributes::Writable | PropertyAttributes::Configurable),
])?;
env.adjust_external_memory((width * height * 4) as i64)?;
this.define_properties(&[Property::new("ctx")?
.with_value(&ctx)
.with_property_attributes(PropertyAttributes::Default)])?;
Ok(Self { width, height, ctx })
}
}
as_unknown
for Either
typesFor the scenario that preserves original JavaScript values in Either
types and sets them into object property, and retrieves it back in the other place.
#[napi(getter)]
pub fn get_fill_style(&self, this: This) -> Result<Unknown> {
this.get_named_property_unchecked(FILL_STYLE_HIDDEN_NAME)
}
#[napi(setter, return_if_invalid)]
pub fn set_fill_style(
&mut self,
env: Env,
mut this: This,
fill_style: Either3<JsString, ClassInstance<CanvasGradient>, ClassInstance<CanvasPattern>>,
) -> Result<()> {
// ... some logic
let raw_fill_style = fill_style.as_unknown(env);
this.set(FILL_STYLE_HIDDEN_NAME, &raw_fill_style)?;
Ok(())
}
ToNapiValue
for f32
You can use f32
as the return type:
#[napi]
pub fn return_f32() -> f32 {
3.14
}
ThreadsafeFunction
's callback closure captures data by @messense in https://github.com/napi-rs/napi-rs/pull/1281
Full Changelog: https://github.com/napi-rs/napi-rs/compare/[email protected]@2.9.0
Published by Brooooooklyn about 2 years ago
Full Changelog: https://github.com/napi-rs/napi-rs/compare/[email protected]@2.9.0
Published by Brooooooklyn about 2 years ago
https://napi.rs/docs/concepts/class#custom-finalize-logic
use napi::bindgen_prelude::*;
use napi_derive::napi;
#[napi(custom_finalize)]
pub struct CustomFinalize {
width: u32,
height: u32,
inner: Vec<u8>,
}
#[napi]
impl CustomFinalize {
#[napi(constructor)]
pub fn new(mut env: Env, width: u32, height: u32) -> Result<Self> {
let inner = vec![0; (width * height * 4) as usize];
let inner_size = inner.len();
env.adjust_external_memory(inner_size as i64)?;
Ok(Self {
width,
height,
inner,
})
}
}
impl ObjectFinalize for CustomFinalize {
fn finalize(self, mut env: Env) -> Result<()> {
env.adjust_external_memory(-(self.inner.len() as i64))?;
Ok(())
}
}
This
in functionshttps://napi.rs/docs/concepts/inject-this
use napi::bindgen_prelude::*;
use napi_derive::napi;
#[napi(constructor)]
pub struct Width {
pub value: i32,
}
#[napi]
pub fn plus_one(this: This<&Width>) -> i32 {
this.value + 1
}
instance of
https://napi.rs/docs/concepts/class#instance-of
use napi::bindgen_prelude::*;
use napi_derive::napi;
#[napi]
pub struct NativeClass {}
#[napi]
pub fn is_native_class_instance(env: Env, value: Unknown) -> Result<bool> {
NativeClass::instance_of(env, value)
}
import { NativeClass, isNativeClassInstance } from './index.js'
const nc = new NativeClass()
console.log(isNativeClassInstance(nc)) // true
console.log(isNativeClassInstance(1)) // false
WeakReference
by @Brooooooklyn in https://github.com/napi-rs/napi-rs/pull/1274
error_anyhow
feature by @i-user-link in https://github.com/napi-rs/napi-rs/pull/1275
Full Changelog: https://github.com/napi-rs/napi-rs/compare/[email protected]@2.8.0
Published by Brooooooklyn about 2 years ago
This<Value>
into raw function by @Brooooooklyn in https://github.com/napi-rs/napi-rs/pull/1273
Full Changelog: https://github.com/napi-rs/napi-rs/compare/[email protected]@2.8.0
Published by Brooooooklyn about 2 years ago
Published by Brooooooklyn about 2 years ago
Published by Brooooooklyn about 2 years ago
Published by Brooooooklyn about 2 years ago
Published by Brooooooklyn about 2 years ago
Binary for linux-musl-cross toolchain
Published by Brooooooklyn about 2 years ago
CARGO_TARGET_DIR
if set by @amrbashir in https://github.com/napi-rs/napi-rs/pull/1251
*.node
doesn't exist warning msg wording by @amrbashir in https://github.com/napi-rs/napi-rs/pull/1254
Published by Brooooooklyn about 2 years ago
#[napi(strict)]
on &T
and &mut T
by @Hywan in https://github.com/napi-rs/napi-rs/pull/1238
Published by Brooooooklyn about 2 years ago
The Object property attribute in objects and Class created by NAPI-RS is Writable
& Configurable
& Enumerable
by default now.
For NativeClass
:
#[napi]
pub struct NativeClass {}
#[napi]
impl NativeClass {
#[napi]
pub fn hello(&self) {
println!("hello");
}
}
Before:
const instance = new NativeClass()
instance.hello = function() {} // Cannot assign to read only property \'hello\' of object \'#<NativeClass>\'
After:
const instance = new NativeClass()
instance.hello = function() {} // Just fine
You can also configure the Property attribute via #[napi]
:
#[napi]
pub struct NativeClass {}
#[napi]
impl NativeClass {
#[napi(configurable = false, writable = false, enumerable = false)]
pub fn hello(&self) {
println!("hello");
}
}
ThreadsafeFunctionCallMode
by @seritools in https://github.com/napi-rs/napi-rs/pull/1243
Published by Brooooooklyn about 2 years ago
Binary for linux-musl-cross toolchain
Published by Brooooooklyn about 2 years ago
Published by Brooooooklyn about 2 years ago
Published by Brooooooklyn over 2 years ago
Full Changelog: https://github.com/napi-rs/napi-rs/compare/[email protected]@2.6.3