@@ -59,8 +59,7 @@ class WasmBase : public std::enable_shared_from_this<WasmBase> {
59
59
std::string_view vm_key () const { return vm_key_; }
60
60
WasmVm *wasm_vm () const { return wasm_vm_.get (); }
61
61
ContextBase *vm_context () const { return vm_context_.get (); }
62
- ContextBase *getRootContext (std::string_view root_id);
63
- ContextBase *getOrCreateRootContext (const std::shared_ptr<PluginBase> &plugin);
62
+ ContextBase *getRootContext (const std::shared_ptr<PluginBase> &plugin, bool allow_closed);
64
63
ContextBase *getContext (uint32_t id) {
65
64
auto it = contexts_.find (id);
66
65
if (it != contexts_.end ())
@@ -78,6 +77,7 @@ class WasmBase : public std::enable_shared_from_this<WasmBase> {
78
77
void timerReady (uint32_t root_context_id);
79
78
void queueReady (uint32_t root_context_id, uint32_t token);
80
79
80
+ void startShutdown (std::string_view plugin_key);
81
81
void startShutdown ();
82
82
WasmResult done (ContextBase *root_context);
83
83
void finishShutdown ();
@@ -170,11 +170,12 @@ class WasmBase : public std::enable_shared_from_this<WasmBase> {
170
170
uint32_t next_context_id_ = 1 ; // 0 is reserved for the VM context.
171
171
std::shared_ptr<ContextBase> vm_context_; // Context unrelated to any specific root or stream
172
172
// (e.g. for global constructors).
173
- std::unordered_map<std::string, std::unique_ptr<ContextBase>> root_contexts_;
173
+ std::unordered_map<std::string, std::unique_ptr<ContextBase>> root_contexts_; // Root contexts.
174
+ std::unordered_map<std::string, std::unique_ptr<ContextBase>> pending_done_; // Root contexts.
175
+ std::unordered_set<std::unique_ptr<ContextBase>> pending_delete_; // Root contexts.
174
176
std::unordered_map<uint32_t , ContextBase *> contexts_; // Contains all contexts.
175
177
std::unordered_map<uint32_t , std::chrono::milliseconds> timer_period_; // per root_id.
176
178
std::unique_ptr<ShutdownHandle> shutdown_handle_;
177
- std::unordered_set<ContextBase *> pending_done_; // Root contexts not done during shutdown.
178
179
179
180
WasmCallVoid<0 > _initialize_; /* Emscripten v1.39.17+ */
180
181
WasmCallVoid<0 > _start_; /* Emscripten v1.39.0+ */
@@ -275,11 +276,29 @@ createWasm(std::string vm_key, std::string code, std::shared_ptr<PluginBase> plu
275
276
WasmHandleFactory factory, WasmHandleCloneFactory clone_factory, bool allow_precompiled);
276
277
// Get an existing ThreadLocal VM matching 'vm_id' or nullptr if there isn't one.
277
278
std::shared_ptr<WasmHandleBase> getThreadLocalWasm (std::string_view vm_id);
279
+
280
+ class PluginHandleBase : public std ::enable_shared_from_this<PluginHandleBase> {
281
+ public:
282
+ explicit PluginHandleBase (std::shared_ptr<WasmHandleBase> wasm_handle,
283
+ std::string_view plugin_key)
284
+ : wasm_handle_(wasm_handle), plugin_key_(plugin_key) {}
285
+ ~PluginHandleBase () { wasm_handle_->wasm ()->startShutdown (plugin_key_); }
286
+
287
+ std::shared_ptr<WasmBase> &wasm () { return wasm_handle_->wasm (); }
288
+
289
+ protected:
290
+ std::shared_ptr<WasmHandleBase> wasm_handle_;
291
+ std::string plugin_key_;
292
+ };
293
+
294
+ using PluginHandleFactory = std::function<std::shared_ptr<PluginHandleBase>(
295
+ std::shared_ptr<WasmHandleBase> base_wasm, std::string_view plugin_key)>;
296
+
278
297
// Get an existing ThreadLocal VM matching 'vm_id' or create one using 'base_wavm' by cloning or by
279
298
// using it it as a template.
280
- std::shared_ptr<WasmHandleBase>
281
- getOrCreateThreadLocalWasm ( std::shared_ptr<WasmHandleBase> base_wasm,
282
- std::shared_ptr<PluginBase> plugin, WasmHandleCloneFactory factory );
299
+ std::shared_ptr<PluginHandleBase> getOrCreateThreadLocalPlugin (
300
+ std::shared_ptr<WasmHandleBase> base_wasm, std::shared_ptr<PluginBase> plugin ,
301
+ WasmHandleCloneFactory clone_factory, PluginHandleFactory plugin_factory );
283
302
284
303
// Clear Base Wasm cache and the thread-local Wasm sandbox cache for the calling thread.
285
304
void clearWasmCachesForTesting ();
0 commit comments