Skip to content
This repository has been archived by the owner on May 11, 2020. It is now read-only.

Commit

Permalink
wasm: implement imports
Browse files Browse the repository at this point in the history
Updates #40.
  • Loading branch information
sbinet committed Dec 14, 2017
1 parent 3d087b0 commit adcf76a
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
9 changes: 7 additions & 2 deletions wasm/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func (module *Module) resolveImports(resolve ResolveFunc) error {

modules := make(map[string]*Module)

var funcs uint32
for _, importEntry := range module.Import.Entries {
importedModule, ok := modules[importEntry.ModuleName]
if !ok {
Expand Down Expand Up @@ -134,6 +135,9 @@ func (module *Module) resolveImports(resolve ResolveFunc) error {
return InvalidFunctionIndexError(index)
}
module.FunctionIndexSpace = append(module.FunctionIndexSpace, *fn)
module.Code.Bodies = append(module.Code.Bodies, *fn.Body)
module.imports.Funcs = append(module.imports.Funcs, funcs)
funcs++
case ExternalGlobal:
glb := importedModule.GetGlobal(int(index))
if glb == nil {
Expand All @@ -143,6 +147,7 @@ func (module *Module) resolveImports(resolve ResolveFunc) error {
return ErrImportMutGlobal
}
module.GlobalIndexSpace = append(module.GlobalIndexSpace, *glb)
module.imports.Globals++

// In both cases below, index should be always 0 (according to the MVP)
// We check it against the length of the index space anyway.
Expand All @@ -151,16 +156,16 @@ func (module *Module) resolveImports(resolve ResolveFunc) error {
return InvalidTableIndexError(index)
}
module.TableIndexSpace[0] = importedModule.TableIndexSpace[0]
module.imports.Tables++
case ExternalMemory:
if int(index) >= len(importedModule.LinearMemoryIndexSpace) {
return InvalidLinearMemoryIndexError(index)
}
module.LinearMemoryIndexSpace[0] = importedModule.LinearMemoryIndexSpace[0]
module.imports.Memories++
default:
return InvalidExternalError(exportEntry.Kind)
}

}

return nil
}
4 changes: 4 additions & 0 deletions wasm/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ func (m *Module) populateFunctions() error {
m.FunctionIndexSpace = append(m.FunctionIndexSpace, fn)
}

funcs := make([]uint32, 0, len(m.Function.Types)+len(m.imports.Funcs))
funcs = append(funcs, m.imports.Funcs...)
funcs = append(funcs, m.Function.Types...)
m.Function.Types = funcs
return nil
}

Expand Down
15 changes: 14 additions & 1 deletion wasm/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ type Module struct {
LinearMemoryIndexSpace [][]byte

Other []Section // Other holds the custom sections if any

imports struct {
Funcs []uint32
Globals int
Tables int
Memories int
}
}

// ResolveFunc is a function that takes a module name and
Expand Down Expand Up @@ -90,7 +97,13 @@ func ReadModule(r io.Reader, resolvePath ResolveFunc) (*Module, error) {
}

if m.Import != nil {
return nil, errors.New("Imports aren't supported")
if resolvePath == nil {
return nil, errors.New("wasm: no module importer provided")
}
err := m.resolveImports(resolvePath)
if err != nil {
return nil, err
}
}

for _, fn := range []func() error{
Expand Down

0 comments on commit adcf76a

Please sign in to comment.