@@ -322,20 +322,20 @@ string_view V8::getCustomSection(string_view name) {
322322 const byte_t *end = source_.get () + source_.size ();
323323 while (pos < end) {
324324 if (pos + 1 > end) {
325- fail (" Failed to parse corrupted Wasm module" );
325+ fail (FailState::UnableToInitializeCode, " Failed to parse corrupted Wasm module" );
326326 return " " ;
327327 }
328328 const auto section_type = *pos++;
329329 const auto section_len = parseVarint (pos, end);
330330 if (section_len == static_cast <uint32_t >(-1 ) || pos + section_len > end) {
331- fail (" Failed to parse corrupted Wasm module" );
331+ fail (FailState::UnableToInitializeCode, " Failed to parse corrupted Wasm module" );
332332 return " " ;
333333 }
334334 if (section_type == 0 /* custom section */ ) {
335335 const auto section_data_start = pos;
336336 const auto section_name_len = parseVarint (pos, end);
337337 if (section_name_len == static_cast <uint32_t >(-1 ) || pos + section_name_len > end) {
338- fail (" Failed to parse corrupted Wasm module" );
338+ fail (FailState::UnableToInitializeCode, " Failed to parse corrupted Wasm module" );
339339 return " " ;
340340 }
341341 if (section_name_len == name.size () && ::memcmp (pos, name.data (), section_name_len) == 0 ) {
@@ -382,28 +382,31 @@ bool V8::link(string_view debug_name) {
382382 case wasm::EXTERN_FUNC: {
383383 auto it = host_functions_.find (std::string (module ) + " ." + std::string (name));
384384 if (it == host_functions_.end ()) {
385- fail (std::string (" Failed to load Wasm module due to a missing import: " ) +
386- std::string (module ) + " ." + std::string (name));
385+ fail (FailState::UnableToInitializeCode,
386+ std::string (" Failed to load Wasm module due to a missing import: " ) +
387+ std::string (module ) + " ." + std::string (name));
387388 break ;
388389 }
389390 auto func = it->second .get ()->callback_ .get ();
390391 if (!equalValTypes (import_type->func ()->params (), func->type ()->params ()) ||
391392 !equalValTypes (import_type->func ()->results (), func->type ()->results ())) {
392- fail (std::string (" Failed to load Wasm module due to an import type mismatch: " ) +
393- std::string (module ) + " ." + std::string (name) +
394- " , want: " + printValTypes (import_type->func ()->params ()) + " -> " +
395- printValTypes (import_type->func ()->results ()) +
396- " , but host exports: " + printValTypes (func->type ()->params ()) + " -> " +
397- printValTypes (func->type ()->results ()));
393+ fail (FailState::UnableToInitializeCode,
394+ std::string (" Failed to load Wasm module due to an import type mismatch: " ) +
395+ std::string (module ) + " ." + std::string (name) +
396+ " , want: " + printValTypes (import_type->func ()->params ()) + " -> " +
397+ printValTypes (import_type->func ()->results ()) +
398+ " , but host exports: " + printValTypes (func->type ()->params ()) + " -> " +
399+ printValTypes (func->type ()->results ()));
398400 break ;
399401 }
400402 imports.push_back (func);
401403 } break ;
402404
403405 case wasm::EXTERN_GLOBAL: {
404406 // TODO(PiotrSikora): add support when/if needed.
405- fail (" Failed to load Wasm module due to a missing import: " + std::string (module ) + " ." +
406- std::string (name));
407+ fail (FailState::UnableToInitializeCode,
408+ " Failed to load Wasm module due to a missing import: " + std::string (module ) + " ." +
409+ std::string (name));
407410 } break ;
408411
409412 case wasm::EXTERN_MEMORY: {
@@ -565,7 +568,8 @@ void V8::getModuleFunctionImpl(string_view function_name,
565568 const wasm::Func *func = it->second .get ();
566569 if (!equalValTypes (func->type ()->params (), convertArgsTupleToValTypes<std::tuple<Args...>>()) ||
567570 !equalValTypes (func->type ()->results (), convertArgsTupleToValTypes<std::tuple<>>())) {
568- fail (std::string (" Bad function signature for: " ) + std::string (function_name));
571+ fail (FailState::UnableToInitializeCode,
572+ std::string (" Bad function signature for: " ) + std::string (function_name));
569573 *function = nullptr ;
570574 return ;
571575 }
@@ -574,8 +578,8 @@ void V8::getModuleFunctionImpl(string_view function_name,
574578 SaveRestoreContext saved_context (context);
575579 auto trap = func->call (params, nullptr );
576580 if (trap) {
577- fail (" Function: " + std::string (function_name) +
578- " failed: " + std::string (trap->message ().get (), trap->message ().size ()));
581+ fail (FailState::RuntimeError, " Function: " + std::string (function_name) + " failed: " +
582+ std::string (trap->message ().get (), trap->message ().size ()));
579583 }
580584 };
581585}
@@ -591,7 +595,8 @@ void V8::getModuleFunctionImpl(string_view function_name,
591595 const wasm::Func *func = it->second .get ();
592596 if (!equalValTypes (func->type ()->params (), convertArgsTupleToValTypes<std::tuple<Args...>>()) ||
593597 !equalValTypes (func->type ()->results (), convertArgsTupleToValTypes<std::tuple<R>>())) {
594- fail (" Bad function signature for: " + std::string (function_name));
598+ fail (FailState::UnableToInitializeCode,
599+ " Bad function signature for: " + std::string (function_name));
595600 *function = nullptr ;
596601 return ;
597602 }
@@ -601,8 +606,8 @@ void V8::getModuleFunctionImpl(string_view function_name,
601606 SaveRestoreContext saved_context (context);
602607 auto trap = func->call (params, results);
603608 if (trap) {
604- fail (" Function: " + std::string (function_name) +
605- " failed: " + std::string (trap->message ().get (), trap->message ().size ()));
609+ fail (FailState::RuntimeError, " Function: " + std::string (function_name) + " failed: " +
610+ std::string (trap->message ().get (), trap->message ().size ()));
606611 return R{};
607612 }
608613 R rvalue = results[0 ].get <typename ConvertWordTypeToUint32<R>::type>();
0 commit comments