XLuaフレームワーク構築——LuaFunction支店クラス拡張

3979 ワード

Luaにおける関数のc#へのマッピングはLuaFunctionであり、関数への呼び出しは一般にcallで呼び出されるが、この効率には一定のGCがあると公式に考えられているため、ActionとFuncが提供され、ここでActionは呼び出しであり、戻り値はなく、Funcは戻り値がある.
実際の使用時に1つの関数が少なくなったことを発見しました.つまり、私が呼び出したのは無参関数ですが、戻り値を手に入れたいと思っています.公式のコードには関連関数はありませんが、コードを読むと、これは分部類で、公式も私たち自身が関連関数数を拡張することを奨励しています.公式のコードを見て、ひょうたんを描いて、関連する機能を増やすのは難しくありません.注記LuaAPIの使用でOKです.次に、実装された戻り値を参照しない関数を示します.
/*
 * Tencent is pleased to support the open source community by making xLua available.
 * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
 * Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
 * http://opensource.org/licenses/MIT
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/

#if USE_UNI_LUA
using LuaAPI = UniLua.Lua;
using RealStatePtr = UniLua.ILuaState;
using LuaCSFunction = UniLua.CSharpFunctionDelegate;
#else
using LuaAPI = XLua.LuaDLL.Lua;
using RealStatePtr = System.IntPtr;
using LuaCSFunction = XLua.LuaDLL.lua_CSFunction;
#endif

using System;
using System.Collections.Generic;

namespace XLua
{
    public partial class LuaFunction : LuaBase
    {

        public TResult Func()
        {
#if THREAD_SAFE || HOTFIX_ENABLE
            lock (luaEnv.luaEnvLock)
            {
#endif
            var L = luaEnv.L;
            var translator = luaEnv.translator;
            int oldTop = LuaAPI.lua_gettop(L);
            int errFunc = LuaAPI.load_error_func(L, luaEnv.errorFuncRef);
            LuaAPI.lua_getref(L, luaReference);
            int error = LuaAPI.lua_pcall(L, 0, 1, errFunc);
            if (error != 0)
                luaEnv.ThrowExceptionFromError(oldTop);
            TResult ret;
            try
            {
                translator.Get(L, -1, out ret);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                LuaAPI.lua_settop(L, oldTop);
            }
            return ret;
#if THREAD_SAFE || HOTFIX_ENABLE
            }
#endif
        }

    }

}


QQ交流群:517539056、みんながいっしょに交流することに参加することを歓迎します