Commit graph

209 commits

Author SHA1 Message Date
LongYinan
938f4df83d
fix(napi-derive): async task void output type (#1795) 2023-11-08 20:05:09 +08:00
LongYinan
36581336c6
feat(napi): pass the rest of async tests (#1792)
Pass the rest of async tests, including await the JavaScript Promise in the Rust side, and the worker_threads tests.
2023-11-07 01:46:43 +08:00
LongYinan
feabcd7f16
test(napi): add tests for napi9 features (#1784) 2023-11-06 11:03:44 +08:00
LongYinan
546b108a5b
feat(napi): support async class factory (#1779)
- Close https://github.com/napi-rs/napi-rs/issues/1777
2023-11-06 10:58:23 +08:00
LongYinan
0dc1ef738b fix(napi): asan caught memory safety issue 2023-11-04 15:26:07 +08:00
LongYinan
7a3cd77bc8
ci: add YARN_IGNORE_NODE on macOS arm64 build 2023-11-02 20:32:36 +08:00
LongYinan
13d0ce075e
feat: integrate with emnapi (#1669)
* Integrate with emnapi

* resolve conflict

* ignore wasm

* generate wasi file

* Add wasi test to workflow

* Fix wasi template

* emnapi new initialize api

* Finish test

* Purne tsconfig

* Generate wasi worker

* Fix electron test

* Finalize check

* Noop adjust_external_memory

* Apply cr suggestions
2023-11-02 12:57:11 +08:00
LongYinan
69c0223b9b
test: reduce memory usage while testing aginst worker thread (#1769) 2023-11-02 00:44:08 +08:00
LongYinan
2e03db1fec
test: refactor the example tests to esm, add bun:test => ava polyfill (#1730) 2023-09-20 01:18:01 -07:00
Tom Sherman
3418fd3e8f
feat(napi,sys): implement Symbol.for (#1721)
Co-authored-by: LongYinan <lynweklm@gmail.com>
2023-09-13 15:45:14 -07:00
LongYinan
05b4be4d80
style: clippy fix (#1711) 2023-08-30 16:41:13 +08:00
yoogo
4259e85e0e fix(backend): attribute of a struct marked as #[napi(constructor)] contain Rust keywords, causing to throw is not a valid identifier when generating getters and setters 2023-08-17 20:48:54 +08:00
LongYinan
a7eeb0c31c
fix(napi): promise resolve error (#1664) 2023-07-24 00:36:24 +08:00
LongYinan
6d62b3f714
chore: upgrade dependencies (#1657) 2023-07-17 14:56:02 +08:00
Markus
73a704a19e
feat(napi): keep stack traces in a deferred context (#1637)
* feat(napi): keep stack traces in deferred context

* chore: reformat code

Signed-off-by: Markus <28785953+MarkusJx@users.noreply.github.com>

* chore: use napi wrappers

Signed-off-by: Markus <28785953+MarkusJx@users.noreply.github.com>

* test(napi): add test for deferred trace

Signed-off-by: Markus <28785953+MarkusJx@users.noreply.github.com>

* chore: fix format

Signed-off-by: Markus <28785953+MarkusJx@users.noreply.github.com>

---------

Signed-off-by: Markus <28785953+MarkusJx@users.noreply.github.com>
2023-07-15 12:07:14 +08:00
LongYinan
53cf696cf8
ci: reduce the complex of CI config (#1628) 2023-06-17 17:03:57 +08:00
LongYinan
c6258cf633
feat(napi): support chrono::NaiveDateTime (#1601) 2023-05-26 18:28:34 +08:00
LongYinan
5bc098144c
chore(example): add callback return promise (#1590) 2023-05-16 11:05:37 +08:00
LongYinan
d9c191d270
test(napi): skip worker tests in slow Docker env (#1569) 2023-04-16 23:59:15 +08:00
LongYinan
d9ff0b4ddf
fix(napi): do nothing in deferred if thread is destroyed (#1568) 2023-04-15 18:58:53 +08:00
LongYinan
d14fdca242
fix(napi): thread safe issue while creating class instance (#1561) 2023-04-15 15:37:01 +08:00
LongYinan
1d78f6c294
chore: upgrade npm dependencies (#1557) 2023-04-11 10:47:52 +08:00
LongYinan
88773a7a8e
fix(napi): re-throw error in ThreadsafeFunction callback if we could 2023-04-10 17:02:13 +08:00
Alexey Orlenko
2d1e4144b3
fix: prevent crashing when napi_register_module_v1 is called twice (#1554)
* fix: prevent crashing when napi_register_module_v1 is called twice

Currently napi-rs addons can lead to the Node.js process aborting with
the following error when initialising the addon on Windows:

```
c:\ws\src\cleanup_queue-inl.h:32: Assertion `(insertion_info.second)
== (true)' failed.
```

This happens because `napi_add_env_cleanup_hook` must not be called
with the same arguments multiple times unless the previously scheduled
cleanup hook with the same arguments was already executed. However,
the cleanup hook added by `napi_register_module_v1` in napi-rs on
Windows was always created with `ptr::null_mut()` as an argument.

One case where this causes a problem is when using the addon from
multiple contexts (e.g. Node.js worker threads) at the same
time. However, Node.js doesn't provide any guarantees that the N-API
addon initialisation code will run only once even per thread and
context. In fact, it's totally valid to run `process.dlopen()`
multiple times from JavaScript land in Node.js, and this will lead to
the initialisation code being run multiple times as different
`exports` objects may need to be populated. This may happen in
numerous cases, e.g.:

- When it's not possible or not desirable to use `require()` and users
  must resort to using `process.dlopen()` (one use case is passing
  non-default flags to `dlopen(3)`, another is ES modules). Caching
  the results of `process.dlopen()` to avoid running it more than once
  may not always be possible reliably in all cases (for example,
  because of Jest sandbox).

- When the `require` cache is cleared.

- On Windows: `require("./addon.node")` and then
  `require(path.toNamespacedPath("./addon.node"))`.

Another issue is fixed inside `napi::tokio_runtime::drop_runtime`:
there's no need to call `napi_remove_env_cleanup_hook` (it's only
useful to cancel the hooks that haven't been executed yet). Null
pointer retrieved from `arg` was being passed as the `env` argument of
that function, so it didn't do anything and just returned
`napi_invalid_arg`.

This patch makes `napi_register_module_v1` use a counter as the
cleanup hook argument, so that the value is always different. An
alternative might have been to use a higher-level abstraction around
`sys::napi_env_cleanup_hook` that would take ownership of a boxed
closure, if there is something like this in the API already. Another
alternative could have been to heap-allocate a value so that we would
have a unique valid memory address.

The patch also contains a minor code cleanup related to
`RT_REFERENCE_COUNT` along the way: the counter is encapsulated inside
its module and `ensure_runtime` takes care of incrementing it, and
less strict memory ordering is now used as there's no need for
`SeqCst` here. If desired, it can be further optimised to
`Ordering::Release` and a separate acquire fence inside the if
statement in `drop_runtime`, as `AcqRel` for every decrement is also a
bit stricter than necessary (although simpler). These changes are not
necessary to fix the issue and can be extracted to a separate patch.

At first it was tempting to use the loaded value of
`RT_REFERENCE_COUNT` as the argument for the cleanup hook but it would
have been wrong: a simple counterexample is the following sequence:

1. init in the first context (queue: 0)
2. init in the second context (queue: 0, 1)
3. destroy the first context (queue: 1)
4. init in the third context (queue: 1, 1)

* test(napi): unload test was excluded unexpected

---------

Co-authored-by: LongYinan <lynweklm@gmail.com>
2023-04-08 23:08:48 +08:00
forehal
a781a4f27e feat(cli): brand new cli tool with both cli and programmatical usage (#1492)
BREAKING CHANGE: requires node >= 16 and some cli options have been renamed
2023-04-06 11:04:53 +08:00
Francesco Benedetto
7c4dc2a2bd
feat(napi-derive-backend, napi-derive): add support for string enums (#1551) 2023-04-03 14:10:58 +08:00
Bo
0a0aa36c28
test(napi): update test case for checking electron renderer crash (#1547)
(cherry picked from commit d22598dbb1082de8ac712de954cd5616c838a48d)
2023-03-29 12:53:57 +08:00
Bo
e47c13f177
fix(napi): check if the tokio runtime exists when registering the module
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)
```
2023-03-28 12:03:00 +08:00
Gabriel Francisco
3983be23f5
fix(napi): big numbers losing precision on serde_json::Value (#1538)
* 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
2023-03-23 13:34:34 +08:00
LongYinan
3d48d4464b
fix(cli,napi-derive): re-export types from shared crate (#1531)
* fix(cli,napi-derive): re-export types from shared crate

* chore: publish

 - @napi-rs/cli@2.15.1-alpha.0

* Clippy fix

* Fix memory testing
2023-03-21 18:12:52 +08:00
Victor Teo
aee742f185
feat(napi): property getter and setter with closure (#1526)
* getter with closure with segment fault

* fix getter closure pointer

* add setter

* Cleanup API

* Add test for `create_function_from_closure`

* Fix compile error

* Fix flaky test title

---------

Co-authored-by: LongYinan <lynweklm@gmail.com>
2023-03-21 11:22:07 +08:00
Francesco Benedetto
c8bd8924e2
fix(cli): export non const enums when generating typedefs (#1527)
* fix(cli): export non const enums when generating typedefs

* Make --const-enum as a build flag

---------

Co-authored-by: LongYinan <lynweklm@gmail.com>
2023-03-20 14:19:18 +08:00
LongYinan
d255a0a575
chore: decrease timeout in tsfn test 2023-03-20 11:59:53 +08:00
Bo
a6e1ff471c
fix(napi): use weak arc for passing thread_finalize_data (#1525)
* fix(napi): use weak arc for passing thread_finalize_data

* fix: try to fix test of tsfn_return_promise_timeout
2023-03-20 11:56:54 +08:00
Alberto Pose
ffc4980d52
fix(napi): panic when Promise callbacks trigger after Promise is dropped (#1469) (#1516)
Co-authored-by: Alberto Pose <albepose@amazon.com>
2023-03-14 15:32:17 +08:00
LongYinan
3bd3c9cc08
test(napi): tests for custom gc in worker_threads (#1505) 2023-03-05 16:51:06 +08:00
LongYinan
8e3eb6204b
fix(napi): support custom status in Error (#1486) 2023-02-09 23:18:57 +08:00
Hana
90cc0a6abe
feat(napi): convert ToNapiValue tuple to variadic tsfn (#1475)
* refactor: convert ToNapiValue tuple to variadic tsfn

* chore: resolve conflicts

* fix: typo

* chore: use into instead of to

* chore: syntax compat
2023-02-08 22:30:43 +08:00
LongYinan
7613d669fb
chore(napi): enhance the error messages while converting types failed (#1473) 2023-02-06 00:52:59 +08:00
LongYinan
3bd2bf40b1
fix(napi): run_script return type (#1467) 2023-01-31 20:36:59 +08:00
LongYinan
e9de5681be
fix(napi): also apply electron external data fallback to lowlevel APIs (#1458)
* fix(napi): also apply electron external data fallback to lowlevel APIs

* chore: allow uninlined_format_args in tests
2023-01-28 21:31:57 +08:00
LongYinan
548f288722
fix(napi): fallback to copy arraybuffer if zero copy is not allowed (#1455) 2023-01-24 22:39:46 +08:00
LongYinan
e3adf5dac4
fix(napi): unhandled promise rejection while using EitherN<Promise<..>> (#1452) 2023-01-24 19:07:33 +08:00
LongYinan
c8352a1fb0
feat(napi-derive): allow partial implement From/To Napivalue for Object (#1448) 2023-01-24 14:51:16 +08:00
LongYinan
e79eb34118
feat(napi-derive): generate ThreadsafeFunction types (#1449) 2023-01-24 14:25:05 +08:00
LongYinan
62b16d6a89
chore: upgrade npm dependencies (#1443) 2023-01-19 00:32:52 +08:00
LongYinan
6739ddda20
fix(napi): remove useless aquire while creating ThreadsafeFunction (#1442) 2023-01-18 11:20:47 +08:00
LongYinan
46f08ee6dd
fix(napi): missing From implementation for Bigint (#1440) 2023-01-17 00:05:19 +08:00
Hana
78b6e1574a
fix(napi-derive): fix union type generation for ts function notation (#1439)
* fix(backend): fix union type generation for ts function notation

* chore: update snapshot

* fix: naming
2023-01-14 18:27:46 +08:00
LongYinan
dc3a4c9f25
feat(napi): refactor ThreadsafeFunction to allow get return value of it (#1427) 2023-01-11 18:54:45 +08:00