Skip to content

Commit ac3d967

Browse files
committed
onEmnapiInitialized
1 parent b0b3752 commit ac3d967

File tree

18 files changed

+581
-495
lines changed

18 files changed

+581
-495
lines changed

README.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,25 @@ emcc -O3 \
9292
hello.c
9393
```
9494

95-
Use the output js in html. The default export key is `emnapiExports` on [`Module`](https://emscripten.org/docs/api_reference/module.html) object. You can change the key by predefining `NODE_GYP_MODULE_NAME`.
95+
Use the output js in html. The default export key is `emnapiExports` on [`Module`](https://emscripten.org/docs/api_reference/module.html) object. You can change the key by predefining `NODE_GYP_MODULE_NAME`. `onEmnapiInitialized` will be called before `onRuntimeInitialized`.
9696

9797
```html
9898
<script src="hello.js"></script>
9999
<script>
100100
// Emscripten js glue code will create a global `Module` object
101+
Module.onEmnapiInitialized = function (err, emnapiExports) {
102+
if (err) {
103+
// error handling
104+
// emnapiExports === undefined
105+
// Module[NODE_GYP_MODULE_NAME] === undefined
106+
console.error(err);
107+
} else {
108+
// emnapiExports === Module[NODE_GYP_MODULE_NAME]
109+
}
110+
};
111+
101112
Module.onRuntimeInitialized = function () {
113+
if (!('emnapiExports' in Module)) return;
102114
var binding = Module.emnapiExports;
103115
var msg = 'hello ' + binding.hello();
104116
window.alert(msg);
@@ -111,7 +123,12 @@ Or in Node.js.
111123
```js
112124
const Module = require('./hello.js')
113125

126+
Module.onEmnapiInitialized = function (err, emnapiExports) {
127+
// ...
128+
}
129+
114130
Module.onRuntimeInitialized = function () {
131+
if (!('emnapiExports' in Module)) return
115132
const binding = Module.emnapiExports
116133
const msg = `hello ${binding.hello()}`
117134
console.log(msg)
@@ -214,7 +231,7 @@ Full example codes can be found [here](https://github.com/toyobayashi/emnapi/tre
214231

215232
Output code can run in recent version modern browsers and Node.js latest LTS. IE is not supported.
216233

217-
If a JS error is thrown on runtime initialization, Node.js process will exit. You can use `-sNODEJS_CATCH_EXIT=0` and add `ununcaughtException` handler yourself to avoid this.
234+
If a JS error is thrown on runtime initialization, Node.js process will exit. You can use `-sNODEJS_CATCH_EXIT=0` and add `ununcaughtException` handler yourself to avoid this. Alternatively, you can use `Module.onEmnapiInitialized` callback to catch error.
218235

219236
## Building
220237

README_CN.md

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,25 @@ emcc -O3 \
8888
hello.c
8989
```
9090

91-
把输出的 JS 引入进 HTML 使用,默认导出在 [`Module`](https://emscripten.org/docs/api_reference/module.html) 对象上的 `emnapiExports`。可通过预定义 `NODE_GYP_MODULE_NAME` 修改默认的导出键值。
91+
把输出的 JS 引入进 HTML 使用,默认导出在 [`Module`](https://emscripten.org/docs/api_reference/module.html) 对象上的 `emnapiExports`。可通过预定义 `NODE_GYP_MODULE_NAME` 修改默认的导出键值。`onEmnapiInitialized` 将在 `onRuntimeInitialized` 之前被调用。
9292

9393
```html
9494
<script src="hello.js"></script>
9595
<script>
9696
// Emscripten js 胶水代码会在全局创建一个 `Module` 对象
97+
Module.onEmnapiInitialized = function (err, emnapiExports) {
98+
if (err) {
99+
// 错误处理
100+
// emnapiExports === undefined
101+
// Module[NODE_GYP_MODULE_NAME] === undefined
102+
console.error(err);
103+
} else {
104+
// emnapiExports === Module[NODE_GYP_MODULE_NAME]
105+
}
106+
};
107+
97108
Module.onRuntimeInitialized = function () {
109+
if (!('emnapiExports' in Module)) return;
98110
var binding = Module.emnapiExports;
99111
var msg = 'hello ' + binding.hello();
100112
window.alert(msg);
@@ -107,7 +119,12 @@ Module.onRuntimeInitialized = function () {
107119
```js
108120
const Module = require('./hello.js')
109121

122+
Module.onEmnapiInitialized = function (err, emnapiExports) {
123+
// ...
124+
}
125+
110126
Module.onRuntimeInitialized = function () {
127+
if (!('emnapiExports' in Module)) return
111128
const binding = Module.emnapiExports
112129
const msg = `hello ${binding.hello()}`
113130
console.log(msg)
@@ -211,7 +228,7 @@ cd ..
211228

212229
输出的代码可以运行在最近版本的现代浏览器和最新的 Node.js LTS 版本。不支持 IE。
213230

214-
如果在运行时初始化时抛出 JS 错误,Node.js 进程将会退出。可以使用`-sNODEJS_CATCH_EXIT=0` 并自己添加`uncaughtException` 事件处理器来避免这种情况
231+
如果在运行时初始化时抛出 JS 错误,Node.js 进程将会退出。可以使用`-sNODEJS_CATCH_EXIT=0` 并自己添加`uncaughtException`。或者可以使用 `Module.onEmnapiInitialized` 来捕获异常
215232

216233
## 构建
217234

cgen.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module.exports = function (_options, { isDebug, isEmscripten }) {
1313
// '-sERROR_ON_UNDEFINED_SYMBOLS=0', if add js function to imports.env
1414
"-sEXPORTED_FUNCTIONS=['_malloc','_free']",
1515
'-sALLOW_MEMORY_GROWTH=1',
16-
'-sNODEJS_CATCH_EXIT=0',
16+
// '-sNODEJS_CATCH_EXIT=0',
1717
...(isDebug ? debugFlags : [])
1818
]
1919
: []

example/index.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,19 @@
66
Module = require('./build/hello.js');
77
}
88

9+
Module.onEmnapiInitialized = function (err, emnapiExports) {
10+
if (err) {
11+
console.error(err);
12+
} else {
13+
console.log('onEmnapiInitialized', emnapiExports === Module.emnapiExports);
14+
}
15+
};
16+
917
Module.onRuntimeInitialized = function () {
18+
console.log('onRuntimeInitialized');
19+
if (!('emnapiExports' in Module)) {
20+
return;
21+
}
1022
var binding = Module.emnapiExports;
1123
var msg = 'hello ' + binding.hello();
1224
if (typeof window !== 'undefined') {

lib/emnapi.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ function emnapi_create_external_uint8array (
4141
const u8arr = new Uint8Array(HEAPU8.buffer, external_data, byte_length)
4242
const handle = envObject.getCurrentScope().add(u8arr)
4343
if (finalize_cb !== emnapi.NULL) {
44-
const status = emnapi.wrap(emnapi.WrapType.anonymous, env, handle.id, external_data, finalize_cb, finalize_hint, emnapi.NULL)
44+
const status = emnapiWrap(emnapi.WrapType.anonymous, env, handle.id, external_data, finalize_cb, finalize_hint, emnapi.NULL)
4545
if (status === emnapi.napi_status.napi_pending_exception) {
4646
throw envObject.tryCatch.extractException()
4747
} else if (status !== emnapi.napi_status.napi_ok) {
@@ -56,4 +56,4 @@ function emnapi_create_external_uint8array (
5656

5757
emnapiImplement('emnapi_get_module_object', emnapi_get_module_object)
5858
emnapiImplement('emnapi_get_module_property', emnapi_get_module_property)
59-
emnapiImplement('emnapi_create_external_uint8array', emnapi_create_external_uint8array)
59+
emnapiImplement('emnapi_create_external_uint8array', emnapi_create_external_uint8array, ['$emnapiWrap'])

lib/function.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
function napi_create_function (env: napi_env, utf8name: Pointer<const_char>, length: size_t, cb: napi_callback, data: void_p, result: Pointer<napi_value>): emnapi.napi_status {
22
return emnapi.preamble(env, (envObject) => {
33
return emnapi.checkArgs(env, [result, cb], () => {
4-
const f = emnapi.createFunction(env, utf8name, length, cb, data)
4+
const f = emnapiCreateFunction(env, utf8name, length, cb, data)
55
const valueHandle = envObject.getCurrentScope().add(f)
66
HEAP32[result >> 2] = valueHandle.id
77
return emnapi.getReturnStatus(env)
@@ -132,7 +132,7 @@ function napi_get_new_target (
132132
})
133133
}
134134

135-
emnapiImplement('napi_create_function', napi_create_function)
135+
emnapiImplement('napi_create_function', napi_create_function, ['$emnapiCreateFunction'])
136136
emnapiImplement('napi_get_cb_info', napi_get_cb_info)
137137
emnapiImplement('napi_call_function', napi_call_function)
138138
emnapiImplement('napi_new_instance', napi_new_instance)

0 commit comments

Comments
 (0)