-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathES5Array.cpp
More file actions
459 lines (376 loc) · 16.6 KB
/
ES5Array.cpp
File metadata and controls
459 lines (376 loc) · 16.6 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
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
457
458
459
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#include "RuntimeLibraryPch.h"
namespace Js
{
CompileAssert(sizeof(ES5Array) == sizeof(JavascriptArray));
ES5ArrayType::ES5ArrayType(DynamicType* type)
: DynamicType(type->GetScriptContext(), TypeIds_ES5Array, type->GetPrototype(), type->GetEntryPoint(), type->GetTypeHandler(), false, false)
{
}
DynamicType* ES5Array::DuplicateType()
{
return RecyclerNew(GetScriptContext()->GetRecycler(), ES5ArrayType, this->GetDynamicType());
}
bool ES5Array::IsLengthWritable() const
{
return GetTypeHandler()->IsLengthWritable();
}
PropertyQueryFlags ES5Array::HasPropertyQuery(PropertyId propertyId, _Inout_opt_ PropertyValueInfo* info)
{
if (propertyId == PropertyIds::length)
{
return PropertyQueryFlags::Property_Found;
}
// Skip JavascriptArray override
return DynamicObject::HasPropertyQuery(propertyId, info);
}
BOOL ES5Array::IsWritable(PropertyId propertyId)
{
if (propertyId == PropertyIds::length)
{
return IsLengthWritable();
}
return __super::IsWritable(propertyId);
}
BOOL ES5Array::SetEnumerable(PropertyId propertyId, BOOL value)
{
// Skip JavascriptArray override
return DynamicObject::SetEnumerable(propertyId, value);
}
BOOL ES5Array::SetWritable(PropertyId propertyId, BOOL value)
{
// Skip JavascriptArray override
return DynamicObject::SetWritable(propertyId, value);
}
BOOL ES5Array::SetConfigurable(PropertyId propertyId, BOOL value)
{
// Skip JavascriptArray override
return DynamicObject::SetConfigurable(propertyId, value);
}
BOOL ES5Array::SetAttributes(PropertyId propertyId, PropertyAttributes attributes)
{
// Skip JavascriptArray override
return DynamicObject::SetAttributes(propertyId, attributes);
}
PropertyQueryFlags ES5Array::GetPropertyQuery(Var originalInstance, PropertyId propertyId, Var* value, PropertyValueInfo* info, ScriptContext* requestContext)
{
BOOL result;
if (GetPropertyBuiltIns(propertyId, value, &result))
{
return JavascriptConversion::BooleanToPropertyQueryFlags(result);
}
// Skip JavascriptArray override
return DynamicObject::GetPropertyQuery(originalInstance, propertyId, value, info, requestContext);
}
PropertyQueryFlags ES5Array::GetPropertyQuery(Var originalInstance, JavascriptString* propertyNameString, Var* value, PropertyValueInfo* info, ScriptContext* requestContext)
{
BOOL result;
PropertyRecord const* propertyRecord;
this->GetScriptContext()->FindPropertyRecord(propertyNameString, &propertyRecord);
if (propertyRecord != nullptr && GetPropertyBuiltIns(propertyRecord->GetPropertyId(), value, &result))
{
return JavascriptConversion::BooleanToPropertyQueryFlags(result);
}
// Skip JavascriptArray override
return DynamicObject::GetPropertyQuery(originalInstance, propertyNameString, value, info, requestContext);
}
bool ES5Array::GetPropertyBuiltIns(PropertyId propertyId, Var* value, BOOL* result)
{
if (propertyId == PropertyIds::length)
{
*value = JavascriptNumber::ToVar(this->GetLength(), GetScriptContext());
*result = true;
return true;
}
return false;
}
PropertyQueryFlags ES5Array::GetPropertyReferenceQuery(Var originalInstance, PropertyId propertyId, Var* value, PropertyValueInfo* info, ScriptContext* requestContext)
{
return ES5Array::GetPropertyQuery(originalInstance, propertyId, value, info, requestContext);
}
// Convert a Var to array length, throw RangeError if value is not valid for array length.
uint32 ES5Array::ToLengthValue(Var value, ScriptContext* scriptContext)
{
if (TaggedInt::Is(value))
{
int32 newLen = TaggedInt::ToInt32(value);
if (newLen < 0)
{
JavascriptError::ThrowRangeError(scriptContext, JSERR_ArrayLengthAssignIncorrect);
}
return static_cast<uint32>(newLen);
}
else
{
uint32 newLen = JavascriptConversion::ToUInt32(value, scriptContext);
if (newLen != JavascriptConversion::ToNumber(value, scriptContext))
{
JavascriptError::ThrowRangeError(scriptContext, JSERR_ArrayLengthAssignIncorrect);
}
// Conversion can change the type (e.g. from String), invalidating assumptions made by the JIT
scriptContext->GetThreadContext()->AddImplicitCallFlags(ImplicitCall_Accessor);
return newLen;
}
}
DescriptorFlags ES5Array::GetSetter(PropertyId propertyId, Var *setterValue, PropertyValueInfo* info, ScriptContext* requestContext)
{
DescriptorFlags result;
if (GetSetterBuiltIns(propertyId, info, &result))
{
return result;
}
return DynamicObject::GetSetter(propertyId, setterValue, info, requestContext);
}
DescriptorFlags ES5Array::GetSetter(JavascriptString* propertyNameString, Var *setterValue, PropertyValueInfo* info, ScriptContext* requestContext)
{
DescriptorFlags result;
PropertyRecord const* propertyRecord;
this->GetScriptContext()->FindPropertyRecord(propertyNameString, &propertyRecord);
if (propertyRecord != nullptr && GetSetterBuiltIns(propertyRecord->GetPropertyId(), info, &result))
{
return result;
}
return DynamicObject::GetSetter(propertyNameString, setterValue, info, requestContext);
}
bool ES5Array::GetSetterBuiltIns(PropertyId propertyId, PropertyValueInfo* info, DescriptorFlags* result)
{
if (propertyId == PropertyIds::length)
{
PropertyValueInfo::SetNoCache(info, this);
*result = IsLengthWritable() ? WritableData : Data;
return true;
}
return false;
}
BOOL ES5Array::SetProperty(PropertyId propertyId, Var value, PropertyOperationFlags propertyOperationFlags, PropertyValueInfo* info)
{
BOOL result;
if (SetPropertyBuiltIns(propertyId, value, propertyOperationFlags, &result))
{
return result;
}
return __super::SetProperty(propertyId, value, propertyOperationFlags, info);
}
BOOL ES5Array::SetProperty(JavascriptString* propertyNameString, Var value, PropertyOperationFlags propertyOperationFlags, PropertyValueInfo* info)
{
BOOL result;
PropertyRecord const* propertyRecord;
this->GetScriptContext()->FindPropertyRecord(propertyNameString, &propertyRecord);
if (propertyRecord != nullptr && SetPropertyBuiltIns(propertyRecord->GetPropertyId(), value, propertyOperationFlags, &result))
{
return result;
}
return __super::SetProperty(propertyNameString, value, propertyOperationFlags, info);
}
bool ES5Array::SetPropertyBuiltIns(PropertyId propertyId, Var value, PropertyOperationFlags propertyOperationFlags, BOOL* result)
{
ScriptContext* scriptContext = GetScriptContext();
if (propertyId == PropertyIds::length)
{
if (!GetTypeHandler()->IsLengthWritable())
{
*result = false; // reject
return true;
}
uint32 newLen = ToLengthValue(value, scriptContext);
uint32 assignedLen = GetTypeHandler()->SetLength(this, newLen, propertyOperationFlags);
if (newLen != assignedLen)
{
scriptContext->GetThreadContext()->AddImplicitCallFlags(ImplicitCall_NoOpSet);
}
*result = true;
return true;
}
return false;
}
BOOL ES5Array::SetPropertyWithAttributes(PropertyId propertyId, Var value, PropertyAttributes attributes, PropertyValueInfo* info, PropertyOperationFlags flags, SideEffects possibleSideEffects)
{
if (propertyId == PropertyIds::length)
{
Assert(attributes == PropertyWritable);
Assert(IsWritable(propertyId) && !IsConfigurable(propertyId) && !IsEnumerable(propertyId));
uint32 newLen = ToLengthValue(value, GetScriptContext());
GetTypeHandler()->SetLength(this, newLen, PropertyOperation_None);
return true;
}
return __super::SetPropertyWithAttributes(propertyId, value, attributes, info, flags, possibleSideEffects);
}
BOOL ES5Array::DeleteItem(uint32 index, PropertyOperationFlags flags)
{
// Skip JavascriptArray override
return DynamicObject::DeleteItem(index, flags);
}
PropertyQueryFlags ES5Array::HasItemQuery(uint32 index)
{
// Skip JavascriptArray override
return DynamicObject::HasItemQuery(index);
}
PropertyQueryFlags ES5Array::GetItemQuery(Var originalInstance, uint32 index, Var* value, ScriptContext * requestContext)
{
// Skip JavascriptArray override
return DynamicObject::GetItemQuery(originalInstance, index, value, requestContext);
}
PropertyQueryFlags ES5Array::GetItemReferenceQuery(Var originalInstance, uint32 index, Var* value, ScriptContext * requestContext)
{
// Skip JavascriptArray override
return DynamicObject::GetItemReferenceQuery(originalInstance, index, value, requestContext);
}
DescriptorFlags ES5Array::GetItemSetter(uint32 index, Var* setterValue, ScriptContext* requestContext)
{
// Skip JavascriptArray override
return DynamicObject::GetItemSetter(index, setterValue, requestContext);
}
BOOL ES5Array::SetItem(uint32 index, Var value, PropertyOperationFlags flags)
{
// Skip JavascriptArray override
return DynamicObject::SetItem(index, value, flags);
}
BOOL ES5Array::SetAccessors(PropertyId propertyId, Var getter, Var setter, PropertyOperationFlags flags)
{
// Skip JavascriptArray override
return DynamicObject::SetAccessors(propertyId, getter, setter, flags);
}
BOOL ES5Array::PreventExtensions()
{
// Skip JavascriptArray override
return DynamicObject::PreventExtensions();
}
BOOL ES5Array::Seal()
{
// Skip JavascriptArray override
return DynamicObject::Seal();
}
BOOL ES5Array::Freeze()
{
// Skip JavascriptArray override
return DynamicObject::Freeze();
}
BOOL ES5Array::GetEnumerator(JavascriptStaticEnumerator * enumerator, EnumeratorFlags flags, ScriptContext* requestContext, EnumeratorCache * enumeratorCache)
{
return enumerator->Initialize(nullptr, this, this, flags, requestContext, enumeratorCache);
}
JavascriptEnumerator * ES5Array::GetIndexEnumerator(EnumeratorFlags flags, ScriptContext* requestContext)
{
// ES5Array does not support compat mode, ignore preferSnapshotSemantics
return RecyclerNew(GetScriptContext()->GetRecycler(), ES5ArrayIndexEnumerator, this, flags, requestContext);
}
BOOL ES5Array::IsItemEnumerable(uint32 index)
{
return GetTypeHandler()->IsItemEnumerable(this, index);
}
BOOL ES5Array::SetItemWithAttributes(uint32 index, Var value, PropertyAttributes attributes)
{
return GetTypeHandler()->SetItemWithAttributes(this, index, value, attributes);
}
BOOL ES5Array::SetItemAttributes(uint32 index, PropertyAttributes attributes)
{
return GetTypeHandler()->SetItemAttributes(this, index, attributes);
}
BOOL ES5Array::SetItemAccessors(uint32 index, Var getter, Var setter)
{
return GetTypeHandler()->SetItemAccessors(this, index, getter, setter);
}
BOOL ES5Array::IsObjectArrayFrozen()
{
return GetTypeHandler()->IsObjectArrayFrozen(this);
}
BOOL ES5Array::IsValidDescriptorToken(void * descriptorValidationToken) const
{
return GetTypeHandler()->IsValidDescriptorToken(descriptorValidationToken);
}
uint32 ES5Array::GetNextDescriptor(uint32 key, IndexPropertyDescriptor** descriptor, void ** descriptorValidationToken)
{
return GetTypeHandler()->GetNextDescriptor(key, descriptor, descriptorValidationToken);
}
BOOL ES5Array::GetDescriptor(uint32 index, Js::IndexPropertyDescriptor **ppDescriptor)
{
return GetTypeHandler()->GetDescriptor(index, ppDescriptor);
}
#if ENABLE_TTD
void ES5Array::MarkVisitKindSpecificPtrs(TTD::SnapshotExtractor* extractor)
{
this->JavascriptArray::MarkVisitKindSpecificPtrs(extractor);
uint32 length = this->GetLength();
uint32 descriptorIndex = Js::JavascriptArray::InvalidIndex;
IndexPropertyDescriptor* descriptor = nullptr;
void* descriptorValidationToken = nullptr;
do
{
descriptorIndex = this->GetNextDescriptor(descriptorIndex, &descriptor, &descriptorValidationToken);
if(descriptorIndex == Js::JavascriptArray::InvalidIndex || descriptorIndex >= length)
{
break;
}
if((descriptor->Attributes & PropertyDeleted) != PropertyDeleted)
{
if(descriptor->Getter != nullptr)
{
extractor->MarkVisitVar(descriptor->Getter);
}
if(descriptor->Setter != nullptr)
{
extractor->MarkVisitVar(descriptor->Setter);
}
}
} while(true);
}
TTD::NSSnapObjects::SnapObjectType ES5Array::GetSnapTag_TTD() const
{
return TTD::NSSnapObjects::SnapObjectType::SnapES5ArrayObject;
}
void ES5Array::ExtractSnapObjectDataInto(TTD::NSSnapObjects::SnapObject* objData, TTD::SlabAllocator& alloc)
{
TTD::NSSnapObjects::SnapArrayInfo<TTD::TTDVar>* sai = TTD::NSSnapObjects::ExtractArrayValues<TTD::TTDVar>(this, alloc);
TTD::NSSnapObjects::SnapES5ArrayInfo* es5ArrayInfo = alloc.SlabAllocateStruct<TTD::NSSnapObjects::SnapES5ArrayInfo>();
//
//TODO: reserving memory for entire length might be a problem if we have very large sparse arrays.
//
uint32 length = this->GetLength();
es5ArrayInfo->IsLengthWritable = this->IsLengthWritable();
es5ArrayInfo->GetterSetterCount = 0;
es5ArrayInfo->GetterSetterEntries = alloc.SlabReserveArraySpace<TTD::NSSnapObjects::SnapES5ArrayGetterSetterEntry>(length + 1); //ensure we don't do a 0 reserve
uint32 descriptorIndex = Js::JavascriptArray::InvalidIndex;
IndexPropertyDescriptor* descriptor = nullptr;
void* descriptorValidationToken = nullptr;
do
{
descriptorIndex = this->GetNextDescriptor(descriptorIndex, &descriptor, &descriptorValidationToken);
if(descriptorIndex == Js::JavascriptArray::InvalidIndex || descriptorIndex >= length)
{
break;
}
if((descriptor->Attributes & PropertyDeleted) != PropertyDeleted)
{
TTD::NSSnapObjects::SnapES5ArrayGetterSetterEntry* entry = es5ArrayInfo->GetterSetterEntries + es5ArrayInfo->GetterSetterCount;
es5ArrayInfo->GetterSetterCount++;
entry->Index = (uint32)descriptorIndex;
entry->Attributes = descriptor->Attributes;
entry->Getter = nullptr;
if(descriptor->Getter != nullptr)
{
entry->Getter = descriptor->Getter;
}
entry->Setter = nullptr;
if(descriptor->Setter != nullptr)
{
entry->Setter = descriptor->Setter;
}
}
} while(true);
if(es5ArrayInfo->GetterSetterCount != 0)
{
alloc.SlabCommitArraySpace<TTD::NSSnapObjects::SnapES5ArrayGetterSetterEntry>(es5ArrayInfo->GetterSetterCount, length + 1);
}
else
{
alloc.SlabAbortArraySpace<TTD::NSSnapObjects::SnapES5ArrayGetterSetterEntry>(length + 1);
es5ArrayInfo->GetterSetterEntries = nullptr;
}
es5ArrayInfo->BasicArrayData = sai;
TTD::NSSnapObjects::StdExtractSetKindSpecificInfo<TTD::NSSnapObjects::SnapES5ArrayInfo*, TTD::NSSnapObjects::SnapObjectType::SnapES5ArrayObject>(objData, es5ArrayInfo);
}
#endif
}