Mercurial > pub > Jint1
comparison Jint.Runtime/VM/Frame.cs @ 4:1ae5b10f7a10
Code cleanup, minor refactoring
author | cin |
---|---|
date | Sun, 27 Oct 2013 21:20:59 +0400 |
parents | aced2ae9957f |
children | cb13da6e3349 |
comparison
equal
deleted
inserted
replaced
3:aced2ae9957f | 4:1ae5b10f7a10 |
---|---|
3 using System.Linq; | 3 using System.Linq; |
4 using System.Text; | 4 using System.Text; |
5 | 5 |
6 namespace Jint.Runtime.VM { | 6 namespace Jint.Runtime.VM { |
7 class Frame { | 7 class Frame { |
8 IBox[] m_registers; | 8 AbstractBox[] m_registers; |
9 RuntimeContext m_runtime; | 9 RuntimeContext m_runtime; |
10 | 10 |
11 public Frame(int size, RuntimeContext runtime) { | 11 public Frame(int size, RuntimeContext runtime) { |
12 if (runtime == null) | 12 if (runtime == null) |
13 throw new ArgumentNullException("runtime"); | 13 throw new ArgumentNullException("runtime"); |
14 if (size < 0) | 14 if (size < 0) |
15 throw new ArgumentOutOfRangeException("size"); | 15 throw new ArgumentOutOfRangeException("size"); |
16 m_runtime = runtime; | 16 m_runtime = runtime; |
17 m_registers = new IBox[size]; | 17 m_registers = new AbstractBox[size]; |
18 } | 18 } |
19 | 19 |
20 public IBox this[int index] { | 20 public AbstractBox this[int index] { |
21 get { | 21 get { |
22 return m_registers[index]; | 22 return m_registers[index]; |
23 } | 23 } |
24 set { | 24 set { |
25 m_registers[index] = value; | 25 m_registers[index] = value; |
26 } | 26 } |
27 } | 27 } |
28 | 28 |
29 /// <summary> | |
30 /// Extracts value stored in the registry specified by the index. | |
31 /// </summary> | |
32 /// <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> | |
33 /// <typeparam name="T">The type of the value stored in the registry.</typeparam> | |
34 /// <param name="index">The index of the registry.</param> | |
35 /// <returns>The value stored in the registry.</returns> | |
29 public T GetValue<T>(int index) { | 36 public T GetValue<T>(int index) { |
30 // TODO handle conversion errors | |
31 return ((Box<T>)m_registers[index]).holdingValue; | 37 return ((Box<T>)m_registers[index]).holdingValue; |
32 } | 38 } |
33 | 39 |
40 /// <summary> | |
41 /// Stores a new value in the register specified by the index. | |
42 /// </summary> | |
43 /// <remarks> | |
44 /// If the previous value has the same type as the value being stored in the registry, | |
45 /// the new value will replace the old one, otherwise the registry will be reallocated to | |
46 /// store the new value. | |
47 /// </remarks> | |
48 /// <typeparam name="T">The type of the value being stored</typeparam> | |
49 /// <param name="index">The index of the registry where the value will be stored</param> | |
50 /// <param name="value">The value to be stored in the registry</param> | |
34 public void SetValue<T>(int index, T value) { | 51 public void SetValue<T>(int index, T value) { |
35 var reg = m_registers[index] as Box<T>; | 52 var reg = m_registers[index] as Box<T>; |
36 if (reg == null) | 53 if (reg == null || reg.holdingType != typeof(T)) |
37 m_registers[index] = m_runtime.BoxValue(value); | 54 m_registers[index] = m_runtime.BoxValue(value); |
38 else | 55 else |
39 reg.holdingValue = value; | 56 reg.holdingValue = value; |
40 } | 57 } |
41 } | 58 } |