Exemple #1
0
int main(int argc, char *argv[])    
{   
    int i,ii;
    lua_State *L;
    L = lua_open();   
    luaopen_base(L);             /* opens the basic library */
    luaopen_table(L);            /* opens the table library */
    luaopen_string(L);           /* opens the string lib. */
    luaopen_math(L);             /* opens the math lib. */

    lua_pop(L,5);
    static const luaL_reg EventNet_meta[] = {
	{"__gc", Bla_gc},
        {"__tostring", Bla_tostring},
        {0, 0}
    };
            
    static const luaL_Reg RegisterEventNet[] = {
        {"new", BlaNew},
        {"sleep", BlaSleep},
        {"bla", BlaBla},
        {NULL, NULL}
    };

    luaL_openlib(L, "Bla", RegisterEventNet, 0);
    luaL_newmetatable(L, "Bla");
    luaL_openlib(L, 0, EventNet_meta, 0);
    lua_pushliteral(L, "__index");
    lua_pushvalue(L, -3);
    lua_rawset(L, -3);
    lua_pushliteral(L, "__metatable");
    lua_pushvalue(L, -3);
    lua_rawset(L, -3);
            
    lua_pop(L, 2);

    luaL_loadstring(L,"function main() local bla=Bla:new() bla:sleep() bla:bla() end");



    int count=0;
    lua_pcall(L,0,0,0);
    printf("Memory used = %d\n",lua_gc(L, LUA_GCCOUNT, 0));
    for(i=0;i<1;i++) {
	for(ii=0;ii<=len;ii++) {
	    count++;
	    snprintf(bla[ii].bla,50,"bla%d",ii);
	    bla[ii].thread = lua_newthread(L);
	    bla[ii].table  = luaL_ref (L,LUA_REGISTRYINDEX);

	    lua_pushthread(bla[ii].thread);
            lua_pushlightuserdata(bla[ii].thread,(void*) &bla[ii]);
            lua_settable(bla[ii].thread, LUA_REGISTRYINDEX);
	    lua_getglobal(bla[ii].thread, "main");
	    lua_resume(bla[ii].thread, 0);
//	    printf("init %s %p\n",bla[ii].bla,bla[ii].thread);
	    
	}
	for(ii=0;ii<=len;ii++) {
	    lua_resume(bla[ii].thread, 0);
//	    printf("in c bla[%d] = %p and bla string is    %s\n\n",ii,bla[ii].thread,bla[ii].bla);
	    lua_pushthread(bla[ii].thread);
	    lua_pushnil(bla[ii].thread);
	    lua_settable(bla[ii].thread, LUA_REGISTRYINDEX);
	    luaL_unref(L, LUA_REGISTRYINDEX, bla[ii].table );
	}
    }
    printf("Memory used = %d, cycle %d\n",lua_gc(L, LUA_GCCOUNT, 0),count);
    lua_gc(L, LUA_GCCOLLECT, 0);
    printf("Memory used = %d, cycle %d\n",lua_gc(L, LUA_GCCOUNT, 0),count);
    lua_close(L);


    return 0;   
} 
lua_State *luaScriptInit(char *buff) {
	ScriptErrorStr[0]='\0';
	ScriptErrorCode=0;
	setlocale(LC_ALL, "");
    //
	lua_State *L = lua_open();  /* create state */
    // 関数を登録する
    lua_register(L, "out", luaPrint);

	lua_register(L, "_MX", luaGetMouseX);
	lua_register(L, "_MY", luaGetMouseY);
	lua_register(L, "_ML", luaGetMouseL);
	lua_register(L, "_MR", luaGetMouseR);
	lua_register(L, "_MM", luaGetMouseM);

	lua_register(L, "_DT", luaGetDt);
    lua_register(L, "_FPS", luaGetFps);
    lua_register(L, "_BASE", luaGetBase);
    lua_register(L, "_TICKS", luaGetTickCount);
    lua_register(L, "_SETTICKS", luaSetTicks);
    lua_register(L, "_KEY", luaKey);
    lua_register(L, "_KEYDOWN", luaKeyDown);
    lua_register(L, "_KEYUP", luaKeyUp);
    lua_register(L, "_ANALOG", luaAnalog);
    lua_register(L, "_HAT", luaHat);
    lua_register(L, "_CHIPS", luaChips);
    lua_register(L, "_WEIGHT", luaWeight);
    lua_register(L, "_WIDTH", luaGetWidth);
    lua_register(L, "_HEIGHT", luaGetHeight);
    lua_register(L, "_FACE", luaGetFaces);
    lua_register(L, "_ZOOM", luaSetCCDZoom);
    lua_register(L, "_OX", luaObjPosx);
    lua_register(L, "_OY", luaObjPosy);
    lua_register(L, "_OZ", luaObjPosz);
    lua_register(L, "_X", luaPosx);
    lua_register(L, "_Y", luaPosy);
    lua_register(L, "_Z", luaPosz);
    lua_register(L, "_H", luaGetY);
    lua_register(L, "_AX", luaAx);
    lua_register(L, "_AY", luaAy);
    lua_register(L, "_AZ", luaAz);
    lua_register(L, "_EX", luaEx);
    lua_register(L, "_EY", luaEy);
    lua_register(L, "_EZ", luaEz);
    lua_register(L, "_GX", luaGx);
    lua_register(L, "_GY", luaGy);
    lua_register(L, "_GZ", luaGz);
    lua_register(L, "_XX", luaXx);
    lua_register(L, "_XY", luaXy);
    lua_register(L, "_XZ", luaXz);
    lua_register(L, "_YX", luaYx);
    lua_register(L, "_YY", luaYy);
    lua_register(L, "_YZ", luaYz);
    lua_register(L, "_ZX", luaZx);
    lua_register(L, "_ZY", luaZy);
    lua_register(L, "_ZZ", luaZz);
    lua_register(L, "_QX", luaQx);
    lua_register(L, "_QY", luaQy);
    lua_register(L, "_QZ", luaQz);
    lua_register(L, "_QW", luaQw);
    lua_register(L, "_RX", luaRx);
    lua_register(L, "_RY", luaRy);
    lua_register(L, "_RZ", luaRz);
    lua_register(L, "_LX", luaLx);
    lua_register(L, "_LY", luaLy);
    lua_register(L, "_LZ", luaLz);
    lua_register(L, "_VX", luaVx);
    lua_register(L, "_VY", luaVy);
    lua_register(L, "_VZ", luaVz);
    lua_register(L, "_FX", luaFx);
    lua_register(L, "_FY", luaFy);
    lua_register(L, "_FZ", luaFz);
    lua_register(L, "_WX", luaWx);
    lua_register(L, "_WY", luaWy);
    lua_register(L, "_WZ", luaWz);
    lua_register(L, "_CCD", luaGetCCD);
    lua_register(L, "_RED", luaGetCCDRed);
    lua_register(L, "_GREEN", luaGetCCDGreen);
    lua_register(L, "_BLUE", luaGetCCDBlue);
    lua_register(L, "_BYE", luaUnLinkBye);
    lua_register(L, "_SPLIT", luaUnLink);
    lua_register(L, "_RND", luaRnd);
    lua_register(L, "_TODEG", luaToDeg);
    lua_register(L, "_TORAD", luaToRad);
    lua_register(L, "_TYPE", luaGetType);
    lua_register(L, "_OPTION", luaGetOption);
    lua_register(L, "_EFFECT", luaGetEffect);
    lua_register(L, "_USER1", luaGetUserEffect);
    lua_register(L, "_USER2", luaGetUserOption);
    lua_register(L, "_DIR", luaGetDir);
    lua_register(L, "_ANGLE", luaGetAngle);
    lua_register(L, "_POWER", luaGetPower);
    lua_register(L, "_SPRING", luaGetSpring);
    lua_register(L, "_DAMPER", luaGetDamper);
    lua_register(L, "_BRAKE", luaGetBrake);
    lua_register(L, "_COLOR", luaGetColor);
    lua_register(L, "_PARENT", luaGetParent);
    lua_register(L, "_TOP", luaGetTop);
    lua_register(L, "_M", luaGetM);
    lua_register(L, "_I", luaGetI);
    lua_register(L, "_MOBJ", luaGetObjM);
    lua_register(L, "_IOBJ", luaGetObjI);
    lua_register(L, "_E", luaGetEnergy);
	lua_register(L, "_T",luaGetTolerant);
	lua_register(L, "_MOVE3D",luaMove3D);
	lua_register(L, "_LINE3D",luaLine3D);
	lua_register(L, "_MOVE2D",luaMove2D);
	lua_register(L, "_LINE2D",luaLine2D);
	lua_register(L, "_SETCOLOR",luaSetColor);
	lua_register(L, "_PLAYERS",luaGetPlayers);
	lua_register(L, "_PLAYERHOSTID",luaGetPlayerHostID);
	lua_register(L, "_PLAYERMYID",luaGetPlayerMyID);
	lua_register(L, "_PLAYERID",luaGetPlayerID);
	lua_register(L, "_PLAYERCHIPS",luaGetPlayerChips);
	lua_register(L, "_PLAYERCRUSHES",luaGetPlayerCrushes);
	lua_register(L, "_PLAYERRESETS",luaGetPlayerResets);
	lua_register(L, "_PLAYERINITS",luaGetPlayerInits);
	lua_register(L, "_PLAYERCOLOR",luaGetPlayerColor);
	lua_register(L, "_PLAYERX",luaGetPlayerX);
	lua_register(L, "_PLAYERY",luaGetPlayerY);
	lua_register(L, "_PLAYERZ",luaGetPlayerZ);
	lua_register(L, "_PLAYERARMS",luaGetPlayerArms);
	lua_register(L, "_PLAYERYFORCES",luaGetPlayerYForces);
	lua_register(L, "_PLAYERNAME",luaGetPlayerName);
	lua_register(L, "_FUEL",luaGetFuel);
	lua_register(L, "_FUELMAX",luaGetFuelMax);

	luaL3dx=luaL3dy=luaL3dz=0.0f;
	luaGraColor=0xffffff;
	//グローバル変数の登録
	for(int i=0;i<VarCount;i++) {
		lua_pushnumber(L,ValList[i].Val);
		lua_setglobal(L,ValList[i].Name);
	}
	//スクリプトをセットする
      luaopen_string(L);
      luaopen_base(L);
      luaopen_table(L);
      luaopen_math(L);
//      luaopen_io(L);
	int e=lua_dobuffer (L,buff,strlen(buff),szUpdateFileName0);
	if(e!=0) {
		ScriptErrorCode=-1;
		sprintf(ScriptErrorStr,"%s\n",lua_tostring(L,0));
		lua_close(L);
		return NULL;
	}
	return L;
}
Exemple #3
0
void LSLuaState::open()
{
    assert(!L);

    L = lua_newstate(lsLuaAlloc, NULL);
    //L = lua_open();
    toLuaState.insert(L, this);

    luaopen_base(L);
    luaopen_table(L);
    luaopen_string(L);
    luaopen_math(L);
    luaL_openlibs(L);

    // TODO: turn this back on when it doesn't fail on the testWhile unit test
    // update luajit and test again
    luaJIT_setmode(L, 0, LUAJIT_MODE_ENGINE | LUAJIT_MODE_OFF);

    // open the lua debug library
    luaopen_debug(L);

    // open socket library
    luaopen_socket_core(L);

    lua_newtable(L);
    lua_rawseti(L, LUA_GLOBALSINDEX, LSINDEXCLASSES);

    lua_newtable(L);
    lua_setglobal(L, "__ls_nativeclasses");

    lua_pushcfunction(L, traceback);
    lua_setglobal(L, "__ls_traceback");
    _tracemessage[0] = 0;

    // entry -> version
    lua_newtable(L);
    lua_rawseti(L, LUA_GLOBALSINDEX, LSINDEXMANAGEDVERSION);

    // entry -> native user data
    lua_newtable(L);
    lua_rawseti(L, LUA_GLOBALSINDEX, LSINDEXMANAGEDUSERDATA);

    // native user data -> script instance
    lua_newtable(L);
    lua_rawseti(L, LUA_GLOBALSINDEX, LSINDEXMANAGEDNATIVESCRIPT);

    // native delegate table
    lua_newtable(L);
    lua_rawseti(L, LUA_GLOBALSINDEX, LSINDEXNATIVEDELEGATES);

    // interned field name lookup
    lua_newtable(L);
    lua_rawseti(L, LUA_GLOBALSINDEX, LSINDEXMEMBERINFONAME);

    // typeid -> type*
    lua_newtable(L);
    lua_rawseti(L, LUA_GLOBALSINDEX, LSASSEMBLYLOOKUP);

    // lua/luacfunction -> MethodBase* lookups
    lua_newtable(L);

    // weak key metatable
    lua_newtable(L);
    lua_pushstring(L, "k");
    lua_setfield(L, -2, "__mode");
    lua_setmetatable(L, -2);

    lua_rawseti(L, LUA_GLOBALSINDEX, LSINDEXMETHODLOOKUP);

    lsr_instanceregister(L);

    NativeInterface::registerNativeTypes(L);
}
Exemple #4
0
int
weechat_lua_load (const char *filename)
{
    FILE *fp;
    char *weechat_lua_code = {
        "weechat_outputs = {\n"
        "    write = function (self, str)\n"
        "        weechat.print(\"\", \"lua: stdout/stderr: \" .. str)\n"
        "    end\n"
        "}\n"
        "io.stdout = weechat_outputs\n"
        "io.stderr = weechat_outputs\n"
    };

    if ((fp = fopen (filename, "r")) == NULL)
    {
        weechat_printf (NULL,
                        weechat_gettext ("%s%s: script \"%s\" not found"),
                        weechat_prefix ("error"), LUA_PLUGIN_NAME, filename);
        return 0;
    }

    if ((weechat_lua_plugin->debug >= 2) || !lua_quiet)
    {
        weechat_printf (NULL,
                        weechat_gettext ("%s: loading script \"%s\""),
                        LUA_PLUGIN_NAME, filename);
    }

    lua_current_script = NULL;
    lua_registered_script = NULL;

    lua_current_interpreter = luaL_newstate();

    if (lua_current_interpreter == NULL)
    {
        weechat_printf (NULL,
                        weechat_gettext ("%s%s: unable to create new "
                                         "sub-interpreter"),
                        weechat_prefix ("error"), LUA_PLUGIN_NAME);
        fclose (fp);
        return 0;
    }

#ifdef LUA_VERSION_NUM /* LUA_VERSION_NUM is defined only in lua >= 5.1.0 */
    luaL_openlibs (lua_current_interpreter);
#else
    luaopen_base (lua_current_interpreter);
    luaopen_string (lua_current_interpreter);
    luaopen_table (lua_current_interpreter);
    luaopen_math (lua_current_interpreter);
    luaopen_io (lua_current_interpreter);
    luaopen_debug (lua_current_interpreter);
#endif /* LUA_VERSION_NUM */

    weechat_lua_register_lib (lua_current_interpreter, "weechat",
                              weechat_lua_api_funcs,
                              weechat_lua_api_consts);

#ifdef LUA_VERSION_NUM
    if (luaL_dostring (lua_current_interpreter, weechat_lua_code) != 0)
#else
    if (lua_dostring (lua_current_interpreter, weechat_lua_code) != 0)
#endif /* LUA_VERSION_NUM */
    {
        weechat_printf (NULL,
                        weechat_gettext ("%s%s: unable to redirect stdout "
                                         "and stderr"),
                        weechat_prefix ("error"), LUA_PLUGIN_NAME);
    }

    lua_current_script_filename = filename;

    if (luaL_loadfile (lua_current_interpreter, filename) != 0)
    {
        weechat_printf (NULL,
                        weechat_gettext ("%s%s: unable to load file \"%s\""),
                        weechat_prefix ("error"), LUA_PLUGIN_NAME, filename);
        weechat_printf (NULL,
                        weechat_gettext ("%s%s: error: %s"),
                        weechat_prefix ("error"), LUA_PLUGIN_NAME,
                        lua_tostring (lua_current_interpreter, -1));
        lua_close (lua_current_interpreter);
        fclose (fp);
        return 0;
    }

    if (lua_pcall (lua_current_interpreter, 0, 0, 0) != 0)
    {
        weechat_printf (NULL,
                        weechat_gettext ("%s%s: unable to execute file "
                                         "\"%s\""),
                        weechat_prefix ("error"), LUA_PLUGIN_NAME, filename);
        weechat_printf (NULL,
                        weechat_gettext ("%s%s: error: %s"),
                        weechat_prefix ("error"), LUA_PLUGIN_NAME,
                        lua_tostring (lua_current_interpreter, -1));
        lua_close (lua_current_interpreter);
        fclose (fp);

        /* if script was registered, remove it from list */
        if (lua_current_script)
        {
            plugin_script_remove (weechat_lua_plugin,
                                  &lua_scripts, &last_lua_script,
                                  lua_current_script);
            lua_current_script = NULL;
        }

        return 0;
    }
    fclose (fp);

    if (!lua_registered_script)
    {
        weechat_printf (NULL,
                        weechat_gettext ("%s%s: function \"register\" not "
                                         "found (or failed) in file \"%s\""),
                        weechat_prefix ("error"), LUA_PLUGIN_NAME, filename);
        lua_close (lua_current_interpreter);
        return 0;
    }
    lua_current_script = lua_registered_script;

    /*
     * set input/close callbacks for buffers created by this script
     * (to restore callbacks after upgrade)
     */
    plugin_script_set_buffer_callbacks (weechat_lua_plugin,
                                        lua_scripts,
                                        lua_current_script,
                                        &weechat_lua_api_buffer_input_data_cb,
                                        &weechat_lua_api_buffer_close_cb);

    (void) weechat_hook_signal_send ("lua_script_loaded",
                                     WEECHAT_HOOK_SIGNAL_STRING,
                                     lua_current_script->filename);

    return 1;
}
Exemple #5
0
bool LuaWrapper::openMath()			{ luaopen_math(m_luaState);			return true; }