napi-rs

A framework for building compiled Node.js add-ons in Rust via Node-API

OTHER License

Downloads
6.9M
Stars
5.4K
Committers
119

Bot releases are hidden (Show)

napi-rs - [email protected]

Published by Brooooooklyn about 2 years ago

What's Changed

New Contributors

Full Changelog: https://github.com/napi-rs/napi-rs/compare/[email protected]@2.10.0

napi-rs - @napi-rs/[email protected]

Published by Brooooooklyn about 2 years ago

2.12.0 (2022-10-04)

Bug Fixes

  • cli: custom Cargo (build) target directories (#1300) (f7c26cc)

Features

napi-rs - [email protected]

Published by Brooooooklyn about 2 years ago

What's Changed

New Contributors

Full Changelog: https://github.com/napi-rs/napi-rs/compare/[email protected]@2.9.1

napi-rs - [email protected]

Published by Brooooooklyn about 2 years ago

What's Changed

New Contributors

Full Changelog: https://github.com/napi-rs/napi-rs/compare/[email protected]@2.9.1

napi-rs - [email protected]

Published by Brooooooklyn about 2 years ago

Core changes

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 types

For 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
}

What's Changed

New Contributors

Full Changelog: https://github.com/napi-rs/napi-rs/compare/[email protected]@2.9.0

napi-rs - [email protected]

Published by Brooooooklyn about 2 years ago

What's Changed

New Contributors

Full Changelog: https://github.com/napi-rs/napi-rs/compare/[email protected]@2.9.0

napi-rs - [email protected]

Published by Brooooooklyn about 2 years ago

Core changes

Custom finalize trait

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(())
  }
}

Inject This in functions

https://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

What's Changed

New Contributors

Full Changelog: https://github.com/napi-rs/napi-rs/compare/[email protected]@2.8.0

napi-rs - [email protected]

Published by Brooooooklyn about 2 years ago

What's Changed

Full Changelog: https://github.com/napi-rs/napi-rs/compare/[email protected]@2.8.0

napi-rs - @napi-rs/[email protected]

Published by Brooooooklyn about 2 years ago

2.11.4 (2022-08-12)

Bug Fixes

  • cli: zig cross to *-apple-darwin target (14aab06)
napi-rs - @napi-rs/[email protected]

Published by Brooooooklyn about 2 years ago

2.11.3 (2022-08-12)

Bug Fixes

  • cli: ignore preinstall scripts on FreeBSD while installing yarn (1d1ef3d)
napi-rs - @napi-rs/[email protected]

Published by Brooooooklyn about 2 years ago

2.11.2 (2022-08-12)

Bug Fixes

  • cli: npm i -g flag is deprecated (9b9cd5d)
napi-rs - @napi-rs/[email protected]

Published by Brooooooklyn about 2 years ago

2.11.1 (2022-08-09)

Bug Fixes

  • cli: add .yarn and test folder to .npmignore (1cf5a0d)
napi-rs - linux-musl-cross@10

Published by Brooooooklyn about 2 years ago

Binary for linux-musl-cross toolchain

napi-rs - @napi-rs/[email protected]

Published by Brooooooklyn about 2 years ago

What's Changed

New Contributors

napi-rs - [email protected]

Published by Brooooooklyn about 2 years ago

What's Changed

napi-rs - [email protected]

Published by Brooooooklyn about 2 years ago

Core features

Set property attribute in napi macro

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");
  }
}

What's Changed

New Contributors

napi-rs - [email protected]

Published by Brooooooklyn about 2 years ago

Binary for linux-musl-cross toolchain

napi-rs - @napi-rs/[email protected]

Published by Brooooooklyn about 2 years ago

2.10.3 (2022-07-27)

Bug Fixes

  • cli: android build due to GitHub Actions environments change (fd2060b)
napi-rs - @napi-rs/[email protected]

Published by Brooooooklyn about 2 years ago

2.10.2 (2022-07-22)

Bug Fixes

  • cli: upgrade freebsd ci (ed5fd40)
napi-rs - [email protected]

Published by Brooooooklyn over 2 years ago

What's Changed

Full Changelog: https://github.com/napi-rs/napi-rs/compare/[email protected]@2.6.3