Example #1
0
 public CheckType(ObjectTranslator translator)
 {
     this.translator = translator;
     extractValues.Add(GetExtractDictionaryKey(typeof(object)), new ExtractValue(GetAsObject));
     extractValues.Add(GetExtractDictionaryKey(typeof(sbyte)), new ExtractValue(GetAsSbyte));
     extractValues.Add(GetExtractDictionaryKey(typeof(byte)), new ExtractValue(GetAsByte));
     extractValues.Add(GetExtractDictionaryKey(typeof(short)), new ExtractValue(GetAsShort));
     extractValues.Add(GetExtractDictionaryKey(typeof(ushort)), new ExtractValue(GetAsUshort));
     extractValues.Add(GetExtractDictionaryKey(typeof(int)), new ExtractValue(GetAsInt));
     extractValues.Add(GetExtractDictionaryKey(typeof(uint)), new ExtractValue(GetAsUint));
     extractValues.Add(GetExtractDictionaryKey(typeof(long)), new ExtractValue(GetAsLong));
     extractValues.Add(GetExtractDictionaryKey(typeof(ulong)), new ExtractValue(GetAsUlong));
     extractValues.Add(GetExtractDictionaryKey(typeof(double)), new ExtractValue(GetAsDouble));
     extractValues.Add(GetExtractDictionaryKey(typeof(char)), new ExtractValue(GetAsChar));
     extractValues.Add(GetExtractDictionaryKey(typeof(float)), new ExtractValue(GetAsFloat));
     extractValues.Add(GetExtractDictionaryKey(typeof(decimal)), new ExtractValue(GetAsDecimal));
     extractValues.Add(GetExtractDictionaryKey(typeof(bool)), new ExtractValue(GetAsBoolean));
     extractValues.Add(GetExtractDictionaryKey(typeof(string)), new ExtractValue(GetAsString));
     extractValues.Add(GetExtractDictionaryKey(typeof(char[])), new ExtractValue (GetAsCharArray));
     extractValues.Add(GetExtractDictionaryKey(typeof(LuaFunction)), new ExtractValue(GetAsFunction));
     extractValues.Add(GetExtractDictionaryKey(typeof(LuaTable)), new ExtractValue(GetAsTable));
     extractValues.Add(GetExtractDictionaryKey(typeof(Dictionary<object,object>)), new ExtractValue(GetAsDictionary));
     extractValues.Add(GetExtractDictionaryKey(typeof(LuaUserData)), new ExtractValue(GetAsUserdata));
     extractNetObject = new ExtractValue (GetAsNetObject);
 }
Example #2
0
		public MetaFunctions (ObjectTranslator translator)
		{
			this.translator = translator;
			gcFunction = new LuaCore.lua_CFunction (MetaFunctions.collectObject);
			toStringFunction = new LuaCore.lua_CFunction (MetaFunctions.toString);
			indexFunction = new LuaCore.lua_CFunction (MetaFunctions.getMethod);
			newindexFunction = new LuaCore.lua_CFunction (MetaFunctions.setFieldOrProperty);
			baseIndexFunction = new LuaCore.lua_CFunction (MetaFunctions.getBaseMethod);
			callConstructorFunction = new LuaCore.lua_CFunction (MetaFunctions.callConstructor);
			classIndexFunction = new LuaCore.lua_CFunction (MetaFunctions.getClassMethod);
			classNewindexFunction = new LuaCore.lua_CFunction (MetaFunctions.setClassFieldOrProperty);
			execDelegateFunction = new LuaCore.lua_CFunction (MetaFunctions.runFunctionDelegate);
		}
