Skip to content

Commit 4f108eb

Browse files
authored
refactor: make async work plugin (#186)
1 parent 743f4c9 commit 4f108eb

File tree

16 files changed

+379
-231
lines changed

16 files changed

+379
-231
lines changed

packages/core/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"exports": {
1111
".": "./dist/emnapi-core.js",
1212
"./plugins/v8": "./dist/plugins/v8.js",
13+
"./plugins/async-work": "./dist/plugins/async-work.js",
1314
"./package.json": "./package.json"
1415
},
1516
"dependencies": {

packages/core/rollup.config.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,24 @@ export default [
1414
outputName: 'emnapiCorePluginsV8',
1515
outputFile: 'plugins/v8',
1616
dtsEntry: 'dist/types/emnapi/v8.d.ts',
17+
staticDtsContent: (code) => code.replace(/\.\/index/g, '../emnapi-core'),
1718
input: join(import.meta.dirname, 'src/emnapi/v8.js'),
1819
compilerOptions: {
1920
declaration: false,
2021
declarationMap: false,
2122
declarationDir: undefined
2223
}
24+
}),
25+
...defineConfig({
26+
outputName: 'emnapiCorePluginsAsyncWork',
27+
outputFile: 'plugins/async-work',
28+
dtsEntry: 'dist/types/emnapi/async-work.d.ts',
29+
staticDtsContent: (code) => code.replace(/\.\/index/g, '../emnapi-core'),
30+
input: join(import.meta.dirname, 'src/emnapi/async-work.js'),
31+
compilerOptions: {
32+
declaration: false,
33+
declarationMap: false,
34+
declarationDir: undefined
35+
}
2336
})
2437
]
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { PluginFactory } from './index'
2+
export { PluginFactory, PluginContext, EmnapiPlugin } from './index'
3+
4+
declare const _default: PluginFactory
5+
export default _default

packages/core/src/emnapi/index.d.ts

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,7 @@ export declare interface CreateWorkerInfo {
6969
}
7070

7171
/** @public */
72-
export declare interface PluginContext {
73-
wasmMemory: () => WebAssembly.Memory
74-
wasmTable: () => WebAssembly.Table
75-
emnapiCtx: Context
76-
emnapiString: any // TODO
77-
}
72+
export declare type PluginContext = any // TODO
7873

