예제 #1
0
        /// <summary>
        /// Push a debug.traceback reference onto the stack, for a pcall function to use as error handler. (Remember to increment any top-of-stack markers!)
        /// </summary>
        private static int PushDebugTraceback(LuaState luaState, int argCount)
        {
            luaState.GetGlobal("debug");
            luaState.GetField(-1, "traceback");
            luaState.Remove(-2);
            int errindex = -argCount - 2;

            luaState.Insert(errindex);
            return(errindex);
        }
예제 #2
0
        void Init()
        {
            _luaState.PushString("NLua_Loaded");
            _luaState.PushBoolean(true);
            _luaState.SetTable((int)LuaRegistry.Index);//registry.NLua_loaded=true
            //Trace.WriteLine("init luastate");
            if (_StatePassed == false)
            {
                _luaState.NewTable();
                _luaState.SetGlobal("luanet");
            }

            _luaState.GetGlobal("luanet");
            _luaState.PushString("getmetatable");
            _luaState.GetGlobal("getmetatable");
            _luaState.SetTable(-3); //luanet.getmetatable=getmetatable, left luanet on top of stack
            //_luaState.PopGlobalTable();//REG[2]=luanet ??
            _luaState.Pop(1);

            //_luaState.PopGlobalTable();
            _translator = new ObjectTranslator(this, _luaState);
            ObjectTranslatorPool.Instance.Add(_luaState, _translator);
            _luaState.DoString(InitLuanet);
            string cfg = ".\\nlua.config";

            if (File.Exists(cfg))
            {
                //Console.WriteLine("load config:" + Environment.CurrentDirectory);
                string cfgscript = File.ReadAllText(cfg);
                if (!_luaState.DoString(cfgscript))
                {
                    Trace.WriteLine("Error executing " + cfg);
                }
            }
        }
예제 #3
0
        private void Init()
        {
            _luaState.PushString("NLua_Loaded");
            _luaState.PushBoolean(true);
            _luaState.SetTable((int)LuaRegistry.Index);
            if (_StatePassed == false)
            {
                _luaState.NewTable();
                _luaState.SetGlobal("luanet");
            }
            _luaState.PushGlobalTable();
            _luaState.GetGlobal("luanet");
            _luaState.PushString("getmetatable");
            _luaState.GetGlobal("getmetatable");
            _luaState.SetTable(-3);
            _luaState.PopGlobalTable();
            _translator = new ObjectTranslator(this, _luaState);

            ObjectTranslatorPool.Instance.Add(_luaState, _translator);

            _luaState.PopGlobalTable();
            _luaState.DoString(InitLuanet);
        }
예제 #4
0
 /*
  * Registers the global functions used by NLua
  */
 private void SetGlobalFunctions(LuaState luaState)
 {
     luaState.GetGlobal("luanet");
     luaState.PushCFunction(MetaFunctions.IndexFunction);
     luaState.SetField(-2, "get_object_member");
     luaState.PushCFunction(_importTypeFunction);
     luaState.SetField(-2, "import_type");
     luaState.PushCFunction(_loadAssemblyFunction);
     luaState.SetField(-2, "load_assembly");
     luaState.PushCFunction(_registerTableFunction);
     luaState.SetField(-2, "make_object");
     luaState.PushCFunction(_unregisterTableFunction);
     luaState.SetField(-2, "free_object");
     luaState.PushCFunction(_getMethodSigFunction);
     luaState.SetField(-2, "get_method_bysig");
     luaState.PushCFunction(_getConstructorSigFunction);
     luaState.SetField(-2, "get_constructor_bysig");
     luaState.PushCFunction(_ctypeFunction);
     luaState.SetField(-2, "ctype");
     luaState.PushCFunction(_enumFromIntFunction);
     luaState.SetField(-2, "enum");
 }
예제 #5
0
 public static void PopGlobalTable(this LuaState luaState)
 {
     if (!luaState.IsTable(-1))
     {
         throw new Exception("expect table - " + luaState.Type(-1).ToString());
     }
     //REG[2]=value at top?
     //luaState.RawSetInteger(LuaRegistry.Index, (long) LuaRegistryIndex.Globals);
     luaState.GetGlobal("setfenv");
     if (!luaState.IsCFunction(-1))
     {
         throw new Exception("expect setfenv");
     }
     luaState.PushInteger(1);
     luaState.PushCopy(-3);
     try
     {
         luaState.Call(2, 0);
     }catch (Exception e)
     {
         Debug.WriteLine(e.Message);
     }
 }