@@ -103,13 +103,6 @@ RegisterForeignFunction::RegisterForeignFunction(std::string name, WasmForeignFu
103103 (*foreign_functions)[name] = f;
104104}
105105
106- WasmBase::WasmBase (std::unique_ptr<WasmVm> wasm_vm, string_view vm_id, string_view vm_configuration,
107- string_view vm_key)
108- : vm_id_(std::string(vm_id)), vm_key_(std::string(vm_key)), wasm_vm_(std::move(wasm_vm)),
109- vm_configuration_ (std::string(vm_configuration)) {}
110-
111- WasmBase::~WasmBase () {}
112-
113106void WasmBase::registerCallbacks () {
114107#define _REGISTER (_fn ) \
115108 wasm_vm_->registerCallback ( \
@@ -241,7 +234,7 @@ void WasmBase::getFunctions() {
241234#undef _GET_PROXY
242235
243236 if (!malloc_) {
244- error (" WASM missing malloc" );
237+ error (" Wasm missing malloc" );
245238 }
246239}
247240
@@ -255,12 +248,15 @@ WasmBase::WasmBase(const std::shared_ptr<WasmHandleBase> &base_wasm_handle, Wasm
255248 } else {
256249 wasm_vm_ = factory ();
257250 }
258- if (!initialize (base_wasm_handle->wasm ()->code (),
259- base_wasm_handle->wasm ()->allow_precompiled ())) {
260- error (" Failed to load WASM code" );
261- }
262251}
263252
253+ WasmBase::WasmBase (std::unique_ptr<WasmVm> wasm_vm, string_view vm_id, string_view vm_configuration,
254+ string_view vm_key)
255+ : vm_id_(std::string(vm_id)), vm_key_(std::string(vm_key)), wasm_vm_(std::move(wasm_vm)),
256+ vm_configuration_(std::string(vm_configuration)) {}
257+
258+ WasmBase::~WasmBase () {}
259+
264260bool WasmBase::initialize (const std::string &code, bool allow_precompiled) {
265261 if (!wasm_vm_) {
266262 return false ;
@@ -354,11 +350,13 @@ ContextBase *WasmBase::start(std::shared_ptr<PluginBase> plugin) {
354350 auto context = std::unique_ptr<ContextBase>(createContext (plugin));
355351 auto context_ptr = context.get ();
356352 root_contexts_[root_id] = std::move (context);
357- context_ptr->onStart (plugin);
353+ if (!context_ptr->onStart (plugin)) {
354+ return nullptr ;
355+ }
358356 return context_ptr;
359357};
360358
361- void WasmBase::startForTesting (std::unique_ptr<ContextBase> context,
359+ bool WasmBase::startForTesting (std::unique_ptr<ContextBase> context,
362360 std::shared_ptr<PluginBase> plugin) {
363361 auto context_ptr = context.get ();
364362 if (!context->wasm_ ) {
@@ -367,7 +365,7 @@ void WasmBase::startForTesting(std::unique_ptr<ContextBase> context,
367365 }
368366 root_contexts_[plugin->root_id_ ] = std::move (context);
369367 // Set the current plugin over the lifetime of the onConfigure call to the RootContext.
370- context_ptr->onStart (plugin);
368+ return context_ptr->onStart (plugin) != 0 ;
371369}
372370
373371uint32_t WasmBase::allocContextId () {
@@ -445,44 +443,72 @@ std::shared_ptr<WasmHandleBase> createWasm(std::string vm_key, std::string code,
445443 std::shared_ptr<PluginBase> plugin,
446444 WasmHandleFactory factory, bool allow_precompiled,
447445 std::unique_ptr<ContextBase> root_context_for_testing) {
448- std::shared_ptr<WasmHandleBase> wasm ;
446+ std::shared_ptr<WasmHandleBase> wasm_handle ;
449447 {
450448 std::lock_guard<std::mutex> guard (base_wasms_mutex);
451449 if (!base_wasms) {
452450 base_wasms = new std::remove_reference<decltype (*base_wasms)>::type;
453451 }
454452 auto it = base_wasms->find (vm_key);
455453 if (it != base_wasms->end ()) {
456- wasm = it->second .lock ();
457- if (!wasm ) {
454+ wasm_handle = it->second .lock ();
455+ if (!wasm_handle ) {
458456 base_wasms->erase (it);
459457 }
460458 }
461- if (wasm)
462- return wasm;
463- wasm = factory (vm_key);
464- (*base_wasms)[vm_key] = wasm;
459+ if (wasm_handle) {
460+ return wasm_handle;
461+ }
462+ wasm_handle = factory (vm_key);
463+ if (!wasm_handle) {
464+ return nullptr ;
465+ }
466+ (*base_wasms)[vm_key] = wasm_handle;
465467 }
466468
467- if (!wasm ->wasm ()->initialize (code, allow_precompiled)) {
468- wasm ->wasm ()->error (" Failed to initialize WASM code" );
469+ if (!wasm_handle ->wasm ()->initialize (code, allow_precompiled)) {
470+ wasm_handle ->wasm ()->error (" Failed to initialize Wasm code" );
469471 return nullptr ;
470472 }
473+ ContextBase *root_context = root_context_for_testing.get ();
471474 if (!root_context_for_testing) {
472- wasm->wasm ()->start (plugin);
475+ root_context = wasm_handle->wasm ()->start (plugin);
476+ if (!root_context) {
477+ wasm_handle->wasm ()->error (" Failed to start base Wasm" );
478+ return nullptr ;
479+ }
473480 } else {
474- wasm->wasm ()->startForTesting (std::move (root_context_for_testing), plugin);
481+ if (!wasm_handle->wasm ()->startForTesting (std::move (root_context_for_testing), plugin)) {
482+ wasm_handle->wasm ()->error (" Failed to start base Wasm" );
483+ return nullptr ;
484+ }
475485 }
476- return wasm;
486+ if (!wasm_handle->wasm ()->configure (root_context, plugin)) {
487+ wasm_handle->wasm ()->error (" Failed to configure base Wasm plugin" );
488+ return nullptr ;
489+ }
490+ return wasm_handle;
477491};
478492
479493static std::shared_ptr<WasmHandleBase>
480494createThreadLocalWasm (std::shared_ptr<WasmHandleBase> &base_wasm,
481495 std::shared_ptr<PluginBase> plugin, WasmHandleCloneFactory factory) {
482496 auto wasm_handle = factory (base_wasm);
497+ if (!wasm_handle) {
498+ return nullptr ;
499+ }
500+ if (!wasm_handle->wasm ()->initialize (base_wasm->wasm ()->code (),
501+ base_wasm->wasm ()->allow_precompiled ())) {
502+ base_wasm->wasm ()->error (" Failed to load Wasm code" );
503+ return nullptr ;
504+ }
483505 ContextBase *root_context = wasm_handle->wasm ()->start (plugin);
506+ if (!root_context) {
507+ base_wasm->wasm ()->error (" Failed to start thread-local Wasm" );
508+ return nullptr ;
509+ }
484510 if (!wasm_handle->wasm ()->configure (root_context, plugin)) {
485- base_wasm->wasm ()->error (" Failed to configure WASM code " );
511+ base_wasm->wasm ()->error (" Failed to configure thread-local Wasm plugin " );
486512 return nullptr ;
487513 }
488514 local_wasms[std::string (wasm_handle->wasm ()->vm_key ())] = wasm_handle;
@@ -508,7 +534,7 @@ getOrCreateThreadLocalWasm(std::shared_ptr<WasmHandleBase> base_wasm,
508534 if (wasm_handle) {
509535 auto root_context = wasm_handle->wasm ()->getOrCreateRootContext (plugin);
510536 if (!wasm_handle->wasm ()->configure (root_context, plugin)) {
511- base_wasm->wasm ()->error (" Failed to configure WASM code" );
537+ base_wasm->wasm ()->error (" Failed to configure thread-local Wasm code" );
512538 return nullptr ;
513539 }
514540 return wasm_handle;
0 commit comments