napi
napi@2.12.1
3/23/2023
What's Changed
- fix(napi): big numbers losing precision on serde_json::Value by @ceifa (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1538
Full Changelog: https://github.com/napi-rs/napi-rs/compare/napi-derive@2.12.2...napi@2.12.1
napi-derive@2.12.2
3/22/2023
What's Changed
- fix(cli,napi-derive): backward compatible with older cli with #1531 by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1536
Full Changelog: https://github.com/napi-rs/napi-rs/compare/napi-derive@2.12.1...napi-derive@2.12.2
napi@2.12.0
3/21/2023
What's Changed
- chore(napi): upgrade bitflags to v2 by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1518
- fix(napi): use weak arc for passing
thread_finalize_data
by @meowtec (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1525 - fix(napi):
ThreadsafeFunctionHandle
never being dropped by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1530 - feat: export registers in wasm32 target by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1529
- feat(napi): property getter and setter with closure by @victorteokw (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1526
New Contributors
- @meowtec (opens in a new tab) made their first contribution in https://github.com/napi-rs/napi-rs/pull/1525
- @nebarf (opens in a new tab) made their first contribution in https://github.com/napi-rs/napi-rs/pull/1527
- @victorteokw (opens in a new tab) made their first contribution in https://github.com/napi-rs/napi-rs/pull/1526
Full Changelog: https://github.com/napi-rs/napi-rs/compare/napi@2.11.4...napi@2.12.0
napi-derive@2.12.1
3/21/2023
What's Changed
- fix(cli,napi-derive): re-export types from shared crate by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1531
Full Changelog: https://github.com/napi-rs/napi-rs/compare/napi-derive@2.12.0...napi-derive@2.12.1
napi-derive@2.12.0
3/21/2023
What's Changed
- feat: export registers in wasm32 target by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1529
Full Changelog: https://github.com/napi-rs/napi-rs/compare/napi@2.11.4...napi-derive@2.12.0
napi@2.11.4
3/14/2023
What's Changed
- fix(napi): prevent access to tsfn-raw after env's destroyed(#1514) by @HotQ (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1515
New Contributors
- @HotQ (opens in a new tab) made their first contribution in https://github.com/napi-rs/napi-rs/pull/1515
Full Changelog: https://github.com/napi-rs/napi-rs/compare/napi@2.11.3...napi@2.11.4
napi@2.11.3
3/14/2023
What's Changed
- fix(napi): display should be implemented on error generics with
Debug
by @h-a-n-a (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1497 - feat(napi): expose
uv_run
by @MarkusJx (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1499 - test(napi): tests for custom gc in worker_threads by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1505
- chore(napi): remove thread_local from dependenies by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1506
- build: add libc++ in aarch64-linux-gnu Docker by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1511
- chore: ci components upgrade by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1512
- fix(napi): panic when
Promise
callbacks trigger after Promise is dropped by @pose (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1516
New Contributors
- @MarkusJx (opens in a new tab) made their first contribution in https://github.com/napi-rs/napi-rs/pull/1499
- @pose (opens in a new tab) made their first contribution in https://github.com/napi-rs/napi-rs/pull/1516
Full Changelog: https://github.com/napi-rs/napi-rs/compare/napi-derive@2.11.1...napi@2.11.3
napi-derive@2.11.2
3/14/2023
What's Changed
- chore(napi-derive): remove thread_local from dependenies by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1506
Full Changelog: https://github.com/napi-rs/napi-rs/compare/napi-derive@2.11.1...napi-derive@2.11.2
napi@2.11.2
2/18/2023
What's Changed
- fix(napi): error should be
Send Sync
conditionally by @h-a-n-a (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1487
Full Changelog: https://github.com/napi-rs/napi-rs/compare/napi@2.11.1...napi@2.11.2
napi-derive@2.11.1
2/18/2023
Changes
- Part of https://github.com/napi-rs/napi-rs/releases/tag/%40napi-rs%2Fcli%402.14.8 by @forehalo (opens in a new tab)
Full Changelog: https://github.com/napi-rs/napi-rs/compare/@napi-rs/cli@2.14.8...napi-derive@2.11.1
napi@2.11.1
2/9/2023
Error::new
API enhancement
Error::new
now support passing status: AsRef<str>
and message: ToString
pub enum CustomError {
NapiError(Error<Status>),
Panic,
}
impl AsRef<str> for CustomError {
fn as_ref(&self) -> &str {
match self {
CustomError::Panic => "Panic",
CustomError::NapiError(e) => e.status.as_ref(),
}
}
}
#[napi]
pub fn custom_status_code() -> Result<(), CustomError> {
Err(Error::new(CustomError::Panic, "don't panic"))
}
test('custom status code in Error', (t) => {
t.throws(() => customStatusCode(), {
code: 'Panic',
})
})
What's Changed
- fix(napi): support custom status in Error by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1486
Full Changelog: https://github.com/napi-rs/napi-rs/compare/napi@2.11.0...napi@2.11.1
napi@2.11.0
2/8/2023
Spread tuple arguments in ThreadsafeFunction
#[napi]
pub fn accept_threadsafe_function_tuple_args(func: ThreadsafeFunction<(u32, bool, String)>) {
thread::spawn(move || {
func.call(
Ok((1, false, "NAPI-RS".into())),
ThreadsafeFunctionCallMode::NonBlocking,
);
});
}
⬇️⬇️⬇️⬇️⬇️⬇️⬇️
export function acceptThreadsafeFunctionTupleArgs(func: (err: Error | null, arg0: number, arg1: boolean, arg2: string) => any): void
What's Changed
- feat(napi): convert
ToNapiValue
tuple to variadic tsfn by @h-a-n-a (opens in a new tab) #1475 - fix(napi): convert u64 to u32 in serialization by @m1212e (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1478
New Contributors
- @m1212e (opens in a new tab) made their first contribution in https://github.com/napi-rs/napi-rs/pull/1478
Full Changelog: https://github.com/napi-rs/napi-rs/compare/napi@2.10.17...napi@2.11.0
napi@2.10.17
2/8/2023
What's Changed
- fix(napi): impl Send Sync to External (opens in a new tab) by @Brooooooklyn
Full Changelog: https://github.com/napi-rs/napi-rs/compare/napi@2.10.16...napi@2.10.17
napi@2.10.16
2/7/2023
What's Changed
- fix(napi): dropping Error should not call sys if feature is set to noop by @h-a-n-a (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1477
Full Changelog: https://github.com/napi-rs/napi-rs/compare/napi@2.10.15...napi@2.10.16
napi@2.10.15
2/5/2023
What's Changed
- build(deps): bump http-cache-semantics from 4.1.0 to 4.1.1 by @dependabot (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1472
- chore(napi): enhance the error messages while converting types failed by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1473
Full Changelog: https://github.com/napi-rs/napi-rs/compare/@napi-rs/cli@2.14.6...napi@2.10.15
napi@2.10.14
1/31/2023
What's Changed
- fix(napi): run_script return type by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1467
Full Changelog: https://github.com/napi-rs/napi-rs/compare/napi@2.10.13...napi@2.10.14
napi-derive@2.10.1
1/31/2023
What's Changed
- fix(napi-derive): incorrect Task output type in some cases by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1466
Full Changelog: https://github.com/napi-rs/napi-rs/compare/napi-derive@2.10.0...napi-derive@2.10.1
napi@2.10.13
1/28/2023
What's Changed
- fix(napi): also apply electron external data fallback to lowlevel APIs by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1458
Full Changelog: https://github.com/napi-rs/napi-rs/compare/napi@2.10.12...napi@2.10.13
napi@2.10.12
1/28/2023
What's Changed
- fix(napi): add missing NoExternalBuffersAllowed by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1457
Full Changelog: https://github.com/napi-rs/napi-rs/compare/napi@2.10.11...napi@2.10.12
napi@2.10.11
1/25/2023
What's Changed
- fix(napi): fallback to copy arraybuffer if zero copy is not allowed by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1455
Background
Electron disallowed the create_external_xxx
API since electron@21
: https://www.electronjs.org/blog/v8-memory-cage
NAPI-RS will fallback to use create_buffer_copy
if napi_create_external_buffer
return the napi_no_external_buffers_allowed
status.
Full Changelog: https://github.com/napi-rs/napi-rs/compare/napi@2.10.10...napi@2.10.11
napi@2.10.10
1/24/2023
What's Changed
- chore(napi): implement FromNapiValue for ThreadsafeFunction by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1447
- fix(napi): throw fatal error if cast return value failed by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1450
- fix(napi): add UnknownReturnValue to use in call_async scenario by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1451
- fix(napi): unhandled promise rejection while using
EitherN<Promise<..>>
by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1452
Full Changelog: https://github.com/napi-rs/napi-rs/compare/@napi-rs/cli@2.14.4...napi@2.10.10
napi-derive@2.10.0
1/24/2023
Highlights
ThreadsafeFunction
as params and object fields
#[napi]
pub fn accept_callback(callback: ThreadsafeFunction<u32>) {
std::thread::spawn(move || {
callback.call(200);
});
}
⬇️⬇️⬇️⬇️⬇️
export function acceptCallback(callback: (err: Error | null, value: number) -> any)
Disable ToNapiValue
for Object
With the object_to_js = false
filed, the ThreadsafeFunction
could be set as an Object field.
#[napi(object, object_to_js = false)]
pub struct Options {
pub enable_jsx: bool,
pub on_data: ThreadsafeFunction<Vec<u8>>,
}
What's Changed
- feat(napi-derive): generate ThreadsafeFunction types by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1449
- feat(napi-derive): allow partial implement From/To Napivalue for Object by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1448
- chore(napi-derive): allow unsafe async &mut self by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1453
Full Changelog: https://github.com/napi-rs/napi-rs/compare/@napi-rs/cli@2.14.4...napi-derive@2.10.0
napi@2.10.9
1/19/2023
What's Changed
- fix(napi): fallback to copy buffer if zero-copy is not allowed by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1445
Background
Electron disallowed the create_external_xxx
API since electron@21
: https://www.electronjs.org/blog/v8-memory-cage
NAPI-RS will fallback to use create_buffer_copy
if napi_create_external_buffer
return the napi_no_external_buffers_allowed
status.
Full Changelog: https://github.com/napi-rs/napi-rs/compare/napi@2.10.8...napi@2.10.9
napi-sys@2.2.3
1/19/2023
What's Changed
- chore(sys): add
napi_no_external_buffers_allowed
toStatus
by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1445
Full Changelog: https://github.com/napi-rs/napi-rs/compare/napi@2.10.8...napi-sys@2.2.3
napi@2.10.8
1/18/2023
What's Changed
- fix(napi): remove useless aquire while creating ThreadsafeFunction by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1442
Full Changelog: https://github.com/napi-rs/napi-rs/compare/napi@2.10.7...napi@2.10.8
napi-derive@2.9.5
1/14/2023
What's Changed
- fix(napi-derive): fix union type generation for ts function notation by @h-a-n-a (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1439
Full Changelog: https://github.com/napi-rs/napi-rs/compare/@napi-rs/cli@2.14.3...napi-derive@2.9.5
napi-derive@2.9.4
1/13/2023
What's Changed
- fix(napi-derive): remove ts type on field attrs for feature
noop
by @h-a-n-a (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1436
Full Changelog: https://github.com/napi-rs/napi-rs/compare/napi@2.10.6...napi-derive@2.9.4
napi@2.10.6
1/11/2023
What's Changed
- fix(napi): delete reference should be after global custom gc by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1433
- feat(napi): refactor ThreadsafeFunction to allow get return value of it by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1427
- fix(napi): should correctly return while dropping Buffer by @iheyunfei (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1434
New Contributors
- @iheyunfei (opens in a new tab) made their first contribution in https://github.com/napi-rs/napi-rs/pull/1434
Full Changelog: https://github.com/napi-rs/napi-rs/compare/@napi-rs/cli@2.14.2...napi@2.10.6
napi@2.10.5
1/9/2023
What's Changed
- build(deps): bump json5 from 1.0.1 to 1.0.2 by @dependabot (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1418
- Fix ArrayBuffer memory leak by @overlookmotel (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1420
- test(napi): remove outdated assersion by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1423
- fix(napi): array buffer drop notify never be called in cloned buffer by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1424
New Contributors
- @overlookmotel (opens in a new tab) made their first contribution in https://github.com/napi-rs/napi-rs/pull/1420
Full Changelog: https://github.com/napi-rs/napi-rs/compare/napi@2.10.4...napi@2.10.5
napi@2.10.4
12/29/2022
What's Changed
- test: Memory leak reproduction for futures by @SevInf (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1413
- Fix promise leak by @xiaopengli89 (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1403
- Delete reference by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1414
New Contributors
- @SevInf (opens in a new tab) made their first contribution in https://github.com/napi-rs/napi-rs/pull/1413
- @xiaopengli89 (opens in a new tab) made their first contribution in https://github.com/napi-rs/napi-rs/pull/1403
Full Changelog: https://github.com/napi-rs/napi-rs/compare/@napi-rs/cli@2.14.0...napi@2.10.4
napi@2.10.3
12/19/2022
What's Changed
- fix(napi): napi_create_async_work incorrect argument (napi-rs#1392) by @patrickpilch (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1396
- chore(napi): add "run_script" for "Env" by @F001 (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1393
- chore(napi): reduce Mutex usage while loading addon by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1395
- fix(napi): add back custom gc for Send Buffer by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1399
New Contributors
- @patrickpilch (opens in a new tab) made their first contribution in https://github.com/napi-rs/napi-rs/pull/1396
- @F001 (opens in a new tab) made their first contribution in https://github.com/napi-rs/napi-rs/pull/1393
Full Changelog: https://github.com/napi-rs/napi-rs/compare/napi@2.10.2...napi@2.10.3
napi-derive@2.9.3
12/9/2022
What's Changed
- Better error message for
#[napi(catch_unwind)]
by @simonvandel (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1383
Full Changelog: https://github.com/napi-rs/napi-rs/compare/napi-derive@2.9.2...napi-derive@2.9.3
napi@2.10.2
12/8/2022
What's Changed
- build: focal->jammy, llvm-14->llvm-15 by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1369
- Fixes #1338 use after free with async, and fixes #1340 by @Xaeroxe (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1339
- chore(napi-derive): make_ref tweaks by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1371
- Bug fix: typed arrays ref shouldn't use offset. by @nihohit (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1376
New Contributors
- @nihohit (opens in a new tab) made their first contribution in https://github.com/napi-rs/napi-rs/pull/1376
Full Changelog: https://github.com/napi-rs/napi-rs/compare/@napi-rs/cli@2.13.0...napi@2.10.2
napi@2.10.1
11/12/2022
What's Changed
- fix(napi): BigInt::get_u64 lossless check by @Wodann (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1348
- chore(napi): including type message in error message by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1350
- fix(cli): support help command by @ahaoboy (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1355
- ci: test for zig 0.10 by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1360
New Contributors
- @ahaoboy (opens in a new tab) made their first contribution in https://github.com/napi-rs/napi-rs/pull/1355
- @SASUKE40 (opens in a new tab) made their first contribution in https://github.com/napi-rs/napi-rs/pull/1364
Full Changelog: https://github.com/napi-rs/napi-rs/compare/napi@2.10.0...napi@2.10.1
napi@2.10.0
10/4/2022
What's Changed
- chore: fix ci for forked repo by @h-a-n-a (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1302
- ci: add arm64 macOS CI by @messense (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1313
- fix(napi): improve error propagation by @devongovett (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1303
- fix(napi): propagation error in function call by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1315
- feat(napi): add
impl<T: Into<Vec<u8>>> From<T> for Uint8Array
by @usrtax (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1317 - feat(napi): add support for
Vec<(std::string::String, u16)>
and some other small change by @usrtax (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1320 - fix(napi): should also delete the reference while dropping the Buffer by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1331
- fix(napi): make Buffer/ArrayBuffer truely Send/Sync safe by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1332
- feat(napi): add threadsafe deferred values by @devongovett (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1306
New Contributors
- @Wodann (opens in a new tab) made their first contribution in https://github.com/napi-rs/napi-rs/pull/1300
- @usrtax (opens in a new tab) made their first contribution in https://github.com/napi-rs/napi-rs/pull/1317
Full Changelog: https://github.com/napi-rs/napi-rs/compare/napi@2.9.1...napi@2.10.0
napi@2.9.1
9/8/2022
What's Changed
- fix(napi): remove previous reference if value_ref existed by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1290
- fix(napi): some of the unsoundness in
Buffer
by @seritools (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1294
New Contributors
- @mat-if (opens in a new tab) made their first contribution in https://github.com/napi-rs/napi-rs/pull/1293
Full Changelog: https://github.com/napi-rs/napi-rs/compare/napi@2.9.0...napi@2.9.1
napi-derive@2.9.1
9/8/2022
What's Changed
- fix(napi-derive): should not generate this types for Constructor/Getter/Setter by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1291
- pin minimum usable version of dependency syn by @mat-if (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1293
New Contributors
- @mat-if (opens in a new tab) made their first contribution in https://github.com/napi-rs/napi-rs/pull/1293
Full Changelog: https://github.com/napi-rs/napi-rs/compare/napi@2.9.0...napi-derive@2.9.1
napi@2.9.0
8/23/2022
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
- fix(napi): segfault when
ThreadsafeFunction
's callback closure captures data by @messense (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1281 - feat(napi): implement as_object and validate for ClassInstance by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1284
- feat(napi): implement as_unknown and validate for Either types by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1285
- feat(napi): implement ToNapiValue for f32 by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1286
New Contributors
- @JSerFeng (opens in a new tab) made their first contribution in https://github.com/napi-rs/napi-rs/pull/1278
Full Changelog: https://github.com/napi-rs/napi-rs/compare/napi-derive@2.8.0...napi@2.9.0
napi-derive@2.9.0
8/23/2022
What's Changed
- fix(napi-derive): fix macro expansion naming shadow by @JSerFeng (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1278
- feat(napi-derive): catch_unwind attribute by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1280
New Contributors
- @JSerFeng (opens in a new tab) made their first contribution in https://github.com/napi-rs/napi-rs/pull/1278
Full Changelog: https://github.com/napi-rs/napi-rs/compare/napi-derive@2.8.0...napi-derive@2.9.0
napi@2.8.0
8/17/2022
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
- feat(napi): allow implement custom finalize logic for Class by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1270
- feat(napi): add get and get_mut method on
WeakReference
by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1274 - feat(napi):
error_anyhow
feature by @i-user-link (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1275
New Contributors
- @i-user-link (opens in a new tab) made their first contribution in https://github.com/napi-rs/napi-rs/pull/1275
Full Changelog: https://github.com/napi-rs/napi-rs/compare/napi@2.7.0...napi@2.8.0
napi-derive@2.8.0
8/17/2022
What's Changed
- feat(napi-derive): implement instance_of for Class by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1272
- feat(napi-derive): support inject
This<Value>
into raw function by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1273
Full Changelog: https://github.com/napi-rs/napi-rs/compare/napi-derive@2.7.0...napi-derive@2.8.0
napi@2.7.0
8/7/2022
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
- feat(napi): add some useful derived traits for the Null type by @Xaeroxe (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1241
- ci: fix self testing for Android platforms by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1248
- feat(napi): add derived traits to
ThreadsafeFunctionCallMode
by @seritools (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1243 - feat(napi): Call sync functions within tokio runtime by @Xaeroxe (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1242
- fix(napi): either for #[napi(object)] types by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1258
New Contributors
- @amrbashir (opens in a new tab) made their first contribution in https://github.com/napi-rs/napi-rs/pull/1251
- @seritools (opens in a new tab) made their first contribution in https://github.com/napi-rs/napi-rs/pull/1243
napi-derive@2.7.0
8/7/2022
What's Changed
- feat(napi-derive): support set property attribute in napi macro by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1257
- feat(napi-derive): Support
#[napi(strict)]
on&T
and&mut T
by @Hywan (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1238
napi@2.6.3
7/11/2022
What's Changed
- fix(napi): use Mutex instead of Atomic in ThreadsafeFunction by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1234
- fix(napi): memory leak in Buffer/ArrayBuffer by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1235
Full Changelog: https://github.com/napi-rs/napi-rs/compare/napi@2.6.2...napi@2.6.3
napi@2.6.2
7/8/2022
What's Changed
- fix(napi)
undefined
is recognized as a validNone
forOption<T>
by @Hywan (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1228 - fix(napi): memory leak in ThreadsafeFunction by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1230
Full Changelog: https://github.com/napi-rs/napi-rs/compare/napi@2.6.1...napi@2.6.2
napi@2.6.1
7/6/2022
What's Changed
- fix(napi): drop buffer inner data only when Reference count is 0 by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1227
Full Changelog: https://github.com/napi-rs/napi-rs/compare/napi-derive@2.6.0...napi@2.6.1
napi@2.6.0
7/6/2022
What's Changed
- chore(napi): replace lazy_static with once_cell by @jplatte (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1213
- feat(napi) Implement
Either3
toEither26
. by @Hywan (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1200 - fix(napi): validate fn for
Option<T>
by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1223 - feat(napi): clone reference for TypedArray/Buffer by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1224
New Contributors
- @jplatte (opens in a new tab) made their first contribution in https://github.com/napi-rs/napi-rs/pull/1213
Full Changelog: https://github.com/napi-rs/napi-rs/compare/@napi-rs/cli@2.10.0...@napi-rs/cli@2.10.1
napi-derive@2.6.0
7/6/2022
What's Changed
- feat(napi-derive): support return_if_invalid by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1219
- feat(napi-derive): allow injecting this in class method by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1221
Full Changelog: https://github.com/napi-rs/napi-rs/compare/napi-derive@2.5.0...napi-derive@2.6.0
napi@2.5.0
6/10/2022
What's Changed
- Add
rust-version
field toCargo.toml
s by @messense (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1190 - feat(napi) Implement
ToNapiValue
for 8 and 16-bits numbers by @Hywan (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1195 - feat(napi) implement
From<Infallible>
fornapi::Error
by @sunli829 (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1197 - feat(napi) Extend #1195 to
Vec<T>
. by @Hywan (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1198 - feat(napi): support into_instance in class struct by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1201
New Contributors
- @Hywan (opens in a new tab) made their first contribution in https://github.com/napi-rs/napi-rs/pull/1195
- @sunli829 (opens in a new tab) made their first contribution in https://github.com/napi-rs/napi-rs/pull/1197
Full Changelog: https://github.com/napi-rs/napi-rs/compare/napi@2.4.3...napi@2.5.0
napi-derive@2.5.0
6/10/2022
What's Changed
- Add
rust-version
field toCargo.toml
s by @messense (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1190 - feat(napi-derive): add ts_arg_type attribute to override individual args on functions by @jose-acevedoflores (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1192
Full Changelog: https://github.com/napi-rs/napi-rs/compare/napi-derive@2.4.1...napi-derive@2.5.0
napi-build@2.0.1
6/10/2022
What's Changed
- fix: android builds broken by actions default ndk version bump by @forehalo (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1207
Full Changelog: https://github.com/napi-rs/napi-rs/compare/napi-build@2.0.0...napi-build@2.0.1
napi@2.4.3
5/12/2022
What's Changed
- fix(napi): type constraint for either types by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1178
Full Changelog: https://github.com/napi-rs/napi-rs/compare/napi@2.4.2...napi@2.4.3
napi@2.4.2
5/10/2022
What's Changed
- refactor(napi): remove
thread_local!
usage by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1176 - test(napi): add electron test by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1176
- fix(napi): missing iterator implementation from class factory by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1177
Experimental
- feat(napi): add support for weak references by @devongovett (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1172
Full Changelog: https://github.com/napi-rs/napi-rs/compare/napi@2.4.1...napi@2.4.2
napi-sys@2.2.2
5/10/2022
What's Changed
- fix(napi-sys): don't panic if
napi_*
is failed to load by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1176
Full Changelog: https://github.com/napi-rs/napi-rs/compare/napi-sys@2.2.1...napi-sys@2.2.2
napi@2.4.1
5/7/2022
What's Changed
- fix(napi): load Node-API symbols manually on Windows by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1169
Full Changelog: https://github.com/napi-rs/napi-rs/compare/napi-sys@2.2.0...napi@2.4.1
napi@2.4.0
5/7/2022
What's Changed
- feat(napi): make Error::from_reason() generic by @bnoordhuis (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1159
- fix(napi): crash when process exits due to reference by @devongovett (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1161
- feat(napi):
Iterator
Support by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1166 - refactor(napi): use
libloading
on Windows by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1167
New Contributors
- @bnoordhuis (opens in a new tab) made their first contribution in https://github.com/napi-rs/napi-rs/pull/1159
- @devongovett (opens in a new tab) made their first contribution in https://github.com/napi-rs/napi-rs/pull/1161
Full Changelog: https://github.com/napi-rs/napi-rs/compare/napi-derive@2.3.3...napi@2.4.0
napi-sys@2.2.1
5/7/2022
What's Changed
- fix(napi-sys): load Node-API symbols manually on Windows by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1169
Full Changelog: https://github.com/napi-rs/napi-rs/compare/napi-sys@2.2.0...napi-sys@2.2.1
napi-sys@2.2.0
5/7/2022
What's Changed
- Use
libloading
on Windows to avoid Windows i686 compiling issues and reduce crate size by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1167
Full Changelog: https://github.com/napi-rs/napi-rs/compare/napi-derive@2.3.3...napi-sys@2.2.0
napi-build@2.0.0
5/7/2022
What's Changed
BREAKING
- Use
libloading
on Windows, removenode-{arch}.lib
files innapi-build
by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1167
You must use napi 2.4+
with the napi-build@2
Full Changelog: https://github.com/napi-rs/napi-rs/compare/napi-derive@2.3.3...napi-build@2.0.0
napi@2.3.3
4/27/2022
What's Changed
- fix(napi): use create_buffer/arrary_buffer if provided data is empty by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1147
- chore: upgrade dependencies by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1151
- chore: drop node 17, add node 18 by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1152
- chore(napi): upgrade windows crate by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1153
Experimental
- feat(napi): support return Reference on class instance by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1150
Full Changelog: https://github.com/napi-rs/napi-rs/compare/napi@2.3.2...napi@2.3.3
napi-derive@2.3.3
4/27/2022
What's Changed
- fix(napi-derive): invalid TypeScript return type by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1155
Full Changelog: https://github.com/napi-rs/napi-rs/compare/napi-derive@2.3.2...napi-derive@2.3.3
napi-derive@2.3.2
4/27/2022
What's Changed
- fix(napi-derive): should transform struct case on Impl by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1148
Full Changelog: https://github.com/napi-rs/napi-rs/compare/napi-derive@2.3.1...napi-derive@2.3.2
napi@2.3.2
4/25/2022
What's Changed
- fix(napi): use null pointer for empty buffers by @mischnic (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1144
Full Changelog: https://github.com/napi-rs/napi-rs/compare/napi@2.3.1...napi@2.3.2
napi-derive@2.3.1
4/25/2022
What's Changed
- Add build option to not include the header in dts file by @ceifa (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1140
- Simplify the optional values in generated declaration file by @ceifa (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1141
- Add u64 to BigInt conversion through From trait by @ceifa (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1143
New Contributors
- @ceifa (opens in a new tab) made their first contribution in https://github.com/napi-rs/napi-rs/pull/1140
Full Changelog: https://github.com/napi-rs/napi-rs/compare/napi@2.3.1...napi-derive@2.3.1
napi@2.3.1
4/15/2022
What's Changed
- fix(napi):
cannot find trait ToNapiValue
error by @U-C-S (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1136 - ci: check all napi features by @Brooooooklyn (opens in a new tab) in https://github.com/napi-rs/napi-rs/pull/1137
New Contributors
- @U-C-S (opens in a new tab) made their first contribution in https://github.com/napi-rs/napi-rs/pull/1136
Full Changelog: https://github.com/napi-rs/napi-rs/compare/napi@2.3.0...napi@2.3.1
napi@2.3.0
4/15/2022
What's Changed
- docs: add return Promise callback function @Brooooooklyn (opens in a new tab)
- feat(napi): relax the value type on ThreadSafeFunction @Brooooooklyn (opens in a new tab)
- feat(napi-derive): support generic types on fn @Brooooooklyn (opens in a new tab)
- fix(napi-derive): JsUnknown should be unknown type @Brooooooklyn (opens in a new tab)
- feat(napi): add
from_unknown
method on FromNapiValue @Brooooooklyn (opens in a new tab) - fix(napi): typo unrwap -> unwrap @waltonseymour
- chore(napi): show tips if create different buffers with same data @Brooooooklyn (opens in a new tab)
- test(napi): add memory tests for
Reference
@Brooooooklyn (opens in a new tab) - fix(napi): make buffer
Send
&Sync
safe @Brooooooklyn (opens in a new tab) - feat(napi): redesign the Reference API (#1118) @Brooooooklyn (opens in a new tab)
- Revert "chore: unset CARGO_BUILD_TARGET in debian.Dockerfile" @Brooooooklyn (opens in a new tab)
- chore: unset
CARGO_BUILD_TARGET
in debian.Dockerfile @Brooooooklyn (opens in a new tab) - build: migrate debian image to messense/manylinux2014-cross @Brooooooklyn (opens in a new tab)
- feat(napi): experimental class reference API @Brooooooklyn (opens in a new tab)
- fix(napi): remove CString::from_vec_with_nul_unchecked @Brooooooklyn (opens in a new tab)
- feat(napi): allow empty enums @antoniomuso
- ci: add binary build check @messense (opens in a new tab)
- fix(napi): double allocation in create_buffer @Brooooooklyn (opens in a new tab)
Reference API
use napi::bindgen_prelude::*;
pub struct Repository {
dir: String,
}
impl Repository {
fn remote(&self) -> Remote {
Remote { inner: self }
}
}
pub struct Remote<'repo> {
inner: &'repo Repository,
}
impl<'repo> Remote<'repo> {
fn name(&self) -> String {
"origin".to_owned()
}
}
#[napi]
pub struct JsRepo {
inner: Repository,
}
#[napi]
impl JsRepo {
#[napi(constructor)]
pub fn new(dir: String) -> Self {
JsRepo {
inner: Repository { dir },
}
}
#[napi]
pub fn remote(&self, reference: Reference<JsRepo>, env: Env) -> Result<JsRemote> {
Ok(JsRemote {
inner: reference.share_with(env, |repo| Ok(repo.inner.remote()))?,
})
}
}
#[napi]
pub struct JsRemote {
inner: SharedReference<JsRepo, Remote<'static>>,
}
#[napi]
impl JsRemote {
#[napi]
pub fn name(&self) -> String {
self.inner.name()
}
}
const repo = new JsRepo('.')
t.is(repo.remote().name(), 'origin')
New Contributors
- @waltonseymour (opens in a new tab) made their first contribution in https://github.com/napi-rs/napi-rs/pull/1133
- @antoniomuso (opens in a new tab) made their first contribution in https://github.com/napi-rs/napi-rs/pull/1089
Full Changelog: https://github.com/napi-rs/napi-rs/compare/@napi-rs/cli@2.2.0...napi@2.3.0