|
1 | 1 | import wizer from '@bytecodealliance/wizer'; |
| 2 | +import getWeval from '@cfallin/weval'; |
2 | 3 | import { |
3 | 4 | componentNew, |
4 | 5 | metadataAdd, |
5 | 6 | preview1AdapterReactorPath, |
6 | 7 | } from '@bytecodealliance/jco'; |
7 | 8 | import { spawnSync } from 'node:child_process'; |
8 | 9 | import { tmpdir } from 'node:os'; |
9 | | -import { resolve, join } from 'node:path'; |
| 10 | +import { resolve, join, dirname } from 'node:path'; |
10 | 11 | import { readFile, writeFile, mkdir, rm } from 'node:fs/promises'; |
11 | 12 | import { rmSync } from 'node:fs'; |
12 | 13 | import { createHash } from 'node:crypto'; |
@@ -37,16 +38,17 @@ export async function componentize(jsSource, witWorld, opts) { |
37 | 38 | } |
38 | 39 | const { |
39 | 40 | sourceName = 'source.js', |
40 | | - engine = fileURLToPath( |
41 | | - new URL(`../lib/starlingmonkey_embedding.wasm`, import.meta.url) |
42 | | - ), |
43 | 41 | preview2Adapter = preview1AdapterReactorPath(), |
44 | 42 | witPath, |
45 | 43 | worldName, |
46 | 44 | disableFeatures = [], |
47 | 45 | enableFeatures = [], |
| 46 | + aotCache = fileURLToPath(new URL(`../lib/starlingmonkey_ics.wevalcache`, import.meta.url)) |
48 | 47 | } = opts || {}; |
49 | 48 |
|
| 49 | + const engine = opts.engine || fileURLToPath( |
| 50 | + new URL(opts.enableAot ? `../lib/starlingmonkey_embedding_weval.wasm` : `../lib/starlingmonkey_embedding.wasm`, import.meta.url)); |
| 51 | + |
50 | 52 | await lexerInit; |
51 | 53 | let jsImports = []; |
52 | 54 | let jsExports = []; |
@@ -181,42 +183,82 @@ export async function componentize(jsSource, witWorld, opts) { |
181 | 183 | console.log('--- Wizer Env ---'); |
182 | 184 | console.log(env); |
183 | 185 | } |
184 | | - |
185 | | - try { |
186 | | - let wizerProcess = spawnSync( |
187 | | - wizer, |
188 | | - [ |
189 | | - '--allow-wasi', |
190 | | - '--init-func', |
191 | | - 'componentize.wizer', |
192 | | - `--dir=${maybeWindowsPath(sourceDir)}`, |
193 | | - `--wasm-bulk-memory=true`, |
194 | | - '--inherit-env=true', |
195 | | - `-o=${output}`, |
196 | | - input, |
197 | | - ], |
198 | | - { |
199 | | - stdio: [null, stdout, stderr], |
200 | | - env, |
201 | | - input: maybeWindowsPath( |
202 | | - join(sourceDir, sourceName.slice(0, -3) + '.bindings.js') |
203 | | - ), |
204 | | - shell: true, |
205 | | - encoding: 'utf-8', |
| 186 | + if (opts.enableAot) { |
| 187 | + const wevalBin = await getWeval(); |
| 188 | + |
| 189 | + try { |
| 190 | + let wevalProcess = spawnSync( |
| 191 | + wevalBin, |
| 192 | + [ |
| 193 | + 'weval', |
| 194 | + `--cache-ro ${aotCache}`, |
| 195 | + `--dir ${maybeWindowsPath(sourceDir)}`, |
| 196 | + '-w', |
| 197 | + '--init-func', |
| 198 | + 'componentize.wizer', |
| 199 | + `-i ${input}`, |
| 200 | + `-o ${output}` |
| 201 | + ], |
| 202 | + { |
| 203 | + stdio: [null, stdout, stderr], |
| 204 | + env, |
| 205 | + input: maybeWindowsPath( |
| 206 | + join(sourceDir, sourceName.slice(0, -3) + '.bindings.js') |
| 207 | + ), |
| 208 | + shell: true, |
| 209 | + encoding: 'utf-8', |
| 210 | + } |
| 211 | + ); |
| 212 | + if (wevalProcess.status !== 0) |
| 213 | + throw new Error('Wevaling failed to complete'); |
| 214 | + } catch (error) { |
| 215 | + let err = |
| 216 | + `Failed to initialize the compiled Wasm binary with Weval:\n` + |
| 217 | + error.message; |
| 218 | + if (DEBUG_BINDINGS) { |
| 219 | + err += `\nBinary and sources available for debugging at ${tmpDir}\n`; |
| 220 | + } else { |
| 221 | + rmSync(tmpDir, { recursive: true }); |
206 | 222 | } |
207 | | - ); |
208 | | - if (wizerProcess.status !== 0) |
209 | | - throw new Error('Wizering failed to complete'); |
210 | | - } catch (error) { |
211 | | - let err = |
212 | | - `Failed to initialize the compiled Wasm binary with Wizer:\n` + |
213 | | - error.message; |
214 | | - if (DEBUG_BINDINGS) { |
215 | | - err += `\nBinary and sources available for debugging at ${tmpDir}\n`; |
216 | | - } else { |
217 | | - rmSync(tmpDir, { recursive: true }); |
| 223 | + throw new Error(err); |
| 224 | + } |
| 225 | + } else { |
| 226 | + try { |
| 227 | + let wizerProcess = spawnSync( |
| 228 | + wizer, |
| 229 | + [ |
| 230 | + '--allow-wasi', |
| 231 | + '--init-func', |
| 232 | + 'componentize.wizer', |
| 233 | + `--dir=${maybeWindowsPath(sourceDir)}`, |
| 234 | + `--wasm-bulk-memory=true`, |
| 235 | + '--inherit-env=true', |
| 236 | + `-o=${output}`, |
| 237 | + input, |
| 238 | + ], |
| 239 | + { |
| 240 | + stdio: [null, stdout, stderr], |
| 241 | + env, |
| 242 | + input: maybeWindowsPath( |
| 243 | + join(sourceDir, sourceName.slice(0, -3) + '.bindings.js') |
| 244 | + ), |
| 245 | + shell: true, |
| 246 | + encoding: 'utf-8', |
| 247 | + } |
| 248 | + ); |
| 249 | + if (wizerProcess.status !== 0) |
| 250 | + throw new Error('Wizering failed to complete'); |
| 251 | + } catch (error) { |
| 252 | + let err = |
| 253 | + `Failed to initialize the compiled Wasm binary with Wizer:\n` + |
| 254 | + error.message; |
| 255 | + if (DEBUG_BINDINGS) { |
| 256 | + err += `\nBinary and sources available for debugging at ${tmpDir}\n`; |
| 257 | + } else { |
| 258 | + rmSync(tmpDir, { recursive: true }); |
| 259 | + } |
| 260 | + throw new Error(err); |
218 | 261 | } |
219 | | - throw new Error(err); |
220 | 262 | } |
221 | 263 |
|
222 | 264 | const bin = await readFile(output); |
|
0 commit comments