@@ -446,12 +446,38 @@ void WasmBase::finishShutdown() {
446
446
}
447
447
}
448
448
449
+ bool canary (std::shared_ptr<WasmHandleBase> &wasm_handle, const std::shared_ptr<PluginBase> &plugin,
450
+ const WasmHandleCloneFactory &clone_factory) {
451
+ auto configuration_canary_handle = clone_factory (wasm_handle);
452
+ if (!configuration_canary_handle) {
453
+ wasm_handle->wasm ()->fail (FailState::UnableToCloneVm, " Failed to clone Base Wasm" );
454
+ return false ;
455
+ }
456
+ if (!configuration_canary_handle->wasm ()->initialize ()) {
457
+ configuration_canary_handle->wasm ()->fail (FailState::UnableToInitializeCode, " Failed to initialize Wasm code" );
458
+ return false ;
459
+ }
460
+ auto *root_context = configuration_canary_handle->wasm ()->start (plugin);
461
+ if (root_context == nullptr ) {
462
+ configuration_canary_handle->wasm ()->fail (FailState::StartFailed, " Failed to start base Wasm" );
463
+ return false ;
464
+ }
465
+ if (!configuration_canary_handle->wasm ()->configure (root_context, plugin)) {
466
+ configuration_canary_handle->wasm ()->fail (FailState::ConfigureFailed,
467
+ " Failed to configure base Wasm plugin" );
468
+ return false ;
469
+ }
470
+ configuration_canary_handle->kill ();
471
+ return true ;
472
+ }
473
+
449
474
std::shared_ptr<WasmHandleBase> createWasm (const std::string &vm_key, const std::string &code,
450
475
const std::shared_ptr<PluginBase> &plugin,
451
476
const WasmHandleFactory &factory,
452
477
const WasmHandleCloneFactory &clone_factory,
453
478
bool allow_precompiled) {
454
479
std::shared_ptr<WasmHandleBase> wasm_handle;
480
+ bool is_new_wasm = false ;
455
481
{
456
482
std::lock_guard<std::mutex> guard (base_wasms_mutex);
457
483
if (base_wasms == nullptr ) {
@@ -464,44 +490,31 @@ std::shared_ptr<WasmHandleBase> createWasm(const std::string &vm_key, const std:
464
490
base_wasms->erase (it);
465
491
}
466
492
}
467
- if (wasm_handle) {
468
- return wasm_handle;
469
- }
470
- wasm_handle = factory (vm_key);
471
- if (!wasm_handle) {
472
- return nullptr ;
493
+ if (!wasm_handle) {
494
+ wasm_handle = factory (vm_key);
495
+ if (!wasm_handle) {
496
+ return nullptr ;
497
+ }
498
+ is_new_wasm = true ;
499
+ (*base_wasms)[vm_key] = wasm_handle;
473
500
}
474
- (*base_wasms)[vm_key] = wasm_handle;
475
501
}
476
502
477
- if (!wasm_handle->wasm ()->load (code, allow_precompiled)) {
478
- wasm_handle->wasm ()->fail (FailState::UnableToInitializeCode, " Failed to load Wasm code" );
479
- return nullptr ;
480
- }
481
- if (!wasm_handle->wasm ()->initialize ()) {
482
- wasm_handle->wasm ()->fail (FailState::UnableToInitializeCode, " Failed to initialize Wasm code" );
483
- return nullptr ;
484
- }
485
- auto configuration_canary_handle = clone_factory (wasm_handle);
486
- if (!configuration_canary_handle) {
487
- wasm_handle->wasm ()->fail (FailState::UnableToCloneVm, " Failed to clone Base Wasm" );
488
- return nullptr ;
489
- }
490
- if (!configuration_canary_handle->wasm ()->initialize ()) {
491
- wasm_handle->wasm ()->fail (FailState::UnableToInitializeCode, " Failed to initialize Wasm code" );
492
- return nullptr ;
493
- }
494
- auto *root_context = configuration_canary_handle->wasm ()->start (plugin);
495
- if (root_context == nullptr ) {
496
- configuration_canary_handle->wasm ()->fail (FailState::StartFailed, " Failed to start base Wasm" );
497
- return nullptr ;
503
+ if (is_new_wasm) {
504
+ if (!wasm_handle->wasm ()->load (code, allow_precompiled)) {
505
+ wasm_handle->wasm ()->fail (FailState::UnableToInitializeCode, " Failed to load Wasm code" );
506
+ return nullptr ;
507
+ }
508
+ if (!wasm_handle->wasm ()->initialize ()) {
509
+ wasm_handle->wasm ()->fail (FailState::UnableToInitializeCode, " Failed to initialize Wasm code" );
510
+ return nullptr ;
511
+ }
498
512
}
499
- if (!configuration_canary_handle->wasm ()->configure (root_context, plugin)) {
500
- configuration_canary_handle->wasm ()->fail (FailState::ConfigureFailed,
501
- " Failed to configure base Wasm plugin" );
513
+
514
+ if (!canary (wasm_handle, plugin, clone_factory)) {
502
515
return nullptr ;
503
516
}
504
- configuration_canary_handle-> kill ();
517
+
505
518
return wasm_handle;
506
519
};
507
520
0 commit comments