Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(module) Add Compile, Module.Instantiate*, and Module.Close #33

Merged
merged 3 commits into from
Jun 5, 2019

Conversation

Hywan
Copy link
Contributor

@Hywan Hywan commented Jun 5, 2019

Fix #27.

This patch adds a way to build a real Module by using the Compile
function. Then, a module can instantiated with Module.Instantiate or
Module.InstantiateWithImports.

The NewInstanceWithImports function has been updated to use a new
newInstanceWithImports private function that takes an “instance
builder” as a parameter, so that it's easier to share code Instance
and Module when it comes to create an Instance with its imports,
exports, and memory.

Hywan added 2 commits June 5, 2019 14:36
This patch adds a way to build a real `Module` by using the `Compile`
function. Then, a module can instantiated with `Module.Instantiate` or
`Module.InstantiateWithImports`.

The `NewInstanceWithImports` function has been updated to use a new
`newInstanceWithImports` private function that takes an “instance
builder” as a parameter, so that it's easier to share code `Instance`
and `Module` when it comes to create an `Instance` with its imports,
exports, and memory.
@Hywan Hywan added 🎉 enhancement New feature or request 📦 component-extension About the Go extension 📚 documentation Do you like to read? 🧪 tests I love tests labels Jun 5, 2019
@Hywan Hywan self-assigned this Jun 5, 2019
@Hywan
Copy link
Contributor Author

Hywan commented Jun 5, 2019

bors try

bors bot added a commit that referenced this pull request Jun 5, 2019
@bors
Copy link
Contributor

bors bot commented Jun 5, 2019

@Hywan
Copy link
Contributor Author

Hywan commented Jun 5, 2019

bors r+

bors bot added a commit that referenced this pull request Jun 5, 2019
33: feat(module) Add `Compile`, `Module.Instantiate*`, and `Module.Close` r=Hywan a=Hywan

Fix #27.

This patch adds a way to build a real `Module` by using the `Compile`
function. Then, a module can instantiated with `Module.Instantiate` or
`Module.InstantiateWithImports`.

The `NewInstanceWithImports` function has been updated to use a new
`newInstanceWithImports` private function that takes an “instance
builder” as a parameter, so that it's easier to share code `Instance`
and `Module` when it comes to create an `Instance` with its imports,
exports, and memory.

Co-authored-by: Ivan Enderlin <ivan.enderlin@hoa-project.net>
@bors
Copy link
Contributor

bors bot commented Jun 5, 2019

@bors bors bot merged commit 8267b08 into wasmerio:master Jun 5, 2019
bors bot added a commit that referenced this pull request Jun 5, 2019
34: feat(module) Support module serialization and deserialization r=Hywan a=Hywan

Sequel of #27 and #33.
Ping #23.

Example:

```go
	// Compiles the bytes into a WebAssembly module.
	module1, _ := wasm.Compile(GetBytes())
	defer module1.Close()

	// Serializes the module into a sequence of bytes.
	serialization, _ := module1.Serialize()

	// Do something with `serialization`.
	// Then later…

	// Deserializes the module.
	module2, _ := wasm.DeserializeModule(serialization)
	defer module2.Close()
	// And enjoy!

	// Instantiates the WebAssembly module.
	instance, _ := module2.Instantiate()
	defer instance.Close()

	// Gets an exported function.
	sum, functionExists := instance.Exports["sum"]

	fmt.Println(functionExists)

	// Calls the `sum` exported function with Go values.
	result, _ := sum(1, 2)

	fmt.Println(result)

	// Output:
	// true
	// 3
```

The final API is quite easy to use. I hope `[]byte` is a generic enough type to be accepted by Go caching libraries.

Co-authored-by: Ivan Enderlin <ivan.enderlin@hoa-project.net>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📦 component-extension About the Go extension 📚 documentation Do you like to read? 🎉 enhancement New feature or request 🧪 tests I love tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Compile module without instantiating it
1 participant