Mercurial > pub > Jint1
changeset 4:1ae5b10f7a10
Code cleanup, minor refactoring
author | cin |
---|---|
date | Sun, 27 Oct 2013 21:20:59 +0400 |
parents | aced2ae9957f |
children | cb13da6e3349 |
files | Jint.Runtime/Jint.Runtime.csproj Jint.Runtime/VM/AbstractBox.cs Jint.Runtime/VM/Box.cs Jint.Runtime/VM/Frame.cs Jint.Runtime/VM/IBox.cs Jint.Runtime/VM/OpCodes/Codes.cs Jint.Runtime/VM/OperationDelegates.cs Jint.suo |
diffstat | 8 files changed, 71 insertions(+), 38 deletions(-) [+] |
line wrap: on
line diff
--- 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 @@ <ItemGroup> <Compile Include="Main.cs" /> <Compile Include="AssemblyInfo.cs" /> + <Compile Include="VM\AbstractBox.cs" /> <Compile Include="VM\Box.cs" /> <Compile Include="VM\Frame.cs" /> - <Compile Include="VM\IBox.cs" /> <Compile Include="VM\IGettter.cs" /> <Compile Include="VM\IInstruction.cs" /> <Compile Include="VM\ISetter.cs" /> <Compile Include="VM\Machine.cs" /> <Compile Include="VM\OpCodes\BinaryOp.cs" /> <Compile Include="VM\OpCodes\Codes.cs" /> + <Compile Include="VM\OpCodes\Loop.cs" /> <Compile Include="VM\OperationDelegates.cs" /> <Compile Include="VM\RuntimeContext.cs" /> </ItemGroup>
--- /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); + } +}
--- 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<T>: IBox, IGettter<T>, ISetter<T> { + class Box<T>: AbstractBox, IGettter<T>, ISetter<T> { 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<T>)Impl[(int)code]; - frame.SetValue(dest, op(holdingValue, frame.GetValue<T>(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<T>)Impl[(int)code]; + frame.SetValue(dest, op(holdingValue, frame.GetValue<T>(arg2))); + } + + public override void InvokeUnaryOperation(Codes code, int dest, Frame frame) { + var op = (UnaryOperation<T>)Impl[(int)code]; + frame.SetValue(dest, op(holdingValue)); + } + + public override void InvokeCompareOperation(int arg2, int dest, Frame frame) { + var op = (CompareOperation<T>)Impl[(int)Codes.Cmp]; + frame.SetValue(dest, op(holdingValue, frame.GetValue<T>(arg2))); + } + + public override void InvokeEqualityOperation(int arg2, int dest, Frame frame) { + var op = (EqualityOperation<T>)Impl[(int)Codes.Cmp]; + frame.SetValue(dest, op(holdingValue, frame.GetValue<T>(arg2))); + } } }
--- 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 @@ } } + /// <summary> + /// Extracts value stored in the registry specified by the index. + /// </summary> + /// <remarks>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.</remarks> + /// <typeparam name="T">The type of the value stored in the registry.</typeparam> + /// <param name="index">The index of the registry.</param> + /// <returns>The value stored in the registry.</returns> public T GetValue<T>(int index) { - // TODO handle conversion errors return ((Box<T>)m_registers[index]).holdingValue; } + /// <summary> + /// Stores a new value in the register specified by the index. + /// </summary> + /// <remarks> + /// 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. + /// </remarks> + /// <typeparam name="T">The type of the value being stored</typeparam> + /// <param name="index">The index of the registry where the value will be stored</param> + /// <param name="value">The value to be stored in the registry</param> public void SetValue<T>(int index, T value) { var reg = m_registers[index] as Box<T>; - if (reg == null) + if (reg == null || reg.holdingType != typeof(T)) m_registers[index] = m_runtime.BoxValue(value); else reg.holdingValue = value;
--- 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); - } -}
--- 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 }
--- 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>(T arg1, T arg2); public delegate T UnaryOperation<T>(T arg); public delegate int CompareOperation<T>(T arg1, T arg2); - public delegate bool EqualsOperation<T>(T arg1, T arg2); + public delegate bool EqualityOperation<T>(T arg1, T arg2); }