From 8c04b0e965559942433e14a3a3751539d57463c7 Mon Sep 17 00:00:00 2001 From: Tim van Osch Date: Thu, 26 Dec 2024 19:21:48 +0100 Subject: [PATCH] style: Run gofumpt over all source --- _example/alloc.go | 19 ++++++++------- _example/basic.go | 6 +++-- _example/error.go | 10 +++++--- _example/library.go | 4 +-- _example/panic.go | 9 ++++--- _example/userdata.go | 8 +++--- lua/dummy.go | 1 + lua/golua.go | 7 ++++-- lua/golua_c_lua51.go | 3 ++- lua/golua_c_lua52.go | 3 ++- lua/golua_c_lua53.go | 3 ++- lua/golua_c_lua54.go | 3 ++- lua/lauxlib.go | 3 ++- lua/lua.go | 57 ++++++++++++++++++++++--------------------- lua/lua51/dummy.go | 1 + lua/lua52/dummy.go | 1 + lua/lua53/dummy.go | 1 + lua/lua54/dummy.go | 1 + lua/lua_defs_lua51.go | 3 ++- lua/lua_defs_lua52.go | 3 ++- lua/lua_defs_lua53.go | 3 ++- lua/lua_defs_lua54.go | 3 ++- lua/lua_test.go | 1 - 23 files changed, 89 insertions(+), 64 deletions(-) diff --git a/_example/alloc.go b/_example/alloc.go index cd3b437d..a4abe90c 100644 --- a/_example/alloc.go +++ b/_example/alloc.go @@ -1,14 +1,16 @@ package main -import "github.com/aarzilli/golua/lua" -import "unsafe" -import "fmt" +import ( + "github.com/aarzilli/golua/lua" + "unsafe" + "fmt" +) var refHolder = map[unsafe.Pointer][]byte{} -//a terrible allocator! -//meant to be illustrative of the mechanics, -//not usable as an actual implementation +// a terrible allocator! +// meant to be illustrative of the mechanics, +// not usable as an actual implementation func AllocatorF(ptr unsafe.Pointer, osize uint, nsize uint) unsafe.Pointer { if nsize == 0 { if _, ok := refHolder[ptr]; ok { @@ -27,7 +29,7 @@ func AllocatorF(ptr unsafe.Pointer, osize uint, nsize uint) unsafe.Pointer { ptr = unsafe.Pointer(&(slice[0])) refHolder[ptr] = slice } - //fmt.Println("in allocf"); + // fmt.Println("in allocf"); return ptr } @@ -36,8 +38,7 @@ func A2(ptr unsafe.Pointer, osize uint, nsize uint) unsafe.Pointer { } func main() { - - //refHolder = make([][]byte,0,500); + // refHolder = make([][]byte,0,500); L := lua.NewStateAlloc(AllocatorF) defer L.Close() diff --git a/_example/basic.go b/_example/basic.go index 28f766d6..3013a191 100644 --- a/_example/basic.go +++ b/_example/basic.go @@ -1,7 +1,9 @@ package main -import "github.com/aarzilli/golua/lua" -import "fmt" +import ( + "github.com/aarzilli/golua/lua" + "fmt" +) func test(L *lua.State) int { fmt.Println("hello world! from go!") diff --git a/_example/error.go b/_example/error.go index 5ae65d93..08120014 100644 --- a/_example/error.go +++ b/_example/error.go @@ -1,9 +1,11 @@ package main -import "github.com/aarzilli/golua/lua" -import "fmt" -import "errors" -import "os" +import ( + "github.com/aarzilli/golua/lua" + "fmt" + "errors" + "os" +) func testDefault(L *lua.State) { err := L.DoString("print(\"Unknown variable\" .. x)") diff --git a/_example/library.go b/_example/library.go index d968d82f..110028bd 100644 --- a/_example/library.go +++ b/_example/library.go @@ -13,7 +13,7 @@ func test2(L *lua.State) int { } var funcs = map[string]lua.LuaGoFunction{ - "test": test, + "test": test, "test2": test2, } @@ -27,7 +27,7 @@ func main() { L := lua.NewState() defer L.Close() L.OpenLibs() - + L.RegisterLibrary("example", funcs) if err := L.DoString(code); err != nil { diff --git a/_example/panic.go b/_example/panic.go index ef8b9d7d..cdc36cf0 100644 --- a/_example/panic.go +++ b/_example/panic.go @@ -1,7 +1,9 @@ package main -import "github.com/aarzilli/golua/lua" -import "fmt" +import ( + "github.com/aarzilli/golua/lua" + "fmt" +) func test(L *lua.State) int { fmt.Println("hello world! from go!") @@ -9,7 +11,6 @@ func test(L *lua.State) int { } func main() { - var L *lua.State L = lua.NewState() @@ -29,7 +30,7 @@ func main() { L.AtPanic(newPanic) - //force a panic + // force a panic L.PushNil() L.Call(0, 0) diff --git a/_example/userdata.go b/_example/userdata.go index 5cd56b36..e4c9e06f 100644 --- a/_example/userdata.go +++ b/_example/userdata.go @@ -1,8 +1,10 @@ package main -import "github.com/aarzilli/golua/lua" -import "unsafe" -import "fmt" +import ( + "github.com/aarzilli/golua/lua" + "unsafe" + "fmt" +) type Userdata struct { a, b int diff --git a/lua/dummy.go b/lua/dummy.go index 491fbb8b..485ec273 100644 --- a/lua/dummy.go +++ b/lua/dummy.go @@ -1,3 +1,4 @@ +//go:build dummy // +build dummy // This file is part of a workaround for `go mod vendor` which won't diff --git a/lua/golua.go b/lua/golua.go index 09ad9025..d09f5ad2 100644 --- a/lua/golua.go +++ b/lua/golua.go @@ -32,6 +32,7 @@ type HookFunction func(L *State) const ExecutionQuantumExceeded = "Lua execution quantum exceeded" // Wrapper to keep cgo from complaining about incomplete ptr type +// //export State type State struct { // Wrapped lua_State object @@ -55,8 +56,10 @@ type State struct { ctx context.Context } -var goStates map[uintptr]*State -var goStatesMutex sync.Mutex +var ( + goStates map[uintptr]*State + goStatesMutex sync.Mutex +) func init() { goStates = make(map[uintptr]*State, 16) diff --git a/lua/golua_c_lua51.go b/lua/golua_c_lua51.go index 4b59d4c5..1b08dfff 100644 --- a/lua/golua_c_lua51.go +++ b/lua/golua_c_lua51.go @@ -1,4 +1,5 @@ -//+build !lua52,!lua53,!lua54 +//go:build !lua52 && !lua53 && !lua54 +// +build !lua52,!lua53,!lua54 package lua diff --git a/lua/golua_c_lua52.go b/lua/golua_c_lua52.go index a792ccd1..ec891f81 100644 --- a/lua/golua_c_lua52.go +++ b/lua/golua_c_lua52.go @@ -1,4 +1,5 @@ -//+build lua52 +//go:build lua52 +// +build lua52 package lua diff --git a/lua/golua_c_lua53.go b/lua/golua_c_lua53.go index d490b1b0..5a11aec8 100644 --- a/lua/golua_c_lua53.go +++ b/lua/golua_c_lua53.go @@ -1,4 +1,5 @@ -//+build lua53 +//go:build lua53 +// +build lua53 package lua diff --git a/lua/golua_c_lua54.go b/lua/golua_c_lua54.go index 9acafe53..e27e4df0 100644 --- a/lua/golua_c_lua54.go +++ b/lua/golua_c_lua54.go @@ -1,4 +1,5 @@ -//+build lua54 +//go:build lua54 +// +build lua54 package lua diff --git a/lua/lauxlib.go b/lua/lauxlib.go index 5dc43f53..282187f4 100644 --- a/lua/lauxlib.go +++ b/lua/lauxlib.go @@ -6,6 +6,7 @@ package lua //#include //#include "golua.h" import "C" + import ( "context" "unsafe" @@ -78,7 +79,7 @@ func (L *State) CheckString(narg int) string { // // BUG(everyone_involved): not implemented func (L *State) CheckOption(narg int, def string, lst []string) int { - //TODO: complication: lst conversion to const char* lst[] from string slice + // TODO: complication: lst conversion to const char* lst[] from string slice return 0 } diff --git a/lua/lua.go b/lua/lua.go index 15d365bf..207d0b1c 100644 --- a/lua/lua.go +++ b/lua/lua.go @@ -46,6 +46,7 @@ package lua */ import "C" + import ( "fmt" "unsafe" @@ -68,39 +69,39 @@ func newState(L *C.lua_State) *State { func (L *State) addFreeIndex(i uint) { freelen := len(L.freeIndices) - //reallocate if necessary + // reallocate if necessary if freelen+1 > cap(L.freeIndices) { newSlice := make([]uint, freelen, cap(L.freeIndices)*2) copy(newSlice, L.freeIndices) L.freeIndices = newSlice } - //reslice + // reslice L.freeIndices = L.freeIndices[0 : freelen+1] L.freeIndices[freelen] = i } func (L *State) getFreeIndex() (index uint, ok bool) { freelen := len(L.freeIndices) - //if there exist entries in the freelist + // if there exist entries in the freelist if freelen > 0 { - i := L.freeIndices[freelen-1] //get index - //fmt.Printf("Free indices before: %v\n", L.freeIndices) + i := L.freeIndices[freelen-1] // get index + // fmt.Printf("Free indices before: %v\n", L.freeIndices) L.freeIndices = L.freeIndices[0 : freelen-1] //'pop' index from list - //fmt.Printf("Free indices after: %v\n", L.freeIndices) + // fmt.Printf("Free indices after: %v\n", L.freeIndices) return i, true } return 0, false } -//returns the registered function id +// returns the registered function id func (L *State) register(f interface{}) uint { - //fmt.Printf("Registering %v\n") + // fmt.Printf("Registering %v\n") index, ok := L.getFreeIndex() - //fmt.Printf("\tfreeindex: index = %v, ok = %v\n", index, ok) - //if not ok, then we need to add new index by extending the slice + // fmt.Printf("\tfreeindex: index = %v, ok = %v\n", index, ok) + // if not ok, then we need to add new index by extending the slice if !ok { index = uint(len(L.registry)) - //reallocate backing array if necessary + // reallocate backing array if necessary if index+1 > uint(cap(L.registry)) { newcap := cap(L.registry) * 2 if index+1 > uint(newcap) { @@ -110,16 +111,16 @@ func (L *State) register(f interface{}) uint { copy(newSlice, L.registry) L.registry = newSlice } - //reslice + // reslice L.registry = L.registry[0 : index+1] } - //fmt.Printf("\tregistering %d %v\n", index, f) + // fmt.Printf("\tregistering %d %v\n", index, f) L.registry[index] = f return index } func (L *State) unregister(fid uint) { - //fmt.Printf("Unregistering %d (len: %d, value: %v)\n", fid, len(L.registry), L.registry[fid]) + // fmt.Printf("Unregistering %d (len: %d, value: %v)\n", fid, len(L.registry), L.registry[fid]) if (fid < uint(len(L.registry))) && (L.registry[fid] != nil) { L.registry[fid] = nil L.addFreeIndex(fid) @@ -144,14 +145,14 @@ func (L *State) PushGoClosure(f LuaGoFunction) { // // The code: // -// L.LGetMetaTable(tableName) -// L.SetMetaMethod(methodName, function) +// L.LGetMetaTable(tableName) +// L.SetMetaMethod(methodName, function) // // is the logical equivalent of: // -// L.LGetMetaTable(tableName) -// L.PushGoFunction(function) -// L.SetField(-2, methodName) +// L.LGetMetaTable(tableName) +// L.PushGoFunction(function) +// L.SetField(-2, methodName) // // except this wouldn't work because pushing a go function results in user data not a cfunction func (L *State) SetMetaMethod(methodName string, f LuaGoFunction) { @@ -172,7 +173,7 @@ func (L *State) PushGoStruct(iface interface{}) { // // This function doesn't save a reference to the interface, it is the responsibility of the caller of this function to insure that the interface outlasts the lifetime of the lua object that this function creates. func (L *State) PushLightUserdata(ud *interface{}) { - //push + // push C.lua_pushlightuserdata(L.s, unsafe.Pointer(ud)) } @@ -193,7 +194,7 @@ func (L *State) AtPanic(panicf LuaGoFunction) (oldpanicf LuaGoFunction) { switch i := oldres.(type) { case C.uint: f := L.registry[uint(i)].(LuaGoFunction) - //free registry entry + // free registry entry L.unregister(uint(i)) return f case C.lua_CFunction: @@ -201,8 +202,8 @@ func (L *State) AtPanic(panicf LuaGoFunction) (oldpanicf LuaGoFunction) { return int(C.clua_callluacfunc(L1.s, i)) } } - //generally we only get here if the panicf got set to something like nil - //potentially dangerous because we may silently fail + // generally we only get here if the panicf got set to something like nil + // potentially dangerous because we may silently fail return nil } @@ -350,8 +351,8 @@ func (L *State) NewTable() { // lua_newthread func (L *State) NewThread() *State { - //TODO: call newState with result from C.lua_newthread and return it - //TODO: should have same lists as parent + // TODO: call newState with result from C.lua_newthread and return it + // TODO: should have same lists as parent // but may complicate gc s := C.lua_newthread(L.s) return &State{s, 0, nil, nil, nil, nil, nil} @@ -365,8 +366,8 @@ func (L *State) Next(index int) int { // lua_objlen // lua_pop func (L *State) Pop(n int) { - //Why is this implemented this way? I don't get it... - //C.lua_pop(L.s, C.int(n)); + // Why is this implemented this way? I don't get it... + // C.lua_pop(L.s, C.int(n)); C.lua_settop(L.s, C.int(-n-1)) } @@ -543,7 +544,7 @@ func (L *State) ToPointer(index int) uintptr { // lua_tothread func (L *State) ToThread(index int) *State { - //TODO: find a way to link lua_State* to existing *State, return that + // TODO: find a way to link lua_State* to existing *State, return that return &State{} } diff --git a/lua/lua51/dummy.go b/lua/lua51/dummy.go index 651a7971..b632fce4 100644 --- a/lua/lua51/dummy.go +++ b/lua/lua51/dummy.go @@ -1,3 +1,4 @@ +//go:build dummy // +build dummy // Package lua contains only a C header files. diff --git a/lua/lua52/dummy.go b/lua/lua52/dummy.go index 651a7971..b632fce4 100644 --- a/lua/lua52/dummy.go +++ b/lua/lua52/dummy.go @@ -1,3 +1,4 @@ +//go:build dummy // +build dummy // Package lua contains only a C header files. diff --git a/lua/lua53/dummy.go b/lua/lua53/dummy.go index 651a7971..b632fce4 100644 --- a/lua/lua53/dummy.go +++ b/lua/lua53/dummy.go @@ -1,3 +1,4 @@ +//go:build dummy // +build dummy // Package lua contains only a C header files. diff --git a/lua/lua54/dummy.go b/lua/lua54/dummy.go index 651a7971..b632fce4 100644 --- a/lua/lua54/dummy.go +++ b/lua/lua54/dummy.go @@ -1,3 +1,4 @@ +//go:build dummy // +build dummy // Package lua contains only a C header files. diff --git a/lua/lua_defs_lua51.go b/lua/lua_defs_lua51.go index 3696c391..2f945fed 100644 --- a/lua/lua_defs_lua51.go +++ b/lua/lua_defs_lua51.go @@ -1,4 +1,5 @@ -//+build !lua52,!lua53,!lua54 +//go:build !lua52 && !lua53 && !lua54 +// +build !lua52,!lua53,!lua54 package lua diff --git a/lua/lua_defs_lua52.go b/lua/lua_defs_lua52.go index c6623f04..34901b1b 100644 --- a/lua/lua_defs_lua52.go +++ b/lua/lua_defs_lua52.go @@ -1,4 +1,5 @@ -//+build lua52 +//go:build lua52 +// +build lua52 package lua diff --git a/lua/lua_defs_lua53.go b/lua/lua_defs_lua53.go index 0a674338..e3ad80ef 100644 --- a/lua/lua_defs_lua53.go +++ b/lua/lua_defs_lua53.go @@ -1,4 +1,5 @@ -//+build lua53 +//go:build lua53 +// +build lua53 package lua diff --git a/lua/lua_defs_lua54.go b/lua/lua_defs_lua54.go index f3768358..122479a0 100644 --- a/lua/lua_defs_lua54.go +++ b/lua/lua_defs_lua54.go @@ -1,4 +1,5 @@ -//+build lua54 +//go:build lua54 +// +build lua54 package lua diff --git a/lua/lua_test.go b/lua/lua_test.go index 7191f077..895b71fb 100644 --- a/lua/lua_test.go +++ b/lua/lua_test.go @@ -130,7 +130,6 @@ func TestCall(t *testing.T) { L.PushString("Argument2") L.PushString("Argument3") err := L.Call(3, 2) - if err != nil { t.Fatalf("Error executing call: %v\n", err) }