diff Jint.Runtime/VM/Frame.cs @ 3:aced2ae9957f

temp commit, new virtual machine concept (strongly typed version of VM2).
author cin
date Sun, 27 Oct 2013 17:23:25 +0400
parents e113095f1de0
children 1ae5b10f7a10
line wrap: on
line diff
--- a/Jint.Runtime/VM/Frame.cs	Fri Oct 25 15:52:16 2013 +0400
+++ b/Jint.Runtime/VM/Frame.cs	Sun Oct 27 17:23:25 2013 +0400
@@ -1,59 +1,42 @@
-using System;
-
-namespace Jint.Runtime.VM
-{
-	public class Frame
-	{
-		BoxBase[] m_data;
-		RuntimeContext m_runtimeContext;
-
-		public Frame (int size, RuntimeContext runtime)
-		{
-			if (size < 0)
-				throw new ArgumentOutOfRangeException ("size");
-			if (runtime == null)
-				throw new ArgumentNullException ("runtime");
-
-			m_data = new BoxBase[size];
-			m_runtimeContext = runtime;
-		}
-
-		public RuntimeContext Runtime {
-			get { return m_runtimeContext; }
-		}
-
-		public T Get<T> (int index)
-		{
-			var bbox = m_data [index];
-
-			if (bbox == null)
-				return default(T);
-
-			var box = bbox as IGetter<T>;
-			if (box != null)
-				return box.Get();
-			else
-				return bbox.Convert<T>();
-		}
-
-		public void Set<T> (int index, T value)
-		{
-			var bbox = m_data [index];
-			var box = bbox as ISetter<T>;
-			if (box != null)
-				box.Set (value);
-			else
-				m_data [index] = m_runtimeContext.BoxValue(value);
-		}
-
-		public BoxBase GetBox(int index) {
-			return m_data[index];
-		}
-
-		public void SetBox(int index, BoxBase box) {
-			m_data [index] = box;
-		}
-
-	}
-}
-
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+
+namespace Jint.Runtime.VM {
+    class Frame {
+        IBox[] m_registers;
+        RuntimeContext m_runtime;
+
+        public Frame(int size, RuntimeContext runtime) {
+            if (runtime == null)
+                throw new ArgumentNullException("runtime");
+            if (size < 0)
+                throw new ArgumentOutOfRangeException("size");
+            m_runtime = runtime;
+            m_registers = new IBox[size];
+        }
+
+        public IBox this[int index] {
+            get {
+                return m_registers[index];
+            }
+            set {
+                m_registers[index] = value;
+            }
+        }
+
+        public T GetValue<T>(int index) {
+            // TODO handle conversion errors
+            return ((Box<T>)m_registers[index]).holdingValue;
+        }
+
+        public void SetValue<T>(int index, T value) {
+            var reg = m_registers[index] as Box<T>;
+            if (reg == null)
+                m_registers[index] = m_runtime.BoxValue(value);
+            else
+                reg.holdingValue = value;
+        }
+    }
+}