compiler: ResolveImportedFunction should infer the typeID from the importing module #2314
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #2313.
This changes the signature for
ResolveImportedFunction()
so that it takes an additional index into the typeID section of the "current" module, i.e. the subject of the import.The current behavior is to search for the import recursively into the chain of dependencies; however, this resolves to a typeID that is specific to that module. For instance:
m
declaring an importm2inst.sym
.m2inst.sym
an import with typet
with IDtid
m2inst
be linked to an instance of modulem2
with typet2
and IDt2id
.Now: we want
t
to be compatible witht2
; this we verify at:wazero/internal/wasm/store.go
Lines 441 to 447 in 6bb9899
The current behavior is for
ResolveImportedFunction()
to resolve recursively a reference to a function so that we can then jump precisely to its address at runtime; however, we should not carry on its type ID, and instead use the type ID of the import.In other words, even if the types are compatible, we should be still referring to type
t
with IDtid
fromm
, and not type IDt2id
, because, of course, the indext2id
might be different from the local indextid
.The solution is to add another
descFunc Index
(from the name of the fieldImport.DescFunc
) to the signature ofResolveImportedFunction()
, so that the typeID is correctly resolved frommoduleEngine.module.TypeIDs[descFunc]
(i.e. the typeID defined in the module that is requiring the imported function).The tests are adjusted accordingly; notably, this also means that, in order for
m.ResolveImportedFunction()
to work properly,m
must be correctly initialized with anm.module
being the module that is performing the import, so the initialization ofmoduleEngine
is pushed down, after the declaration of the "imported
" and "importing
" modules.Let me know if you think it is worth adding more tests!
@zshipko please double-check that this is fixing your issue (I have slightly changed my original approach)
@ncruces would you like to double-check this against your sqlite lib?
@anuraaga if you spare any time, would you take a look too? :P