-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathffi.go
44 lines (35 loc) · 1.23 KB
/
ffi.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package idris_go_rts
import . "reflect"
//-------------------------------------------------------------------------------------------------
// Function used for calls from go into Idris
//-------------------------------------------------------------------------------------------------
func ProxyFunction(vm *VirtualMachine,
applyFn vmFunction,
con interface{},
args ...interface{}) {
// Create (empty) private stack and use it for this context.
var savedCallStack []CallPair
copy(savedCallStack, (*vm).CallStack)
(*vm).CallStack = make([]CallPair, 0)
conType := ValueOf(con).Type()
res := con
apply := func(arg interface{}) {
if ValueOf(res).Type() == conType {
Reserve(vm, (*vm).ValueStackTop + 2)
(*vm).ValueStack[(*vm).ValueStackTop] = res
(*vm).ValueStack[(*vm).ValueStackTop + 1] = arg
oldbase := (*vm).ValueStackBase
(*vm).ValueStackBase = (*vm).ValueStackTop
(*vm).ValueStackTop += 2
Call(vm, applyFn, oldbase)
res = (*vm).ReturnValue
}
}
for _, arg := range args {
apply(arg)
}
// Specifically for cases of IO functions
apply(res)
// Restore the original stack
copy((*vm).CallStack, savedCallStack)
}