Example #3
0
		public MetaFunctions (ObjectTranslator translator)
		{
			this.translator = translator;
			gcFunction = new LuaNativeFunction (MetaFunctions.CollectObject);
			toStringFunction = new LuaNativeFunction (MetaFunctions.ToStringLua);
			indexFunction = new LuaNativeFunction (MetaFunctions.GetMethod);
			newindexFunction = new LuaNativeFunction (MetaFunctions.SetFieldOrProperty);
			baseIndexFunction = new LuaNativeFunction (MetaFunctions.GetBaseMethod);
			callConstructorFunction = new LuaNativeFunction (MetaFunctions.CallConstructor);
			classIndexFunction = new LuaNativeFunction (MetaFunctions.GetClassMethod);
			classNewindexFunction = new LuaNativeFunction (MetaFunctions.SetClassFieldOrProperty);
			execDelegateFunction = new LuaNativeFunction (MetaFunctions.RunFunctionDelegate);
		}
Example #4
0
 public CheckType(ObjectTranslator translator)
 {
     this.translator = translator;
     extractValues.Add (typeof(object).TypeHandle.Value.ToInt64 (), new ExtractValue (getAsObject));
     extractValues.Add (typeof(sbyte).TypeHandle.Value.ToInt64 (), new ExtractValue (getAsSbyte));
     extractValues.Add (typeof(byte).TypeHandle.Value.ToInt64 (), new ExtractValue (getAsByte));
     extractValues.Add (typeof(short).TypeHandle.Value.ToInt64 (), new ExtractValue (getAsShort));
     extractValues.Add (typeof(ushort).TypeHandle.Value.ToInt64 (), new ExtractValue (getAsUshort));
     extractValues.Add (typeof(int).TypeHandle.Value.ToInt64 (), new ExtractValue (getAsInt));
     extractValues.Add (typeof(uint).TypeHandle.Value.ToInt64 (), new ExtractValue (getAsUint));
     extractValues.Add (typeof(long).TypeHandle.Value.ToInt64 (), new ExtractValue (getAsLong));
     extractValues.Add (typeof(ulong).TypeHandle.Value.ToInt64 (), new ExtractValue (getAsUlong));
     extractValues.Add (typeof(double).TypeHandle.Value.ToInt64 (), new ExtractValue (getAsDouble));
     extractValues.Add (typeof(char).TypeHandle.Value.ToInt64 (), new ExtractValue (getAsChar));
     extractValues.Add (typeof(float).TypeHandle.Value.ToInt64 (), new ExtractValue (getAsFloat));
     extractValues.Add (typeof(decimal).TypeHandle.Value.ToInt64 (), new ExtractValue (getAsDecimal));
     extractValues.Add (typeof(bool).TypeHandle.Value.ToInt64 (), new ExtractValue (getAsBoolean));
     extractValues.Add (typeof(string).TypeHandle.Value.ToInt64 (), new ExtractValue (getAsString));
     extractValues.Add (typeof(LuaFunction).TypeHandle.Value.ToInt64 (), new ExtractValue (getAsFunction));
     extractValues.Add (typeof(LuaTable).TypeHandle.Value.ToInt64 (), new ExtractValue (getAsTable));
     extractValues.Add (typeof(LuaUserData).TypeHandle.Value.ToInt64 (), new ExtractValue (getAsUserdata));
     extractNetObject = new ExtractValue (getAsNetObject);
 }
Example #5
0
File: Lua.cs Project: zwwl0801/NLua
 void Init()
 {
     LuaLib.LuaPushString(luaState, "LUAINTERFACE LOADED");
     LuaLib.LuaPushBoolean(luaState, true);
     LuaLib.LuaSetTable(luaState, (int)LuaIndexes.Registry);
     if (_StatePassed == false)
     {
         LuaLib.LuaNewTable(luaState);
         LuaLib.LuaSetGlobal(luaState, "luanet");
     }
     LuaLib.LuaNetPushGlobalTable(luaState);
     LuaLib.LuaGetGlobal(luaState, "luanet");
     LuaLib.LuaPushString(luaState, "getmetatable");
     LuaLib.LuaGetGlobal(luaState, "getmetatable");
     LuaLib.LuaSetTable(luaState, -3);
     LuaLib.LuaNetPopGlobalTable(luaState);
     translator = new ObjectTranslator(this, luaState);
     lock (translatorPoolLock)
     {
         ObjectTranslatorPool.Instance.Add(luaState, translator);
     }
     LuaLib.LuaNetPopGlobalTable(luaState);
     LuaLib.LuaLDoString(luaState, Lua.initLuanet);
 }
