how to get the Golang function pointer and the external program can call this function by the pointer?

32 Views Asked by At

First call the update function, and then call an asynchronous function ic0.Call_new. Pass the pointer address of the callback function ic.Call_new as a parameter. The host env will call the callback through the pointer address. the running environment of the code is on the wasm virtual machine

type State struct {
    value uint16
}

var state State = State{
    value: 0,
}


func update() {
   
    callbackPtr := int32(uintptr(reflect.ValueOf(callback).Pointer()))
    callId := [20]byte{68, 73, 68, 76, 1, 110, 104, 1, 0, 1, 1, 0}
    method := [16]byte{101, 99, 100, 115, 97, 95, 112, 117, 98, 108, 105, 99, 95, 107, 101, 121}
    statePtr := int32(uintptr(unsafe.Pointer(&state)))
    ic0.Call_new(
        int32(uintptr(unsafe.Pointer(&callId))),
        20,
        int32(uintptr(unsafe.Pointer(&method))),
        16,
        callbackPtr,
        statePtr,
        callbackPtr,
        statePtr,
    )
}



func callback(statePtr int32) {
    var tmpState *State = (*State)(unsafe.Pointer(uintptr(statePtr)))
    tmpState.value = 3
}
  1. In the wasm environment, is this really the correct way to obtain the callback function pointer?

  2. the error info: function invocation does not match its signature, error code None

0

There are 0 best solutions below