public static int setFieldOrProperty(IntPtr luaState) { ObjectTranslator objectTranslator = ObjectTranslator.FromState(luaState); object rawNetObject = objectTranslator.getRawNetObject(luaState, 1); if (rawNetObject == null) { objectTranslator.throwError(luaState, "trying to index and invalid object reference"); return(0); } Type type = rawNetObject.GetType(); string message; bool flag = objectTranslator.metaFunctions.trySetMember(luaState, type, rawNetObject, BindingFlags.IgnoreCase | BindingFlags.Instance, out message); if (flag) { return(0); } try { if (type.IsArray && LuaDLL.lua_isnumber(luaState, 2)) { int index = (int)LuaDLL.lua_tonumber(luaState, 2); Array array = (Array)rawNetObject; object asType = objectTranslator.getAsType(luaState, 3, array.GetType().GetElementType()); array.SetValue(asType, index); } else { MethodInfo method = type.GetMethod("set_Item"); if (method != null) { ParameterInfo[] parameters = method.GetParameters(); Type parameterType = parameters[1].ParameterType; object asType2 = objectTranslator.getAsType(luaState, 3, parameterType); Type parameterType2 = parameters[0].ParameterType; object asType3 = objectTranslator.getAsType(luaState, 2, parameterType2); method.Invoke(rawNetObject, new object[] { asType3, asType2 }); } else { objectTranslator.throwError(luaState, message); } } } catch (SEHException) { throw; } catch (Exception e) { objectTranslator.metaFunctions.ThrowError(luaState, e); } return(0); }
/* * Called by the __index metafunction of CLR objects in case the * method is not cached or it is a field/property/event. * Receives the object and the member name as arguments and returns * either the value of the member or a delegate to call it. * If the member does not exist returns nil. */ private int getMethod(KopiLua.Lua.lua_State luaState) { object obj = translator.getRawNetObject(luaState, 1); if (obj == null) { translator.throwError(luaState, "trying to index an invalid object reference"); LuaDLL.lua_pushnil(luaState); return(1); } object index = translator.getObject(luaState, 2); Type indexType = index.GetType(); string methodName = index as string; // will be null if not a string arg Type objType = obj.GetType(); // Handle the most common case, looking up the method by name if (methodName != null && isMemberPresent(objType, methodName)) { return(getMember(luaState, objType, obj, methodName, BindingFlags.Instance)); } // Try to access by array if the type is right and index is an int (lua numbers always come across as double) if (objType.IsArray && index is double) { object[] arr = (object[])obj; translator.push(luaState, arr[(int)((double)index)]); } else { // Try to use get_Item to index into this .net object MethodInfo getter = objType.GetMethod("get_Item"); ParameterInfo[] actualParms = (getter != null) ? getter.GetParameters() : null; if (actualParms == null || actualParms.Length != 1) { translator.throwError(luaState, "method not found (or no indexer): " + index); LuaDLL.lua_pushnil(luaState); } else { // Get the index in a form acceptable to the getter index = translator.getAsType(luaState, 2, actualParms[0].ParameterType); object[] args = new object[1]; // Just call the indexer - if out of bounds an exception will happen args[0] = index; try { object result = getter.Invoke(obj, args); translator.push(luaState, result); } catch (TargetInvocationException e) { // Provide a more readable description for the common case of key not found if (e.InnerException is KeyNotFoundException) { translator.throwError(luaState, "key '" + index + "' not found "); } else { translator.throwError(luaState, "exception indexing '" + index + "' " + e.Message); } LuaDLL.lua_pushnil(luaState); } } } LuaDLL.lua_pushboolean(luaState, false); return(2); }
/* * Called by the __index metafunction of CLR objects in case the * method is not cached or it is a field/property/event. * Receives the object and the member name as arguments and returns * either the value of the member or a delegate to call it. * If the member does not exist returns nil. */ private int getMethod(IntPtr luaState) { object obj = translator.getRawNetObject(luaState, 1); if (obj == null) { translator.throwError(luaState, "trying to index an invalid object reference"); LuaDLL.lua_pushnil(luaState); return(1); } object index = translator.getObject(luaState, 2); Type indexType = index.GetType(); string methodName = index as string; // will be null if not a string arg Type objType = obj.GetType(); // Handle the most common case, looking up the method by name. // CP: This will fail when using indexers and attempting to get a value with the same name as a property of the object, // ie: xmlelement['item'] <- item is a property of xmlelement try { if (methodName != null && isMemberPresent(objType, methodName)) { return(getMember(luaState, objType, obj, methodName, BindingFlags.Instance | BindingFlags.IgnoreCase)); } } catch { } // Try to access by array if the type is right and index is an int (lua numbers always come across as double) if (objType.IsArray && index is double) { int intIndex = (int)((double)index); if (objType.UnderlyingSystemType == typeof(float[])) { float[] arr = ((float[])obj); translator.push(luaState, arr[intIndex]); } else if (objType.UnderlyingSystemType == typeof(double[])) { double[] arr = ((double[])obj); translator.push(luaState, arr[intIndex]); } else if (objType.UnderlyingSystemType == typeof(int[])) { int[] arr = ((int[])obj); translator.push(luaState, arr[intIndex]); } else { object[] arr = (object[])obj; translator.push(luaState, arr[intIndex]); } } else { // Try to use get_Item to index into this .net object //MethodInfo getter = objType.GetMethod("get_Item"); MethodInfo[] methods = objType.GetMethods(); foreach (MethodInfo mInfo in methods) { if (mInfo.Name == "get_Item") { //check if the signature matches the input if (mInfo.GetParameters().Length == 1) { MethodInfo getter = mInfo; ParameterInfo[] actualParms = (getter != null) ? getter.GetParameters() : null; if (actualParms == null || actualParms.Length != 1) { translator.throwError(luaState, "method not found (or no indexer): " + index); LuaDLL.lua_pushnil(luaState); } else { // Get the index in a form acceptable to the getter index = translator.getAsType(luaState, 2, actualParms[0].ParameterType); object[] args = new object[1]; // Just call the indexer - if out of bounds an exception will happen args[0] = index; try { object result = getter.Invoke(obj, args); translator.push(luaState, result); } catch (TargetInvocationException e) { // Provide a more readable description for the common case of key not found if (e.InnerException is KeyNotFoundException) { translator.throwError(luaState, "key '" + index + "' not found "); } else { translator.throwError(luaState, "exception indexing '" + index + "' " + e.Message); } LuaDLL.lua_pushnil(luaState); } } } } } } LuaDLL.lua_pushboolean(luaState, false); return(2); }
/// <summary>[-0, +2, e] /// Called by the __index metafunction of CLR objects in case the method is not cached or it is a field/property/event. /// Receives the object and the member name as arguments and returns either the value of the member or a delegate to call it. /// If the member does not exist returns nil. /// </summary> int getMethod(lua.State L) { Debug.Assert(interpreter.IsSameLua(L) && luanet.infunction(L)); object obj = luaclr.checkref(L, 1); object index = translator.getObject(L, 2); //Type indexType = index.GetType(); string methodName = index as string; // will be null if not a string arg Type objType = obj.GetType(); // Handle the most common case, looking up the method by name. // CP: This will fail when using indexers and attempting to get a value with the same name as a property of the object, // ie: xmlelement['item'] <- item is a property of xmlelement if (methodName != null && isMemberPresent(objType, methodName)) { return(getMember(L, objType, obj, methodName, BindingFlags.Instance)); } bool failed = true; // Try to access by array if the type is right and index is an int (lua numbers always come across as double) if (objType.IsArray && index is double) { object val; try { val = ((Array)obj).GetValue((int)(double)index); } catch (Exception ex) { return(translator.throwError(L, ex)); } translator.push(L, val); failed = false; } else { try { // Try to use get_Item to index into this .net object //MethodInfo getter = objType.GetMethod("get_Item"); // issue here is that there may be multiple indexers.. foreach (MethodInfo mInfo in objType.GetMethods()) { if (mInfo.Name == "get_Item") { ParameterInfo[] actualParms = mInfo.GetParameters(); if (actualParms.Length != 1) //check if the signature matches the input { continue; } if (!translator.memberIsAllowed(mInfo)) { continue; } // Get the index in a form acceptable to the getter index = translator.getAsType(L, 2, actualParms[0].ParameterType); // Just call the indexer - if out of bounds an exception will happen object o = mInfo.Invoke(obj, new[] { index }); failed = false; translator.push(L, o); break; } } } catch (TargetInvocationException ex) { return(translator.throwError(L, luaclr.verifyex(ex.InnerException))); } catch (LuaInternalException) { throw; } catch (Exception ex) { return(luaL.error(L, "unable to index {0}: {1}", objType, ex.Message)); } } if (failed) { return(luaL.error(L, "cannot find " + index)); } lua.pushboolean(L, false); return(2); }
/// <summary> /// Tries to set a named property or field /// </summary> /// <param name="luaState"></param> /// <param name="targetType"></param> /// <param name="target"></param> /// <param name="bindingType"></param> /// <returns>false if unable to find the named member, true for success</returns> private bool trySetMember(IntPtr luaState, IReflect targetType, object target, BindingFlags bindingType, out string detailMessage) { detailMessage = null; // No error yet // If not already a string just return - we don't want to call tostring - which has the side effect of // changing the lua typecode to string // Note: We don't use isstring because the standard lua C isstring considers either strings or numbers to // be true for isstring. if (LuaDLL.lua_type(luaState, 2) != LuaTypes.LUA_TSTRING) { detailMessage = "property names must be strings"; return(false); } // We only look up property names by string string fieldName = LuaDLL.lua_tostring(luaState, 2); if (fieldName == null || fieldName.Length < 1 || !(char.IsLetter(fieldName[0]) || fieldName[0] == '_')) { detailMessage = "invalid property name"; return(false); } // Find our member via reflection or the cache MemberInfo member = (MemberInfo)checkMemberCache(memberCache, targetType, fieldName); if (member == null) { //CP: Removed NonPublic binding search and made case insensitive MemberInfo[] members = targetType.GetMember(fieldName, bindingType | BindingFlags.Public | BindingFlags.IgnoreCase /*| BindingFlags.NonPublic*/); if (members.Length > 0) { member = members[0]; setMemberCache(memberCache, targetType, fieldName, member); } else { detailMessage = "field or property '" + fieldName + "' does not exist"; return(false); } } if (member.MemberType == MemberTypes.Field) { FieldInfo field = (FieldInfo)member; object val = translator.getAsType(luaState, 3, field.FieldType); try { field.SetValue(target, val); } catch (Exception e) { ThrowError(luaState, e); } // We did a call return(true); } else if (member.MemberType == MemberTypes.Property) { PropertyInfo property = (PropertyInfo)member; object val = translator.getAsType(luaState, 3, property.PropertyType); try { property.SetValue(target, val, null); } catch (Exception e) { ThrowError(luaState, e); } // We did a call return(true); } detailMessage = "'" + fieldName + "' is not a .net field or property"; return(false); }
public static int setFieldOrProperty(IntPtr luaState) { ObjectTranslator translator = ObjectTranslator.FromState(luaState); object target = translator.getRawNetObject(luaState, 1); if (target == null) { translator.throwError(luaState, "trying to index and invalid object reference"); return(0); } Type type = target.GetType(); // First try to look up the parameter as a property name string detailMessage; bool didMember = translator.metaFunctions.trySetMember(luaState, type, target, BindingFlags.Instance | BindingFlags.IgnoreCase, out detailMessage); if (didMember) { return(0); // Must have found the property name } // We didn't find a property name, now see if we can use a [] style this accessor to set array contents try { if (type.IsArray && LuaDLL.lua_isnumber(luaState, 2)) { int index = (int)LuaDLL.lua_tonumber(luaState, 2); Array arr = (Array)target; object val = translator.getAsType(luaState, 3, arr.GetType().GetElementType()); arr.SetValue(val, index); } else { // Try to see if we have a this[] accessor MethodInfo setter = type.GetMethod("set_Item"); if (setter != null) { ParameterInfo[] args = setter.GetParameters(); Type valueType = args[1].ParameterType; // The new val ue the user specified object val = translator.getAsType(luaState, 3, valueType); Type indexType = args[0].ParameterType; object index = translator.getAsType(luaState, 2, indexType); object[] methodArgs = new object[2]; // Just call the indexer - if out of bounds an exception will happen methodArgs[0] = index; methodArgs[1] = val; setter.Invoke(target, methodArgs); } else { translator.throwError(luaState, detailMessage); // Pass the original message from trySetMember because it is probably best } } } catch (SEHException) { // If we are seeing a C++ exception - this must actually be for Lua's private use. Let it handle it throw; } catch (Exception e) { translator.metaFunctions.ThrowError(luaState, e); } return(0); }
public static int getMethod(IntPtr luaState) { ObjectTranslator translator = ObjectTranslator.FromState(luaState); object obj = translator.getRawNetObject(luaState, 1); if (obj == null) { translator.throwError(luaState, "trying to index an invalid object reference"); LuaDLL.lua_pushnil(luaState); return(1); } object index = translator.getObject(luaState, 2); //Type indexType = index.GetType(); //* not used string methodName = index as string; // will be null if not a string arg Type objType = obj.GetType(); // Handle the most common case, looking up the method by name. // CP: This will fail when using indexers and attempting to get a value with the same name as a property of the object, // ie: xmlelement['item'] <- item is a property of xmlelement try { if (methodName != null && translator.metaFunctions.isMemberPresent(objType, methodName)) { return(translator.metaFunctions.getMember(luaState, objType, obj, methodName, BindingFlags.Instance | BindingFlags.IgnoreCase)); } } catch { } bool failed = true; // Try to access by array if the type is right and index is an int (lua numbers always come across as double) if (objType.IsArray && index is double) { int intIndex = (int)((double)index); Array aa = obj as Array; if (intIndex >= aa.Length) { return(translator.pushError(luaState, "array index out of bounds: " + intIndex + " " + aa.Length)); } object val = aa.GetValue(intIndex); translator.push(luaState, val); failed = false; } else { // Try to use get_Item to index into this .net object //MethodInfo getter = objType.GetMethod("get_Item"); // issue here is that there may be multiple indexers.. MethodInfo[] methods = objType.GetMethods(); foreach (MethodInfo mInfo in methods) { if (mInfo.Name == "get_Item") { //check if the signature matches the input if (mInfo.GetParameters().Length == 1) { MethodInfo getter = mInfo; ParameterInfo[] actualParms = (getter != null) ? getter.GetParameters() : null; if (actualParms == null || actualParms.Length != 1) { return(translator.pushError(luaState, "method not found (or no indexer): " + index)); } else { // Get the index in a form acceptable to the getter index = translator.getAsType(luaState, 2, actualParms[0].ParameterType); // Just call the indexer - if out of bounds an exception will happen try { object result = getter.Invoke(obj, new object[] { index }); translator.push(luaState, result); failed = false; } catch (TargetInvocationException e) { // Provide a more readable description for the common case of key not found if (e.InnerException is KeyNotFoundException) { return(translator.pushError(luaState, "key '" + index + "' not found ")); } else { return(translator.pushError(luaState, "exception indexing '" + index + "' " + e.Message)); } } } } } } } if (failed) { return(translator.pushError(luaState, "cannot find " + index)); } LuaDLL.lua_pushboolean(luaState, false); return(2); }
/* * __newindex metafunction of CLR objects. Receives the object, * the member name and the value to be stored as arguments. Throws * and error if the assignment is invalid. */ private int setFieldOrProperty(IntPtr luaState) { if (LuaDLL.lua_isnumber(luaState, 2)) { object target = translator.getRawNetObject(luaState, 1); if (target == null) { translator.throwError(luaState, "trying to index and invalid object reference"); return(0); } int index = (int)LuaDLL.lua_tonumber(luaState, 2); try { if (target is Array) { object[] arr = (object[])target; object val = translator.getAsType(luaState, 3, arr.GetType().GetElementType()); arr[index] = val; } else { // Try to see if we have an array accessor Type type = target.GetType(); MethodInfo setter = type.GetMethod("set_Item"); if (setter == null) { translator.throwError(luaState, "this object does not support indexed setting"); } else { ParameterInfo [] args = setter.GetParameters(); Type valueType = args[1].ParameterType; // The new val ue the user specified object val = translator.getAsType(luaState, 3, valueType); object[] methodArgs = new object[2]; // Just call the indexer - if out of bounds an exception will happen methodArgs[0] = index; methodArgs[1] = val; setter.Invoke(target, methodArgs); } } } catch (Exception e) { translator.throwError(luaState, e); } return(0); } else { object target = translator.getRawNetObject(luaState, 1); if (target == null) { translator.throwError(luaState, "trying to index an invalid object reference"); return(0); } return(setMember(luaState, target.GetType(), target, BindingFlags.Instance)); } }
public static int getMethod(IntPtr luaState) { ObjectTranslator objectTranslator = ObjectTranslator.FromState(luaState); object rawNetObject = objectTranslator.getRawNetObject(luaState, 1); if (rawNetObject == null) { objectTranslator.throwError(luaState, "trying to index an invalid object reference"); LuaDLL.lua_pushnil(luaState); return(1); } object obj = objectTranslator.getObject(luaState, 2); string text = obj as string; Type type = rawNetObject.GetType(); try { if (text != null && objectTranslator.metaFunctions.isMemberPresent(type, text)) { int result = objectTranslator.metaFunctions.getMember(luaState, type, rawNetObject, text, BindingFlags.IgnoreCase | BindingFlags.Instance); return(result); } } catch { } bool flag = true; if (type.IsArray && obj is double) { int num = (int)((double)obj); Array array = rawNetObject as Array; if (num >= array.Length) { return(objectTranslator.pushError(luaState, string.Concat(new object[] { "array index out of bounds: ", num, " ", array.Length }))); } object value = array.GetValue(num); objectTranslator.push(luaState, value); flag = false; } else { MethodInfo[] methods = type.GetMethods(); MethodInfo[] array2 = methods; for (int i = 0; i < array2.Length; i++) { MethodInfo methodInfo = array2[i]; if (methodInfo.Name == "get_Item" && methodInfo.GetParameters().Length == 1) { MethodInfo methodInfo2 = methodInfo; ParameterInfo[] array3 = (methodInfo2 == null) ? null : methodInfo2.GetParameters(); if (array3 == null || array3.Length != 1) { return(objectTranslator.pushError(luaState, "method not found (or no indexer): " + obj)); } obj = objectTranslator.getAsType(luaState, 2, array3[0].ParameterType); try { object o = methodInfo2.Invoke(rawNetObject, new object[] { obj }); objectTranslator.push(luaState, o); flag = false; } catch (TargetInvocationException ex) { int result; if (ex.InnerException is KeyNotFoundException) { result = objectTranslator.pushError(luaState, "key '" + obj + "' not found "); return(result); } result = objectTranslator.pushError(luaState, string.Concat(new object[] { "exception indexing '", obj, "' ", ex.Message })); return(result); } } } } if (flag) { return(objectTranslator.pushError(luaState, "cannot find " + obj)); } LuaDLL.lua_pushboolean(luaState, false); return(2); }
/// <summary> /// Implementation of get_object_member. Called by the __index metafunction of CLR objects in case the method is not cached or it is a field/property/event. /// Receives the object and the member name as arguments and returns either the value of the member or a delegate to call it. /// If the member does not exist returns nil. /// </summary> int getMethod(lua.State L) { Debug.Assert(translator.interpreter.IsSameLua(L) && luanet.infunction(L)); object obj = luaclr.checkref(L, 1); object index = translator.getObject(L, 2); //Type indexType = index.GetType(); string methodName = index as string; // will be null if not a string arg Type objType = obj.GetType(); // Handle the most common case, looking up the method by name. // CP: This will fail when using indexers and attempting to get a value with the same name as a property of the object, // ie: xmlelement['item'] <- item is a property of xmlelement try { // todo: investigate: getMember throws lua errors. all other call sites are also CFunctions and do not use try{}. // possible reasons: (1) the author didn't know that Lua errors pass through catch{} // (2) it is passing unusual input that might generate exceptions not seen in the other use cases // (3) it is a hasty fix for a bug if (methodName != null && isMemberPresent(objType, methodName)) { return(getMember(L, objType, obj, methodName, BindingFlags.Instance)); } } catch { } bool failed = true; // Try to access by array if the type is right and index is an int (lua numbers always come across as double) if (objType.IsArray && index is double) { int intIndex = (int)((double)index); var aa = (Array)obj; if (intIndex >= aa.Length) { return(pushError(L, "array index out of bounds: " + intIndex + " " + aa.Length)); } object val = aa.GetValue(intIndex); translator.push(L, val); failed = false; } else { // Try to use get_Item to index into this .net object //MethodInfo getter = objType.GetMethod("get_Item"); // issue here is that there may be multiple indexers.. foreach (MethodInfo mInfo in objType.GetMethods()) { if (mInfo.Name == "get_Item") { ParameterInfo[] actualParms = mInfo.GetParameters(); if (actualParms.Length != 1) //check if the signature matches the input { continue; } // Get the index in a form acceptable to the getter index = translator.getAsType(L, 2, actualParms[0].ParameterType); // Just call the indexer - if out of bounds an exception will happen try { translator.push(L, mInfo.Invoke(obj, new[] { index })); failed = false; } catch (TargetInvocationException e) { // Provide a more readable description for the common case of key not found if (e.InnerException is KeyNotFoundException) { return(pushError(L, "key '" + index + "' not found ")); } else { return(pushError(L, "exception indexing '" + index + "' " + e.InnerException.Message)); } } } } } if (failed) { return(pushError(L, "cannot find " + index)); } lua.pushboolean(L, false); return(2); }