Exemple #1
0
static lua_State* luastate_New(int (*terminationCallback)(void* stateptr)) {
    lua_State* l = luaL_newstate();

    lua_gc(l, LUA_GCSTOP, 0);
    //lua_gc(l, LUA_GCSETPAUSE, 110);
    //lua_gc(l, LUA_GCSETSTEPMUL, 300);

    if (terminationCallback) {
        // allow possible script kill when script runs too long:
        lua_sethook(l, &luastate_InstructionCallback, LUA_MASKCOUNT, 5000);

        // push the pointer to the lua registry:
        lua_pushstring(l, "terminationCallback");
        lua_pushlightuserdata(l, (void*)terminationCallback);
        lua_settable(l, LUA_REGISTRYINDEX);
    }

    // standard libs
    luaL_openlibs(l);
    luastate_RememberTracebackFunc(l);
    luastate_VoidDebug(l);
    luastate_AddBlitwizFuncs(l);

    // clean up global namespace apart from whitelist:
    luastate_ApplyWhitelist(l);

    // own dofile/loadfile/print
    lua_pushcfunction(l, &luafuncs_loadfile);
    lua_setglobal(l, "loadfile");
    lua_pushcfunction(l, &luafuncs_dofile);
    lua_setglobal(l, "dofile");
    lua_pushcfunction(l, &luafuncs_print);
    lua_setglobal(l, "print");
    lua_pushcfunction(l, &luafuncs_dostring);
    lua_setglobal(l, "dostring");
    lua_pushcfunction(l, &luafuncs_dostring_returnvalues);
    lua_setglobal(l, "dostring_returnvalues");

    // obtain the blitwiz lib
    lua_getglobal(l, "blitwizard");

    // blitwizard.runDelayed:
    lua_pushstring(l, "runDelayed");
    lua_pushcfunction(l, &luafuncs_runDelayed);
    lua_settable(l, -3);

    // blitwizard.object:
    lua_pushstring(l, "object");
    luastate_CreateObjectTable(l);
    lua_settable(l, -3);

    // blitwizard.getAllObjects():
    lua_pushstring(l, "getAllObjects");
    lua_pushcfunction(l, &luafuncs_getAllObjects);
    lua_settable(l, -3);

    // blitwizard.scanFor2dObjects():
    lua_pushstring(l, "scanFor2dObjects");
    lua_pushcfunction(l, &luafuncs_scanFor2dObjects);
    lua_settable(l, -3);

    // blitwizard.setStep:
    lua_pushstring(l, "setStep");
    lua_pushcfunction(l, &luafuncs_setstep);
    lua_settable(l, -3);

    // blitwizard.loadResourceArchive
    lua_pushstring(l, "loadResourceArchive");
    lua_pushcfunction(l, &luafuncs_loadResourceArchive);
    lua_settable(l, -3);

    // blitwizard.getTemplateDirectory:
    lua_pushstring(l, "getTemplateDirectory");
    lua_pushcfunction(l, &luafuncs_getTemplateDirectory);
    lua_settable(l, -3);

    // blitwizard namespaces
    lua_pushstring(l, "graphics");
    luastate_CreateGraphicsTable(l);
    lua_settable(l, -3);

    lua_pushstring(l, "net");
    luastate_CreateNetTable(l);
    lua_settable(l, -3);

    lua_pushstring(l, "audio");
    luastate_CreateAudioTable(l);
    lua_settable(l, -3);

    lua_pushstring(l, "debug");
    luastate_CreateDebugTable(l);
    lua_settable(l, -3);

    lua_pushstring(l, "callback");
    lua_newtable(l);
        lua_pushstring(l, "event");
        lua_newtable(l);
        lua_settable(l,  -3);
    lua_settable(l, -3);

    lua_pushstring(l, "time");
    luastate_CreateTimeTable(l);
    lua_settable(l, -3);

    lua_pushstring(l, "physics");
    luastate_CreatePhysicsTable(l);
    lua_settable(l, -3);

    // we still have the module "blitwiz" on the stack here
    lua_pop(l, 1);

    // vector namespace:
    luastate_CreateVectorTable(l);
    lua_setglobal(l, "vector");

    // obtain math table
    lua_getglobal(l, "math");

    // math namespace extensions
    lua_pushstring(l, "trandom");
    lua_pushcfunction(l, &luafuncs_trandom);
    lua_settable(l, -3);

    // remove math table from stack
    lua_pop(l, 1);

    // obtain os table
    lua_getglobal(l, "os");

    // os namespace extensions
    lua_pushstring(l, "exit");
    lua_pushcfunction(l, &luafuncs_exit);
    lua_settable(l, -3);
    lua_pushstring(l, "chdir");
    lua_pushcfunction(l, &luafuncs_chdir);
    lua_settable(l, -3);
    lua_pushstring(l, "templatedir");
    lua_pushcfunction(l, &luafuncs_templatedir);
    lua_settable(l, -3);
    lua_pushstring(l, "sleep");
    lua_pushcfunction(l, &luafuncs_sleep);
    lua_settable(l, -3);
    lua_pushstring(l, "gameluapath");
    lua_pushcfunction(l, &luafuncs_gameluapath);
    lua_settable(l, -3);
    lua_pushstring(l, "forcetemplatedir");
    lua_pushcfunction(l, &luafuncs_forcetemplatedir);
    lua_settable(l, -3);
    lua_pushstring(l, "openConsole");
    lua_pushcfunction(l, &luafuncs_openConsole);
    lua_settable(l, -3);
    lua_pushstring(l, "ls");
    lua_pushcfunction(l, &luafuncs_ls);
    lua_settable(l, -3);
    lua_pushstring(l, "isdir");
    lua_pushcfunction(l, &luafuncs_isdir);
    lua_settable(l, -3);
    lua_pushstring(l, "getcwd");
    lua_pushcfunction(l, &luafuncs_getcwd);
    lua_settable(l, -3);
    lua_pushstring(l, "exists");
    lua_pushcfunction(l, &luafuncs_exists);
    lua_settable(l, -3);
    lua_pushstring(l, "sysname");
    lua_pushcfunction(l, &luafuncs_sysname);
    lua_settable(l, -3);
    lua_pushstring(l, "sysversion");
    lua_pushcfunction(l, &luafuncs_sysversion);
    lua_settable(l, -3);

    // throw table "os" off the stack
    lua_pop(l, 1);

    // get "string" table for custom string functions
    lua_getglobal(l, "string");

    // set custom string functions
    lua_pushstring(l, "startswith");
    lua_pushcfunction(l, &luafuncs_startswith);
    lua_settable(l, -3);
    lua_pushstring(l, "endswith");
    lua_pushcfunction(l, &luafuncs_endswith);
    lua_settable(l, -3);
    lua_pushstring(l, "split");
    lua_pushcfunction(l, &luafuncs_split);
    lua_settable(l, -3);

    // throw table "string" off the stack
    lua_pop(l, 1);

    // set _VERSION string:
    char vstr[512];
    char is64bit[] = " (64-bit binary)";
#ifndef _64BIT
    strcpy(is64bit, "");
#endif
    snprintf(vstr, sizeof(vstr), "Blitwizard %s based on Lua 5.2%s", VERSION, is64bit);
    lua_pushstring(l, vstr);
    lua_setglobal(l, "_VERSION");

    // now set a default blitwizard.onLog handler:
    lua_getglobal(l, "dostring");
    lua_pushstring(l, "function blitwizard.onLog(type, msg)\n"
        "    print(\"[LOG:\" .. type .. \"] \" .. msg)\n"
        "end\n");
    // coverity[unchecked_value] - we don't want to cause error message
    // logs from logging itself since that could cause an infinite loop.
    // therefore, we don't handle possible lua errors here.
    lua_pcall(l, 1, 0, 0);

    return l;
}
Exemple #2
0
static lua_State* luastate_New(void) {
    lua_State* l = luaL_newstate();

    lua_gc(l, LUA_GCSETPAUSE, 110);
    lua_gc(l, LUA_GCSETSTEPMUL, 300);

    // standard libs
    luaL_openlibs(l);
    luastate_RememberTracebackFunc(l);
    luastate_VoidDebug(l);
    luastate_AddBlitwizFuncs(l);

    // own dofile/loadfile/print
    lua_pushcfunction(l, &luafuncs_loadfile);
    lua_setglobal(l, "loadfile");
    lua_pushcfunction(l, &luafuncs_dofile);
    lua_setglobal(l, "dofile");
    lua_pushcfunction(l, &luafuncs_print);
    lua_setglobal(l, "print");

    // obtain the blitwiz lib
    lua_getglobal(l, "blitwiz");

    // blitwiz.setStep:
    lua_pushstring(l, "setStep");
    lua_pushcfunction(l, &luafuncs_setstep);
    lua_settable(l, -3);

    // blitwiz namespaces
    lua_pushstring(l, "graphics");
    luastate_CreateGraphicsTable(l);
    lua_settable(l, -3);

    lua_pushstring(l, "net");
    luastate_CreateNetTable(l);
    lua_settable(l, -3);

    /*lua_pushstring(l, "sound");
    luastate_CreateSoundTable(l);
    lua_settable(l, -3);*/

    lua_pushstring(l, "callback");
    lua_newtable(l);
        lua_pushstring(l, "event");
        lua_newtable(l);
        lua_settable(l,  -3);
    lua_settable(l, -3);

    lua_pushstring(l, "time");
    luastate_CreateTimeTable(l);
    lua_settable(l, -3);

    lua_pushstring(l, "physics");
    luastate_CreatePhysicsTable(l);
    lua_settable(l, -3);

    // we still have the module "blitwiz" on the stack here
    lua_pop(l, 1);

    // obtain math table
    lua_getglobal(l, "math");

    // math namespace extensions
    lua_pushstring(l, "trandom");
    lua_pushcfunction(l, &luafuncs_trandom);
    lua_settable(l, -3);

    // remove math table from stack
    lua_pop(l, 1);

    // obtain os table
    lua_getglobal(l, "os");

    // os namespace extensions
    lua_pushstring(l, "exit");
    lua_pushcfunction(l, &luafuncs_exit);
    lua_settable(l, -3);
    lua_pushstring(l, "chdir");
    lua_pushcfunction(l, &luafuncs_chdir);
    lua_settable(l, -3);
    lua_pushstring(l, "openConsole");
    lua_pushcfunction(l, &luafuncs_openConsole);
    lua_settable(l, -3);
    lua_pushstring(l, "ls");
    lua_pushcfunction(l, &luafuncs_ls);
    lua_settable(l, -3);
    lua_pushstring(l, "isdir");
    lua_pushcfunction(l, &luafuncs_isdir);
    lua_settable(l, -3);
    lua_pushstring(l, "getcwd");
    lua_pushcfunction(l, &luafuncs_getcwd);
    lua_settable(l, -3);
    lua_pushstring(l, "exists");
    lua_pushcfunction(l, &luafuncs_exists);
    lua_settable(l, -3);
    lua_pushstring(l, "sysname");
    lua_pushcfunction(l, &luafuncs_sysname);
    lua_settable(l, -3);
    lua_pushstring(l, "sysversion");
    lua_pushcfunction(l, &luafuncs_sysversion);
    lua_settable(l, -3);

    // throw table "os" off the stack
    lua_pop(l, 1);

    // get "string" table for custom string functions
    lua_getglobal(l, "string");

    // set custom string functions
    lua_pushstring(l, "starts");
    lua_pushcfunction(l, &luafuncs_startswith);
    lua_settable(l, -3);
    lua_pushstring(l, "ends");
    lua_pushcfunction(l, &luafuncs_endswith);
    lua_settable(l, -3);
    lua_pushstring(l, "split");
    lua_pushcfunction(l, &luafuncs_split);
    lua_settable(l, -3);

    // throw table "string" off the stack
    lua_pop(l, 1);

    char vstr[512];
    char is64bit[] = " (64-bit binary)";
#ifndef _64BIT
    strcpy(is64bit, "");
#endif
    snprintf(vstr, sizeof(vstr), "Blitwizard %s based on Lua 5.2%s", VERSION, is64bit);
    lua_pushstring(l, vstr);
    lua_setglobal(l, "_VERSION");

    return l;
}