1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: 2022 The Ebitengine Authors
3
4package purego
5
6import (
7 "errors"
8 "reflect"
9 "sync"
10 "syscall"
11 "unsafe"
12)
13
14var syscall15XABI0 uintptr
15
16func syscall_syscall15X(fn, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15 uintptr) (r1, r2, err uintptr) {
17 r1, r2, errno := syscall.Syscall15(fn, 15, a1, a2, a3, a4, a5, a6, a7, a8, a9, a10, a11, a12, a13, a14, a15)
18 return r1, r2, uintptr(errno)
19}
20
21// NewCallback converts a Go function to a function pointer conforming to the stdcall calling convention.
22// This is useful when interoperating with Windows code requiring callbacks. The argument is expected to be a
23// function with one uintptr-sized result. The function must not have arguments with size larger than the
24// size of uintptr. Only a limited number of callbacks may be created in a single Go process, and any memory
25// allocated for these callbacks is never released. Between NewCallback and NewCallbackCDecl, at least 1024
26// callbacks can always be created. Although this function is similiar to the darwin version it may act
27// differently.
28func NewCallback(fn any) uintptr {
29 isCDecl := false
30 ty := reflect.TypeOf(fn)
31 for i := 0; i < ty.NumIn(); i++ {
32 in := ty.In(i)
33 if !in.AssignableTo(reflect.TypeOf(CDecl{})) {
34 continue
35 }
36 if i != 0 {
37 panic("purego: CDecl must be the first argument")
38 }
39 isCDecl = true
40 }
41 if isCDecl {
42 return syscall.NewCallbackCDecl(fn)
43 }
44 return syscall.NewCallback(fn)
45}
46
47// NewCallbackFnPtr converts a Go function pointer to a Windows callback and reuses an existing callback when possible.
48func NewCallbackFnPtr(fnPtr any) uintptr {
49 val := reflect.ValueOf(fnPtr)
50 if val.IsNil() {
51 panic("purego: function must not be nil")
52 }
53 if val.Kind() != reflect.Ptr || val.Elem().Kind() != reflect.Func {
54 panic("purego: the type must be a function pointer but was not")
55 }
56 if addr, ok := getCallbackByFnPtr(val); ok {
57 return addr
58 }
59 addr := NewCallback(val.Elem().Interface())
60 cbs.lock.Lock()
61 cbs.knownFnPtr[val.Pointer()] = addr
62 cbs.lock.Unlock()
63 return addr
64}
65
66// UnrefCallback is unsupported on Windows because runtime callback slots cannot be reclaimed.
67func UnrefCallback(cb uintptr) error {
68 if cb == 0 {
69 return errors.New("callback not found")
70 }
71 return errors.New("purego: unreferencing callbacks is unsupported on windows")
72}
73
74// UnrefCallbackFnPtr is unsupported on Windows because runtime callback slots cannot be reclaimed.
75func UnrefCallbackFnPtr(fnPtr any) error {
76 val := reflect.ValueOf(fnPtr)
77 if val.IsNil() {
78 panic("purego: function must not be nil")
79 }
80 if val.Kind() != reflect.Ptr || val.Elem().Kind() != reflect.Func {
81 panic("purego: the type must be a function pointer but was not")
82 }
83 return errors.New("purego: unreferencing callbacks is unsupported on windows")
84}
85
86// maxCb is the maximum number of tracked Windows callback function pointers.
87const maxCB = 1024
88
89var cbs = struct {
90 lock sync.RWMutex
91 knownFnPtr map[uintptr]uintptr
92}{
93 knownFnPtr: make(map[uintptr]uintptr, maxCB),
94}
95
96func getCallbackByFnPtr(val reflect.Value) (uintptr, bool) {
97 cbs.lock.RLock()
98 defer cbs.lock.RUnlock()
99 addr, ok := cbs.knownFnPtr[val.Pointer()]
100 return addr, ok
101}
102
103func loadSymbol(handle uintptr, name string) (uintptr, error) {
104 return syscall.GetProcAddress(syscall.Handle(handle), name)
105}
106
107// callbackMaxFrame is a stub definition.
108const callbackMaxFrame = 0
109
110func callbackArgFromStack(argsBase unsafe.Pointer, stackSlot int, stackByteOffset *uintptr, inType reflect.Type) reflect.Value {
111 panic("purego: callbackArgFromStack should not be called on windows")
112}