Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes some memory leaks in CGO usage #4

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
56de1d3
Fix building of golua against a shared Lua-5.1.4
jnwhiteh Jan 26, 2011
f6c908e
Add some .gitignore files to clean repository up
jnwhiteh Jan 26, 2011
a85ba7c
Enable building against included lua-5.1.4 source
jnwhiteh Jan 26, 2011
854230e
Updated README
jnwhiteh Jan 26, 2011
85775f0
Fix merge.
xenith Mar 16, 2011
e7bb270
This introduces no API change, but it introduces an incomptable chang…
xenith Mar 18, 2011
0165915
setting an execution limit to lua code in terms of number of instruct…
aarzilli Mar 20, 2011
4d51a67
Forgot to fix the package imports in the examples. This also attempts…
xenith Mar 21, 2011
ae65842
Fix to compile and link properly on BOTH OS X and Linux. Also, make c…
xenith Mar 21, 2011
d0e68b1
Include lua_defs.go pre-generated
jnwhiteh Jun 21, 2011
5ce1a9c
Update README pointing to this repository since we have diverged a bi…
xenith Jun 29, 2011
726545c
Clean up some memory leaks in initialization.
Jul 6, 2011
8a140ba
Merge in jnwhiteh's changes do store lua_defs.go.
xenith Jul 6, 2011
98f3b01
Generate lua_defs with our package name.
xenith Jul 6, 2011
8d691c6
Merge commit '726545c51700777c4ca3be099fb1ed19d99eae9b'
xenith Jul 6, 2011
2a1127c
Merge commit '01659158a423dc292ff818369685329173bc394d'
xenith Jul 6, 2011
dc0b2f3
- take out local lua build (breaks my build)
Jul 7, 2011
b6423aa
Clean up lua_defs generation for better godoc output
Jul 7, 2011
8a7c4e9
For that matter, don't ship lua_defs.go -- simply generate at build.
Jul 7, 2011
2994075
Ignore lua_defs output, also handle arm/386 arch.
Jul 7, 2011
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
_cgo1_.o
_cgo_.o
_cgo_defun.6
_cgo_defun.c
_cgo_export.c
_cgo_export.h
_cgo_export.o
_cgo_gotypes.go
_cgo_import.c
_cgo_main.c
_cgo_main.o
_obj/
golua.o
lauxlib.cgo1.go
lauxlib.cgo2.c
lauxlib.cgo2.o
lua.cgo1.go
lua.cgo2.c
lua.cgo2.o
*.[568]
lua_defs.*
59 changes: 47 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,18 +1,53 @@
# Copyright 2009 The Go Authors. All rights reserved.
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file.

LUA51_DIR=lua51
include $(GOROOT)/src/Make.inc

all: $(LUA51_DIR)/_obj/lua51.a examples
LUA_INCLUDE_DIR=/usr/include/lua5.1/
LUA_LIB_DIR=/usr/lib/

$(LUA51_DIR)/_obj/lua51.a:
cd $(LUA51_DIR) && make
CGO_CFLAGS+=-I$(LUA_INCLUDE_DIR)
CGO_DEPS=_cgo_export.o
CGO_LDFLAGS+=-L$(LUA_LIB_DIR) -lm -llua5.1
CGO_OFILES=\
golua.o \

examples: install
cd example && make
TARG=golua

CGOFILES=\
lua.go \
lauxlib.go \
lua_defs.go

CLEANFILES+=lua-5.1.4/src/*.o\
example/*.8\
example/basic\
example/alloc\
example/panic\
example/userdata\

LUA_HEADERS=lua.h lauxlib.h lualib.h
LUA_HEADER_FILES:=$(patsubst %,$(LUA_INCLUDE_DIR)%,$(LUA_HEADERS))
LUA_INCLUDE_DIRECTIVES:=$(patsubst %,//\#include <%>\n, $(LUA_HEADERS))

include $(GOROOT)/src/Make.pkg

clean:
cd example && make clean
cd $(LUA51_DIR) && make clean
all: install examples

install:
cd $(LUA51_DIR) && make install

%: install %.go
$(QUOTED_GOBIN)/$(GC) $*.go
$(QUOTED_GOBIN)/$(LD) -o $@ $*.$O

golua.o: golua.c
gcc $(CGO_CFLAGS) $(_CGO_CFLAGS_$(GOARCH)) -fPIC $(CFLAGS) -c golua.c -o golua.o

lua_defs.go:
echo "package golua;" > lua_defs.go
echo "$(LUA_INCLUDE_DIRECTIVES)" "import \"C\"" >> lua_defs.go
echo "const (" >> lua_defs.go
cat $(LUA_HEADER_FILES) | grep '#define LUA' | sed 's/#define/ /' | sed 's/\([A-Z_][A-Z_]*\)[[:space:]]*.*/\1 = C.\1/' >> lua_defs.go
echo ")" >> lua_defs.go

