From 4a0d2b118a6d2ff8bcc1041ec176942cee9821b1 Mon Sep 17 00:00:00 2001 From: Denys Smirnov Date: Tue, 30 May 2023 22:07:59 +0300 Subject: [PATCH] Fix header registry. --- libs/reglibs.go | 44 ++++++++++++++++++-------------------------- libs/reglibs_test.go | 15 ++++++++------- 2 files changed, 26 insertions(+), 33 deletions(-) diff --git a/libs/reglibs.go b/libs/reglibs.go index dbde302..80faacb 100644 --- a/libs/reglibs.go +++ b/libs/reglibs.go @@ -7,38 +7,30 @@ import ( "strings" ) -//go:embed includes/embed/* +//go:embed includes/embed var efs embed.FS func init() { - - onFile := func(path string, d fs.DirEntry, err error) error { - if d.IsDir() { + const root = "includes/embed" + err := fs.WalkDir(efs, root, func(path string, d fs.DirEntry, err error) error { + if err != nil { + return err + } else if d.IsDir() { return nil } - return registerInclude(path) - } - - err := fs.WalkDir(efs, ".", onFile) - + data, err := efs.ReadFile(path) + if err != nil { + return err + } + fname := strings.TrimPrefix(path, root+"/") + RegisterLibrary(fname, func(env *Env) *Library { + return &Library{ + Header: fmt.Sprintf("#include <%s>\n%s", BuiltinH, string(data)), + } + }) + return nil + }) if err != nil { panic(err) } } - -func registerInclude(fName string) error { - - fBuff, fErr := efs.ReadFile(fName) - - if fErr != nil { - return fErr - } - - RegisterLibrary(strings.TrimPrefix(fName, "includes/"), func(env *Env) *Library { - return &Library{ - Header: fmt.Sprintf("#include <%s>\n%s", BuiltinH, string(fBuff)), - } - }) - - return nil -} diff --git a/libs/reglibs_test.go b/libs/reglibs_test.go index 21dff9d..68cf763 100644 --- a/libs/reglibs_test.go +++ b/libs/reglibs_test.go @@ -1,30 +1,31 @@ package libs import ( - "github.com/gotranspile/cxgo/types" - "github.com/stretchr/testify/require" "testing" + + "github.com/stretchr/testify/require" + + "github.com/gotranspile/cxgo/types" ) var autoRegLibs = []string{ - "AL/alc.h", "AL/al.h", - + "dirent.h", "fcntl.h", "float.h", "getopt.h", - + "GL/gl.h", - + "inttypes.h", "libgen.h", "sched.h", "semaphore.h", "signal.h", "strings.h", - + "sys/mkdev.h", "windows.h", }