7974
/** @public */
8075
export declare interface EmnapiPlugin {

packages/core/src/emnapi/v8.d.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import { PluginFactory, PluginContext, EmnapiPlugin } from './index'
2-
3-
export { PluginFactory, PluginContext, EmnapiPlugin }
1+
import { PluginFactory } from './index'
2+
export { PluginFactory, PluginContext, EmnapiPlugin } from './index'
43

54
declare const _default: PluginFactory
6-
75
export default _default

packages/emnapi/CMakeLists.txt

Lines changed: 65 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ project(emnapi LANGUAGES C CXX ASM)
44

55
option(EMNAPI_INSTALL_SRC "EMNAPI_INSTALL_SRC" OFF)
66
option(EMNAPI_FIND_NODE_ADDON_API "EMNAPI_FIND_NODE_ADDON_API" OFF)
7+
option(EMNAPI_BUILD_BASIC_LIBS "EMNAPI_BUILD_BASIC_LIBS" OFF)
78

89
if(CMAKE_SYSTEM_NAME STREQUAL "Emscripten")
910
set(IS_EMSCRIPTEN ON)
@@ -90,12 +91,12 @@ else()
9091
set(EMNAPI_MT_CFLAGS "-pthread")
9192
endif()
9293

93-
# set(EMNAPI_BASIC_TARGET_NAME "emnapi-basic")
94-
# set(EMNAPI_BASIC_MT_TARGET_NAME "emnapi-basic-mt")
94+
set(EMNAPI_BASIC_TARGET_NAME "emnapi-basic")
95+
set(EMNAPI_BASIC_MT_TARGET_NAME "emnapi-basic-mt")
9596
set(EMNAPI_TARGET_NAME "emnapi")
9697
set(EMNAPI_MT_TARGET_NAME "emnapi-mt")
9798

98-
# set(EMNAPI_BASIC_NAPI_RS_MT_TARGET_NAME "emnapi-basic-napi-rs-mt")
99+
set(EMNAPI_BASIC_NAPI_RS_MT_TARGET_NAME "emnapi-basic-napi-rs-mt")
99100
set(EMNAPI_NAPI_RS_MT_TARGET_NAME "emnapi-napi-rs-mt")
100101

101102
set(DLMALLOC_TARGET_NAME "dlmalloc")
@@ -193,22 +194,24 @@ if(IS_EMSCRIPTEN)
193194
target_link_options(${EMNAPI_TARGET_NAME} INTERFACE "--js-library=${EMNAPI_JS_LIB}")
194195
endif()
195196

196-
# add_library(${EMNAPI_BASIC_TARGET_NAME} STATIC ${ENAPI_BASIC_SRC})
197-
# target_include_directories(${EMNAPI_BASIC_TARGET_NAME} PUBLIC ${EMNAPI_INCLUDE})
198-
# target_compile_definitions(${EMNAPI_BASIC_TARGET_NAME}
199-
# PUBLIC ${EMNAPI_DEFINES}
200-
# PRIVATE "EMNAPI_DISABLE_UV"
201-
# )
202-
# if(IS_EMSCRIPTEN)
203-
# set_target_properties(${EMNAPI_BASIC_TARGET_NAME} PROPERTIES INTERFACE_LINK_DEPENDS ${EMNAPI_JS_LIB})
204-
# target_link_options(${EMNAPI_BASIC_TARGET_NAME} INTERFACE "--js-library=${EMNAPI_JS_LIB}")
205-
# endif()
197+
if(EMNAPI_BUILD_BASIC_LIBS)
198+
add_library(${EMNAPI_BASIC_TARGET_NAME} STATIC ${ENAPI_BASIC_SRC})
199+
target_include_directories(${EMNAPI_BASIC_TARGET_NAME} PUBLIC ${EMNAPI_INCLUDE})
200+
target_compile_definitions(${EMNAPI_BASIC_TARGET_NAME}
201+
PUBLIC ${EMNAPI_DEFINES}
202+
PRIVATE "EMNAPI_DISABLE_UV"
203+
)
204+
if(IS_EMSCRIPTEN)
205+
set_target_properties(${EMNAPI_BASIC_TARGET_NAME} PROPERTIES INTERFACE_LINK_DEPENDS ${EMNAPI_JS_LIB})
206+
target_link_options(${EMNAPI_BASIC_TARGET_NAME} INTERFACE "--js-library=${EMNAPI_JS_LIB}")
207+
endif()
206208

207-
# if(IS_WASM32 OR IS_WASM32_WASIP1 OR IS_WASM32_WASIP1_THREADS)
208-
# set(EMNAPI_BUILD_BASIC_MT ON)
209-
# else()
210-
# set(EMNAPI_BUILD_BASIC_MT OFF)
211-
# endif()
209+
if(IS_WASM32 OR IS_WASM32_WASIP1 OR IS_WASM32_WASIP1_THREADS)
210+
set(EMNAPI_BUILD_BASIC_MT ON)
211+
else()
212+
set(EMNAPI_BUILD_BASIC_MT OFF)
213+
endif()
214+
endif()
212215

213216
if(IS_WASM32_WASIP1_THREADS)
214217
set(EMNAPI_BUILD_FOR_NAPI_RS ON)
@@ -223,36 +226,40 @@ if(EMNAPI_BUILD_FOR_NAPI_RS)
223226
PUBLIC ${EMNAPI_DEFINES} "NAPI_EXTERN=__attribute__((__import_module__(\"env\")))"
224227
)
225228

226-
# add_library(${EMNAPI_BASIC_NAPI_RS_MT_TARGET_NAME} STATIC
227-
# ${ENAPI_BASIC_SRC}
228-
# "${CMAKE_CURRENT_SOURCE_DIR}/src/thread/async_worker_create.c"
229-
# "${CMAKE_CURRENT_SOURCE_DIR}/src/thread/async_worker_init.S"
230-
# )
231-
# target_include_directories(${EMNAPI_BASIC_NAPI_RS_MT_TARGET_NAME} PUBLIC ${EMNAPI_INCLUDE})
232-
# target_compile_definitions(${EMNAPI_BASIC_NAPI_RS_MT_TARGET_NAME}
233-
# PUBLIC ${EMNAPI_DEFINES} "NAPI_EXTERN=__attribute__((__import_module__(\"env\")))"
234-
# PRIVATE "EMNAPI_DISABLE_UV"
235-
# )
229+
if(EMNAPI_BUILD_BASIC_LIBS)
230+
add_library(${EMNAPI_BASIC_NAPI_RS_MT_TARGET_NAME} STATIC
231+
${ENAPI_BASIC_SRC}
232+
"${CMAKE_CURRENT_SOURCE_DIR}/src/thread/async_worker_create.c"
233+
"${CMAKE_CURRENT_SOURCE_DIR}/src/thread/async_worker_init.S"
234+
)
235+
target_include_directories(${EMNAPI_BASIC_NAPI_RS_MT_TARGET_NAME} PUBLIC ${EMNAPI_INCLUDE})
236+
target_compile_definitions(${EMNAPI_BASIC_NAPI_RS_MT_TARGET_NAME}
237+
PUBLIC ${EMNAPI_DEFINES} "NAPI_EXTERN=__attribute__((__import_module__(\"env\")))"
238+
PRIVATE "EMNAPI_DISABLE_UV"
239+
)
240+
endif()
236241
endif()
237242

