-
-
Notifications
You must be signed in to change notification settings - Fork 548
/
ArrayConstructor.cs
456 lines (387 loc) · 15.2 KB
/
ArrayConstructor.cs
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Jint.Collections;
using Jint.Native.Function;
using Jint.Native.Iterator;
using Jint.Native.Object;
using Jint.Native.Symbol;
using Jint.Runtime;
using Jint.Runtime.Descriptors;
using Jint.Runtime.Interop;
namespace Jint.Native.Array
{
public sealed class ArrayConstructor : FunctionInstance, IConstructor
{
private static readonly JsString _functionName = new JsString("Array");
private ArrayConstructor(Engine engine) : base(engine, _functionName)
{
}
public ArrayPrototype PrototypeObject { get; private set; }
public static ArrayConstructor CreateArrayConstructor(Engine engine)
{
var obj = new ArrayConstructor(engine)
{
_prototype = engine.Function.PrototypeObject
};
// The value of the [[Prototype]] internal property of the Array constructor is the Function prototype object
obj.PrototypeObject = ArrayPrototype.CreatePrototypeObject(engine, obj);
obj._length = new PropertyDescriptor(1, PropertyFlag.Configurable);
// The initial value of Array.prototype is the Array prototype object
obj._prototypeDescriptor = new PropertyDescriptor(obj.PrototypeObject, PropertyFlag.AllForbidden);
return obj;
}
protected override void Initialize()
{
var properties = new PropertyDictionary(3, checkExistingKeys: false)
{
["from"] = new PropertyDescriptor(new PropertyDescriptor(new ClrFunctionInstance(Engine, "from", From, 1, PropertyFlag.Configurable), PropertyFlag.NonEnumerable)),
["isArray"] = new PropertyDescriptor(new PropertyDescriptor(new ClrFunctionInstance(Engine, "isArray", IsArray, 1), PropertyFlag.NonEnumerable)),
["of"] = new PropertyDescriptor(new PropertyDescriptor(new ClrFunctionInstance(Engine, "of", Of, 0, PropertyFlag.Configurable), PropertyFlag.NonEnumerable))
};
SetProperties(properties);
var symbols = new SymbolDictionary(1)
{
[GlobalSymbolRegistry.Species] = new GetSetPropertyDescriptor(get: new ClrFunctionInstance(Engine, "get [Symbol.species]", Species, 0, PropertyFlag.Configurable), set: Undefined,PropertyFlag.Configurable),
};
SetSymbols(symbols);
}
private JsValue From(JsValue thisObj, JsValue[] arguments)
{
var source = arguments.At(0);
var mapFunction = arguments.At(1);
var callable = !mapFunction.IsUndefined() ? GetCallable(mapFunction) : null;
var thisArg = arguments.At(2);
if (source.IsNullOrUndefined())
{
ExceptionHelper.ThrowTypeError(_engine, "Cannot convert undefined or null to object");
}
if (source is JsString jsString)
{
var a = _engine.Array.ConstructFast((uint) jsString.Length);
for (int i = 0; i < jsString._value.Length; i++)
{
a.SetIndexValue((uint) i, JsString.Create(jsString._value[i]), updateLength: false);
}
return a;
}
if (thisObj.IsNull() || !(source is ObjectInstance objectInstance))
{
return _engine.Array.ConstructFast(0);
}
if (objectInstance is IObjectWrapper wrapper && wrapper.Target is IEnumerable enumerable)
{
return ConstructArrayFromIEnumerable(enumerable);
}
if (objectInstance.IsArrayLike)
{
return ConstructArrayFromArrayLike(thisObj, objectInstance, callable, thisArg);
}
ObjectInstance instance;
if (thisObj is IConstructor constructor)
{
instance = constructor.Construct(System.Array.Empty<JsValue>(), thisObj);
}
else
{
instance = _engine.Array.ConstructFast(0);
}
if (objectInstance.TryGetIterator(_engine, out var iterator))
{
var protocol = new ArrayProtocol(_engine, thisArg, instance, iterator, callable);
protocol.Execute();
}
return instance;
}
private ObjectInstance ConstructArrayFromArrayLike(
JsValue thisObj,
ObjectInstance objectInstance,
ICallable callable,
JsValue thisArg)
{
var source = ArrayOperations.For(objectInstance);
var length = source.GetLength();
ObjectInstance a;
if (thisObj is IConstructor constructor)
{
var argumentsList = objectInstance.Get(GlobalSymbolRegistry.Iterator).IsNullOrUndefined()
? new JsValue[] { length }
: null;
a = Construct(constructor, argumentsList);
}
else
{
a = _engine.Array.ConstructFast(length);
}
var args = !ReferenceEquals(callable, null)
? _engine._jsValueArrayPool.RentArray(2)
: null;
var target = ArrayOperations.For(a);
uint n = 0;
for (uint i = 0; i < length; i++)
{
JsValue jsValue;
source.TryGetValue(i, out var value);
if (!ReferenceEquals(callable, null))
{
args[0] = value;
args[1] = i;
jsValue = callable.Call(thisArg, args);
// function can alter data
length = source.GetLength();
}
else
{
jsValue = value;
}
target.Set(i, jsValue, updateLength: false, throwOnError: false);
n++;
}
if (!ReferenceEquals(callable, null))
{
_engine._jsValueArrayPool.ReturnArray(args);
}
target.SetLength(length);
return a;
}
internal sealed class ArrayProtocol : IteratorProtocol
{
private readonly JsValue _thisArg;
private readonly ArrayOperations _instance;
private readonly ICallable _callable;
private long _index = -1;
public ArrayProtocol(
Engine engine,
JsValue thisArg,
ObjectInstance instance,
IIterator iterator,
ICallable callable) : base(engine, iterator, 2)
{
_thisArg = thisArg;
_instance = ArrayOperations.For(instance);
_callable = callable;
}
protected override void ProcessItem(JsValue[] args, JsValue currentValue)
{
_index++;
var sourceValue = ExtractValueFromIteratorInstance(currentValue);
JsValue jsValue;
if (!ReferenceEquals(_callable, null))
{
args[0] = sourceValue;
args[1] = _index;
jsValue = _callable.Call(_thisArg, args);
}
else
{
jsValue = sourceValue;
}
_instance.Set((uint) _index, jsValue, updateLength: false, throwOnError: true);
}
protected override void IterationEnd()
{
_instance.SetLength((ulong) (_index + 1));
}
}
private JsValue Of(JsValue thisObj, JsValue[] arguments)
{
var len = arguments.Length;
ObjectInstance a;
if (thisObj.IsConstructor)
{
a = ((IConstructor) thisObj).Construct(new JsValue[] { len }, thisObj);
for (uint k = 0; k < arguments.Length; k++)
{
var kValue = arguments[k];
var key = JsString.Create(k);
a.CreateDataPropertyOrThrow(key, kValue);
}
a.Set(CommonProperties.Length, len, true);
}
else
{
// faster for real arrays
ArrayInstance ai;
a = ai = _engine.Array.Construct(len);
for (uint k = 0; k < arguments.Length; k++)
{
var kValue = arguments[k];
ai.SetIndexValue(k, kValue, updateLength: false);
}
ai.SetLength((uint) arguments.Length);
}
return a;
}
private static JsValue Species(JsValue thisObject, JsValue[] arguments)
{
return thisObject;
}
private static JsValue IsArray(JsValue thisObj, JsValue[] arguments)
{
var o = arguments.At(0);
return IsArray(o);
}
private static JsValue IsArray(JsValue o)
{
if (!(o is ObjectInstance oi))
{
return JsBoolean.False;
}
return oi.IsArray();
}
public override JsValue Call(JsValue thisObject, JsValue[] arguments)
{
return Construct(arguments, thisObject);
}
public ObjectInstance Construct(JsValue[] arguments)
{
return Construct(arguments, this);
}
public ObjectInstance Construct(JsValue[] arguments, JsValue newTarget)
{
if (newTarget.IsUndefined())
{
newTarget = this;
}
var proto = GetPrototypeFromConstructor(newTarget, PrototypeObject);
// check if we can figure out good size
var capacity = arguments.Length > 0 ? (uint) arguments.Length : 0;
if (arguments.Length == 1 && arguments[0].IsNumber())
{
var number = ((JsNumber) arguments[0])._value;
ValidateLength(number);
capacity = (uint) number;
}
return Construct(arguments, capacity, proto);
}
public ArrayInstance Construct(int capacity)
{
return Construct(System.Array.Empty<JsValue>(), (uint) capacity);
}
public ArrayInstance Construct(uint capacity)
{
return Construct(System.Array.Empty<JsValue>(), capacity);
}
public ArrayInstance Construct(JsValue[] arguments, uint capacity)
{
return Construct(arguments, capacity, PrototypeObject);
}
private ArrayInstance Construct(JsValue[] arguments, uint capacity, ObjectInstance prototypeObject)
{
var instance = ArrayCreate(capacity, prototypeObject);
if (arguments.Length == 1 && arguments.At(0).IsNumber())
{
var length = TypeConverter.ToNumber(arguments.At(0));
ValidateLength(length);
instance._length = new PropertyDescriptor(length, PropertyFlag.OnlyWritable);
}
else if (arguments.Length == 1 && arguments[0] is IObjectWrapper objectWrapper)
{
if (objectWrapper.Target is IEnumerable enumerable)
{
return ConstructArrayFromIEnumerable(enumerable);
}
}
else if (arguments.Length == 1 && arguments[0] is ArrayInstance arrayInstance)
{
// direct copy
return (ArrayInstance) ConstructArrayFromArrayLike(Undefined, arrayInstance, null, this);
}
else
{
instance._length = new PropertyDescriptor(0, PropertyFlag.OnlyWritable);
if (arguments.Length > 0)
{
PrototypeObject.Push(instance, arguments);
}
}
return instance;
}
/// <summary>
/// https://tc39.es/ecma262/#sec-arraycreate
/// </summary>
private ArrayInstance ArrayCreate(uint capacity, ObjectInstance proto)
{
proto ??= PrototypeObject;
var instance = new ArrayInstance(Engine, capacity)
{
_prototype = proto
};
return instance;
}
private ArrayInstance ConstructArrayFromIEnumerable(IEnumerable enumerable)
{
var jsArray = (ArrayInstance) Construct(Arguments.Empty);
var tempArray = _engine._jsValueArrayPool.RentArray(1);
foreach (var item in enumerable)
{
var jsItem = FromObject(Engine, item);
tempArray[0] = jsItem;
Engine.Array.PrototypeObject.Push(jsArray, tempArray);
}
_engine._jsValueArrayPool.ReturnArray(tempArray);
return jsArray;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal ArrayInstance ConstructFast(ulong length)
{
ValidateLength(length);
var instance = new ArrayInstance(Engine, (uint) length)
{
_prototype = PrototypeObject,
_length = new PropertyDescriptor(length, PropertyFlag.OnlyWritable)
};
return instance;
}
public ObjectInstance ArraySpeciesCreate(ObjectInstance originalArray, ulong length)
{
var isArray = originalArray.IsArray();
if (!isArray)
{
return ConstructFast(length);
}
var c = originalArray.Get(CommonProperties.Constructor);
// If IsConstructor(C) is true, then
// Let thisRealm be the current Realm Record.
// Let realmC be ? GetFunctionRealm(C).
// If thisRealm and realmC are not the same Realm Record, then
// If SameValue(C, realmC.[[Intrinsics]].[[%Array%]]) is true, set C to undefined.
if (c is ObjectInstance oi)
{
c = oi.Get(GlobalSymbolRegistry.Species);
if (c.IsNull())
{
c = Undefined;
}
}
if (c.IsUndefined())
{
return ConstructFast(length);
}
if (!c.IsConstructor)
{
ExceptionHelper.ThrowTypeError(_engine);
}
return ((IConstructor) c).Construct(new JsValue[] { JsNumber.Create(length) }, c);
}
internal JsValue CreateArrayFromList(List<JsValue> values)
{
var jsArray = ConstructFast((uint) values.Count);
var index = 0;
for (; index < values.Count; index++)
{
var item = values[index];
jsArray.SetIndexValue((uint) index, item, false);
}
jsArray.SetLength((uint) index);
return jsArray;
}
private void ValidateLength(double length)
{
if (length < 0 || length > ArrayOperations.MaxArrayLength || ((long) length) != length)
{
ExceptionHelper.ThrowRangeError<object>(_engine, "Invalid array length");
}
}
}
}