public VM.RuntimeState ToRuntimeState()
        {
            VM.RuntimeState state = new VM.RuntimeState();
            state.IP       = this.IP;
            state.LSLState = this.LSLState;
            state.Globals  = SerializedLSLPrimitive.ToPrimitiveList(this.Globals);
            state.Operands = SerializedLSLPrimitive.ToPrimitiveStack(this.Operands);

            if (this.Calls != null)
            {
                state.Calls = new Stack <VM.StackFrame>(this.Calls.Length);
                //calls is a stack, so again push them in reverse order
                for (int i = this.Calls.Length - 1; i >= 0; i--)
                {
                    state.Calls.Push(this.Calls[i].ToStackFrame());
                }
            }
            else
            {
                state.Calls = new Stack <VM.StackFrame>();
            }

            if (state.Calls.Count > 0)
            {
                //DO NOT USE THE SERIALIZED TOPFRAME HERE, IT IS A DIFFERENT REFERENCE THAN
                //state.Calls.Peek!!!
                state.TopFrame = state.Calls.Peek();
            }
            else
            {
                state.TopFrame = null;
            }

            state.MemInfo = this.MemInfo;

            state.EventQueue = new C5.LinkedList <VM.PostedEvent>();
            if (this.EventQueue != null)
            {
                foreach (SerializedPostedEvent evt in this.EventQueue)
                {
                    state.EventQueue.Add(evt.ToPostedEvent());
                }
            }

            state.RunState      = this.RunState;
            state.GeneralEnable = this.Enabled;

            UInt64 currentTickCount = Util.Clock.GetLongTickCount();

            state.StateCapturedOn = currentTickCount;

            Int64 relativeNextWakeup = (Int64)currentTickCount + (Int64)(this.NextWakeup - this.StateCapturedOn).TotalMilliseconds;

            if (relativeNextWakeup < 0)
            {
                relativeNextWakeup = 0;
            }

            state.NextWakeup = (UInt64)relativeNextWakeup;

            Int64 relativeTimerLastScheduledOn = (Int64)currentTickCount + (Int64)(this.TimerLastScheduledOn - this.StateCapturedOn).TotalMilliseconds;

            if (relativeTimerLastScheduledOn < 0)
            {
                relativeTimerLastScheduledOn = 0;
            }

            state.TimerLastScheduledOn = (UInt64)relativeTimerLastScheduledOn;
            state.TimerInterval        = this.TimerInterval;

            if (this.RunningEvent != null)
            {
                state.RunningEvent = this.RunningEvent.ToPostedEvent();
            }

            if (this.ActiveListens != null)
            {
                state.ActiveListens = this.ActiveListens;
            }
            else
            {
                state.ActiveListens = new Dictionary <int, VM.ActiveListen>();
            }

            state.StartParameter = this.StartParameter;

            state.MiscAttributes = new Dictionary <int, object[]>();
            if (this.MiscAttributes != null)
            {
                foreach (KeyValuePair <int, SerializedLSLPrimitive[]> kvp in MiscAttributes)
                {
                    state.MiscAttributes[kvp.Key] = SerializedLSLPrimitive.ToPrimitiveList(kvp.Value);
                }
            }

            state.OtherRuntime         = TotalRuntime;
            state.StartTimeOnSimulator = Util.Clock.GetLongTickCount();

            return(state);
        }