238-
# if(EMNAPI_BUILD_BASIC_MT)
239-
# add_library(${EMNAPI_BASIC_MT_TARGET_NAME} STATIC
240-
# ${ENAPI_BASIC_SRC}
241-
# "${CMAKE_CURRENT_SOURCE_DIR}/src/thread/async_worker_create.c"
242-
# "${CMAKE_CURRENT_SOURCE_DIR}/src/thread/async_worker_init.S"
243-
# )
244-
# target_compile_options(${EMNAPI_BASIC_MT_TARGET_NAME} PUBLIC "-matomics" "-mbulk-memory")
245-
# target_include_directories(${EMNAPI_BASIC_MT_TARGET_NAME} PUBLIC ${EMNAPI_INCLUDE})
246-
# target_compile_definitions(${EMNAPI_BASIC_MT_TARGET_NAME}
247-
# PUBLIC ${EMNAPI_DEFINES}
248-
# PRIVATE "EMNAPI_DISABLE_UV"
249-
# )
250-
251-
# if(IS_EMSCRIPTEN)
252-
# set_target_properties(${EMNAPI_BASIC_MT_TARGET_NAME} PROPERTIES INTERFACE_LINK_DEPENDS ${EMNAPI_JS_LIB})
253-
# target_link_options(${EMNAPI_BASIC_MT_TARGET_NAME} INTERFACE "--js-library=${EMNAPI_JS_LIB}")
254-
# endif()
255-
# endif()
243+
if(EMNAPI_BUILD_BASIC_LIBS)
244+
if(EMNAPI_BUILD_BASIC_MT)
245+
add_library(${EMNAPI_BASIC_MT_TARGET_NAME} STATIC
246+
${ENAPI_BASIC_SRC}
247+
"${CMAKE_CURRENT_SOURCE_DIR}/src/thread/async_worker_create.c"
248+
"${CMAKE_CURRENT_SOURCE_DIR}/src/thread/async_worker_init.S"
249+
)
250+
target_compile_options(${EMNAPI_BASIC_MT_TARGET_NAME} PUBLIC "-matomics" "-mbulk-memory")
251+
target_include_directories(${EMNAPI_BASIC_MT_TARGET_NAME} PUBLIC ${EMNAPI_INCLUDE})
252+
target_compile_definitions(${EMNAPI_BASIC_MT_TARGET_NAME}
253+
PUBLIC ${EMNAPI_DEFINES}
254+
PRIVATE "EMNAPI_DISABLE_UV"
255+
)
256+
257+
if(IS_EMSCRIPTEN)
258+
set_target_properties(${EMNAPI_BASIC_MT_TARGET_NAME} PROPERTIES INTERFACE_LINK_DEPENDS ${EMNAPI_JS_LIB})
259+
target_link_options(${EMNAPI_BASIC_MT_TARGET_NAME} INTERFACE "--js-library=${EMNAPI_JS_LIB}")
260+
endif()
261+
endif()
262+
endif()
256263

257264
if(IS_EMSCRIPTEN OR IS_WASM32_WASIP1_THREADS)
258265
set(EMNAPI_BUILD_MT ON)
@@ -319,12 +326,19 @@ if(LIB_ARCH)
319326
install(TARGETS ${V8_MT_TARGET_NAME} DESTINATION "lib/${LIB_ARCH}")
320327
endif()
321328
endif()
322-
# if(EMNAPI_BUILD_BASIC_MT)
323-
# install(TARGETS ${EMNAPI_BASIC_MT_TARGET_NAME} DESTINATION "lib/${LIB_ARCH}")
324-
# endif()
329+
330+
if(EMNAPI_BUILD_BASIC_LIBS)
331+
install(TARGETS ${EMNAPI_BASIC_TARGET_NAME} DESTINATION "lib/${LIB_ARCH}")
332+
if(EMNAPI_BUILD_BASIC_MT)
333+
install(TARGETS ${EMNAPI_BASIC_MT_TARGET_NAME} DESTINATION "lib/${LIB_ARCH}")
334+
endif()
335+
endif()
336+
325337
if(EMNAPI_BUILD_FOR_NAPI_RS)
326338
install(TARGETS ${EMNAPI_NAPI_RS_MT_TARGET_NAME} DESTINATION "lib/${LIB_ARCH}")
327-
# install(TARGETS ${EMNAPI_BASIC_NAPI_RS_MT_TARGET_NAME} DESTINATION "lib/${LIB_ARCH}")
339+
if(EMNAPI_BUILD_BASIC_LIBS)
340+
install(TARGETS ${EMNAPI_BASIC_NAPI_RS_MT_TARGET_NAME} DESTINATION "lib/${LIB_ARCH}")
341+
endif()
328342
endif()
329343
if(IS_WASM32)
330344
install(TARGETS ${DLMALLOC_TARGET_NAME} DESTINATION "lib/${LIB_ARCH}")
@@ -346,6 +360,7 @@ install(DIRECTORY
346360
install(FILES
347361
${CMAKE_CURRENT_SOURCE_DIR}/dist/library_napi.js
348362
${CMAKE_CURRENT_SOURCE_DIR}/dist/library_v8.js
363+
${CMAKE_CURRENT_SOURCE_DIR}/dist/library_async_work.js
349364
DESTINATION "dist")
350365

351366
if(EMNAPI_INSTALL_SRC)

0 commit comments

Comments
 (0)