examples: install
cd example && make
28 changes: 6 additions & 22 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,15 @@ Go Bindings for the lua C API

Simplest way to install:

#goinstall -u github.com/afitz/golua
#cd $GOROOT/src/pkg/github.com/afitz/golua
#make install
$ goinstall -u github.com/abneptis/golua
$ cd $GOROOT/src/pkg/github.com/abneptis/golua
$ make install


Configuration Variables
-----------------------
LUA51_LIBNAME
default: lua5.1
used if LUA51_LIB_DIR is defined

LUA51_INCLUDE_DIR
default: undefined
defining this overrides the pkg-config mechanism to find the lua cflags

LUA51_LIB_DIR
default: undefined
defining this overrides the pkg-config mechanism to create the LD_FLAGS

Licensing
Licensing
-------------------------
GoLua is released under the MIT license.
Please see the LICENSE file for more information.

Lua is Copyright (c) Lua.org, PUC-Rio. All rights reserved.



The Lua license can be found in the source and is available online:
http://www.lua.org/license.html
5 changes: 5 additions & 0 deletions example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*.[5678vqo]
alloc
basic
panic
userdata
2 changes: 1 addition & 1 deletion example/alloc.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package main

import lua "lua51"
import lua "golua"
import "unsafe"
import "fmt"

Expand Down
16 changes: 8 additions & 8 deletions example/basic.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
package main

import "lua51"
import "golua"
import "fmt"

