Commit graph

2565 commits

Author SHA1 Message Date
LongYinan
baf0db2f19
feat(napi): new Function/FunctionRef API (#1913)
This is the experimental feature that provides a all new design `Function` API in NAPI-RS.
The main motivation to design a new `Function` instead of improving the old `JsFunction` is there are some fundamental problems in the old `JsFunction` API.
1. The old `JsFunction` doesn't contains a lifetime, which means you can send it to a outlive scope and call it later, which would cause a `napi_invalid_arg` error in the underlying `napi_call_function` API. This design issue also happens in the `JsObject`/`JsBuffer` and all other non-primitive types APIs.
2. It's not possible to generate correct TypeScript type definitions for the old `JsFunction` API.
3. The arguments of the old `JsFunction` API must be the same type, which is makes it really unfriendly to use.

Expect that, we also have a high level and modern Function exists in the `NAPI-RS` which is the Generic type style `Fn(Args) -> Return`.
This API is pretty nice to use, and more importantly, it's sound.
But there are some limitations to use it, like create a reference to it to outlive the scope of the JavaScript function under the hood. And you can't use it create a `ThreadsafeFunction`.
So there is the new design `Function` API, there are some core features:
1. It's a generic typed API, which means you can get more accurate Rust type information and generate correct TypeScript type definitions.
2. It's sound, which means you can't send it to a outlive scope and call it later, if you want do that, you must create a reference to it or create a `ThreadsafeFunction`.
3. It's friendly to use, you can use different types of arguments and return types, and it can covert the Rust tuple type to JavaScript arguments automatically.
Here is some examples to show how to use it:
```rust
use napi::bindgen_prelude::*;
use napi_derive::napi;

#[napi]
pub fn callback_javascript_callback(add_one: Function<u32, u32>) -> Result<u32> {
  add_one.call(100)
}
```
⬇️⬇️⬇️
```typescript
export function callbackJavascriptCallback(add_one: (arg0: number) => number): number;
```
⬇️⬇️⬇️
```javascript
callbackJavascriptCallback((arg0) => arg0 + 1);
// 101
```
If you define a tuple as the `Function` arguments, it will be converted to JavaScript arguments automatically.
```rust
use napi::bindgen_prelude::*;
use napi_derive::napi;

#[napi]
pub fn callback_javascript_callback(add: Function<(u32, u32), u32>) -> Result<u32> {
  add.call((100, 200))
}
```
⬇️⬇️⬇️
```typescript
export function callbackJavascriptCallback(add: (arg0: number, arg1: number) => number): number;
```
⬇️⬇️⬇️
```javascript
callbackJavascriptCallback((arg0, arg1) => arg0 + arg1);
// 300
```
If you are trying to send it into a outlive scope, you will get a compile error.
For example, if you are trying to send a callback to `git2-rs` `RemoteCallbacks::credentials` API:
```rust
use napi::bindgen_prelude::*;
use napi_derive::napi;

#[napi]
pub fn build_credential(on_credential: Function<(String, Option<String>, CredentialType), ClassInstance<Cred>>) -> Result<()> {
 let mut callbacks = git2::RemoteCallbacks::new();
 callbacks.credentials(move |url, username_from_url, allowed_types| {
   on_credential.call((url.to_string(), username_from_url.map(|s| s.to_string()), allowed_types.into()))
    .map(...)
    .map_error(...)
 });
}
```
You will get a compile error:
```text
error[E0597]: `on_credential` does not live long enough
```
To fix this issue, you can create a reference to it:
```rust
use napi::bindgen_prelude::*;
use napi_derive::napi;
#[napi]
pub fn build_credential(env: Env. on_credential: Function<(String, Option<String>, CredentialType), ClassInstance<Cred>>) -> Result<()> {
  let mut callbacks = git2::RemoteCallbacks::new();
  let on_credential_ref = on_credential.create_ref()?;
  callbacks.credentials(move |url, username_from_url, allowed_types| {
    let on_credential = on_credential_ref.borrow_back(&env)?;
    on_credential.call((url.to_string(), username_from_url.map(|s| s.to_string()), allowed_types.into()))
    .map(...)
    .map_error(...)
  });
}
```
2024-01-26 10:58:30 +08:00
LongYinan
fecd0d8049
Update crates/napi/src/bindgen_runtime/js_values/function.rs 2024-01-26 02:27:46 +00:00
LongYinan
5be7ab0f6b
feat(napi): new Function/FunctionRef API 2024-01-26 02:27:46 +00:00
LongYinan
134707ef1d
fix(napi): callback in execute_tokio_future does not need to be Send (#1917)
The resolver does not need to be `Send` or `Sync`, because it's assumed to be called from the same thread that the JavaScript thread is running on.
2024-01-26 10:27:17 +08:00
renovate[bot]
80f37eeb6a
chore(deps): update dependency husky to v9 (#1914)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2024-01-25 16:06:58 +08:00
LongYinan
69d2a75384
Release independent packages
- napi@2.14.4
2024-01-24 17:20:46 +08:00
LongYinan
e332270698
fix(napi): future in block_on do not need to be send 2024-01-24 17:20:31 +08:00
LongYinan
3cbae4e8c1
Release independent packages
- napi@2.14.3
2024-01-24 17:16:41 +08:00
LongYinan
38568c2693
chore(napi): expose spawn_blocking on tokio runtime (#1912) 2024-01-24 17:15:36 +08:00
LongYinan
b4345d1375
fix(napi): block_on type (#1911) 2024-01-24 17:13:42 +08:00
咲奈Sakina
57bcee590b
fix(wasm-runtime): wasm compatible with core-js (#1909) 2024-01-20 15:24:16 +08:00
dependabot[bot]
e595f99266
chore(deps-dev): bump vite from 5.0.11 to 5.0.12 (#1910) 2024-01-20 09:00:53 +08:00
renovate[bot]
84701ef879
chore(deps): update actions/cache action to v4 (#1908)
[![Mend Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [actions/cache](https://togithub.com/actions/cache) | action | major | `v3` -> `v4` |

---

### Release Notes

<details>
<summary>actions/cache (actions/cache)</summary>

### [`v4`](https://togithub.com/actions/cache/compare/v3...v4)

[Compare Source](https://togithub.com/actions/cache/compare/v3...v4)

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Mend Renovate](https://www.mend.io/free-developer-tools/renovate/). View repository job log [here](https://developer.mend.io/github/napi-rs/napi-rs).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4xMzUuMCIsInVwZGF0ZWRJblZlciI6IjM3LjEzNS4wIiwidGFyZ2V0QnJhbmNoIjoibWFpbiJ9-->
2024-01-18 10:35:59 +08:00
LongYinan
5b9f86fea0
chore(release): publish
- @napi-rs/cli@3.0.0-alpha.36
 - @napi-rs/wasm-runtime@0.1.1
2024-01-17 00:59:12 +08:00
LongYinan
44dc39f1f0
fix(cli,wasm-runtime): dependencies (#1905) 2024-01-17 00:57:29 +08:00
LongYinan
a439e2c3e6
chore(release): publish
- @napi-rs/cli@3.0.0-alpha.35
 - @napi-rs/wasm-runtime@0.1.0
2024-01-16 23:58:23 +08:00
LongYinan
120accd965
feat: add wasm runtime package (#1904) 2024-01-16 23:28:40 +08:00
Milton Moura
ddeaf30f14
feat(cli): Add support for s390x linux arch in js bindings template (#1901)
Signed-off-by: Milton Moura <miltonmoura@gmail.com>
2024-01-16 11:18:05 +08:00
LongYinan
78de67e08f
chore: bump memfs-browser (#1900) 2024-01-16 11:10:55 +08:00
LongYinan
c2b6c18031
chore(release): publish
- @napi-rs/cli@3.0.0-alpha.34
2024-01-10 13:29:47 +08:00
LongYinan
fc3d5cbcff
fix(cli): add browser entry (#1899) 2024-01-10 11:18:13 +08:00
LongYinan
0a70d6ffd7
chore(release): publish
- @napi-rs/cli@3.0.0-alpha.33
2024-01-09 01:06:03 +08:00
LongYinan
1676930728
fix(cli): artifacts wasi worker name (#1895) 2024-01-09 01:05:19 +08:00
LongYinan
a6934ab041
chore: fix generated .d.ts file 2024-01-09 00:58:41 +08:00
LongYinan
2de358aaa6
chore(release): publish
- @napi-rs/cli@3.0.0-alpha.32
2024-01-09 00:36:01 +08:00
LongYinan
9b8dab6b63
fix(cli): missing files in created wasi package (#1894) 2024-01-09 00:34:59 +08:00
LongYinan
ffbaba3a9b
chore(release): publish
- @napi-rs/cli@3.0.0-alpha.31
2024-01-08 21:05:45 +08:00
LongYinan
7d3b53d41d
feat(cli): support generate browser compatible codes (#1891) 2024-01-08 21:02:46 +08:00
renovate[bot]
ecde07f8c1
fix(deps): update dependency @tybys/wasm-util to v0.8.1 (#1892)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2024-01-07 19:17:45 +00:00
renovate[bot]
19df3c1ea2
chore(deps): update dependency c8 to v9 (#1889)
[![Mend Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Change | Age | Adoption | Passing | Confidence |
|---|---|---|---|---|---|
| [c8](https://togithub.com/bcoe/c8) | [`^8.0.1` -> `^9.0.0`](https://renovatebot.com/diffs/npm/c8/8.0.1/9.0.0) | [![age](https://developer.mend.io/api/mc/badges/age/npm/c8/9.0.0?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://developer.mend.io/api/mc/badges/adoption/npm/c8/9.0.0?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://developer.mend.io/api/mc/badges/compatibility/npm/c8/8.0.1/9.0.0?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://developer.mend.io/api/mc/badges/confidence/npm/c8/8.0.1/9.0.0?slim=true)](https://docs.renovatebot.com/merge-confidence/) |

---

### Release Notes

<details>
<summary>bcoe/c8 (c8)</summary>

### [`v9.0.0`](https://togithub.com/bcoe/c8/blob/HEAD/CHANGELOG.md#900-2024-01-03)

[Compare Source](https://togithub.com/bcoe/c8/compare/v8.0.1...v9.0.0)

##### ⚠ BREAKING CHANGES

-   **build:** minimum Node.js version is now 14.14.0

##### Features

-   **build:** minimum Node.js version is now 14.14.0 ([2cdc86b](2cdc86bd0a))
-   **deps:** update foreground-child to promise API ([#&#8203;512](https://togithub.com/bcoe/c8/issues/512)) ([b46b640](b46b640127))
-   **deps:** use Node.js built in rm ([2cdc86b](2cdc86bd0a))

</details>

---

### Configuration

📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update again.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Mend Renovate](https://www.mend.io/free-developer-tools/renovate/). View repository job log [here](https://developer.mend.io/github/napi-rs/napi-rs).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4xMDMuMSIsInVwZGF0ZWRJblZlciI6IjM3LjEwMy4xIiwidGFyZ2V0QnJhbmNoIjoibWFpbiJ9-->
2024-01-04 11:49:50 +08:00
LongYinan
1392954c32
chore(release): publish
- @napi-rs/cli@3.0.0-alpha.30
2024-01-03 19:31:17 +08:00
LongYinan
3889d8ad17
fix(cli): upload to github releases issue (#1888) 2024-01-03 19:29:33 +08:00
LongYinan
57463554e9
fix(cli): wasi fallback package load logic (#1887) 2024-01-03 18:53:09 +08:00
renovate[bot]
5080fb28a2
chore(deps): lock file maintenance (#1882)
[![Mend Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Update | Change |
|---|---|
| lockFileMaintenance | All locks refreshed |

🔧 This Pull Request updates lock files to use the latest dependency versions.

---

### Configuration

📅 **Schedule**: Branch creation - "before 4am on the first day of the month" (UTC), Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox.

👻 **Immortal**: This PR will be recreated if closed unmerged. Get [config help](https://togithub.com/renovatebot/renovate/discussions) if that's undesired.

---

 - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box

---

This PR has been generated by [Mend Renovate](https://www.mend.io/free-developer-tools/renovate/). View repository job log [here](https://developer.mend.io/github/napi-rs/napi-rs).
<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNy4xMDMuMSIsInVwZGF0ZWRJblZlciI6IjM3LjEwMy4xIiwidGFyZ2V0QnJhbmNoIjoibWFpbiJ9-->
2024-01-02 15:23:26 +08:00
LongYinan
8e3e688c28
chore(release): publish
- @napi-rs/cli@3.0.0-alpha.29
2024-01-02 14:52:25 +08:00
Ranger
4d82737efe
chore: fix async_work status typo (#1883) 2024-01-02 13:23:45 +08:00
LongYinan
c73cb31c11
fix(cli): missing wasm files in artifacts command (#1884) 2024-01-02 12:33:13 +08:00
LongYinan
03eb476cef
Release independent packages
- napi@2.14.2
2023-12-31 22:08:11 +08:00
LongYinan
f47cc72749
chore(release): publish
- @napi-rs/cli@3.0.0-alpha.28
2023-12-31 22:05:12 +08:00
LongYinan
f29801686b
fix(cli): copy binding files into wasi packages (#1881) 2023-12-31 15:51:46 +08:00
LongYinan
65273a4631
chore(napi): add status to error messages in AsyncWork (#1880) 2023-12-30 11:56:36 +08:00
LongYinan
f2972c743f
chore(release): publish
- @napi-rs/cli@3.0.0-alpha.27
2023-12-30 00:47:09 +08:00
renovate[bot]
e175e6fbd6
fix(deps): update dependency emnapi to v0.45.0 (#1879)
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
2023-12-30 00:45:50 +08:00
LongYinan
dc79bb86d0
chore(release): publish
- @napi-rs/cli@3.0.0-alpha.26
 - @napi-rs/triples@2.0.0-alpha.7
2023-12-30 00:44:20 +08:00
LongYinan
b0ba466f95
fix(cli): also load wasm file from packages (#1876) 2023-12-30 00:43:38 +08:00
LongYinan
f62685e836
fix(cli): exclude node_modules in artifacts command (#1875) 2023-12-30 00:00:28 +08:00
LongYinan
67743b1046
fix(cli): exclude node_modules in artifacts command 2023-12-29 15:09:13 +00:00
LongYinan
43a080d52b
fix(napi): apply clippy suggestions (#1878) 2023-12-29 23:08:56 +08:00
LongYinan
958ba74aff
chore(release): publish
- @napi-rs/cli@3.0.0-alpha.25
2023-12-28 12:38:00 +08:00
LongYinan
72afe03aa4
fix(cli): compatible with napi artifacts -d option (#1872) 2023-12-28 12:16:07 +08:00