コード例 #1
0
ファイル: Chunk.cs プロジェクト: net-lisias-ksph/MuMechLib
        /// <summary>
        /// Executes the chunk
        /// </summary>
        /// <param name="isBreak">whether to break execution</param>
        /// <returns></returns>
        public LuaValue Execute(out bool isBreak)
        {
            foreach (Statement statement in Statements)
            {
                ReturnStmt returnStmt = statement as ReturnStmt;
                if (returnStmt != null)
                {
                    isBreak = false;
                    return(LuaMultiValue.WrapLuaValues(returnStmt.ExprList.ConvertAll(expr => expr.Evaluate(this.Enviroment)).ToArray()));
                }
                else if (statement is BreakStmt)
                {
                    isBreak = true;
                    return(null);
                }
                else
                {
                    var returnValue = statement.Execute(this.Enviroment, out isBreak);
                    if (returnValue != null || isBreak == true)
                    {
                        return(returnValue);
                    }
                }
            }

            isBreak = false;
            return(null);
        }
コード例 #2
0
ファイル: ForInStmt.cs プロジェクト: Hengle/SharpLua-1
        /// <summary>
        /// Executes the chunk
        /// </summary>
        /// <param name="enviroment">Runs in the given environment</param>
        /// <param name="isBreak">whether to break execution</param>
        /// <returns></returns>
        public override LuaValue Execute(LuaTable enviroment, out bool isBreak)
        {
            LuaValue[] values     = this.ExprList.ConvertAll(expr => expr.Evaluate(enviroment)).ToArray();
            LuaValue[] neatValues = LuaMultiValue.UnWrapLuaValues(values);

            if (neatValues.Length < 3) //probably LuaUserdata. Literal will also fail...
            {
                return(ExecuteAlternative(enviroment, out isBreak));
            }

            LuaFunction func    = neatValues[0] as LuaFunction;
            LuaValue    state   = neatValues[1];
            LuaValue    loopVar = neatValues[2];

            var table = new LuaTable(enviroment);

            this.Body.Enviroment = table;

            while (true)
            {
                LuaValue      result     = func.Invoke(new LuaValue[] { state, loopVar });
                LuaMultiValue multiValue = result as LuaMultiValue;

                if (multiValue != null)
                {
                    neatValues = LuaMultiValue.UnWrapLuaValues(multiValue.Values);
                    loopVar    = neatValues[0];

                    for (int i = 0; i < Math.Min(this.NameList.Count, neatValues.Length); i++)
                    {
                        table.SetNameValue(this.NameList[i], neatValues[i]);
                    }
                }
                else
                {
                    loopVar = result;
                    table.SetNameValue(this.NameList[0], result);
                }

                if (loopVar == LuaNil.Nil)
                {
                    break;
                }

                var returnValue = this.Body.Execute(out isBreak);
                if (returnValue != null || isBreak == true)
                {
                    isBreak = false;
                    return(returnValue);
                }
            }

            isBreak = false;
            return(null);
        }
コード例 #3
0
ファイル: Assignment.cs プロジェクト: Hengle/SharpLua-1
        /// <summary>
        /// Runs the Statement
        /// </summary>
        /// <param name="enviroment">The environment to run in</param>
        /// <param name="isBreak">whether to break execution or not</param>
        /// <returns></returns>
        public override LuaValue Execute(LuaTable enviroment, out bool isBreak)
        {
            LuaValue[] values     = this.ExprList.ConvertAll(expr => expr.Evaluate(enviroment)).ToArray();
            LuaValue[] neatValues = LuaMultiValue.UnWrapLuaValues(values);

            for (int i = 0; i < Math.Min(this.VarList.Count, neatValues.Length); i++)
            {
                Var var = this.VarList[i];

                if (var.Accesses.Count == 0)
                {
                    VarName varName = var.Base as VarName;

                    if (varName != null)
                    {
                        // inital creation, no metatable yet
                        SetKeyValue(enviroment, new LuaString(varName.Name), values[i]);
                        continue;
                    }
                }
                else
                {
                    LuaValue baseValue = var.Base.Evaluate(enviroment);

                    for (int j = 0; j < var.Accesses.Count - 1; j++)
                    {
                        Access access = var.Accesses[j];

                        baseValue = access.Evaluate(baseValue, enviroment);
                    }

                    Access lastAccess = var.Accesses[var.Accesses.Count - 1];

                    NameAccess nameAccess = lastAccess as NameAccess;
                    if (nameAccess != null)
                    {
                        SetKeyValue(baseValue, new LuaString(nameAccess.Name), values[i]);
                        continue;
                    }

                    KeyAccess keyAccess = lastAccess as KeyAccess;
                    if (lastAccess != null)
                    {
                        SetKeyValue(baseValue, keyAccess.Key.Evaluate(enviroment), values[i]);
                    }
                }
            }

            isBreak = false;
            return(null);
        }
