Exemplo n.º 1
0
 static ILuaMultiValue _getStream(ILuaValue file, ILuaEnvironment E, out Stream s)
 {
     s = null;
     if (file == LuaNil.Nil)
     {
         if (_output == null)
         {
             return(E.Runtime.CreateMultiValueFromObj(null, "No default output file set."));
         }
         s = _output;
     }
     else
     {
         if (file.ValueType == LuaValueType.Table)
         {
             s = ((ILuaTable)file).GetItemRaw(_stream).GetValue() as Stream;
             if (s == null)
             {
                 return(E.Runtime.CreateMultiValueFromObj(null, "Specified argument is not a valid file stream."));
             }
         }
         else if (file.ValueType == LuaValueType.UserData)
         {
             s = file.GetValue() as Stream;
         }
         else
         {
             return(E.Runtime.CreateMultiValueFromObj(null, "Specified argument is not a valid file stream."));
         }
     }
     return(null);
 }
Exemplo n.º 2
0
        /// <summary>
        /// Defines basic arithmetic for derived classes.  This returns a
        /// non-null value when default, or null if it is a visitor.  This
        /// throws if not a visitor.
        /// </summary>
        /// <param name="type">The type of operation to perform.</param>
        /// <param name="other">The other value to use.</param>
        /// <returns>The result of the operation.</returns>
        /// <exception cref="System.InvalidOperationException">
        /// If the operation cannot be performed with the given values.
        /// </exception>
        /// <exception cref="System.InvalidArgumentException">
        /// If the argument is an invalid value.
        /// </exception>
        protected ILuaValue ArithmeticBase(BinaryOperationType type, ILuaValue other)
        {
            // Attempt to use a meta-method.
            var ret = AttempMetamethod(type, this, other);

            if (ret != null)
            {
                return(ret);
            }

            // Do some default operations.
            ret = DefaultArithmetic(type, other);
            if (ret != null)
            {
                return(ret);
            }

            // If the other is not a visitor, throw.
            if (!(other is ILuaValueVisitor))
            {
                throw new InvalidOperationException(Errors.CannotArithmetic(this.ValueType));
            }
            else
            {
                return(null);
            }
        }
Exemplo n.º 3
0
                protected override ILuaMultiValue _invokeInternal(ILuaMultiValue args)
                {
                    if (args.Count < 2)
                    {
                        throw new ArgumentException("Expecting at least two arguments to function 'overload'.");
                    }

                    ILuaValue meth = args[0];
                    ILuaValue obj  = args[1];

                    if (meth.ValueType != LuaValueType.Function)
                    {
                        throw new ArgumentException("First argument to function 'overload' must be a method.");
                    }

                    if (obj.ValueType != LuaValueType.Number || ((double)obj.GetValue() % 1 != 0))
                    {
                        throw new ArgumentException(
                                  "Second argument to function 'overload' must be an integer.");
                    }

                    int i = Convert.ToInt32((double)obj.GetValue());

                    return(meth.Invoke(null, false, i,
                                       _environment.Runtime.CreateMultiValue(args.Skip(2).ToArray())));
                }
Exemplo n.º 4
0
            void insert(ILuaTable table, ILuaValue pos, ILuaValue value = null)
            {
                CheckNotNull("table.insert", table);
                CheckNotNull("table.insert", pos);
                double i;
                double len = table.Length().AsDouble() ?? 0;

                if (value == null)
                {
                    value = pos;
                    i     = len + 1;
                }
                else
                {
                    i = pos.AsDouble() ?? 0;
                }

                if (i > len + 1 || i < 1 || i % 1 != 0)
                {
                    throw new ArgumentException(
                              "Position given to function 'table.insert' is outside valid range.");
                }

                for (double d = len; d >= i; d--)
                {
                    var temp = table.GetItemRaw(E.Runtime.CreateValue(d));
                    table.SetItemRaw(E.Runtime.CreateValue(d + 1), temp);
                }
                table.SetItemRaw(pos, value);
            }
Exemplo n.º 5
0
        protected override ILuaMultiValue _invokeInternal(ILuaValue target, bool memberCall,
                                                          int overload, ILuaMultiValue args)
        {
            ILuaValue method = (ILuaValue)Activator.CreateInstance(_type, new[] { _env });

            return(method.Invoke(LuaNil.Nil, false, -1, args));
        }
