| 193 | } |
| 194 | |
| 195 | void Initialize(HContext context) |
| 196 | { |
| 197 | lua_State* L = context->m_LuaState; |
| 198 | DM_LUA_STACK_CHECK(L, 0); |
| 199 | |
| 200 | luaL_openlibs(L); |
| 201 | |
| 202 | // To support 'math.mod' even though it's been deprecated in 5.1 and removed in 5.2 |
| 203 | lua_getglobal(L, "math"); |
| 204 | lua_getfield(L, -1, "fmod"); |
| 205 | lua_setfield(L, -2, "mod"); |
| 206 | lua_pop(L, 1); |
| 207 | |
| 208 | InitializeHash(L); |
| 209 | InitializeMsg(L); |
| 210 | InitializeVmath(L); |
| 211 | InitializeSys(L); |
| 212 | InitializeModule(L); |
| 213 | InitializeJson(L); |
| 214 | InitializeZlib(L); |
| 215 | InitializeHtml5(L); |
| 216 | InitializeLuasocket(L); |
| 217 | InitializeBitop(L); |
| 218 | InitializeGraphics(L, context->m_GraphicsContext); |
| 219 | |
| 220 | lua_register(L, "print", LuaPrint); |
| 221 | lua_register(L, "pprint", LuaPPrint); |
| 222 | |
| 223 | lua_getglobal(L, "math"); |
| 224 | if (!lua_isnil(L, -1)) { |
| 225 | uint32_t *seed = (uint32_t*) malloc(sizeof(uint32_t)); |
| 226 | *seed = 0; |
| 227 | lua_pushlightuserdata(L, seed); |
| 228 | lua_setglobal(L, RANDOM_SEED); |
| 229 | // discard first value to avoid repeated values |
| 230 | dmMath::Rand(seed); |
| 231 | |
| 232 | lua_pushcfunction(L, Lua_Math_Random); |
| 233 | lua_setfield(L, -2, "random"); |
| 234 | |
| 235 | lua_pushcfunction(L, Lua_Math_Randomseed); |
| 236 | lua_setfield(L, -2, "randomseed"); |
| 237 | } else { |
| 238 | dmLogWarning("math library not loaded") |
| 239 | } |
| 240 | |
| 241 | lua_pop(L, 1); |
| 242 | |
| 243 | lua_pushlightuserdata(L, (void*)context); |
| 244 | SCRIPT_CONTEXT_HASH = dmScript::SetGlobal(L, SCRIPT_CONTEXT); |
| 245 | |
| 246 | lua_pushlightuserdata(L, (void*)L); |
| 247 | lua_setglobal(L, SCRIPT_MAIN_THREAD); |
| 248 | |
| 249 | // Create main context table |
| 250 | lua_newtable(L); |
| 251 | // [-1] context_table |
| 252 | // Create weak subtable |