@@ -71,6 +71,7 @@ class WasmBase : public std::enable_shared_from_this<WasmBase> {
7171 return nullptr ;
7272 }
7373 uint32_t allocContextId ();
74+ bool isFailed () { return failed_; }
7475
7576 const std::string &code () const { return code_; }
7677 const std::string &vm_configuration () const ;
@@ -91,20 +92,17 @@ class WasmBase : public std::enable_shared_from_this<WasmBase> {
9192 unimplemented ();
9293 return nullptr ;
9394 }
94- // NB: if plugin is nullptr, then a VM Context is returned.
95- virtual ContextBase *createContext (std::shared_ptr<PluginBase> plugin) {
96- if (plugin)
97- return new ContextBase (this , plugin);
98- return new ContextBase (this );
95+
96+ virtual ContextBase *createVmContext () { return new ContextBase (this ); }
97+ virtual ContextBase *createRootContext (const std::shared_ptr<PluginBase> &plugin) {
98+ return new ContextBase (this , plugin);
99+ }
100+ virtual ContextBase *createContext (const std::shared_ptr<PluginBase> &plugin) {
101+ return new ContextBase (this , plugin);
99102 }
100103 virtual void setTimerPeriod (uint32_t root_context_id, std::chrono::milliseconds period) {
101104 timer_period_[root_context_id] = period;
102105 }
103- virtual void error (string_view message) {
104- std::cerr << message << " \n " ;
105- abort ();
106- }
107- virtual void unimplemented () { error (" unimplemented proxy-wasm API" ); }
108106
109107 // Support functions.
110108 //
@@ -117,12 +115,12 @@ class WasmBase : public std::enable_shared_from_this<WasmBase> {
117115
118116 WasmForeignFunction getForeignFunction (string_view function_name);
119117
120- // For testing.
121- //
122- void setContextForTesting (ContextBase *context) { contexts_[context-> id ()] = context; }
123- // Returns false if onStart returns false.
124- bool startForTesting ( std::unique_ptr<ContextBase> root_context,
125- std::shared_ptr<PluginBase> plugin);
118+ void fail (string_view message) {
119+ error (message);
120+ failed_ = true ;
121+ }
122+ virtual void error (string_view message) { std::cerr << message << " \n " ; }
123+ virtual void unimplemented () { error ( " unimplemented proxy-wasm API " ); }
126124
127125 bool getEmscriptenVersion (uint32_t *emscripten_metadata_major_version,
128126 uint32_t *emscripten_metadata_minor_version,
@@ -238,6 +236,7 @@ class WasmBase : public std::enable_shared_from_this<WasmBase> {
238236 std::string code_;
239237 std::string vm_configuration_;
240238 bool allow_precompiled_ = false ;
239+ bool failed_ = false ; // The Wasm VM has experienced a fatal error.
241240
242241 bool is_emscripten_ = false ;
243242 uint32_t emscripten_metadata_major_version_ = 0 ;
@@ -259,7 +258,13 @@ class WasmBase : public std::enable_shared_from_this<WasmBase> {
259258class WasmHandleBase : public std ::enable_shared_from_this<WasmHandleBase> {
260259public:
261260 explicit WasmHandleBase (std::shared_ptr<WasmBase> wasm_base) : wasm_base_(wasm_base) {}
262- ~WasmHandleBase () { wasm_base_->startShutdown (); }
261+ ~WasmHandleBase () {
262+ if (wasm_base_) {
263+ wasm_base_->startShutdown ();
264+ }
265+ }
266+
267+ void kill () { wasm_base_ = nullptr ; }
263268
264269 std::shared_ptr<WasmBase> &wasm () { return wasm_base_; }
265270
@@ -276,8 +281,7 @@ using WasmHandleCloneFactory =
276281// Returns nullptr on failure (i.e. initialization of the VM fails).
277282std::shared_ptr<WasmHandleBase>
278283createWasm (std::string vm_key, std::string code, std::shared_ptr<PluginBase> plugin,
279- WasmHandleFactory factory, bool allow_precompiled,
280- std::unique_ptr<ContextBase> root_context_for_testing = nullptr );
284+ WasmHandleFactory factory, WasmHandleCloneFactory clone_factory, bool allow_precompiled);
281285// Get an existing ThreadLocal VM matching 'vm_id' or nullptr if there isn't one.
282286std::shared_ptr<WasmHandleBase> getThreadLocalWasm (string_view vm_id);
283287// Get an existing ThreadLocal VM matching 'vm_id' or create one using 'base_wavm' by cloning or by
@@ -286,6 +290,9 @@ std::shared_ptr<WasmHandleBase>
286290getOrCreateThreadLocalWasm (std::shared_ptr<WasmHandleBase> base_wasm,
287291 std::shared_ptr<PluginBase> plugin, WasmHandleCloneFactory factory);
288292
293+ // Clear Base Wasm cache and the thread-local Wasm sandbox cache for the calling thread.
294+ void clearWasmCachesForTesting ();
295+
289296inline const std::string &WasmBase::vm_configuration () const {
290297 if (base_wasm_handle_)
291298 return base_wasm_handle_->wasm ()->vm_configuration_ ;
0 commit comments