Skip to content

Commit

Permalink
settled on simple forwarding functionality for full userdata
Browse files Browse the repository at this point in the history
go-style helpers as well as go type wrappers destined for separate lib
  • Loading branch information
afitz committed Aug 1, 2010
1 parent d96c920 commit 9936093
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
8 changes: 7 additions & 1 deletion example/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

include $(GOROOT)/src/Make.$(GOARCH)

all: basic panic
all: basic panic userdata

basic: basic.8
$(LD) -o basic basic.8
Expand All @@ -15,5 +15,11 @@ panic: panic.8
panic.8: panic.go
$(GC) -o panic.8 panic.go

userdata: userdata.8
$(LD) -o userdata userdata.8

userdata.8: userdata.go
$(GC) -o userdata.8 userdata.go

clean:
-rm -f basic panic *.8
28 changes: 28 additions & 0 deletions example/userdata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

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

type Userdata struct {
a,b int
}

func main() {
var L *lua.State;
L = lua.NewState();
lua.OpenLibs(L);

rawptr := lua.NewUserdata(L,uintptr(unsafe.Sizeof(Userdata{})));
var ptr *Userdata;
ptr = (*Userdata)(rawptr);
ptr.a = 2;
ptr.b = 3;

fmt.Println(ptr);

rawptr2 := lua.ToUserdata(L,-1);
ptr2 := (*Userdata)(rawptr2);

fmt.Println(ptr2);
}
12 changes: 6 additions & 6 deletions lua51/lua.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ func PushUserdata(L *State, ud interface{}) {

//TODO:
//old style
func NewUserdata(L* State, size uintptr) uintptr {
return 0;
func NewUserdata(L* State, size uintptr) unsafe.Pointer {
return unsafe.Pointer(C.lua_newuserdata(L.s, C.size_t(size)));
}


Expand Down Expand Up @@ -235,7 +235,7 @@ func IsBoolean(L *State, index int) bool {
}

func IsGoFunction(L *State, index int) bool {
//TODO: add a check if c function to distinguish c function from go function
//TODO:go function is now a userdatum, not a c function, so this will not work
return C.lua_iscfunction(L.s, C.int(index)) == 1
}

Expand Down Expand Up @@ -290,6 +290,7 @@ func NewTable(L *State) {
C.lua_createtable(L.s,0,0);
}


func NewThread(L* State) *State {
//TODO: call newState with result from C.lua_newthread and return it
//TODO:
Expand Down Expand Up @@ -449,9 +450,8 @@ func ToThread(L *State, index int) *State {
return &State{}
}

func ToUserdata(L *State, index int) interface{} {
//TODO: needs userdata implementation first...
return 0;
func ToUserdata(L *State, index int) unsafe.Pointer {
return unsafe.Pointer(C.lua_touserdata(L.s,C.int(index)));
}

func Type(L *State, index int) int {
Expand Down

0 comments on commit 9936093

Please sign in to comment.