Exemplo n.º 6
0
            string concat(ILuaTable table, string sep = null, int i = 1, int j = -1)
            {
                CheckNotNull("table.concat", table);
                int len = (int)(table.Length().AsDouble() ?? 0);

                if (i >= len)
                {
                    return("");
                }

                i = normalizeIndex_(len, i);
                j = normalizeIndex_(len, j);

                StringBuilder str = new StringBuilder();

                for (; i <= j; i++)
                {
                    ILuaValue temp = table.GetItemRaw(E.Runtime.CreateValue(i));
                    if (temp.ValueType != LuaValueType.String && temp.ValueType != LuaValueType.Number)
                    {
                        throw new ArgumentException(
                                  "Invalid '" + temp.ValueType + "' value for function 'table.concat'.");
                    }

                    if (str.Length > 0)
                    {
                        str.Append(sep);
                    }
                    str.Append(temp);
                }

                return(str.ToString());
            }
            ILuaValue create(ILuaValue method)
            {
                if (method.ValueType != LuaValueType.Function)
                    throw new ArgumentException("First argument to function 'coroutine.create' must be a function.");

                return E_.Runtime.CreateThread(method);
            }
Exemplo n.º 8
0
                protected override ILuaMultiValue InvokeInternal(ILuaMultiValue args)
                {
                    ILuaValue obj = args[0];

                    if (obj != LuaNil.Nil)
                    {
                        if (obj.ValueType == LuaValueType.String)
                        {
                            Stream s = File.OpenRead(obj.GetValue() as string);
                            _output = s;
                        }
                        else if (obj.ValueType == LuaValueType.Table)
                        {
                            Stream s = ((ILuaTable)obj).GetItemRaw(_stream) as Stream;
                            if (s == null)
                            {
                                throw new InvalidOperationException("First argument to function 'io.output' must be a file-stream or a string path.");
                            }

                            _output = s;
                        }
                        else if (obj is Stream)
                        {
                            _output = obj as Stream;
                        }
                        else
                        {
                            throw new InvalidOperationException("First argument to function 'io.output' must be a file-stream or a string path.");
                        }
                    }

                    return(Environment.Runtime.CreateMultiValue(_CreateFile(_output, Environment)));
                }
Exemplo n.º 9
0
        static void Register(ILuaEnvironment env, ILuaValue table, Delegate func, string name = null)
        {
            var funcValue = env.Runtime.CreateValue(func);
            var nameValue = env.Runtime.CreateValue(name ?? func.Method.Name);

            table.SetIndex(nameValue, funcValue);
        }
Exemplo n.º 10
0
        public override ILuaMultiValue Invoke(ILuaValue self, bool memberCall, int overload, ILuaMultiValue args)
        {
            if (overload >= 0)
            {
                throw new NotSupportedException(string.Format(Resources.CannotUseOverload, "LuaType"));
            }

            if (args == null)
            {
                args = new LuaMultiValue();
            }

            Type t = Type;

            if (Helpers.GetCompatibleMethod(
                    t.GetConstructors()
                    .Where(c => c.GetCustomAttributes(typeof(LuaIgnoreAttribute), true).Length == 0)
                    .Select(c => Tuple.Create(c, (object)null)),
                    args, out ConstructorInfo method, out _))
            {
                object value = method.Invoke(Helpers.ConvertForArgs(args, method));
                return(LuaMultiValue.CreateMultiValueFromObj(value));
            }

            throw new InvalidOperationException(string.Format(Resources.CannotCall, "LuaType"));
        }
Exemplo n.º 11
0
                protected override ILuaMultiValue InvokeInternal(ILuaMultiValue args)
                {
                    ILuaValue obj = args[0];
                    Stream    s;
                    int       start = 0;

                    if (obj.ValueType == LuaValueType.Table)
                    {
                        s = ((ILuaTable)obj).GetItemRaw(_stream) as Stream;
                        if (s == null)
                        {
                            throw new ArgumentException("First argument to io.read must be a file-stream or a file path, make sure to use file:read.");
                        }
                        start = 1;
                    }
                    else if (obj.GetValue() is Stream)
                    {
                        s     = obj.GetValue() as Stream;
                        start = 1;
                    }
                    else
                    {
                        s     = _input;
                        start = 0;
                    }

                    int[] a = _parse(args.Skip(start), "io.read");
                    return(_read(a, new StreamReader(s), Environment));
                }
Exemplo n.º 12
0
        /// <summary>
        /// Performs a binary arithmetic operation and returns the result.
        /// </summary>
        /// <param name="type">The type of operation to perform.</param>
        /// <param name="other">The other value to use.</param>
        /// <returns>The result of the operation.</returns>
        /// <exception cref="System.InvalidOperationException">
        /// If the operation cannot be performed with the given values.
        /// </exception>
        /// <exception cref="System.InvalidArgumentException">
        /// If the argument is an invalid value.
        /// </exception>
        ILuaValue ILuaValue.Arithmetic(BinaryOperationType type, ILuaValue other)
        {
            // Attempt to use a meta-method.
            var ret = LuaValueBase.AttempMetamethod(type, this, other);

            if (ret != null)
            {
                return(ret);
            }

            // Do some default operations.
            ret = DefaultArithmetic(type, other);
            if (ret != null)
            {
                return(ret);
            }

            // If the other is not a visitor, throw.
            if (!(other is ILuaValueVisitor))
            {
                throw new InvalidOperationException(Errors.CannotArithmetic(LuaValueType.Function));
            }
            else
            {
                return(((ILuaValueVisitor)other).Arithmetic(type, this));
            }
        }
