From fe99e3458f11d1a01fa3ad5b68883dbcba3903af Mon Sep 17 00:00:00 2001 From: Heejin Ahn Date: Fri, 31 May 2019 20:02:37 -0700 Subject: Add event section (#2151) This adds support for the event and the event section, as specified in https://github.com/WebAssembly/exception-handling/blob/master/proposals/Exceptions.md#changes-to-the-binary-model. Wasm events are features that suspend the current execution and transfer the control flow to a corresponding handler. Currently the only supported event kind is exceptions. For events, this includes support for - Binary file reading/writing - Wast file reading/writing - Binaryen.js API - Fuzzer - Validation - Metadce - Passes: metrics, minify-imports-and-exports, remove-unused-module-elements --- src/wasm/wasm.cpp | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) (limited to 'src/wasm/wasm.cpp') diff --git a/src/wasm/wasm.cpp b/src/wasm/wasm.cpp index c32ead836..c543686ed 100644 --- a/src/wasm/wasm.cpp +++ b/src/wasm/wasm.cpp @@ -82,6 +82,8 @@ Name SPECTEST("spectest"); Name PRINT("print"); Name EXIT("exit"); Name SHARED("shared"); +Name EVENT("event"); +Name ATTR("attr"); // Expressions @@ -908,11 +910,20 @@ Function* Module::getFunction(Name name) { Global* Module::getGlobal(Name name) { auto iter = globalsMap.find(name); if (iter == globalsMap.end()) { + assert(false); Fatal() << "Module::getGlobal: " << name << " does not exist"; } return iter->second; } +Event* Module::getEvent(Name name) { + auto iter = eventsMap.find(name); + if (iter == eventsMap.end()) { + Fatal() << "Module::getEvent: " << name << " does not exist"; + } + return iter->second; +} + FunctionType* Module::getFunctionTypeOrNull(Name name) { auto iter = functionTypesMap.find(name); if (iter == functionTypesMap.end()) { @@ -945,6 +956,14 @@ Global* Module::getGlobalOrNull(Name name) { return iter->second; } +Event* Module::getEventOrNull(Name name) { + auto iter = eventsMap.find(name); + if (iter == eventsMap.end()) { + return nullptr; + } + return iter->second; +} + FunctionType* Module::addFunctionType(std::unique_ptr curr) { if (!curr->name.is()) { Fatal() << "Module::addFunctionType: empty name"; @@ -1009,6 +1028,20 @@ Global* Module::addGlobal(Global* curr) { return curr; } +Event* Module::addEvent(Event* curr) { + if (!curr->name.is()) { + Fatal() << "Module::addEvent: empty name"; + } + if (getEventOrNull(curr->name)) { + Fatal() << "Module::addEvent: " << curr->name << " already exists"; + } + + events.emplace_back(curr); + + eventsMap[curr->name] = curr; + return curr; +} + void Module::addStart(const Name& s) { start = s; } void Module::removeFunctionType(Name name) { @@ -1051,6 +1084,16 @@ void Module::removeGlobal(Name name) { globalsMap.erase(name); } +void Module::removeEvent(Name name) { + for (size_t i = 0; i < events.size(); i++) { + if (events[i]->name == name) { + events.erase(events.begin() + i); + break; + } + } + eventsMap.erase(name); +} + // TODO: remove* for other elements void Module::updateMaps() { @@ -1070,6 +1113,10 @@ void Module::updateMaps() { for (auto& curr : globals) { globalsMap[curr->name] = curr.get(); } + eventsMap.clear(); + for (auto& curr : events) { + eventsMap[curr->name] = curr.get(); + } } void Module::clearDebugInfo() { debugInfoFileNames.clear(); } -- cgit v1.2.3