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(import) Get an instance context data API #28

Closed
Hywan opened this issue Jun 4, 2019 · 0 comments · Fixed by #30
Closed

feat(import) Get an instance context data API #28

Hywan opened this issue Jun 4, 2019 · 0 comments · Fixed by #30
Assignees
Labels
📦 component-extension About the Go extension 🎉 enhancement New feature or request

Comments

@Hywan
Copy link
Contributor

Hywan commented Jun 4, 2019

Sequel of #26. We must support wasmer_instance_context_data_set and wasmer_instance_context_data_get, introduced in wasmerio/wasmer#261.

@Hywan Hywan added 🎉 enhancement New feature or request 📦 component-extension About the Go extension labels Jun 4, 2019
@Hywan Hywan self-assigned this Jun 4, 2019
bors bot added a commit that referenced this issue Jun 5, 2019
30: feat(import) Support instance context data r=Hywan a=Hywan

Fix #28.

Example:

```go
//export logMessageWithContextData
func logMessageWithContextData(context unsafe.Pointer, pointer int32, length int32) {
	var instanceContext = wasm.IntoInstanceContext(context)
	var memory = instanceContext.Memory().Data()
	var logMessage = (*logMessageContext)(instanceContext.Data())

	logMessage.message = string(memory[pointer : pointer+length])
}

type logMessageContext struct {
	message string
}

func testImportInstanceContextData(t *testing.T) {
	imports, err := wasm.NewImports().Append("log_message", logMessageWithContextData, C.logMessageWithContextData)
	assert.NoError(t, err)

	instance, err := wasm.NewInstanceWithImports(getImportedFunctionBytes("log.wasm"), imports)
	assert.NoError(t, err)

	defer instance.Close()

	contextData := logMessageContext{message: "first"}
	instance.SetContextData(unsafe.Pointer(&contextData))

	doSomething := instance.Exports["do_something"]

	result, err := doSomething()

	assert.NoError(t, err)
	assert.Equal(t, wasm.TypeVoid, result.GetType())
	assert.Equal(t, "hello", contextData.message)
}
```

1. When defining the instance, we see `SetContextData` that assigns a data to the instance context.
2. In the imported function, on the instance context, we see the `Data` method call that retrieves the instance context data (here, cast as a `logMessageContext` structure).

Co-authored-by: Ivan Enderlin <[email protected]>
@bors bors bot closed this as completed in #30 Jun 5, 2019
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 🎉 enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant