This repository was archived by the owner on Apr 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathArguments.cs
More file actions
328 lines (285 loc) · 11 KB
/
Arguments.cs
File metadata and controls
328 lines (285 loc) · 11 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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
//------------------------------------------------------------------------------
// <license file="Arguments.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 class implements the "arguments" object.
///
/// See ECMA 10.1.8
///
/// </summary>
sealed class Arguments : IdScriptableObject
{
override public string ClassName
{
get
{
return "Arguments";
}
}
override protected internal int MaxInstanceId
{
get
{
return MAX_INSTANCE_ID;
}
}
public Arguments (BuiltinCall activation)
{
this.activation = activation;
IScriptable parent = activation.ParentScope;
ParentScope = parent;
SetPrototype (ScriptableObject.GetObjectPrototype (parent));
args = activation.originalArgs;
lengthObj = (int)args.Length;
BuiltinFunction f = activation.function;
calleeObj = f;
Context.Versions version = f.LanguageVersion;
if (version <= Context.Versions.JS1_3 && version != Context.Versions.Default) {
callerObj = null;
}
else {
callerObj = UniqueTag.NotFound;
}
}
public override bool Has (int index, IScriptable start)
{
if (0 <= index && index < args.Length) {
if (args [index] != UniqueTag.NotFound) {
return true;
}
}
return base.Has (index, start);
}
public override object Get (int index, IScriptable start)
{
if (0 <= index && index < args.Length) {
object value = args [index];
if (value != UniqueTag.NotFound) {
if (sharedWithActivation (index)) {
BuiltinFunction f = activation.function;
string argName = f.getParamOrVarName (index);
value = activation.Get (argName, activation);
if (value == UniqueTag.NotFound)
Context.CodeBug ();
}
return value;
}
}
return base.Get (index, start);
}
private bool sharedWithActivation (int index)
{
BuiltinFunction f = activation.function;
int definedCount = f.ParamCount;
if (index < definedCount) {
// Check if argument is not hidden by later argument with the same
// name as hidden arguments are not shared with activation
if (index < definedCount - 1) {
string argName = f.getParamOrVarName (index);
for (int i = index + 1; i < definedCount; i++) {
if (argName.Equals (f.getParamOrVarName (i))) {
return false;
}
}
}
return true;
}
return false;
}
public override object Put (int index, IScriptable start, object value)
{
if (0 <= index && index < args.Length) {
if (args [index] != UniqueTag.NotFound) {
if (sharedWithActivation (index)) {
string argName;
argName = activation.function.getParamOrVarName (index);
return activation.Put (argName, activation, value);
}
lock (this) {
if (args [index] != UniqueTag.NotFound) {
if (args == activation.originalArgs) {
args = new object [args.Length];
args.CopyTo (args, 0);
}
args [index] = value;
return value;
}
}
}
}
return base.Put (index, start, value);
}
public override void Delete (int index)
{
if (0 <= index && index < args.Length) {
lock (this) {
if (args [index] != UniqueTag.NotFound) {
if (args == activation.originalArgs) {
args = new object [args.Length];
args.CopyTo (args, 0);
}
args [index] = UniqueTag.NotFound;
return;
}
}
}
base.Delete (index);
}
#region InstanceIds
private const int Id_callee = 1;
private const int Id_length = 2;
private const int Id_caller = 3;
private const int MAX_INSTANCE_ID = 3;
#endregion
protected internal override int FindInstanceIdInfo (string s)
{
int id;
#region Generated InstanceId Switch
L0: {
id = 0;
string X = null;
int c;
if (s.Length == 6) {
c = s [5];
if (c == 'e') { X = "callee"; id = Id_callee; }
else if (c == 'h') { X = "length"; id = Id_length; }
else if (c == 'r') { X = "caller"; id = Id_caller; }
}
if (X != null && X != s && !X.Equals (s))
id = 0;
}
EL0:
#endregion
if (id == 0)
return base.FindInstanceIdInfo (s);
int attr;
switch (id) {
case Id_callee:
case Id_caller:
case Id_length:
attr = DONTENUM;
break;
default:
throw new Exception ();
}
return InstanceIdInfo (attr, id);
}
// #/string_id_map#
protected internal override string GetInstanceIdName (int id)
{
switch (id) {
case Id_callee:
return "callee";
case Id_length:
return "length";
case Id_caller:
return "caller";
}
return null;
}
protected internal override object GetInstanceIdValue (int id)
{
switch (id) {
case Id_callee:
return calleeObj;
case Id_length:
return lengthObj;
case Id_caller: {
object value = callerObj;
if (value == UniqueTag.NullValue) {
value = null;
}
else if (value == null) {
BuiltinCall caller = activation.parentActivationCall;
if (caller != null) {
value = caller.Get ("arguments", caller);
}
else {
value = null;
}
}
return value;
}
}
return base.GetInstanceIdValue (id);
}
protected internal override void SetInstanceIdValue (int id, object value)
{
switch (id) {
case Id_callee:
calleeObj = value;
return;
case Id_length:
lengthObj = value;
return;
case Id_caller:
callerObj = (value != null) ? value : UniqueTag.NullValue;
return;
}
base.SetInstanceIdValue (id, value);
}
internal override object [] GetIds (bool getAll)
{
object [] ids = base.GetIds (getAll);
if (getAll && args.Length != 0) {
bool [] present = null;
int extraCount = args.Length;
for (int i = 0; i != ids.Length; ++i) {
object id = ids [i];
if (id is int) {
int index = ((int)id);
if (0 <= index && index < args.Length) {
if (present == null) {
present = new bool [args.Length];
}
if (!present [index]) {
present [index] = true;
extraCount--;
}
}
}
}
if (extraCount != 0) {
object [] tmp = new object [extraCount + ids.Length];
Array.Copy (ids, 0, tmp, extraCount, ids.Length);
ids = tmp;
int offset = 0;
for (int i = 0; i != args.Length; ++i) {
if (present == null || !present [i]) {
ids [offset] = (int)i;
++offset;
}
}
if (offset != extraCount)
Context.CodeBug ();
}
}
return ids;
}
// Fields to hold caller, callee and length properties,
// where NOT_FOUND value tags deleted properties.
// In addition if callerObj == NullValue, it tags null for scripts, as
// initial callerObj == null means access to caller arguments available
// only in JS <= 1.3 scripts
private object callerObj;
private object calleeObj;
private object lengthObj;
private BuiltinCall activation;
// Initially args holds activation.getOriginalArgs(), but any modification
// of its elements triggers creation of a copy. If its element holds NOT_FOUND,
// it indicates deleted index, in which case super class is queried.
private object [] args;
}
}