2020#include " support/bits.h"
2121#include " wasm-binary.h"
2222#include " ast/branch-utils.h"
23+ #include " ast/module-utils.h"
2324
2425namespace wasm {
2526
@@ -31,6 +32,9 @@ void WasmBinaryWriter::prepare() {
3132 }
3233 // TODO: depending on upstream flux https://github.com/WebAssembly/spec/pull/301 might want this: assert(!func->type.isNull());
3334 }
35+ ModuleUtils::BinaryIndexes indexes (*wasm);
36+ mappedFunctions = std::move (indexes.functionIndexes );
37+ mappedGlobals = std::move (indexes.globalIndexes );
3438}
3539
3640void WasmBinaryWriter::write () {
@@ -343,39 +347,11 @@ void WasmBinaryWriter::writeDataSegments() {
343347}
344348
345349uint32_t WasmBinaryWriter::getFunctionIndex (Name name) {
346- if (!mappedFunctions.size ()) {
347- // Create name => index mapping.
348- for (auto & import : wasm->imports ) {
349- if (import ->kind != ExternalKind::Function) continue ;
350- assert (mappedFunctions.count (import ->name ) == 0 );
351- auto index = mappedFunctions.size ();
352- mappedFunctions[import ->name ] = index;
353- }
354- for (size_t i = 0 ; i < wasm->functions .size (); i++) {
355- assert (mappedFunctions.count (wasm->functions [i]->name ) == 0 );
356- auto index = mappedFunctions.size ();
357- mappedFunctions[wasm->functions [i]->name ] = index;
358- }
359- }
360350 assert (mappedFunctions.count (name));
361351 return mappedFunctions[name];
362352}
363353
364354uint32_t WasmBinaryWriter::getGlobalIndex (Name name) {
365- if (!mappedGlobals.size ()) {
366- // Create name => index mapping.
367- for (auto & import : wasm->imports ) {
368- if (import ->kind != ExternalKind::Global) continue ;
369- assert (mappedGlobals.count (import ->name ) == 0 );
370- auto index = mappedGlobals.size ();
371- mappedGlobals[import ->name ] = index;
372- }
373- for (size_t i = 0 ; i < wasm->globals .size (); i++) {
374- assert (mappedGlobals.count (wasm->globals [i]->name ) == 0 );
375- auto index = mappedGlobals.size ();
376- mappedGlobals[wasm->globals [i]->name ] = index;
377- }
378- }
379355 assert (mappedGlobals.count (name));
380356 return mappedGlobals[name];
381357}
0 commit comments