Skip to content

Commit

Permalink
Split C util funcs to separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
diamondburned committed Oct 22, 2024
1 parent ff5fdfa commit b050c73
Show file tree
Hide file tree
Showing 11 changed files with 123 additions and 104 deletions.
6 changes: 6 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = nixpkgs.legacyPackages.${system};
cmds = with pkgs; [
(writeShellScriptBin
"gir-generate"
"go run github.com/diamondburned/gotk4/gir/cmd/gir-generate \"$@\"")
];
in
{
devShells.default = import "${gotk4-nix}/shell.nix" {
Expand All @@ -38,6 +43,7 @@
})
];
};
buildInputs = _: ([] ++ cmds);
};
packages.dockerEnv = let
env = pkgs.writeShellScriptBin "docker-env" ''
Expand Down
2 changes: 1 addition & 1 deletion gir/girgen/generators/class-interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ var classInterfaceTmpl = gotmpl.NewGoTemplate(`
{{ $needsPrivate = true -}}
//
// To get the original type, the caller must assert this to an interface or
// another type.
// another type or use [Base{{ .StructName }}].
type {{ .InterfaceName }} interface {
coreglib.Objector
base{{ .StructName }}() *{{ .StructName }}
Expand Down
40 changes: 40 additions & 0 deletions pkg/core/girepository/cstr.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package girepository

// #include <girepository.h>
import "C"

import (
"log"
"reflect"
"strings"
"unsafe"
)

func trimNull(str string) string {
return strings.TrimSuffix(str, "\x00")
}

func unsafeCStr(gostr *string) *C.gchar {
if len(*gostr) == 0 {
return nil
}

if (*gostr)[len(*gostr)-1] != 0 {
log.Panicf("Go string %q should be null-terminated with \\x00", *gostr)
}

header := (*reflect.StringHeader)(unsafe.Pointer(gostr))
return (*C.gchar)(unsafe.Pointer(header.Data))
}

func cpyCStr(cptr *C.gchar, gostr string) (next *C.gchar) {
cbytes := unsafe.Slice((*byte)(unsafe.Pointer(cptr)), len(gostr)+1)
copy(cbytes, []byte(gostr))
cbytes[len(gostr)] = 0

return (*C.gchar)(unsafe.Add(unsafe.Pointer(cptr), len(cbytes)))
}

func cstr2bytes[T IntegerType](cstr *C.gchar, len int) []byte {
return unsafe.Slice((*byte)(unsafe.Pointer(cptr)), len)
}
27 changes: 0 additions & 27 deletions pkg/core/girepository/girepository.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import "C"
import (
"fmt"
"log"
"reflect"
"strings"
"sync"
"unsafe"

Expand Down Expand Up @@ -112,14 +110,6 @@ func (k infoKey) String() string {
return name
}

func cpyCStr(cptr *C.gchar, gostr string) (next *C.gchar) {
cbytes := unsafe.Slice((*byte)(unsafe.Pointer(cptr)), len(gostr)+1)
copy(cbytes, []byte(gostr))
cbytes[len(gostr)] = 0

return (*C.gchar)(unsafe.Add(unsafe.Pointer(cptr), len(cbytes)))
}

func (k infoCKey) free() {
C.free(unsafe.Pointer(k[0]))
}
Expand Down Expand Up @@ -298,20 +288,3 @@ func invokeFunc(info *Info, in, out []Argument) Argument {

return ret
}

func trimNull(str string) string {
return strings.TrimSuffix(str, "\x00")
}

func unsafeCStr(gostr *string) *C.gchar {
if len(*gostr) == 0 {
return nil
}

if (*gostr)[len(*gostr)-1] != 0 {
log.Panicf("Go string %q should be null-terminated with \\x00", *gostr)
}

header := (*reflect.StringHeader)(unsafe.Pointer(gostr))
return (*C.gchar)(unsafe.Pointer(header.Data))
}
14 changes: 7 additions & 7 deletions pkg/gdk/v3/gdk.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 13 additions & 13 deletions pkg/gdk/v4/gdk.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit b050c73

Please sign in to comment.