コード例 #4
0
        private static LuaValue NextLine(LuaValue[] values)
        {
            LuaMultiValue table   = values[0] as LuaMultiValue;
            LuaValue      loopVar = values[1];
            LuaValue      result  = LuaNil.Nil;

            int idx = (loopVar == LuaNil.Nil ? 0 : Convert.ToInt32(loopVar.MetaTable.GetValue("__lines_index").Value));

            if (idx < table.Values.Length)
            {
                result = table.Values[idx];
                result.MetaTable.SetNameValue("__lines_index", new LuaNumber(idx + 1));
            }
            return(result);
        }
コード例 #5
0
ファイル: ClassLib.cs プロジェクト: Hengle/SharpLua-1
        public static LuaValue NextClassIterator(LuaValue[] args)
        {
            LuaMultiValue multi   = args[0] as LuaMultiValue;
            LuaValue      loopVar = args[1];
            LuaValue      result  = LuaNil.Nil;

            int idx = (loopVar == LuaNil.Nil ? 0 : Convert.ToInt32(loopVar.MetaTable.GetValue("__class_iterator_index").Value));

            if (idx < multi.Values.Length)
            {
                result = multi.Values[idx];
                result.MetaTable.SetNameValue("__class_iterator_index", new LuaNumber(idx + 1));
            }
            return(result);
        }
コード例 #6
0
                public string Match(Match match)
                {
                    if (count_ >= max_)
                    {
                        return(match.Value);
                    }

                    count_++;
                    if (string_ != null)
                    {
                        return(Regex.Replace(string_, @"%[0-9%]", m =>
                        {
                            if (m.Value == "%%")
                            {
                                return "%";
                            }
                            int i = int.Parse(m.Groups[0].Value.Substring(1));
                            return i == 0 ? match.Value : (match.Groups.Count > i ? match.Groups[i].Value : "");
                        }));
                    }
                    else if (table_ != null)
                    {
                        string    key   = match.Groups.Count == 0 ? match.Value : match.Groups[1].Value;
                        ILuaValue value = table_.GetItemRaw(new LuaString(key));
                        if (value != null && value.IsTrue)
                        {
                            return(value.ToString());
                        }
                    }
                    else if (method_ != null)
                    {
                        var            groups = match.Groups.Cast <Group>().Skip(1).Select(c => c.Value).ToArray();
                        var            args   = LuaMultiValue.CreateMultiValueFromObj(groups);
                        ILuaMultiValue obj    = method_.Invoke(LuaNil.Nil, false, -1, args);
                        if (obj != null && obj.Count > 0)
                        {
                            ILuaValue value = obj[0];
                            if (value != null && value.IsTrue)
                            {
                                return(value.ToString());
                            }
                        }
                    }

                    return(match.Value);
                }
コード例 #7
0
        public static LuaValue Pcall(LuaValue[] values)
        {
            LuaFunction func = values[0] as LuaFunction;

            try
            {
                LuaValue[] args = new LuaValue[values.Length - 1];
                for (int i = 0; i < args.Length; i++)
                {
                    args[i] = values[i + 1];
                }
                LuaValue result = func.Invoke(args);
                return(new LuaMultiValue(LuaMultiValue.UnWrapLuaValues(new LuaValue[] { LuaBoolean.True, result })));
            }
            catch (Exception error)
            {
                return(new LuaMultiValue(new LuaValue[] { LuaBoolean.False, new LuaString(error.Message) }));
            }
        }
