go-interpreter / wagon

wagon, a WebAssembly-based Go interpreter, for Go.
BSD 3-Clause "New" or "Revised" License
902 stars 150 forks source link

Import Table #170

Open sampaioletti opened 5 years ago

sampaioletti commented 5 years ago

Related to work for #163

In a pretty standard (from what i can tell) wasm generated with emscripten it has the following imports

  (import "env" "memory" (memory (;0;) 256 256))
  (import "env" "table" (table (;0;) 14 14 funcref))
  ;;with table def further down the file
  (elem (;0;) (global.get 1) 50 22 51 51 28 51 23 51 51 51 52 52 52 24) ;;global 1 is just the mem base

and has no memory/table statement for the root module

This results in

the LinearMemory space gets instantiated automatically https://github.com/go-interpreter/wagon/blob/3fd36533bb90cbfea01da645a92b1a7e828ea336/wasm/module.go#L140

while the TableIndexSpace does not (as there is no Table Section) https://github.com/go-interpreter/wagon/blob/3fd36533bb90cbfea01da645a92b1a7e828ea336/wasm/module.go#L141-L143

Which causes a fault when importing the "env" modules table definition (line 213) as module.TableIndexSpace is undefnied https://github.com/go-interpreter/wagon/blob/3fd36533bb90cbfea01da645a92b1a7e828ea336/wasm/imports.go#L209-L214

I can't seem to find much info on if this is per MVP spec or if its a hack for emscripten but should a root module instantiate its own table/memory when an imported module has these entries? Or am I missing something. I've played with various hacks..like inserting

 //while resolving imports wasm/imports.go 209
case ExternalTable:
    if int(index) >= len(importedModule.TableIndexSpace) {
        return InvalidTableIndexError(index)
    }
    //Inserted to setup module.Table if not already setup since import requires it
    if module.Table==nil{
        module.Table=importedModule.Table
        moudile.TableIndexSpace=importedModule.TableIndexSpace
    }
    //end insert
    module.TableIndexSpace[0] = importedModule.TableIndexSpace[0]
    module.imports.Tables++

which seems to give me the correct functionality.

If I'm on the right path I can prep a PR...if I'm way off base I can accept that also (:

Looking for insight. Thanks!

twitchyliquid64 commented 4 years ago

Hmmm, enscripten as in compiling C++/C for the browser yeah?

Does the browser setup some memory and a table for us by default? That might be the cause.