Skip to content

Commit

Permalink
remove golang.org/x/sys dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
JupiterRider committed Sep 30, 2024
1 parent 06d983f commit 21e556e
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 45 deletions.
6 changes: 3 additions & 3 deletions examples/simple/cos/main_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ package main

import (
"fmt"
"syscall"
"unsafe"

"github.com/jupiterrider/ffi"
"golang.org/x/sys/windows"
)

func main() {
// open the shared library
const libname = "ntdll.dll"
ntdll, err := windows.LoadLibrary(libname)
ntdll, err := syscall.LoadLibrary(libname)
if err != nil {
panic(fmt.Errorf("cannot load library %s: %w", libname, err))
}

// get the function's address
cos, err := windows.GetProcAddress(ntdll, "cos")
cos, err := syscall.GetProcAddress(ntdll, "cos")
if err != nil {
panic(err)
}
Expand Down
11 changes: 2 additions & 9 deletions examples/structs/raylib/main_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"github.com/ebitengine/purego"
"github.com/jupiterrider/ffi"
"golang.org/x/sys/unix"
)

type Texture struct {
Expand Down Expand Up @@ -54,10 +53,7 @@ func init() {
}

InitWindow = func(width, height int32, title string) {
byteTitle, err := unix.BytePtrFromString(title)
if err != nil {
panic(err)
}
byteTitle := &[]byte(title + "\x00")[0] // you can also use golang.org/x/sys/unix.BytePtrFromString to create a null-terminated string
ffi.Call(&cifInitWindow, symInitWindow, nil, unsafe.Pointer(&width), unsafe.Pointer(&height), unsafe.Pointer(&byteTitle))
}

Expand Down Expand Up @@ -140,10 +136,7 @@ func init() {
}

LoadTexture = func(filename string) Texture {
byteFilename, err := unix.BytePtrFromString(filename)
if err != nil {
panic(err)
}
byteFilename := &[]byte(filename + "\x00")[0] // you can also use golang.org/x/sys/unix.BytePtrFromString to create a null-terminated string
var texture Texture
ffi.Call(&cifLoadTexture, symLoadTexture, unsafe.Pointer(&texture), unsafe.Pointer(&byteFilename))
return texture
Expand Down
32 changes: 13 additions & 19 deletions examples/structs/raylib/main_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"fmt"
"image/color"
"runtime"
"syscall"
"unsafe"

"github.com/jupiterrider/ffi"
"golang.org/x/sys/windows"
)

type Texture struct {
Expand Down Expand Up @@ -38,7 +38,7 @@ func init() {
runtime.LockOSThread()

const libname = "raylib.dll"
raylib, err := windows.LoadLibrary(libname)
raylib, err := syscall.LoadLibrary(libname)
if err != nil {
panic(fmt.Errorf("cannot load library %s: %w", libname, err))
}
Expand All @@ -49,16 +49,13 @@ func init() {
panic(status)
}

symInitWindow, err := windows.GetProcAddress(raylib, "InitWindow")
symInitWindow, err := syscall.GetProcAddress(raylib, "InitWindow")
if err != nil {
panic(err)
}

InitWindow = func(width, height int32, title string) {
byteTitle, err := windows.BytePtrFromString(title)
if err != nil {
panic(err)
}
byteTitle := &[]byte(title + "\x00")[0] // you can also use golang.org/x/sys/windows.BytePtrFromString to create a null-terminated string
ffi.Call(&cifInitWindow, symInitWindow, nil, unsafe.Pointer(&width), unsafe.Pointer(&height), unsafe.Pointer(&byteTitle))
}

Expand All @@ -68,7 +65,7 @@ func init() {
panic(status)
}

symCloseWindow, err := windows.GetProcAddress(raylib, "CloseWindow")
symCloseWindow, err := syscall.GetProcAddress(raylib, "CloseWindow")
if err != nil {
panic(err)
}
Expand All @@ -83,7 +80,7 @@ func init() {
panic(status)
}

symWindowShouldClose, err := windows.GetProcAddress(raylib, "WindowShouldClose")
symWindowShouldClose, err := syscall.GetProcAddress(raylib, "WindowShouldClose")
if err != nil {
panic(err)
}
Expand All @@ -95,7 +92,7 @@ func init() {
}

// BeginDrawing -----------------------------
symBeginDrawing, err := windows.GetProcAddress(raylib, "BeginDrawing")
symBeginDrawing, err := syscall.GetProcAddress(raylib, "BeginDrawing")
if err != nil {
panic(err)
}
Expand All @@ -105,7 +102,7 @@ func init() {
}

// EndDrawing -------------------------------
symEndDrawing, err := windows.GetProcAddress(raylib, "EndDrawing")
symEndDrawing, err := syscall.GetProcAddress(raylib, "EndDrawing")
if err != nil {
panic(err)
}
Expand All @@ -120,7 +117,7 @@ func init() {
panic(status)
}

symClearBackground, err := windows.GetProcAddress(raylib, "ClearBackground")
symClearBackground, err := syscall.GetProcAddress(raylib, "ClearBackground")
if err != nil {
panic(err)
}
Expand All @@ -135,16 +132,13 @@ func init() {
panic(status)
}

symLoadTexture, err := windows.GetProcAddress(raylib, "LoadTexture")
symLoadTexture, err := syscall.GetProcAddress(raylib, "LoadTexture")
if err != nil {
panic(err)
}

LoadTexture = func(filename string) Texture {
byteFilename, err := windows.BytePtrFromString(filename)
if err != nil {
panic(err)
}
byteFilename := &[]byte(filename + "\x00")[0] // you can also use golang.org/x/sys/windows.BytePtrFromString to create a null-terminated string
var texture Texture
ffi.Call(&cifLoadTexture, symLoadTexture, unsafe.Pointer(&texture), unsafe.Pointer(&byteFilename))
return texture
Expand All @@ -156,7 +150,7 @@ func init() {
panic(status)
}

symUnloadTexture, err := windows.GetProcAddress(raylib, "UnloadTexture")
symUnloadTexture, err := syscall.GetProcAddress(raylib, "UnloadTexture")
if err != nil {
panic(err)
}
Expand All @@ -171,7 +165,7 @@ func init() {
panic(status)
}

symDrawTexture, err := windows.GetProcAddress(raylib, "DrawTexture")
symDrawTexture, err := syscall.GetProcAddress(raylib, "DrawTexture")
if err != nil {
panic(err)
}
Expand Down
3 changes: 1 addition & 2 deletions examples/varargs/libc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (

"github.com/ebitengine/purego"
"github.com/jupiterrider/ffi"
"golang.org/x/sys/unix"
)

func main() {
Expand Down Expand Up @@ -38,7 +37,7 @@ func main() {
panic(status)
}

text, _ := unix.BytePtrFromString("Pi is %f\n")
text := &[]byte("Pi is %f\n\x00")[0] // C requires a null-terminated string
pi := math.Pi
var nCharsPrinted ffi.Arg
ffi.Call(&cif, printf, unsafe.Pointer(&nCharsPrinted), unsafe.Pointer(&text), unsafe.Pointer(&pi))
Expand Down
5 changes: 1 addition & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,4 @@ module github.com/jupiterrider/ffi

go 1.18

require (
github.com/ebitengine/purego v0.8.0
golang.org/x/sys v0.25.0
)
require github.com/ebitengine/purego v0.8.0
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
github.com/ebitengine/purego v0.8.0 h1:JbqvnEzRvPpxhCJzJJ2y0RbiZ8nyjccVUrSM3q+GvvE=
github.com/ebitengine/purego v0.8.0/go.mod h1:iIjxzd6CiRiOG0UyXP+V1+jWqUXVjPKLAI0mRfJZTmQ=
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
11 changes: 5 additions & 6 deletions init_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,28 @@ package ffi

import (
"fmt"

"golang.org/x/sys/windows"
"syscall"
)

func init() {
const filename = "libffi-8.dll"

libffi, err := windows.LoadLibrary(filename)
libffi, err := syscall.LoadLibrary(filename)
if err != nil {
panic(fmt.Errorf("cannot load library %s: %w", filename, err))
}

prepCif, err = windows.GetProcAddress(libffi, "ffi_prep_cif")
prepCif, err = syscall.GetProcAddress(libffi, "ffi_prep_cif")
if err != nil {
panic(err)
}

prepCifVar, err = windows.GetProcAddress(libffi, "ffi_prep_cif_var")
prepCifVar, err = syscall.GetProcAddress(libffi, "ffi_prep_cif_var")
if err != nil {
panic(err)
}

call, err = windows.GetProcAddress(libffi, "ffi_call")
call, err = syscall.GetProcAddress(libffi, "ffi_call")
if err != nil {
panic(err)
}
Expand Down

0 comments on commit 21e556e

Please sign in to comment.