From 1631aad9a054bda308b06425fe36fd63ed3a15d5 Mon Sep 17 00:00:00 2001 From: YiYing He Date: Wed, 10 Feb 2021 03:31:11 +0800 Subject: [PATCH] [VM] Add load wasm function with AST::Module input. --- include/vm/vm.h | 11 ++++++----- lib/vm/vm.cpp | 17 ++++++++++++++++- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/include/vm/vm.h b/include/vm/vm.h index 4bdca39..e891f8a 100644 --- a/include/vm/vm.h +++ b/include/vm/vm.h @@ -45,6 +45,7 @@ public: Expect registerModule(std::string_view Name, const std::filesystem::path &Path); Expect registerModule(std::string_view Name, Span Code); + Expect registerModule(std::string_view Name, const AST::Module &Module); Expect registerModule(const Runtime::ImportObject &Obj); /// Rapidly load, validate, instantiate, and run wasm function. @@ -54,10 +55,14 @@ public: Expect> runWasmFile(Span Code, std::string_view Func, Span Params = {}); + Expect> + runWasmFile(const AST::Module &Module, std::string_view Func, + Span Params = {}); - /// Load given wasm file or wasm bytecode. + /// Load given wasm file, wasm bytecode, or wasm module. Expect loadWasm(const std::filesystem::path &Path); Expect loadWasm(Span Code); + Expect loadWasm(const AST::Module &Module); /// ======= Functions can be called after loaded stage. ======= /// Validate loaded wasm module. @@ -98,10 +103,6 @@ private: enum class VMStage : uint8_t { Inited, Loaded, Validated, Instantiated }; void initVM(); - Expect registerModule(std::string_view Name, const AST::Module &Module); - Expect> runWasmFile(const AST::Module &Module, - std::string_view Func, - Span Params); /// VM environment. const Configure &Config; diff --git a/lib/vm/vm.cpp b/lib/vm/vm.cpp index e214144..c9e035d 100644 --- a/lib/vm/vm.cpp +++ b/lib/vm/vm.cpp @@ -58,7 +58,6 @@ Expect VM::registerModule(std::string_view Name, Span Code) { Stage = VMStage::Validated; } /// Load module. - std::unique_ptr LoadedMod; if (auto Res = LoaderEngine.parseModule(Code)) { return registerModule(Name, *(*Res).get()); } else { @@ -77,6 +76,11 @@ Expect VM::registerModule(const Runtime::ImportObject &Obj) { Expect VM::registerModule(std::string_view Name, const AST::Module &Module) { + if (Stage == VMStage::Instantiated) { + /// When registering module, instantiated module in store will be reset. + /// Therefore the instantiation should restart. + Stage = VMStage::Validated; + } /// Validate module. if (auto Res = ValidatorEngine.validate(Module); !Res) { return Unexpect(Res); @@ -119,6 +123,11 @@ Expect> VM::runWasmFile(Span Code, Expect> VM::runWasmFile(const AST::Module &Module, std::string_view Func, Span Params) { + if (Stage == VMStage::Instantiated) { + /// When running another module, instantiated module in store will be reset. + /// Therefore the instantiation should restart. + Stage = VMStage::Validated; + } if (auto Res = ValidatorEngine.validate(Module); !Res) { return Unexpect(Res); } @@ -161,6 +170,12 @@ Expect VM::loadWasm(Span Code) { return {}; } +Expect VM::loadWasm(const AST::Module &Module) { + Mod = std::make_unique(Module); + Stage = VMStage::Loaded; + return {}; +} + Expect VM::validate() { if (Stage < VMStage::Loaded) { /// When module is not loaded, not validate. -- GitLab