# HG changeset patch # User cin # Date 1382894459 -14400 # Node ID 1ae5b10f7a10bea0b1746802427983f577b2387a # Parent aced2ae9957f17b69e68f199b559b1cd2e3bc991 Code cleanup, minor refactoring diff -r aced2ae9957f -r 1ae5b10f7a10 Jint.Runtime/Jint.Runtime.csproj --- a/Jint.Runtime/Jint.Runtime.csproj Sun Oct 27 17:23:25 2013 +0400 +++ b/Jint.Runtime/Jint.Runtime.csproj Sun Oct 27 21:20:59 2013 +0400 @@ -36,15 +36,16 @@ + - + diff -r aced2ae9957f -r 1ae5b10f7a10 Jint.Runtime/VM/AbstractBox.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Jint.Runtime/VM/AbstractBox.cs Sun Oct 27 21:20:59 2013 +0400 @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace Jint.Runtime.VM { + using OpCodes; + + abstract class AbstractBox { + + protected AbstractBox(Type type) { + holdingType = type; + } + + public readonly Type holdingType; + + public abstract void InvokeBinaryOperation(Codes code, int arg2, int dest, Frame frame); + public abstract void InvokeUnaryOperation(Codes code, int dest, Frame frame); + public abstract void InvokeCompareOperation(int arg2, int dest, Frame frame); + public abstract void InvokeEqualityOperation(int arg2, int dest, Frame frame); + } +} diff -r aced2ae9957f -r 1ae5b10f7a10 Jint.Runtime/VM/Box.cs --- a/Jint.Runtime/VM/Box.cs Sun Oct 27 17:23:25 2013 +0400 +++ b/Jint.Runtime/VM/Box.cs Sun Oct 27 21:20:59 2013 +0400 @@ -5,13 +5,13 @@ using Jint.Runtime.VM.OpCodes; namespace Jint.Runtime.VM { - class Box: IBox, IGettter, ISetter { + class Box: AbstractBox, IGettter, ISetter { public T holdingValue; object[] m_impl; RuntimeContext m_runtime; - public Box(T value, RuntimeContext runtime) { + public Box(T value, RuntimeContext runtime): base(typeof(T)) { if (runtime == null) throw new ArgumentNullException("runtime"); @@ -19,7 +19,7 @@ m_runtime = runtime; } - public object[] Impl { + object[] Impl { get { if (m_impl == null) m_impl = m_runtime.GetImpl(typeof(T)); @@ -27,15 +27,6 @@ } } - public Type HoldingType { - get { return typeof(T); } - } - - public void InvokeBinaryOperation(Codes code, int arg2, int dest, Frame frame) { - var op = (BinaryOperation)Impl[(int)code]; - frame.SetValue(dest, op(holdingValue, frame.GetValue(arg2))); - } - public T Get() { return holdingValue; } @@ -43,5 +34,25 @@ public void Set(T value) { holdingValue = value; } + + public override void InvokeBinaryOperation(Codes code, int arg2, int dest, Frame frame) { + var op = (BinaryOperation)Impl[(int)code]; + frame.SetValue(dest, op(holdingValue, frame.GetValue(arg2))); + } + + public override void InvokeUnaryOperation(Codes code, int dest, Frame frame) { + var op = (UnaryOperation)Impl[(int)code]; + frame.SetValue(dest, op(holdingValue)); + } + + public override void InvokeCompareOperation(int arg2, int dest, Frame frame) { + var op = (CompareOperation)Impl[(int)Codes.Cmp]; + frame.SetValue(dest, op(holdingValue, frame.GetValue(arg2))); + } + + public override void InvokeEqualityOperation(int arg2, int dest, Frame frame) { + var op = (EqualityOperation)Impl[(int)Codes.Cmp]; + frame.SetValue(dest, op(holdingValue, frame.GetValue(arg2))); + } } } diff -r aced2ae9957f -r 1ae5b10f7a10 Jint.Runtime/VM/Frame.cs --- a/Jint.Runtime/VM/Frame.cs Sun Oct 27 17:23:25 2013 +0400 +++ b/Jint.Runtime/VM/Frame.cs Sun Oct 27 21:20:59 2013 +0400 @@ -5,7 +5,7 @@ namespace Jint.Runtime.VM { class Frame { - IBox[] m_registers; + AbstractBox[] m_registers; RuntimeContext m_runtime; public Frame(int size, RuntimeContext runtime) { @@ -14,10 +14,10 @@ if (size < 0) throw new ArgumentOutOfRangeException("size"); m_runtime = runtime; - m_registers = new IBox[size]; + m_registers = new AbstractBox[size]; } - public IBox this[int index] { + public AbstractBox this[int index] { get { return m_registers[index]; } @@ -26,14 +26,31 @@ } } + /// + /// Extracts value stored in the registry specified by the index. + /// + /// This method doesn't do any cast, if the specified type isn't the same as the type of the stored value a type cast exception will occur. + /// The type of the value stored in the registry. + /// The index of the registry. + /// The value stored in the registry. public T GetValue(int index) { - // TODO handle conversion errors return ((Box)m_registers[index]).holdingValue; } + /// + /// Stores a new value in the register specified by the index. + /// + /// + /// If the previous value has the same type as the value being stored in the registry, + /// the new value will replace the old one, otherwise the registry will be reallocated to + /// store the new value. + /// + /// The type of the value being stored + /// The index of the registry where the value will be stored + /// The value to be stored in the registry public void SetValue(int index, T value) { var reg = m_registers[index] as Box; - if (reg == null) + if (reg == null || reg.holdingType != typeof(T)) m_registers[index] = m_runtime.BoxValue(value); else reg.holdingValue = value; diff -r aced2ae9957f -r 1ae5b10f7a10 Jint.Runtime/VM/IBox.cs --- a/Jint.Runtime/VM/IBox.cs Sun Oct 27 17:23:25 2013 +0400 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,14 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using Jint.Runtime.VM.OpCodes; - -namespace Jint.Runtime.VM { - interface IBox { - Type HoldingType { - get; - } - void InvokeBinaryOperation(Codes code, int arg2, int dest, Frame frame); - } -} diff -r aced2ae9957f -r 1ae5b10f7a10 Jint.Runtime/VM/OpCodes/Codes.cs --- a/Jint.Runtime/VM/OpCodes/Codes.cs Sun Oct 27 17:23:25 2013 +0400 +++ b/Jint.Runtime/VM/OpCodes/Codes.cs Sun Oct 27 21:20:59 2013 +0400 @@ -20,12 +20,8 @@ LOr, LNot, - Gte, - Lte, - Gt, - Lt, + Cmp, Eq, - Ne, MaxOp } diff -r aced2ae9957f -r 1ae5b10f7a10 Jint.Runtime/VM/OperationDelegates.cs --- a/Jint.Runtime/VM/OperationDelegates.cs Sun Oct 27 17:23:25 2013 +0400 +++ b/Jint.Runtime/VM/OperationDelegates.cs Sun Oct 27 21:20:59 2013 +0400 @@ -7,5 +7,5 @@ public delegate T BinaryOperation(T arg1, T arg2); public delegate T UnaryOperation(T arg); public delegate int CompareOperation(T arg1, T arg2); - public delegate bool EqualsOperation(T arg1, T arg2); + public delegate bool EqualityOperation(T arg1, T arg2); } diff -r aced2ae9957f -r 1ae5b10f7a10 Jint.suo Binary file Jint.suo has changed