* refactor(napi): reduce boilerplate code for accessing `aborted` lock
* refactor: ensure that `napi_call_threadsafe_function` cannot be called after abort
---------
Co-authored-by: LongYinan <lynweklm@gmail.com>
And recreate it if it does not exist.
In windows, electron renderer process will crash if:
1. Import some NAPI module that enable `tokio-rt` flag in renderer process.
2. Reload the page.
3. Call a function imported from that NAPI module.
Because the tokio runtime will be dropped when reloading the page, and won't create again, but currently we assume that the runtime must exist in tokio-based `within_runtime_if_available`.
This will cause some panic like this:
```
thread '<unnamed>' panicked at 'called `Option::unwrap()` on a `None` value', napi-rs\crates\napi\src\tokio_runtime.rs:72:42
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
Error: Renderer process crashed: crashed, exitCode: -529697949
at EventEmitter.<anonymous> (napi-rs\examples\napi\electron.js:33:9)
at EventEmitter.emit (node:events:525:35)
```
* fix(napi): big numbers losing precision on serde_json::Value
* fix(napi): add feature flags for bigint on number conversion
* chore(napi): change MAX_SAFE_INT to constant and convert big numbers to string when bigint is not available
* chore(napi): change how the check for safe integer range is made
https://github.com/napi-rs/napi-rs/issues/1470
serde_json::Value parses positive integers to u64 types by default. When converting serde_json::Value to a js value, those numbers are converted to BigInts, even if they are within the bounds of the number primitive. The added checks converts an u64 to an u32 if possible to prevent this behavior.
Co-authored-by: m1212e <->
* add "run_script" for "Env"
* Apply suggestions from code review
use `AsRef<str>` instead of `&str`
Co-authored-by: LongYinan <lynweklm@gmail.com>
* use `AsRef<str>` instead of `&str`
Co-authored-by: LongYinan <lynweklm@gmail.com>
The third argument to `napi_create_async_work` (async_resource_name)
should be a `napi_value` that corresponds to a null-terminated utf-8
string.
This resolves errors running Next.js with GraalVM.
oracle/graal#5582oracle/graal#5581
Notice from the n-api docs that the data returned from
`napi_get_typedarray_info` is already adjusted by the byte offset.
https://nodejs.org/api/n-api.html#napi_get_typedarray_info
This means that when `as_ref`/`as_mut` apply the byte offset, the
offset is in practice applied twice.
This wasn't caught in tests because no test tried to modify a typed
array with a byte offset, and the test didn't us the typed array
structs, only `JsTypedArray`. If you want, I can modify the rest of the
functions in examples/napi-compt-mode/src/arraybuffers.rs
and the matching tests, to test all typed arrays.
IMO the `byte_offset` field can be removed entirely from the struct,
but I wanted to submit a minimal PR.
we need this to be able to safely drop an struct,
that makes use of an async task.
the drop function needs to be able to call `.abort()`
on the join handle to avoid memory corruption and undefined bahviour.
* fix leaked napi refcount in `Buffer` when cloning
Cloning unconditionally increased the refcount in `Buffer::clone`, but only called `napi_reference_unref` on dropping the last Buffer (the one with `strong_count == 1`). This means that the refcount will never drop back to zero after cloning, leaking the Buffer.
This commit changes it to also unconditionally unref the buffer.
* fix multiple sources of UB in `Buffer`
- `slice::from_raw_parts` may never be created with a null pointer, but `napi_get_buffer_info` was not sufficiently checked → UB when passing an empty Buffer
- `&'static mut [u8],` is invalid, as it certainly doesn't live for `'static`
Switching to `NonNull<u8>` and a `len` field fixes both of these.
- I also don't really understand how the `impl ToNapiValue for &mut Buffer` could have been sound. It creates an entirely new `Arc`, but reuses the same `Vec` allocation, leading to... a double free of the `Vec` on drop? I have replaced it with a simple call to `clone` instead.
* remove overcomplicated bool and drop impl
As far as I can tell, by just removing the bool and letting the drop code do its thing we clean up correctly in all cases. Because `napi_create_external_buffer` gets an owned `Buffer` attached to it via the Box, we can rely on `from_raw` retrieving it in the `drop_buffer` function.