forked from vovgou/ClearScriptForUnity3d
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathV8Platform.h
More file actions
428 lines (346 loc) · 10.9 KB
/
V8Platform.h
File metadata and controls
428 lines (346 loc) · 10.9 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
#pragma once
//-----------------------------------------------------------------------------
// V8 headers
//-----------------------------------------------------------------------------
#pragma warning(push, 3)
#pragma warning(disable: 4996) // deprecated item usage
#define V8_DEPRECATION_WARNINGS
#define V8_IMMINENT_DEPRECATION_WARNINGS
#ifdef _M_X64
//#define V8_COMPRESS_POINTERS
//#define V8_31BIT_SMIS_ON_64BIT_ARCH
#endif //_M_X64
#include "v8.h"
#include "v8-platform.h"
#include "v8-inspector.h"
#include "v8-profiler.h"
#include "libplatform/libplatform.h"
#pragma warning(pop)
//-----------------------------------------------------------------------------
// V8FastPersistent - a (nearly) drop-in replacement for classic v8::Persistent
//
// WARNING: This class breaks encapsulation in order to avoid heap allocation.
// It makes assumptions about v8::Persistent implementation and memory layout.
//-----------------------------------------------------------------------------
template <typename T>
class V8FastPersistent final
{
template <typename TOther>
friend class V8FastPersistent;
public:
V8FastPersistent<T>():
m_pValue(nullptr)
{
}
template <typename TOther>
static V8FastPersistent<T> New(v8::Isolate* pIsolate, const v8::Local<TOther>& hValue)
{
v8::Persistent<T> hTemp(pIsolate, hValue);
return V8FastPersistent<T>(GetPtrAndClear(hTemp));
}
template <typename TOther>
static V8FastPersistent<T> New(v8::Isolate* pIsolate, const V8FastPersistent<TOther>& hValue)
{
v8::Persistent<T> hTemp(pIsolate, hValue.AsPersistent());
return V8FastPersistent<T>(GetPtrAndClear(hTemp));
}
template <typename TOther>
bool operator==(const v8::Local<TOther>& hValue) const
{
return AsPersistent() == hValue;
}
template <typename TOther>
bool operator==(const V8FastPersistent<TOther>& hValue) const
{
return AsPersistent() == hValue.AsPersistent();
}
template <typename TOther>
bool operator!=(const v8::Local<TOther>& hValue) const
{
return AsPersistent() != hValue;
}
template <typename TOther>
bool operator!=(const V8FastPersistent<TOther>& hValue) const
{
return AsPersistent() != hValue.AsPersistent();
}
bool IsEmpty() const
{
return AsPersistent().IsEmpty();
}
void* ToPtr() const
{
return m_pValue;
}
static V8FastPersistent<T> FromPtr(void* pvValue)
{
return V8FastPersistent<T>(static_cast<T*>(pvValue));
}
v8::Local<T> operator->() const
{
return CreateLocal();
}
template <typename TOther>
operator v8::Local<TOther>() const
{
return CreateLocal();
}
v8::Local<T> CreateLocal(v8::Isolate* pIsolate) const
{
return v8::Local<T>::New(pIsolate, AsPersistent());
}
bool IsWeak() const
{
return AsPersistent().IsWeak();
}
template <typename TArg1, typename TArg2>
V8FastPersistent<T> MakeWeak(v8::Isolate* pIsolate, TArg1* pArg1, TArg2* pArg2, void (*pCallback)(v8::Isolate*, V8FastPersistent<T>*, TArg1*, TArg2*))
{
IGNORE_UNUSED(pIsolate);
_ASSERTE(!IsWeak() && !IsEmpty());
AsPersistent().SetWeak(new WeakCallbackContext<TArg1, TArg2>(m_pValue, pArg1, pArg2, pCallback), WeakCallback, v8::WeakCallbackType::kParameter);
return *this;
}
void ClearWeak()
{
_ASSERTE(IsWeak());
auto pContext = AsPersistent().template ClearWeak<WeakCallbackContextBase>();
_ASSERTE(pContext);
delete pContext;
}
void Dispose()
{
AsPersistent().Reset();
}
private:
explicit V8FastPersistent<T>(T* pValue):
m_pValue(pValue)
{
}
v8::Local<T> CreateLocal() const
{
return v8::Local<T>::New(v8::Isolate::GetCurrent(), AsPersistent());
}
const v8::Persistent<T>& AsPersistent() const
{
return *(reinterpret_cast<const v8::Persistent<T>*>(&m_pValue));
}
v8::Persistent<T>& AsPersistent()
{
return *(reinterpret_cast<v8::Persistent<T>*>(&m_pValue));
}
static T* GetPtrAndClear(v8::Persistent<T>& hValue)
{
auto ppValue = reinterpret_cast<T**>(&hValue);
auto pValue = *ppValue;
*ppValue = nullptr;
_ASSERTE(hValue.IsEmpty());
return pValue;
}
class WeakCallbackContextBase
{
public:
virtual ~WeakCallbackContextBase() {}
};
template <typename TArg1, typename TArg2>
class WeakCallbackContext final: public WeakCallbackContextBase
{
public:
WeakCallbackContext(T* pValue, TArg1* pArg1, TArg2* pArg2, void (*pCallback)(v8::Isolate*, V8FastPersistent<T>*, TArg1*, TArg2*)):
m_pValue(pValue),
m_pArg1(pArg1),
m_pArg2(pArg2),
m_pCallback(pCallback)
{
}
void InvokeCallback(v8::Isolate* pIsolate) const
{
V8FastPersistent<T> hTarget(m_pValue);
m_pCallback(pIsolate, &hTarget, m_pArg1, m_pArg2);
}
private:
T* m_pValue;
TArg1* m_pArg1;
TArg2* m_pArg2;
void (*m_pCallback)(v8::Isolate*, V8FastPersistent<T>*, TArg1*, TArg2*);
};
template <typename TArg>
static void WeakCallback(const v8::WeakCallbackInfo<TArg>& data)
{
auto pContext = data.GetParameter();
_ASSERTE(pContext);
pContext->InvokeCallback(data.GetIsolate());
delete pContext;
}
T* m_pValue;
};
//-----------------------------------------------------------------------------
// V8SafePersistent - a (nearly) drop-in replacement for classic v8::Persistent
//-----------------------------------------------------------------------------
template <typename T>
class V8SafePersistent final
{
template <typename TOther>
friend class V8SafePersistent;
public:
V8SafePersistent<T>():
m_pImpl(nullptr)
{
}
template <typename TOther>
static V8SafePersistent<T> New(v8::Isolate* pIsolate, const v8::Local<TOther>& hValue)
{
return V8SafePersistent<T>(new v8::Persistent<T>(pIsolate, hValue));
}
template <typename TOther>
static V8SafePersistent<T> New(v8::Isolate* pIsolate, const V8SafePersistent<TOther>& hValue)
{
return V8SafePersistent<T>(new v8::Persistent<T>(pIsolate, hValue.GetImpl()));
}
template <typename TOther>
bool operator==(const v8::Local<TOther>& hValue) const
{
return GetImpl() == hValue;
}
template <typename TOther>
bool operator==(const V8SafePersistent<TOther>& hValue) const
{
return GetImpl() == hValue.GetImpl();
}
template <typename TOther>
bool operator!=(const v8::Local<TOther>& hValue) const
{
return GetImpl() != hValue;
}
template <typename TOther>
bool operator!=(const V8SafePersistent<TOther>& hValue) const
{
return GetImpl() != hValue.GetImpl();
}
bool IsEmpty() const
{
return (m_pImpl == nullptr) || m_pImpl->IsEmpty();
}
void* ToPtr() const
{
return m_pImpl;
}
static V8SafePersistent<T> FromPtr(void* pvImpl)
{
return V8SafePersistent<T>(static_cast<v8::Persistent<T>*>(pvImpl));
}
v8::Local<T> operator->() const
{
return CreateLocal();
}
template <typename TOther>
operator v8::Local<TOther>() const
{
return CreateLocal();
}
v8::Local<T> CreateLocal(v8::Isolate* pIsolate) const
{
return v8::Local<T>::New(pIsolate, GetImpl());
}
bool IsWeak() const
{
return (m_pImpl != nullptr) && m_pImpl->IsWeak();
}
template <typename TArg1, typename TArg2>
V8SafePersistent<T> MakeWeak(v8::Isolate* pIsolate, TArg1* pArg1, TArg2* pArg2, void (*pCallback)(v8::Isolate*, V8SafePersistent<T>*, TArg1*, TArg2*))
{
IGNORE_UNUSED(pIsolate);
_ASSERTE(!IsWeak() && !IsEmpty());
m_pImpl->SetWeak(new WeakCallbackContext<TArg1, TArg2>(m_pImpl, pArg1, pArg2, pCallback), WeakCallback, v8::WeakCallbackType::kParameter);
return *this;
}
void ClearWeak()
{
_ASSERTE(IsWeak());
auto pContext = m_pImpl->template ClearWeak<WeakCallbackContextBase>();
_ASSERTE(pContext);
delete pContext;
}
void Dispose()
{
if (m_pImpl != nullptr)
{
m_pImpl->Reset();
delete m_pImpl;
m_pImpl = nullptr;
}
}
private:
explicit V8SafePersistent<T>(v8::Persistent<T>* pImpl):
m_pImpl(pImpl)
{
}
v8::Local<T> CreateLocal() const
{
return v8::Local<T>::New(v8::Isolate::GetCurrent(), GetImpl());
}
const v8::Persistent<T>& GetImpl() const
{
return (m_pImpl != nullptr) ? *m_pImpl : ms_EmptyImpl;
}
class WeakCallbackContextBase
{
public:
virtual ~WeakCallbackContextBase() {}
};
template <typename TArg1, typename TArg2>
class WeakCallbackContext final: public WeakCallbackContextBase
{
public:
WeakCallbackContext(v8::Persistent<T>* pImpl, TArg1* pArg1, TArg2* pArg2, void (*pCallback)(v8::Isolate*, V8SafePersistent<T>*, TArg1*, TArg2*)):
m_pImpl(pImpl),
m_pArg1(pArg1),
m_pArg2(pArg2),
m_pCallback(pCallback)
{
}
void InvokeCallback(v8::Isolate* pIsolate) const
{
V8SafePersistent<T> hTarget(m_pImpl);
m_pCallback(pIsolate, &hTarget, m_pArg1, m_pArg2);
}
private:
v8::Persistent<T>* m_pImpl;
TArg1* m_pArg1;
TArg2* m_pArg2;
void (*m_pCallback)(v8::Isolate*, V8SafePersistent<T>*, TArg1*, TArg2*);
};
template <typename TArg>
static void WeakCallback(const v8::WeakCallbackInfo<TArg>& data)
{
auto pContext = data.GetParameter();
_ASSERTE(pContext);
pContext->InvokeCallback(data.GetIsolate());
delete pContext;
}
v8::Persistent<T>* m_pImpl;
static const v8::Persistent<T> ms_EmptyImpl;
};
template <typename T>
const v8::Persistent<T> V8SafePersistent<T>::ms_EmptyImpl;
//-----------------------------------------------------------------------------
// define classic v8::Persistent replacement
//-----------------------------------------------------------------------------
template <typename T>
using Persistent = V8FastPersistent<T>;
//-----------------------------------------------------------------------------
// helper functions
//-----------------------------------------------------------------------------
template <typename T>
inline void* PtrFromHandle(const Persistent<T>& handle)
{
return handle.ToPtr();
}
//-----------------------------------------------------------------------------
template <typename T>
inline Persistent<T> HandleFromPtr(void* pvObject)
{
return Persistent<T>::FromPtr(pvObject);
}