@@ -60,6 +60,7 @@ class BinaryReaderInterp : public BinaryReaderNop {
6060 BinaryReaderInterp (Environment* env,
6161 DefinedModule* module ,
6262 std::unique_ptr<OutputBuffer> istream,
63+ const std::vector<Export*>& imports_,
6364 Errors* errors,
6465 const Features& features);
6566
@@ -323,11 +324,15 @@ class BinaryReaderInterp : public BinaryReaderNop {
323324 string_view name);
324325 wabt::Result FindRegisteredModule (string_view module_name,
325326 Module** out_module);
326- wabt::Result GetModuleExport (Module* module ,
327- string_view field_name,
328- Export** out_export);
327+ wabt::Result ResolveImport (Index import_index,
328+ ExternalKind kind,
329+ string_view module_name,
330+ string_view field_name,
331+ Index sig_index,
332+ Export** out_export);
329333
330334 Features features_;
335+ const std::vector<Export*>& imports_;
331336 Errors* errors_ = nullptr ;
332337 Environment* env_ = nullptr ;
333338 DefinedModule* module_ = nullptr ;
@@ -363,9 +368,11 @@ class BinaryReaderInterp : public BinaryReaderNop {
363368BinaryReaderInterp::BinaryReaderInterp (Environment* env,
364369 DefinedModule* module ,
365370 std::unique_ptr<OutputBuffer> istream,
371+ const std::vector<Export*>& imports,
366372 Errors* errors,
367373 const Features& features)
368374 : features_(features),
375+ imports_ (imports),
369376 errors_(errors),
370377 env_(env),
371378 module_(module ),
@@ -765,16 +772,35 @@ wabt::Result BinaryReaderInterp::FindRegisteredModule(string_view module_name,
765772 return wabt::Result::Ok;
766773}
767774
768- wabt::Result BinaryReaderInterp::GetModuleExport (Module* module ,
769- string_view field_name,
770- Export** out_export) {
771- Export* export_ = module ->GetExport (field_name);
772- if (!export_) {
773- PrintError (" unknown module field \" " PRIstringview " \" " ,
774- WABT_PRINTF_STRING_VIEW_ARG (field_name));
775- return wabt::Result::Error;
775+ wabt::Result BinaryReaderInterp::ResolveImport (Index import_index,
776+ ExternalKind kind,
777+ string_view module_name,
778+ string_view field_name,
779+ Index sig_index,
780+ Export** out_export) {
781+ Export* export_ = nullptr ;
782+ if (!imports_.empty ()) {
783+ export_ = imports_[import_index];
784+ } else {
785+ Module* module ;
786+ CHECK_RESULT (FindRegisteredModule (module_name, &module ));
787+
788+ // Func imports get special handled due to the face that they can be
789+ // overloaded on signature.
790+ if (kind == ExternalKind::Func) {
791+ export_ = module ->GetFuncExport (env_, field_name, sig_index);
792+ }
793+ if (!export_) {
794+ export_ = module ->GetExport (field_name);
795+ if (!export_) {
796+ PrintError (" unknown module field \" " PRIstringview " \" " ,
797+ WABT_PRINTF_STRING_VIEW_ARG (field_name));
798+ return wabt::Result::Error;
799+ }
800+ }
776801 }
777802
803+ CHECK_RESULT (CheckImportKind (module_name, field_name, kind, export_->kind ));
778804 *out_export = export_;
779805 return wabt::Result::Ok;
780806}
@@ -786,20 +812,9 @@ wabt::Result BinaryReaderInterp::OnImportFunc(Index import_index,
786812 Index sig_index) {
787813 Index env_sig_index = TranslateSigIndexToEnv (sig_index);
788814
789- Module* import_module;
790- CHECK_RESULT (FindRegisteredModule (module_name, &import_module));
791-
792- Export* export_ =
793- import_module->GetFuncExport (env_, field_name, env_sig_index);
794- if (!export_) {
795- // If GetFuncExport fails then GetModuleExport will fail too. But it's
796- // useful to call here to share the same error handling code as other
797- // imports.
798- CHECK_RESULT (GetModuleExport (import_module, field_name, &export_));
799- }
800-
801- CHECK_RESULT (CheckImportKind (module_name, field_name, ExternalKind::Func,
802- export_->kind ));
815+ Export* export_;
816+ CHECK_RESULT (ResolveImport (import_index, ExternalKind::Func, module_name,
817+ field_name, env_sig_index, &export_));
803818
804819 Func* func = env_->GetFunc (export_->index );
805820 if (!env_->FuncSignaturesAreEqual (env_sig_index, func->sig_index )) {
@@ -819,12 +834,9 @@ wabt::Result BinaryReaderInterp::OnImportTable(Index import_index,
819834 Type elem_type,
820835 const Limits* elem_limits) {
821836 has_table = true ;
822- Module* import_module;
823- CHECK_RESULT (FindRegisteredModule (module_name, &import_module));
824-
825837 Export* export_;
826- CHECK_RESULT (GetModuleExport (import_module, field_name, &export_));
827- CHECK_RESULT ( CheckImportKind (module_name, field_name, ExternalKind::Table, export_-> kind ));
838+ CHECK_RESULT (ResolveImport (import_index, ExternalKind::Table, module_name,
839+ field_name, 0 , & export_));
828840
829841 Table* table = env_->GetTable (export_->index );
830842 if (elem_type != table->elem_type ) {
@@ -849,12 +861,9 @@ wabt::Result BinaryReaderInterp::OnImportMemory(Index import_index,
849861 return wabt::Result::Error;
850862 }
851863
852- Module* import_module;
853- CHECK_RESULT (FindRegisteredModule (module_name, &import_module));
854-
855864 Export* export_;
856- CHECK_RESULT (GetModuleExport (import_module, field_name, &export_));
857- CHECK_RESULT ( CheckImportKind (module_name, field_name, ExternalKind::Memory, export_-> kind ));
865+ CHECK_RESULT (ResolveImport (import_index, ExternalKind::Memory, module_name,
866+ field_name, 0 , & export_));
858867
859868 Memory* memory = env_->GetMemory (export_->index );
860869 CHECK_RESULT (CheckImportLimits (page_limits, &memory->page_limits ));
@@ -890,18 +899,13 @@ wabt::Result BinaryReaderInterp::OnImportGlobal(Index import_index,
890899 Index global_index,
891900 Type type,
892901 bool mutable_) {
893- Module* import_module;
894- CHECK_RESULT (FindRegisteredModule (module_name, &import_module));
895-
896902 Export* export_;
897- CHECK_RESULT (GetModuleExport (import_module, field_name, &export_));
898- CHECK_RESULT (CheckImportKind (module_name, field_name, ExternalKind::Global,
899- export_->kind ));
903+ CHECK_RESULT (ResolveImport (import_index, ExternalKind::Global, module_name,
904+ field_name, 0 , &export_));
900905
901906 Global* exported_global = env_->GetGlobal (export_->index );
902907 GlobalType expected = {type, mutable_};
903908 GlobalType actual = {exported_global->type , exported_global->mutable_ };
904-
905909 if (Failed (CheckGlobalType (actual, expected))) {
906910 return wabt::Result::Error;
907911 }
@@ -1932,6 +1936,7 @@ wabt::Result ReadBinaryInterp(Environment* env,
19321936 const void * data,
19331937 size_t size,
19341938 const ReadBinaryOptions& options,
1939+ const std::vector<Export*>& imports,
19351940 Errors* errors,
19361941 DefinedModule** out_module) {
19371942 // Need to mark before taking ownership of env->istream.
@@ -1941,7 +1946,7 @@ wabt::Result ReadBinaryInterp(Environment* env,
19411946 IstreamOffset istream_offset = istream->size ();
19421947 DefinedModule* module = new DefinedModule (env);
19431948
1944- BinaryReaderInterp reader (env, module , std::move (istream), errors,
1949+ BinaryReaderInterp reader (env, module , std::move (istream), imports, errors,
19451950 options.features );
19461951 env->EmplaceBackModule (module );
19471952
@@ -1959,4 +1964,15 @@ wabt::Result ReadBinaryInterp(Environment* env,
19591964 return result;
19601965}
19611966
1967+ wabt::Result ReadBinaryInterp (Environment* env,
1968+ const void * data,
1969+ size_t size,
1970+ const ReadBinaryOptions& options,
1971+ Errors* errors,
1972+ DefinedModule** out_module) {
1973+ std::vector<Export*> empty_imports;
1974+ return ReadBinaryInterp (env, data, size, options, empty_imports, errors,
1975+ out_module);
1976+ }
1977+
19621978} // namespace wabt
0 commit comments