-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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]>
- Loading branch information
Showing
6 changed files
with
69 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters