Commit graph

36 commits

Author SHA1 Message Date
LongYinan
cc937e1dba
feat(cli): allow control the wasm Memory options via napi config (#2038) 2024-04-15 23:19:02 +08:00
LongYinan
6a29446dfb
chore(wasm-runtime): upgrade emnapi (#2008) 2024-03-21 14:41:05 +08:00
LongYinan
4719caa643
feat(napi): support Return generic of ThreadsafeFunction (#1997)
* feat(napi): support to use tuple with either (#1993)

`Either` uses `ValidateNapiValue` + `TypeName` to validate and report error on value not being matched. So there's no way to remove these super traits from it. So I implemented these types to `Tuple` types.

* feat(napi): support `Return` generic of ThreadsafeFunction

* depracate JsFunction

* CalleeHandled tsfn should handle Result in callback

* Pass env to call_with_return_value callback

* Fix compile

* clippy fix

* Fix electron test

* Function args

---------

Co-authored-by: Hana <andywangsy@gmail.com>
2024-03-20 21:37:08 +08:00
Caleb Everett
95dd6ef485
refactor(cli): refactor js-binding to support easier bundling. (#1957)
* Refactor js-binding to support bundling single-package artifacts

The existsSync check breaks the bundlers I've tested (esbuild, webpack,
ncc), so you cannot bundle napi-rs packages that have all binarys in a
single package.

I've tested this change with both single package and multi package
libraries.

* Update snapshots

* Update CI

* Fix electron test

---------

Co-authored-by: Caleb ツ Everett <calebev@amazon.com>
Co-authored-by: LongYinan <lynweklm@gmail.com>
2024-02-26 15:46:07 +08:00
LongYinan
2fda360742
chore: fix renovate path and dedupe electron versions (#1974) 2024-02-25 00:50:22 +08:00
LongYinan
0007e3aaf6
chore: upgrade all dependencies (#1933) 2024-02-01 12:14:56 +08:00
LongYinan
120accd965
feat: add wasm runtime package (#1904) 2024-01-16 23:28:40 +08:00
LongYinan
78de67e08f
chore: bump memfs-browser (#1900) 2024-01-16 11:10:55 +08:00
LongYinan
fc3d5cbcff
fix(cli): add browser entry (#1899) 2024-01-10 11:18:13 +08:00
LongYinan
7d3b53d41d
feat(cli): support generate browser compatible codes (#1891) 2024-01-08 21:02:46 +08:00
LongYinan
b0ba466f95
fix(cli): also load wasm file from packages (#1876) 2023-12-30 00:43:38 +08:00
liuyi
e32c105a26
fix(napi-derive): upgrade syn (#1849) 2023-12-13 12:40:01 +08:00
LongYinan
db89395966
chore: upgrade all npm dependencies (#1843) 2023-12-06 19:41:21 +08:00
LongYinan
8c911b5d34
chore: upgrade emnapi dependencies (#1817) 2023-11-19 15:13:06 +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
2e03db1fec
test: refactor the example tests to esm, add bun:test => ava polyfill (#1730) 2023-09-20 01:18:01 -07:00
LongYinan
6d62b3f714
chore: upgrade dependencies (#1657) 2023-07-17 14:56:02 +08:00
LongYinan
d9ff0b4ddf
fix(napi): do nothing in deferred if thread is destroyed (#1568) 2023-04-15 18:58:53 +08:00
LongYinan
1d78f6c294
chore: upgrade npm dependencies (#1557) 2023-04-11 10:47:52 +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
LongYinan
62b16d6a89
chore: upgrade npm dependencies (#1443) 2023-01-19 00:32:52 +08:00
LongYinan
28be7e256b
chore(cli): upgrade Node.js dependencies (#1368) 2022-11-20 22:59:35 +08:00
LongYinan
1037e6f14d
chore: upgrade dependencies (#1349) 2022-10-23 23:03:18 +08:00
LongYinan
184c4af588
chore: upgrade npm dependencies 2022-08-23 20:18:25 +08:00
LongYinan
cf6b2c9241
chore: upgrade dependencies 2022-04-27 00:57:57 +08:00
LongYinan
11967dec79
chore: upgrade dependencies 2022-04-01 12:10:56 +08:00
LongYinan
5005aaa5af
chore: upgrade dependencies 2022-03-13 18:30:11 +08:00
LongYinan
1531489e18
chore: upgrade to yarn3 2022-01-24 17:25:40 +08:00
LongYinan
4406059de1
fix(napi): addon packages become undefined in worker_threads 2021-12-21 23:38:35 +08:00
LongYinan
91d07810a2
fix(napi): ThreadsafeFunction with ErrorStrategy::Fatal should throw fatal exception 2021-12-14 12:47:23 +08:00
LongYinan
179f20a7c5
feat(cli): generate js binding to avoid dynamic require logic 2021-11-09 21:43:51 +08:00
forehalo
c5cebec812 test full features once for napi v2 2021-10-27 14:42:57 +08:00
LongYinan
0e8de173a4 feat(cli): dts flag for build command 2021-10-01 15:01:23 +08:00
forehalo
2467b7139b
Introduce #[napi] procedural macro to automation development boilerplate (#696)
* napi procedural macro for basic rust/JavaScript types
* introduce the `compat-mode` for `napi` and `napi-derive` crates for backward compatible
* remove #[inline] and let compiler to decide the inline behavior
* cli now can produce the `.d.ts` file for native binding
* many tests and example for the new procedural macro

Co-authored-by: LongYinan <lynweklm@gmail.com>
2021-09-23 01:29:09 +08:00