Exemplo n.º 13
0
        static ILuaValue _processType(Type t, ILuaEnvironment env)
        {
            if (!typeof(ILuaValue).IsAssignableFrom(t))
            {
                return(null);
            }

            ConstructorInfo ci = t.GetConstructor(new Type[0]);

            if (ci == null)
            {
                ci = t.GetConstructor(new Type[] { typeof(ILuaEnvironment) });
                if (ci != null)
                {
                    ILuaValue mod = (ILuaValue)ci.Invoke(new[] { env });
                    return(mod.Invoke(LuaNil.Nil, false, -1, env.Runtime.CreateMultiValue()).Single());
                }
                return(null);
            }
            else
            {
                ILuaValue mod = (ILuaValue)ci.Invoke(null);
                return(mod.Invoke(LuaNil.Nil, false, -1, env.Runtime.CreateMultiValue()).Single());
            }
        }
Exemplo n.º 14
0
        /// <summary>
        /// Helper function, sets the value at the given index.  Uses
        /// meta-methods if needed.
        /// </summary>
        /// <param name="index">The index to use.</param>
        /// <param name="value">The value to set to.</param>
        void Set(ILuaValue index, ILuaValue value)
        {
            index = index ?? LuaNil.Nil;
            value = value ?? LuaNil.Nil;

            ILuaValue ret;

            if (!_values.TryGetValue(index, out ret) && MetaTable != null)
            {
                var method = MetaTable.GetItemRaw(_newindex);
                if (method != null && method != LuaNil.Nil)
                {
                    if (method.ValueType == LuaValueType.Function)
                    {
                        method.Invoke(this, true, -1, new LuaMultiValue(index, value));
                    }
                    else
                    {
                        method.SetIndex(index, value);
                    }

                    return;
                }
            }

            SetItemRaw(index, value);
        }
Exemplo n.º 15
0
            public override void Assign(ILuaValue value)
            {
                LuaType type = value as LuaType;

                if (type != null)
                {
                    if (Type == null)
                    {
                        Type = type.Type;
                    }
                    else
                    {
                        throw new InvalidOperationException(string.Format(Resources.MemberHasType, Name));
                    }
                }
                else
                {
                    if (Default != null)
                    {
                        throw new InvalidOperationException(string.Format(Resources.MemberHasDefault, Name));
                    }

                    if (Type == null)
                    {
                        object temp = value == null ? value : value.GetValue();
                        Type = temp == null ? typeof(object) : temp.GetType();
                    }
                    Default = value;
                }
            }
Exemplo n.º 16
0
        /// <summary>
        /// Performs some default arithmetic like comparisons and returns the result.
        /// Returns null if there is no default.
        /// </summary>
        /// <param name="type">The type of operation to perform.</param>
        /// <param name="other">The other value to use.</param>
        /// <returns>The result of the operation.</returns>
        private ILuaValue DefaultArithmetic(BinaryOperationType type, ILuaValue other)
        {
            switch (type)
            {
            case BinaryOperationType.Concat:
                return(new LuaString(this.ToString() + other.ToString()));

            case BinaryOperationType.Gt:
                return(LuaBoolean.Create(CompareTo(other) > 0));

            case BinaryOperationType.Lt:
                return(LuaBoolean.Create(CompareTo(other) < 0));

            case BinaryOperationType.Gte:
                return(LuaBoolean.Create(CompareTo(other) >= 0));

            case BinaryOperationType.Lte:
                return(LuaBoolean.Create(CompareTo(other) <= 0));

            case BinaryOperationType.Equals:
                return(LuaBoolean.Create(Equals(other)));

            case BinaryOperationType.NotEquals:
                return(LuaBoolean.Create(!Equals(other)));

            case BinaryOperationType.And:
                return(other);

            case BinaryOperationType.Or:
                return(this);

            default:
                return(null);
            }
        }
Exemplo n.º 17
0
        public virtual void RegisterType(Type t, string name)
        {
            if (t == null)
            {
                throw new ArgumentNullException(nameof(t));
            }

            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            lock (this) {
                var       n = new LuaString(name);
                ILuaValue o = GlobalsTable.GetItemRaw(n);
                if (o != LuaNil.Nil)
                {
                    throw new ArgumentException(string.Format(Resources.AlreadyRegistered, name));
                }
                else
                {
                    GlobalsTable.SetItemRaw(n, new LuaType(t));
                }
            }
        }
