-
Notifications
You must be signed in to change notification settings - Fork 269
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds MemoryDefinition to CompiledModule and Memory (#817)
It is more often the case that projects are enabling a freestanding target, and that may or may not have an exporting memory depending on how that's interpreted. This adds the ability to inspect memories similar to how you can already inspect compiled code prior to instantiation. For example, you can enforce an ABI constraint that "memory" must be exported even if WASI is not in use. Signed-off-by: Adrian Cole <[email protected]>
- Loading branch information
1 parent
761347d
commit 9a623c4
Showing
15 changed files
with
430 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package wasm | ||
|
||
import "github.com/tetratelabs/wazero/api" | ||
|
||
// ImportedMemories implements the same method as documented on wazero.CompiledModule. | ||
func (m *Module) ImportedMemories() (ret []api.MemoryDefinition) { | ||
for _, d := range m.MemoryDefinitionSection { | ||
if d.importDesc != nil { | ||
ret = append(ret, d) | ||
} | ||
} | ||
return | ||
} | ||
|
||
// ExportedMemories implements the same method as documented on wazero.CompiledModule. | ||
func (m *Module) ExportedMemories() map[string]api.MemoryDefinition { | ||
ret := map[string]api.MemoryDefinition{} | ||
for _, d := range m.MemoryDefinitionSection { | ||
for _, e := range d.exportNames { | ||
ret[e] = d | ||
} | ||
} | ||
return ret | ||
} | ||
|
||
// BuildMemoryDefinitions generates memory metadata that can be parsed from | ||
// the module. This must be called after all validation. | ||
// | ||
// Note: This is exported for wazero.Runtime `CompileModule`. | ||
func (m *Module) BuildMemoryDefinitions() { | ||
var moduleName string | ||
if m.NameSection != nil { | ||
moduleName = m.NameSection.ModuleName | ||
} | ||
|
||
memoryCount := m.ImportMemoryCount() | ||
if m.MemorySection != nil { | ||
memoryCount++ | ||
} | ||
|
||
if memoryCount == 0 { | ||
return | ||
} | ||
|
||
m.MemoryDefinitionSection = make([]*MemoryDefinition, 0, memoryCount) | ||
importMemIdx := Index(0) | ||
for _, i := range m.ImportSection { | ||
if i.Type != ExternTypeMemory { | ||
continue | ||
} | ||
|
||
m.MemoryDefinitionSection = append(m.MemoryDefinitionSection, &MemoryDefinition{ | ||
importDesc: &[2]string{i.Module, i.Name}, | ||
index: importMemIdx, | ||
memory: i.DescMem, | ||
}) | ||
importMemIdx++ | ||
} | ||
|
||
if m.MemorySection != nil { | ||
m.MemoryDefinitionSection = append(m.MemoryDefinitionSection, &MemoryDefinition{ | ||
index: importMemIdx, | ||
memory: m.MemorySection, | ||
}) | ||
} | ||
|
||
for _, d := range m.MemoryDefinitionSection { | ||
d.moduleName = moduleName | ||
for _, e := range m.ExportSection { | ||
if e.Type == ExternTypeMemory && e.Index == d.index { | ||
d.exportNames = append(d.exportNames, e.Name) | ||
} | ||
} | ||
} | ||
} | ||
|
||
// MemoryDefinition implements api.MemoryDefinition | ||
type MemoryDefinition struct { | ||
moduleName string | ||
index Index | ||
importDesc *[2]string | ||
exportNames []string | ||
memory *Memory | ||
} | ||
|
||
// ModuleName implements the same method as documented on api.MemoryDefinition. | ||
func (f *MemoryDefinition) ModuleName() string { | ||
return f.moduleName | ||
} | ||
|
||
// Index implements the same method as documented on api.MemoryDefinition. | ||
func (f *MemoryDefinition) Index() uint32 { | ||
return f.index | ||
} | ||
|
||
// Import implements the same method as documented on api.MemoryDefinition. | ||
func (f *MemoryDefinition) Import() (moduleName, name string, isImport bool) { | ||
if importDesc := f.importDesc; importDesc != nil { | ||
moduleName, name, isImport = importDesc[0], importDesc[1], true | ||
} | ||
return | ||
} | ||
|
||
// ExportNames implements the same method as documented on api.MemoryDefinition. | ||
func (f *MemoryDefinition) ExportNames() []string { | ||
return f.exportNames | ||
} | ||
|
||
// Min implements the same method as documented on api.MemoryDefinition. | ||
func (f *MemoryDefinition) Min() uint32 { | ||
return f.memory.Min | ||
} | ||
|
||
// Max implements the same method as documented on api.MemoryDefinition. | ||
func (f *MemoryDefinition) Max() (max uint32, encoded bool) { | ||
max = f.memory.Max | ||
encoded = f.memory.IsMaxEncoded | ||
return | ||
} |
Oops, something went wrong.