6
|
1 using System;
|
|
2 using System.Collections.Generic;
|
|
3 using System.Linq;
|
|
4 using System.Text;
|
|
5 using Jint.Runtime.VM.OpCodes;
|
|
6 using System.ComponentModel;
|
|
7
|
|
8 namespace Jint.Runtime.VM {
|
|
9 class Box<T>: AbstractBox, IGettter<T>, ISetter<T> {
|
|
10
|
|
11 public T holdingValue;
|
|
12 object[] m_impl;
|
|
13 RuntimeContext m_runtime;
|
|
14 TypeConverter m_converter;
|
|
15
|
|
16 public Box(T value, RuntimeContext runtime): base(typeof(T)) {
|
|
17 if (runtime == null)
|
|
18 throw new ArgumentNullException("runtime");
|
|
19
|
|
20 holdingValue = value;
|
|
21 m_runtime = runtime;
|
|
22 }
|
|
23
|
|
24 object[] Impl {
|
|
25 get {
|
|
26 if (m_impl == null)
|
|
27 m_impl = m_runtime.GetImpl(typeof(T));
|
|
28 return m_impl;
|
|
29 }
|
|
30 }
|
|
31
|
|
32 TypeConverter TypeConverter {
|
|
33 get {
|
|
34 if (m_converter == null)
|
|
35 m_converter = TypeDescriptor.GetConverter(typeof(T));
|
|
36 return m_converter;
|
|
37 }
|
|
38 }
|
|
39
|
|
40 public T Get() {
|
|
41 return holdingValue;
|
|
42 }
|
|
43
|
|
44 public void Set(T value) {
|
|
45 holdingValue = value;
|
|
46 }
|
|
47
|
|
48 public override void InvokeBinaryOperation(Codes code, int arg2, int dest, Frame frame) {
|
|
49 var op = (BinaryOperation<T>)Impl[(int)code];
|
|
50 frame.SetValue(dest, op(holdingValue, frame.GetValue<T>(arg2)));
|
|
51 }
|
|
52
|
|
53 public override void InvokeUnaryOperation(Codes code, int dest, Frame frame) {
|
|
54 var op = (UnaryOperation<T>)Impl[(int)code];
|
|
55 frame.SetValue(dest, op(holdingValue));
|
|
56 }
|
|
57
|
|
58 public override int InvokeCompareOperation(int arg2, Frame frame) {
|
|
59 var op = (CompareOperation<T>)Impl[(int)Codes.Cmp];
|
|
60 return op(holdingValue, frame.GetValue<T>(arg2));
|
|
61 }
|
|
62
|
|
63 public override bool InvokeEqualityOperation(int arg2, Frame frame) {
|
|
64 var op = (EqualityOperation<T>)Impl[(int)Codes.Cmp];
|
|
65 return op(holdingValue, frame.GetValue<T>(arg2));
|
|
66 }
|
|
67
|
|
68 public override T2 Convert<T2>() {
|
|
69 return (T2)TypeConverter.ConvertTo(holdingValue, typeof(T2));
|
|
70 }
|
|
71
|
|
72 public override void CopyTo (Frame frame, int dest)
|
|
73 {
|
|
74 frame.SetValue (dest, holdingValue);
|
|
75 }
|
|
76 }
|
|
77 }
|