forked from chakra-core/ChakraCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSCASerialization.cpp
More file actions
637 lines (565 loc) · 20.6 KB
/
SCASerialization.cpp
File metadata and controls
637 lines (565 loc) · 20.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
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#include "SCACorePch.h"
#include "Common/ByteSwap.h"
#include "Library/JavascriptNumberObject.h"
#include "Library/JavascriptStringObject.h"
#include "Library/JavascriptBooleanObject.h"
#include "Library/DateImplementation.h"
#include "Library/JavascriptDate.h"
#include "Library/DataView.h"
#include "Library/ES5Array.h"
#include "Types/PropertyIndexRanges.h"
#include "Types/DictionaryPropertyDescriptor.h"
#include "Types/DictionaryTypeHandler.h"
#include "Types/ES5ArrayTypeHandler.h"
#include "Library/JavascriptArrayIndexStaticEnumerator.h"
#include "Library/ES5ArrayIndexStaticEnumerator.h"
namespace Js
{
template <class Writer>
bool SerializationCloner<Writer>::TryClonePrimitive(SrcTypeId typeId, Src src, Dst* dst)
{
switch (typeId)
{
case TypeIds_Undefined:
WriteTypeId(SCA_UndefinedValue);
break;
case TypeIds_Null:
WriteTypeId(SCA_NullValue);
break;
case TypeIds_Boolean:
WriteTypeId(
VarTo<JavascriptBoolean>(src)->GetValue() ? SCA_TrueValue : SCA_FalseValue);
break;
case TypeIds_Integer:
{
WriteTypeId(SCA_Int32Value);
m_writer->Write(TaggedInt::ToInt32(src));
}
break;
case TypeIds_Number:
{
WriteTypeId(SCA_DoubleValue);
m_writer->Write(JavascriptNumber::GetValue(src));
}
break;
case TypeIds_Int64Number:
{
WriteTypeId(SCA_Int64Value);
m_writer->Write(VarTo<JavascriptInt64Number>(src)->GetValue());
}
break;
case TypeIds_UInt64Number:
{
WriteTypeId(SCA_Uint64Value);
m_writer->Write(VarTo<JavascriptUInt64Number>(src)->GetValue());
}
break;
default:
return false; // Not a recognized primitive type
}
return true;
}
template <class Writer>
bool SerializationCloner<Writer>::TryCloneObject(SrcTypeId typeId, Src src, Dst* dst, SCADeepCloneType* deepClone)
{
RecyclableObject* obj = VarTo<RecyclableObject>(src);
scaposition_t beginPos = m_writer->GetPosition();
*deepClone = SCADeepCloneType::None;
size_t transferredIndex = 0;
if (this->CanBeTransferred(typeId) && this->GetEngine()->TryGetTransferredOrShared(src, &transferredIndex))
{
WriteTypeId(SCA_Transferable);
m_writer->Write((uint32)transferredIndex);
}
else if (JavascriptOperators::IsObjectDetached(src))
{
//Object is detached, throw error
this->ThrowSCAObjectDetached();
}
else
{
switch (typeId)
{
case TypeIds_String: // Clone string value as object type to resolve multiple references
{
JavascriptString* str = VarTo<JavascriptString>(obj);
WriteTypeId(SCA_StringValue);
Write(str->GetString(), str->GetLength());
}
break;
case TypeIds_Object:
{
WriteTypeId(SCA_Object);
*deepClone = SCADeepCloneType::Object;
}
break;
case TypeIds_Proxy:
{
// Currently SCA algorithm does not support proxy. We'll see
// if the spec will be updated. I don't support QueryObjectInterface in proxy
// so we don't want to go through the default code path.
return false;
}
case TypeIds_Array:
case TypeIds_ES5Array:
case TypeIds_NativeIntArray:
case TypeIds_NativeFloatArray:
{
// Postpone writing to CloneProperties
*deepClone = SCADeepCloneType::Object;
}
break;
case TypeIds_Date:
{
WriteTypeId(SCA_DateObject);
m_writer->Write(VarTo<JavascriptDate>(src)->GetTime());
}
break;
case TypeIds_RegEx:
{
JavascriptRegExp* regex = VarTo<JavascriptRegExp>(src);
InternalString str = regex->GetSource();
DWORD flags = static_cast<DWORD>(regex->GetFlags());
WriteTypeId(SCA_RegExpObject);
Write(str.GetBuffer(), str.GetLength());
m_writer->Write(flags);
}
break;
case TypeIds_BooleanObject:
WriteTypeId(VarTo<JavascriptBooleanObject>(src)->GetValue() ?
SCA_BooleanTrueObject : SCA_BooleanFalseObject);
break;
case TypeIds_NumberObject:
{
WriteTypeId(SCA_NumberObject);
m_writer->Write(VarTo<JavascriptNumberObject>(src)->GetValue());
}
break;
case TypeIds_StringObject:
{
JavascriptString* str = VarTo<JavascriptStringObject>(src)->Unwrap();
WriteTypeId(SCA_StringObject);
Write(str->GetString(), str->GetLength());
}
break;
case TypeIds_ArrayBuffer:
{
ArrayBuffer* buf = VarTo<ArrayBuffer>(src);
WriteTypeId(SCA_ArrayBuffer);
Write(buf->GetBuffer(), buf->GetByteLength());
}
break;
case TypeIds_SharedArrayBuffer:
{
// TBD
//SCAContextType contextType;
//if (FAILED(this->m_pSCAContext->GetContext(&contextType)))
//{
// return false;
//}
//
//if(contextType == SCAContext_CrossProcess || contextType == SCAContext_Persist)
//{
// return false;
//}
SharedArrayBuffer* buf = VarTo<SharedArrayBuffer>(src);
SharedContents* sharedContents = buf->GetSharedContents();
Assert(buf->IsWebAssemblyArrayBuffer() == sharedContents->IsWebAssembly());
sharedContents->AddRef();
this->m_sharedContentsrList->Add(sharedContents);
WriteTypeId(SCA_SharedArrayBuffer);
m_writer->Write((intptr_t)sharedContents);
}
break;
case TypeIds_Map:
{
WriteTypeId(SCA_Map);
*deepClone = SCADeepCloneType::Map;
}
break;
case TypeIds_Set:
{
WriteTypeId(SCA_Set);
*deepClone = SCADeepCloneType::Set;
}
break;
#ifdef ENABLE_WASM
case TypeIds_WebAssemblyModule:
{
WebAssemblyModule* wasmModule = VarTo<WebAssemblyModule>(src);
WriteTypeId(SCA_WebAssemblyModule);
Write(wasmModule->GetBinaryBuffer(), wasmModule->GetBinaryBufferLength());
}
break;
case TypeIds_WebAssemblyMemory:
{
WebAssemblyMemory* wasmMem = VarTo<WebAssemblyMemory>(src);
ArrayBufferBase* buffer = wasmMem->GetBuffer();
WriteTypeId(SCA_WebAssemblyMemory);
Write(wasmMem->GetInitialLength());
Write(wasmMem->GetMaximumLength());
#ifdef ENABLE_WASM_THREADS
Write((uint32)wasmMem->IsSharedMemory());
if (wasmMem->IsSharedMemory())
{
WebAssemblySharedArrayBuffer* buf = VarTo<WebAssemblySharedArrayBuffer>(buffer);
SharedContents* sharedContents = buf->GetSharedContents();
sharedContents->AddRef();
this->m_sharedContentsrList->Add(sharedContents);
m_writer->Write((intptr_t)sharedContents);
}
else
#endif
{
Write(buffer->GetBuffer(), buffer->GetByteLength());
}
break;
}
#endif
#if ENABLE_COPYONACCESS_ARRAY
case TypeIds_CopyOnAccessNativeIntArray:
Assert(false);
// fall-through
#endif
default:
if (IsTypedArray(typeId))
{
WriteTypedArray(typeId, src);
}
else
{
// Try Host Object
*deepClone = SCADeepCloneType::HostObject;
}
break;
}
}
*dst = beginPos;
return true;
}
template <class Writer>
void SerializationCloner<Writer>::CloneHostObjectProperties(SrcTypeId srcTypeId, Src src, Dst dst)
{
WriteTypeId(SCA_FirstHostObject);
// Ask host to fill rest of the properties
m_writer->WriteHostObject((void*)src);
}
template <class Writer>
void SerializationCloner<Writer>::CloneProperties(SrcTypeId srcTypeId, Src src, Dst dst)
{
RecyclableObject* obj = VarTo<RecyclableObject>(src);
// allocate the JavascriptStaticEnumerator on the heap to avoid blowing the stack
JavascriptStaticEnumerator enumerator;
ScriptContext* scriptContext = this->GetScriptContext();
if (DynamicObject::IsAnyArrayTypeId(srcTypeId))
{
JavascriptArray* arr = JavascriptArray::FromAnyArray(src);
bool isSparseArray = IsSparseArray(arr);
WriteTypeId(isSparseArray ? SCA_SparseArray : SCA_DenseArray);
Write(arr->GetLength());
if (isSparseArray)
{
WriteSparseArrayIndexProperties(arr);
}
else
{
WriteDenseArrayIndexProperties(arr);
}
// Now we only need to write remaining non-index properties
arr->GetNonIndexEnumerator(&enumerator, scriptContext);
}
else if (!obj->GetEnumerator(&enumerator, EnumeratorFlags::SnapShotSemantics, scriptContext))
{
// Mark property end if we don't have enumerator
m_writer->Write(static_cast<uint32>(SCA_PROPERTY_TERMINATOR));
return;
}
ObjectPropertyEnumerator propEnumerator(scriptContext, obj, &enumerator);
WriteObjectProperties(&propEnumerator);
}
template <class Writer>
void SerializationCloner<Writer>::CloneMap(Src src, Dst dst)
{
JavascriptMap* map = VarTo<JavascriptMap>(src);
Write((int32)(map->Size()));
JavascriptMap::MapDataList::Iterator iter = map->GetIterator();
while (iter.Next())
{
const JavascriptMap::MapDataKeyValuePair& entry = iter.Current();
this->GetEngine()->Clone(entry.Key());
this->GetEngine()->Clone(entry.Value());
}
}
template <class Writer>
void SerializationCloner<Writer>::CloneSet(Src src, Dst dst)
{
JavascriptSet* set = VarTo<JavascriptSet>(src);
Write((int32)(set->Size()));
JavascriptSet::SetDataList::Iterator iter = set->GetIterator();
while (iter.Next())
{
this->GetEngine()->Clone(iter.Current());
}
}
template <class Writer>
void SerializationCloner<Writer>::CloneObjectReference(Src src, Dst dst)
{
WriteTypeId(SCA_Reference);
m_writer->Write(dst);
}
//
// Write layout: [byteLen] [string content] [padding]
//
template <class Writer>
void SerializationCloner<Writer>::Write(const char16* str, charcount_t len) const
{
uint32 byteLen = static_cast<uint32>(sizeof(char16) * len);
m_writer->Write(byteLen);
m_writer->Write(str, byteLen);
uint32 unalignedLen = byteLen % sizeof(uint32);
if (unalignedLen)
{
uint32 padding = 0;
m_writer->Write(&padding, sizeof(uint32) - unalignedLen);
}
}
//
// Write layout: [byteLen] [byte data] [padding]
//
template <class Writer>
void SerializationCloner<Writer>::Write(const BYTE* bytes, uint32 len) const
{
m_writer->Write(len);
m_writer->Write(bytes, len);
uint32 unalignedLen = len % sizeof(uint32);
if (unalignedLen)
{
uint32 padding = 0;
m_writer->Write(&padding, sizeof(uint32) - unalignedLen);
}
}
//
// Check if a SrcTypeId is of a TypedArray or DataView.
//
template <class Writer>
bool SerializationCloner<Writer>::IsTypedArray(SrcTypeId typeId)
{
return (typeId >= TypeIds_TypedArraySCAMin && typeId <= TypeIds_TypedArraySCAMax)
|| typeId == TypeIds_DataView;
}
//
// Write a TypedArray or a DataView layout.
//
template <class Writer>
void SerializationCloner<Writer>::WriteTypedArray(SrcTypeId typeId, Src src) const
{
switch (typeId)
{
case TypeIds_Int8Array:
if (Int8VirtualArray::HasVirtualTableInfo(src))
{
WriteTypedArray<int8, false, true>(src);
}
else
{
WriteTypedArray<int8, false>(src);
}
break;
case TypeIds_Uint8Array:
if (Uint8VirtualArray::HasVirtualTableInfo(src))
{
WriteTypedArray<uint8, false, true>(src);
}
else
{
WriteTypedArray<uint8, false>(src);
}
break;
case TypeIds_Uint8ClampedArray:
if (Uint8ClampedVirtualArray::HasVirtualTableInfo(src))
{
WriteTypedArray<uint8, true, true>(src);
}
else
{
WriteTypedArray<uint8, true>(src);
}
break;
case TypeIds_Int16Array:
if (Int16VirtualArray::HasVirtualTableInfo(src))
{
WriteTypedArray<int16, false, true>(src);
}
else
{
WriteTypedArray<int16, false>(src);
}
break;
case TypeIds_Uint16Array:
if (Uint16VirtualArray::HasVirtualTableInfo(src))
{
WriteTypedArray<uint16, false, true>(src);
}
else
{
WriteTypedArray<uint16, false>(src);
}
break;
case TypeIds_Int32Array:
if (Int32VirtualArray::HasVirtualTableInfo(src))
{
WriteTypedArray<int32, false, true>(src);
}
else
{
WriteTypedArray<int32, false>(src);
}
break;
case TypeIds_Uint32Array:
if (Uint32VirtualArray::HasVirtualTableInfo(src))
{
WriteTypedArray<uint32, false, true>(src);
}
else
{
WriteTypedArray<uint32, false>(src);
}
break;
case TypeIds_Float32Array:
if (Float32VirtualArray::HasVirtualTableInfo(src))
{
WriteTypedArray<float, false, true>(src);
}
else
{
WriteTypedArray<float, false>(src);
}
break;
case TypeIds_Float64Array:
if (Float64VirtualArray::HasVirtualTableInfo(src))
{
WriteTypedArray<double, false, true>(src);
}
else
{
WriteTypedArray<double, false>(src);
}
break;
case TypeIds_DataView:
WriteTypedArray<DataView, false>(src);
break;
default:
Assert(false);
}
}
//
// Do an arbitrary test to determine serializing a JavascriptArray as sparse array or not.
//
template <class Writer>
bool SerializationCloner<Writer>::IsSparseArray(JavascriptArray* arr)
{
uint32 length = arr->GetLength();
if (length > SparseArraySegmentBase::HEAD_CHUNK_SIZE)
{
// Consider it a sparse array if the array size is non-trivial, and there
// are empty slots found by some arbitrary sampling.
return !JavascriptOperators::HasOwnItem(arr, length / 4)
|| !JavascriptOperators::HasOwnItem(arr, length / 2)
|| !JavascriptOperators::HasOwnItem(arr, length / 2 + length / 4);
}
return false;
}
//
// Write dense array index named properties.
//
template <class Writer>
void SerializationCloner<Writer>::WriteDenseArrayIndexProperties(JavascriptArray* arr)
{
if (JavascriptArray::IsNonES5Array(arr))
{
if (!arr->IsCrossSiteObject())
{
WriteArrayIndexProperties<JavascriptArrayDirectItemAccessor>(arr);
}
else
{
WriteArrayIndexProperties<JavascriptArrayItemAccessor>(arr);
}
}
else
{
WriteArrayIndexProperties<JavascriptArrayEnumerableItemAccessor>(arr);
}
}
//
// Write sparse array index named properties.
//
template <class Writer>
void SerializationCloner<Writer>::WriteSparseArrayIndexProperties(JavascriptArray* arr)
{
if (JavascriptArray::IsNonES5Array(arr))
{
if (!arr->IsCrossSiteObject())
{
WriteSparseArrayIndexProperties<
JavascriptArrayIndexStaticEnumerator, JavascriptArrayDirectItemAccessor>(arr);
}
else
{
WriteSparseArrayIndexProperties<
JavascriptArrayIndexStaticEnumerator, JavascriptArrayItemAccessor>(arr);
}
}
else
{
// We don't need JavascriptArrayEnumerableItemAccessor to check enumberable since we'll
// enumerate enumerable index named properties through ES5ArrayIndexEnumerator. Just use
// JavascriptArrayItemAccessor.
WriteSparseArrayIndexProperties<
ES5ArrayIndexStaticEnumerator<>, JavascriptArrayItemAccessor>(VarTo<ES5Array>(arr));
}
}
template class SerializationCloner<StreamWriter>;
bool ObjectPropertyEnumerator::MoveNext()
{
if (m_innerEnumerator)
{
ScriptContext* scriptContext = this->GetScriptContext();
Var undefined = scriptContext->GetLibrary()->GetUndefined();
Var propertyName;
PropertyId propertyId;
while ((propertyName = m_innerEnumerator->MoveAndGetNext(propertyId)) != NULL)
{
if (propertyName != undefined) //There are some code paths in which GetCurrentIndex can return undefined
{
m_name = VarTo<JavascriptString>(propertyName);
if (propertyId != Constants::NoProperty)
{
m_value = JavascriptOperators::GetProperty(m_obj, propertyId, scriptContext);
}
else
{
m_value = JavascriptOperators::OP_GetElementI(m_obj, propertyName, scriptContext);
}
return true;
}
}
// No more properties, mark end
m_innerEnumerator = NULL;
}
return false;
}
void SCASerializationEngine::Serialize(Var root, StreamWriter* writer, Var* transferableVars, size_t cTransferableVars,
JsUtil::List<Js::SharedContents*, HeapAllocator>* sharedContentsList)
{
ScriptContext* scriptContext = writer->GetScriptContext();
// Write version
writer->Write(static_cast<uint32>(SCA_FORMAT_VERSION));
StreamSerializationCloner cloner(scriptContext, writer, sharedContentsList);
SCAEngine<Var, scaposition_t, StreamSerializationCloner>::Clone(root, &cloner, transferableVars, cTransferableVars);
}
}