Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ce99054

Browse filesBrowse files
authored
fix(napi): module_exports binding (#2632)
1 parent 8cd752d commit ce99054
Copy full SHA for ce99054

File tree

Expand file treeCollapse file tree

12 files changed

+42
-26
lines changed
Filter options
Expand file treeCollapse file tree

12 files changed

+42
-26
lines changed

‎cli/src/api/build.ts

Copy file name to clipboardExpand all lines: cli/src/api/build.ts
+9-5Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -940,11 +940,14 @@ class Builder {
940940
const workerPath = join(dir, 'wasi-worker.mjs')
941941
const browserWorkerPath = join(dir, 'wasi-worker-browser.mjs')
942942
const browserEntryPath = join(dir, 'browser.js')
943-
const exportsCode = idents
944-
.map(
945-
(ident) => `module.exports.${ident} = __napiModule.exports.${ident}`,
946-
)
947-
.join('\n')
943+
const exportsCode =
944+
`module.exports = __napiModule.exports\n` +
945+
idents
946+
.map(
947+
(ident) =>
948+
`module.exports.${ident} = __napiModule.exports.${ident}`,
949+
)
950+
.join('\n')
948951
await writeFileAsync(
949952
bindingPath,
950953
createWasiBinding(
@@ -966,6 +969,7 @@ class Builder {
966969
this.config.wasm?.browser?.fs,
967970
this.config.wasm?.browser?.asyncInit,
968971
) +
972+
`export default __napiModule.exports\n` +
969973
idents
970974
.map(
971975
(ident) =>

‎crates/napi/src/bindgen_runtime/js_values/symbol.rs

Copy file name to clipboardExpand all lines: crates/napi/src/bindgen_runtime/js_values/symbol.rs
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ impl Symbol {
4040
}
4141

4242
#[cfg(feature = "napi9")]
43-
pub fn for_desc(desc: String) -> Self {
43+
pub fn for_desc<S: AsRef<str>>(desc: S) -> Self {
4444
Self {
4545
desc: None,
46-
for_desc: Some(desc.to_owned()),
46+
for_desc: Some(desc.as_ref().to_owned()),
4747
}
4848
}
4949
}

‎crates/napi/src/bindgen_runtime/module_register.rs

Copy file name to clipboardExpand all lines: crates/napi/src/bindgen_runtime/module_register.rs
+12-14Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,6 @@ impl<K, V> Default for PersistedPerInstanceHashMap<K, V> {
6060
type ModuleRegisterCallback =
6161
RwLock<Vec<(Option<&'static str>, (&'static str, ExportRegisterCallback))>>;
6262

63-
#[cfg(not(feature = "noop"))]
64-
type ModuleRegisterHookCallback = PersistedPerInstanceHashMap<ThreadId, ExportRegisterHookCallback>;
65-
6663
#[cfg(not(feature = "noop"))]
6764
type ModuleClassProperty =
6865
PersistedPerInstanceHashMap<TypeId, HashMap<Option<&'static str>, (&'static str, Vec<Property>)>>;
@@ -77,7 +74,7 @@ type RegisteredClassesMap = PersistedPerInstanceHashMap<ThreadId, RegisteredClas
7774
#[cfg(not(feature = "noop"))]
7875
static MODULE_REGISTER_CALLBACK: LazyLock<ModuleRegisterCallback> = LazyLock::new(Default::default);
7976
#[cfg(not(feature = "noop"))]
80-
static MODULE_REGISTER_HOOK_CALLBACK: LazyLock<ModuleRegisterHookCallback> =
77+
static MODULE_REGISTER_HOOK_CALLBACK: LazyLock<RwLock<Option<ExportRegisterHookCallback>>> =
8178
LazyLock::new(Default::default);
8279
#[cfg(not(feature = "noop"))]
8380
static MODULE_CLASS_PROPERTIES: LazyLock<ModuleClassProperty> = LazyLock::new(Default::default);
@@ -152,10 +149,10 @@ pub fn register_module_export(
152149
#[cfg(not(feature = "noop"))]
153150
#[doc(hidden)]
154151
pub fn register_module_export_hook(cb: ExportRegisterHookCallback) {
155-
let current_id = std::thread::current().id();
156-
MODULE_REGISTER_HOOK_CALLBACK.borrow_mut(|inner| {
157-
inner.insert(current_id, cb);
158-
});
152+
let mut inner = MODULE_REGISTER_HOOK_CALLBACK
153+
.write()
154+
.expect("Write MODULE_REGISTER_HOOK_CALLBACK failed");
155+
*inner = Some(cb);
159156
}
160157

161158
#[cfg(feature = "noop")]
@@ -465,13 +462,14 @@ pub unsafe extern "C" fn napi_register_module_v1(
465462
)
466463
});
467464

468-
MODULE_REGISTER_HOOK_CALLBACK.borrow_mut(|inner| {
469-
if let Some(cb) = inner.get(&current_thread_id) {
470-
if let Err(e) = cb(env, exports) {
471-
JsError::from(e).throw_into(env);
472-
}
465+
let module_register_hook_callback = MODULE_REGISTER_HOOK_CALLBACK
466+
.read()
467+
.expect("Read MODULE_REGISTER_HOOK_CALLBACK failed");
468+
if let Some(cb) = module_register_hook_callback.as_ref() {
469+
if let Err(e) = cb(env, exports) {
470+
JsError::from(e).throw_into(env);
473471
}
474-
});
472+
}
475473

476474
#[cfg(feature = "compat-mode")]
477475
{

‎examples/napi/__tests__/__snapshots__/values.spec.ts.md

Copy file name to clipboardExpand all lines: examples/napi/__tests__/__snapshots__/values.spec.ts.md
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ Generated by [AVA](https://avajs.dev).
4141
4242
type MaybePromise<T> = T | Promise<T>␊
4343
44+
export declare const NAPI_RS_SYMBOL: symbol␊
45+
4446
export declare class ExternalObject<T> {␊
4547
readonly '': {␊
4648
readonly '': unique symbol␊
Binary file not shown.

‎examples/napi/__tests__/values.spec.ts

Copy file name to clipboardExpand all lines: examples/napi/__tests__/values.spec.ts
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,8 @@ import {
233233
shutdownRuntime,
234234
callAsyncWithUnknownReturnValue,
235235
} from '../index.cjs'
236+
// import other stuff in `#[napi(module_exports)]`
237+
import nativeAddon from '../index.cjs'
236238

237239
import { test } from './test.framework.js'
238240

@@ -1634,3 +1636,7 @@ test('extends javascript error', (t) => {
16341636
t.true(typeof e.nativeStackTrace === 'string')
16351637
}
16361638
})
1639+
1640+
test('module exports', (t) => {
1641+
t.is(nativeAddon.NAPI_RS_SYMBOL, Symbol.for('NAPI_RS_SYMBOL'))
1642+
})

‎examples/napi/dts-header.d.ts

Copy file name to clipboardExpand all lines: examples/napi/dts-header.d.ts
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
/* eslint-disable */
33

44
type MaybePromise<T> = T | Promise<T>
5+
6+
export declare const NAPI_RS_SYMBOL: symbol

‎examples/napi/example.wasi-browser.js

Copy file name to clipboardExpand all lines: examples/napi/example.wasi-browser.js
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ const {
6060
}
6161
},
6262
})
63+
export default __napiModule.exports
6364
export const Animal = __napiModule.exports.Animal
6465
export const AnimalWithDefaultConstructor = __napiModule.exports.AnimalWithDefaultConstructor
6566
export const AnotherClassForEither = __napiModule.exports.AnotherClassForEither

‎examples/napi/example.wasi.cjs

Copy file name to clipboardExpand all lines: examples/napi/example.wasi.cjs
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ const { instance: __napiInstance, module: __wasiModule, napiModule: __napiModule
8484
}
8585
},
8686
})
87+
module.exports = __napiModule.exports
8788
module.exports.Animal = __napiModule.exports.Animal
8889
module.exports.AnimalWithDefaultConstructor = __napiModule.exports.AnimalWithDefaultConstructor
8990
module.exports.AnotherClassForEither = __napiModule.exports.AnotherClassForEither

‎examples/napi/index.d.cts

Copy file name to clipboardExpand all lines: examples/napi/index.d.cts
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
type MaybePromise<T> = T | Promise<T>
55

6+
export declare const NAPI_RS_SYMBOL: symbol
7+
68
export declare class ExternalObject<T> {
79
readonly '': {
810
readonly '': unique symbol

‎examples/napi/package.json

Copy file name to clipboardExpand all lines: examples/napi/package.json
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
"scripts": {
99
"browser": "vite",
1010
"build": "napi-raw build --platform --js index.cjs --dts index.d.cts",
11-
"test": "cross-env TS_NODE_PROJECT=./tsconfig.json node --import @oxc-node/core/register ../../node_modules/ava/entrypoints/cli.mjs",
12-
"test-js": "ava"
11+
"test": "ava reset-cache && cross-env TS_NODE_PROJECT=./tsconfig.json node --import @oxc-node/core/register ../../node_modules/ava/entrypoints/cli.mjs",
12+
"test-js": "ava reset-cache && ava"
1313
},
1414
"napi": {
1515
"binaryName": "example",

‎examples/napi/src/lib.rs

Copy file name to clipboardExpand all lines: examples/napi/src/lib.rs
+3-3Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#[cfg(not(target_family = "wasm"))]
99
use napi::bindgen_prelude::create_custom_tokio_runtime;
1010
use napi::{
11-
bindgen_prelude::{Env, Object, Result},
11+
bindgen_prelude::{Object, Result, Symbol},
1212
JsObjectValue,
1313
};
1414

@@ -53,8 +53,8 @@ pub fn shutdown_runtime() {
5353
}
5454

5555
#[napi(module_exports)]
56-
pub fn exports(env: Env, mut export: Object) -> Result<()> {
57-
let symbol = env.create_symbol(Some("NAPI_RS_SYMBOL"))?;
56+
pub fn exports(mut export: Object) -> Result<()> {
57+
let symbol = Symbol::for_desc("NAPI_RS_SYMBOL");
5858
export.set_named_property("NAPI_RS_SYMBOL", symbol)?;
5959
Ok(())
6060
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.