Example #6
0
		private static int CollectObject (LuaState luaState, ObjectTranslator translator)
		{
			int udata = LuaLib.LuaNetRawNetObj (luaState, 1);

			if (udata != -1)
				translator.CollectObject (udata);

			return 0;
		}
Example #7
0
		private static int ToStringLua (LuaState luaState, ObjectTranslator translator)
		{
			object obj = translator.GetRawNetObject (luaState, 1);

			if (obj != null)
				translator.Push (luaState, obj.ToString () + ": " + obj.GetHashCode ().ToString());
			else
				LuaLib.LuaPushNil (luaState);

			return 1;
		}
Example #8
0
		static int MatchOperator (LuaState luaState, string operation, ObjectTranslator translator)
		{
			var validOperator = new MethodCache ();

			object target = GetTargetObject (luaState, operation, translator);

			if (target == null) {
				translator.ThrowError (luaState, "Cannot call " + operation + " on a nil object");
				LuaLib.LuaPushNil (luaState);
				return 1;
			}

			Type type = target.GetType ();
			var operators = type.GetMethods (operation, BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static);

			foreach (var op in operators) {
				bool isOk = translator.MatchParameters (luaState, op, ref validOperator);

				if (!isOk)
					continue;

				object result;
				if (op.IsStatic)
					result = op.Invoke (null, validOperator.args);
				else
					result = op.Invoke (target, validOperator.args);
				translator.Push (luaState, result);
				return 1;
			}

			translator.ThrowError (luaState, "Cannot call (" + operation + ") on object type " + type.Name);
			LuaLib.LuaPushNil (luaState);
			return 1;
		}
Example #9
0
		private static int RunFunctionDelegate (LuaState luaState, ObjectTranslator translator)
		{
			LuaNativeFunction func = (LuaNativeFunction)translator.GetRawNetObject (luaState, 1);
			LuaLib.LuaRemove (luaState, 1);
			return func (luaState);
		}
Example #10
0
File: Lua.cs Project: Azhei/NLua
		public Lua ()
		{
			luaState = LuaLib.luaL_newstate ();	// steffenj: Lua 5.1.1 API change (lua_open is gone)
			LuaLib.luaL_openlibs (luaState);		// steffenj: Lua 5.1.1 API change (luaopen_base is gone, just open all libs right here)
			LuaLib.lua_pushstring (luaState, "LUAINTERFACE LOADED");
			LuaLib.lua_pushboolean (luaState, true);
			LuaLib.lua_settable (luaState, (int)LuaIndexes.Registry);
			LuaLib.lua_newtable (luaState);
			LuaLib.lua_setglobal (luaState, "luanet");
			LuaLib.lua_pushvalue (luaState, (int)LuaIndexes.Globals);
			LuaLib.lua_getglobal (luaState, "luanet");
			LuaLib.lua_pushstring (luaState, "getmetatable");
			LuaLib.lua_getglobal (luaState, "getmetatable");
			LuaLib.lua_settable (luaState, -3);
			LuaLib.lua_replace (luaState, (int)LuaIndexes.Globals);
			translator = new ObjectTranslator (this, luaState);
			ObjectTranslatorPool.Instance.Add (luaState, translator);
			LuaLib.lua_replace (luaState, (int)LuaIndexes.Globals);
			LuaLib.luaL_dostring (luaState, Lua.init_luanet);	// steffenj: lua_dostring renamed to luaL_dostring

			// We need to keep this in a managed reference so the delegate doesn't get garbage collected
			panicCallback = new LuaCore.lua_CFunction (PanicCallback);
			LuaLib.lua_atpanic (luaState, panicCallback);
		}
