示例#1
0
文件: main.cpp 项目: wgq168668/misc
void test_varlist()
{
	VarList args;	
	args.add(1);
	args.add("ddddd");
	args.add(true);
	printf("args count: %d\n", args.count());
	printf("args 0: %d\n", args.get(0).toInt());
	printf("args 1: %s\n", args.get(1).toString());
	printf("args 2: %s\n", args.get(2).toBool() ? "true" : "false");
}
示例#2
0
void ScriptEngine::callEvent( const char *name, const VarList &args, VarList &result )
{
    FUNCTION_MAP::iterator it = m_functionMap.find(name);
    if (it != m_functionMap.end())
    {
        LUA_FUNCTION nHandler = it->second;

        LuaHelper::getRefFunction(m_luaState, nHandler);
        if (lua_isfunction(m_luaState, -1))
        {
            size_t count = args.count();
            for (size_t i=0; i<count; ++i)
            {
                int type = args.get(i).getType();
                switch (type)
                {
                case Var::BOOL:
                    lua_pushboolean(m_luaState, args.get(i).toBool() ? 1 : 0);
                    break;
                case Var::BYTE:
                case Var::SHORT:
                case Var::INT:
                case Var::INT64:
                case Var::FLOAT:
                case Var::NUMBER:
                    lua_pushnumber(m_luaState, args.get(i).toNumber());
                    break;
                case Var::STRING:
                    lua_pushstring(m_luaState, args.get(i).toString());
                    break;
                default:
                    break;
                }
            }
            int error = lua_pcall(m_luaState, args.count(), 1, NULL);
            if (error)
            {
                printf("[LUA ERROR] %s\n", lua_tostring(m_luaState, - 1));
                lua_pop(m_luaState, 1); // remove error message from stack
            }
            else
            {
                // get return value
                if (lua_isnumber(m_luaState, -1))
                {
                    int ret = lua_tointeger(m_luaState, -1);
                    result.add(ret);
                }
                else if (lua_isboolean(m_luaState, -1))
                {
                    int ret = lua_toboolean(m_luaState, -1);
                    result.add(ret);
                }
                else if (lua_isstring(m_luaState, -1))
                {
                    const char *ret = lua_tostring(m_luaState, -1);
                    result.add(ret);
                }

                // remove return value from stack
                lua_pop(m_luaState, 1);
            }
        }

    }
}