Skip to content

Commit 94e9198

Browse files
authored
[Backport] fix: env undefined after emitting beforeExit event (#162)
* fix: env undefined after emitting beforeExit event (#161) * open scope without env
1 parent 4afb0e7 commit 94e9198

File tree

6 files changed

+66
-14
lines changed

6 files changed

+66
-14
lines changed

packages/emnapi/CMakeLists.txt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ set(EMNAPI_BASIC_TARGET_NAME "emnapi-basic")
7070
set(EMNAPI_BASIC_MT_TARGET_NAME "emnapi-basic-mt")
7171
set(EMNAPI_TARGET_NAME "emnapi")
7272
set(EMNAPI_MT_TARGET_NAME "emnapi-mt")
73+
74+
set(EMNAPI_BASIC_NAPI_RS_MT_TARGET_NAME "emnapi-basic-napi-rs-mt")
75+
set(EMNAPI_NAPI_RS_MT_TARGET_NAME "emnapi-napi-rs-mt")
76+
7377
set(DLMALLOC_TARGET_NAME "dlmalloc")
7478
set(DLMALLOC_MT_TARGET_NAME "dlmalloc-mt")
7579
set(EMMALLOC_TARGET_NAME "emmalloc")
@@ -155,6 +159,31 @@ else()
155159
set(EMNAPI_BUILD_BASIC_MT OFF)
156160
endif()
157161

162+
if(IS_WASM32_WASIP1_THREADS)
163+
set(EMNAPI_BUILD_FOR_NAPI_RS ON)
164+
else()
165+
set(EMNAPI_BUILD_FOR_NAPI_RS OFF)
166+
endif()
167+
168+
if(EMNAPI_BUILD_FOR_NAPI_RS)
169+
add_library(${EMNAPI_NAPI_RS_MT_TARGET_NAME} STATIC ${EMNAPI_SRC} ${UV_SRC})
170+
target_include_directories(${EMNAPI_NAPI_RS_MT_TARGET_NAME} PUBLIC ${EMNAPI_INCLUDE})
171+
target_compile_definitions(${EMNAPI_NAPI_RS_MT_TARGET_NAME}
172+
PUBLIC ${EMNAPI_DEFINES} "NAPI_EXTERN=__attribute__((__import_module__(\"env\")))"
173+
)
174+
175+
add_library(${EMNAPI_BASIC_NAPI_RS_MT_TARGET_NAME} STATIC
176+
${ENAPI_BASIC_SRC}
177+
"${CMAKE_CURRENT_SOURCE_DIR}/src/thread/async_worker_create.c"
178+
"${CMAKE_CURRENT_SOURCE_DIR}/src/thread/async_worker_init.S"
179+
)
180+
target_include_directories(${EMNAPI_BASIC_NAPI_RS_MT_TARGET_NAME} PUBLIC ${EMNAPI_INCLUDE})
181+
target_compile_definitions(${EMNAPI_BASIC_NAPI_RS_MT_TARGET_NAME}
182+
PUBLIC ${EMNAPI_DEFINES} "NAPI_EXTERN=__attribute__((__import_module__(\"env\")))"
183+
PRIVATE "EMNAPI_DISABLE_UV"
184+
)
185+
endif()
186+
158187
if(EMNAPI_BUILD_BASIC_MT)
159188
add_library(${EMNAPI_BASIC_MT_TARGET_NAME} STATIC
160189
${ENAPI_BASIC_SRC}
@@ -222,6 +251,10 @@ if(LIB_ARCH)
222251
if(EMNAPI_BUILD_BASIC_MT)
223252
install(TARGETS ${EMNAPI_BASIC_MT_TARGET_NAME} DESTINATION "lib/${LIB_ARCH}")
224253
endif()
254+
if(EMNAPI_BUILD_FOR_NAPI_RS)
255+
install(TARGETS ${EMNAPI_NAPI_RS_MT_TARGET_NAME} DESTINATION "lib/${LIB_ARCH}")
256+
install(TARGETS ${EMNAPI_BASIC_NAPI_RS_MT_TARGET_NAME} DESTINATION "lib/${LIB_ARCH}")
257+
endif()
225258
if(IS_WASM32)
226259
install(TARGETS ${DLMALLOC_TARGET_NAME} DESTINATION "lib/${LIB_ARCH}")
227260
install(TARGETS ${DLMALLOC_MT_TARGET_NAME} DESTINATION "lib/${LIB_ARCH}")

packages/emnapi/src/emnapi_internal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ EMNAPI_INTERNAL_EXTERN void _emnapi_runtime_keepalive_pop();
9898
#define EMNAPI_KEEPALIVE_POP _emnapi_runtime_keepalive_pop
9999
#endif
100100

101+
EMNAPI_INTERNAL_EXTERN napi_handle_scope _emnapi_open_handle_scope();
102+
EMNAPI_INTERNAL_EXTERN void _emnapi_close_handle_scope(napi_handle_scope scope);
101103
EMNAPI_INTERNAL_EXTERN void _emnapi_env_ref(napi_env env);
102104
EMNAPI_INTERNAL_EXTERN void _emnapi_env_unref(napi_env env);
103105
EMNAPI_INTERNAL_EXTERN void _emnapi_ctx_increase_waiting_request_counter();

packages/emnapi/src/threadsafe_function.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ static void _emnapi_tsfn_destroy(napi_threadsafe_function func) {
136136
EMNAPI_ASSERT_CALL(napi_delete_reference(func->env, func->ref));
137137
}
138138

139+
EMNAPI_ASYNC_RESOURCE_DTOR(func->env, (emnapi_async_resource*) func);
140+
139141
EMNAPI_ASSERT_CALL(napi_remove_env_cleanup_hook(func->env, _emnapi_tsfn_cleanup, func));
140142
_emnapi_env_unref(func->env);
141143
if (func->async_ref) {
@@ -144,7 +146,6 @@ static void _emnapi_tsfn_destroy(napi_threadsafe_function func) {
144146
func->async_ref = false;
145147
}
146148

147-
EMNAPI_ASYNC_RESOURCE_DTOR(func->env, (emnapi_async_resource*) func);
148149
free(func);
149150
}
150151

@@ -210,7 +211,7 @@ static napi_value _emnapi_tsfn_finalize_in_callback_scope(napi_env env, napi_cal
210211

211212
static void _emnapi_tsfn_finalize(napi_threadsafe_function func) {
212213
napi_handle_scope scope;
213-
EMNAPI_ASSERT_CALL(napi_open_handle_scope(func->env, &scope));
214+
_emnapi_open_handle_scope(&scope);
214215
if (func->finalize_cb) {
215216
if (emnapi_is_node_binding_available()) {
216217
napi_value resource, cb;
@@ -229,7 +230,7 @@ static void _emnapi_tsfn_finalize(napi_threadsafe_function func) {
229230
}
230231
}
231232
_emnapi_tsfn_empty_queue_and_delete(func);
232-
EMNAPI_ASSERT_CALL(napi_close_handle_scope(func->env, scope));
233+
_emnapi_close_handle_scope(scope);
233234
}
234235

235236
static void _emnapi_tsfn_do_finalize(uv_handle_t* data) {

packages/emnapi/src/util.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,19 @@ export function $emnapiSetValueI64 (result: Pointer<int64_t>, numberValue: numbe
133133
makeSetValue('result', 0, 'tempI64[0]', 'i32')
134134
makeSetValue('result', 4, 'tempI64[1]', 'i32')
135135
}
136+
137+
/**
138+
* @__deps $emnapiCtx
139+
* @__sig p
140+
*/
141+
export function _emnapi_open_handle_scope (): napi_handle_scope {
142+
return emnapiCtx.openScope().id
143+
}
144+
145+
/**
146+
* @__deps $emnapiCtx
147+
* @__sig vp
148+
*/
149+
export function _emnapi_close_handle_scope (_scope: napi_handle_scope): void {
150+
return emnapiCtx.closeScope()
151+
}

packages/runtime/src/Context.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -259,12 +259,16 @@ export class Context {
259259
return this.scopeStore.currentScope.add(value)
260260
}
261261

262-
openScope (envObject: Env): HandleScope {
263-
return this.scopeStore.openScope(envObject)
262+
openScope (envObject?: Env): HandleScope {
263+
const scope = this.scopeStore.openScope(this.handleStore)
264+
if (envObject) envObject.openHandleScopes++
265+
return scope
264266
}
265267

266-
closeScope (envObject: Env, _scope?: HandleScope): void {
267-
this.scopeStore.closeScope(envObject)
268+
closeScope (envObject?: Env, _scope?: HandleScope): void {
269+
if (envObject && envObject.openHandleScopes === 0) return
270+
this.scopeStore.closeScope()
271+
if (envObject) envObject.openHandleScopes--
268272
}
269273

270274
ensureHandle<S> (value: S): Handle<S> {

packages/runtime/src/ScopeStore.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import type { Env } from './env'
21
import { HandleStore } from './Handle'
32
import { HandleScope } from './HandleScope'
43

@@ -17,29 +16,26 @@ export class ScopeStore {
1716
return this._values[id]
1817
}
1918

20-
openScope (envObject: Env): HandleScope {
19+
openScope (handleStore: HandleStore): HandleScope {
2120
const currentScope = this.currentScope
2221
let scope = currentScope.child
2322

2423
if (scope !== null) {
2524
scope.start = scope.end = currentScope.end
2625
} else {
2726
const id = currentScope.id + 1
28-
scope = new HandleScope(envObject.ctx.handleStore, id, currentScope, currentScope.end)
27+
scope = new HandleScope(handleStore, id, currentScope, currentScope.end)
2928
this._values[id] = scope
3029
}
3130
this.currentScope = scope
3231

33-
envObject.openHandleScopes++
3432
return scope
3533
}
3634

37-
closeScope (envObject: Env): void {
38-
if (envObject.openHandleScopes === 0) return
35+
closeScope (): void {
3936
const scope = this.currentScope
4037
this.currentScope = scope.parent!
4138
scope.dispose()
42-
envObject.openHandleScopes--
4339
}
4440

4541
dispose (): void {

0 commit comments

Comments
 (0)