@@ -60,6 +60,7 @@ class BinaryReaderInterp : public BinaryReaderNop {
60
60
BinaryReaderInterp (Environment* env,
61
61
DefinedModule* module ,
62
62
std::unique_ptr<OutputBuffer> istream,
63
+ const std::vector<Export*>& imports_,
63
64
Errors* errors,
64
65
const Features& features);
65
66
@@ -323,11 +324,15 @@ class BinaryReaderInterp : public BinaryReaderNop {
323
324
string_view name);
324
325
wabt::Result FindRegisteredModule (string_view module_name,
325
326
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);
329
333
330
334
Features features_;
335
+ const std::vector<Export*>& imports_;
331
336
Errors* errors_ = nullptr ;
332
337
Environment* env_ = nullptr ;
333
338
DefinedModule* module_ = nullptr ;
@@ -363,9 +368,11 @@ class BinaryReaderInterp : public BinaryReaderNop {
363
368
BinaryReaderInterp::BinaryReaderInterp (Environment* env,
364
369
DefinedModule* module ,
365
370
std::unique_ptr<OutputBuffer> istream,
371
+ const std::vector<Export*>& imports,
366
372
Errors* errors,
367
373
const Features& features)
368
374
: features_(features),
375
+ imports_ (imports),
369
376
errors_(errors),
370
377
env_(env),
371
378
module_(module ),
@@ -765,16 +772,35 @@ wabt::Result BinaryReaderInterp::FindRegisteredModule(string_view module_name,
765
772
return wabt::Result::Ok;
766
773
}
767
774
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
+ }
776
801
}
777
802
803
+ CHECK_RESULT (CheckImportKind (module_name, field_name, kind, export_->kind ));
778
804
*out_export = export_;
779
805
return wabt::Result::Ok;
780
806
}
@@ -786,20 +812,9 @@ wabt::Result BinaryReaderInterp::OnImportFunc(Index import_index,
786
812
Index sig_index) {
787
813
Index env_sig_index = TranslateSigIndexToEnv (sig_index);
788
814
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_));
803
818
804
819
Func* func = env_->GetFunc (export_->index );
805
820
if (!env_->FuncSignaturesAreEqual (env_sig_index, func->sig_index )) {
@@ -819,12 +834,9 @@ wabt::Result BinaryReaderInterp::OnImportTable(Index import_index,
819
834
Type elem_type,
820
835
const Limits* elem_limits) {
821
836
has_table = true ;
822
- Module* import_module;
823
- CHECK_RESULT (FindRegisteredModule (module_name, &import_module));
824
-
825
837
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_));
828
840
829
841
Table* table = env_->GetTable (export_->index );
830
842
if (elem_type != table->elem_type ) {
@@ -849,12 +861,9 @@ wabt::Result BinaryReaderInterp::OnImportMemory(Index import_index,
849
861
return wabt::Result::Error;
850
862
}
851
863
852
- Module* import_module;
853
- CHECK_RESULT (FindRegisteredModule (module_name, &import_module));
854
-
855
864
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_));
858
867
859
868
Memory* memory = env_->GetMemory (export_->index );
860
869
CHECK_RESULT (CheckImportLimits (page_limits, &memory->page_limits ));
@@ -890,18 +899,13 @@ wabt::Result BinaryReaderInterp::OnImportGlobal(Index import_index,
890
899
Index global_index,
891
900
Type type,
892
901
bool mutable_) {
893
- Module* import_module;
894
- CHECK_RESULT (FindRegisteredModule (module_name, &import_module));
895
-
896
902
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_));
900
905
901
906
Global* exported_global = env_->GetGlobal (export_->index );
902
907
GlobalType expected = {type, mutable_};
903
908
GlobalType actual = {exported_global->type , exported_global->mutable_ };
904
-
905
909
if (Failed (CheckGlobalType (actual, expected))) {
906
910
return wabt::Result::Error;
907
911
}
@@ -1932,6 +1936,7 @@ wabt::Result ReadBinaryInterp(Environment* env,
1932
1936
const void * data,
1933
1937
size_t size,
1934
1938
const ReadBinaryOptions& options,
1939
+ const std::vector<Export*>& imports,
1935
1940
Errors* errors,
1936
1941
DefinedModule** out_module) {
1937
1942
// Need to mark before taking ownership of env->istream.
@@ -1941,7 +1946,7 @@ wabt::Result ReadBinaryInterp(Environment* env,
1941
1946
IstreamOffset istream_offset = istream->size ();
1942
1947
DefinedModule* module = new DefinedModule (env);
1943
1948
1944
- BinaryReaderInterp reader (env, module , std::move (istream), errors,
1949
+ BinaryReaderInterp reader (env, module , std::move (istream), imports, errors,
1945
1950
options.features );
1946
1951
env->EmplaceBackModule (module );
1947
1952
@@ -1959,4 +1964,15 @@ wabt::Result ReadBinaryInterp(Environment* env,
1959
1964
return result;
1960
1965
}
1961
1966
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
+
1962
1978
} // namespace wabt
0 commit comments