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
347 lines (296 loc) · 6.87 KB
/
Copy pathV8Value.h
File metadata and controls
347 lines (296 loc) · 6.87 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
#pragma once
//-----------------------------------------------------------------------------
// V8Value
//-----------------------------------------------------------------------------
class V8Value
{
public:
enum NonexistentInitializer
{
Nonexistent
};
enum UndefinedInitializer
{
Undefined
};
enum NullInitializer
{
Null
};
enum DateTimeInitializer
{
DateTime
};
enum class Subtype: std::uint16_t
{
None,
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(std::int32_t value):
m_Type(Type::Int32),
m_Subtype(Subtype::None)
{
m_Data.Int32Value = value;
}
explicit V8Value(std::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 V8Value& that)
{
Copy(that);
}
V8Value(V8Value&& that)
{
Move(that);
}
const V8Value& operator=(const V8Value& that)
{
Dispose();
Copy(that);
return *this;
}
const V8Value& operator=(V8Value&& that)
{
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(std::int32_t& result) const
{
if (m_Type == Type::Int32)
{
result = m_Data.Int32Value;
return true;
}
return false;
}
bool AsUInt32(std::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;
}
~V8Value()
{
Dispose();
}
private:
enum class Type: std::uint16_t
{
Nonexistent,
Undefined,
Null,
Boolean,
Number,
Int32,
UInt32,
String,
V8Object,
HostObject,
DateTime
};
union Data
{
bool BooleanValue;
double DoubleValue;
std::int32_t Int32Value;
std::uint32_t UInt32Value;
const StdString* pString;
V8ObjectHolder* pV8ObjectHolder;
HostObjectHolder* pHostObjectHolder;
};
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();
}
}
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;
}
}
Type m_Type;
Subtype m_Subtype;
Data m_Data;
};