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) Support instance context data #30

Merged
merged 3 commits into from
Jun 5, 2019

Conversation

Hywan
Copy link
Contributor

@Hywan Hywan commented Jun 4, 2019

Fix #28.

Example:

//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).

@Hywan Hywan self-assigned this Jun 4, 2019
@Hywan Hywan added 🎉 enhancement New feature or request 📦 component-runtime About the Wasm runtime 🧪 tests I love tests labels Jun 4, 2019
Hywan added 3 commits June 5, 2019 11:47
Each imported function receives an instance context. An instance
context can hold a pointer to a data, and this data can be anything: A
scalar value, a structure… anything.
@Hywan Hywan force-pushed the feat-import-instance-context-data branch from deeada5 to a172bac Compare June 5, 2019 09:51
@Hywan Hywan marked this pull request as ready for review June 5, 2019 09:51
@Hywan Hywan changed the title [wip] feat(import) Try to support instance context data feat(import) Try to support instance context data Jun 5, 2019
@Hywan Hywan changed the title feat(import) Try to support instance context data feat(import) Support instance context data 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
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 <ivan.enderlin@hoa-project.net>
@bors
Copy link
Contributor

bors bot commented Jun 5, 2019

@bors bors bot merged commit a172bac into wasmerio:master Jun 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
📦 component-runtime About the Wasm runtime 🎉 enhancement New feature or request 🧪 tests I love tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feat(import) Get an instance context data API
1 participant