public static int luaL_dostring(LuaCore.lua_State luaState, string chunk) { int result = luaL_loadstring(luaState, chunk); if (result != 0) { return(result); } return(lua_pcall(luaState, 0, -1, 0)); }
/* * Creates the metatable for delegates */ private void createFunctionMetatable(LuaCore.lua_State luaState) { LuaLib.luaL_newmetatable(luaState, "luaNet_function"); LuaLib.lua_pushstring(luaState, "__gc"); LuaLib.lua_pushstdcallcfunction(luaState, metaFunctions.gcFunction); LuaLib.lua_settable(luaState, -3); LuaLib.lua_pushstring(luaState, "__call"); LuaLib.lua_pushstdcallcfunction(luaState, metaFunctions.execDelegateFunction); LuaLib.lua_settable(luaState, -3); LuaLib.lua_settop(luaState, -2); }
private object getAsChar(LuaCore.lua_State luaState, int stackPos) { char retVal = (char)LuaLib.lua_tonumber(luaState, stackPos); if (retVal == 0 && !LuaLib.lua_isnumber(luaState, stackPos)) { return(null); } return(retVal); }
private object getAsUlong(LuaCore.lua_State luaState, int stackPos) { ulong retVal = (ulong)LuaLib.lua_tonumber(luaState, stackPos); if (retVal == 0 && !LuaLib.lua_isnumber(luaState, stackPos)) { return(null); } return(retVal); }
// steffenj: END Lua 5.1.1 API change (lua_newtable is gone, lua_createtable is new) // steffenj: BEGIN Lua 5.1.1 API change (lua_dofile now in LuaLib as luaL_dofile macro) public static int luaL_dofile(LuaCore.lua_State luaState, string fileName) { int result = LuaCore.luaL_loadfile(luaState, fileName); if (result != 0) { return(result); } return(LuaCore.lua_pcall(luaState, 0, -1, 0)); }
private object getAsByte(LuaCore.lua_State luaState, int stackPos) { byte retVal = (byte)LuaLib.lua_tonumber(luaState, stackPos); if (retVal == 0 && !LuaLib.lua_isnumber(luaState, stackPos)) { return(null); } return(retVal); }
private object getAsString(LuaCore.lua_State luaState, int stackPos) { string retVal = LuaLib.lua_tostring(luaState, stackPos).ToString(); if (retVal == string.Empty && !LuaLib.lua_isstring(luaState, stackPos)) { return(null); } return(retVal); }
private object getAsDecimal(LuaCore.lua_State luaState, int stackPos) { decimal retVal = (decimal)LuaLib.lua_tonumber(luaState, stackPos); if (retVal == 0 && !LuaLib.lua_isnumber(luaState, stackPos)) { return(null); } return(retVal); }
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); }
private object getAsFloat(LuaCore.lua_State luaState, int stackPos) { float retVal = (float)LuaLib.lua_tonumber(luaState, stackPos); if (retVal == 0 && !LuaLib.lua_isnumber(luaState, stackPos)) { return(null); } return(retVal); }
/* * Sets up the list of objects in the Lua side */ private void createLuaObjectList(LuaCore.lua_State luaState) { LuaLib.lua_pushstring(luaState, "luaNet_objects"); LuaLib.lua_newtable(luaState); LuaLib.lua_newtable(luaState); LuaLib.lua_pushstring(luaState, "__mode"); LuaLib.lua_pushstring(luaState, "v"); LuaLib.lua_settable(luaState, -3); LuaLib.lua_setmetatable(luaState, -2); LuaLib.lua_settable(luaState, (int)LuaIndexes.Registry); }
private object getAsDouble(LuaCore.lua_State luaState, int stackPos) { double retVal = LuaLib.lua_tonumber(luaState, stackPos); if (retVal == 0 && !LuaLib.lua_isnumber(luaState, stackPos)) { return(null); } return(retVal); }
/* * Writes to fields or properties, either static or instance. Throws an error * if the operation is invalid. */ private int setMember(LuaCore.lua_State luaState, IReflect targetType, object target, BindingFlags bindingType) { string detail; bool success = trySetMember(luaState, targetType, target, bindingType, out detail); if (!success) { translator.throwError(luaState, detail); } return(0); }
/// <summary> /// Convert a C# exception into a Lua error /// </summary> /// <param name="e"></param> /// We try to look into the exception to give the most meaningful description void ThrowError(LuaCore.lua_State luaState, Exception e) { // If we got inside a reflection show what really happened var te = e as TargetInvocationException; if (!te.IsNull()) { e = te.InnerException; } translator.throwError(luaState, e); }
private int callConstructorInternal(LuaCore.lua_State luaState) { var validConstructor = new MethodCache(); IReflect klass; object obj = translator.getRawNetObject(luaState, 1); if (obj.IsNull() || !(obj is IReflect)) { translator.throwError(luaState, "trying to call constructor on an invalid type reference"); LuaLib.lua_pushnil(luaState); return(1); } else { klass = (IReflect)obj; } if (!translator.interpreter.ManagedObjectSecurityPolicy.PermitObjectConstruction(klass.UnderlyingSystemType)) { LuaLib.lua_pushnil(luaState); return(1); } LuaLib.lua_remove(luaState, 1); var constructors = klass.UnderlyingSystemType.GetConstructors(); foreach (var constructor in constructors) { bool isConstructor = matchParameters(luaState, constructor, ref validConstructor); if (isConstructor) { try { translator.push(luaState, constructor.Invoke(validConstructor.args)); } catch (TargetInvocationException e) { ThrowError(luaState, e); LuaLib.lua_pushnil(luaState); } catch { LuaLib.lua_pushnil(luaState); } return(1); } } string constructorName = (constructors.Length == 0) ? "unknown" : constructors [0].Name; translator.throwError(luaState, String.Format("{0} does not contain constructor({1}) argument match", klass.UnderlyingSystemType, constructorName)); LuaLib.lua_pushnil(luaState); return(1); }
/// <summary> /// CP: Fix for operator overloading failure /// Returns true if the type is set and assigns the extract value /// </summary> /// <param name="luaState"></param> /// <param name="currentLuaParam"></param> /// <param name="currentNetParam"></param> /// <param name="extractValue"></param> /// <returns></returns> private bool _IsTypeCorrect(LuaCore.lua_State luaState, int currentLuaParam, ParameterInfo currentNetParam, out ExtractValue extractValue) { try { return((extractValue = translator.typeChecker.checkType(luaState, currentLuaParam, currentNetParam.ParameterType)) != null); } catch { extractValue = null; Debug.WriteLine("Type wasn't correct"); return(false); } }
private void DebugHookCallbackInternal(LuaCore.lua_State luaState, LuaCore.lua_Debug luaDebug) { try { var temp = DebugHook; if (!temp.IsNull()) { temp(this, new DebugHookEventArgs(luaDebug)); } } catch (Exception ex) { OnHookException(new HookExceptionEventArgs(ex)); } }
/* * Pushes a CLR object into the Lua stack as an userdata * with the provided metatable */ internal void pushObject(LuaCore.lua_State luaState, object o, string metatable) { int index = -1; if (metatable == "luaNet_metatable") { var serializer = interpreter.ManagedObjectSerializer; object serializedObject; if (serializer != null && serializer.Serialize(interpreter, o, out serializedObject)) { push(luaState, serializedObject); return; } } // Pushes nil if (o.IsNull()) { LuaLib.lua_pushnil(luaState); return; } // Object already in the list of Lua objects? Push the stored reference. bool found = objectsBackMap.TryGetValue(o, out index); if (found) { LuaLib.luaL_getmetatable(luaState, "luaNet_objects"); LuaLib.lua_rawgeti(luaState, -1, index); // Note: starting with lua5.1 the garbage collector may remove weak reference items (such as our luaNet_objects values) when the initial GC sweep // occurs, but the actual call of the __gc finalizer for that object may not happen until a little while later. During that window we might call // this routine and find the element missing from luaNet_objects, but collectObject() has not yet been called. In that case, we go ahead and call collect // object here // did we find a non nil object in our table? if not, we need to call collect object var type = LuaLib.lua_type(luaState, -1); if (type != LuaTypes.Nil) { LuaLib.lua_remove(luaState, -2); // drop the metatable - we're going to leave our object on the stack return; } // MetaFunctions.dumpStack(this, luaState); LuaLib.lua_remove(luaState, -1); // remove the nil object value LuaLib.lua_remove(luaState, -1); // remove the metatable collectObject(o, index); // Remove from both our tables and fall out to get a new ID } index = addObject(o); pushNewObject(luaState, o, index, metatable); }
private bool _IsParamsArray(LuaCore.lua_State luaState, int currentLuaParam, ParameterInfo currentNetParam, out ExtractValue extractValue) { extractValue = null; if (currentNetParam.GetCustomAttributes(typeof(ParamArrayAttribute), false).Length > 0) { LuaTypes luaType; try { luaType = LuaLib.lua_type(luaState, currentLuaParam); } catch (Exception ex) { Debug.WriteLine("Could not retrieve lua type while attempting to determine params Array Status."); Debug.WriteLine(ex.Message); extractValue = null; return(false); } if (luaType == LuaTypes.Table) { try { extractValue = translator.typeChecker.getExtractor(typeof(LuaTable)); } catch (Exception /* ex*/) { Debug.WriteLine("An error occurred during an attempt to retrieve a LuaTable extractor while checking for params array status."); } if (!extractValue.IsNull()) { return(true); } } else { var paramElementType = currentNetParam.ParameterType.GetElementType(); try { extractValue = translator.typeChecker.checkType(luaState, currentLuaParam, paramElementType); } catch (Exception /* ex*/) { Debug.WriteLine(string.Format("An error occurred during an attempt to retrieve an extractor ({0}) while checking for params array status.", paramElementType.FullName)); } if (!extractValue.IsNull()) { return(true); } } } Debug.WriteLine("Type wasn't Params object."); return(false); }
public static bool luaL_checkmetatable(LuaCore.lua_State luaState, int index) { bool retVal = false; if (lua_getmetatable(luaState, index) != 0) { lua_pushlightuserdata(luaState, tag); lua_rawget(luaState, -2); retVal = !lua_isnil(luaState, -1); lua_settop(luaState, -3); } return(retVal); }
private int registerTableInternal(LuaCore.lua_State luaState) { if (LuaLib.lua_type(luaState, 1) == LuaTypes.Table) { var luaTable = getTable(luaState, 1); string superclassName = LuaLib.lua_tostring(luaState, 2).ToString(); if (!superclassName.IsNull()) { var klass = FindType(superclassName); if (!klass.IsNull()) { // Creates and pushes the object in the stack, setting // it as the metatable of the first argument object obj = CodeGeneration.Instance.GetClassInstance(klass, luaTable); pushObject(luaState, obj, "luaNet_metatable"); LuaLib.lua_newtable(luaState); LuaLib.lua_pushstring(luaState, "__index"); LuaLib.lua_pushvalue(luaState, -3); LuaLib.lua_settable(luaState, -3); LuaLib.lua_pushstring(luaState, "__newindex"); LuaLib.lua_pushvalue(luaState, -3); LuaLib.lua_settable(luaState, -3); LuaLib.lua_setmetatable(luaState, 1); // Pushes the object again, this time as the base field // of the table and with the luaNet_searchbase metatable LuaLib.lua_pushstring(luaState, "base"); int index = addObject(obj); pushNewObject(luaState, obj, index, "luaNet_searchbase"); LuaLib.lua_rawset(luaState, 1); } else { throwError(luaState, "register_table: can not find superclass '" + superclassName + "'"); } } else { throwError(luaState, "register_table: superclass name can not be null"); } } else { throwError(luaState, "register_table: first arg is not a table"); } return(0); }
/* * Pushes the object into the Lua stack according to its type. */ internal void push(LuaCore.lua_State luaState, object o) { if (o.IsNull()) { LuaLib.lua_pushnil(luaState); } else if (o is sbyte || o is byte || o is short || o is ushort || o is int || o is uint || o is long || o is float || o is ulong || o is decimal || o is double) { double d = Convert.ToDouble(o); LuaLib.lua_pushnumber(luaState, d); } else if (o is char) { double d = (char)o; LuaLib.lua_pushnumber(luaState, d); } else if (o is string) { string str = (string)o; LuaLib.lua_pushstring(luaState, str); } else if (o is bool) { bool b = (bool)o; LuaLib.lua_pushboolean(luaState, b); } else if (IsILua(o)) { (((ILuaGeneratedType)o).LuaInterfaceGetLuaTable()).push(luaState); } else if (o is LuaTable) { ((LuaTable)o).push(luaState); } else if (o is LuaCore.lua_CFunction) { pushFunction(luaState, (LuaCore.lua_CFunction)o); } else if (o is LuaFunction) { ((LuaFunction)o).push(luaState); } else { pushObject(luaState, o, "luaNet_metatable"); } }
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); }
/* * __gc metafunction of CLR objects. */ private int collectObject(LuaCore.lua_State luaState) { int udata = LuaLib.luanet_rawnetobj(luaState, 1); if (udata != -1) { translator.collectObject(udata); } else { // Debug.WriteLine("not found: " + udata); } return(0); }
/* * Implementation of get_method_bysig. Returns nil * if no matching method is not found. */ private int getMethodSignature(LuaCore.lua_State luaState) { IReflect klass; object target; int udata = LuaLib.luanet_checkudata(luaState, 1, "luaNet_class"); if (udata != -1) { klass = (IReflect)objects[udata]; target = null; } else { target = getRawNetObject(luaState, 1); if (target.IsNull()) { throwError(luaState, "get_method_bysig: first arg is not type or object reference"); LuaLib.lua_pushnil(luaState); return(1); } klass = target.GetType(); } string methodName = LuaLib.lua_tostring(luaState, 2).ToString(); var signature = new Type[LuaLib.lua_gettop(luaState) - 2]; for (int i = 0; i < signature.Length; i++) { signature[i] = FindType(LuaLib.lua_tostring(luaState, i + 3).ToString()); } try { //CP: Added ignore case var method = klass.GetMethod(methodName, BindingFlags.Public | BindingFlags.Static | BindingFlags.Instance | BindingFlags.FlattenHierarchy | BindingFlags.IgnoreCase, null, signature, null); pushFunction(luaState, new LuaCore.lua_CFunction((new LuaMethodWrapper(this, target, klass, method)).call)); } catch (Exception e) { throwError(luaState, e); LuaLib.lua_pushnil(luaState); } return(1); }
/* * Pushes a new object into the Lua stack with the provided * metatable */ private void pushNewObject(LuaCore.lua_State luaState, object o, int index, string metatable) { if (metatable == "luaNet_metatable") { // Gets or creates the metatable for the object's type LuaLib.luaL_getmetatable(luaState, o.GetType().AssemblyQualifiedName); if (LuaLib.lua_isnil(luaState, -1)) { LuaLib.lua_settop(luaState, -2); LuaLib.luaL_newmetatable(luaState, o.GetType().AssemblyQualifiedName); LuaLib.lua_pushstring(luaState, "cache"); LuaLib.lua_newtable(luaState); LuaLib.lua_rawset(luaState, -3); LuaLib.lua_pushlightuserdata(luaState, LuaLib.luanet_gettag()); LuaLib.lua_pushnumber(luaState, 1); LuaLib.lua_rawset(luaState, -3); LuaLib.lua_pushstring(luaState, "__index"); LuaLib.lua_pushstring(luaState, "luaNet_indexfunction"); LuaLib.lua_rawget(luaState, (int)LuaIndexes.Registry); LuaLib.lua_rawset(luaState, -3); LuaLib.lua_pushstring(luaState, "__gc"); LuaLib.lua_pushstdcallcfunction(luaState, metaFunctions.gcFunction); LuaLib.lua_rawset(luaState, -3); LuaLib.lua_pushstring(luaState, "__tostring"); LuaLib.lua_pushstdcallcfunction(luaState, metaFunctions.toStringFunction); LuaLib.lua_rawset(luaState, -3); LuaLib.lua_pushstring(luaState, "__newindex"); LuaLib.lua_pushstdcallcfunction(luaState, metaFunctions.newindexFunction); LuaLib.lua_rawset(luaState, -3); } } else { LuaLib.luaL_getmetatable(luaState, metatable); } // Stores the object index in the Lua list and pushes the // index into the Lua stack LuaLib.luaL_getmetatable(luaState, "luaNet_objects"); LuaLib.luanet_newudata(luaState, index); LuaLib.lua_pushvalue(luaState, -3); LuaLib.lua_remove(luaState, -4); LuaLib.lua_setmetatable(luaState, -2); LuaLib.lua_pushvalue(luaState, -1); LuaLib.lua_rawseti(luaState, -3, index); LuaLib.lua_remove(luaState, -2); }
private int importTypeInternal(LuaCore.lua_State luaState) { string className = LuaLib.lua_tostring(luaState, 1).ToString(); var klass = FindType(className); if (!klass.IsNull()) { pushType(luaState, klass); } else { LuaLib.lua_pushnil(luaState); } return(1); }
/* * Pushes the entire array into the Lua stack and returns the number * of elements pushed. */ internal int returnValues(LuaCore.lua_State luaState, object[] returnValues) { if (LuaLib.lua_checkstack(luaState, returnValues.Length + 5)) { for (int i = 0; i < returnValues.Length; i++) { push(luaState, returnValues [i]); } return(returnValues.Length); } else { return(0); } }
/* * Registers the global functions used by NLua */ private void setGlobalFunctions(LuaCore.lua_State luaState) { LuaLib.lua_pushstdcallcfunction(luaState, metaFunctions.indexFunction); LuaLib.lua_setglobal(luaState, "get_object_member"); LuaLib.lua_pushstdcallcfunction(luaState, importTypeFunction); LuaLib.lua_setglobal(luaState, "import_type"); LuaLib.lua_pushstdcallcfunction(luaState, loadAssemblyFunction); LuaLib.lua_setglobal(luaState, "load_assembly"); LuaLib.lua_pushstdcallcfunction(luaState, registerTableFunction); LuaLib.lua_setglobal(luaState, "make_object"); LuaLib.lua_pushstdcallcfunction(luaState, unregisterTableFunction); LuaLib.lua_setglobal(luaState, "free_object"); LuaLib.lua_pushstdcallcfunction(luaState, getMethodSigFunction); LuaLib.lua_setglobal(luaState, "get_method_bysig"); LuaLib.lua_pushstdcallcfunction(luaState, getConstructorSigFunction); LuaLib.lua_setglobal(luaState, "get_constructor_bysig"); }
private int setClassFieldOrPropertyInternal(LuaCore.lua_State luaState) { IReflect target; object obj = translator.getRawNetObject(luaState, 1); if (obj.IsNull() || !(obj is IReflect)) { translator.throwError(luaState, "trying to index an invalid type reference"); return(0); } else { target = (IReflect)obj; } return(setMember(luaState, target, null, BindingFlags.FlattenHierarchy | BindingFlags.Static | BindingFlags.IgnoreCase)); }
/* * 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; }
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); }