提交 1631aad9 编写于 作者: Y YiYing He 提交者: Yi-Ying He

[VM] Add load wasm function with AST::Module input.

上级 1c815996
...@@ -45,6 +45,7 @@ public: ...@@ -45,6 +45,7 @@ public:
Expect<void> registerModule(std::string_view Name, Expect<void> registerModule(std::string_view Name,
const std::filesystem::path &Path); const std::filesystem::path &Path);
Expect<void> registerModule(std::string_view Name, Span<const Byte> Code); Expect<void> registerModule(std::string_view Name, Span<const Byte> Code);
Expect<void> registerModule(std::string_view Name, const AST::Module &Module);
Expect<void> registerModule(const Runtime::ImportObject &Obj); Expect<void> registerModule(const Runtime::ImportObject &Obj);
/// Rapidly load, validate, instantiate, and run wasm function. /// Rapidly load, validate, instantiate, and run wasm function.
...@@ -54,10 +55,14 @@ public: ...@@ -54,10 +55,14 @@ public:
Expect<std::vector<ValVariant>> Expect<std::vector<ValVariant>>
runWasmFile(Span<const Byte> Code, std::string_view Func, runWasmFile(Span<const Byte> Code, std::string_view Func,
Span<const ValVariant> Params = {}); Span<const ValVariant> Params = {});
Expect<std::vector<ValVariant>>
runWasmFile(const AST::Module &Module, std::string_view Func,
Span<const ValVariant> Params = {});
/// Load given wasm file or wasm bytecode. /// Load given wasm file, wasm bytecode, or wasm module.
Expect<void> loadWasm(const std::filesystem::path &Path); Expect<void> loadWasm(const std::filesystem::path &Path);
Expect<void> loadWasm(Span<const Byte> Code); Expect<void> loadWasm(Span<const Byte> Code);
Expect<void> loadWasm(const AST::Module &Module);
/// ======= Functions can be called after loaded stage. ======= /// ======= Functions can be called after loaded stage. =======
/// Validate loaded wasm module. /// Validate loaded wasm module.
...@@ -98,10 +103,6 @@ private: ...@@ -98,10 +103,6 @@ private:
enum class VMStage : uint8_t { Inited, Loaded, Validated, Instantiated }; enum class VMStage : uint8_t { Inited, Loaded, Validated, Instantiated };
void initVM(); void initVM();
Expect<void> registerModule(std::string_view Name, const AST::Module &Module);
Expect<std::vector<ValVariant>> runWasmFile(const AST::Module &Module,
std::string_view Func,
Span<const ValVariant> Params);
/// VM environment. /// VM environment.
const Configure &Config; const Configure &Config;
......
...@@ -58,7 +58,6 @@ Expect<void> VM::registerModule(std::string_view Name, Span<const Byte> Code) { ...@@ -58,7 +58,6 @@ Expect<void> VM::registerModule(std::string_view Name, Span<const Byte> Code) {
Stage = VMStage::Validated; Stage = VMStage::Validated;
} }
/// Load module. /// Load module.
std::unique_ptr<AST::Module> LoadedMod;
if (auto Res = LoaderEngine.parseModule(Code)) { if (auto Res = LoaderEngine.parseModule(Code)) {
return registerModule(Name, *(*Res).get()); return registerModule(Name, *(*Res).get());
} else { } else {
...@@ -77,6 +76,11 @@ Expect<void> VM::registerModule(const Runtime::ImportObject &Obj) { ...@@ -77,6 +76,11 @@ Expect<void> VM::registerModule(const Runtime::ImportObject &Obj) {
Expect<void> VM::registerModule(std::string_view Name, Expect<void> VM::registerModule(std::string_view Name,
const AST::Module &Module) { 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. /// Validate module.
if (auto Res = ValidatorEngine.validate(Module); !Res) { if (auto Res = ValidatorEngine.validate(Module); !Res) {
return Unexpect(Res); return Unexpect(Res);
...@@ -119,6 +123,11 @@ Expect<std::vector<ValVariant>> VM::runWasmFile(Span<const Byte> Code, ...@@ -119,6 +123,11 @@ Expect<std::vector<ValVariant>> VM::runWasmFile(Span<const Byte> Code,
Expect<std::vector<ValVariant>> VM::runWasmFile(const AST::Module &Module, Expect<std::vector<ValVariant>> VM::runWasmFile(const AST::Module &Module,
std::string_view Func, std::string_view Func,
Span<const ValVariant> Params) { Span<const ValVariant> 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) { if (auto Res = ValidatorEngine.validate(Module); !Res) {
return Unexpect(Res); return Unexpect(Res);
} }
...@@ -161,6 +170,12 @@ Expect<void> VM::loadWasm(Span<const Byte> Code) { ...@@ -161,6 +170,12 @@ Expect<void> VM::loadWasm(Span<const Byte> Code) {
return {}; return {};
} }
Expect<void> VM::loadWasm(const AST::Module &Module) {
Mod = std::make_unique<AST::Module>(Module);
Stage = VMStage::Loaded;
return {};
}
Expect<void> VM::validate() { Expect<void> VM::validate() {
if (Stage < VMStage::Loaded) { if (Stage < VMStage::Loaded) {
/// When module is not loaded, not validate. /// When module is not loaded, not validate.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册