Created
November 5, 2015 16:44
-
-
Save obscuren/9bbf24e972c3eb06343a to your computer and use it in GitHub Desktop.
This file contains hidden or 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
func ExampleExecute_ffi() { | |
// enable FFI | |
vm.EnableFFI() | |
cfg := &Config{ | |
DisableJit: true, | |
// create ffi model and functions | |
Ffi: vm.Ffi{ | |
0: func(in []byte) ([]byte, error) { | |
reverse := make([]byte, len(in)) | |
for i := 0; i < len(in); i++ { | |
reverse[len(in)-i-1] = in[i] | |
} | |
return reverse, nil | |
}, | |
}, | |
} | |
// hello push string | |
helloPush := append([]byte{byte(vm.PUSH11)}, []byte("hello world")...) | |
ret, _ := Execute(append(helloPush, []byte{ | |
byte(vm.PUSH1), 0, // push 0'th offset as start pos for mem write | |
byte(vm.MSTORE), // write hello world to 0th offset | |
byte(vm.PUSH1), 32, byte(vm.PUSH1), 0, // push 0 to 32 for input mem read | |
byte(vm.PUSH1), 32, byte(vm.PUSH1), 0, // push 0 to 32 for ret mem write | |
byte(vm.PUSH1), 0x0, // push FFI address 0 | |
byte(vm.FFI), // execute FFI 0 addr | |
byte(vm.PUSH1), 32, byte(vm.PUSH1), 0, // push 0 to 32 for return mem read | |
byte(vm.RETURN), // return memory | |
}...), nil, cfg) | |
fmt.Printf("%x\n", ret) | |
// Output: | |
// 646c726f77206f6c6c6568000000000000000000000000000000000000000000 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment