Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
62 commits
Select commit Hold shift + click to select a range
254e8db
support wasi threads
toyobayashi Feb 15, 2023
cfb3b4e
fix lint
toyobayashi Feb 15, 2023
1d7d518
do not set PTHREAD_MUTEX_ERRORCHECK in wasi
toyobayashi Feb 16, 2023
981fd63
test ci
toyobayashi Feb 16, 2023
8dca2f1
test ci
toyobayashi Feb 16, 2023
a8d1e94
update eslint
toyobayashi Feb 16, 2023
87dde35
test ci
toyobayashi Feb 16, 2023
11f6f43
eslint ignore dts
toyobayashi Feb 16, 2023
ee4cb2e
ci skip lint
toyobayashi Feb 16, 2023
b81a5ef
test ci
toyobayashi Feb 16, 2023
7497586
test ci
toyobayashi Feb 16, 2023
e1872e6
may rename (WebAssembly/wasi-sdk#297)
toyobayashi Feb 16, 2023
38d9953
fix SharedArrayBuffer sync
toyobayashi Feb 16, 2023
1d36c34
spawn thread from child thread
toyobayashi Feb 16, 2023
21a8fdf
wasi-threads skip node-addon-api test
toyobayashi Feb 16, 2023
61d8236
test wasi polyfill
toyobayashi Feb 19, 2023
4720e35
update WASI polyfill in test
toyobayashi Feb 19, 2023
31f155a
pass WebAssembly.Module to child thread
toyobayashi Feb 19, 2023
c3ee136
rename message
toyobayashi Feb 19, 2023
7f8c706
worker message structure
toyobayashi Feb 19, 2023
1abb463
fix test
toyobayashi Feb 19, 2023
7670e7c
Merge branch 'main' into feat-wasi-threads
toyobayashi Feb 26, 2023
b872bfe
fix ci
toyobayashi Feb 26, 2023
eefa90b
fix ci
toyobayashi Feb 26, 2023
b59421e
add `loadNapiModule` and `loadNapiModuleSync`
toyobayashi Feb 26, 2023
af162ff
move `postMessage` to `napiModule`
toyobayashi Feb 26, 2023
b4465e0
test
toyobayashi Feb 26, 2023
fd9a332
bundle `load` & `loadSync` from `@tybys/wasm-util`
toyobayashi Feb 26, 2023
16732c1
loading api change
toyobayashi Feb 27, 2023
96e3572
test browser compatitable worker js file
toyobayashi Feb 27, 2023
8d0342e
do not wait threads on wasi since worker may throw
toyobayashi Feb 27, 2023
8b5436d
function type
toyobayashi Feb 27, 2023
ad4c0bb
wake child thread when throw
toyobayashi Feb 27, 2023
ab9e1d5
add `instantiateNapiModule(Sync)`
toyobayashi Feb 28, 2023
74597b6
error message
toyobayashi Feb 28, 2023
c6d6968
handleMessage
toyobayashi Feb 28, 2023
e7665d6
new abi of thread-spawn(WebAssembly/wasi-libc#385)
toyobayashi Mar 1, 2023
ced8e78
cleanup thread
toyobayashi Mar 1, 2023
9904099
add `ES2017.SharedMemory` to root tsconfig
toyobayashi Mar 3, 2023
ddd77d7
only unref uv threadpool workers
toyobayashi Mar 3, 2023
edc8083
remove `pthread_atfork`
toyobayashi Mar 3, 2023
6a7fb88
test worker
toyobayashi Mar 3, 2023
e91e501
refactor spawnThread
toyobayashi Mar 3, 2023
4b903be
zero memory
toyobayashi Mar 3, 2023
c8dd0b5
zero memory
toyobayashi Mar 3, 2023
2df6fb0
add `reuseWorker` option
toyobayashi Mar 3, 2023
4d08f21
ignore eslint no-var
toyobayashi Mar 3, 2023
16a1ed3
`MessageHandler`
toyobayashi Mar 5, 2023
769600c
queue async work after uv threadpool ready on wasi
toyobayashi Mar 6, 2023
d909cde
tsc build core
toyobayashi Mar 6, 2023
f5f0d8f
test wasm32-wasi-threads node-addon-api
toyobayashi Mar 7, 2023
48ced5d
save messages before load
toyobayashi Mar 8, 2023
e9f8498
test wasi async in worker
toyobayashi Mar 8, 2023
7d8fff7
rename .h
toyobayashi Mar 8, 2023
6cdfa8f
rename .h
toyobayashi Mar 8, 2023
31b2328
readme
toyobayashi Mar 8, 2023
9386a49
instantiateNapiModule support `Promise<Response>`
toyobayashi Mar 9, 2023
328db93
proxy
toyobayashi Mar 9, 2023
68cbb23
do not merge symbols to env
toyobayashi Mar 9, 2023
9d480d9
test cmake
toyobayashi Mar 9, 2023
0ab8f98
readme
toyobayashi Mar 9, 2023
8f64bda
readme
toyobayashi Mar 9, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
**/dist
**/lib
node_modules
/lib/typings
.eslintrc.js
Expand All @@ -9,3 +10,4 @@ node_modules
/packages/emnapi/lib
/packages/emnapi/transformer/out
/packages/core/src/index.js
/out
5 changes: 5 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ module.exports = {
'spaced-comment': 'off',
'no-new-func': 'off',
'no-implied-eval': 'off',
'no-var': 'off',
'@typescript-eslint/no-implied-eval': 'off',
'@typescript-eslint/no-unused-vars': 'error',
'@typescript-eslint/no-namespace': 'off',
Expand All @@ -24,6 +25,10 @@ module.exports = {
'@typescript-eslint/no-dynamic-delete': 'off',
'@typescript-eslint/method-signature-style': 'off',
'@typescript-eslint/prefer-includes': 'off',
'@typescript-eslint/ban-ts-comment': 'off',
'@typescript-eslint/ban-types': 'off',
'@typescript-eslint/consistent-type-imports': 'off',
'@typescript-eslint/consistent-generic-constructors': 'off',
'@typescript-eslint/no-unnecessary-type-assertion': 'off',
'@typescript-eslint/member-delimiter-style': ['error', {
multiline: {
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
- 'wasm64-unknown-emscripten'
- 'wasm32-unknown-unknown'
- 'wasm32-wasi'
# - 'wasm32-wasi-threads'
- 'wasm32-wasi-threads'

steps:
- uses: actions/checkout@v3
Expand Down
10 changes: 10 additions & 0 deletions .vscode/c_cpp_properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@
"cppStandard": "c++14",
"includePath": ["${includePath}"]
},
{
"name": "WASI-THREADS",
"defines": ["${defines}", "_REENTRANT"],
"compilerPath": "${env:WASI_SDK_PATH}/bin/clang",
"intelliSenseMode": "clang-x86",
"cStandard": "c11",
"cppStandard": "c++14",
"includePath": ["${includePath}"],
"compilerArgs": ["--target=wasm32-wasi-threads"]
},
{
"name": "WASM32",
"defines": ["${defines}", "PAGESIZE=65536"],
Expand Down
6 changes: 4 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@
"request": "launch",
"name": "Launch Program",
"env": {
"EMNAPI_TEST_WASI": "1"
"NODE_TEST_KNOWN_GLOBALS": "0",
"EMNAPI_TEST_WASI": "1",
"EMNAPI_TEST_WASI_THREADS": "1"
},
"runtimeArgs": ["--experimental-wasi-unstable-preview1", "--expose-gc"],
"program": "${workspaceFolder}/packages/test/arg/arg.test.js",
"program": "${workspaceFolder}/packages/test/node-addon-api/async_worker.test.js",
"args": []
},
{
Expand Down
14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@
"@tybys/tsapi": "^0.6.0",
"@types/fs-extra": "^9.0.13",
"@types/node": "^16.11.21",
"@typescript-eslint/eslint-plugin": "^5.32.0",
"@typescript-eslint/parser": "^5.32.0",
"eslint": "^8.21.0",
"eslint-config-standard-with-typescript": "^22.0.0",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-n": "^15.2.4",
"eslint-plugin-promise": "^6.0.0",
"@typescript-eslint/eslint-plugin": "^5.52.0",
"@typescript-eslint/parser": "^5.52.0",
"eslint": "^8.34.0",
"eslint-config-standard-with-typescript": "^34.0.0",
"eslint-plugin-import": "^2.27.5",
"eslint-plugin-n": "^15.6.1",
"eslint-plugin-promise": "^6.1.1",
"fs-extra": "^10.1.0",
"typescript": "~4.8.4"
},
Expand Down
109 changes: 94 additions & 15 deletions packages/core/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,36 @@
import type { Context } from '@emnapi/runtime'

export declare interface CreateOptions {
context: Context
filename?: string
nodeBinding?: {
node: {
emitAsyncInit: Function
emitAsyncDestroy: Function
makeCallback: Function
}
napi: {
asyncInit: Function
asyncDestroy: Function
makeCallback: Function
}
export declare interface NodeBinding {
node: {
emitAsyncInit: Function
emitAsyncDestroy: Function
makeCallback: Function
}
napi: {
asyncInit: Function
asyncDestroy: Function
makeCallback: Function
}
}

export declare type BaseCreateOptions = {
filename?: string
nodeBinding?: NodeBinding
reuseWorker?: boolean
onCreateWorker?: () => any
print?: (str: string) => void
printErr?: (str: string) => void
postMessage?: (msg: any) => any
}

export declare type CreateOptions = BaseCreateOptions & ({
context: Context
childThread?: boolean
} | {
context?: Context
childThread: true
})

export declare interface PointerInfo {
address: number
ownership: 0 | 1
Expand All @@ -39,6 +53,7 @@ export declare interface NapiModule {
exports: any
loaded: boolean
filename: string
childThread: boolean
emnapi: {
syncMemory<T extends ArrayBuffer | ArrayBufferView> (
js_to_wasm: boolean,
Expand All @@ -50,6 +65,70 @@ export declare interface NapiModule {
}

init (options: InitOptions): any
spawnThread (startArg: number, errorOrTid?: number): number
startThread (tid: number, startArg: number): void
postMessage?: (msg: any) => any
}

export declare function createNapiModule (
options: CreateOptions
): NapiModule

export declare interface ReactorWASI {
readonly wasiImport?: Record<string, any>
initialize (instance: object): void
getImportObject? (): any
}

export declare interface LoadOptions {
wasi?: ReactorWASI
overwriteImports?: (importObject: WebAssembly.Imports) => WebAssembly.Imports
getMemory?: (exports: WebAssembly.Exports) => WebAssembly.Memory
getTable?: (exports: WebAssembly.Exports) => WebAssembly.Table
}

export function createNapiModule (options: CreateOptions): NapiModule
export declare type InstantiateOptions = CreateOptions & LoadOptions

export declare interface InstantiatedSource extends WebAssembly.WebAssemblyInstantiatedSource {
napiModule: NapiModule
}

export declare type InputType = string | URL | Response | BufferSource | WebAssembly.Module

export declare function loadNapiModule (
napiModule: NapiModule,
/** Only support `BufferSource` or `WebAssembly.Module` on Node.js */
wasmInput: InputType | Promise<InputType>,
options?: LoadOptions
): Promise<WebAssembly.WebAssemblyInstantiatedSource>

export declare function loadNapiModuleSync (
napiModule: NapiModule,
wasmInput: BufferSource | WebAssembly.Module,
options?: LoadOptions
): WebAssembly.WebAssemblyInstantiatedSource

export declare function instantiateNapiModule (
/** Only support `BufferSource` or `WebAssembly.Module` on Node.js */
wasmInput: InputType | Promise<InputType>,
options: InstantiateOptions
): Promise<InstantiatedSource>

export declare function instantiateNapiModuleSync (
wasmInput: BufferSource | WebAssembly.Module,
options: InstantiateOptions
): InstantiatedSource

export declare interface OnLoadData {
wasmModule: WebAssembly.Module
wasmMemory: WebAssembly.Memory
}

export declare interface HandleOptions {
onLoad (data: OnLoadData): InstantiatedSource | Promise<InstantiatedSource>
}

export declare class MessageHandler {
constructor (options: HandleOptions)
handle (e: { data: any }): void
}
18 changes: 17 additions & 1 deletion packages/core/script/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,32 @@ const rollupNodeResolve = require('@rollup/plugin-node-resolve').default
const rollupReplace = require('@rollup/plugin-replace').default
const rollupTerser = require('rollup-plugin-terser').terser
const dist = path.join(__dirname, '../dist')
const { compile } = require('@tybys/tsapi')

function build () {
compile(path.join(__dirname, '../tsconfig.json'), {
optionsToExtend: {
target: require('typescript').ScriptTarget.ES5,
outDir: path.join(__dirname, '../lib/es5')
}
})
compile(path.join(__dirname, '../tsconfig.json'), {
optionsToExtend: {
target: require('typescript').ScriptTarget.ES2019,
outDir: path.join(__dirname, '../lib/es2019'),
removeComments: true,
downlevelIteration: false
}
})

/**
* @param {'es5' | 'es2019'} esversion
* @param {boolean=} minify
* @returns {rollup.RollupOptions}
*/
function createInput (esversion, minify, options) {
return {
input: path.join(__dirname, '../src/index.js'),
input: path.join(__dirname, '../lib', esversion, 'index.js'),
plugins: [
rollupNodeResolve({
mainFields: ['module', 'main'],
Expand Down
8 changes: 8 additions & 0 deletions packages/core/src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
export { createNapiModule } from './module.js'
export {
loadNapiModule,
loadNapiModuleSync,
instantiateNapiModule,
instantiateNapiModuleSync
} from './load.js'

export { MessageHandler } from './worker.js'

export const version = __VERSION__
Loading