annotate Jint.Runtime/VM/Frame.cs @ 6:a6329b092499

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