@@ -58,8 +58,8 @@ class Wamr : public WasmVm {
5858 std::string_view getEngineName () override { return " wamr" ; }
5959 std::string_view getPrecompiledSectionName () override { return " " ; }
6060
61- Cloneable cloneable () override { return Cloneable::NotCloneable ; }
62- std::unique_ptr<WasmVm> clone () override { return nullptr ; }
61+ Cloneable cloneable () override { return Cloneable::CompiledBytecode ; }
62+ std::unique_ptr<WasmVm> clone () override ;
6363
6464 bool load (std::string_view bytecode, std::string_view precompiled,
6565 const std::unordered_map<uint32_t , std::string> &function_names) override ;
@@ -108,6 +108,7 @@ class Wamr : public WasmVm {
108108
109109 WasmStorePtr store_;
110110 WasmModulePtr module_;
111+ WasmSharedModulePtr shared_module_;
111112 WasmInstancePtr instance_;
112113
113114 WasmMemoryPtr memory_;
@@ -132,9 +133,41 @@ bool Wamr::load(std::string_view bytecode, std::string_view /*precompiled*/,
132133 return false ;
133134 }
134135
136+ shared_module_ = wasm_module_share (module_.get ());
137+ if (shared_module_ == nullptr ) {
138+ return false ;
139+ }
140+
135141 return true ;
136142}
137143
144+ std::unique_ptr<WasmVm> Wamr::clone () {
145+ assert (module_ != nullptr );
146+
147+ auto vm = std::make_unique<Wamr>();
148+ if (vm == nullptr ) {
149+ return nullptr ;
150+ }
151+
152+ vm->store_ = wasm_store_new (engine ());
153+ if (vm->store_ == nullptr ) {
154+ return nullptr ;
155+ }
156+
157+ vm->module_ = wasm_module_obtain (vm->store_ .get (), shared_module_.get ());
158+ if (vm->module_ == nullptr ) {
159+ return nullptr ;
160+ }
161+
162+ auto *integration_clone = integration ()->clone ();
163+ if (integration_clone == nullptr ) {
164+ return nullptr ;
165+ }
166+ vm->integration ().reset (integration_clone);
167+
168+ return vm;
169+ }
170+
138171static bool equalValTypes (const wasm_valtype_vec_t *left, const wasm_valtype_vec_t *right) {
139172 if (left->size != right->size ) {
140173 return false ;
0 commit comments