Example #11
0
		/// <summary>
		/// Debug tool to dump the lua stack
		/// </summary>
		/// FIXME, move somewhere else
		public static void dumpStack (ObjectTranslator translator, LuaCore.lua_State luaState)
		{
			int depth = LuaLib.lua_gettop (luaState);
			Debug.WriteLine ("lua stack depth: " + depth);

			for (int i = 1; i <= depth; i++) {
				var type = LuaLib.lua_type (luaState, i);
				// we dump stacks when deep in calls, calling typename while the stack is in flux can fail sometimes, so manually check for key types
				string typestr = (type == LuaTypes.Table) ? "table" : LuaLib.lua_typename (luaState, type);
				string strrep = LuaLib.lua_tostring (luaState, i).ToString ();

				if (type == LuaTypes.UserData) {
					object obj = translator.getRawNetObject (luaState, i);
					strrep = obj.ToString ();
				}

#if WINDOWS_PHONE
                Debug.WriteLine("{0}: ({1}) {2}", i, typestr, strrep);
#elif !SILVERLIGHT
				Debug.Print ("{0}: ({1}) {2}", i, typestr, strrep);
#endif
			}
		}
Example #12
0
 public ClassGenerator(ObjectTranslator objTranslator, Type typeClass)
 {
     translator = objTranslator;
     klass = typeClass;
 }
Example #13
0
		public MetaFunctions (ObjectTranslator translator)
		{
			this.translator = translator;
			GcFunction = new LuaNativeFunction (MetaFunctions.CollectObject);
			ToStringFunction = new LuaNativeFunction (MetaFunctions.ToStringLua);
			IndexFunction = new LuaNativeFunction (MetaFunctions.GetMethod);
			NewIndexFunction = new LuaNativeFunction (MetaFunctions.SetFieldOrProperty);
			BaseIndexFunction = new LuaNativeFunction (MetaFunctions.GetBaseMethod);
			CallConstructorFunction = new LuaNativeFunction (MetaFunctions.CallConstructor);
			ClassIndexFunction = new LuaNativeFunction (MetaFunctions.GetClassMethod);
			ClassNewindexFunction = new LuaNativeFunction (MetaFunctions.SetClassFieldOrProperty);
			ExecuteDelegateFunction = new LuaNativeFunction (MetaFunctions.RunFunctionDelegate);
			AddFunction = new LuaNativeFunction (MetaFunctions.AddLua);
			SubtractFunction = new LuaNativeFunction (MetaFunctions.SubtractLua);
			MultiplyFunction = new LuaNativeFunction (MetaFunctions.MultiplyLua);
			DivisionFunction = new LuaNativeFunction (MetaFunctions.DivideLua);
			ModulosFunction = new LuaNativeFunction (MetaFunctions.ModLua);
			UnaryNegationFunction = new LuaNativeFunction (MetaFunctions.UnaryNegationLua);
			EqualFunction = new LuaNativeFunction (MetaFunctions.EqualLua);
			LessThanFunction = new LuaNativeFunction (MetaFunctions.LessThanLua);
			LessThanOrEqualFunction = new LuaNativeFunction (MetaFunctions.LessThanOrEqualLua);
		}
		public void Add (LuaState luaState, ObjectTranslator translator)
		{
			translators.Add(luaState , translator);			
		}
 public DelegateGenerator(ObjectTranslator objectTranslator, Type type)
 {
     translator = objectTranslator;
     delegateType = type;
 }
Example #16
0
 public ClassGenerator(ObjectTranslator translator, Type klass)
 {
     this.translator = translator;
     this.klass = klass;
 }
Example #17
0
		private static int runFunctionDelegate (LuaCore.lua_State luaState, ObjectTranslator translator)
		{
			LuaCore.lua_CFunction func = (LuaCore.lua_CFunction)translator.getRawNetObject (luaState, 1);
			LuaLib.lua_remove (luaState, 1);
			return func (luaState);
		}
