Skip to content

Commit

Permalink
fix(cli): also load wasm file from packages (#1876)
Browse files Browse the repository at this point in the history
  • Loading branch information
Brooooooklyn committed Dec 29, 2023
1 parent f62685e commit b0ba466
Show file tree
Hide file tree
Showing 8 changed files with 250 additions and 204 deletions.
11 changes: 6 additions & 5 deletions cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,13 @@
"typanion": "^3.14.0"
},
"devDependencies": {
"@emnapi/core": "0.44.0",
"@emnapi/runtime": "0.44.0",
"@emnapi/core": "^0.45.0",
"@emnapi/runtime": "^0.45.0",
"@types/debug": "^4.1.12",
"@types/inquirer": "^9.0.7",
"@types/js-yaml": "^4.0.9",
"@types/lodash-es": "^4.17.12",
"@types/node": "^20.10.5",
"ava": "^6.0.0",
"env-paths": "^3.0.0",
"esbuild": "^0.19.8",
Expand All @@ -96,9 +97,9 @@
"typescript": "^5.3.2"
},
"peerDependencies": {
"@emnapi/runtime": "0.44.0",
"@tybys/wasm-util": "0.8.0",
"emnapi": "0.44.0"
"@emnapi/runtime": "^0.45.0",
"@tybys/wasm-util": "^0.8.0",
"emnapi": "^0.45.0"
},
"peerDependenciesMeta": {
"@emnapi/runtime": {
Expand Down
8 changes: 7 additions & 1 deletion cli/src/api/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -799,7 +799,13 @@ class Builder {
.join(',\n')
await writeFileAsync(
newPath,
createWasiBinding(name, wasiRegisterFunctions) + exportsCode + '\n',
createWasiBinding(
name,
this.config.packageName,
wasiRegisterFunctions,
) +
exportsCode +
'\n',
'utf8',
)
await writeFileAsync(workerPath, WASI_WORKER_TEMPLATE, 'utf8')
Expand Down
9 changes: 1 addition & 8 deletions cli/src/api/templates/js-binding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,14 +313,7 @@ switch (platform) {
if (!nativeBinding || process.env.NAPI_RS_FORCE_WASI) {
try {
localFileExisted = existsSync(
join(__dirname, '${localName}.wasm32-wasi.wasm')
) && existsSync(join(__dirname, '${localName}.wasi.cjs'))
if (localFileExisted) {
nativeBinding = require('./${localName}.wasi.cjs')
} else {
nativeBinding = require('${pkgName}-wasm32-wasi')
}
nativeBinding = require('./${localName}.wasi.cjs')
} catch {
// ignore
}
Expand Down
23 changes: 21 additions & 2 deletions cli/src/api/templates/load-wasi-template.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export const createWasiBinding = (
wasmFileName: string,
packageName: string,
wasiRegisterFunctions: string[],
) => `/* eslint-disable */
/* prettier-ignore */
Expand Down Expand Up @@ -30,9 +31,27 @@ const __sharedMemory = new WebAssembly.Memory({
shared: true,
})
const { instance: __napiInstance, module: __wasiModule, napiModule: __napiModule } = __emnapiInstantiateNapiModuleSync(__nodeFs.readFileSync(__nodePath.join(__dirname, '${wasmFileName}.wasm')), {
let __wasmFilePath = __nodePath.join(__dirname, '${wasmFileName}.wasm')
if (!__nodeFs.existsSync(__wasmFilePath)) {
try {
__wasmFilePath = __nodePath.resolve('${packageName}-wasm32-wasi')
} catch {
throw new Error('Cannot find ${wasmFileName}.wasm file, and ${packageName}-wasm32-wasi package is not installed.')
}
}
const { instance: __napiInstance, module: __wasiModule, napiModule: __napiModule } = __emnapiInstantiateNapiModuleSync(__nodeFs.readFileSync(__wasmFilePath), {
context: __emnapiContext,
asyncWorkPoolSize: 4,
asyncWorkPoolSize: (function() {
const threadsSizeFromEnv = Number(process.env.NAPI_RS_ASYNC_WORK_POOL_SIZE ?? process.env.UV_THREADPOOL_SIZE)
// NaN > 0 is false
if (threadsSizeFromEnv > 0) {
return threadsSizeFromEnv
} else {
return 4
}
})(),
wasi: __wasi,
onCreateWorker() {
return new Worker(__nodePath.join(__dirname, 'wasi-worker.mjs'), {
Expand Down
362 changes: 193 additions & 169 deletions examples/napi/index.wasi.cjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion examples/napi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"timeout": "10m"
},
"dependencies": {
"@emnapi/core": "0.44.0",
"@emnapi/core": "0.45.0",
"@tybys/wasm-util": "^0.8.0",
"@types/lodash": "^4.14.202",
"lodash": "^4.17.21",
Expand Down
1 change: 1 addition & 0 deletions triples/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"build": "node --loader ts-node/esm/transpile-only ./generate-triple-list.ts && prettier --write ./index.cjs"
},
"devDependencies": {
"@types/node": "^20.10.5",
"lodash-es": "^4.17.21",
"ts-node": "^10.9.1",
"typescript": "^5.3.2"
Expand Down
38 changes: 20 additions & 18 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -126,21 +126,21 @@ __metadata:
languageName: node
linkType: hard

"@emnapi/core@npm:0.44.0":
version: 0.44.0
resolution: "@emnapi/core@npm:0.44.0"
"@emnapi/core@npm:0.45.0, @emnapi/core@npm:^0.45.0":
version: 0.45.0
resolution: "@emnapi/core@npm:0.45.0"
dependencies:
tslib: "npm:^2.4.0"
checksum: 554ad897ae5d6e49471f1922113e60f74ecdc7617ea83ba070b022e7878d7f35160b26d42da98f692f915ba89ccc8900a57ee1afeafbe9fc225c0b92db5772f4
checksum: 26071cfe32a5a15068483ed36a152c03add319166c30e6cb81f0d6eb2614e69f836daa2d69af5af2e00fc2b8335f172e5ce2ee91f1819b3e689c908a7883433f
languageName: node
linkType: hard

"@emnapi/runtime@npm:0.44.0":
version: 0.44.0
resolution: "@emnapi/runtime@npm:0.44.0"
"@emnapi/runtime@npm:^0.45.0":
version: 0.45.0
resolution: "@emnapi/runtime@npm:0.45.0"
dependencies:
tslib: "npm:^2.4.0"
checksum: 68133f288a5f413787610232c90194ab0b692b859a130866f4869483217d94f71ac3fd23aacfb428e0eb438e4882d0e04874690a1f06386b7b35ebae330e698a
checksum: c83052b05efb7147c256bfbb69214c9642fef1dce8d7d901e0314a7b2d2dcd14e1cd75502c6565004847e552658e9913a7e58889c7dca92e240173032f1db5d5
languageName: node
linkType: hard

Expand Down Expand Up @@ -354,7 +354,7 @@ __metadata:
version: 0.0.0-use.local
resolution: "@examples/napi@workspace:examples/napi"
dependencies:
"@emnapi/core": "npm:0.44.0"
"@emnapi/core": "npm:0.45.0"
"@napi-rs/cli": "workspace:*"
"@tybys/wasm-util": "npm:^0.8.0"
"@types/lodash": "npm:^4.14.201"
Expand Down Expand Up @@ -573,15 +573,16 @@ __metadata:
version: 0.0.0-use.local
resolution: "@napi-rs/cli@workspace:cli"
dependencies:
"@emnapi/core": "npm:0.44.0"
"@emnapi/runtime": "npm:0.44.0"
"@emnapi/core": "npm:^0.45.0"
"@emnapi/runtime": "npm:^0.45.0"
"@napi-rs/cross-toolchain": "npm:^0.0.14"
"@octokit/rest": "npm:^20.0.2"
"@tybys/wasm-util": "npm:0.8.0"
"@types/debug": "npm:^4.1.12"
"@types/inquirer": "npm:^9.0.7"
"@types/js-yaml": "npm:^4.0.9"
"@types/lodash-es": "npm:^4.17.12"
"@types/node": "npm:^20.10.5"
ava: "npm:^6.0.0"
clipanion: "npm:^3.2.1"
colorette: "npm:^2.0.20"
Expand All @@ -599,9 +600,9 @@ __metadata:
typanion: "npm:^3.14.0"
typescript: "npm:^5.3.2"
peerDependencies:
"@emnapi/runtime": 0.44.0
"@tybys/wasm-util": 0.8.0
emnapi: 0.44.0
"@emnapi/runtime": ^0.45.0
"@tybys/wasm-util": ^0.8.0
emnapi: ^0.45.0
peerDependenciesMeta:
"@emnapi/runtime":
optional: true
Expand Down Expand Up @@ -928,6 +929,7 @@ __metadata:
version: 0.0.0-use.local
resolution: "@napi-rs/triples@workspace:triples"
dependencies:
"@types/node": "npm:^20.10.5"
lodash-es: "npm:^4.17.21"
ts-node: "npm:^10.9.1"
typescript: "npm:^5.3.2"
Expand Down Expand Up @@ -1889,12 +1891,12 @@ __metadata:
languageName: node
linkType: hard

"@types/node@npm:*, @types/node@npm:^20.10.3":
version: 20.10.3
resolution: "@types/node@npm:20.10.3"
"@types/node@npm:*, @types/node@npm:^20.10.3, @types/node@npm:^20.10.5":
version: 20.10.5
resolution: "@types/node@npm:20.10.5"
dependencies:
undici-types: "npm:~5.26.4"
checksum: 7c8a0327a29d68a0bb152ae30e8c4738638515dd599064821c9d0c1884a1efa103021cd8f8c91e690cd56b0602bb6fe0e6651a5df2337bdc85e0c0f66054b2ce
checksum: be30609aae0bfe492097815f166ccc07f465220cb604647fa4e5ec05a1d16c012a41b82b5f11ecfe2485cbb479d4d20384b95b809ca0bcff6d94d5bbafa645bb
languageName: node
linkType: hard

Expand Down

0 comments on commit b0ba466

Please sign in to comment.