6
|
1 using System;
|
|
2 using System.Collections.Generic;
|
|
3 using System.Linq;
|
|
4 using System.Text;
|
|
5
|
|
6 namespace Jint.Runtime.VM {
|
|
7 class Frame {
|
|
8 AbstractBox[] m_registers;
|
|
9 RuntimeContext m_runtime;
|
|
10
|
|
11 public const int ThisRegister = 0;
|
|
12 public const int ScopeRegister = 1;
|
|
13 public const int FirstVarRegsiter = 2;
|
|
14
|
|
15 public Frame(int size, RuntimeContext runtime) {
|
|
16 if (runtime == null)
|
|
17 throw new ArgumentNullException("runtime");
|
|
18 if (size < 0)
|
|
19 throw new ArgumentOutOfRangeException("size");
|
|
20 m_runtime = runtime;
|
|
21 m_registers = new AbstractBox[size];
|
|
22 }
|
|
23
|
|
24 /// <summary>
|
|
25 /// Return register at the specified index.
|
|
26 /// </summary>
|
|
27 /// <param name="index">The index of the register</param>
|
|
28 /// <returns>The register.</returns>
|
|
29 public AbstractBox this[int index] {
|
|
30 get {
|
|
31 return m_registers[index];
|
|
32 }
|
|
33 set {
|
|
34 m_registers[index] = value;
|
|
35 }
|
|
36 }
|
|
37
|
|
38 /// <summary>
|
|
39 /// Extracts value stored in the register specified by the index.
|
|
40 /// </summary>
|
|
41 /// <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>
|
|
42 /// <typeparam name="T">The type of the value stored in the register.</typeparam>
|
|
43 /// <param name="index">The index of the register.</param>
|
|
44 /// <returns>The value stored in the register.</returns>
|
|
45 public T GetValue<T>(int index) {
|
|
46 return ((Box<T>)m_registers[index]).holdingValue;
|
|
47 }
|
|
48
|
|
49 /// <summary>
|
|
50 /// Stores a new value in the register specified by the index.
|
|
51 /// </summary>
|
|
52 /// <remarks>
|
|
53 /// If the previous value has the same type as the value being stored in the register,
|
|
54 /// the new value will replace the old one, otherwise the register will be reallocated to
|
|
55 /// store the new value.
|
|
56 /// </remarks>
|
|
57 /// <typeparam name="T">The type of the value being stored</typeparam>
|
|
58 /// <param name="index">The index of the register where the value will be stored</param>
|
|
59 /// <param name="value">The value to be stored in the register</param>
|
|
60 public void SetValue<T>(int index, T value) {
|
|
61 var reg = m_registers[index] as Box<T>;
|
|
62 if (reg == null || reg.holdingType != typeof(T))
|
|
63 m_registers[index] = m_runtime.BoxValue(value);
|
|
64 else
|
|
65 reg.holdingValue = value;
|
|
66 }
|
|
67
|
|
68 public T GetConverted<T>(int index) {
|
|
69 var reg = m_registers[index];
|
|
70
|
|
71 if (reg.holdingType == typeof(T))
|
|
72 return ((Box<T>)reg).holdingValue;
|
|
73 else
|
|
74 return reg.Convert<T>();
|
|
75 }
|
|
76 }
|
|
77 }
|