Exemplo n.º 1
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.º 2
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.º 3
0
                protected override ILuaMultiValue _invokeInternal(ILuaMultiValue args)
                {
                    ILuaValue obj = args[0];
                    bool      close;
                    Stream    s;
                    int       start;
                    string    oString = obj.GetValue() as string;

                    if (oString != null)
                    {
                        if (oString[0] != '*')
                        {
                            s     = File.OpenRead(oString);
                            close = true;
                            start = 1;
                        }
                        else
                        {
                            s     = _input;
                            close = false;
                            start = 0;
                        }
                    }
                    else if (obj.ValueType == LuaValueType.Table)
                    {
                        s = ((ILuaTable)obj).GetItemRaw(_stream) as Stream;
                        if (s == null)
                        {
                            throw new ArgumentException("First argument to io.lines must be a file-stream or a " +
                                                        "file path, make sure to use file:lines.");
                        }

                        close = false;
                        start = 1;
                    }
                    else if (obj.GetValue() is Stream st)
                    {
                        s     = st;
                        close = false;
                        start = 1;
                    }
                    else
                    {
                        s     = _input;
                        close = false;
                        start = 0;
                    }

                    int[] a = _parse(args.Skip(start), "io.lines");
                    return(_environment.Runtime.CreateMultiValue(new LinesHelper(_environment, close, s, a)));
                }
Exemplo n.º 4
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.º 5
0
                protected override ILuaMultiValue InvokeInternal(ILuaMultiValue args)
                {
                    ILuaValue obj = args[0];
                    bool close;
                    Stream s;
                    int start = 0;
                    string oString = obj.GetValue() as string;

                    if (oString != null)
                    {
                        if (oString[0] != '*')
                        {
                            s = File.OpenRead(oString);
                            close = true;
                            start = 1;
                        }
                        else
                        {
                            s = _input;
                            close = false;
                            start = 0;
                        }
                    }
                    else if (obj.ValueType == LuaValueType.Table)
                    {
                        s = ((ILuaTable)obj).GetItemRaw(_stream) as Stream;
                        if (s == null)
                            throw new ArgumentException("First argument to io.lines must be a file-stream or a file path, make sure to use file:lines.");
                        close = false;
                        start = 1;
                    }
                    else if (obj.GetValue() is Stream)
                    {
                        s = obj.GetValue() as Stream;
                        close = false;
                        start = 1;
                    }
                    else
                    {
                        s = _input;
                        close = false;
                        start = 0;
                    }

                    int[] a = _parse(args.Skip(start), "io.lines");
                    return Environment.Runtime.CreateMultiValue(new LinesHelper(Environment, close, s, a));
                }
                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.");
                }
                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()));
                }
                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.");
                    }
                }