コード例 #8
0
        public static LuaValue Require(LuaValue[] args)
        {
            // get loaders table
            LuaTable t = (LuaRuntime.GlobalEnvironment.GetValue("package") as LuaTable).GetValue("loaders") as LuaTable;

            if (t == null)
            {
                throw new Exception("Cannot get loaders table from package module!");
            }
            if (t.Count == 0)
            {
                throw new Exception("Loaders table is empty!");
            }
            // whether package was found/loaded
            LuaBoolean b      = LuaBoolean.False;
            LuaValue   module = null;

            foreach (LuaValue key in t.Keys)
            {
                LuaFunction f = t.GetValue(key) as LuaFunction;
                if (f != null)
                {
                    LuaMultiValue lmv = f.Invoke(new LuaValue[] { new LuaString(args[0].Value.ToString()) }) as LuaMultiValue;
                    b = lmv.Values[0] as LuaBoolean;
                    if (b.BoolValue == true)
                    {
                        module = lmv.Values[1];
                        break;
                    }
                }
                else
                {
                    throw new Exception("Cannot cast type '" + t.GetValue(key).GetType().ToString() + "' to type 'LuaFunction'");
                }
            }
            if (b.BoolValue == false)
            {
                Console.WriteLine("Could not load package '" + args[0].Value.ToString() + "'!");
            }
            return(module);
        }
コード例 #9
0
ファイル: LocalVar.cs プロジェクト: Hengle/SharpLua-1
        /// <summary>
        /// Executes the chunk
        /// </summary>
        /// <param name="enviroment">The environment to run in</param>
        /// <param name="isBreak">whether to break execution</param>
        /// <returns></returns>
        public override LuaValue Execute(LuaTable enviroment, out bool isBreak)
        {
            LuaValue[] values     = this.ExprList.ConvertAll(expr => expr.Evaluate(enviroment)).ToArray();
            LuaValue[] neatValues = LuaMultiValue.UnWrapLuaValues(values);

            for (int i = 0; i < Math.Min(this.NameList.Count, neatValues.Length); i++)
            {
                enviroment.RawSetValue(this.NameList[i], neatValues[i]);
            }

            if (neatValues.Length < this.NameList.Count)
            {
                for (int i = neatValues.Length; i < this.NameList.Count - neatValues.Length; i++)
                {
                    enviroment.RawSetValue(this.NameList[i], LuaNil.Nil);
                }
            }

            isBreak = false;
            return(null);
        }
コード例 #10
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 methodCall, int overload, ILuaMultiValue args)
 {
     if (methodCall) args = new LuaMultiValue(new[] { target }.Concat(args).ToArray());
     return InvokeInternal(args);
 }
コード例 #11
0
ファイル: ForInStmt.cs プロジェクト: Hengle/SharpLua-1
        private LuaValue ExecuteAlternative(LuaTable enviroment, out bool isBreak)
        {
            LuaValue returnValue;

            LuaValue[] values     = this.ExprList.ConvertAll(expr => expr.Evaluate(enviroment)).ToArray();
            LuaValue[] neatValues = LuaMultiValue.UnWrapLuaValues(values);

            LuaValue state = neatValues[0];

            LuaTable table = new LuaTable(enviroment);

            this.Body.Enviroment = table;
            System.Collections.IDictionary dict = state.Value as System.Collections.IDictionary;
            System.Collections.IEnumerable ie   = state.Value as System.Collections.IEnumerable;
            if (dict != null)
            {
                foreach (object key in dict.Keys)
                {
                    //for (int i = 0; i < this.NameList.Count; i++)
                    //{
                    //table.SetNameValue(this.NameList[i], ObjectToLua.ToLuaValue(key));
                    //}
                    table.SetNameValue(this.NameList[0], ObjectToLua.ToLuaValue(key));
                    table.SetNameValue(this.NameList[1], ObjectToLua.ToLuaValue(dict[key]));

                    returnValue = this.Body.Execute(out isBreak);
                    if (returnValue != null || isBreak == true)
                    {
                        isBreak = false;
                        return(returnValue);
                    }
                }
            }
            else if (ie != null)
            {
                foreach (object obj in ie)
                {
                    for (int i = 0; i < this.NameList.Count; i++)
                    {
                        table.SetNameValue(this.NameList[i], ObjectToLua.ToLuaValue(obj));
                    }

                    returnValue = this.Body.Execute(out isBreak);
                    if (returnValue != null || isBreak == true)
                    {
                        isBreak = false;
                        return(returnValue);
                    }
                }
            }
            else
            {
                // its some other value...
                for (int i = 0; i < this.NameList.Count; i++)
                {
                    table.SetNameValue(this.NameList[i], ObjectToLua.ToLuaValue(state.Value));
                }

                returnValue = this.Body.Execute(out isBreak);
                if (returnValue != null || isBreak == true)
                {
                    isBreak = false;
                    return(returnValue);
                }

                isBreak = false;
                return(null);
            }
            isBreak = false;
            return(null);
        }
