License: MIT
Signed-off-by: Jakub Sztandera <kubuxu@protonmail.ch>
This commit is contained in:
Jakub Sztandera 2019-07-11 17:47:05 +02:00
parent 6f2cd71e86
commit 219fc093b1
3 changed files with 17 additions and 8 deletions

View File

@ -57,7 +57,7 @@ type unmarshalCBOR interface {
var tUnmarhsalCBOR = reflect.TypeOf((*unmarshalCBOR)(nil)).Elem()
var tError = reflect.TypeOf((*error)(nil)).Elem()
func (_ *invoker) transform(instance interface{}) (nativeCode, error) {
func (*invoker) transform(instance interface{}) (nativeCode, error) {
itype := reflect.TypeOf(instance)
newErr := func(str string) error {
return fmt.Errorf("transform(%s): %s", itype.Name(), str)
@ -95,11 +95,11 @@ func (_ *invoker) transform(instance interface{}) (nativeCode, error) {
}
if !t.In(3).Implements(tUnmarhsalCBOR) {
return nil, newErr("paramter doesn't implement UnmarshalCBOR")
return nil, newErr("parameter doesn't implement UnmarshalCBOR")
}
if t.In(3).Kind() != reflect.Ptr {
return nil, newErr("paramter has to be a pointer")
return nil, newErr("parameter has to be a pointer")
}
if t.NumOut() != 2 {
@ -124,12 +124,20 @@ func (_ *invoker) transform(instance interface{}) (nativeCode, error) {
code := make(nativeCode, maxn+1)
_ = code
for id, meth := range invokes {
meth := meth
code[id] = reflect.MakeFunc(reflect.TypeOf((invokeFunc)(nil)),
func(in []reflect.Value) []reflect.Value {
paramT := meth.Type.In(3).Elem()
param := reflect.New(paramT)
param.Interface().(unmarshalCBOR).UnmarshalCBOR(in[2].Interface().([]byte))
inBytes := in[2].Interface().([]byte)
_, err := param.Interface().(unmarshalCBOR).UnmarshalCBOR(inBytes)
if err != nil {
return []reflect.Value{
reflect.ValueOf(InvokeRet{}),
reflect.ValueOf(err),
}
}
return meth.Func.Call([]reflect.Value{
reflect.ValueOf(instance), in[0], in[1], param,
})

View File

@ -16,16 +16,16 @@ func (b *basicParams) UnmarshalCBOR(in []byte) (int, error) {
return 1, nil
}
func (_ basicContract) InvokeSomething0(act *Actor, vmctx *VMContext,
func (basicContract) InvokeSomething0(act *Actor, vmctx *VMContext,
params *basicParams) (InvokeRet, error) {
return InvokeRet{
returnCode: params.b,
}, nil
}
func (_ basicContract) InvokeSomething10(act *Actor, vmctx *VMContext,
func (basicContract) InvokeSomething10(act *Actor, vmctx *VMContext,
params *basicParams) (InvokeRet, error) {
return InvokeRet{
returnCode: params.b,
returnCode: params.b + 10,
}, nil
}
@ -39,5 +39,5 @@ func TestInvokerBasic(t *testing.T) {
ret, err = code[10](nil, nil, []byte{1})
assert.NoError(t, err)
assert.Equal(t, byte(1), ret.returnCode, "return code should be 1")
assert.Equal(t, byte(11), ret.returnCode, "return code should be 1")
}

View File

@ -86,6 +86,7 @@ func NewVM(base cid.Cid, height uint64, maddr address.Address, cs *ChainStore) (
buf: buf,
blockHeight: height,
blockMiner: maddr,
inv: newInvoker(),
}, nil
}