public LuaFunction RawGetFunc(string field) { IntPtr L = _Interpreter.L; LuaTypes type = LuaTypes.LUA_TNONE; LuaFunction func = null; int oldTop = LuaDLL.lua_gettop(L); LuaDLL.lua_getref(L, _Reference); LuaDLL.lua_pushstring(L, field); LuaDLL.lua_gettable(L, -2); type = LuaDLL.lua_type(L, -1); if (type == LuaTypes.LUA_TFUNCTION) { func = new LuaFunction(LuaDLL.luaL_ref(L, LuaIndexes.LUA_REGISTRYINDEX), L); } LuaDLL.lua_settop(L, oldTop); return(func); }
public static int unregisterTable(IntPtr luaState) { ObjectTranslator translator = ObjectTranslator.FromState(luaState); try { if (LuaDLL.lua_getmetatable(luaState, 1) != 0) { LuaDLL.lua_pushstring(luaState, "__index"); LuaDLL.lua_gettable(luaState, -2); object obj = translator.getRawNetObject(luaState, -1); if (obj == null) { translator.throwError(luaState, "unregister_table: arg is not valid table"); } FieldInfo luaTableField = obj.GetType().GetField("__luaInterface_luaTable"); if (luaTableField == null) { translator.throwError(luaState, "unregister_table: arg is not valid table"); } luaTableField.SetValue(obj, null); LuaDLL.lua_pushnil(luaState); LuaDLL.lua_setmetatable(luaState, 1); LuaDLL.lua_pushstring(luaState, "base"); LuaDLL.lua_pushnil(luaState); LuaDLL.lua_settable(luaState, 1); } else { translator.throwError(luaState, "unregister_table: arg is not valid table"); } } catch (Exception e) { translator.throwError(luaState, e.Message); } return(0); }
/* * Creates a new table as a global variable or as a field * inside an existing table */ public void NewTable(string fullPath) { string[] path = fullPath.Split('.'); int oldTop = LuaDLL.lua_gettop(L); if (path.Length == 1) { LuaDLL.lua_newtable(L); LuaDLL.lua_setglobal(L, fullPath); } else { LuaDLL.lua_getglobal(L, path[0]); for (int i = 1; i < path.Length - 1; i++) { LuaDLL.lua_pushstring(L, path[i]); LuaDLL.lua_gettable(L, -2); } LuaDLL.lua_pushstring(L, path[path.Length - 1]); LuaDLL.lua_newtable(L); LuaDLL.lua_settable(L, -3); } LuaDLL.lua_settop(L, oldTop); }
/* * Creates a new table as a global variable or as a field * inside an existing table */ public void NewTable(string fullPath) { string[] path = fullPath.Split(new char[] { '.' }); int oldTop = LuaDLL.lua_gettop(luaState); if (path.Length == 1) { LuaDLL.lua_newtable(luaState); LuaDLL.lua_setglobal(luaState, fullPath); } else { LuaDLL.lua_getglobal(luaState, path[0]); for (int i = 1; i < path.Length - 1; i++) { LuaDLL.lua_pushstring(luaState, path[i]); LuaDLL.lua_gettable(luaState, -2); } LuaDLL.lua_pushstring(luaState, path[path.Length - 1]); LuaDLL.lua_newtable(luaState); LuaDLL.lua_settable(luaState, -3); } LuaDLL.lua_settop(luaState, oldTop); }
/* * Implementation of free_object. Clears the metatable and the * base field, freeing the created object for garbage-collection */ private int unregisterTable(KopiLua.Lua.lua_State luaState) { try { if (LuaDLL.lua_getmetatable(luaState, 1) != 0) { LuaDLL.lua_pushstring(luaState, "__index"); LuaDLL.lua_gettable(luaState, -2); object obj = getRawNetObject(luaState, -1); if (obj == null) { throwError(luaState, "unregister_table: arg is not valid table"); } FieldInfo luaTableField = obj.GetType().GetField("__luaInterface_luaTable"); if (luaTableField == null) { throwError(luaState, "unregister_table: arg is not valid table"); } luaTableField.SetValue(obj, null); LuaDLL.lua_pushnil(luaState); LuaDLL.lua_setmetatable(luaState, 1); LuaDLL.lua_pushstring(luaState, "base"); LuaDLL.lua_pushnil(luaState); LuaDLL.lua_settable(luaState, 1); } else { throwError(luaState, "unregister_table: arg is not valid table"); } } catch (Exception e) { throwError(luaState, e.Message); } return(0); }
public static void lua_getglobal(IntPtr luaState, string name) { LuaDLL.lua_pushstring(luaState, name); LuaDLL.lua_gettable(luaState, LuaIndexes.LUA_GLOBALSINDEX); }
internal ExtractValue checkType(IntPtr luaState, int stackPos, Type paramType) { LuaTypes luatype = LuaDLL.lua_type(luaState, stackPos); if (paramType.IsByRef) { paramType = paramType.GetElementType(); } Type underlyingType = Nullable.GetUnderlyingType(paramType); if (underlyingType != null) { paramType = underlyingType; // Silently convert nullable types to their non null requics } long runtimeHandleValue = paramType.TypeHandle.Value.ToInt64(); if (paramType.Equals(typeof(object))) { return(extractValues[runtimeHandleValue]); } //CP: Added support for generic parameters if (paramType.IsGenericParameter) { if (luatype == LuaTypes.LUA_TBOOLEAN) { return(extractValues[typeof(bool).TypeHandle.Value.ToInt64()]); } else if (luatype == LuaTypes.LUA_TSTRING) { return(extractValues[typeof(string).TypeHandle.Value.ToInt64()]); } else if (luatype == LuaTypes.LUA_TTABLE) { return(extractValues[typeof(LuaTable).TypeHandle.Value.ToInt64()]); } else if (luatype == LuaTypes.LUA_TUSERDATA) { return(extractValues[typeof(object).TypeHandle.Value.ToInt64()]); } else if (luatype == LuaTypes.LUA_TFUNCTION) { return(extractValues[typeof(LuaFunction).TypeHandle.Value.ToInt64()]); } else if (luatype == LuaTypes.LUA_TNUMBER) { return(extractValues[typeof(double).TypeHandle.Value.ToInt64()]); } //else // suppress CS0642 ; //an unsupported type was encountered } if (paramType.IsValueType && luatype == LuaTypes.LUA_TTABLE) { int oldTop = LuaDLL.lua_gettop(luaState); ExtractValue ret = null; LuaDLL.lua_pushvalue(luaState, stackPos); LuaDLL.lua_pushstring(luaState, "class"); LuaDLL.lua_gettable(luaState, -2); if (!LuaDLL.lua_isnil(luaState, -1)) { string cls = LuaDLL.lua_tostring(luaState, -1); if (cls == "Vector3" && paramType == typeof(Vector3)) { ret = extractValues[typeof(Vector3).TypeHandle.Value.ToInt64()]; } else if (cls == "Vector2" && paramType == typeof(Vector2)) { ret = extractValues[typeof(Vector2).TypeHandle.Value.ToInt64()]; } else if (cls == "Quaternion" && paramType == typeof(Quaternion)) { ret = extractValues[typeof(Quaternion).TypeHandle.Value.ToInt64()]; } else if (cls == "Color" && paramType == typeof(Color)) { ret = extractValues[typeof(Color).TypeHandle.Value.ToInt64()]; } else if (cls == "Vector4" && paramType == typeof(Vector4)) { ret = extractValues[typeof(Vector4).TypeHandle.Value.ToInt64()]; } else if (cls == "Ray" && paramType == typeof(Ray)) { ret = extractValues[typeof(Ray).TypeHandle.Value.ToInt64()]; } else { ret = null; } } LuaDLL.lua_settop(luaState, oldTop); if (ret != null) { return(ret); } } if (LuaDLL.lua_isnumber(luaState, stackPos)) { return(extractValues[runtimeHandleValue]); } if (paramType == typeof(bool)) { if (LuaDLL.lua_isboolean(luaState, stackPos)) { return(extractValues[runtimeHandleValue]); } } else if (paramType == typeof(string)) { if (LuaDLL.lua_isstring(luaState, stackPos)) { return(extractValues[runtimeHandleValue]); } else if (luatype == LuaTypes.LUA_TNIL) { return(extractNetObject); // kevinh - silently convert nil to a null string pointer } } else if (paramType == typeof(LuaTable)) { if (luatype == LuaTypes.LUA_TTABLE) { return(extractValues[runtimeHandleValue]); } } else if (paramType == typeof(LuaFunction)) { if (luatype == LuaTypes.LUA_TFUNCTION) { return(extractValues[runtimeHandleValue]); } } else if (typeof(Delegate).IsAssignableFrom(paramType) && luatype == LuaTypes.LUA_TFUNCTION) { #if __NOGEN__ translator.throwError(luaState, "Delegates not implemnented"); #else return(new ExtractValue(new DelegateGenerator(translator, paramType).extractGenerated)); #endif } else if (paramType.IsInterface && luatype == LuaTypes.LUA_TTABLE) { #if __NOGEN__ translator.throwError(luaState, "Interfaces not implemnented"); #else return(new ExtractValue(new ClassGenerator(translator, paramType).extractGenerated)); #endif } else if ((paramType.IsInterface || paramType.IsClass) && luatype == LuaTypes.LUA_TNIL) { // kevinh - allow nil to be silently converted to null - extractNetObject will return null when the item ain't found return(extractNetObject); } else if (LuaDLL.lua_type(luaState, stackPos) == LuaTypes.LUA_TTABLE) { if (LuaTypes.LUA_TNIL != LuaDLL.luaL_getmetafield(luaState, stackPos, "__index")) { object obj = translator.getNetObject(luaState, -1); LuaDLL.lua_settop(luaState, -2); if (obj != null && paramType.IsAssignableFrom(obj.GetType())) { return(extractNetObject); } } } else { //object obj = translator.getNetObject(luaState, stackPos); //topameng 修改这里使支持注册到c#的lua类 object obj = translator.getRawNetObject(luaState, stackPos); if (obj != null && paramType.IsAssignableFrom(obj.GetType())) { return(extractNetObject); } } return(null); }
public void LuaGetTable(int idx) { LuaDLL.lua_gettable(L, idx); }
internal ExtractValue checkType(IntPtr luaState, int stackPos, Type paramType) { LuaTypes luaTypes = LuaDLL.lua_type(luaState, stackPos); if (paramType.IsByRef) { paramType = paramType.GetElementType(); } Type underlyingType = Nullable.GetUnderlyingType(paramType); if (underlyingType != null) { paramType = underlyingType; } long key = paramType.TypeHandle.Value.ToInt64(); if (paramType.Equals(typeof(object))) { return(this.extractValues[key]); } if (paramType.IsGenericParameter) { if (luaTypes == LuaTypes.LUA_TBOOLEAN) { return(this.extractValues[typeof(bool).TypeHandle.Value.ToInt64()]); } if (luaTypes == LuaTypes.LUA_TSTRING) { return(this.extractValues[typeof(string).TypeHandle.Value.ToInt64()]); } if (luaTypes == LuaTypes.LUA_TTABLE) { return(this.extractValues[typeof(LuaTable).TypeHandle.Value.ToInt64()]); } if (luaTypes == LuaTypes.LUA_TUSERDATA) { return(this.extractValues[typeof(object).TypeHandle.Value.ToInt64()]); } if (luaTypes == LuaTypes.LUA_TFUNCTION) { return(this.extractValues[typeof(LuaFunction).TypeHandle.Value.ToInt64()]); } if (luaTypes == LuaTypes.LUA_TNUMBER) { return(this.extractValues[typeof(double).TypeHandle.Value.ToInt64()]); } } if (paramType.IsValueType && luaTypes == LuaTypes.LUA_TTABLE) { int newTop = LuaDLL.lua_gettop(luaState); ExtractValue extractValue = null; LuaDLL.lua_pushvalue(luaState, stackPos); LuaDLL.lua_pushstring(luaState, "class"); LuaDLL.lua_gettable(luaState, -2); if (!LuaDLL.lua_isnil(luaState, -1)) { string a = LuaDLL.lua_tostring(luaState, -1); if (a == "Vector3" && paramType == typeof(Vector3)) { extractValue = this.extractValues[typeof(Vector3).TypeHandle.Value.ToInt64()]; } else if (a == "Vector2" && paramType == typeof(Vector2)) { extractValue = this.extractValues[typeof(Vector2).TypeHandle.Value.ToInt64()]; } else if (a == "Quaternion" && paramType == typeof(Quaternion)) { extractValue = this.extractValues[typeof(Quaternion).TypeHandle.Value.ToInt64()]; } else if (a == "Color" && paramType == typeof(Color)) { extractValue = this.extractValues[typeof(Color).TypeHandle.Value.ToInt64()]; } else if (a == "Vector4" && paramType == typeof(Vector4)) { extractValue = this.extractValues[typeof(Vector4).TypeHandle.Value.ToInt64()]; } else if (a == "Ray" && paramType == typeof(Ray)) { extractValue = this.extractValues[typeof(Ray).TypeHandle.Value.ToInt64()]; } else { extractValue = null; } } LuaDLL.lua_settop(luaState, newTop); if (extractValue != null) { return(extractValue); } } if (LuaDLL.lua_isnumber(luaState, stackPos)) { return(this.extractValues[key]); } if (paramType == typeof(bool)) { if (LuaDLL.lua_isboolean(luaState, stackPos)) { return(this.extractValues[key]); } } else if (paramType == typeof(string)) { if (LuaDLL.lua_isstring(luaState, stackPos)) { return(this.extractValues[key]); } if (luaTypes == LuaTypes.LUA_TNIL) { return(this.extractNetObject); } } else if (paramType == typeof(LuaTable)) { if (luaTypes == LuaTypes.LUA_TTABLE) { return(this.extractValues[key]); } } else if (paramType == typeof(LuaFunction)) { if (luaTypes == LuaTypes.LUA_TFUNCTION) { return(this.extractValues[key]); } } else if (typeof(Delegate).IsAssignableFrom(paramType) && luaTypes == LuaTypes.LUA_TFUNCTION) { this.translator.throwError(luaState, "Delegates not implemnented"); } else if (paramType.IsInterface && luaTypes == LuaTypes.LUA_TTABLE) { this.translator.throwError(luaState, "Interfaces not implemnented"); } else { if ((paramType.IsInterface || paramType.IsClass) && luaTypes == LuaTypes.LUA_TNIL) { return(this.extractNetObject); } if (LuaDLL.lua_type(luaState, stackPos) == LuaTypes.LUA_TTABLE) { if (LuaDLL.luaL_getmetafield(luaState, stackPos, "__index") != LuaTypes.LUA_TNIL) { object netObject = this.translator.getNetObject(luaState, -1); LuaDLL.lua_settop(luaState, -2); if (netObject != null && paramType.IsAssignableFrom(netObject.GetType())) { return(this.extractNetObject); } } } else { object rawNetObject = this.translator.getRawNetObject(luaState, stackPos); if (rawNetObject != null && paramType.IsAssignableFrom(rawNetObject.GetType())) { return(this.extractNetObject); } } } return(null); }