| 1 |
// SPDX-License-Identifier: Apache-2.0 |
| 2 |
// SPDX-FileCopyrightText: 2022 The Ebitengine Authors |
| 3 |
|
| 4 |
package purego |
| 5 |
|
| 6 |
import ( |
| 7 |
"reflect" |
| 8 |
"syscall" |
| 9 |
"unsafe" |
| 10 |
) |
| 11 |
|
| 12 |
var syscallXABI0 uintptr |
| 13 |
|
| 14 |
func syscall_syscallN(fn uintptr, args ...uintptr) (r1, r2, err uintptr) { |
| 15 |
r1, r2, errno := syscall.SyscallN(fn, args...) |
| 16 |
return r1, r2, uintptr(errno) |
| 17 |
} |
| 18 |
|
| 19 |
// NewCallback converts a Go function to a function pointer conforming to the stdcall calling convention. |
| 20 |
// This is useful when interoperating with Windows code requiring callbacks. The argument is expected to be a |
| 21 |
// function with one uintptr-sized result. The function must not have arguments with size larger than the |
| 22 |
// size of uintptr. Only a limited number of callbacks may be created in a single Go process, and any memory |
| 23 |
// allocated for these callbacks is never released. Between NewCallback and NewCallbackCDecl, at least 1024 |
| 24 |
// callbacks can always be created. Although this function is similar to the darwin version it may act |
| 25 |
// differently. |
| 26 |
func NewCallback(fn any) uintptr { |
| 27 |
isCDecl := false |
| 28 |
ty := reflect.TypeOf(fn) |
| 29 |
for i := range ty.NumIn() { |
| 30 |
in := ty.In(i) |
| 31 |
if !in.AssignableTo(reflect.TypeFor[CDecl]()) { |
| 32 |
continue |
| 33 |
} |
| 34 |
if i != 0 { |
| 35 |
panic("purego: CDecl must be the first argument") |
| 36 |
} |
| 37 |
isCDecl = true |
| 38 |
} |
| 39 |
if isCDecl { |
| 40 |
return syscall.NewCallbackCDecl(fn) |
| 41 |
} |
| 42 |
return syscall.NewCallback(fn) |
| 43 |
} |
| 44 |
|
| 45 |
func loadSymbol(handle uintptr, name string) (uintptr, error) { |
| 46 |
return syscall.GetProcAddress(syscall.Handle(handle), name) |
| 47 |
} |
| 48 |
|
| 49 |
// callbackMaxFrame is a stub definition. |
| 50 |
const callbackMaxFrame = 0 |
| 51 |
|
| 52 |
func callbackArgFromStack(argsBase unsafe.Pointer, stackSlot int, stackByteOffset *uintptr, inType reflect.Type) reflect.Value { |
| 53 |
panic("purego: callbackArgFromStack should not be called on windows") |
| 54 |
} |