Skip to content

Commit

Permalink
style: Run gofumpt over all source
Browse files Browse the repository at this point in the history
  • Loading branch information
TimVosch authored and aarzilli committed Dec 27, 2024
1 parent 99a25f0 commit 8c04b0e
Show file tree
Hide file tree
Showing 23 changed files with 89 additions and 64 deletions.
19 changes: 10 additions & 9 deletions _example/alloc.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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
}

Expand All @@ -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()
Expand Down
6 changes: 4 additions & 2 deletions _example/basic.go
Original file line number Diff line number Diff line change
@@ -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!")
Expand Down
10 changes: 6 additions & 4 deletions _example/error.go
Original file line number Diff line number Diff line change
@@ -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)")
Expand Down
4 changes: 2 additions & 2 deletions _example/library.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func test2(L *lua.State) int {
}

var funcs = map[string]lua.LuaGoFunction{
"test": test,
"test": test,
"test2": test2,
}

Expand All @@ -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 {
Expand Down
9 changes: 5 additions & 4 deletions _example/panic.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
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!")
return 0
}

func main() {

var L *lua.State

L = lua.NewState()
Expand All @@ -29,7 +30,7 @@ func main() {

L.AtPanic(newPanic)

//force a panic
// force a panic
L.PushNil()
L.Call(0, 0)

Expand Down
8 changes: 5 additions & 3 deletions _example/userdata.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down
1 change: 1 addition & 0 deletions lua/dummy.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build dummy
// +build dummy

// This file is part of a workaround for `go mod vendor` which won't
Expand Down
7 changes: 5 additions & 2 deletions lua/golua.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion lua/golua_c_lua51.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//+build !lua52,!lua53,!lua54
//go:build !lua52 && !lua53 && !lua54
// +build !lua52,!lua53,!lua54

package lua

Expand Down
3 changes: 2 additions & 1 deletion lua/golua_c_lua52.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//+build lua52
//go:build lua52
// +build lua52

package lua

Expand Down
3 changes: 2 additions & 1 deletion lua/golua_c_lua53.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//+build lua53
//go:build lua53
// +build lua53

package lua

Expand Down
3 changes: 2 additions & 1 deletion lua/golua_c_lua54.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//+build lua54
//go:build lua54
// +build lua54

package lua

Expand Down
3 changes: 2 additions & 1 deletion lua/lauxlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package lua
//#include <stdlib.h>
//#include "golua.h"
import "C"

import (
"context"
"unsafe"
Expand Down Expand Up @@ -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
}

Expand Down
57 changes: 29 additions & 28 deletions lua/lua.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ package lua
*/
import "C"

import (
"fmt"
"unsafe"
Expand All @@ -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) {
Expand All @@ -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)
Expand All @@ -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) {
Expand All @@ -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))
}

Expand All @@ -193,16 +194,16 @@ 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:
return func(L1 *State) int {
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
}

Expand Down Expand Up @@ -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}
Expand All @@ -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))
}

Expand Down Expand Up @@ -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{}
}

Expand Down
1 change: 1 addition & 0 deletions lua/lua51/dummy.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build dummy
// +build dummy

// Package lua contains only a C header files.
Expand Down
1 change: 1 addition & 0 deletions lua/lua52/dummy.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build dummy
// +build dummy

// Package lua contains only a C header files.
Expand Down
1 change: 1 addition & 0 deletions lua/lua53/dummy.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build dummy
// +build dummy

// Package lua contains only a C header files.
Expand Down
1 change: 1 addition & 0 deletions lua/lua54/dummy.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
//go:build dummy
// +build dummy

// Package lua contains only a C header files.
Expand Down
3 changes: 2 additions & 1 deletion lua/lua_defs_lua51.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//+build !lua52,!lua53,!lua54
//go:build !lua52 && !lua53 && !lua54
// +build !lua52,!lua53,!lua54

package lua

Expand Down
3 changes: 2 additions & 1 deletion lua/lua_defs_lua52.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//+build lua52
//go:build lua52
// +build lua52

package lua

Expand Down
Loading

0 comments on commit 8c04b0e

Please sign in to comment.