Beispiel #1
0
        public static VarArgs GetMetaTable(VarArgs args, LamnEngine s)
        {
            Object o = args.PopArg();
            VarArgs returnArgs = new VarArgs();

            if (o != null && o is Table)
            {
                returnArgs.PushArg(((Table)o).MetaTable);
            }

            return returnArgs;
        }
Beispiel #2
0
        public static VarArgs SetMetaTable(VarArgs args, LamnEngine s)
        {
            Object obj = args.PopArg();
            Object metatable = args.PopArg();
            VarArgs returnArgs = new VarArgs();

            if (obj != null && obj is Table)
            {
                ((Table)obj).MetaTable = (Table)metatable;
            }

            return returnArgs;
        }
Beispiel #3
0
        public static VarArgs ToNumber(VarArgs args, LamnEngine s)
        {
            Object arg = args.PopArg();
            VarArgs returnArgs = new VarArgs();

            if (arg is Double)
            {
                returnArgs.PushArg(arg);
            }
            else if (arg is String)
            {
                returnArgs.PushArg(Double.Parse((String)arg));
            }

            return returnArgs;
        }
Beispiel #4
0
        public static VarArgs TypeString(VarArgs args, LamnEngine s)
        {
            Object arg = args.PopArg();
            VarArgs returnArgs = new VarArgs();

            if (arg is Double)
            {
                returnArgs.PushArg("number");
            }
            else if (arg is String)
            {
                returnArgs.PushArg("string");
            }
            else if (arg is Table)
            {
                returnArgs.PushArg("table");
            }

            return returnArgs;
        }
Beispiel #5
0
        private static VarArgs StrLen(VarArgs args, LamnEngine s)
        {
            String str = (String)args.PopArg();

            VarArgs returnArgs = new VarArgs();
            returnArgs.PushArg((double)str.Length);
            return returnArgs;
        }
Beispiel #6
0
 static VarArgs LoadString(VarArgs input, LamnEngine s)
 {
     VarArgs retArgs = new VarArgs();
     retArgs.PushArg(s.CompileString((String)input.PopArg()));
     return retArgs;
 }
Beispiel #7
0
        private static VarArgs StrFmt(VarArgs args, LamnEngine s)
        {
            String currentString = (String)args.PopArg();
            while(true)
            {
                int index = currentString.IndexOf("%s");
                if (index < 0) {
                    break;
                }

                String replace = (String)args.PopArg();
                currentString = currentString.Substring(0,index) + replace + currentString.Substring(index + 2);
            }
            VarArgs retArgs = new VarArgs();
            retArgs.PushArg(currentString);
            return retArgs;
        }
Beispiel #8
0
        private static VarArgs StrGSub(VarArgs args, LamnEngine s)
        {
            String str = (String)args.PopArg();
            String pattern = (String)args.PopArg();
            String repl = (String)args.PopArg();
            Object n = args.PopArg();

            int count = -1;
            if (n != null)
            {
                count = (int)(Double)n;
            }

            Regex patternRegex = RegexCompiler.compile(pattern);
            String retStr = patternRegex.Replace(str, repl, count);

            VarArgs returnArgs = new VarArgs();
            returnArgs.PushArg(retStr);
            return returnArgs;
        }
Beispiel #9
0
        private static VarArgs CoroutineCreate(VarArgs args, LamnEngine s)
        {
            Thread t = new Thread((Closure)args.PopArg(), 512);
            t.State.PushStack(new YieldPoint());
            t.State.PushStack(0);
            t.State.BasePosition = 1;

            VarArgs returnArgs = new VarArgs();
            returnArgs.PushArg(t);
            return returnArgs;
        }
Beispiel #10
0
        private static VarArgs Sin(VarArgs args, LamnEngine s)
        {
            args = ToNumber(args, s);
            Double d = (Double)args.PopArg();

            VarArgs returnArgs = new VarArgs();
            returnArgs.PushArg(Math.Sin(d));
            return returnArgs;
        }
Beispiel #11
0
 public void ResumeThread(Thread t, VarArgs args)
 {
     if (t.State.StackPosition == 0) // Thread has terminated
     {
         VarArgs retArgs = new VarArgs();
         retArgs.PushArg(false);
         CurrentThread.PushStack(retArgs);
     }
     else // Resume thread
     {
         t.State.PushStack(args);
         ThreadStack.Push(t.State);
     }
     CurrentIP.InstructionIndex++;
 }
Beispiel #12
0
        private void DoRET(UInt32 instruction)
        {
            int oldBaseIndex = (int)CurrentThread.GetStackAtIndex(CurrentThread.BasePosition);

            VarArgs args = new VarArgs();
            int numArgs = (int)((instruction & OpCodes.OP1_MASK) >> OpCodes.OP1_SHIFT);
            for (int i = 0; i < numArgs; i++)
            {
                if (i == 0)
                {
                    args.PushLastArg(CurrentThread.PopStack());
                }
                else
                {
                    args.PushArg(CurrentThread.PopStack());
                }
            }

            while (CurrentThread.StackPosition > CurrentThread.BasePosition)
            {
                CurrentThread.PopStack();
            }

            CurrentThread.BasePosition = oldBaseIndex;

            Object retObj = CurrentThread.PopStack();

            if (retObj is ReturnPoint) // Returning inside a thread
            {
                ReturnPoint retPoint = (ReturnPoint)retObj;
                CurrentIP = retPoint.instructionPointer;
                if (CurrentIP != null)
                {
                    CurrentIP.InstructionIndex++;
                }

                if (retPoint.popArgs > 0)
                {
                    for (int i = 0; i < retPoint.popArgs; i++)
                    {
                        CurrentThread.PushStack(args.PopArg());
                    }
                }
                else
                {
                    CurrentThread.PushStack(args);
                }
            }
            else if (retObj is YieldPoint) // Returning across a thread boundary
            {
                YieldThread(args);
            }
            else
            {
                throw new VMException();
            }
        }