Example #18
0
		private static int collectObject (LuaCore.lua_State luaState, ObjectTranslator translator)
		{
			int udata = LuaLib.luanet_rawnetobj (luaState, 1);

			if (udata != -1)
				translator.collectObject (udata);

			return 0;
		}
Example #19
0
		static int UnaryNegationLua (LuaState luaState, ObjectTranslator translator)
		{
			object obj1 = translator.GetRawNetObject (luaState, 1);

			if (obj1 == null) {
				translator.ThrowError (luaState, "Cannot negate a nil object");
				LuaLib.LuaPushNil (luaState);
				return 1;
			}

			Type type = obj1.GetType ();
			MethodInfo opUnaryNegation = type.GetMethod ("op_UnaryNegation");

			if (opUnaryNegation == null) {
				translator.ThrowError (luaState, "Cannot negate object (" + type.Name + " does not overload the operator -)");
				LuaLib.LuaPushNil (luaState);
				return 1;
			}
			obj1 = opUnaryNegation.Invoke (obj1, new object [] { obj1 });
			translator.Push (luaState, obj1);
			return 1;
		}
Example #20
0
		public virtual void Dispose ()
		{
			if (translator != null) {
				translator.pendingEvents.Dispose ();
				translator = null;
			}

			Close ();
			GC.WaitForPendingFinalizers ();
		}
Example #21
0
		/// <summary>
		/// Debug tool to dump the lua stack
		/// </summary>
		/// FIXME, move somewhere else
		public static void DumpStack (ObjectTranslator translator, LuaState luaState)
		{
			int depth = LuaLib.LuaGetTop (luaState);

#if WINDOWS_PHONE
			Debug.WriteLine("lua stack depth: {0}", depth);
#elif UNITY_3D
			UnityEngine.Debug.Log(string.Format("lua stack depth: {0}", depth));
#elif !SILVERLIGHT
			Debug.Print ("lua stack depth: {0}", depth);
#endif

			for (int i = 1; i <= depth; i++) {
				var type = LuaLib.LuaType (luaState, i);
				// we dump stacks when deep in calls, calling typename while the stack is in flux can fail sometimes, so manually check for key types
				string typestr = (type == LuaTypes.Table) ? "table" : LuaLib.LuaTypeName (luaState, type);
				string strrep = LuaLib.LuaToString (luaState, i).ToString ();

				if (type == LuaTypes.UserData) {
					object obj = translator.GetRawNetObject (luaState, i);
					strrep = obj.ToString ();
				}

#if WINDOWS_PHONE
                Debug.WriteLine("{0}: ({1}) {2}", i, typestr, strrep);
#elif UNITY_3D
				UnityEngine.Debug.Log(string.Format("{0}: ({1}) {2}", i, typestr, strrep));
#elif !SILVERLIGHT
				Debug.Print ("{0}: ({1}) {2}", i, typestr, strrep);
#endif
			}
		}
Example #22
0
		static int AddLua (LuaState luaState, ObjectTranslator translator)
		{
			object obj1 = translator.GetRawNetObject (luaState, 1);
			object obj2 = translator.GetRawNetObject (luaState, 2);

			if (obj1 == null || obj2 == null) {
				translator.ThrowError (luaState, "Cannot add a nil object");
				LuaLib.LuaPushNil (luaState);
				return 1;
			}

			Type type = obj1.GetType ();
			MethodInfo opAddition = type.GetMethod ("op_Addition");

			if (opAddition == null) {
				translator.ThrowError (luaState, "Cannot add object (" + type.Name+ " does not overload the operator +)");
				LuaLib.LuaPushNil (luaState);
				return 1;
			}
			obj1 = opAddition.Invoke (obj1, new object[] { obj1, obj2 });
			translator.Push (luaState, obj1);
			return 1;
		}
Example #23
0
File: Lua.cs Project: Azhei/NLua
		public virtual void Dispose ()
		{
			if (!translator.IsNull ()) {
				translator.pendingEvents.Dispose ();
				translator = null;
			}

			this.Close ();
			GC.Collect ();
			GC.WaitForPendingFinalizers ();
		}
