forked from chakra-core/ChakraCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicType.cpp
More file actions
485 lines (409 loc) · 19.7 KB
/
DynamicType.cpp
File metadata and controls
485 lines (409 loc) · 19.7 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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#include "RuntimeTypePch.h"
namespace Js
{
DEFINE_RECYCLER_TRACKER_PERF_COUNTER(DynamicType);
DEFINE_RECYCLER_TRACKER_WEAKREF_PERF_COUNTER(DynamicType);
DynamicType::DynamicType(DynamicType * type, DynamicTypeHandler *typeHandler, bool isLocked, bool isShared)
: Type(type), typeHandler(typeHandler), isLocked(isLocked), isShared(isShared)
#if DBG
, isCachedForChangePrototype(false)
#endif
{
Assert(!this->isLocked || this->typeHandler->GetIsLocked());
Assert(!this->isShared || this->typeHandler->GetIsShared());
}
DynamicType::DynamicType(ScriptContext* scriptContext, TypeId typeId, RecyclableObject* prototype, JavascriptMethod entryPoint, DynamicTypeHandler * typeHandler, bool isLocked, bool isShared)
: Type(scriptContext, typeId, prototype, entryPoint) , typeHandler(typeHandler), isLocked(isLocked), isShared(isShared), hasNoEnumerableProperties(false)
#if DBG
, isCachedForChangePrototype(false)
#endif
{
Assert(typeHandler != nullptr);
Assert(!this->isLocked || this->typeHandler->GetIsLocked());
Assert(!this->isShared || this->typeHandler->GetIsShared());
}
DynamicType *
DynamicType::New(ScriptContext* scriptContext, TypeId typeId, RecyclableObject* prototype, JavascriptMethod entryPoint, DynamicTypeHandler * typeHandler, bool isLocked, bool isShared)
{
return RecyclerNew(scriptContext->GetRecycler(), DynamicType, scriptContext, typeId, prototype, entryPoint, typeHandler, isLocked, isShared);
}
bool
DynamicType::Is(TypeId typeId)
{
return !StaticType::Is(typeId);
}
bool
DynamicType::SetHasNoEnumerableProperties(bool value)
{
if (!value)
{
this->hasNoEnumerableProperties = value;
return false;
}
#if DEBUG
PropertyIndex propertyIndex = (PropertyIndex)-1;
JavascriptString* propertyString = nullptr;
PropertyId propertyId = Constants::NoProperty;
Assert(!this->GetTypeHandler()->FindNextProperty(this->GetScriptContext(), propertyIndex, &propertyString, &propertyId, nullptr, this, this, EnumeratorFlags::None));
#endif
this->hasNoEnumerableProperties = true;
return true;
}
bool DynamicType::PrepareForTypeSnapshotEnumeration()
{
if (CONFIG_FLAG(TypeSnapshotEnumeration))
{
// Lock the type and handler, enabling us to enumerate properties of the type snapshotted
// at the beginning of enumeration, despite property changes made by script during enumeration.
return LockType(); // Note: this only works for type handlers that support locking.
}
return false;
}
void DynamicObject::InitSlots(DynamicObject* instance)
{
InitSlots(instance, GetScriptContext());
}
void DynamicObject::InitSlots(DynamicObject * instance, ScriptContext * scriptContext)
{
Recycler * recycler = scriptContext->GetRecycler();
int slotCapacity = GetTypeHandler()->GetSlotCapacity();
int inlineSlotCapacity = GetTypeHandler()->GetInlineSlotCapacity();
if (slotCapacity > inlineSlotCapacity)
{
instance->auxSlots = RecyclerNewArrayZ(recycler, Var, slotCapacity - inlineSlotCapacity);
}
}
int DynamicObject::GetPropertyCount()
{
if (!this->GetTypeHandler()->EnsureObjectReady(this))
{
return 0;
}
return GetTypeHandler()->GetPropertyCount();
}
PropertyId DynamicObject::GetPropertyId(PropertyIndex index)
{
return GetTypeHandler()->GetPropertyId(this->GetScriptContext(), index);
}
PropertyId DynamicObject::GetPropertyId(BigPropertyIndex index)
{
return GetTypeHandler()->GetPropertyId(this->GetScriptContext(), index);
}
PropertyIndex DynamicObject::GetPropertyIndex(PropertyId propertyId)
{
Assert(!Js::IsInternalPropertyId(propertyId));
Assert(propertyId != Constants::NoProperty);
return GetTypeHandler()->GetPropertyIndex(this->GetScriptContext()->GetPropertyName(propertyId));
}
BOOL DynamicObject::HasProperty(PropertyId propertyId)
{
// HasProperty can be invoked with propertyId = NoProperty in some cases, namely cross-thread and DOM
// This is done to force creation of a type handler in case the type handler is deferred
Assert(!Js::IsInternalPropertyId(propertyId) || propertyId == Js::Constants::NoProperty);
return GetTypeHandler()->HasProperty(this, propertyId);
}
// HasOwnProperty and HasProperty is the same for most objects except globalobject (moduleroot as well in legacy)
// Note that in GlobalObject, HasProperty and HasRootProperty is not quite the same as it's handling let/const global etc.
BOOL DynamicObject::HasOwnProperty(PropertyId propertyId)
{
Assert(!Js::IsInternalPropertyId(propertyId));
return HasProperty(propertyId);
}
BOOL DynamicObject::GetProperty(Var originalInstance, PropertyId propertyId, Var* value, PropertyValueInfo* info, ScriptContext* requestContext)
{
Assert(!Js::IsInternalPropertyId(propertyId));
return GetTypeHandler()->GetProperty(this, originalInstance, propertyId, value, info, requestContext);
}
BOOL DynamicObject::GetProperty(Var originalInstance, JavascriptString* propertyNameString, Var* value, PropertyValueInfo* info, ScriptContext* requestContext)
{
AssertMsg(!PropertyRecord::IsPropertyNameNumeric(propertyNameString->GetString(), propertyNameString->GetLength()),
"Numeric property names should have been converted to uint or PropertyRecord* before calling GetProperty");
return GetTypeHandler()->GetProperty(this, originalInstance, propertyNameString, value, info, requestContext);
}
BOOL DynamicObject::GetInternalProperty(Var originalInstance, PropertyId propertyId, Var* value, PropertyValueInfo* info, ScriptContext* requestContext)
{
Assert(Js::IsInternalPropertyId(propertyId));
return GetTypeHandler()->GetProperty(this, originalInstance, propertyId, value, nullptr, requestContext);
}
BOOL DynamicObject::GetPropertyReference(Var originalInstance, PropertyId propertyId, Var* value, PropertyValueInfo* info, ScriptContext* requestContext)
{
Assert(!Js::IsInternalPropertyId(propertyId));
return GetTypeHandler()->GetProperty(this, originalInstance, propertyId, value, info, requestContext);
}
BOOL DynamicObject::SetProperty(PropertyId propertyId, Var value, PropertyOperationFlags flags, PropertyValueInfo* info)
{
Assert(!Js::IsInternalPropertyId(propertyId));
return GetTypeHandler()->SetProperty(this, propertyId, value, flags, info);
}
BOOL DynamicObject::SetProperty(JavascriptString* propertyNameString, Var value, PropertyOperationFlags flags, PropertyValueInfo* info)
{
AssertMsg(!PropertyRecord::IsPropertyNameNumeric(propertyNameString->GetString(), propertyNameString->GetLength()),
"Numeric property names should have been converted to uint or PropertyRecord* before calling SetProperty");
return GetTypeHandler()->SetProperty(this, propertyNameString, value, flags, info);
}
BOOL DynamicObject::SetInternalProperty(PropertyId propertyId, Var value, PropertyOperationFlags flags, PropertyValueInfo* info)
{
Assert(Js::IsInternalPropertyId(propertyId));
return GetTypeHandler()->SetProperty(this, propertyId, value, flags, nullptr);
}
DescriptorFlags DynamicObject::GetSetter(PropertyId propertyId, Var* setterValue, PropertyValueInfo* info, ScriptContext* requestContext)
{
Assert(!Js::IsInternalPropertyId(propertyId));
return GetTypeHandler()->GetSetter(this, propertyId, setterValue, info, requestContext);
}
DescriptorFlags DynamicObject::GetSetter(JavascriptString* propertyNameString, Var* setterValue, PropertyValueInfo* info, ScriptContext* requestContext)
{
AssertMsg(!PropertyRecord::IsPropertyNameNumeric(propertyNameString->GetString(), propertyNameString->GetLength()),
"Numeric property names should have been converted to uint or PropertyRecord* before calling GetSetter");
return GetTypeHandler()->GetSetter(this, propertyNameString, setterValue, info, requestContext);
}
BOOL DynamicObject::InitProperty(PropertyId propertyId, Var value, PropertyOperationFlags flags, PropertyValueInfo* info)
{
Assert(!Js::IsInternalPropertyId(propertyId));
return GetTypeHandler()->InitProperty(this, propertyId, value, flags, info);
}
BOOL DynamicObject::DeleteProperty(PropertyId propertyId, PropertyOperationFlags flags)
{
Assert(!Js::IsInternalPropertyId(propertyId));
return GetTypeHandler()->DeleteProperty(this, propertyId, flags);
}
BOOL DynamicObject::DeleteProperty(JavascriptString *propertyNameString, PropertyOperationFlags flags)
{
return GetTypeHandler()->DeleteProperty(this, propertyNameString, flags);
}
BOOL DynamicObject::IsFixedProperty(PropertyId propertyId)
{
Assert(!Js::IsInternalPropertyId(propertyId));
return GetTypeHandler()->IsFixedProperty(this, propertyId);
}
BOOL DynamicObject::HasItem(uint32 index)
{
return GetTypeHandler()->HasItem(this, index);
}
BOOL DynamicObject::HasOwnItem(uint32 index)
{
return HasItem(index);
}
BOOL DynamicObject::GetItem(Var originalInstance, uint32 index, Var* value, ScriptContext * requestContext)
{
return GetTypeHandler()->GetItem(this, originalInstance, index, value, requestContext);
}
BOOL DynamicObject::GetItemReference(Var originalInstance, uint32 index, Var* value, ScriptContext * requestContext)
{
return GetTypeHandler()->GetItem(this, originalInstance, index, value, requestContext);
}
DescriptorFlags DynamicObject::GetItemSetter(uint32 index, Var* setterValue, ScriptContext* requestContext)
{
return GetTypeHandler()->GetItemSetter(this, index, setterValue, requestContext);
}
BOOL DynamicObject::SetItem(uint32 index, Var value, PropertyOperationFlags flags)
{
return GetTypeHandler()->SetItem(this, index, value, flags);
}
BOOL DynamicObject::DeleteItem(uint32 index, PropertyOperationFlags flags)
{
return GetTypeHandler()->DeleteItem(this, index, flags);
}
BOOL DynamicObject::ToPrimitive(JavascriptHint hint, Var* result, ScriptContext * requestContext)
{
if(hint == JavascriptHint::HintString)
{
return ToPrimitiveImpl<PropertyIds::toString>(result, requestContext)
|| ToPrimitiveImpl<PropertyIds::valueOf>(result, requestContext);
}
else
{
Assert(hint == JavascriptHint::None || hint == JavascriptHint::HintNumber);
return ToPrimitiveImpl<PropertyIds::valueOf>(result, requestContext)
|| ToPrimitiveImpl<PropertyIds::toString>(result, requestContext);
}
}
template <PropertyId propertyId>
BOOL DynamicObject::ToPrimitiveImpl(Var* result, ScriptContext * requestContext)
{
CompileAssert(propertyId == PropertyIds::valueOf || propertyId == PropertyIds::toString);
InlineCache * inlineCache = propertyId == PropertyIds::valueOf ? requestContext->GetValueOfInlineCache() : requestContext->GetToStringInlineCache();
// Use per script context inline cache for valueOf and toString
Var aValue = JavascriptOperators::PatchGetValueUsingSpecifiedInlineCache(inlineCache, this, this, propertyId, requestContext);
// Fast path to the default valueOf/toString implementation
if (propertyId == PropertyIds::valueOf)
{
if (aValue == requestContext->GetLibrary()->GetObjectValueOfFunction())
{
Assert(JavascriptConversion::IsCallable(aValue));
// The default Object.prototype.valueOf will in turn just call ToObject().
// The result is always an object if it is not undefined or null (which "this" is not)
return false;
}
}
else
{
if (aValue == requestContext->GetLibrary()->GetObjectToStringFunction())
{
Assert(JavascriptConversion::IsCallable(aValue));
// These typeIds should never be here (they override ToPrimitive or they don't derive to DynamicObject::ToPrimitive)
// Otherwise, they may case implicit call in ToStringHelper
Assert(this->GetTypeId() != TypeIds_HostDispatch
&& this->GetTypeId() != TypeIds_HostObject);
*result = JavascriptObject::ToStringHelper(this, requestContext);
return true;
}
}
return CallToPrimitiveFunction(aValue, propertyId, result, requestContext);
}
BOOL DynamicObject::CallToPrimitiveFunction(Var toPrimitiveFunction, PropertyId propertyId, Var* result, ScriptContext * requestContext)
{
if (JavascriptConversion::IsCallable(toPrimitiveFunction))
{
RecyclableObject* toStringFunction = RecyclableObject::FromVar(toPrimitiveFunction);
ThreadContext * threadContext = requestContext->GetThreadContext();
Var aResult = threadContext->ExecuteImplicitCall(toStringFunction, ImplicitCall_ToPrimitive, [=]() -> Js::Var
{
// Stack object should have a pre-op bail on implicit call. We shouldn't see them here.
Assert(!ThreadContext::IsOnStack(this) || threadContext->HasNoSideEffect(toStringFunction));
return CALL_FUNCTION(threadContext, toStringFunction, CallInfo(CallFlags_Value, 1), this);
});
if (!aResult)
{
// There was an implicit call and implicit calls are disabled. This would typically cause a bailout.
Assert(threadContext->IsDisableImplicitCall());
*result = requestContext->GetLibrary()->GetNull();
return true;
}
if (JavascriptOperators::GetTypeId(aResult) <= TypeIds_LastToPrimitiveType)
{
*result = aResult;
return true;
}
}
return false;
}
BOOL DynamicObject::GetEnumeratorWithPrefix(JavascriptEnumerator * prefixEnumerator, JavascriptStaticEnumerator * enumerator, EnumeratorFlags flags, ScriptContext * requestContext, ForInCache * forInCache)
{
Js::ArrayObject * arrayObject = nullptr;
if (this->HasObjectArray())
{
arrayObject = this->GetObjectArrayOrFlagsAsArray();
Assert(arrayObject->GetPropertyCount() == 0);
}
return enumerator->Initialize(prefixEnumerator, arrayObject, this, flags, requestContext, forInCache);
}
BOOL DynamicObject::GetEnumerator(JavascriptStaticEnumerator * enumerator, EnumeratorFlags flags, ScriptContext * requestContext, ForInCache * forInCache)
{
return GetEnumeratorWithPrefix(nullptr, enumerator, flags, requestContext, forInCache);
}
BOOL DynamicObject::SetAccessors(PropertyId propertyId, Var getter, Var setter, PropertyOperationFlags flags)
{
return GetTypeHandler()->SetAccessors(this, propertyId, getter, setter, flags);
}
BOOL DynamicObject::GetAccessors(PropertyId propertyId, Var *getter, Var *setter, ScriptContext * requestContext)
{
return GetTypeHandler()->GetAccessors(this, propertyId, getter, setter);
}
BOOL DynamicObject::PreventExtensions()
{
return GetTypeHandler()->PreventExtensions(this);
}
BOOL DynamicObject::Seal()
{
return GetTypeHandler()->Seal(this);
}
BOOL DynamicObject::Freeze()
{
Type* oldType = this->GetType();
BOOL ret = GetTypeHandler()->Freeze(this);
// We just made all properties on this object non-writable.
// Make sure the type is evolved so that the property string caches
// are no longer hit.
if (this->GetType() == oldType)
{
this->ChangeType();
}
return ret;
}
BOOL DynamicObject::IsSealed()
{
return GetTypeHandler()->IsSealed(this);
}
BOOL DynamicObject::IsFrozen()
{
return GetTypeHandler()->IsFrozen(this);
}
BOOL DynamicObject::IsWritable(PropertyId propertyId)
{
return GetTypeHandler()->IsWritable(this, propertyId);
}
BOOL DynamicObject::IsConfigurable(PropertyId propertyId)
{
return GetTypeHandler()->IsConfigurable(this, propertyId);
}
BOOL DynamicObject::IsEnumerable(PropertyId propertyId)
{
return GetTypeHandler()->IsEnumerable(this, propertyId);
}
BOOL DynamicObject::SetEnumerable(PropertyId propertyId, BOOL value)
{
return GetTypeHandler()->SetEnumerable(this, propertyId, value);
}
BOOL DynamicObject::SetWritable(PropertyId propertyId, BOOL value)
{
return GetTypeHandler()->SetWritable(this, propertyId, value);
}
BOOL DynamicObject::SetConfigurable(PropertyId propertyId, BOOL value)
{
return GetTypeHandler()->SetConfigurable(this, propertyId, value);
}
BOOL DynamicObject::SetAttributes(PropertyId propertyId, PropertyAttributes attributes)
{
return GetTypeHandler()->SetAttributes(this, propertyId, attributes);
}
BOOL DynamicObject::GetDiagValueString(StringBuilder<ArenaAllocator>* stringBuilder, ScriptContext* requestContext)
{
stringBuilder->AppendCppLiteral(_u("{...}"));
return TRUE;
}
BOOL DynamicObject::GetDiagTypeString(StringBuilder<ArenaAllocator>* stringBuilder, ScriptContext* requestContext)
{
stringBuilder->AppendCppLiteral(_u("Object"));
return TRUE;
}
Var DynamicObject::GetTypeOfString(ScriptContext * requestContext)
{
return requestContext->GetLibrary()->GetObjectTypeDisplayString();
}
// If this object is not extensible and the property being set does not already exist,
// if throwIfNotExtensible is
// * true, a type error will be thrown
// * false, FALSE will be returned (unless strict mode is enabled, in which case a type error will be thrown).
// Either way, the property will not be set.
//
// throwIfNotExtensible should always be false for non-numeric properties.
BOOL DynamicObject::SetPropertyWithAttributes(PropertyId propertyId, Var value, PropertyAttributes attributes, PropertyValueInfo* info, PropertyOperationFlags flags, SideEffects possibleSideEffects)
{
return GetTypeHandler()->SetPropertyWithAttributes(this, propertyId, value, attributes, info, flags, possibleSideEffects);
}
#if DBG
bool DynamicObject::CanStorePropertyValueDirectly(PropertyId propertyId, bool allowLetConst)
{
return GetTypeHandler()->CanStorePropertyValueDirectly(this, propertyId, allowLetConst);
}
#endif
void DynamicObject::RemoveFromPrototype(ScriptContext * requestContext)
{
GetTypeHandler()->RemoveFromPrototype(this, requestContext);
}
void DynamicObject::AddToPrototype(ScriptContext * requestContext)
{
GetTypeHandler()->AddToPrototype(this, requestContext);
}
void DynamicObject::SetPrototype(RecyclableObject* newPrototype)
{
// Mark newPrototype it is being set as prototype
newPrototype->SetIsPrototype();
GetTypeHandler()->SetPrototype(this, newPrototype);
}
}