@@ -58,8 +58,8 @@ class Wamr : public WasmVm {
58
58
std::string_view getEngineName () override { return " wamr" ; }
59
59
std::string_view getPrecompiledSectionName () override { return " " ; }
60
60
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 ;
63
63
64
64
bool load (std::string_view bytecode, std::string_view precompiled,
65
65
const std::unordered_map<uint32_t , std::string> &function_names) override ;
@@ -108,6 +108,7 @@ class Wamr : public WasmVm {
108
108
109
109
WasmStorePtr store_;
110
110
WasmModulePtr module_;
111
+ WasmSharedModulePtr shared_module_;
111
112
WasmInstancePtr instance_;
112
113
113
114
WasmMemoryPtr memory_;
@@ -132,9 +133,41 @@ bool Wamr::load(std::string_view bytecode, std::string_view /*precompiled*/,
132
133
return false ;
133
134
}
134
135
136
+ shared_module_ = wasm_module_share (module_.get ());
137
+ if (shared_module_ == nullptr ) {
138
+ return false ;
139
+ }
140
+
135
141
return true ;
136
142
}
137
143
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
+
138
171
static bool equalValTypes (const wasm_valtype_vec_t *left, const wasm_valtype_vec_t *right) {
139
172
if (left->size != right->size ) {
140
173
return false ;
0 commit comments