Commit graph

44 commits

Author SHA1 Message Date
翠 / green
923b82aee9
feat(napi): allow &External to be created from napi value (#2037) 2024-04-15 17:54:10 +08:00
LongYinan
e274cf7ae6
feat(napi-derive): enhance the error messages in object validator (#2034) 2024-04-10 16:52:23 +08:00
LongYinan
67f03a7fd4
ci: fix flaky tests (#2033)
* ci: fix flaky tests

* chore: remove useless deps

* chore: decresse worker_threads counts
2024-04-10 15:43:05 +08:00
LongYinan
0550c56fcf
fix(napi): External should impl FromNapiRef rather than FromNapiValue (#2013)
- Close https://github.com/napi-rs/napi-rs/issues/1994
2024-03-25 15:11:11 +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
Hana
97746b79a9
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.
2024-03-13 13:29:06 +08:00
Andrew Toth
6b1058a268
feat(napi-derive): add optional enum_string case conversion (#1995) 2024-03-11 20:10:00 +08:00
LongYinan
d962e34d3a
fix(napi): remove useless FromNapiValue bound check for ValidateNapiValue (#1999) 2024-03-10 21:22:46 +08:00
LongYinan
aeb0b4766d
fix(napi): add back the typecheck logic that was accidentally removed in Object::get_named_property (#1982)
- Close https://github.com/napi-rs/napi-rs/issues/1641
2024-02-28 14:29:02 +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
117e4ce153
fix(napi-derive): wrong dependency condition (#1980) 2024-02-25 15:25:42 +08:00
LongYinan
8ca1967bd8
feat(napi): impl BufferSlice and Uint8ClampedSlice (#1979) 2024-02-25 01:00:28 +08:00
LongYinan
3e187151a6
fix(napi-derive): HashMap/IndexMap and slice generated types (#1977) 2024-02-25 00:56:30 +08:00
ebe97257a6
feat(napi-derive): add use_nullable attribute (#1971)
* feat(napi-derive): add use_nullable attribute

Co-authored-by: naskya <m@naskya.net>

* chore(napi-derive): update tests

Co-authored-by: naskya <m@naskya.net>

---------

Co-authored-by: naskya <m@naskya.net>
2024-02-24 21:49:54 +08:00
Louis
43415251b8
feat(napi): allow Reference as a class method param (#1966)
As of before this commit, there was a lock in the codegen preventing Reference
from being used as a function argument outside of a Reference<Self>.

This changes it, allowing Reference of any class to be added as a class method
argument anywhere. It has the same limitations as reference, as in it requires
the class to have been created with a factory or constructor. This change
implements FromNapiValue on Reference, which will unwrap the class and call the
existing from_value_ptr method. It also updated typegen so that we only emit
the reference type if we're in an impl block that doesn't match the Reference
we're getting. This ensures that typegen works as expected with the previous
behaviour.
2024-02-22 22:37:50 +08:00
LongYinan
5ac153388f
feat(napi): accept slice as TypedArray (#1951) 2024-02-15 23:40:45 +08:00
LongYinan
d7dc4dc5a2
feat(napi): extends the Map types interoperability (#1950) 2024-02-15 15:43:21 +08:00
LongYinan
f69771e2d4
fix(napi-derive): JsArrayBuffer generated type 2024-01-26 14:32:24 +08:00
LongYinan
5be7ab0f6b
feat(napi): new Function/FunctionRef API 2024-01-26 02:27:46 +00:00
LongYinan
120accd965
feat: add wasm runtime package (#1904) 2024-01-16 23:28:40 +08:00
LongYinan
7d3b53d41d
feat(cli): support generate browser compatible codes (#1891) 2024-01-08 21:02:46 +08:00
LongYinan
02dd4c3fd3
fix(napi-derive): catch_unwind on constructor (#1869)
- Close https://github.com/napi-rs/napi-rs/issues/1852
2023-12-27 12:49:10 +08:00
LongYinan
440bea29f8
test(napi): decrease the worker sizes on Linux x64 musl platform 2023-11-20 23:07:43 +08:00
LongYinan
8c911b5d34
chore: upgrade emnapi dependencies (#1817) 2023-11-19 15:13:06 +08:00
LongYinan
e930a6aab3
fix(napi-derive): async task optional output type (#1796) 2023-11-08 20:06:27 +08:00
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
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
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
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
d14fdca242
fix(napi): thread safe issue while creating class instance (#1561) 2023-04-15 15:37:01 +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