|
| 1 | +#include "lua.h" |
| 2 | +#include "lauxlib.h" |
| 3 | + |
| 4 | +static int s_newtable(lua_State* state) { |
| 5 | + lua_newtable(state); |
| 6 | + return 1; |
| 7 | +} |
| 8 | + |
| 9 | +int plua_newtable(lua_State* state) { |
| 10 | + lua_pushcfunction(state, s_newtable); |
| 11 | + return lua_pcall(state, 0, 1, 0); |
| 12 | +} |
| 13 | + |
| 14 | +static int s_len(lua_State* state) { |
| 15 | + lua_pushinteger(state, luaL_len(state, -1)); |
| 16 | + return 1; |
| 17 | +} |
| 18 | + |
| 19 | +int pluaL_len(lua_State* state, int index, lua_Integer* len) { |
| 20 | + index = lua_absindex(state, index); |
| 21 | + lua_pushcfunction(state, s_len); |
| 22 | + lua_pushvalue(state, index); |
| 23 | + int r = lua_pcall(state, 1, 1, 0); |
| 24 | + if (r == LUA_OK) { |
| 25 | + *len = lua_tointeger(state, -1); |
| 26 | + lua_pop(state, 1); |
| 27 | + } |
| 28 | + return r; |
| 29 | +} |
| 30 | + |
| 31 | +static int s_geti(lua_State* state) { |
| 32 | + lua_gettable(state, -2); |
| 33 | + return 1; |
| 34 | +} |
| 35 | + |
| 36 | +int plua_geti(lua_State* state, int index, lua_Integer i) { |
| 37 | + index = lua_absindex(state, index); |
| 38 | + lua_pushcfunction(state, s_geti); |
| 39 | + lua_pushvalue(state, index); |
| 40 | + lua_pushinteger(state, i); |
| 41 | + return lua_pcall(state, 2, 1, 0); |
| 42 | +} |
| 43 | + |
| 44 | +static int s_gettable(lua_State* state) { |
| 45 | + lua_gettable(state, -2); |
| 46 | + return 1; |
| 47 | +} |
| 48 | + |
| 49 | +int plua_gettable(lua_State* state, int index) { |
| 50 | + index = lua_absindex(state, index); |
| 51 | + lua_pushcfunction(state, s_gettable); |
| 52 | + lua_pushvalue(state, index); |
| 53 | + lua_rotate(state, -3, -1); |
| 54 | + return lua_pcall(state, 2, 1, 0); |
| 55 | +} |
| 56 | + |
| 57 | +static int s_newthread(lua_State* state) { |
| 58 | + lua_newthread(state); |
| 59 | + return 1; |
| 60 | +} |
| 61 | + |
| 62 | +int plua_newthread(lua_State* state, lua_State** thread) { |
| 63 | + lua_pushcfunction(state, s_newthread); |
| 64 | + int r = lua_pcall(state, 0, 1, 0); |
| 65 | + if (r == LUA_OK) { |
| 66 | + *thread = lua_tothread(state, -1); |
| 67 | + } |
| 68 | + return r; |
| 69 | +} |
| 70 | + |
| 71 | +static int s_newuserdata(lua_State* state) { |
| 72 | + size_t size = lua_tointeger(state, -1); |
| 73 | + lua_pop(state, 1); |
| 74 | + lua_newuserdata(state, size); |
| 75 | + return 1; |
| 76 | +} |
| 77 | + |
| 78 | +int plua_newuserdata(lua_State* state, size_t size, void** ud) { |
| 79 | + lua_pushcfunction(state, s_newuserdata); |
| 80 | + lua_pushinteger(state, size); |
| 81 | + int r = lua_pcall(state, 1, 1, 0); |
| 82 | + if (r == LUA_OK) { |
| 83 | + *ud = lua_touserdata(state, -1); |
| 84 | + } |
| 85 | + return r; |
| 86 | +} |
| 87 | + |
| 88 | +static int s_next(lua_State* state) { |
| 89 | + if (lua_next(state, -2) == 0) { |
| 90 | + return 0; |
| 91 | + } else { |
| 92 | + return 2; |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +int plua_next(lua_State* state, int index, int* res) { |
| 97 | + int top = lua_gettop(state) - 1; |
| 98 | + index = lua_absindex(state, index); |
| 99 | + lua_pushcfunction(state, s_next); |
| 100 | + lua_pushvalue(state, index); |
| 101 | + lua_rotate(state, -3, -1); |
| 102 | + int r = lua_pcall(state, 2, LUA_MULTRET, 0); |
| 103 | + if (r == LUA_OK) { |
| 104 | + if (lua_gettop(state) - top == 2) { |
| 105 | + *res = 1; |
| 106 | + } else { |
| 107 | + *res = 0; |
| 108 | + } |
| 109 | + } |
| 110 | + return r; |
| 111 | +} |
| 112 | + |
| 113 | +static int s_pushcclosure(lua_State* state) { |
| 114 | + lua_CFunction cf = lua_tocfunction(state, -1); |
| 115 | + lua_pop(state, 1); |
| 116 | + lua_pushcclosure(state, cf, lua_gettop(state)); |
| 117 | + return 1; |
| 118 | +} |
| 119 | + |
| 120 | +int plua_pushcclosure(lua_State* state, lua_CFunction function, int n) { |
| 121 | + lua_pushcfunction(state, s_pushcclosure); |
| 122 | + lua_insert(state, -(n + 1)); |
| 123 | + lua_pushcfunction(state, function); |
| 124 | + return lua_pcall(state, n + 1, 1, 0); |
| 125 | +} |
| 126 | + |
| 127 | +static int s_pushlstring(lua_State* state) { |
| 128 | + char const* s = lua_touserdata(state, -2); |
| 129 | + size_t len = lua_tointeger(state, -1); |
| 130 | + lua_pop(state, 2); |
| 131 | + lua_pushlstring(state, s, len); |
| 132 | + return 1; |
| 133 | +} |
| 134 | + |
| 135 | +int plua_pushlstring(lua_State* state, char const* s, size_t len) { |
| 136 | + lua_pushcfunction(state, s_pushlstring); |
| 137 | + lua_pushlightuserdata(state, (void*)s); |
| 138 | + lua_pushinteger(state, len); |
| 139 | + return lua_pcall(state, 2, 1, 0); |
| 140 | +} |
| 141 | + |
| 142 | +static int s_pushstring(lua_State* state) { |
| 143 | + char const* s = lua_touserdata(state, -1); |
| 144 | + lua_pop(state, 1); |
| 145 | + lua_pushstring(state, s); |
| 146 | + return 1; |
| 147 | +} |
| 148 | + |
| 149 | +int plua_pushstring(lua_State* state, char const* s) { |
| 150 | + lua_pushcfunction(state, s_pushstring); |
| 151 | + lua_pushlightuserdata(state, (void*)s); |
| 152 | + return lua_pcall(state, 1, 1, 0); |
| 153 | +} |
| 154 | + |
| 155 | +static int s_rawset(lua_State* state) { |
| 156 | + lua_rawset(state, -3); |
| 157 | + return 0; |
| 158 | +} |
| 159 | + |
| 160 | +int plua_rawset(lua_State* state, int index) { |
| 161 | + lua_pushvalue(state, index); |
| 162 | + lua_insert(state, -3); |
| 163 | + lua_pushcfunction(state, s_rawset); |
| 164 | + lua_insert(state, -4); |
| 165 | + return lua_pcall(state, 3, 0, 0); |
| 166 | +} |
| 167 | + |
| 168 | +static int s_settable(lua_State* state) { |
| 169 | + lua_settable(state, -3); |
| 170 | + return 0; |
| 171 | +} |
| 172 | + |
| 173 | +int plua_settable(lua_State* state, int index) { |
| 174 | + lua_pushvalue(state, index); |
| 175 | + lua_insert(state, -3); |
| 176 | + lua_pushcfunction(state, s_settable); |
| 177 | + lua_insert(state, -4); |
| 178 | + return lua_pcall(state, 3, 0, 0); |
| 179 | +} |
| 180 | + |
| 181 | +static int s_tostring(lua_State* state) { |
| 182 | + char const** s = lua_touserdata(state, -1); |
| 183 | + *s = lua_tostring(state, -2); |
| 184 | + lua_pop(state, 1); |
| 185 | + return 1; |
| 186 | +} |
| 187 | + |
| 188 | +int plua_tostring(lua_State* state, int index, char const** s) { |
| 189 | + index = lua_absindex(state, index); |
| 190 | + lua_pushcfunction(state, s_tostring); |
| 191 | + lua_pushvalue(state, index); |
| 192 | + lua_pushlightuserdata(state, s); |
| 193 | + int r = lua_pcall(state, 2, 1, 0); |
| 194 | + if (r == LUA_OK) { |
| 195 | + lua_replace(state, index); |
| 196 | + } |
| 197 | + return r; |
| 198 | +} |
| 199 | + |
| 200 | +typedef int (*RustCallback)(lua_State*); |
| 201 | +// Out of stack space in callback |
| 202 | +static int const RCALL_STACK_ERR = -2; |
| 203 | +// Throw the error at the top of the stack |
| 204 | +static int const RCALL_ERR = -3; |
| 205 | + |
| 206 | +static int s_call_rust(lua_State* state) { |
| 207 | + RustCallback callback = lua_touserdata(state, lua_upvalueindex(1)); |
| 208 | + int ret = callback(state); |
| 209 | + |
| 210 | + if (ret == LUA_MULTRET) { |
| 211 | + return LUA_MULTRET; |
| 212 | + } else if (ret == RCALL_STACK_ERR) { |
| 213 | + return luaL_error(state, "stack overflow in rust callback"); |
| 214 | + } else if (ret == RCALL_ERR) { |
| 215 | + return lua_error(state); |
| 216 | + } else { |
| 217 | + return ret; |
| 218 | + } |
| 219 | +} |
| 220 | + |
| 221 | +int plua_pushrclosure(lua_State* state, RustCallback function, int n) { |
| 222 | + lua_pushlightuserdata(state, function); |
| 223 | + lua_insert(state, -(n + 1)); |
| 224 | + return plua_pushcclosure(state, s_call_rust, n + 1); |
| 225 | +} |
0 commit comments