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

need usage examples #60

Open
andreimatei opened this issue Jun 7, 2018 · 10 comments
Open

need usage examples #60

andreimatei opened this issue Jun 7, 2018 · 10 comments

Comments

@andreimatei
Copy link

Hi folks,

I've found this project while looking for a Go wasm interpreter that I can use as a library. It seems awesome, but I've got to say that I'm a bit at a loss about how I am supposed to use it. I've perused the tests in the repo some, but it wasn't enough for me. This is partially because I'm new to wasm too.
If it's not too much trouble, I would kindly ask for some guidance/example around:

  1. How would I go about producing "compatible" wasm files from a high-level language? validate: could not validate a simple program #36 suggests that the current go compiler targeting the wasm arch does not work for some reason. How have y'all produced the testdata files / what is known to work? Any hand-holding would be gold.
  2. How would I pass data (input args of different types, say) into a (function from a) loaded module?
  3. I see that recently support for calling Go functions provided by the host binary has been added. I've tried parsing the test case added, but the interesting bit - calling the exposed function from wasm - seems to be hidden inside a binary blob. Any example of both the wasm side calling the host function and the go side providing the function together would be, again, gold.

I'm interested in playing around with wagon for prototyping wasm user-defined functions in a SQL database - CockroachDB.

Thanks!

@sbinet
Copy link
Contributor

sbinet commented Jun 8, 2018

hi Andrei,

you're right, the amount of documentation (both low-level and high-level) is rather scarce.
For the high-level, I tend to redirect people to this blog post of mine:

and for the low-level, well, I sheepishly redirect them to webassembly.org :}

  1. How would I go about producing "compatible" wasm files from a high-level language? validate: could not validate a simple program #36 suggests that the current go compiler targeting the wasm arch does not work for some reason. How have y'all produced the testdata files / what is known to work? Any hand-holding would be gold.

these files have been usually either copied from the official test suite of webassembly:

  1. How would I pass data (input args of different types, say) into a (function from a) loaded module?

I'll try to come up with an example for this over the week-end.

  1. I see that recently support for calling Go functions provided by the host binary has been added. I've tried parsing the test case added, but the interesting bit - calling the exposed function from wasm - seems to be hidden inside a binary blob. Any example of both the wasm side calling the host function and the go side providing the function together would be, again, gold.

ditto: I'll try to hack something up during the week-end.

@sbinet
Copy link
Contributor

sbinet commented Jun 8, 2018

wrt your 1.), fixing #34 would go a long way towards easily compiling some .wasm files while waiting for a fix for #36

@andreimatei
Copy link
Author

That would be awesome, thank you!

@andreimatei
Copy link
Author

For my immediate needs, this is what I'd love hand-holding for concretely:

  • I want to be able to write a function in some reasonable language (C, js, Go, don't particularly care) that takes some args (say, the language equivalent of int64, string, []byte). I want to compile to wasm (I guess manually for now).
  • I want to load the resulting module and call it from Go code by passing the arguments.
  • Bonus points if the module can call back into Go code by using a well-known host-exported function that returns one or more values.

Thanks!

sbinet added a commit that referenced this issue Jun 11, 2018
@sbinet
Copy link
Contributor

sbinet commented Jun 11, 2018

@andreimatei I guess I won't have any bonus point... but #61 may help you somewhat?

@andreimatei
Copy link
Author

andreimatei commented Jun 12, 2018 via email

@sbinet
Copy link
Contributor

sbinet commented Jun 12, 2018

Of course!

@andreimatei
Copy link
Author

Hey Sebastien,

So I'm digging around and I've got two related questions, if you don't mind the bother.
Any C program compiled by emscripten that operates with pointers has an import like this:

  (import "env" "memory" (memory (;0;) 256 256))

What's the right thing to do (or even the wrong thing) for handling it? Should I make the ResolveFunc provide an "env" module that's empty other than one entry in its Memory section?

And 2nd, how would one go about passing a Go string or []byte to a function? The C function I'm compiling takes a char* which gets transformed into a i32 which is (I think) an index into the module's linear memory. What would be the proper way for a caller to copy the string into the module's memory? I see that the memory (namely module.LinearMemoryIndexSpace[0]) is initialized in NewVM. Should I just copy my strings to module.LinearMemoryIndexSpace[0] before calling vm.ExecCode? Is there a related example somewhere by any chance?

@andreimatei
Copy link
Author

andreimatei commented Jun 15, 2018

I was able to get a C program using strings and things like strlen() working by hacking how a module's Memory is initialized when importing an entry of type ExternalMemory. I've had to initialize module.Memory.Entries by hand, as otherwise populateLinearMemory() was a no-op, hitting this condition:

wagon/wasm/index.go

Lines 139 to 141 in d4bc452

if m.Data == nil || len(m.Data.Entries) == 0 {
return nil
}

I've got no idea what I'm doing, but I can attempt to put a PR together towards improving importing of memory sections if you think there's something to do here.

@sbinet
Copy link
Contributor

sbinet commented Jun 18, 2018

I must admit I am starting to be a bit out of my confort zone :)

funnily enough, I stumbled upon this (on the #webassembly slack channel):

sbinet added a commit that referenced this issue Jun 28, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants