Skip to content

Commit

Permalink
ability to push an arbitrary integer as light userdata
Browse files Browse the repository at this point in the history
  • Loading branch information
aarzilli committed Dec 31, 2010
1 parent 4582993 commit 610b3e5
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
9 changes: 9 additions & 0 deletions lua51/golua.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ void clua_pushgofunction(lua_State* L, unsigned int fid)
lua_setmetatable(L, -2);
}

void clua_pushlightinteger(lua_State* L, unsigned int n)
{
lua_pushlightuserdata(L, (void*)n);
}

int clua_tolightinteger(lua_State *L, unsigned int index)
{
return (int)lua_touserdata(L, index);
}

void clua_setgostate(lua_State* L, GoInterface gi)
{
Expand Down
2 changes: 2 additions & 0 deletions lua51/golua.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ void clua_initstate(lua_State* L);

unsigned int clua_togofunction(lua_State* L, int index);
void clua_pushgofunction(lua_State* L, unsigned int fid);
void clua_pushlightinteger(lua_State* L, int n);
int clua_tolightinteger(lua_State* L, int index);
void clua_setgostate(lua_State* L, GoInterface gostate);
GoInterface* clua_getgostate(lua_State* L);
GoInterface clua_atpanic(lua_State* L, unsigned int panicf_id);
Expand Down
4 changes: 2 additions & 2 deletions lua51/lauxlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ func CheckUdata(L *State, narg int, tname string) unsafe.Pointer {
//true if no errors, false otherwise
func (L *State) DoFile(filename string) bool {
if L.LoadFile(filename) == 0 {
return PCall(L,0,LUA_MULTRET,0) == 0;
return L.PCall(0,LUA_MULTRET,0) == 0;
}
return false;
}

//true if no errors, false otherwise
func (L *State) DoString(str string) bool {
if L.LoadString(str) == 0 {
return PCall(L,0,LUA_MULTRET,0) == 0;
return L.PCall(0,LUA_MULTRET,0) == 0;
}
return false;
}
Expand Down
17 changes: 13 additions & 4 deletions lua51/lua.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ func (L *State) PushGoFunction(f GoFunction) {
}


func (L *State) PushLightInteger(n int) {
C.clua_pushlightinteger(L.s, C.int(n))
}


//push pointer by value, as a value - we don't impact lifetime
func (L *State) PushLightUserdata(ud *interface{}) {
//push
Expand Down Expand Up @@ -300,23 +305,23 @@ func (L *State) NewTable() {
}


func NewThread(L* State) *State {
func (L *State) NewThread() *State {
//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,nil,nil};
}

func Next(L *State, index int) int {
func (L *State) Next(index int) int {
return int(C.lua_next(L.s,C.int(index)));
}

func ObjLen(L *State, index int) uint {
func (L *State) ObjLen(index int) uint {
return uint(C.lua_objlen(L.s,C.int(index)));
}

func PCall(L *State, nargs int, nresults int, errfunc int) int {
func (L *State) PCall(nargs int, nresults int, errfunc int) int {
return int(C.lua_pcall(L.s, C.int(nargs), C.int(nresults), C.int(errfunc)));
}

Expand Down Expand Up @@ -464,6 +469,10 @@ func (L *State) ToUserdata(index int) unsafe.Pointer {
return unsafe.Pointer(C.lua_touserdata(L.s,C.int(index)));
}

func (L *State) ToLightInteger(index int) int {
return int(C.clua_tolightinteger(L.s, C.int(index)))
}

func (L *State) Type(index int) int {
return int(C.lua_type(L.s, C.int(index)));
}
Expand Down

0 comments on commit 610b3e5

Please sign in to comment.