golua_c_lua54.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. package lua
  2. /*
  3. #define LUA_USE_MACOSX
  4. #include "golua.h"
  5. typedef struct _chunk {
  6. int size; // chunk size
  7. char *buffer; // chunk data
  8. char* toread; // chunk to read
  9. } chunk;
  10. LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
  11. return lua_newuserdatauv(L, size, 1);
  12. }
  13. LUA_API int (lua_gc_compat) (lua_State *L, int what, int data) {
  14. return lua_gc(L, what, data);
  15. }
  16. static const char * reader (lua_State *L, void *ud, size_t *sz) {
  17. chunk *ck = (chunk *)ud;
  18. if (ck->size > LUAL_BUFFERSIZE) {
  19. ck->size -= LUAL_BUFFERSIZE;
  20. *sz = LUAL_BUFFERSIZE;
  21. ck->toread = ck->buffer;
  22. ck->buffer += LUAL_BUFFERSIZE;
  23. }else{
  24. *sz = ck->size;
  25. ck->toread = ck->buffer;
  26. ck->size = 0;
  27. }
  28. return ck->toread;
  29. }
  30. static int writer (lua_State *L, const void* b, size_t size, void* B) {
  31. static int count=0;
  32. (void)L;
  33. luaL_addlstring((luaL_Buffer*) B, (const char *)b, size);
  34. return 0;
  35. }
  36. // load function chunk dumped from dump_chunk
  37. int load_chunk(lua_State *L, const char *b, int size, const char* chunk_name) {
  38. chunk ck;
  39. ck.buffer = b;
  40. ck.size = size;
  41. int err;
  42. err = lua_load(L, reader, &ck, chunk_name, NULL);
  43. if (err != 0) {
  44. return luaL_error(L, "unable to load chunk, err: %d", err);
  45. }
  46. return 0;
  47. }
  48. void clua_openio(lua_State* L)
  49. {
  50. luaL_requiref(L, "io", &luaopen_io, 1);
  51. lua_pop(L, 1);
  52. }
  53. void clua_openmath(lua_State* L)
  54. {
  55. luaL_requiref(L, "math", &luaopen_math, 1);
  56. lua_pop(L, 1);
  57. }
  58. void clua_openpackage(lua_State* L)
  59. {
  60. luaL_requiref(L, "package", &luaopen_package, 1);
  61. lua_pop(L, 1);
  62. }
  63. void clua_openstring(lua_State* L)
  64. {
  65. luaL_requiref(L, "string", &luaopen_string, 1);
  66. lua_pop(L, 1);
  67. }
  68. void clua_opentable(lua_State* L)
  69. {
  70. luaL_requiref(L, "table", &luaopen_table, 1);
  71. lua_pop(L, 1);
  72. }
  73. void clua_openos(lua_State* L)
  74. {
  75. luaL_requiref(L, "os", &luaopen_os, 1);
  76. lua_pop(L, 1);
  77. }
  78. void clua_opencoroutine(lua_State *L)
  79. {
  80. luaL_requiref(L, "coroutine", &luaopen_coroutine, 1);
  81. lua_pop(L, 1);
  82. }
  83. void clua_opendebug(lua_State *L)
  84. {
  85. luaL_requiref(L, "debug", &luaopen_debug, 1);
  86. lua_pop(L, 1);
  87. }
  88. // dump function chunk from luaL_loadstring
  89. int dump_chunk (lua_State *L) {
  90. luaL_Buffer b;
  91. luaL_checktype(L, -1, LUA_TFUNCTION);
  92. lua_settop(L, -1);
  93. luaL_buffinit(L,&b);
  94. int err;
  95. err = lua_dump(L, writer, &b, 0);
  96. if (err != 0){
  97. return luaL_error(L, "unable to dump given function, err:%d", err);
  98. }
  99. luaL_pushresult(&b);
  100. return 0;
  101. }
  102. */
  103. import "C"
  104. import "unsafe"
  105. func luaToInteger(s *C.lua_State, n C.int) C.longlong {
  106. return C.lua_tointegerx(s, n, nil)
  107. }
  108. func luaToNumber(s *C.lua_State, n C.int) C.double {
  109. return C.lua_tonumberx(s, n, nil)
  110. }
  111. func lualLoadFile(s *C.lua_State, filename *C.char) C.int {
  112. return C.luaL_loadfilex(s, filename, nil)
  113. }
  114. // lua_equal
  115. func (L *State) Equal(index1, index2 int) bool {
  116. return C.lua_compare(L.s, C.int(index1), C.int(index2), C.LUA_OPEQ) == 1
  117. }
  118. // lua_lessthan
  119. func (L *State) LessThan(index1, index2 int) bool {
  120. return C.lua_compare(L.s, C.int(index1), C.int(index2), C.LUA_OPLT) == 1
  121. }
  122. func (L *State) ObjLen(index int) uint {
  123. return uint(C.lua_rawlen(L.s, C.int(index)))
  124. }
  125. // lua_tointeger
  126. func (L *State) ToInteger(index int) int {
  127. return int(C.lua_tointegerx(L.s, C.int(index), nil))
  128. }
  129. // lua_tonumber
  130. func (L *State) ToNumber(index int) float64 {
  131. return float64(C.lua_tonumberx(L.s, C.int(index), nil))
  132. }
  133. // lua_yield
  134. func (L *State) Yield(nresults int) int {
  135. return int(C.lua_yieldk(L.s, C.int(nresults), 0, nil))
  136. }
  137. func (L *State) pcall(nargs, nresults, errfunc int) int {
  138. return int(C.lua_pcallk(L.s, C.int(nargs), C.int(nresults), C.int(errfunc), 0, nil))
  139. }
  140. // Pushes on the stack the value of a global variable (lua_getglobal)
  141. func (L *State) GetGlobal(name string) {
  142. Ck := C.CString(name)
  143. defer C.free(unsafe.Pointer(Ck))
  144. C.lua_getglobal(L.s, Ck)
  145. }
  146. // lua_resume
  147. func (L *State) Resume(narg int) int {
  148. return int(C.lua_resume(L.s, nil, C.int(narg), nil))
  149. }
  150. // lua_setglobal
  151. func (L *State) SetGlobal(name string) {
  152. Cname := C.CString(name)
  153. defer C.free(unsafe.Pointer(Cname))
  154. C.lua_setglobal(L.s, Cname)
  155. }
  156. // Calls luaopen_debug
  157. func (L *State) OpenDebug() {
  158. C.clua_opendebug(L.s)
  159. }
  160. // Calls luaopen_coroutine
  161. func (L *State) OpenCoroutine() {
  162. C.clua_opencoroutine(L.s)
  163. }
  164. // lua_insert
  165. func (L *State) Insert(index int) { C.lua_rotate(L.s, C.int(index), 1) }
  166. // lua_remove
  167. func (L *State) Remove(index int) {
  168. C.lua_rotate(L.s, C.int(index), -1)
  169. C.lua_settop(L.s, C.int(-2))
  170. }
  171. // lua_replace
  172. func (L *State) Replace(index int) {
  173. C.lua_copy(L.s, -1, C.int(index))
  174. C.lua_settop(L.s, -2)
  175. }
  176. // lua_rawgeti
  177. func (L *State) RawGeti(index int, n int) {
  178. C.lua_rawgeti(L.s, C.int(index), C.longlong(n))
  179. }
  180. // lua_rawseti
  181. func (L *State) RawSeti(index int, n int) {
  182. C.lua_rawseti(L.s, C.int(index), C.longlong(n))
  183. }
  184. // lua_gc
  185. func (L *State) GC(what, data int) int {
  186. return int(C.lua_gc_compat(L.s, C.int(what), C.int(data)))
  187. }