示例#1
0
        internal void _do()
        {
            try {
                lock (_handle) {
                    while (Status != LuaThreadStatus.Running)
                    {
                        Monitor.Wait(_handle);
                    }
                }

                ILuaMultiValue args = Interlocked.Exchange(ref _args, null);
                ILuaMultiValue ret  = _method.Invoke(LuaNil.Nil, false, -1, args);

                lock (_handle) {
                    _args      = ret;
                    _exception = null;
                    Status     = LuaThreadStatus.Complete;
                    Monitor.Pulse(_handle);
                }
            } catch (Exception e) {
                lock (_handle) {
                    _exception = e;
                    Status     = LuaThreadStatus.Complete;
                    Monitor.Pulse(_handle);
                }
            }
        }
示例#2
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());
            }
        }
示例#3
0
        internal void Do()
        {
            try
            {
                lock (handle_)
                {
                    while (Status != LuaThreadStatus.Running)
                    {
                        Monitor.Wait(handle_);
                    }
                }

                ILuaMultiValue args = Interlocked.Exchange(ref args_, null);
                ILuaMultiValue ret  = method_.Invoke(LuaNil.Nil, false, -1, args);

                lock (handle_)
                {
                    args_      = ret;
                    exception_ = null;
                    Status     = LuaThreadStatus.Complete;
                    Monitor.Pulse(handle_);
                }
            }
            catch (Exception e)
            {
                lock (handle_)
                {
                    exception_ = e;
                    Status     = LuaThreadStatus.Complete;
                    Monitor.Pulse(handle_);
                }
            }
        }
                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.");
                    }
                }
示例#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));
        }
示例#6
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())));
                }
示例#7
0
        public virtual IEnumerable <ILuaMultiValue> GenericLoop(ILuaEnvironment env,
                                                                ILuaMultiValue args)
        {
            // TODO: Replace this.
            if (args == null)
            {
                throw new ArgumentNullException(nameof(args));
            }

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

            ILuaValue target = args[0];
            object    temp   = target.GetValue();

            if (temp is IEnumerable <ILuaMultiValue> enumT)
            {
                foreach (var item in enumT)
                {
                    yield return(item);
                }
            }
            else if (temp is IEnumerable en)
            {
                foreach (var item in en)
                {
                    yield return(new LuaMultiValue(CreateValue(item)));
                }
            }
            else if (target.ValueType == LuaValueType.Function)
            {
                ILuaValue s   = args[1];
                ILuaValue var = args[2];

                while (true)
                {
                    var ret = target.Invoke(LuaNil.Nil, false, -1, CreateMultiValue(s, var));
                    if (ret == null || ret[0] == null || ret[0] == LuaNil.Nil)
                    {
                        yield break;
                    }

                    var = ret[0];

                    yield return(ret);
                }
            }
            else
            {
                throw new InvalidOperationException(
                          $"Cannot enumerate over an object of type '{args[0]}'.");
            }
        }
示例#8
0
        void _assertThrows(string message, ILuaValue value)
        {
            bool throws = false;

            try {
                value.Invoke(LuaNil.Nil, false, -1, _lua.Environment.Runtime.CreateMultiValue());
            } catch (Exception) {
                throws = true;
            }
            Assert.IsTrue(throws, "Should throw exception:" + message);
        }
示例#9
0
                protected override ILuaMultiValue InvokeInternal(ILuaMultiValue args)
                {
                    if (args.Count < 1)
                    {
                        throw new ArgumentException("Expecting at least one argument to function 'load'.");
                    }

                    ILuaValue ld = args[0];
                    string    chunk;

                    if (ld.ValueType == LuaValueType.Function)
                    {
                        chunk = "";
                        while (true)
                        {
                            var ret = ld.Invoke(LuaNil.Nil, false, -1, Environment.Runtime.CreateMultiValue());
                            if (ret[0].ValueType == LuaValueType.String)
                            {
                                if (string.IsNullOrEmpty(ret[0].GetValue() as string))
                                {
                                    break;
                                }
                                else
                                {
                                    chunk += ret[0].GetValue() as string;
                                }
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                    else if (ld.ValueType == LuaValueType.String)
                    {
                        chunk = ld.GetValue() as string;
                    }
                    else
                    {
                        throw new ArgumentException("First argument to 'load' must be a string or a method.");
                    }

                    try
                    {
                        return(Environment.Runtime.CreateMultiValue(Environment.CodeCompiler.Compile(Environment, PlainParser.Parse(Environment.Parser, chunk, null), null)));
                    }
                    catch (Exception e)
                    {
                        return(Environment.Runtime.CreateMultiValueFromObj(null, e.Message));
                    }
                }
示例#10
0
        /// <summary>
        /// Gets the length of the value.
        /// </summary>
        /// <returns>The length of the value.</returns>
        public override ILuaValue Length()
        {
            if (MetaTable != null)
            {
                ILuaValue meth = MetaTable.GetItemRaw(_len);
                if (meth != null)
                {
                    var ret = meth.Invoke(this, true, -1, LuaMultiValue.Empty);
                    return(ret[0]);
                }
            }

            return(RawLength());
        }
            static object[] pairs(ILuaTable table)
            {
                ILuaTable meta = table.MetaTable;

                if (meta != null)
                {
                    ILuaValue p = meta.GetItemRaw(_pairs);
                    if (p != null && p != LuaNil.Nil)
                    {
                        var ret = p.Invoke(table, true, -1, LuaMultiValue.Empty);
                        return(new object[] { ret.AdjustResults(3) });
                    }
                }

                return(new object[] { (Func <ILuaTable, ILuaValue, object[]>)next, table });
            }
            static object[] ipairs(ILuaTable table)
            {
                ILuaTable meta = table.MetaTable;

                if (meta != null)
                {
                    ILuaValue method = meta.GetItemRaw(_ipairs);
                    if (method != null && method != LuaNil.Nil)
                    {
                        var ret = method.Invoke(table, true, -1, LuaMultiValue.Empty);
                        // The runtime will correctly expand the results (because the multi-value
                        // is at the end).
                        return(new object[] { ret.AdjustResults(3) });
                    }
                }

                return(new object[] { (Func <ILuaTable, double, object[]>)_ipairs_itr, table, 0 });
            }