Exemplo n.º 18
0
                protected override ILuaMultiValue InvokeInternal(ILuaMultiValue args)
                {
                    if (args.Count < 1)
                    {
                        throw new ArgumentException("Expecting at least one argument to function 'pcall'.");
                    }

                    ILuaValue func = args[0];

                    if (func.ValueType == LuaValueType.Function)
                    {
                        try
                        {
                            var ret = func.Invoke(LuaNil.Nil, false, -1, Environment.Runtime.CreateMultiValue(args.Skip(1).ToArray()));
                            return(Environment.Runtime.CreateMultiValue(new ILuaValue[] { LuaBoolean.True }.Union(ret).ToArray()));
                        }
                        catch (ThreadAbortException)
                        {
                            throw;
                        }
                        catch (ThreadInterruptedException)
                        {
                            throw;
                        }
                        catch (Exception e)
                        {
                            return(Environment.Runtime.CreateMultiValueFromObj(false, e.Message, e));
                        }
                    }
                    else
                    {
                        throw new ArgumentException("First argument to 'pcall' must be a function.");
                    }
                }
 static ILuaValue assert(ILuaValue value, ILuaValue obj = null)
 {
     string message = "Assertion failed: '" + (obj != null ? obj.ToString() : "") + "'.";
     if (value.IsTrue)
         return obj;
     else
         throw new AssertException(message);
 }
Exemplo n.º 20
0
    protected override ILuaMultiValue _invokeInternal(ILuaValue target, bool methodCall,
                                                     int overload, ILuaMultiValue args) {
      if (methodCall) {
        args = new LuaMultiValue(new[] { target }.Concat(args).ToArray());
      }

      return _invokeInternal(args);
    }
Exemplo n.º 21
0
        /// <summary>
        /// Gets the value at the given index without using meta-methods.
        /// </summary>
        /// <param name="index">The index to get at.</param>
        /// <returns>The value at the given index.</returns>
        public ILuaValue GetItemRaw(ILuaValue index)
        {
            index = index ?? LuaNil.Nil;

            ILuaValue ret;

            return(_values.TryGetValue(index, out ret) && ret != null ? ret : LuaNil.Nil);
        }
Exemplo n.º 22
0
        private LuaClass GetLuaClass(string name)
        {
            ILuaValue luaName = Lua.Environment.Runtime.CreateValue(name);
            ILuaValue cls     = Lua.Environment.GlobalsTable.GetItemRaw(luaName);

            Assert.IsInstanceOf <LuaClass>(cls);
            return((LuaClass)cls);
        }
Exemplo n.º 23
0
 /// <summary>
 /// Creates a new LuaThread object that calls the given method.
 /// </summary>
 /// <param name="env">The current environment.</param>
 /// <param name="method">The method to invoke.</param>
 internal LuaThreadNet(ILuaEnvironment env, ILuaValue method)
 {
     IsLua           = true;
     _env            = env;
     _method         = method;
     _backing        = new Thread(_do);
     _releaseBacking = true;
 }
Exemplo n.º 24
0
 /// <summary>
 /// Creates a new LuaThread object that calls the given method and executes on the given thread.
 /// </summary>
 /// <param name="env">The current environment.</param>
 /// <param name="method">The method to invoke.</param>
 /// <param name="thread">The thread that will execute this thread.</param>
 internal LuaThreadNet(ILuaEnvironment env, Thread thread, ILuaValue method)
 {
     IsLua           = true;
     _env            = env;
     _method         = method;
     _backing        = thread;
     _releaseBacking = false;
 }
Exemplo n.º 25
0
 /// <summary>
 /// Creates a new LuaThread object that calls the given method and
 /// executes on the given thread.
 /// </summary>
 /// <param name="E">The current environment.</param>
 /// <param name="method">The method to invoke.</param>
 /// <param name="thread">The thread that will execute this thread.</param>
 internal LuaThreadNet(ILuaEnvironment E, Thread thread, ILuaValue method)
 {
     this.IsLua           = true;
     this.E_              = E;
     this.method_         = method;
     this.backing_        = thread;
     this.releaseBacking_ = false;
 }
Exemplo n.º 26
0
 /// <summary>
 /// Creates a new LuaThread object that calls the given method.
 /// </summary>
 /// <param name="E">The current environment.</param>
 /// <param name="method">The method to invoke.</param>
 internal LuaThreadNet(ILuaEnvironment E, ILuaValue method)
 {
     this.IsLua           = true;
     this.E_              = E;
     this.method_         = method;
     this.backing_        = new Thread(Do);
     this.releaseBacking_ = true;
 }
Exemplo n.º 27
0
 /// <summary>
 /// Creates a new LuaThread object that calls the given method.
 /// </summary>
 /// <param name="E">The current environment.</param>
 /// <param name="method">The method to invoke.</param>
 internal LuaThreadNet(ILuaEnvironment E, ILuaValue method)
 {
     this.IsLua = true;
     this.E_ = E;
     this.method_ = method;
     this.backing_ = new Thread(Do);
     this.releaseBacking_ = true;
 }
