diff options
Diffstat (limited to 'src/tools/wasm-ctor-eval.cpp')
-rw-r--r-- | src/tools/wasm-ctor-eval.cpp | 54 |
1 files changed, 39 insertions, 15 deletions
diff --git a/src/tools/wasm-ctor-eval.cpp b/src/tools/wasm-ctor-eval.cpp index 867335a20..e6883e351 100644 --- a/src/tools/wasm-ctor-eval.cpp +++ b/src/tools/wasm-ctor-eval.cpp @@ -33,6 +33,7 @@ #include "pass.h" #include "support/colors.h" #include "support/file.h" +#include "support/string.h" #include "tool-options.h" #include "wasm-builder.h" #include "wasm-interpreter.h" @@ -645,7 +646,12 @@ bool evalCtor(EvallingModuleInstance& instance, } // Eval all ctors in a module. -void evalCtors(Module& wasm, std::vector<std::string> ctors) { +void evalCtors(Module& wasm, + std::vector<std::string>& ctors, + std::vector<std::string>& keptExports) { + std::unordered_set<std::string> keptExportsSet(keptExports.begin(), + keptExports.end()); + std::map<Name, std::shared_ptr<EvallingModuleInstance>> linkedInstances; // build and link the env module @@ -683,7 +689,20 @@ void evalCtors(Module& wasm, std::vector<std::string> ctors) { // Success! Remove the export, and continue. std::cout << " ...success on " << ctor << ".\n"; - wasm.removeExport(ctor); + + // Remove the export if we should. + auto* exp = wasm.getExport(ctor); + if (!keptExportsSet.count(ctor)) { + wasm.removeExport(exp->name); + } else { + // We are keeping around the export, which should now refer to an + // empty function since calling the export should do nothing. + auto* func = wasm.getFunction(exp->value); + auto copyName = Names::getValidFunctionName(wasm, func->name); + auto* copyFunc = ModuleUtils::copyFunction(func, wasm, copyName); + copyFunc->body = Builder(wasm).makeNop(); + wasm.getExport(exp->name)->value = copyName; + } } } catch (FailToEvalException& fail) { // that's it, we failed to even create the instance @@ -704,7 +723,8 @@ int main(int argc, const char* argv[]) { std::vector<std::string> passes; bool emitBinary = true; bool debugInfo = false; - std::string ctorsString; + String::Split ctors; + String::Split keptExports; const std::string WasmCtorEvalOption = "wasm-ctor-eval options"; @@ -732,13 +752,24 @@ int main(int argc, const char* argv[]) { WasmCtorEvalOption, Options::Arguments::Zero, [&](Options* o, const std::string& arguments) { debugInfo = true; }) + .add("--ctors", + "-c", + "Comma-separated list of global constructor functions to evaluate", + WasmCtorEvalOption, + Options::Arguments::One, + [&](Options* o, const std::string& argument) { + ctors = String::Split(argument, ","); + }) .add( - "--ctors", - "-c", - "Comma-separated list of global constructor functions to evaluate", + "--kept-exports", + "-ke", + "Comma-separated list of ctors whose exports we keep around even if we " + "eval those ctors", WasmCtorEvalOption, Options::Arguments::One, - [&](Options* o, const std::string& argument) { ctorsString = argument; }) + [&](Options* o, const std::string& argument) { + keptExports = String::Split(argument, ","); + }) .add("--ignore-external-input", "-ipi", "Assumes no env vars are to be read, stdin is empty, etc.", @@ -777,14 +808,7 @@ int main(int argc, const char* argv[]) { Fatal() << "error in validating input"; } - // get list of ctors, and eval them - std::vector<std::string> ctors; - std::istringstream stream(ctorsString); - std::string temp; - while (std::getline(stream, temp, ',')) { - ctors.push_back(temp); - } - evalCtors(wasm, ctors); + evalCtors(wasm, ctors, keptExports); // Do some useful optimizations after the evalling { |