| 140 | } |
| 141 | |
| 142 | lua_State* skr_lua_newstate(skr_vfs_t* vfs) |
| 143 | { |
| 144 | lua_State* L = lua_newstate( |
| 145 | +[](void* ud, void* ptr, size_t osize, size_t nsize) -> void* { |
| 146 | if (nsize == 0) |
| 147 | { |
| 148 | sakura_free(ptr); |
| 149 | return nullptr; |
| 150 | } |
| 151 | else if (osize == 0) |
| 152 | { |
| 153 | return sakura_malloc(nsize); |
| 154 | } |
| 155 | else |
| 156 | { |
| 157 | return sakura_realloc(ptr, nsize); |
| 158 | } |
| 159 | }, |
| 160 | nullptr); |
| 161 | auto extra = SkrNew<skr_lua_state_extra_t>(); |
| 162 | extra->vfs = vfs; |
| 163 | *(void**)lua_getextraspace(L) = extra; |
| 164 | |
| 165 | // open standard libraries |
| 166 | luaL_openlibs(L); |
| 167 | luaopen_package(L); |
| 168 | // // insert loader |
| 169 | // lua_pushcfunction(L,skr_load_file,"skr_load_file"); |
| 170 | // int loaderFunc = lua_gettop(L); |
| 171 | |
| 172 | // lua_getglobal(L,"package"); |
| 173 | // lua_getfield(L,-1,"searchers"); |
| 174 | |
| 175 | // int loaderTable = lua_gettop(L); |
| 176 | |
| 177 | // for(auto i = lua_objlen(L,loaderTable) + 1; i > 2u; i--) |
| 178 | // { |
| 179 | // lua_rawgeti(L,loaderTable,i-1); |
| 180 | // lua_rawseti(L,loaderTable,i); |
| 181 | // } |
| 182 | // lua_pushvalue(L,loaderFunc); |
| 183 | // lua_rawseti(L,loaderTable,2); |
| 184 | // lua_settop(L, 0); |
| 185 | |
| 186 | // create skr global table |
| 187 | lua_newtable(L); |
| 188 | lua_pushvalue(L, -1); |
| 189 | lua_setglobal(L, "skr"); |
| 190 | lua_pop(L, 1); |
| 191 | |
| 192 | // bind utilities |
| 193 | lua_pushcfunction(L, +[](lua_State* L) -> int { |
| 194 | auto asize = luaL_checkinteger(L, 1); |
| 195 | auto nsize = luaL_checkinteger(L, 2); |
| 196 | lua_createtable(L, (int)asize, (int)nsize); |
| 197 | return 1; |
| 198 | }, "newtable"); |
| 199 | lua_setglobal(L, "newtable"); |
no test coverage detected