Exemplo n.º 28
0
 /// <summary>
 /// Creates a new LuaThread object that calls the given method and
 /// executes on the given thread.
 /// </summary>
 /// <param name="E">The current environment.</param>
 /// <param name="method">The method to invoke.</param>
 /// <param name="thread">The thread that will execute this thread.</param>
 internal LuaThreadNet(ILuaEnvironment E, Thread thread, ILuaValue method)
 {
     this.IsLua = true;
     this.E_ = E;
     this.method_ = method;
     this.backing_ = thread;
     this.releaseBacking_ = false;
 }
Exemplo n.º 29
0
                protected override ILuaMultiValue InvokeInternal(ILuaMultiValue args)
                {
                    ILuaValue obj = args[0];
                    Stream    s;
                    int       start = 0;

                    if (obj.ValueType == LuaValueType.Table)
                    {
                        s = ((ILuaTable)obj).GetItemRaw(_stream) as Stream;
                        if (s == null)
                        {
                            return(Environment.Runtime.CreateMultiValueFromObj(null, "First argument must be a file-stream or a file path."));
                        }
                        start = 1;
                    }
                    else if (obj is Stream)
                    {
                        s     = obj as Stream;
                        start = 1;
                    }
                    else
                    {
                        s     = _output;
                        start = 0;
                    }

                    try
                    {
                        for (int i = start; i < args.Count; i++)
                        {
                            var temp = args[i].GetValue();
                            if (temp is double)
                            {
                                var bt = (Environment.Settings.Encoding ?? Encoding.UTF8).GetBytes(((double)temp).ToString(CultureInfo.InvariantCulture));
                                s.Write(bt, 0, bt.Length);
                            }
                            else if (temp is string)
                            {
                                var bt = (Environment.Settings.Encoding ?? Encoding.UTF8).GetBytes(temp as string);
                                s.Write(bt, 0, bt.Length);
                            }
                            else
                            {
                                throw new ArgumentException("Arguments to io.write must be a string or number.");
                            }
                        }

                        return(Environment.Runtime.CreateMultiValue(_CreateFile(s, Environment)));
                    }
                    catch (ArgumentException)
                    {
                        throw;
                    }
                    catch (Exception e)
                    {
                        return(Environment.Runtime.CreateMultiValueFromObj(null, e.Message, e));
                    }
                }
Exemplo n.º 30
0
            public override void SetIndex(ILuaValue index, ILuaValue value)
            {
                string name     = index.GetValue() as string;
                int    overload = -1;

                if (name == null)
                {
                    throw new InvalidOperationException(string.Format(Resources.BadIndexType, "class definition", "string"));
                }

                // find if name is defining an overload.
                if (name.Contains('`'))
                {
                    if (!int.TryParse(name.Substring(name.IndexOf('`') + 1), out overload))
                    {
                        throw new InvalidOperationException(Resources.OnlyNumbersInOverload);
                    }

                    name = name.Substring(0, name.IndexOf('`'));
                }

                // find the members with the given name.
                var members = type.GetMembers(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance).Where(m => m.Name == name).ToArray();

                if (members == null || members.Length == 0)
                {
                    throw new InvalidOperationException(string.Format(Resources.NoMemberFound, type, name));
                }

                if (members.Length > 1 && overload == -1)
                {
                    throw new AmbiguousMatchException(string.Format(Resources.NoMemberFound, type, name));
                }
                if (overload != -1 && overload >= members.Length)
                {
                    throw new InvalidOperationException(Resources.OverloadOutOfRange);
                }

                // set the backing parrent object.
                name = type.FullName + "." + name;
                if (members[0].MemberType == MemberTypes.Method)
                {
                    if (value.ValueType != LuaValueType.Function)
                    {
                        throw new InvalidOperationException(string.Format(Resources.MustBeFunction, name));
                    }

                    Item item = parent.CreateItem(name, new[] { members[overload == -1 ? 0 : overload] });
                    item.Assign(value);
                    parent._items.Add(item);
                }
                else if (members[0].MemberType == MemberTypes.Property)
                {
                    Item item = parent.CreateItem(name, members);
                    item.Assign(value);
                    parent._items.Add(item);
                }
            }
