} // func random /// <summary>Implementation of http://www.lua.org/manual/5.3/manual.html#pdf-math.randomseed </summary> /// <param name="x"></param> public static void randomseed(object x) { int seed; if (x == null) { seed = Environment.TickCount; } else { var tc = LuaEmit.GetTypeCode(x.GetType()); if (tc >= LuaEmitTypeCode.Byte && tc <= LuaEmitTypeCode.Decimal) { seed = Convert.ToInt32(x); } else if (tc == LuaEmitTypeCode.DateTime) { seed = unchecked ((int)((DateTime)x).ToFileTime()); } else { seed = x.GetHashCode(); } } rand = new Random(seed); } // proc randomseed
/// <summary>Implementation of http://www.lua.org/manual/5.3/manual.html#pdf-math.type </summary> /// <param name="x"></param> /// <returns></returns> public static string type(object x) { if (x == null) { return(null); } else { switch (LuaEmit.GetTypeCode(x.GetType())) { case LuaEmitTypeCode.Byte: case LuaEmitTypeCode.SByte: case LuaEmitTypeCode.Int16: case LuaEmitTypeCode.UInt16: case LuaEmitTypeCode.Int32: case LuaEmitTypeCode.UInt32: case LuaEmitTypeCode.Int64: case LuaEmitTypeCode.UInt64: return("integer"); case LuaEmitTypeCode.Double: case LuaEmitTypeCode.Single: case LuaEmitTypeCode.Decimal: return("float"); default: return(null); } } } // func type
} // func MemberGetSandbox private static Expression MemberGetExpressionCore(Lua lua, Token tokenStart, Expression instance, string memberName) { // if this is a dynamic type, let the type deside what is to do if (LuaEmit.IsDynamicType(instance.Type)) { return(DynamicExpression.Dynamic(lua.GetGetMemberBinder(memberName), typeof(object), ConvertObjectExpression(lua, tokenStart, instance, true))); } else { Expression result; switch (LuaEmit.TryGetMember(instance, instance.Type, memberName, false, out result)) { case LuaTryGetMemberReturn.None: throw ParseError(tokenStart, LuaEmitException.GetMessageText(LuaEmitException.MemberNotFound, instance.Type.Name, memberName)); case LuaTryGetMemberReturn.NotReadable: throw ParseError(tokenStart, LuaEmitException.GetMessageText(LuaEmitException.CanNotReadMember, instance.Type.Name, memberName)); case LuaTryGetMemberReturn.ValidExpression: return(result); default: throw new ArgumentException("Internal return type of TryGetMember"); } } } // func MemberGetExpressionCore
private static Expression MemberSetExpressionCore(Lua lua, Token tokenStart, Expression instance, string memberName, Expression set) { if (LuaEmit.IsDynamicType(instance.Type)) // first call the dynamic interface { return(DynamicExpression.Dynamic(lua.GetSetMemberBinder(memberName), typeof(object), ConvertObjectExpression(lua, tokenStart, instance, true), ConvertObjectExpression(lua, tokenStart, set, true) )); } else { Expression result; switch (LuaEmit.TrySetMember(instance, instance.Type, memberName, false, (setType) => ConvertExpression(lua, tokenStart, set, setType), out result)) { case LuaTrySetMemberReturn.None: throw ParseError(tokenStart, LuaEmitException.GetMessageText(LuaEmitException.MemberNotFound, instance.Type.Name, memberName)); case LuaTrySetMemberReturn.NotWritable: throw ParseError(tokenStart, LuaEmitException.GetMessageText(LuaEmitException.CanNotWriteMember, instance.Type.Name, memberName)); case LuaTrySetMemberReturn.ValidExpression: return(result); default: throw new ArgumentException("Internal return type of TrySetMember"); } } } // func MemberSetExpressionCore
} // ctor public override DynamicMetaObject FallbackBinaryOperation(DynamicMetaObject target, DynamicMetaObject arg, DynamicMetaObject errorSuggestion) { // defer target and all arguments if (!target.HasValue || !arg.HasValue) { return(Defer(target, arg)); } Expression expr; try { expr = EnsureType(LuaEmit.BinaryOperationExpression(lua, lInteger && Operation == ExpressionType.Divide ? Lua.IntegerDivide : Operation, target.Expression, target.LimitType, arg.Expression, arg.LimitType, false), this.ReturnType); } catch (LuaEmitException e) { if (errorSuggestion != null) { return(errorSuggestion); } expr = ThrowExpression(e.Message, this.ReturnType); } // restrictions var restrictions = target.Restrictions .Merge(arg.Restrictions) .Merge(Lua.GetSimpleRestriction(target)) .Merge(Lua.GetSimpleRestriction(arg)); return(new DynamicMetaObject(expr, restrictions)); } // func FallbackBinaryOperation
} // func InvokeMemberExpressionDynamic private static Expression EnsureInvokeResult(Scope scope, Token tStart, Expression expr, InvokeResult result, Expression instance, string memberName) { switch (result) { case InvokeResult.LuaResult: if (expr.Type == typeof(object)) { return(ConvertExpression(scope.Runtime, tStart, expr, typeof(LuaResult))); } else { return(MemberGetSandbox(scope, expr, instance, memberName)); } case InvokeResult.Object: if (LuaEmit.IsDynamicType(expr.Type)) { return(MemberGetSandbox(scope, DynamicExpression.Dynamic(scope.Runtime.GetConvertBinder(typeof(object)), typeof(object), ConvertExpression(scope.Runtime, tStart, expr, typeof(object))), instance, memberName)); } else { return(MemberGetSandbox(scope, expr, instance, memberName)); } default: return(MemberGetSandbox(scope, expr, instance, memberName)); } } // func EnsureInvokeResult
} // func InvokeExpression private static Expression InvokeMemberExpression(Scope scope, Token tStart, Expression instance, string sMember, InvokeResult result, ArgumentsList arguments) { if (LuaEmit.IsDynamicType(instance.Type)) { return(EnsureInvokeResult(scope, tStart, DynamicExpression.Dynamic(scope.Runtime.GetInvokeMemberBinder(sMember, arguments.CallInfo), typeof(object), new Expression[] { ConvertExpression(scope.Runtime, tStart, instance, typeof(object)) }.Concat( from c in arguments.Expressions select Lua.EnsureType(c, typeof(object)) ).ToArray() ), result, instance, sMember )); } else { // look up the method MethodInfo method = LuaEmit.FindMethod( LuaType.GetType(instance.Type).GetInstanceMethods(sMember, false), arguments.CallInfo, arguments.Expressions, getExpressionTypeFunction, true); if (method != null) { return(EnsureInvokeResult(scope, tStart, SafeExpression(() => InvokeMemberExpressionBind(method, scope.Runtime, instance, arguments), tStart), result, instance, sMember)); } else { return(InvokeMemberExpressionDynamic(scope, tStart, instance, sMember, result, arguments)); } } } // func InvokeMemberExpression
} // func IndexSetExpression private static Expression InvokeExpression(Scope scope, Token tStart, Expression instance, InvokeResult result, ArgumentsList arguments, bool lParse) { MethodInfo mi; ConstantExpression constInstance = instance as ConstantExpression; LuaType t; if (constInstance != null && (t = constInstance.Value as LuaType) != null && t.Type != null) // we have a type, bind the ctor { var type = t.Type; var typeInfo = type.GetTypeInfo(); var ci = typeInfo.IsValueType && arguments.Count == 0 ? null : LuaEmit.FindMember(typeInfo.DeclaredConstructors.Where(c => c.IsPublic), arguments.CallInfo, arguments.Expressions, getExpressionTypeFunction, false); if (ci == null && !typeInfo.IsValueType) { throw ParseError(tStart, String.Format(Properties.Resources.rsMemberNotResolved, type.Name, "ctor")); } return(SafeExpression(() => LuaEmit.BindParameter(scope.Runtime, args => ci == null ? Expression.New(type) : Expression.New(ci, args), ci == null ? new ParameterInfo[0] : ci.GetParameters(), arguments.CallInfo, arguments.Expressions, getExpressionFunction, getExpressionTypeFunction, true), tStart)); } else if (LuaEmit.IsDynamicType(instance.Type)) { // fallback is a dynamic call return(EnsureInvokeResult(scope, tStart, DynamicExpression.Dynamic(scope.Runtime.GetInvokeBinder(arguments.CallInfo), typeof(object), new Expression[] { ConvertExpression(scope.Runtime, tStart, instance, typeof(object)) }.Concat( from c in arguments.Expressions select Lua.EnsureType(c, typeof(object)) ) ), result, instance, null )); } else if (typeof(Delegate).GetTypeInfo().IsAssignableFrom(instance.Type.GetTypeInfo()) && // test if the type is assignable from delegate (mi = instance.Type.GetRuntimeMethods().Where(c => !c.IsStatic && c.IsPublic && c.Name == "Invoke").FirstOrDefault()) != null) // Search the Invoke method for the arguments { return(EnsureInvokeResult(scope, tStart, SafeExpression(() => LuaEmit.BindParameter <Expression>( scope.Runtime, args => Expression.Invoke(instance, args), mi.GetParameters(), arguments.CallInfo, arguments.Expressions, getExpressionFunction, getExpressionTypeFunction, true), tStart), result, instance, null )); } else { throw ParseError(tStart, LuaEmitException.GetMessageText(LuaEmitException.InvokeNoDelegate, instance.Type.Name)); } } // func InvokeExpression
} // func GetResultExpression private static Expression UnaryOperationExpression(Lua runtime, Token tStart, ExpressionType op, Expression expr) { if (op != ExpressionType.ArrayLength) { expr = ConvertObjectExpression(runtime, tStart, expr); } return(SafeExpression(() => LuaEmit.UnaryOperationExpression(runtime, op, expr, expr.Type, true), tStart)); } // func UnaryOperationExpression
} // func GetTargetDynamicCall private DynamicMetaObject GetTargetDynamicCall(CallSiteBinder binder, Type typeReturn, DynamicMetaObject arg) { return(GetTargetDynamicCall(binder, typeReturn, new Expression[] { GetFirstResultExpression(), LuaEmit.Convert(Lua.GetRuntime(binder), arg.Expression, arg.LimitType, typeof(object), false) } )); } // func GetTargetDynamicCall
} // proc ReadNumber private bool IsFileIndex(object v) { if (v == null) { return(false); } LuaEmitTypeCode tc = LuaEmit.GetTypeCode(v.GetType()); return(tc >= LuaEmitTypeCode.SByte && tc <= LuaEmitTypeCode.UInt32); } // func IsFileIndex
} // func ConvertObjectExpression private static Expression ConvertExpression(Lua runtime, Token tokenStart, Expression expr, Type toType) { // dynamic convert if (LuaEmit.TryConvert(expr, expr.Type, toType, runtime.GetConvertBinder, out var result)) { return((Expression)result); } else { throw ParseError(tokenStart, ((LuaEmitException)result).Message); } } // func ConvertExpression
} // ctor public override DynamicMetaObject FallbackGetMember(DynamicMetaObject target, DynamicMetaObject errorSuggestion) { // defer the target, to get the type if (!target.HasValue) { return(Defer(target)); } if (target.Value == null) { return(errorSuggestion ?? new DynamicMetaObject( ThrowExpression(Properties.Resources.rsNullReference, ReturnType), target.Restrictions.Merge(BindingRestrictions.GetInstanceRestriction(target.Expression, null)) )); } else { // try to bind the member Expression expr; try { expr = EnsureType(LuaEmit.GetMember(lua, target.Expression, target.LimitType, Name, IgnoreCase, false), ReturnType); /* * Fallback-Calls * first it get calls with no error suggestion -> return the default value * second call is to create a bind expression again, with the default value suggest * Example: DynamicObject - creates something like * * fallback(TryGetMember(binder, out result) ? result : fallback(null)); * */ if (expr.NodeType == ExpressionType.Default && errorSuggestion != null) { return(errorSuggestion); } } catch (LuaEmitException e) { if (errorSuggestion != null) { return(errorSuggestion); } expr = ThrowExpression(e.Message, ReturnType); } // restrictions var restrictions = target.Restrictions.Merge(BindingRestrictions.GetTypeRestriction(target.Expression, target.LimitType)); return(new DynamicMetaObject(expr, restrictions)); } } // func FallbackGetMember
} // func BinaryOperationExpression private static Expression ConcatOperationExpression(Lua runtime, Token tStart, Expression[] args) { if (Array.Exists(args, c => LuaEmit.IsDynamicType(c.Type))) // we have a dynamic type in the list -> to the concat on runtime { return(SafeExpression(() => Expression.Call(Lua.ConcatStringMethodInfo, Expression.NewArrayInit(typeof(object), from e in args select ConvertObjectExpression(runtime, tStart, e, true))), tStart)); } else { return(SafeExpression(() => Expression.Call(Lua.StringConcatMethodInfo, Expression.NewArrayInit(typeof(string), from e in args select LuaEmit.Convert(runtime, e, e.Type, typeof(string), true))), tStart)); } } // func ConcatOperationExpression
} // func SafeExpression private static Expression ConvertExpressionNoneEmit(Lua runtime, Token tokenStart, Expression expr, Type toType, bool forceType) { expr = LuaEmit.ConvertToSingleResultExpression(expr, null, toType, runtime.GetConvertBinder); if (forceType) // force { return(Lua.EnsureType(expr, typeof(object))); } else { return(expr); } } // func ConvertExpressionNoneEmit
} // ctor public override DynamicMetaObject FallbackConvert(DynamicMetaObject target, DynamicMetaObject errorSuggestion) { if (!target.HasValue) { return(Defer(target)); } if (target.Value == null) // get the default value { Expression expr; var restrictions = target.Restrictions.Merge(BindingRestrictions.GetInstanceRestriction(target.Expression, null)); if (Type == typeof(LuaResult)) // replace null with empty LuaResult { expr = Expression.Property(null, Lua.ResultEmptyPropertyInfo); } else if (Type == typeof(string)) // replace null with empty String { expr = Expression.Field(null, Lua.StringEmptyFieldInfo); } else { expr = Expression.Default(Type); } return(new DynamicMetaObject(Lua.EnsureType(expr, ReturnType), restrictions)); } else // convert the value { var restrictions = target.Restrictions.Merge(BindingRestrictions.GetTypeRestriction(target.Expression, target.LimitType)); object result; if (LuaEmit.TryConvert(target.Expression, target.LimitType, Type, null, out result)) { return(new DynamicMetaObject(Lua.EnsureType((Expression)result, ReturnType), restrictions)); } else if (errorSuggestion == null) { if (result == null) { throw new ArgumentNullException("expr", "LuaEmit.TryConvert does not return a expression."); } return(new DynamicMetaObject(ThrowExpression(((LuaEmitException)result).Message, ReturnType), restrictions)); } else { return(errorSuggestion); } } } // func FallbackConvert
} // func InvokeExpression private static Expression InvokeMemberExpression(Scope scope, Token tStart, Expression instance, string memberName, InvokeResult result, ArgumentsList arguments) { if (LuaEmit.IsDynamicType(instance.Type) || arguments.Expressions.Any(c => LuaEmit.IsDynamicType(c.Type))) { var dynamicArguments = new Expression[arguments.Count + 1]; // first argument is the instance dynamicArguments[0] = ConvertObjectExpression(scope.Runtime, tStart, instance, false); if (arguments.Count > 0) { // single object for (var i = 0; i < arguments.Count - 1; i++) { dynamicArguments[i + 1] = ConvertObjectExpression(scope.Runtime, tStart, arguments.Expressions[i], false); } // last argument is different if (arguments.CallInfo.ArgumentNames.Count > 0) { dynamicArguments[dynamicArguments.Length - 1] = ConvertObjectExpression(scope.Runtime, tStart, arguments.Expressions[arguments.Count - 1], false); } else { dynamicArguments[dynamicArguments.Length - 1] = Lua.EnsureType(arguments.Expressions[arguments.Count - 1], typeof(object)); } } return(EnsureInvokeResult(scope, tStart, DynamicExpression.Dynamic(scope.Runtime.GetInvokeMemberBinder(memberName, arguments.CallInfo), typeof(object), dynamicArguments), result, instance, memberName )); } else { return(EnsureInvokeResult(scope, tStart, SafeExpression(() => { Expression expr; if (!LuaEmit.TryInvokeMember <Expression>(scope.Runtime, LuaType.GetType(instance.Type), instance, arguments.CallInfo, arguments.Expressions, memberName, false, e => e, e => e.Type, true, out expr)) { throw new LuaEmitException(LuaEmitException.MemberNotFound, instance.Type, memberName); } return expr; }, tStart), result, instance, memberName )); } } // func InvokeMemberExpression
} // func IndexGetExpression private static Expression IndexSetExpression(Lua runtime, Token tStart, Expression instance, Expression[] indexes, Expression set) { if (instance.Type == typeof(LuaTable)) { if (indexes.Length == 1) { var arg = indexes[0]; if (LuaTable.IsIndexKey(arg.Type)) // integer access { return(Expression.Call(instance, Lua.TableSetValueKeyIntMethodInfo, ConvertExpression(runtime, tStart, arg, typeof(int)), ConvertObjectExpression(runtime, tStart, set, true), Expression.Constant(false) )); } else if (arg.Type == typeof(string)) // member access { return(Expression.Call(instance, Lua.TableSetValueKeyStringMethodInfo, arg, ConvertObjectExpression(runtime, tStart, set, true), Expression.Constant(false), Expression.Constant(false) )); } else // key access { return(Expression.Call(instance, Lua.TableSetValueKeyObjectMethodInfo, ConvertObjectExpression(runtime, tStart, arg, true), ConvertObjectExpression(runtime, tStart, set, true), Expression.Constant(false) )); } } else { return(Expression.Call(instance, Lua.TableSetValueKeyListMethodInfo, Expression.NewArrayInit(typeof(object), from i in indexes select ConvertObjectExpression(runtime, tStart, i, true)), ConvertObjectExpression(runtime, tStart, set, true), Expression.Constant(false) )); } } else { return(SafeExpression(() => LuaEmit.SetIndex(runtime, instance, indexes, set, getExpressionFunction, getExpressionTypeFunction, true), tStart)); } } // func IndexSetExpression
} // func MemberGetExpression private static Expression MemberSetExpression(Lua runtime, Token tStart, Expression instance, string sMember, bool lMethodMember, Expression set) { // Assign the value to a member if (lMethodMember) { return(Expression.Call( ConvertExpression(runtime, tStart, instance, typeof(LuaTable)), Lua.TableDefineMethodLightMethodInfo, Expression.Constant(sMember, typeof(string)), ConvertExpression(runtime, tStart, set, typeof(Delegate)) )); } else { return(SafeExpression(() => LuaEmit.SetMember(runtime, instance, instance.Type, sMember, false, set, set.Type, true), tStart)); } } // func MemberSetExpression
} // ctor public override DynamicMetaObject FallbackConvert(DynamicMetaObject target, DynamicMetaObject errorSuggestion) { if (!target.HasValue) { return(Defer(target)); } Expression expr = target.Expression; BindingRestrictions restrictions = null; if (target.Value == null) // get the default value { restrictions = target.Restrictions.Merge(BindingRestrictions.GetInstanceRestriction(target.Expression, null)); if (this.Type == typeof(LuaResult)) // replace null with empty LuaResult { expr = Expression.Property(null, Lua.ResultEmptyPropertyInfo); } else if (this.Type == typeof(string)) // replace null with empty String { expr = Expression.Field(null, Lua.StringEmptyFieldInfo); } else { expr = Expression.Default(this.Type); } } else // convert the value { restrictions = target.Restrictions.Merge(BindingRestrictions.GetTypeRestriction(target.Expression, target.LimitType)); try { expr = LuaEmit.Convert(lua, target.Expression, target.LimitType, this.Type, false); // no dynamic allowed } catch (LuaEmitException e) { if (errorSuggestion != null) { return(errorSuggestion); } expr = ThrowExpression(e.Message, this.Type); } } return(new DynamicMetaObject(expr, restrictions)); } // func FallbackConvert
/// <summary> /// Determines whether the specified value is positive. /// </summary> /// <param name="Value">The value.</param> /// <param name="ZeroIsPositive">if set to <c>true</c> treats 0 as positive.</param> /// <returns> /// <c>true</c> if the specified value is positive; otherwise, <c>false</c>. /// </returns> public static bool IsPositive(object Value, bool ZeroIsPositive) { switch (LuaEmit.GetTypeCode(Value.GetType())) { case LuaEmitTypeCode.SByte: return(ZeroIsPositive ? (sbyte)Value >= 0 : (sbyte)Value > 0); case LuaEmitTypeCode.Int16: return(ZeroIsPositive ? (short)Value >= 0 : (short)Value > 0); case LuaEmitTypeCode.Int32: return(ZeroIsPositive ? (int)Value >= 0 : (int)Value > 0); case LuaEmitTypeCode.Int64: return(ZeroIsPositive ? (long)Value >= 0 : (long)Value > 0); case LuaEmitTypeCode.Single: return(ZeroIsPositive ? (float)Value >= 0 : (float)Value > 0); case LuaEmitTypeCode.Double: return(ZeroIsPositive ? (double)Value >= 0 : (double)Value > 0); case LuaEmitTypeCode.Decimal: return(ZeroIsPositive ? (decimal)Value >= 0 : (decimal)Value > 0); case LuaEmitTypeCode.Byte: return(ZeroIsPositive ? true : (byte)Value > 0); case LuaEmitTypeCode.UInt16: return(ZeroIsPositive ? true : (ushort)Value > 0); case LuaEmitTypeCode.UInt32: return(ZeroIsPositive ? true : (uint)Value > 0); case LuaEmitTypeCode.UInt64: return(ZeroIsPositive ? true : (ulong)Value > 0); case LuaEmitTypeCode.Char: return(ZeroIsPositive ? true : (char)Value != '\0'); default: return(false); } }
} // ctor public override DynamicMetaObject FallbackSetMember(DynamicMetaObject target, DynamicMetaObject value, DynamicMetaObject errorSuggestion) { // defer the target if (!target.HasValue) { return(Defer(target)); } if (target.Value == null) { return(errorSuggestion ?? new DynamicMetaObject( ThrowExpression(String.Format(Resources.rsMemberNotResolved, target.LimitType.Name, Name), ReturnType), target.Restrictions.Merge(BindingRestrictions.GetInstanceRestriction(target.Expression, null)) )); } else { Expression expr; // restrictions var restrictions = target.Restrictions.Merge(BindingRestrictions.GetTypeRestriction(target.Expression, target.LimitType)); // try to bind the member switch (LuaEmit.TrySetMember(target.Expression, target.LimitType, Name, IgnoreCase, (setType) => LuaEmit.ConvertWithRuntime(Lua, value.Expression, value.LimitType, setType), out expr)) { case LuaTrySetMemberReturn.None: return(errorSuggestion ?? new DynamicMetaObject(ThrowExpression(LuaEmitException.GetMessageText(LuaEmitException.MemberNotFound, target.LimitType.Name, Name), ReturnType), restrictions)); case LuaTrySetMemberReturn.NotWritable: return(errorSuggestion ?? new DynamicMetaObject(ThrowExpression(LuaEmitException.GetMessageText(LuaEmitException.CanNotWriteMember, target.LimitType.Name, Name), ReturnType), restrictions)); case LuaTrySetMemberReturn.ValidExpression: return(new DynamicMetaObject(Lua.EnsureType(expr, ReturnType), restrictions.Merge(Lua.GetSimpleRestriction(value)))); default: throw new ArgumentException("return of TryGetMember."); } } } // func FallbackSetMember
} // func FallbackInvoke public override DynamicMetaObject FallbackInvokeMember(DynamicMetaObject target, DynamicMetaObject[] args, DynamicMetaObject errorSuggestion) { // defer target and all arguments if (!target.HasValue || args.Any(c => !c.HasValue)) { return(Defer(target, args)); } if (target.Value == null) { return(errorSuggestion ?? new DynamicMetaObject( ThrowExpression(Resources.rsNilNotCallable, ReturnType), target.Restrictions.Merge(BindingRestrictions.GetInstanceRestriction(target.Expression, null)) )); } else { try { var luaType = LuaType.GetType(target.LimitType); Expression expr; if (LuaEmit.TryInvokeMember <DynamicMetaObject>(lua, luaType, target, CallInfo, args, Name, IgnoreCase, mo => mo.Expression, mo => mo.LimitType, false, out expr)) { return(new DynamicMetaObject(Lua.EnsureType(expr, ReturnType), GetMethodSignatureRestriction(target, args))); } else { return(errorSuggestion ?? new DynamicMetaObject (ThrowExpression(String.Format(Resources.rsMemberNotResolved, luaType.FullName, Name), ReturnType), GetMethodSignatureRestriction(target, args) )); } } catch (LuaEmitException e) { return(errorSuggestion ?? new DynamicMetaObject(ThrowExpression(e.Message, ReturnType), GetMethodSignatureRestriction(target, args))); } } } // func FallbackInvokeMember
/// <summary> /// Converts the specified values boxed type to its correpsonding unsigned /// type. /// </summary> /// <param name="Value">The value.</param> /// <returns>A boxed numeric object whos type is unsigned.</returns> public static object ToUnsigned(object Value) { switch (LuaEmit.GetTypeCode(Value.GetType())) { case LuaEmitTypeCode.SByte: return((byte)((sbyte)Value)); case LuaEmitTypeCode.Int16: return((ushort)((short)Value)); case LuaEmitTypeCode.Int32: return((uint)((int)Value)); case LuaEmitTypeCode.Int64: return((ulong)((long)Value)); case LuaEmitTypeCode.Byte: return(Value); case LuaEmitTypeCode.UInt16: return(Value); case LuaEmitTypeCode.UInt32: return(Value); case LuaEmitTypeCode.UInt64: return(Value); case LuaEmitTypeCode.Single: return((UInt32)((float)Value)); case LuaEmitTypeCode.Double: return((ulong)((double)Value)); case LuaEmitTypeCode.Decimal: return((ulong)((decimal)Value)); default: return(null); } }
/// <summary> /// Converts the specified values boxed type to its correpsonding integer /// type. /// </summary> /// <param name="Value">The value.</param> /// <param name="Round"></param> /// <returns>A boxed numeric object whos type is an integer type.</returns> public static object ToInteger(object Value, bool Round) { switch (LuaEmit.GetTypeCode(Value.GetType())) { case LuaEmitTypeCode.SByte: return(Value); case LuaEmitTypeCode.Int16: return(Value); case LuaEmitTypeCode.Int32: return(Value); case LuaEmitTypeCode.Int64: return(Value); case LuaEmitTypeCode.Byte: return(Value); case LuaEmitTypeCode.UInt16: return(Value); case LuaEmitTypeCode.UInt32: return(Value); case LuaEmitTypeCode.UInt64: return(Value); case LuaEmitTypeCode.Single: return(Round ? (int)Math.Round((float)Value) : (int)((float)Value)); case LuaEmitTypeCode.Double: return(Round ? (long)Math.Round((double)Value) : (long)((double)Value)); case LuaEmitTypeCode.Decimal: return(Round ? Math.Round((decimal)Value) : (decimal)Value); default: return(null); } }
public static long UnboxToLong(object Value, bool Round) { switch (LuaEmit.GetTypeCode(Value.GetType())) { case LuaEmitTypeCode.SByte: return((long)((sbyte)Value)); case LuaEmitTypeCode.Int16: return((long)((short)Value)); case LuaEmitTypeCode.Int32: return((long)((int)Value)); case LuaEmitTypeCode.Int64: return((long)Value); case LuaEmitTypeCode.Byte: return((long)((byte)Value)); case LuaEmitTypeCode.UInt16: return((long)((ushort)Value)); case LuaEmitTypeCode.UInt32: return((long)((uint)Value)); case LuaEmitTypeCode.UInt64: return((long)((ulong)Value)); case LuaEmitTypeCode.Single: return(Round ? (long)Math.Round((float)Value) : (long)((float)Value)); case LuaEmitTypeCode.Double: return(Round ? (long)Math.Round((double)Value) : (long)((double)Value)); case LuaEmitTypeCode.Decimal: return(Round ? (long)Math.Round((decimal)Value) : (long)((decimal)Value)); default: return(0); } }
private object LuaToNumber(object v, Nullable <int> iBase = null) { if (v == null) { return(null); } else { switch (LuaEmit.GetTypeCode(v.GetType())) { case LuaEmitTypeCode.String: if (iBase.HasValue) { return(Lua.RtParseNumber(null, (string)v, 0, iBase.Value, lua.FloatType == LuaFloatType.Double, false)); } else { return(Lua.RtParseNumber((string)v, lua.FloatType == LuaFloatType.Double, false)); } case LuaEmitTypeCode.SByte: case LuaEmitTypeCode.Byte: case LuaEmitTypeCode.Int16: case LuaEmitTypeCode.UInt16: case LuaEmitTypeCode.Int32: case LuaEmitTypeCode.UInt32: case LuaEmitTypeCode.Int64: case LuaEmitTypeCode.UInt64: case LuaEmitTypeCode.Single: case LuaEmitTypeCode.Double: case LuaEmitTypeCode.Decimal: return(v); case LuaEmitTypeCode.Boolean: return((bool)v ? 1 : 0); default: return(null); } } } // func LuaToNumber
} // func InvokeMemberExpression private static Expression InvokeMemberExpressionBind(MethodInfo method, Lua runtime, Expression instance, Expression[] arguments) { if (method.IsStatic) { return(LuaEmit.BindParameter(runtime, args => Expression.Call(null, method, args), method.GetParameters(), new Expression[] { instance }.Concat( from a in arguments select Lua.EnsureType(a, typeof(object)) ).ToArray(), getExpressionFunction, getExpressionTypeFunction, true)); } else { return(LuaEmit.BindParameter(runtime, args => Expression.Call(instance, method, args), method.GetParameters(), arguments, getExpressionFunction, getExpressionTypeFunction, true)); } } // func InvokeMemberExpressionBind
} // func RestrictAccess /// <summary>Most core method, that gets called to sandbox a value.</summary> /// <param name="expression">Expression, that should be sandboxed.</param> /// <param name="instance">Optional: Instance, that was called to get the expression.</param> /// <param name="sMember">Optional: Name of the member that was used to resolve the expression.</param> /// <returns>Sandboxed expression</returns> protected internal virtual Expression SandboxCore(Expression expression, Expression instance, string sMember) { switch (Sandbox(expression.Type, instance == null ? null : instance.Type, sMember)) { case LuaSandboxResult.Dynamic: if (DynamicSandbox == null) { return(expression); } else { return(LuaEmit.Convert(null, Expression.Invoke(Expression.Constant(DynamicSandbox), expression), typeof(object), expression.Type, false)); } case LuaSandboxResult.Restrict: return(RestrictAccess()); default: return(expression); } } // func SandboxCore
} // ctor public override DynamicMetaObject FallbackGetMember(DynamicMetaObject target, DynamicMetaObject errorSuggestion) { // defer the target, to get the type if (!target.HasValue) { return(Defer(target)); } if (target.Value == null) // no value for target, finish binding with an error or the suggestion { return(errorSuggestion ?? new DynamicMetaObject( ThrowExpression(Resources.rsNullReference, ReturnType), target.Restrictions.Merge(BindingRestrictions.GetInstanceRestriction(target.Expression, null)) )); } else { Expression expr; // restrictions var restrictions = target.Restrictions.Merge(BindingRestrictions.GetTypeRestriction(target.Expression, target.LimitType)); // try to bind the member switch (LuaEmit.TryGetMember(target.Expression, target.LimitType, Name, IgnoreCase, out expr)) { case LuaTryGetMemberReturn.None: return(errorSuggestion ?? new DynamicMetaObject(Expression.Default(ReturnType), restrictions)); case LuaTryGetMemberReturn.NotReadable: return(errorSuggestion ?? new DynamicMetaObject(ThrowExpression(LuaEmitException.GetMessageText(LuaEmitException.CanNotReadMember, target.LimitType.Name, Name), ReturnType), restrictions)); case LuaTryGetMemberReturn.ValidExpression: return(new DynamicMetaObject(Lua.EnsureType(expr, ReturnType), restrictions)); default: throw new ArgumentException("return of TryGetMember."); } } } // func FallbackGetMember