From e5a9af9a31a1cf7c0d41907cf9521330490a85ac Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Mon, 16 Sep 2024 09:51:55 +0800 Subject: [PATCH 1/5] c/lua:remove redundant --- c/lua/lua.go | 211 ++++++++++++++++++++++++++------------------------- 1 file changed, 107 insertions(+), 104 deletions(-) diff --git a/c/lua/lua.go b/c/lua/lua.go index 3f1fec14..6f967b74 100644 --- a/c/lua/lua.go +++ b/c/lua/lua.go @@ -10,18 +10,18 @@ const ( LLGoPackage = "link: $(pkg-config --libs lua); -llua -lm" ) -// /* mark for precompiled code ('Lua') */ +/* mark for precompiled code ('Lua') */ -// /* option for multiple returns in 'lua_pcall' and 'lua_call' */ +/* option for multiple returns in 'lua_pcall' and 'lua_call' */ const ( MULTRET = -1 ) -// /* -// ** Pseudo-indices -// ** (-LUAI_MAXSTACK is the minimum valid index; we keep some free empty -// ** space after that to help overflow detection) -// */ +/* + * Pseudo-indices + * (-LUAI_MAXSTACK is the minimum valid index; we keep some free empty + * space after that to help overflow detection) + */ const ( REGISTRYINDEX = -MAXSTACK - 1000 @@ -31,7 +31,7 @@ func Upvalueindex(i c.Int) c.Int { return c.Int(REGISTRYINDEX) - i } -// /* thread status */ +/* thread status */ const ( OK = 0 YIELD = 1 @@ -45,9 +45,9 @@ type State struct { Unused [8]byte } -// /* -// ** basic types -// */ +/* + * basic types + */ const ( NONE c.Int = -1 NIL c.Int = 0 @@ -62,47 +62,47 @@ const ( UMTYPES c.Int = 9 ) -// /* minimum Lua stack available to a C function */ +/* minimum Lua stack available to a C function */ const ( MINSTACK = 20 ) -// /* predefined values in the registry */ +/* predefined values in the registry */ const ( RIDX_MAINTHREAD = 1 RIDX_GLOBALS = 2 RIDX_LAST = RIDX_GLOBALS ) -// /* type of numbers in Lua */ +/* type of numbers in Lua */ type Number = c.Double -// /* type for integer functions */ +/* type for integer functions */ type Integer = c.Int -// /* unsigned integer type */ +/* unsigned integer type */ type Unsigned = c.Uint -// /* type for continuation-function contexts */ +/* type for continuation-function contexts */ type KContext = c.Pointer -// /* -// ** Type for C functions registered with Lua -// */ +/* + * Type for C functions registered with Lua + */ // llgo:type C type CFunction func(L *State) c.Int -// /* -// ** Type for continuation functions -// */ +/* + * Type for continuation functions + */ // llgo:type C type KFunction func(L *State, status c.Int, ctx KContext) c.Int -// /* -// ** Type for functions that read/write blocks when loading/dumping Lua chunks -// */ +/* + * Type for functions that read/write blocks when loading/dumping Lua chunks + */ // llgo:type C type Reader func(L *State, ud c.Pointer, sz *c.Ulong) *c.Char @@ -110,47 +110,48 @@ type Reader func(L *State, ud c.Pointer, sz *c.Ulong) *c.Char // llgo:type C type Writer func(L *State, p c.Pointer, sz c.Ulong, ud c.Pointer) c.Int -// /* -// ** Type for memory-allocation functions -// */ +/* + * Type for memory-allocation functions + */ // typedef void * (*lua_Alloc) (void *ud, void *ptr, size_t osize, size_t nsize); -// /* -// ** Type for warning functions -// */ +/* + * Type for warning functions + */ // typedef void (*lua_WarnFunction) (void *ud, const char *msg, int tocont); -// /* -// ** Type used by the debug API to collect debug information -// */ +/* + * Type used by the debug API to collect debug information + */ // typedef struct lua_Debug lua_Debug; -// /* -// ** Functions to be called by the debugger in specific events -// */ +/* + * Functions to be called by the debugger in specific events + */ // typedef void (*lua_Hook) (State *L, lua_Debug *ar); -// /* -// ** generic extra include file -// */ +/* + * generic extra include file + */ // #if defined(LUA_USER_H) // #include LUA_USER_H // #endif -// /* -// ** RCS ident string -// */ +/* + * RCS ident string + */ // extern const char lua_ident[]; -// /* -// ** state manipulation -// */ +/* + ** state manipulation + */ + // llgo:link (*State).Close C.lua_close func (L *State) Close() {} @@ -171,9 +172,9 @@ func (L *State) Atpanic(panicf CFunction) CFunction { return nil } // llgo:link (*State).Version C.lua_version func (L *State) Version() Number { return 0 } -// /* -// ** basic stack manipulation -// */ +/* + * basic stack manipulation + */ // llgo:link (*State).Absindex C.lua_absindex func (L *State) Absindex(idx c.Int) c.Int { return 0 } @@ -199,9 +200,9 @@ func (L *State) Checkstack(n c.Int) c.Int { return 0 } // llgo:link (*State).Xmove C.lua_xmove func (L *State) Xmove(to *State, n c.Int) {} -// /* -// ** access functions (stack -> C) -// */ +/* + * access functions (stack -> C) + */ // llgo:link (*State).Isnumber C.lua_isnumber func (L *State) Isnumber(idx c.Int) c.Int { return 0 } @@ -249,13 +250,14 @@ func (L *State) Tothread(idx c.Int) *State { return nil } // LUA_API const void *(lua_topointer) (State *L, int idx); -// /* -// ** Comparison and arithmetic functions -// */ +/* + * Comparison and arithmetic functions + */ + +/* + * push functions (C -> stack) + */ -// /* -// ** push functions (C -> stack) -// */ // llgo:link (*State).Pushnil C.lua_pushnil func (L *State) Pushnil() {} @@ -286,9 +288,9 @@ func (L *State) Pushlightuserdata(p c.Pointer) {} // llgo:link (*State).Pushthread C.lua_pushthread func (L *State) Pushthread() c.Int { return 0 } -// /* -// ** get functions (Lua -> stack) -// */ +/* + * get functions (Lua -> stack) + */ // llgo:link (*State).Getglobal C.lua_getglobal func (L *State) Getglobal(name *c.Char) c.Int { return 0 } @@ -315,9 +317,9 @@ func (L *State) Getmetatable(objindex c.Int) c.Int { return 0 } // LUA_API int (lua_getiuservalue) (State *L, int idx, int n); -// /* -// ** set functions (stack -> Lua) -// */ +/* + * set functions (stack -> Lua) + */ // llgo:link (*State).Setglobal C.lua_setglobal func (L *State) Setglobal(name *c.Char) {} @@ -338,9 +340,9 @@ func (L *State) Setmetatable(objindex c.Int) c.Int { return 0 } //int (lua_setiuservalue) (State *L, int idx, int n); -// /* -// ** 'load' and 'call' functions (load and run Lua code) -// */ +/* + * 'load' and 'call' functions (load and run Lua code) + */ // llgo:link (*State).Callk C.lua_callk func (L *State) Callk(nargs c.Int, nresults c.Int, ctx KContext, k KFunction) c.Int { @@ -366,9 +368,9 @@ func (L *State) Load(reader Reader, dt c.Pointer, chunkname *c.Char, mode *c.Cha // llgo:link (*State).Dump C.lua_dump func (L *State) Dump(writer Writer, data c.Pointer, strip c.Int) c.Int { return 0 } -// /* -// ** coroutine functions -// */ +/* + * coroutine functions + */ // llgo:link (*State).Resume C.lua_resume func (L *State) Resume(from *State, narg c.Int, nres *c.Int) c.Int { return 0 } @@ -383,16 +385,16 @@ func (L *State) Isyieldable() c.Int { return 0 } func (L *State) Yieldk(nresults c.Int, ctx KContext, k KFunction) c.Int { return 0 } func (L *State) Yield(nresults c.Int) c.Int { return L.Yieldk(nresults, nil, nil) } -// /* -// ** Warning-related functions -// */ +/* + * Warning-related functions + */ //void (lua_setwarnf) (State *L, lua_WarnFunction f, void *ud); //void (lua_warning) (State *L, const char *msg, int tocont); -// /* -// ** garbage-collection function and options -// */ +/* + * garbage-collection function and options + */ const ( GCSTOP = 0 @@ -410,9 +412,9 @@ const ( // LUA_API int (lua_gc) (State *L, int what, ...); -// /* -// ** miscellaneous functions -// */ +/* + * miscellaneous functions + */ // llgo:link (*State).Next C.lua_next func (L *State) Next(idx c.Int) c.Int { return 0 } @@ -430,11 +432,11 @@ func (L *State) Error() c.Int { return 0 } // LUA_API void (lua_toclose) (State *L, int idx); // LUA_API void (lua_closeslot) (State *L, int idx); -// /* -// ** {============================================================== -// ** some useful macros -// ** =============================================================== -// */ +/* + ** {============================================================== + ** some useful macros + ** =============================================================== + */ // #define lua_getextraspace(L) ((void *)((char *)(L) - LUA_EXTRASPACE)) @@ -476,13 +478,13 @@ func (L *State) Replace(idx c.Int) { L.Pop(1) } -// /* }============================================================== */ +/* }============================================================== */ -// /* -// ** {============================================================== -// ** compatibility macros -// ** =============================================================== -// */ +/* +** {============================================================== +** compatibility macros +** =============================================================== + */ func (L *State) Newuserdata(sz uintptr) c.Pointer { return L.Newuserdatauv(sz, 1) @@ -493,16 +495,17 @@ func (L *State) Newuserdata(sz uintptr) c.Pointer { // #define LUA_NUMTAGS LUA_NUMTYPES -// /* }============================================================== */ +/* }============================================================== */ -// /* -// ** {====================================================================== -// ** Debug API -// ** ======================================================================= -// */ -// /* -// ** Event codes -// */ +/* +** {====================================================================== +** Debug API +** ======================================================================= + */ + +/* + * Event codes + */ const ( HOOKCALL = 0 @@ -512,9 +515,9 @@ const ( HOOKTAILCALL = 4 ) -// /* -// ** Event masks -// */ +/* + * Event masks + */ const ( MASKCALL = 1 << HOOKCOUNT @@ -541,4 +544,4 @@ const ( // LUA_API int (lua_setcstacklimit) (State *L, unsigned int limit); // struct lua_Debug -// /* }====================================================================== */ +/* }====================================================================== */ From c6345279ccf12bcce43232514437b074cbc6b767 Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Mon, 16 Sep 2024 22:16:01 +0800 Subject: [PATCH 2/5] c/lua:table field operate --- c/lua/_demo/table/table.go | 54 +++++++++++++++++++++++++++++++++----- c/lua/lua.go | 31 ++++++++++++++++------ 2 files changed, 70 insertions(+), 15 deletions(-) diff --git a/c/lua/_demo/table/table.go b/c/lua/_demo/table/table.go index e062bc65..b9da2bfa 100644 --- a/c/lua/_demo/table/table.go +++ b/c/lua/_demo/table/table.go @@ -1,6 +1,8 @@ package main import ( + "unsafe" + "github.com/goplus/llgo/c" "github.com/goplus/llgo/c/lua" ) @@ -8,9 +10,19 @@ import ( func printTable(L *lua.State) { L.Pushnil() for L.Next(-2) != 0 { - key := L.Tostring(-2) value := L.Tostring(-1) - c.Printf(c.Str("%s - %s\n"), key, value) + switch L.Type(-2) { + case lua.STRING: + key := L.Tostring(-2) + c.Printf(c.Str("%s - %s\n"), key, value) + case lua.NUMBER: + key := L.Tonumber(-2) + c.Printf(c.Str("[%.0f] - %s\n"), key, value) + case lua.LIGHTUSERDATA: + c.Printf(c.Str("[pointer] - %s\n"), value) + default: + c.Printf(c.Str("unknown key type %s %d\n"), L.Typename(-2), L.Type(-2)) + } L.Pop(1) } L.Pop(1) @@ -24,37 +36,65 @@ func main() { L.Newtable() + // set table name:John L.Pushstring(c.Str("name")) L.Pushstring(c.Str("John")) L.Settable(-3) + // set table age:30 L.Pushstring(c.Str("age")) L.Pushnumber(30) L.Settable(-3) + // set table field fullname:John Doe L.Pushstring(c.Str("John Doe")) L.Setfield(-2, c.Str("fullname")) + // set index field + L.Pushinteger(123) + L.Seti(-2, c.Int(1)) + + // set pointer key field + pointerKey := c.AllocaCStr("pointer key") + L.Pushstring(c.Str("pointer value")) + L.Rawsetp(-2, unsafe.Pointer(pointerKey)) + + // get field by Getfield L.Getfield(-1, c.Str("name")) - c.Printf(c.Str("%s\n"), L.Tostring(-1)) + c.Printf(c.Str("name: %s\n"), L.Tostring(-1)) L.Pop(1) + // get field by Rawget + L.Pushstring(c.Str("fullname")) + L.Rawget(-2) + c.Printf(c.Str("fullname: %s\n"), L.Tostring(-1)) + L.Pop(1) + + // get field by Gettable L.Pushstring(c.Str("age")) L.Gettable(-2) age := int(L.Tonumber(-1)) c.Printf(c.Str("Age: %d\n"), age) L.Pop(1) + // get index field + L.Geti(-1, c.Int(1)) + c.Printf(c.Str("Index[%d] value: %d\n"), 1, L.Tointeger(-1)) + L.Pop(1) + c.Printf(c.Str("All entries in the table:\n")) printTable(L) - } /* Expected output: -John +name: John +fullname: John Doe Age: 30 +Index[1] value: 123 All entries in the table: -age - 30.0 -fullname - John Doe +[1] - 123 name - John +[pointer] - pointer value +fullname - John Doe +age - 30.0 */ diff --git a/c/lua/lua.go b/c/lua/lua.go index 6f967b74..207ae10f 100644 --- a/c/lua/lua.go +++ b/c/lua/lua.go @@ -301,10 +301,17 @@ func (L *State) Gettable(idx c.Int) c.Int { return 0 } // llgo:link (*State).Getfield C.lua_getfield func (L *State) Getfield(idx c.Int, k *c.Char) c.Int { return 0 } -// LUA_API int (lua_geti) (State *L, int idx, lua_Integer n); -// LUA_API int (lua_rawget) (State *L, int idx); -// LUA_API int (lua_rawgeti) (State *L, int idx, lua_Integer n); -// LUA_API int (lua_rawgetp) (State *L, int idx, const void *p); +// llgo:link (*State).Geti C.lua_geti +func (L *State) Geti(idx c.Int, n Integer) c.Int { return 0 } + +// llgo:link (*State).Rawget C.lua_rawget +func (L *State) Rawget(idx c.Int) c.Int { return 0 } + +// llgo:link (*State).Rawgeti C.lua_rawgeti +func (L *State) Rawgeti(idx c.Int, n Integer) c.Int { return 0 } + +// llgo:link (*State).Rawgetp C.lua_rawgetp +func (L *State) Rawgetp(idx c.Int, p c.Pointer) c.Int { return 0 } // llgo:link (*State).Createtable C.lua_createtable func (L *State) Createtable(narr c.Int, nrec c.Int) {} @@ -330,10 +337,17 @@ func (L *State) Settable(idx c.Int) {} // llgo:link (*State).Setfield C.lua_setfield func (L *State) Setfield(idx c.Int, k *c.Char) {} -//void (lua_seti) (State *L, int idx, lua_Integer n); -//void (lua_rawset) (State *L, int idx); -//void (lua_rawseti) (State *L, int idx, lua_Integer n); -//void (lua_rawsetp) (State *L, int idx, const void *p); +// llgo:link (*State).Seti C.lua_seti +func (L *State) Seti(idx c.Int, n Integer) {} + +// llgo:link (*State).Rawset C.lua_rawset +func (L *State) Rawset(idx c.Int) {} + +// llgo:link (*State).Rawseti C.lua_rawseti +func (L *State) Rawseti(idx c.Int, n Integer) {} + +// llgo:link (*State).Rawsetp C.lua_rawsetp +func (L *State) Rawsetp(idx c.Int, p c.Pointer) {} // llgo:link (*State).Setmetatable C.lua_setmetatable func (L *State) Setmetatable(objindex c.Int) c.Int { return 0 } @@ -415,6 +429,7 @@ const ( /* * miscellaneous functions */ + // llgo:link (*State).Next C.lua_next func (L *State) Next(idx c.Int) c.Int { return 0 } From e3cb4ebfdc2bd09afa37ba6027d884ba8797cacb Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Mon, 16 Sep 2024 23:12:33 +0800 Subject: [PATCH 3/5] c/lua:alloc --- c/lua/_demo/coroutine-cfunc/coroutine.go | 2 +- c/lua/_demo/coroutine/coroutine.go | 2 +- c/lua/_demo/crroutine-continue/corroutine.go | 2 +- c/lua/_demo/custom-panic/panic.go | 2 +- c/lua/_demo/dump/load/load.go | 2 +- c/lua/_demo/dump/save/save.go | 2 +- c/lua/_demo/error/error.go | 2 +- c/lua/_demo/funccall-concat/funccall.go | 2 +- c/lua/_demo/funccall-number/funccall.go | 2 +- c/lua/_demo/funccall-string/funccall.go | 2 +- c/lua/_demo/hello/hello.go | 2 +- c/lua/_demo/loadcall/loadcall.go | 2 +- c/lua/_demo/metatable/metatable.go | 2 +- c/lua/_demo/stack/stack.go | 4 +- c/lua/_demo/table/table.go | 2 +- c/lua/_demo/thread/thread.go | 2 +- c/lua/_demo/userdata/userdata.go | 2 +- c/lua/lauxlib.go | 4 +- c/lua/lua.go | 57 ++++++++++++++------ 19 files changed, 60 insertions(+), 37 deletions(-) diff --git a/c/lua/_demo/coroutine-cfunc/coroutine.go b/c/lua/_demo/coroutine-cfunc/coroutine.go index 676a5bfc..8be4e405 100644 --- a/c/lua/_demo/coroutine-cfunc/coroutine.go +++ b/c/lua/_demo/coroutine-cfunc/coroutine.go @@ -13,7 +13,7 @@ func coroutineFunc(L *lua.State) c.Int { } func main() { - L := lua.Newstate() + L := lua.Newstate__1() defer L.Close() L.Openlibs() diff --git a/c/lua/_demo/coroutine/coroutine.go b/c/lua/_demo/coroutine/coroutine.go index ed05d98b..7d5e9c69 100644 --- a/c/lua/_demo/coroutine/coroutine.go +++ b/c/lua/_demo/coroutine/coroutine.go @@ -18,7 +18,7 @@ func coroutineFunc(L *lua.State) { } func main() { - L := lua.Newstate() + L := lua.Newstate__1() defer L.Close() L.Openlibs() diff --git a/c/lua/_demo/crroutine-continue/corroutine.go b/c/lua/_demo/crroutine-continue/corroutine.go index 4fe9f8c5..8070cb94 100644 --- a/c/lua/_demo/crroutine-continue/corroutine.go +++ b/c/lua/_demo/crroutine-continue/corroutine.go @@ -29,7 +29,7 @@ func createCountdown(L *lua.State) c.Int { } func main() { - L := lua.Newstate() + L := lua.Newstate__1() L.Openlibs() defer L.Close() L.Register(c.Str("create_countdown"), createCountdown) diff --git a/c/lua/_demo/custom-panic/panic.go b/c/lua/_demo/custom-panic/panic.go index e0e732bf..6c22dbb5 100644 --- a/c/lua/_demo/custom-panic/panic.go +++ b/c/lua/_demo/custom-panic/panic.go @@ -24,7 +24,7 @@ func customPanic(L *lua.State) c.Int { } func main() { - L := lua.Newstate() + L := lua.Newstate__1() defer L.Close() L.Openlibs() diff --git a/c/lua/_demo/dump/load/load.go b/c/lua/_demo/dump/load/load.go index 32d9cd61..f86ee32c 100644 --- a/c/lua/_demo/dump/load/load.go +++ b/c/lua/_demo/dump/load/load.go @@ -33,7 +33,7 @@ func reader(L *lua.State, data c.Pointer, size *c.Ulong) *c.Char { } func main() { - L := lua.Newstate() + L := lua.Newstate__1() defer L.Close() L.Openlibs() diff --git a/c/lua/_demo/dump/save/save.go b/c/lua/_demo/dump/save/save.go index e94cc2e5..0e6b61b2 100644 --- a/c/lua/_demo/dump/save/save.go +++ b/c/lua/_demo/dump/save/save.go @@ -21,7 +21,7 @@ func writer(L *lua.State, p c.Pointer, sz c.Ulong, ud c.Pointer) c.Int { } func main() { - L := lua.Newstate() + L := lua.Newstate__1() defer L.Close() L.Openlibs() diff --git a/c/lua/_demo/error/error.go b/c/lua/_demo/error/error.go index ed518f98..3ca86754 100644 --- a/c/lua/_demo/error/error.go +++ b/c/lua/_demo/error/error.go @@ -8,7 +8,7 @@ import ( ) func main() { - L := lua.Newstate() + L := lua.Newstate__1() defer L.Close() L.Openlibs() if res := L.Loadstring(c.Str("function doubleNumber(x) ! return x * 2 end")); res != lua.OK { diff --git a/c/lua/_demo/funccall-concat/funccall.go b/c/lua/_demo/funccall-concat/funccall.go index b9c0d0b5..4168425d 100644 --- a/c/lua/_demo/funccall-concat/funccall.go +++ b/c/lua/_demo/funccall-concat/funccall.go @@ -8,7 +8,7 @@ import ( ) func main() { - L := lua.Newstate() + L := lua.Newstate__1() defer L.Close() L.Openlibs() diff --git a/c/lua/_demo/funccall-number/funccall.go b/c/lua/_demo/funccall-number/funccall.go index 8cb34d5a..9db886f7 100644 --- a/c/lua/_demo/funccall-number/funccall.go +++ b/c/lua/_demo/funccall-number/funccall.go @@ -8,7 +8,7 @@ import ( ) func main() { - L := lua.Newstate() + L := lua.Newstate__1() defer L.Close() L.Openlibs() diff --git a/c/lua/_demo/funccall-string/funccall.go b/c/lua/_demo/funccall-string/funccall.go index 14b76479..066ce601 100644 --- a/c/lua/_demo/funccall-string/funccall.go +++ b/c/lua/_demo/funccall-string/funccall.go @@ -8,7 +8,7 @@ import ( ) func main() { - L := lua.Newstate() + L := lua.Newstate__1() defer L.Close() L.Openlibs() diff --git a/c/lua/_demo/hello/hello.go b/c/lua/_demo/hello/hello.go index 71ee8592..e640e78b 100644 --- a/c/lua/_demo/hello/hello.go +++ b/c/lua/_demo/hello/hello.go @@ -8,7 +8,7 @@ import ( ) func main() { - L := lua.Newstate() + L := lua.Newstate__1() defer L.Close() L.Openlibs() if res := L.Dostring(c.Str("print('hello world')")); res != lua.OK { diff --git a/c/lua/_demo/loadcall/loadcall.go b/c/lua/_demo/loadcall/loadcall.go index 02886227..8b3247a3 100644 --- a/c/lua/_demo/loadcall/loadcall.go +++ b/c/lua/_demo/loadcall/loadcall.go @@ -8,7 +8,7 @@ import ( ) func main() { - L := lua.Newstate() + L := lua.Newstate__1() defer L.Close() L.Openlibs() diff --git a/c/lua/_demo/metatable/metatable.go b/c/lua/_demo/metatable/metatable.go index 424fd5ed..f952f72c 100644 --- a/c/lua/_demo/metatable/metatable.go +++ b/c/lua/_demo/metatable/metatable.go @@ -29,7 +29,7 @@ func printStack(L *lua.State, message string) { } func main() { - L := lua.Newstate() + L := lua.Newstate__1() defer L.Close() L.Openlibs() diff --git a/c/lua/_demo/stack/stack.go b/c/lua/_demo/stack/stack.go index fed39e7f..a7e13f1d 100644 --- a/c/lua/_demo/stack/stack.go +++ b/c/lua/_demo/stack/stack.go @@ -17,7 +17,7 @@ func printStack(L *lua.State, stateName *c.Char) { func main() { // Create a new Lua state and open libraries - L := lua.Newstate() + L := lua.Newstate__1() defer L.Close() L.Openlibs() @@ -68,7 +68,7 @@ func main() { printStack(L, c.Str("L1")) // Create a second Lua state - L1 := lua.Newstate() + L1 := lua.Newstate__1() defer L1.Close() // Move two elements to the new state diff --git a/c/lua/_demo/table/table.go b/c/lua/_demo/table/table.go index b9da2bfa..14407617 100644 --- a/c/lua/_demo/table/table.go +++ b/c/lua/_demo/table/table.go @@ -29,7 +29,7 @@ func printTable(L *lua.State) { } func main() { - L := lua.Newstate() + L := lua.Newstate__1() defer L.Close() L.Openlibs() diff --git a/c/lua/_demo/thread/thread.go b/c/lua/_demo/thread/thread.go index e5c74b02..f4a25d5c 100644 --- a/c/lua/_demo/thread/thread.go +++ b/c/lua/_demo/thread/thread.go @@ -15,7 +15,7 @@ func pushThread(state *lua.State, name string) { } func main() { - L := lua.Newstate() + L := lua.Newstate__1() defer L.Close() L.Openlibs() diff --git a/c/lua/_demo/userdata/userdata.go b/c/lua/_demo/userdata/userdata.go index f5872e34..3d2c2c35 100644 --- a/c/lua/_demo/userdata/userdata.go +++ b/c/lua/_demo/userdata/userdata.go @@ -12,7 +12,7 @@ type lightdata struct { } func main() { - L := lua.Newstate() + L := lua.Newstate__1() defer L.Close() L.Openlibs() diff --git a/c/lua/lauxlib.go b/c/lua/lauxlib.go index 5b3a54b8..df7aac0d 100644 --- a/c/lua/lauxlib.go +++ b/c/lua/lauxlib.go @@ -33,8 +33,8 @@ func (L *State) Loadfile(filename *c.Char) c.Int { return L.Loadfilex(filename, // llgo:link (*State).Loadstring C.luaL_loadstring func (L *State) Loadstring(s *c.Char) c.Int { return 0 } -//go:linkname Newstate C.luaL_newstate -func Newstate() *State +//go:linkname Newstate__1 C.luaL_newstate +func Newstate__1() *State { return nil } // /* // ** =============================================================== diff --git a/c/lua/lua.go b/c/lua/lua.go index 207ae10f..6c64b5b9 100644 --- a/c/lua/lua.go +++ b/c/lua/lua.go @@ -114,7 +114,8 @@ type Writer func(L *State, p c.Pointer, sz c.Ulong, ud c.Pointer) c.Int * Type for memory-allocation functions */ -// typedef void * (*lua_Alloc) (void *ud, void *ptr, size_t osize, size_t nsize); +// llgo:type C +type Alloc func(ud c.Pointer, ptr c.Pointer, osize c.Ulong, nsize c.Ulong) c.Pointer /* * Type for warning functions @@ -155,7 +156,8 @@ type Writer func(L *State, p c.Pointer, sz c.Ulong, ud c.Pointer) c.Int // llgo:link (*State).Close C.lua_close func (L *State) Close() {} -// State *(lua_newstate) (lua_Alloc f, void *ud); +// llgo:link Newstate__0 C.lua_newstate +func Newstate__0(f Alloc, ud c.Pointer) *State { return nil } // llgo:link (*State).Newthread C.lua_newthread func (L *State) Newthread() *State { return nil } @@ -248,7 +250,8 @@ func (L *State) Touserdata(idx c.Int) c.Pointer { return nil } // llgo:link (*State).Tothread C.lua_tothread func (L *State) Tothread(idx c.Int) *State { return nil } -// LUA_API const void *(lua_topointer) (State *L, int idx); +// llgo:link (*State).Topointer C.lua_topointer +func (L *State) Topointer(idx c.Int) c.Pointer { return nil } /* * Comparison and arithmetic functions @@ -322,7 +325,8 @@ func (L *State) Newuserdatauv(sz uintptr, nuvalue c.Int) c.Pointer { return nil // llgo:link (*State).Getmetatable C.lua_getmetatable func (L *State) Getmetatable(objindex c.Int) c.Int { return 0 } -// LUA_API int (lua_getiuservalue) (State *L, int idx, int n); +// llgo:link (*State).Getiuservalue C.lua_getiuservalue +func (L *State) Getiuservalue(idx c.Int, n c.Int) c.Int { return 0 } /* * set functions (stack -> Lua) @@ -352,7 +356,8 @@ func (L *State) Rawsetp(idx c.Int, p c.Pointer) {} // llgo:link (*State).Setmetatable C.lua_setmetatable func (L *State) Setmetatable(objindex c.Int) c.Int { return 0 } -//int (lua_setiuservalue) (State *L, int idx, int n); +// llgo:link (*State).Setiuservalue C.lua_setiuservalue +func (L *State) Setiuservalue(idx c.Int, n c.Int) c.Int { return 0 } /* * 'load' and 'call' functions (load and run Lua code) @@ -436,16 +441,26 @@ func (L *State) Next(idx c.Int) c.Int { return 0 } // llgo:link (*State).Error C.lua_error func (L *State) Error() c.Int { return 0 } -// LUA_API void (lua_concat) (State *L, int n); -// LUA_API void (lua_len) (State *L, int idx); +// llgo:link (*State).Concat C.lua_concat +func (L *State) Concat(n c.Int) {} -// LUA_API size_t (lua_stringtonumber) (State *L, const char *s); +// llgo:link (*State).Len C.lua_len +func (L *State) Len(idx c.Int) {} -// LUA_API lua_Alloc (lua_getallocf) (State *L, void **ud); -// LUA_API void (lua_setallocf) (State *L, lua_Alloc f, void *ud); +// llgo:link (*State).Stringtonumber C.lua_stringtonumber +func (L *State) Stringtonumber(s *c.Char) c.Ulong { return 0 } -// LUA_API void (lua_toclose) (State *L, int idx); -// LUA_API void (lua_closeslot) (State *L, int idx); +// llgo:link (*State).Getallocf C.lua_getallocf +func (L *State) Getallocf(ud *c.Pointer) Alloc { return nil } + +// llgo:link (*State).Setallocf C.lua_setallocf +func (L *State) Setallocf(f Alloc, ud c.Pointer) Alloc { return nil } + +// llgo:link (*State).Toclose C.lua_toclose +func (L *State) Toclose(idx c.Int) {} + +// llgo:link (*State).Closeslot C.lua_closeslot +func (L *State) Closeslot(idx c.Int) {} /* ** {============================================================== @@ -454,7 +469,6 @@ func (L *State) Error() c.Int { return 0 } */ // #define lua_getextraspace(L) ((void *)((char *)(L) - LUA_EXTRASPACE)) - func (L *State) Tonumber(idx c.Int) Number { return L.Tonumberx(idx, nil) } func (L *State) Tostring(idx c.Int) *c.Char { return L.Tolstring(idx, nil) } func (L *State) Tointeger(idx c.Int) Integer { return L.Tointegerx(idx, nil) } @@ -475,9 +489,13 @@ func (L *State) Isboolean(n c.Int) bool { return L.Type(n) == c.Int(BOOLEA func (L *State) Isthread(n c.Int) bool { return L.Type(n) == c.Int(THREAD) } func (L *State) Isnone(n c.Int) bool { return L.Type(n) == c.Int(NONE) } func (L *State) Isnoneornil(n c.Int) bool { return L.Type(n) <= 0 } +func (L *State) Pushliteral(s *c.Char) *c.Char { + return L.Pushstring(s) +} -// #define lua_pushliteral(L, s) lua_pushstring(L, "" s) -// #define lua_pushglobaltable(L) ((void)lua_rawgeti(L, LUA_REGISTRYINDEX, LUA_RIDX_GLOBALS)) +func (L *State) Pushglobaltable() c.Int { + return L.Rawgeti(REGISTRYINDEX, RIDX_GLOBALS) +} func (L *State) Insert(idx c.Int) { L.Rotate(idx, 1) @@ -505,8 +523,13 @@ func (L *State) Newuserdata(sz uintptr) c.Pointer { return L.Newuserdatauv(sz, 1) } -// #define lua_getuservalue(L,idx) lua_getiuservalue(L,idx,1) -// #define lua_setuservalue(L,idx) lua_setiuservalue(L,idx,1) +func (L *State) Getuservalue(idx c.Int) c.Int { + return L.Getiuservalue(idx, 1) +} + +func (L *State) Setuservalue(idx c.Int) c.Int { + return L.Setiuservalue(idx, 1) +} // #define LUA_NUMTAGS LUA_NUMTYPES From 4defe734e2591e1b65ae3faac151b2ff5117f02b Mon Sep 17 00:00:00 2001 From: luoliwoshang <2643523683@qq.com> Date: Mon, 16 Sep 2024 23:31:17 +0800 Subject: [PATCH 4/5] c/lua:custom alloc for state --- c/lua/_demo/state-alloc/alloc.go | 36 ++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 c/lua/_demo/state-alloc/alloc.go diff --git a/c/lua/_demo/state-alloc/alloc.go b/c/lua/_demo/state-alloc/alloc.go new file mode 100644 index 00000000..0b4ee76c --- /dev/null +++ b/c/lua/_demo/state-alloc/alloc.go @@ -0,0 +1,36 @@ +package main + +import ( + "github.com/goplus/llgo/c" + "github.com/goplus/llgo/c/lua" +) + +func alloc(ud c.Pointer, ptr c.Pointer, osize c.Ulong, nsize c.Ulong) c.Pointer { + if nsize == 0 { + c.Free(ptr) + return nil + } else { + return c.Realloc(ptr, uintptr(nsize)) + } +} + +func main() { + L := lua.Newstate__0(alloc, nil) + defer L.Close() + L.Openlibs() + if res := L.Dostring(c.Str("print('new state success')")); res != lua.OK { + println("newstate error") + } + + allocf := L.Getallocf(nil) + L.Setallocf(allocf, nil) + + if res := L.Dostring(c.Str("print('set newstate success')")); res != lua.OK { + println("set newstate error") + } +} + +/* Expected output: +new state success +set newstate success +*/ From d4273d8e3fce563c35843b60aad444ad9502fda9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E4=B9=8B=E9=98=B3?= <51194195+luoliwoshang@users.noreply.github.com> Date: Tue, 17 Sep 2024 09:01:44 +0800 Subject: [PATCH 5/5] remove redundant fn body --- c/lua/lauxlib.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/c/lua/lauxlib.go b/c/lua/lauxlib.go index df7aac0d..e12978ae 100644 --- a/c/lua/lauxlib.go +++ b/c/lua/lauxlib.go @@ -34,7 +34,7 @@ func (L *State) Loadfile(filename *c.Char) c.Int { return L.Loadfilex(filename, func (L *State) Loadstring(s *c.Char) c.Int { return 0 } //go:linkname Newstate__1 C.luaL_newstate -func Newstate__1() *State { return nil } +func Newstate__1() *State // /* // ** ===============================================================