| 3761 | } |
| 3762 | |
| 3763 | Result WastParser::ParseModuleCommand(Script* script, CommandPtr* out_command) { |
| 3764 | WABT_TRACE(ParseModuleCommand); |
| 3765 | std::unique_ptr<ScriptModule> script_module; |
| 3766 | CHECK_RESULT(ParseScriptModule(&script_module)); |
| 3767 | |
| 3768 | Module* module = nullptr; |
| 3769 | |
| 3770 | switch (script_module->type()) { |
| 3771 | case ScriptModuleType::Text: { |
| 3772 | auto command = std::make_unique<ModuleCommand>(); |
| 3773 | module = &command->module; |
| 3774 | *module = std::move(cast<TextScriptModule>(script_module.get())->module); |
| 3775 | *out_command = std::move(command); |
| 3776 | break; |
| 3777 | } |
| 3778 | |
| 3779 | case ScriptModuleType::Binary: { |
| 3780 | auto command = std::make_unique<ScriptModuleCommand>(); |
| 3781 | module = &command->module; |
| 3782 | auto* bsm = cast<BinaryScriptModule>(script_module.get()); |
| 3783 | ReadBinaryOptions options; |
| 3784 | #if WABT_TRACING |
| 3785 | auto log_stream = FileStream::CreateStdout(); |
| 3786 | options.log_stream = log_stream.get(); |
| 3787 | #endif |
| 3788 | options.features = options_->features; |
| 3789 | Errors errors; |
| 3790 | const char* filename = "<text>"; |
| 3791 | if (options_->parse_binary_modules) { |
| 3792 | ReadBinaryIr(filename, bsm->data.data(), bsm->data.size(), options, |
| 3793 | &errors, module); |
| 3794 | } |
| 3795 | module->name = bsm->name; |
| 3796 | module->loc = bsm->loc; |
| 3797 | for (const auto& error : errors) { |
| 3798 | if (error.loc.offset == kInvalidOffset) { |
| 3799 | Error(bsm->loc, "error in binary module: %s", error.message.c_str()); |
| 3800 | } else { |
| 3801 | Error(bsm->loc, "error in binary module: @0x%08" PRIzx ": %s", |
| 3802 | error.loc.offset, error.message.c_str()); |
| 3803 | } |
| 3804 | } |
| 3805 | |
| 3806 | command->script_module = std::move(script_module); |
| 3807 | *out_command = std::move(command); |
| 3808 | break; |
| 3809 | } |
| 3810 | |
| 3811 | case ScriptModuleType::Quoted: |
| 3812 | auto command = std::make_unique<ModuleCommand>(); |
| 3813 | module = &command->module; |
| 3814 | auto* qsm = cast<QuotedScriptModule>(script_module.get()); |
| 3815 | Errors errors; |
| 3816 | const char* filename = "<text>"; |
| 3817 | std::unique_ptr<Module> m; |
| 3818 | std::unique_ptr<WastLexer> lexer = WastLexer::CreateBufferLexer( |
| 3819 | filename, qsm->data.data(), qsm->data.size(), &errors); |
| 3820 | auto result = ParseWatModule(lexer.get(), &m, &errors, options_); |