Exemplo n.º 31
0
            public override void Assign(ILuaValue value)
            {
                if (value.ValueType == LuaValueType.Table)
                {
                    // set the 'get' method for the item
                    var item = value.GetIndex(new LuaString("get"));
                    if (item != null && item != LuaNil.Nil)
                    {
                        MethodInfo m = _prop.GetGetMethod(true);
                        if (m == null || (!m.Attributes.HasFlag(MethodAttributes.Abstract) && !m.Attributes.HasFlag(MethodAttributes.Virtual)))
                        {
                            throw new InvalidOperationException(string.Format(Resources.CannotOverrideProperty, Name));
                        }
                        BoundTo = m;

                        if (item.ValueType != LuaValueType.Function)
                        {
                            throw new InvalidOperationException(Resources.PropTableFuncs);
                        }
                        Method = item;
                    }
                    // set the 'set' method for the item
                    item = value.GetIndex(new LuaString("set"));
                    if (item != null && item != LuaNil.Nil)
                    {
                        MethodInfo m = _prop.GetSetMethod(true);
                        if (m == null || (!m.Attributes.HasFlag(MethodAttributes.Abstract) && !m.Attributes.HasFlag(MethodAttributes.Virtual)))
                        {
                            throw new InvalidOperationException(string.Format(Resources.CannotOverrideProperty, Name));
                        }
                        BoundSet = m;

                        if (item.ValueType != LuaValueType.Function)
                        {
                            throw new InvalidOperationException(Resources.PropTableFuncs);
                        }
                        MethodSet = item;
                    }
                }
                else
                {
                    MethodInfo m = _prop.GetGetMethod(true);
                    if (m == null || (!m.Attributes.HasFlag(MethodAttributes.Abstract) && !m.Attributes.HasFlag(MethodAttributes.Virtual)))
                    {
                        throw new InvalidOperationException(string.Format(Resources.CannotOverrideProperty, Name));
                    }
                    BoundTo = m;

                    // check if the set method is abstract, if it is, we need to implement that also.
                    m = _prop.GetSetMethod(true);
                    if (m != null && (m.Attributes & MethodAttributes.Abstract) == MethodAttributes.Abstract)
                    {
                        BoundSet = m;
                    }

                    Default = value;
                }
            }
Exemplo n.º 32
0
        public override void SetIndex(ILuaValue index, ILuaValue value)
        {
            if (Value == null)
            {
                throw new InvalidOperationException(Errors.CannotIndex(LuaValueType.Nil));
            }

            Helpers.GetSetMember(Value.GetType(), Value, index, value);
        }
Exemplo n.º 33
0
        public override ILuaValue GetIndex(ILuaValue index)
        {
            if (Value == null)
            {
                throw new InvalidOperationException(Errors.CannotIndex(LuaValueType.Nil));
            }

            return(Helpers.GetSetMember(Value.GetType(), Value, index));
        }
            ILuaValue create(ILuaValue method)
            {
                if (method.ValueType != LuaValueType.Function)
                {
                    throw new ArgumentException("First argument to function 'coroutine.create' must be a function.");
                }

                return(E_.Runtime.CreateThread(method));
            }
            object wrap(ILuaValue func)
            {
                if (func.ValueType != LuaValueType.Function)
                {
                    throw new ArgumentException(
                        "First argument to function 'coroutine.wrap' must be a function.");
                }

                var thread = E_.Runtime.CreateThread(func);
                return (Func<ILuaMultiValue, ILuaMultiValue>)thread.Resume;
            }
Exemplo n.º 36
0
        /// <summary>
        /// Creates a new LuaThread for the given action.
        /// </summary>
        /// <param name="action">The method to invoke.</param>
        /// <returns>A new LuaThread object that will invoke the given method.</returns>
        /// <exception cref="System.ArgumentNullException">If action is null.</exception>
        public ILuaThread Create(ILuaValue action)
        {
            lock (lock_)
            {
                CheckDisposed();
                ResizePool();
                WorkerThread thread = waitingThreads_.Dequeue();

                thread.DoWork(action);
                return thread.Target;
            }
        }
Exemplo n.º 37
0
        /// <summary>
        /// Makes the current thread execute the given method.
        /// </summary>
        /// <param name="target">The method to execute.</param>
        public void DoWork(ILuaValue target)
        {
            if (status_ != Status.Waiting)
            {
                throw new InvalidOperationException(
                    "The worker thread must be waiting to get a new task.");
            }

            Target = new LuaThreadNet(E_, backing_, target);
            status_ = Status.Working;
            lock (lock_)
                Monitor.Pulse(lock_);
        }
