Example #1
0
        }         // 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
Example #2
0
            } // ctor

            public override DynamicMetaObject FallbackSetIndex(DynamicMetaObject target, DynamicMetaObject[] indexes, DynamicMetaObject value, DynamicMetaObject errorSuggestion)
            {
                // Defer the parameters
                if (!target.HasValue || indexes.Any(c => !c.HasValue))
                {
                    DynamicMetaObject[] def = new DynamicMetaObject[indexes.Length + 1];
                    def[0] = target;
                    Array.Copy(indexes, 0, def, 1, indexes.Length);
                    return(Defer(def));
                }

                Expression expr;

                if (target.Value == null)
                {
                    if (errorSuggestion != null)
                    {
                        return(errorSuggestion);
                    }
                    expr = ThrowExpression(Properties.Resources.rsNullReference, ReturnType);
                }
                else
                {
                    try
                    {
                        expr = Lua.EnsureType(LuaEmit.SetIndex(lua, target, indexes, value, mo => mo.Expression, mo => mo.LimitType, false), ReturnType);
                    }
                    catch (LuaEmitException e)
                    {
                        if (errorSuggestion != null)
                        {
                            return(errorSuggestion);
                        }
                        expr = ThrowExpression(e.Message, ReturnType);
                    }
                }

                return(new DynamicMetaObject(expr, GetMethodSignatureRestriction(target, indexes).Merge(Lua.GetSimpleRestriction(value))));
            } // func FallbackSetIndex