Beispiel #13
0
 private static void CoroutineResume(VarArgs args, LamnEngine s)
 {
     Thread t = (Thread)args.PopArg();
     s.LamnState.ResumeThread(t, args);
 }
Beispiel #14
0
        private static VarArgs FMod(VarArgs args, LamnEngine s)
        {
            VarArgs args2 = ToNumber(args, s);
            Double d1 = (Double)args2.PopArg();
            args2 = ToNumber(args, s);
            Double d2 = (Double)args2.PopArg();

            int div = (int)(d1 / d2);
            Double rem = d1 - (d2 * div);

            VarArgs retArgs = new VarArgs();
            retArgs.PushArg(rem);
            return retArgs;
        }
Beispiel #15
0
 static VarArgs Error(VarArgs args, LamnEngine s)
 {
     throw new VMException();
 }
Beispiel #16
0
 private static VarArgs DebugGetInfo(VarArgs args, LamnEngine s)
 {
     VarArgs retArgs = new VarArgs();
     List<ReturnPoint> stack = s.LamnState.GetStackTrace();
     int func = (int)((double)args.PopArg() - 1);
     String spec = (String)args.PopArg();
     if (spec.Equals("n") && func < stack.Count) {
         Table t = new Table();
         ReturnPoint retPoint = stack[func];
         t.RawPut("name", retPoint.instructionPointer.CurrentFunction.Name);
         retArgs.PushArg(t);
     }
     return retArgs;
 }
Beispiel #17
0
 private static void CoroutineYield(VarArgs args, LamnEngine s)
 {
     s.LamnState.YieldThread(args);
 }
Beispiel #18
0
        static void PCall(VarArgs args, LamnEngine s)
        {
            Object func = args.PopArg();

            s.LamnState.MarkExceptionHandler();

            s.LamnState.CurrentThread.PushStack(func);
            s.LamnState.CurrentThread.PushStack(args);
            s.LamnState.DoCALL(OpCodes.MakeCALL(1));
        }
Beispiel #19
0
        static VarArgs Print(VarArgs input, LamnEngine s)
        {
            foreach (Object o in input.Args)
            {
                if (o == null)
                {
                    s.LamnState.OutStream.Write("nil");
                }
                else if (o is Double)
                {
                    s.LamnState.OutStream.Write((Double)o);
                }
                else if (o is String)
                {
                    s.LamnState.OutStream.Write((String)o);
                }
                else if (o is Boolean)
                {
                    s.LamnState.OutStream.Write((Boolean)o);
                }
                else if (o is Table)
                {
                    s.LamnState.OutStream.Write(((Table)o).ToString());
                }
                else
                {
                    s.LamnState.OutStream.Write("[Unknown]");
                }
                s.LamnState.OutStream.Write("\t");
            }

            s.LamnState.OutStream.Write("\n");

            return new VarArgs();
        }
Beispiel #20
0
        public void YieldThread(VarArgs args)
        {
            ThreadStack.Pop();

            args.PushArg(true);
            CurrentThread.PushStack(args);

            CurrentIP.InstructionIndex++;
        }
Beispiel #21
0
 static VarArgs Require(VarArgs input, LamnEngine s)
 {
     VarArgs retArgs = new VarArgs();
     String arg = (String)input.PopArg();
     if (arg.Equals("debug"))
     {
         retArgs.PushArg(GetDebugTable ());
     }
     return retArgs;
 }
Beispiel #22
0
        public void DoCALL(UInt32 instruction)
        {
            int numArgs = (int)((instruction & OpCodes.OP1_MASK) >> OpCodes.OP1_SHIFT);
            int numPop = (int)((instruction & OpCodes.OP2_MASK) >> OpCodes.OP2_SHIFT);

            VarArgs args = new VarArgs();
            for (int i = 0; i < numArgs; i++)
            {
                if (i == 0) {
                    args.PushLastArg(CurrentThread.PopStack());
                }
                else {
                    args.PushArg(CurrentThread.PopStack());
                }
            }

            Object o = CurrentThread.PopStack();

            if (o is Closure)
            {
                Closure closure = (Closure)o;
                Function f = closure.Func;

                InstructionPointer newIP = null;
                newIP = new InstructionPointer(f, closure.ClosedVars, 0);

                ReturnPoint retPoint = new ReturnPoint(CurrentIP, numPop);

                CurrentThread.PushStack(retPoint);
                CurrentThread.PushStack(CurrentThread.BasePosition);
                CurrentThread.BasePosition = CurrentThread.StackPosition - 1;
                CurrentThread.PushStack(args);

                CurrentIP = newIP;
            }
            else if (o is NativeFuncDelegate)
            {
                NativeFuncDelegate nativeFunc = (NativeFuncDelegate)o;
                VarArgs returnArgs = nativeFunc(args, Engine);
                CurrentThread.PushStack(returnArgs);

                CurrentIP.InstructionIndex++;
            }
            else if (o is NativeCoreFuncDelegate)
            {
                NativeCoreFuncDelegate nativeFunc = (NativeCoreFuncDelegate)o;
                nativeFunc(args, Engine);
            }
            else
            {
                throw new VMException();
            }
        }
Beispiel #23
0
        static VarArgs Assert(VarArgs args, LamnEngine s)
        {
            Object o = args.PopArg();
            if (!State.IsValueTrue(o))
            {
                throw new VMException();
            }

            VarArgs newArgs = new VarArgs();
            newArgs.PushArg(o);
            return newArgs;
        }