Exemplo n.º 38
0
 /// <summary>
 /// Performs that actual invokation of the method.
 /// </summary>
 /// <param name="E">The current environment.</param>
 /// <param name="target">The object that this was called on.</param>
 /// <param name="memberCall">Whether the call used member call syntax (:).</param>
 /// <param name="args">The current arguments, not null but maybe empty.</param>
 /// <param name="overload">The overload to chose or negative to do 
 /// overload resoltion.</param>
 /// <param name="byRef">An array of the indicies that are passed by-reference.</param>
 /// <returns>The values to return to Lua.</returns>
 /// <exception cref="System.ArgumentException">If the object cannot be
 /// invoked with the given arguments.</exception>
 /// <exception cref="System.Reflection.AmbiguousMatchException">If there are two
 /// valid overloads for the given arguments.</exception>
 /// <exception cref="System.IndexOutOfRangeException">If overload is
 /// larger than the number of overloads.</exception>
 /// <exception cref="System.NotSupportedException">If this object does
 /// not support overloads.</exception>
 protected override ILuaMultiValue InvokeInternal(ILuaValue target, bool methodCall, int overload, ILuaMultiValue args)
 {
     return _Method(_E, args, target, methodCall);
 }
            static ILuaValue getmetatable(ILuaValue value)
            {
                if (value.ValueType != LuaValueType.Table)
                    return LuaNil.Nil;

                ILuaTable meta = ((ILuaTable)value).MetaTable;
                if (meta != null)
                {
                    ILuaValue method = meta.GetItemRaw(_metamethod);
                    if (method != null && method != LuaNil.Nil)
                        return method;
                }

                return meta;
            }
            static string tostring(ILuaValue value)
            {
                if (value.ValueType == LuaValueType.Table)
                {
                    var meta = ((ILuaTable)value).MetaTable;
                    if (meta != null)
                    {
                        var m = meta.GetItemRaw(_tostring);
                        if (m != null && m.ValueType == LuaValueType.Function)
                        {
                            var result = m.Invoke(value, true, -1, LuaMultiValue.Empty);
                            return result[0].ToString();
                        }
                    }
                }

                return value.ToString();
            }
 static bool rawequal(ILuaValue v1, ILuaValue v2)
 {
     return object.Equals(v1, v2);
 }
            static object[] next(ILuaTable table, ILuaValue index)
            {
                bool return_next = index == LuaNil.Nil;
                foreach (var item in table)
                {
                    if (return_next)
                        return new object[] { item.Key, item.Value };
                    else if (item.Key == index)
                        return_next = true;
                }

                // return nil, nil;
                return new object[0];
            }
Exemplo n.º 43
0
 static string gsub(string source, string pattern, ILuaValue repl, int n = int.MaxValue)
 {
     CheckNotNull("string.gsub", source);
     CheckNotNull("string.gsub", pattern);
     return Regex.Replace(source, pattern, new gsubHelper(repl, n).Match);
 }
 static string type(ILuaValue value)
 {
     return value.ValueType.ToString().ToLower();
 }
 static ILuaValue rawset(ILuaTable table, ILuaValue index, ILuaValue value)
 {
     table.SetItemRaw(index, value);
     return table;
 }
Exemplo n.º 46
0
 /// <summary>
 /// Creates a new Lua thread that calls the given method.
 /// </summary>
 /// <param name="method">The method to call.</param>
 /// <returns>The new Lua thread object.</returns>
 public ILuaThread CreateThread(ILuaValue method)
 {
     return threadPool_.Create(method);
 }
            static ILuaValue setmetatable(ILuaTable table, ILuaValue metatable)
            {
                if (metatable == LuaNil.Nil)
                    table.MetaTable = null;
                else if (metatable.ValueType == LuaValueType.Table)
                    table.MetaTable = (ILuaTable)metatable;
                else
                {
                    throw new ArgumentException(
                        "Second argument to 'setmetatable' must be a table.");
                }

                return table;
            }
            static IEnumerable<object> select(ILuaValue index, params object[] args)
            {
                if (index.Equals("#"))
                {
                    return new object[] { args.Length };
                }
                else if (index.ValueType == LuaValueType.Number)
                {
                    double ind = index.AsDouble() ?? 0;
                    if (ind < 0)
                        ind = args.Length + ind + 1;

                    return args.Skip((int)(ind - 1));
                }
                else
                {
                    throw new ArgumentException(
                        "First argument to function 'select' must be a number or the string '#'.");
                }
            }
 static double? tonumber(ILuaValue value)
 {
     return value.AsDouble();
 }
Exemplo n.º 50
0
 static ILuaMultiValue _getStream(ILuaValue file, ILuaEnvironment E, out Stream s)
 {
     s = null;
     if (file == LuaNil.Nil)
     {
         if (_output == null)
             return E.Runtime.CreateMultiValueFromObj(null, "No default output file set.");
         s = _output;
     }
     else
     {
         if (file.ValueType == LuaValueType.Table)
         {
             s = ((ILuaTable)file).GetItemRaw(_stream).GetValue() as Stream;
             if (s == null)
                 return E.Runtime.CreateMultiValueFromObj(null, "Specified argument is not a valid file stream.");
         }
         else if (file.ValueType == LuaValueType.UserData)
         {
             s = file.GetValue() as Stream;
         }
         else
             return E.Runtime.CreateMultiValueFromObj(null, "Specified argument is not a valid file stream.");
     }
     return null;
 }
