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(InstanceContext): Allow any type of user data #85

Merged
merged 2 commits into from
Nov 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions wasmer/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ func (instanceContext *InstanceContext) Memory() *Memory {

// Data returns the instance context data as an `unsafe.Pointer`. It's
// up to the user to cast it appropriately as a pointer to a data.
func (instanceContext *InstanceContext) Data() unsafe.Pointer {
return cWasmerInstanceContextDataGet(instanceContext.context)
func (instanceContext *InstanceContext) Data() interface{} {
dataPtr := *(*unsafe.Pointer)(
cWasmerInstanceContextDataGet(instanceContext.context))
return *(*interface{})(dataPtr)
}
28 changes: 20 additions & 8 deletions wasmer/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ type Instance struct {

// The exported memory of a WebAssembly instance.
Memory *Memory

// ctxDataC and ctxDataGo ensure that InstanceContext data is not GC'd
// for the lifetime of the Instance.
ctxDataC *uintptr
ctxDataGo interface{}
}

// NewInstance constructs a new `Instance` with no imported functions.
Expand Down Expand Up @@ -419,14 +424,21 @@ func (instance *Instance) HasMemory() bool {
return nil != instance.Memory
}

// SetContextData assigns a data that can be used by all imported
// functions. Indeed, each imported function receives as its first
// argument an instance context (see `InstanceContext`). An instance
// context can hold a pointer to any kind of data. It is important to
// understand that this data is shared by all imported function, it's
// global to the instance.
func (instance *Instance) SetContextData(data unsafe.Pointer) {
cWasmerInstanceContextDataSet(instance.instance, data)
// SetContextData assigns a data that can be used by all imported functions.
// Each imported function receives as its first argument an instance context
// (see `InstanceContext`). An instance context can hold any kind of data,
// including data that contain Go references such as slices, maps, or structs
// with reference types or pointers. It is important to understand that data is
// global to the instance, and thus is shared by all imported functions.
func (instance *Instance) SetContextData(data interface{}) {
// Cache the data and the new uintptr to protect it from GC for the
// lifetime of instance.
instance.ctxDataGo = data
instance.ctxDataC = new(uintptr)
*instance.ctxDataC = uintptr(unsafe.Pointer(&instance.ctxDataGo))

cWasmerInstanceContextDataSet(instance.instance,
unsafe.Pointer(instance.ctxDataC))
}

// Close closes/frees an `Instance`.
Expand Down
13 changes: 10 additions & 3 deletions wasmer/test/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,13 +226,17 @@ func testImportInstanceContext(t *testing.T) {
func logMessageWithContextData(context unsafe.Pointer, pointer int32, length int32) {
var instanceContext = wasm.IntoInstanceContext(context)
var memory = instanceContext.Memory().Data()
var logMessage = (*logMessageContext)(instanceContext.Data())
var logMessage = instanceContext.Data().(*logMessageContext)

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

type logMessageContext struct {
message string

// Ensure that Context Data may include Go pointers & reference types.
slice []string
ptr *string
}

func testImportInstanceContextData(t *testing.T) {
Expand All @@ -244,8 +248,11 @@ func testImportInstanceContextData(t *testing.T) {

defer instance.Close()

contextData := logMessageContext{message: "first"}
instance.SetContextData(unsafe.Pointer(&contextData))
str := "test"
contextData := logMessageContext{message: "first",
slice: []string{str, str},
ptr: &str}
instance.SetContextData(&contextData)

doSomething := instance.Exports["do_something"]

Expand Down