WrapObject() public static method

Wraps an object to prepare it for passing to LuaInterface. If no name is specified, a random one with the default length is used.
public static WrapObject ( object toWrap, NLua.Lua state, string name = null ) : object
toWrap object
state NLua.Lua
name string
return object
コード例 #1
0
        public override bool TryInvoke(InvokeBinder binder, object[] args, out object result)
        {
            for (int i = 0; i < args.Length; i++)
            {
                args[i] = LuaHelper.WrapObject(args[i], state);
            }

            LuaFunction func = GetMetaFunction("call");

            if (func != null)
            {
                if (args.Length == 0)
                {
                    result = new DynamicArray(func.Call(table), state);
                }
                else
                {
                    result = new DynamicArray(func.Call(table, args), state);
                }
                return(true);
            }
            else
            {
                result = null;
                return(false);
            }
        }
コード例 #2
0
ファイル: DynamicLua.cs プロジェクト: hakanaku2009/svn-dump
        private void SetLuaMember(string name, object value)
        {
            object tmp = LuaHelper.WrapObject(value, lua, name);

            if (tmp != null) //if a function was registered tmp is null, but we dont want to nil the function :P
            {
                lua[name] = tmp;
            }
        }
コード例 #3
0
        /// <summary>
        /// Performs the Power operation on this table. This is only
        /// needed for languages like C# which have no native operators
        /// for this. VB etc. users should use the build-in a^b operator.
        /// </summary>
        public dynamic Power(object operand)
        {
            operand = LuaHelper.WrapObject(operand, state);

            LuaFunction func = GetMetaFunction("pow");

            if (func != null)
            {
                return(func.Call(table, operand));
            }
            else
            {
                throw new InvalidOperationException("Metamethod __pow not found");
            }
        }
コード例 #4
0
        private void SetTableMember(string name, object value)
        {
            object tmp = LuaHelper.WrapObject(value, state, path + "." + name);

            if (tmp != null) //if a function was registered tmp is null, but we dont want to nil the function :P
            {
                LuaFunction func = GetMetaFunction("newindex");
                if (func != null)
                {
                    func.Call(table, name, value);
                }
                else
                {
                    table[name] = value;
                }
            }
        }
コード例 #5
0
        public override bool TryBinaryOperation(BinaryOperationBinder binder, object arg, out object result)
        {
            string metamethodName;
            bool   switchOperands = false; //Lua has only comparison metamethods for less, so for greater the we have to switch the operands
            bool   negateResult   = false; //Lua has only metamethods for equal, so we need this trick here.

            switch (binder.Operation)
            {
            //Math
            case ExpressionType.Add:
            case ExpressionType.AddChecked:
            case ExpressionType.AddAssign:
            case ExpressionType.AddAssignChecked:     //TODO: Thing about __concat
                metamethodName = "add";
                break;

            case ExpressionType.Subtract:
            case ExpressionType.SubtractChecked:
            case ExpressionType.SubtractAssign:
            case ExpressionType.SubtractAssignChecked:
                metamethodName = "sub";
                break;

            case ExpressionType.Multiply:
            case ExpressionType.MultiplyChecked:
            case ExpressionType.MultiplyAssign:
            case ExpressionType.MultiplyAssignChecked:
                metamethodName = "mul";
                break;

            case ExpressionType.Divide:
            case ExpressionType.DivideAssign:
                metamethodName = "div";
                break;

            case ExpressionType.Modulo:
            case ExpressionType.ModuloAssign:
                metamethodName = "mod";
                break;

            case ExpressionType.Power:
            case ExpressionType.PowerAssign:
                metamethodName = "pow";
                break;

            //Logic Tests
            case ExpressionType.Equal:
                metamethodName = "eq";
                break;

            case ExpressionType.NotEqual:
                metamethodName = "eq";
                negateResult   = true;
                break;

            case ExpressionType.GreaterThan:
                metamethodName = "lt";
                switchOperands = true;
                break;

            case ExpressionType.GreaterThanOrEqual:
                metamethodName = "le";
                switchOperands = true;
                break;

            case ExpressionType.LessThan:
                metamethodName = "lt";
                break;

            case ExpressionType.LessThanOrEqual:
                metamethodName = "le";
                break;

            default:     //This operation is not supported by Lua...
                result = null;
                return(false);
            }

            LuaFunction mtFunc = GetMetaFunction(metamethodName);

            if (mtFunc == null)
            {
                result = null;
                return(false);
            }

            if (!switchOperands)
            {
                result = mtFunc.Call(table, LuaHelper.WrapObject(arg, state))[0]; //Metamethods just return one value, or the other will be ignored anyway
            }
            else
            {
                result = mtFunc.Call(LuaHelper.WrapObject(arg, state), table)[0];
            }

            if (negateResult && result is bool) //We can't negate if its not bool. If the metamethod returned someting other than bool and ~= is called there will be a bug. (But who would do this?)
            {
                result = !(bool)result;
            }

            return(true);
        }