forked from ClearFoundry/ClearScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathV8Value.h
More file actions
467 lines (390 loc) · 9.78 KB
/
V8Value.h
File metadata and controls
467 lines (390 loc) · 9.78 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
#pragma once
//-----------------------------------------------------------------------------
// V8BigInt
//-----------------------------------------------------------------------------
class V8BigInt final
{
public:
V8BigInt(int signBit, std::vector<uint64_t>&& words):
m_SignBit(signBit),
m_Words(std::move(words))
{
}
V8BigInt(const V8BigInt& that):
m_SignBit(that.m_SignBit),
m_Words(that.m_Words)
{
}
V8BigInt(V8BigInt&& that) noexcept:
m_SignBit(that.m_SignBit),
m_Words(std::move(that.m_Words))
{
}
const V8BigInt& operator=(const V8BigInt& that)
{
if (&that != this)
{
m_SignBit = that.m_SignBit;
m_Words = that.m_Words;
}
return *this;
}
const V8BigInt& operator=(V8BigInt&& that) noexcept
{
if (&that != this)
{
m_SignBit = that.m_SignBit;
m_Words = std::move(that.m_Words);
}
return *this;
}
int GetSignBit() const
{
return m_SignBit;
}
const std::vector<uint64_t>& GetWords() const
{
return m_Words;
}
private:
int m_SignBit;
std::vector<uint64_t> m_Words;
};
//-----------------------------------------------------------------------------
// V8Value
//-----------------------------------------------------------------------------
class V8Value
{
public:
enum NonexistentInitializer
{
Nonexistent
};
enum UndefinedInitializer
{
Undefined
};
enum NullInitializer
{
Null
};
enum DateTimeInitializer
{
DateTime
};
enum class Type: uint8_t
{
// IMPORTANT: maintain bitwise equivalence with managed enum V8.SplitProxy.V8Value.Type
Nonexistent,
Undefined,
Null,
Boolean,
Number,
String,
DateTime,
BigInt,
V8Object,
HostObject
};
enum class Subtype: uint8_t
{
// IMPORTANT: maintain bitwise equivalence with managed enum V8.SplitProxy.V8Value.Subtype
None,
Function,
Iterator,
Promise,
Array,
ArrayBuffer,
DataView,
Uint8Array,
Uint8ClampedArray,
Int8Array,
Uint16Array,
Int16Array,
Uint32Array,
Int32Array,
BigUint64Array,
BigInt64Array,
Float32Array,
Float64Array
};
enum class Flags : uint16_t
{
// IMPORTANT: maintain bitwise equivalence with managed enum V8.SplitProxy.V8Value.Flags
None = 0,
Shared = 0x0001,
Fast = 0x0001,
Async = 0x0002,
Generator = 0x0004,
Pending = 0x0008,
Rejected = 0x0010
};
struct WireData;
struct Decoded;
struct FastArg;
struct FastResult;
explicit V8Value(NonexistentInitializer):
m_Type(Type::Nonexistent)
{
}
explicit V8Value(UndefinedInitializer):
m_Type(Type::Undefined)
{
}
explicit V8Value(NullInitializer):
m_Type(Type::Null)
{
}
explicit V8Value(bool value):
m_Type(Type::Boolean)
{
m_Data.BooleanValue = value;
}
explicit V8Value(double value):
m_Type(Type::Number)
{
m_Data.DoubleValue = value;
}
explicit V8Value(const StdString* pString):
m_Type(Type::String)
{
m_Data.pString = pString;
}
V8Value(DateTimeInitializer, double value):
m_Type(Type::DateTime)
{
m_Data.DoubleValue = value;
}
explicit V8Value(const V8BigInt* pBigInt):
m_Type(Type::BigInt)
{
m_Data.pBigInt = pBigInt;
}
V8Value(V8ObjectHolder* pV8ObjectHolder, Subtype subtype, Flags flags):
m_Type(Type::V8Object),
m_Subtype(subtype),
m_Flags(flags)
{
m_Data.pV8ObjectHolder = pV8ObjectHolder;
}
explicit V8Value(HostObjectHolder* pHostObjectHolder):
m_Type(Type::HostObject),
m_Subtype(::ToEnum<Subtype>(pHostObjectHolder->GetSubtype())),
m_Flags(::ToEnum<Flags>(pHostObjectHolder->GetFlags()))
{
m_Data.pHostObjectHolder = pHostObjectHolder;
}
V8Value(const V8Value& that)
{
Copy(that);
}
V8Value(V8Value&& that) noexcept
{
Move(that);
}
explicit V8Value(const FastResult& result)
{
InitializeFromFastResult(result);
}
explicit V8Value(void*) = delete;
const V8Value& operator=(const V8Value& that)
{
Dispose();
Copy(that);
return *this;
}
const V8Value& operator=(V8Value&& that) noexcept
{
Dispose();
Move(that);
return *this;
}
bool IsNonexistent() const
{
return m_Type == Type::Nonexistent;
}
bool IsUndefined() const
{
return m_Type == Type::Undefined;
}
bool IsNull() const
{
return m_Type == Type::Null;
}
bool AsBoolean(bool& result) const
{
if (m_Type == Type::Boolean)
{
result = m_Data.BooleanValue;
return true;
}
return false;
}
bool AsNumber(double& result) const
{
if (m_Type == Type::Number)
{
result = m_Data.DoubleValue;
return true;
}
return false;
}
bool AsString(const StdString*& pString) const
{
if (m_Type == Type::String)
{
pString = m_Data.pString;
return true;
}
return false;
}
bool AsDateTime(double& result) const
{
if (m_Type == Type::DateTime)
{
result = m_Data.DoubleValue;
return true;
}
return false;
}
bool AsBigInt(const V8BigInt*& pBigInt) const
{
if (m_Type == Type::BigInt)
{
pBigInt = m_Data.pBigInt;
return true;
}
return false;
}
bool AsV8Object(V8ObjectHolder*& pV8ObjectHolder, Subtype& subtype, Flags& flags) const
{
if (m_Type == Type::V8Object)
{
pV8ObjectHolder = m_Data.pV8ObjectHolder;
subtype = m_Subtype;
flags = m_Flags;
return true;
}
return false;
}
bool AsHostObject(HostObjectHolder*& pHostObjectHolder, Subtype& subtype, Flags& flags) const
{
if (m_Type == Type::HostObject)
{
pHostObjectHolder = m_Data.pHostObjectHolder;
subtype = m_Subtype;
flags = m_Flags;
return true;
}
return false;
}
Type GetType() const
{
return m_Type;
}
void Decode(Decoded& decoded) const;
~V8Value()
{
Dispose();
}
private:
union Data
{
bool BooleanValue;
double DoubleValue;
const StdString* pString;
V8ObjectHolder* pV8ObjectHolder;
HostObjectHolder* pHostObjectHolder;
const V8BigInt* pBigInt;
};
void Copy(const V8Value& that)
{
m_Type = that.m_Type;
m_Subtype = that.m_Subtype;
m_Flags = that.m_Flags;
if (m_Type == Type::Boolean)
{
m_Data.BooleanValue = that.m_Data.BooleanValue;
}
else if (m_Type == Type::Number)
{
m_Data.DoubleValue = that.m_Data.DoubleValue;
}
else if (m_Type == Type::String)
{
m_Data.pString = new StdString(*that.m_Data.pString);
}
else if (m_Type == Type::DateTime)
{
m_Data.DoubleValue = that.m_Data.DoubleValue;
}
else if (m_Type == Type::BigInt)
{
m_Data.pBigInt = new V8BigInt(*that.m_Data.pBigInt);
}
else if (m_Type == Type::V8Object)
{
m_Data.pV8ObjectHolder = that.m_Data.pV8ObjectHolder->Clone();
}
else if (m_Type == Type::HostObject)
{
m_Data.pHostObjectHolder = that.m_Data.pHostObjectHolder->Clone();
}
}
void Move(V8Value& that)
{
m_Type = that.m_Type;
m_Subtype = that.m_Subtype;
m_Flags = that.m_Flags;
m_Data = that.m_Data;
that.m_Type = Type::Nonexistent;
}
void InitializeFromFastResult(const FastResult& result);
void Dispose()
{
if (m_Type == Type::String)
{
delete m_Data.pString;
}
else if (m_Type == Type::BigInt)
{
delete m_Data.pBigInt;
}
else if (m_Type == Type::V8Object)
{
delete m_Data.pV8ObjectHolder;
}
else if (m_Type == Type::HostObject)
{
delete m_Data.pHostObjectHolder;
}
}
Type m_Type;
Subtype m_Subtype;
Flags m_Flags;
int32_t m_Padding;
Data m_Data;
};
static_assert(sizeof(V8Value) == 16, "The managed SplitProxy code assumes that sizeof(V8Value) is 16 on all platforms.");
//-----------------------------------------------------------------------------
// DefaultInitializedV8Value
//-----------------------------------------------------------------------------
template <typename TInitializer>
class DefaultInitializedV8Value final: public V8Value
{
public:
using V8Value::V8Value;
DefaultInitializedV8Value():
V8Value(ms_Initializer)
{
static_assert(sizeof(DefaultInitializedV8Value) == sizeof(V8Value));
}
using V8Value::operator=;
private:
static const TInitializer ms_Initializer {};
};
using NonexistentV8Value = DefaultInitializedV8Value<V8Value::NonexistentInitializer>;
using UndefinedV8Value = DefaultInitializedV8Value<V8Value::UndefinedInitializer>;
using NullV8Value = DefaultInitializedV8Value<V8Value::NullInitializer>;