Exemplo n.º 51
0
 /// <summary>
 /// Performs that actual invokation of the method.
 /// </summary>
 /// <param name="target">The object that this was called on.</param>
 /// <param name="memberCall">Whether the call used member call syntax (:).</param>
 /// <param name="args">The current arguments, not null but maybe empty.</param>
 /// <param name="overload">The overload to chose or negative to do 
 /// overload resoltion.</param>
 /// <param name="byRef">An array of the indicies that are passed by-reference.</param>
 /// <returns>The values to return to Lua.</returns>
 /// <exception cref="System.ArgumentException">If the object cannot be
 /// invoked with the given arguments.</exception>
 /// <exception cref="System.Reflection.AmbiguousMatchException">If there are two
 /// valid overloads for the given arguments.</exception>
 /// <exception cref="System.IndexOutOfRangeException">If overload is
 /// larger than the number of overloads.</exception>
 /// <exception cref="System.NotSupportedException">If this object does
 /// not support overloads.</exception>
 protected override ILuaMultiValue InvokeInternal(ILuaValue target, bool memberCall, int overload, ILuaMultiValue args)
 {
     ILuaValue method = (ILuaValue)Activator.CreateInstance(_Type, new[] { _E });
     Contract.Assume(method != null);
     return method.Invoke(LuaNil.Nil, false, -1, args);
 }
Exemplo n.º 52
0
 static void Register(ILuaEnvironment E, ILuaValue table, Delegate func, string name = null)
 {
     var funcValue = E.Runtime.CreateValue(func);
     var nameValue = E.Runtime.CreateValue(name ?? func.Method.Name);
     table.SetIndex(nameValue, funcValue);
 }
 /// <summary>
 /// Performs that actual invokation of the method.
 /// </summary>
 /// <param name="target">The object that this was called on.</param>
 /// <param name="memberCall">Whether the call used member call syntax (:).</param>
 /// <param name="args">The current arguments, not null but maybe empty.</param>
 /// <param name="overload">The overload to chose or negative to do 
 /// overload resoltion.</param>
 /// <param name="byRef">An array of the indicies that are passed by-reference.</param>
 /// <returns>The values to return to Lua.</returns>
 /// <exception cref="System.ArgumentException">If the object cannot be
 /// invoked with the given arguments.</exception>
 /// <exception cref="System.Reflection.AmbiguousMatchException">If there are two
 /// valid overloads for the given arguments.</exception>
 /// <exception cref="System.IndexOutOfRangeException">If overload is
 /// larger than the number of overloads.</exception>
 /// <exception cref="System.NotSupportedException">If this object does
 /// not support overloads.</exception>
 protected override ILuaMultiValue InvokeInternal(ILuaValue target, bool methodCall, int overload, ILuaMultiValue args)
 {
     if (methodCall) args = new LuaMultiValue(new[] { target }.Concat(args).ToArray());
     return InvokeInternal(args);
 }
Exemplo n.º 54
0
 public ILuaThread CreateThread(ILuaValue method)
 {
     Contract.Requires(method != null);
     Contract.Ensures(Contract.Result<ILuaThread>() != null);
     return null;
 }
 static ILuaValue rawget(ILuaTable table, ILuaValue index)
 {
     return table.GetItemRaw(index);
 }
Exemplo n.º 56
0
                public gsubHelper(ILuaValue value, int max)
                {
                    count_ = 0;
                    max_ = max;

                    if (value.ValueType == LuaValueType.String)
                        string_ = (string)value.GetValue();
                    else if (value.ValueType == LuaValueType.Table)
                        table_ = (ILuaTable)value;
                    else if (value.ValueType == LuaValueType.Function)
                        method_ = value;
                    else
                        throw new ArgumentException("Third argument to function 'string.gsub' must be a string, table, or function.");
                }
Exemplo n.º 57
0
        /// <summary>
        /// Creates a delegate that can be called to call the given IMethod.
        /// </summary>
        /// <param name="E">The current environment.</param>
        /// <param name="type">The type of the delegate.</param>
        /// <param name="method">The method to call.</param>
        /// <returns>A delegate that is used to call the method.</returns>
        /// <exception cref="System.ArgumentException">If type is not a delegate
        /// type.</exception>
        /// <exception cref="System.ArgumentNullException">If any argument is null.</exception>
        /// <exception cref="System.NotSupportedException">If this implementation
        /// does not support created delegates.</exception>
        public Delegate CreateDelegate(ILuaEnvironment E, Type type, ILuaValue method)
        {
            if (E == null)
                throw new ArgumentNullException("E");
            if (type == null)
                throw new ArgumentNullException("type");
            if (method == null)
                throw new ArgumentNullException("method");
            if (!typeof(Delegate).IsAssignableFrom(type.BaseType))
                throw new ArgumentException(Resources.DeriveFromDelegate);

            // search through the cache for a compatible delegate helper
            object target; // TODO: Make CreateDelegate more efficient.
            for (int i = 0; i < _delegateTypes.Count; i++)
            {
                target = Activator.CreateInstance(_delegateTypes[i], E, method);
                Delegate d = Delegate.CreateDelegate(type, target, _delegateTypes[i].GetMethod("Do"), false);
                if (d != null)
                    return d;
            }

            Type temp = CreateDelegateType(type.GetMethod("Invoke"));
            _delegateTypes.Add(temp);

            target = Activator.CreateInstance(temp, E, method);
            return Delegate.CreateDelegate(type, target, temp.GetMethod("Do"));
        }