Example #24
0
		static object GetTargetObject (LuaState luaState, string operation, ObjectTranslator translator)
		{
			Type t;
			object target = translator.GetRawNetObject (luaState, 1);
			if (target != null) {
				t = target.GetType ();
				if (t.HasMethod (operation))
					return target;
			}
			target = translator.GetRawNetObject (luaState, 2);
			if (target != null) {
				t = target.GetType ();
				if (t.HasMethod (operation))
					return target;
			}
			return null;
		}
Example #25
0
File: Lua.cs Project: Azhei/NLua
		/*
			* CAUTION: NLua.Lua instances can't share the same lua state! 
			*/
		public Lua (LuaCore.lua_State lState)
		{
			LuaLib.lua_pushstring (lState, "LUAINTERFACE LOADED");
			LuaLib.lua_gettable (lState, (int)LuaIndexes.Registry);
			
			if (LuaLib.lua_toboolean (lState, -1)) {
				LuaLib.lua_settop (lState, -2);
				throw new LuaException ("There is already a NLua.Lua instance associated with this Lua state");
			} else {
				LuaLib.lua_settop (lState, -2);
				LuaLib.lua_pushstring (lState, "LUAINTERFACE LOADED");
				LuaLib.lua_pushboolean (lState, true);
				LuaLib.lua_settable (lState, (int)LuaIndexes.Registry);
				luaState = lState;
				LuaLib.lua_pushvalue (lState, (int)LuaIndexes.Globals);
				LuaLib.lua_getglobal (lState, "luanet");
				LuaLib.lua_pushstring (lState, "getmetatable");
				LuaLib.lua_getglobal (lState, "getmetatable");
				LuaLib.lua_settable (lState, -3);
				LuaLib.lua_replace (lState, (int)LuaIndexes.Globals);
				translator = new ObjectTranslator (this, luaState);
				ObjectTranslatorPool.Instance.Add (luaState, translator);
				LuaLib.lua_replace (lState, (int)LuaIndexes.Globals);
				LuaLib.luaL_dostring (lState, Lua.init_luanet);	// steffenj: lua_dostring renamed to luaL_dostring
			}
				
			_StatePassed = true;
		}
Example #26
0
		private static int toString (LuaCore.lua_State luaState, ObjectTranslator translator)
		{
			object obj = translator.getRawNetObject (luaState, 1);

			if (!obj.IsNull ())
				translator.push (luaState, obj.ToString () + ": " + obj.GetHashCode ());
			else
				LuaLib.lua_pushnil (luaState);

			return 1;
		}
Example #27
0
		void Init ()
		{
			LuaLib.LuaPushString (luaState, "LUAINTERFACE LOADED");
			LuaLib.LuaPushBoolean (luaState, true);
			LuaLib.LuaSetTable (luaState, (int)LuaIndexes.Registry);
			if (_StatePassed == false) {
				LuaLib.LuaNewTable (luaState);
				LuaLib.LuaSetGlobal (luaState, "luanet");
			}
			LuaLib.LuaNetPushGlobalTable (luaState);
			LuaLib.LuaGetGlobal (luaState, "luanet");
			LuaLib.LuaPushString (luaState, "getmetatable");
			LuaLib.LuaGetGlobal (luaState, "getmetatable");
			LuaLib.LuaSetTable (luaState, -3);
			LuaLib.LuaNetPopGlobalTable (luaState);
			translator = new ObjectTranslator (this, luaState);
			ObjectTranslatorPool.Instance.Add (luaState, translator);
			LuaLib.LuaNetPopGlobalTable (luaState);
			LuaLib.LuaLDoString (luaState, Lua.initLuanet);
		}
Example #28
0
 public DelegateGenerator(ObjectTranslator translator, Type delegateType)
 {
     this.translator = translator;
     this.delegateType = delegateType;
 }