({
ast,
initFuncId,
startAtFuncOffset,
importedGlobals,
additionalInitCode,
nextFuncIndex,
nextTypeIndex
})
| 323 | */ |
| 324 | const addInitFunction = |
| 325 | ({ |
| 326 | ast, |
| 327 | initFuncId, |
| 328 | startAtFuncOffset, |
| 329 | importedGlobals, |
| 330 | additionalInitCode, |
| 331 | nextFuncIndex, |
| 332 | nextTypeIndex |
| 333 | }) => |
| 334 | (bin) => { |
| 335 | const funcParams = importedGlobals.map((importedGlobal) => { |
| 336 | // used for debugging |
| 337 | const id = t.identifier( |
| 338 | `${importedGlobal.module}.${importedGlobal.name}` |
| 339 | ); |
| 340 | |
| 341 | return t.funcParam( |
| 342 | /** @type {string} */ (importedGlobal.descr.valtype), |
| 343 | id |
| 344 | ); |
| 345 | }); |
| 346 | |
| 347 | /** @type {Instruction[]} */ |
| 348 | const funcBody = []; |
| 349 | for (const [index, _importedGlobal] of importedGlobals.entries()) { |
| 350 | const args = [t.indexLiteral(index)]; |
| 351 | const body = [ |
| 352 | t.instruction("get_local", args), |
| 353 | t.instruction("set_global", args) |
| 354 | ]; |
| 355 | |
| 356 | funcBody.push(...body); |
| 357 | } |
| 358 | |
| 359 | if (typeof startAtFuncOffset === "number") { |
| 360 | funcBody.push( |
| 361 | t.callInstruction(t.numberLiteralFromRaw(startAtFuncOffset)) |
| 362 | ); |
| 363 | } |
| 364 | |
| 365 | for (const instr of additionalInitCode) { |
| 366 | funcBody.push(instr); |
| 367 | } |
| 368 | |
| 369 | funcBody.push(t.instruction("end")); |
| 370 | |
| 371 | /** @type {string[]} */ |
| 372 | const funcResults = []; |
| 373 | |
| 374 | // Code section |
| 375 | const funcSignature = t.signature(funcParams, funcResults); |
| 376 | const func = t.func(initFuncId, funcSignature, funcBody); |
| 377 | |
| 378 | // Type section |
| 379 | const functype = t.typeInstruction(undefined, funcSignature); |
| 380 | |
| 381 | // Func section |
| 382 | const funcindex = t.indexInFuncSection(nextTypeIndex); |
no test coverage detected