コード例 #12
0
 public ILuaMultiValue CreateMultiValueFromObj(params object[] values)
 {
     return(LuaMultiValue.CreateMultiValueFromObj(values));
 }
コード例 #13
0
ファイル: FunctionCall.cs プロジェクト: Hengle/SharpLua-1
        public override LuaValue Evaluate(LuaValue baseValue, LuaTable enviroment)
        {
            LuaFunction function = baseValue as LuaFunction;

            if (function != null)
            {
                if (function.Function.Method.DeclaringType.FullName == "SharpLua.Library.BaseLib" &&
                    (function.Function.Method.Name == "loadstring" || function.Function.Method.Name == "dofile"))
                {
                    if (this.Args.String != null)
                    {
                        return(function.Function.Invoke(new LuaValue[] { this.Args.String.Evaluate(enviroment), enviroment }));
                    }
                    else
                    {
                        return(function.Function.Invoke(new LuaValue[] { this.Args.ArgList[0].Evaluate(enviroment), enviroment }));
                    }
                }

                if (this.Args.Table != null)
                {
                    return(function.Function.Invoke(new LuaValue[] { this.Args.Table.Evaluate(enviroment) }));
                }
                else if (this.Args.String != null)
                {
                    return(function.Function.Invoke(new LuaValue[] { this.Args.String.Evaluate(enviroment) }));
                }
                else
                {
                    List <LuaValue> args = this.Args.ArgList.ConvertAll(arg => arg.Evaluate(enviroment));
                    return(function.Function.Invoke(LuaMultiValue.UnWrapLuaValues(args.ToArray())));
                }
            }
            else if ((baseValue as LuaTable) != null)
            {
                List <LuaValue> args = this.Args.ArgList.ConvertAll(arg => arg.Evaluate(enviroment));
                args.Insert(0, baseValue);
                return(((baseValue as LuaTable).MetaTable.GetValue("__call") as LuaFunction).Invoke(args.ToArray()));
            }
            else if ((baseValue as LuaClass) != null)
            {
                LuaClass        c    = baseValue as LuaClass;
                List <LuaValue> args = this.Args.ArgList.ConvertAll(arg => arg.Evaluate(enviroment));
                //args.Insert(0, new LuaString(this.Method);
                if (c.Self.MetaTable == null)
                {
                    c.GenerateMetaTable();
                }
                return((c.Self.MetaTable.GetValue("__call") as LuaFunction).Invoke(args.ToArray()));
            }
            else if ((baseValue as LuaUserdata) != null)
            {
                List <LuaValue> args = this.Args.ArgList.ConvertAll(arg => arg.Evaluate(enviroment));
                LuaUserdata     u    = baseValue as LuaUserdata;
                if (u.MetaTable != null)
                {
                    if (u.MetaTable.GetValue("__call") != null)
                    {
                        return(ObjectToLua.ToLuaValue((u.MetaTable.GetValue("__call") as LuaFunction).Invoke(args.ToArray())));
                    }
                    else
                    {
                        throw new NotImplementedException();
                    }
                }
                else
                {
                    throw new NotImplementedException();
                }
            }
            else
            {
                throw new Exception("Invoke function call on non function value.");
            }
        }