mirror of
https://github.com/fumiama/gozel.git
synced 2026-06-05 00:10:24 +08:00
feat(zecall): impl. linux version
This commit is contained in:
2
go.mod
2
go.mod
@@ -1,3 +1,5 @@
|
|||||||
module github.com/fumiama/gozel
|
module github.com/fumiama/gozel
|
||||||
|
|
||||||
go 1.26.1
|
go 1.26.1
|
||||||
|
|
||||||
|
require github.com/ebitengine/purego v0.10.0
|
||||||
|
|||||||
2
go.sum
Normal file
2
go.sum
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
github.com/ebitengine/purego v0.10.0 h1:QIw4xfpWT6GWTzaW5XEKy3HXoqrJGx1ijYHzTF0/ISU=
|
||||||
|
github.com/ebitengine/purego v0.10.0/go.mod h1:iIjxzd6CiRiOG0UyXP+V1+jWqUXVjPKLAI0mRfJZTmQ=
|
||||||
69
internal/zecall/zecall_linux.go
Normal file
69
internal/zecall/zecall_linux.go
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
package zecall
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"syscall"
|
||||||
|
|
||||||
|
"github.com/ebitengine/purego"
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
zeLibraryName = "libze_loader.so"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
// ErrZeCallNotInit please call Init() first.
|
||||||
|
ErrZeCallNotInit = errors.New("zecall not init")
|
||||||
|
// ErrNoSuchProcess please register the process first.
|
||||||
|
ErrNoSuchProcess = errors.New("no such process")
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
libZeLoader uintptr
|
||||||
|
procMap = map[string]uintptr{}
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
if libZeLoader != 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
h, err := purego.Dlopen(zeLibraryName, purego.RTLD_LAZY|purego.RTLD_GLOBAL)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
libZeLoader = h
|
||||||
|
}
|
||||||
|
|
||||||
|
// Register a process for calling. For generated init only. Not thread-safe.
|
||||||
|
func Register(name string) error {
|
||||||
|
if libZeLoader == 0 {
|
||||||
|
return ErrZeCallNotInit
|
||||||
|
}
|
||||||
|
sym, err := purego.Dlsym(libZeLoader, name)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
procMap[name] = sym
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Call invokes a registered proc by name. For generated call only.
|
||||||
|
// The go:uintptrescapes directive tells the compiler that args may contain
|
||||||
|
// pointers converted to uintptr, so the GC will keep them alive during the call.
|
||||||
|
//
|
||||||
|
//go:uintptrescapes
|
||||||
|
func Call(name string, args ...uintptr) (r1, r2 uintptr, err error) {
|
||||||
|
fn, ok := procMap[name]
|
||||||
|
if !ok {
|
||||||
|
return 0, 0, ErrNoSuchProcess
|
||||||
|
}
|
||||||
|
var errno uintptr
|
||||||
|
r1, r2, errno = purego.SyscallN(fn, args...)
|
||||||
|
if r1 == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if errno != 0 {
|
||||||
|
err = syscall.Errno(errno)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package gozel
|
package zecall
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
|||||||
Reference in New Issue
Block a user