func test(L *lua51.State) int {
func test(L *golua.State) int {
fmt.Println("hello world! from go!");
return 0;
}

func test2(L *lua51.State) int {
arg := lua51.CheckInteger(L,-1);
argfrombottom := lua51.CheckInteger(L,1);
func test2(L *golua.State) int {
arg := golua.CheckInteger(L,-1);
argfrombottom := golua.CheckInteger(L,1);
fmt.Print("test2 arg: ");
fmt.Println(arg);
fmt.Print("from bottom: ");
Expand All @@ -19,12 +19,12 @@ func test2(L *lua51.State) int {
}

func main() {
var L *lua51.State;
var L *golua.State;

L = lua51.NewState();
L = golua.NewState();
L.OpenLibs();

L.GetField(lua51.LUA_GLOBALSINDEX, "print");
L.GetField(golua.LUA_GLOBALSINDEX, "print");
L.PushString("Hello World!");
L.Call(1,0);

Expand Down
2 changes: 1 addition & 1 deletion example/panic.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package main

import lua "lua51"
import lua "golua"
import "fmt"

func test(L *lua.State) int {
Expand Down
2 changes: 1 addition & 1 deletion example/userdata.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package main

import lua "lua51"
import lua "golua"
import "unsafe"
import "fmt"

Expand Down
13 changes: 12 additions & 1 deletion lua51/golua.c → golua.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ int gchook_wrapper(lua_State* L)
GoInterface* gi = clua_getgostate(L);
if(fid != NULL)
return golua_gchook(*gi,*fid);

printf("GCHook failed\n");
//TODO: try udata or whatever, after impl

return 0;
Expand Down Expand Up @@ -220,3 +220,14 @@ void clua_openos(lua_State* L){
lua_pushstring(L,"os");
lua_call(L, 1, 0);
}

void clua_hook_function(lua_State *L, lua_Debug *ar) {
lua_checkstack(L, 2);
lua_pushstring(L, "Lua execution quantum exceeded");
lua_error(L);
}

void clua_setexecutionlimit(lua_State* L, int n) {
lua_sethook(L, &clua_hook_function, LUA_MASKCOUNT, n);
}

3 changes: 1 addition & 2 deletions lua51/golua.h → golua.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,4 @@ void clua_openpackage(lua_State* L);
void clua_openstring(lua_State* L);
void clua_opentable(lua_State* L);
void clua_openos(lua_State* L);


void clua_setexecutionlimit(lua_State* L, int n);
60 changes: 46 additions & 14 deletions lua51/lauxlib.go → lauxlib.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package lua51
package golua

//#include <lua.h>
//#include <lauxlib.h>
//#include <lualib.h>
//#include <stdlib.h>
//#include "golua.h"
import "C"
import "unsafe"
Expand All @@ -14,20 +15,26 @@ import "unsafe"

func ArgCheck(L *State, cond bool, narg int, extramsg string) {
if cond {
C.luaL_argerror(L.s, C.int(narg), C.CString(extramsg));
Cextramsg := C.CString(extramsg)
defer C.free(unsafe.Pointer(Cextramsg))
C.luaL_argerror(L.s, C.int(narg), Cextramsg)
}
}

func ArgError(L *State, narg int, extramsg string) int {
return int(C.luaL_argerror(L.s,C.int(narg),C.CString(extramsg)));
Cextramsg := C.CString(extramsg)
defer C.free(unsafe.Pointer(Cextramsg))
return int(C.luaL_argerror(L.s,C.int(narg),Cextramsg));
}

//type luaL_Buffer

//luaL_buffinit

func CallMeta(L *State, obj int, e string) int {
return int(C.luaL_callmeta(L.s,C.int(obj),C.CString(e)));
Ce := C.CString(e)
defer C.free(unsafe.Pointer(Ce))
return int(C.luaL_callmeta(L.s,C.int(obj),Ce))
}

func CheckAny(L *State, narg int) {
Expand Down Expand Up @@ -55,8 +62,11 @@ func CheckOption(L *State, narg int, def string, lst []string) int {
func CheckType(L *State, narg int, t int) {
C.luaL_checktype(L.s,C.int(narg),C.int(t));
}

func CheckUdata(L *State, narg int, tname string) unsafe.Pointer {
return unsafe.Pointer(C.luaL_checkudata(L.s,C.int(narg),C.CString(tname)));
Ctname := C.CString(tname)
defer C.free(unsafe.Pointer(Ctname))
return unsafe.Pointer(C.luaL_checkudata(L.s,C.int(narg),Ctname))
}

//true if no errors, false otherwise
Expand All @@ -83,33 +93,51 @@ func FmtError(L *State, fmt string, args...interface{}) int {

//returns false if no such metatable or no such field
func GetMetaField(L *State, obj int, e string) bool {
return C.luaL_getmetafield(L.s,C.int(obj),C.CString(e)) != 0;
Ce := C.CString(e)
defer C.free(unsafe.Pointer(Ce))
return C.luaL_getmetafield(L.s,C.int(obj),Ce) != 0;
}

//TODO: rename better... clashes with lua_getmetatable
func LGetMetaTable(L *State, tname string) {
//C.luaL_getmetatable(L.s,C.CString(tname));
C.lua_getfield(L.s,LUA_REGISTRYINDEX,C.CString(tname));
Ctname := C.CString(tname)
defer C.free(unsafe.Pointer(Ctname))
C.lua_getfield(L.s,LUA_REGISTRYINDEX,Ctname);
}

func GSub(L *State, s string, p string, r string) string {
return C.GoString(C.luaL_gsub(L.s, C.CString(s), C.CString(p), C.CString(r)));
Cs := C.CString(s)
Cp := C.CString(p)
Cr := C.CString(r)
defer func(){
C.free(unsafe.Pointer(Cs))
C.free(unsafe.Pointer(Cp))
C.free(unsafe.Pointer(Cr))
}()

return C.GoString(C.luaL_gsub(L.s, Cs, Cp, Cr))
}

//TODO: luaL_loadbuffer


func (L *State) LoadFile(filename string) int {
return int(C.luaL_loadfile(L.s,C.CString(filename)));
Cfilename := C.CString(filename)
defer C.free(unsafe.Pointer(Cfilename))
return int(C.luaL_loadfile(L.s,Cfilename));
}

func (L *State) LoadString(s string) int {
return int(C.luaL_loadstring(L.s,C.CString(s)));
Cs := C.CString(s)
defer C.free(unsafe.Pointer(Cs))
return int(C.luaL_loadstring(L.s,Cs))
}

//returns false if registry already contains key tname
func (L *State) NewMetaTable(tname string) bool {
return C.luaL_newmetatable(L.s, C.CString(tname)) != 0;
Ctname := C.CString(tname)
defer C.free(unsafe.Pointer(Ctname))
return C.luaL_newmetatable(L.s, Ctname) != 0;
}

func NewState() *State {
Expand All @@ -132,7 +160,9 @@ func (L *State) OptNumber(narg int, d float64) float64 {

func (L *State) OptString(narg int, d string) string {
var length C.size_t;
return C.GoString(C.luaL_optlstring(L.s,C.int(narg),C.CString(d),&length));
Cd := C.CString(d)
defer C.free(unsafe.Pointer(Cd))
return C.GoString(C.luaL_optlstring(L.s,C.int(narg),Cd,&length));
}

//luaL_prepbuffer
Expand All @@ -150,7 +180,9 @@ func LTypename(L *State, index int) string {

//TODO: decide if we actually want this renamed
func TypeError(L *State, narg int, tname string) int {
return int(C.luaL_typerror(L.s,C.int(narg),C.CString(tname)));
Ctname := C.CString(tname)
defer C.free(unsafe.Pointer(Ctname))
return int(C.luaL_typerror(L.s,C.int(narg),Ctname))
}

func Unref(L *State, t int, ref int) {
Expand Down
Loading