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
440 lines (373 loc) · 8.67 KB
/
V8Value.h
File metadata and controls
440 lines (373 loc) · 8.67 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
// 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) :
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)
{
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 final
{
public:
enum NonexistentInitializer
{
Nonexistent
};
enum UndefinedInitializer
{
Undefined
};
enum NullInitializer
{
Null
};
enum DateTimeInitializer
{
DateTime
};
enum class Subtype: uint16_t
{
None,
Array,
ArrayBuffer,
DataView,
Uint8Array,
Uint8ClampedArray,
Int8Array,
Uint16Array,
Int16Array,
Uint32Array,
Int32Array,
Float32Array,
Float64Array
};
explicit V8Value(NonexistentInitializer):
m_Type(Type::Nonexistent),
m_Subtype(Subtype::None)
{
}
explicit V8Value(UndefinedInitializer):
m_Type(Type::Undefined),
m_Subtype(Subtype::None)
{
}
explicit V8Value(NullInitializer):
m_Type(Type::Null),
m_Subtype(Subtype::None)
{
}
explicit V8Value(bool value):
m_Type(Type::Boolean),
m_Subtype(Subtype::None)
{
m_Data.BooleanValue = value;
}
explicit V8Value(double value):
m_Type(Type::Number),
m_Subtype(Subtype::None)
{
m_Data.DoubleValue = value;
}
explicit V8Value(int32_t value):
m_Type(Type::Int32),
m_Subtype(Subtype::None)
{
m_Data.Int32Value = value;
}
explicit V8Value(uint32_t value):
m_Type(Type::UInt32),
m_Subtype(Subtype::None)
{
m_Data.UInt32Value = value;
}
explicit V8Value(const StdString* pString):
m_Type(Type::String),
m_Subtype(Subtype::None)
{
m_Data.pString = pString;
}
V8Value(V8ObjectHolder* pV8ObjectHolder, Subtype subtype):
m_Type(Type::V8Object),
m_Subtype(subtype)
{
m_Data.pV8ObjectHolder = pV8ObjectHolder;
}
explicit V8Value(HostObjectHolder* pHostObjectHolder):
m_Type(Type::HostObject),
m_Subtype(Subtype::None)
{
m_Data.pHostObjectHolder = pHostObjectHolder;
}
V8Value(DateTimeInitializer, double value):
m_Type(Type::DateTime),
m_Subtype(Subtype::None)
{
m_Data.DoubleValue = value;
}
V8Value(const V8BigInt* pBigInt):
m_Type(Type::BigInt),
m_Subtype(Subtype::None)
{
m_Data.pBigInt = pBigInt;
}
V8Value(const V8Value& that)
{
Copy(that);
}
V8Value(V8Value&& that) noexcept
{
Move(that);
}
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 AsInt32(int32_t& result) const
{
if (m_Type == Type::Int32)
{
result = m_Data.Int32Value;
return true;
}
return false;
}
bool AsUInt32(uint32_t& result) const
{
if (m_Type == Type::UInt32)
{
result = m_Data.UInt32Value;
return true;
}
return false;
}
bool AsString(const StdString*& pString) const
{
if (m_Type == Type::String)
{
pString = m_Data.pString;
return true;
}
return false;
}
bool AsV8Object(V8ObjectHolder*& pV8ObjectHolder, Subtype& subtype) const
{
if (m_Type == Type::V8Object)
{
pV8ObjectHolder = m_Data.pV8ObjectHolder;
subtype = m_Subtype;
return true;
}
return false;
}
bool AsHostObject(HostObjectHolder*& pHostObjectHolder) const
{
if (m_Type == Type::HostObject)
{
pHostObjectHolder = m_Data.pHostObjectHolder;
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;
}
~V8Value()
{
Dispose();
}
private:
enum class Type: uint16_t
{
Nonexistent,
Undefined,
Null,
Boolean,
Number,
Int32,
UInt32,
String,
V8Object,
HostObject,
DateTime,
BigInt
};
union Data
{
bool BooleanValue;
double DoubleValue;
int32_t Int32Value;
uint32_t UInt32Value;
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;
if (m_Type == Type::Boolean)
{
m_Data.BooleanValue = that.m_Data.BooleanValue;
}
else if ((m_Type == Type::Number) || (m_Type == Type::DateTime))
{
m_Data.DoubleValue = that.m_Data.DoubleValue;
}
else if (m_Type == Type::Int32)
{
m_Data.Int32Value = that.m_Data.Int32Value;
}
else if (m_Type == Type::UInt32)
{
m_Data.UInt32Value = that.m_Data.UInt32Value;
}
else if (m_Type == Type::String)
{
m_Data.pString = new StdString(*that.m_Data.pString);
}
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();
}
else if (m_Type == Type::BigInt)
{
m_Data.pBigInt = new V8BigInt(*that.m_Data.pBigInt);
}
}
void Move(V8Value& that)
{
m_Type = that.m_Type;
m_Subtype = that.m_Subtype;
m_Data = that.m_Data;
that.m_Type = Type::Undefined;
}
void Dispose()
{
if (m_Type == Type::String)
{
delete m_Data.pString;
}
else if (m_Type == Type::V8Object)
{
delete m_Data.pV8ObjectHolder;
}
else if (m_Type == Type::HostObject)
{
delete m_Data.pHostObjectHolder;
}
else if (m_Type == Type::BigInt)
{
delete m_Data.pBigInt;
}
}
Type m_Type;
Subtype m_Subtype;
Data m_Data {};
};