Exemple #1
0
        public override bool TryInvoke(InvokeBinder binder, object[] args, out object result)
        {
            for (var i = 0; i < args.Length; i++)
            {
                args[i] = LuaHelper.WrapObject(args[i], _state);
            }

            var func = GetMetaFunction("call");

            if (func != null)
            {
                if (args.Length == 0)
                {
                    result = new DynamicArray(func.Call(_table), _state);
                }
                else
                {
                    result = new DynamicArray(func.Call(new object[] { _table }.Concat(args).ToArray()), _state);
                }

                return(true);
            }
            else
            {
                result = null;
                return(false);
            }
        }
Exemple #2
0
        private void SetLuaMember(string name, object value)
        {
            var 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;
            }
        }
Exemple #3
0
        public dynamic Power(object operand)
        {
            operand = LuaHelper.WrapObject(operand, _state);

            var func = GetMetaFunction("pow");

            if (func != null)
            {
                return(func.Call(_table, operand));
            }

            throw new InvalidOperationException("Metamethod __pow not found");
        }
Exemple #4
0
        private void SetTableMember(string name, object value)
        {
            var tmp = LuaHelper.WrapObject(value, _state, _path + "." + name);

            if (tmp == null)
            {
                return;
            }

            var func = GetMetaFunction("newindex");

            if (func != null)
            {
                func.Call(_table, name, value);
            }
            else
            {
                _table[name] = value;
            }
        }
Exemple #5
0
        public override bool TryBinaryOperation(BinaryOperationBinder binder, object arg, out object result)
        {
            string metamethodName;

            var switchOperands = false; //Lua has only comparison metamethods for less, so for greater the we have to switch the operands
            var 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);
            }

            var mtFunc = GetMetaFunction(metamethodName);

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

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

            //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?)
            if (negateResult && result is bool b)
            {
                result = !b;
            }

            return(true);
        }