-
Notifications
You must be signed in to change notification settings - Fork 8
/
stdarg.go
33 lines (29 loc) · 785 Bytes
/
stdarg.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
package libc
import (
"reflect"
"unsafe"
)
// VAArg returns a pointer to the next argument in a varargs list. The actual
// type of list is *[]interface{}, but it is declared as void * in C.
func VAArg(list *byte) *byte {
vl := (*[]interface{})(unsafe.Pointer(list))
arg := (*vl)[0]
*vl = (*vl)[1:]
var intVal int32
switch arg := arg.(type) {
case byte:
intVal = int32(arg)
return (*byte)(unsafe.Pointer(&intVal))
case int16:
intVal = int32(arg)
return (*byte)(unsafe.Pointer(&intVal))
case int32:
intVal = arg
return (*byte)(unsafe.Pointer(&intVal))
}
// Use reflect to make a copy of arg that we can take the address of.
av := reflect.ValueOf(arg)
p := reflect.New(av.Type())
p.Elem().Set(av)
return (*byte)(unsafe.Pointer((p.Elem().UnsafeAddr())))
}