forked from PureKrome/EcmaScript.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDelegator.cs
More file actions
243 lines (210 loc) · 6.86 KB
/
Copy pathDelegator.cs
File metadata and controls
243 lines (210 loc) · 6.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
//------------------------------------------------------------------------------
// <license file="Delegator.cs">
//
// The use and distribution terms for this software are contained in the file
// named 'LICENSE', which can be found in the resources directory of this
// distribution.
//
// By using this software in any fashion, you are agreeing to be bound by the
// terms of this license.
//
// </license>
//------------------------------------------------------------------------------
using System;
using EcmaScript.NET.Types;
namespace EcmaScript.NET
{
/// <summary> This is a helper class for implementing wrappers around Scriptable
/// objects. It implements the Function interface and delegates all
/// invocations to a delegee Scriptable object. The normal use of this
/// class involves creating a sub-class and overriding one or more of
/// the methods.
///
/// A useful application is the implementation of interceptors,
/// pre/post conditions, debugging.
///
/// </summary>
public class Delegator : IFunction
{
/// <summary> Retrieve the delegee.
///
/// </summary>
/// <returns> the delegee
/// </returns>
/// <summary> Set the delegee.
///
/// </summary>
/// <param name="obj">the delegee
/// </param>
virtual public IScriptable Delegee
{
get
{
return obj;
}
set
{
this.obj = value;
}
}
virtual public string ClassName
{
get
{
return obj.ClassName;
}
}
virtual public IScriptable ParentScope
{
get
{
return obj.ParentScope;
}
set
{
obj.ParentScope = value;
}
}
protected internal IScriptable obj = null;
/// <summary> Create a Delegator prototype.
///
/// This constructor should only be used for creating prototype
/// objects of Delegator.
///
/// </summary>
public Delegator ()
{
}
/// <summary> Create a new Delegator that forwards requests to a delegee
/// Scriptable object.
///
/// </summary>
/// <param name="obj">the delegee
/// </param>
public Delegator (IScriptable obj)
{
this.obj = obj;
}
/// <summary> Crete new Delegator instance.
/// The default implementation calls this.getClass().newInstance().
///
/// </summary>
protected internal virtual Delegator NewInstance ()
{
try {
return (Delegator)System.Activator.CreateInstance (this.GetType ());
}
catch (Exception ex) {
throw Context.ThrowAsScriptRuntimeEx (ex);
}
}
public virtual object Get (string name, IScriptable start)
{
return obj.Get (name, start);
}
public virtual object Get (int index, IScriptable start)
{
return obj.Get (index, start);
}
public virtual bool Has (string name, IScriptable start)
{
return obj.Has (name, start);
}
public virtual bool Has (int index, IScriptable start)
{
return obj.Has (index, start);
}
public virtual object Put (string name, IScriptable start, object value)
{
return obj.Put (name, start, value);
}
public virtual object Put (int index, IScriptable start, object value)
{
return obj.Put (index, start, value);
}
public virtual void Delete (string name)
{
obj.Delete (name);
}
public virtual void Delete (int index)
{
obj.Delete (index);
}
public virtual IScriptable GetPrototype ()
{
return obj.GetPrototype ();
}
public virtual void SetPrototype (IScriptable prototype)
{
obj.SetPrototype (prototype);
}
public virtual object [] GetIds ()
{
return obj.GetIds ();
}
/// <summary> Note that this method does not get forwarded to the delegee if
/// the <code>hint</code> parameter is null,
/// <code>typeof(Scriptable)</code> or
/// <code>typeof(Function)</code>. Instead the object
/// itself is returned.
///
/// </summary>
/// <param name="hint">the type hint
/// </param>
/// <returns> the default value
///
/// </returns>
public virtual object GetDefaultValue (Type hint)
{
return (hint == null
|| hint == typeof (IScriptable)
|| hint == typeof (IFunction))
? this : obj.GetDefaultValue (hint);
}
public virtual bool HasInstance (IScriptable instance)
{
return obj.HasInstance (instance);
}
public virtual object Call (Context cx, IScriptable scope, IScriptable thisObj, object [] args)
{
return ((IFunction)obj).Call (cx, scope, thisObj, args);
}
/// <summary> Note that if the <code>delegee</code> is <code>null</code>,
/// this method creates a new instance of the Delegator itself
/// rathert than forwarding the call to the
/// <code>delegee</code>. This permits the use of Delegator
/// prototypes.
///
/// </summary>
/// <param name="cx">the current Context for this thread
/// </param>
/// <param name="scope">an enclosing scope of the caller except
/// when the function is called from a closure.
/// </param>
/// <param name="args">the array of arguments
/// </param>
/// <returns> the allocated object
///
/// </returns>
public virtual IScriptable Construct (Context cx, IScriptable scope, object [] args)
{
if (obj == null) {
//this little trick allows us to declare prototype objects for
//Delegators
Delegator n = NewInstance ();
IScriptable delegee;
if (args.Length == 0) {
delegee = new BuiltinObject ();
}
else {
delegee = ScriptConvert.ToObject (cx, scope, args [0]);
}
n.Delegee = delegee;
return n;
}
else {
return ((IFunction)obj).Construct (cx, scope, args);
}
}
}
}