forked from ClearFoundry/ClearScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathV8ObjectImpl.cpp
More file actions
358 lines (293 loc) · 10.8 KB
/
Copy pathV8ObjectImpl.cpp
File metadata and controls
358 lines (293 loc) · 10.8 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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
#include "ClearScriptV8Managed.h"
namespace Microsoft {
namespace ClearScript {
namespace V8 {
//-------------------------------------------------------------------------
// local helper functions
//-------------------------------------------------------------------------
static void ArrayBufferOrViewDataCallback(void* pvData, void* pvArg)
{
(*static_cast<Action<IntPtr>^*>(pvArg))(IntPtr(pvData));
}
//-------------------------------------------------------------------------
// V8ObjectImpl implementation
//-------------------------------------------------------------------------
V8ObjectImpl::V8ObjectImpl(V8ObjectHolder* pHolder, V8Value::Subtype subtype):
m_gcLock(gcnew Object),
m_pspHolder(new SharedPtr<V8ObjectHolder>(pHolder)),
m_Subtype(subtype)
{
}
//-------------------------------------------------------------------------
Object^ V8ObjectImpl::GetProperty(String^ gcName)
{
try
{
return V8ContextProxyImpl::ExportValue(V8ObjectHelpers::GetProperty(GetHolder(), StdString(gcName)));
}
catch (const V8Exception& exception)
{
exception.ThrowScriptEngineException();
}
}
//-------------------------------------------------------------------------
Object^ V8ObjectImpl::GetProperty(String^ gcName, [Out] Boolean% isCacheable)
{
isCacheable = false;
return GetProperty(gcName);
}
//-------------------------------------------------------------------------
void V8ObjectImpl::SetProperty(String^ gcName, Object^ gcValue)
{
try
{
V8ObjectHelpers::SetProperty(GetHolder(), StdString(gcName), V8ContextProxyImpl::ImportValue(gcValue));
}
catch (const V8Exception& exception)
{
exception.ThrowScriptEngineException();
}
}
//-------------------------------------------------------------------------
bool V8ObjectImpl::DeleteProperty(String^ gcName)
{
try
{
return V8ObjectHelpers::DeleteProperty(GetHolder(), StdString(gcName));
}
catch (const V8Exception& exception)
{
exception.ThrowScriptEngineException();
}
}
//-------------------------------------------------------------------------
array<String^>^ V8ObjectImpl::GetPropertyNames()
{
try
{
std::vector<StdString> names;
V8ObjectHelpers::GetPropertyNames(GetHolder(), names);
auto nameCount = static_cast<int>(names.size());
auto gcNames = gcnew array<String^>(nameCount);
for (auto index = 0; index < nameCount; index++)
{
gcNames[index] = names[index].ToManagedString();
}
return gcNames;
}
catch (const V8Exception& exception)
{
exception.ThrowScriptEngineException();
}
}
//-------------------------------------------------------------------------
Object^ V8ObjectImpl::GetProperty(int index)
{
try
{
return V8ContextProxyImpl::ExportValue(V8ObjectHelpers::GetProperty(GetHolder(), index));
}
catch (const V8Exception& exception)
{
exception.ThrowScriptEngineException();
}
}
//-------------------------------------------------------------------------
void V8ObjectImpl::SetProperty(int index, Object^ gcValue)
{
try
{
V8ObjectHelpers::SetProperty(GetHolder(), index, V8ContextProxyImpl::ImportValue(gcValue));
}
catch (const V8Exception& exception)
{
exception.ThrowScriptEngineException();
}
}
//-------------------------------------------------------------------------
bool V8ObjectImpl::DeleteProperty(int index)
{
try
{
return V8ObjectHelpers::DeleteProperty(GetHolder(), index);
}
catch (const V8Exception& exception)
{
exception.ThrowScriptEngineException();
}
}
//-------------------------------------------------------------------------
array<int>^ V8ObjectImpl::GetPropertyIndices()
{
try
{
std::vector<int> indices;
V8ObjectHelpers::GetPropertyIndices(GetHolder(), indices);
auto indexCount = static_cast<int>(indices.size());
auto gcIndices = gcnew array<int>(indexCount);
for (auto index = 0; index < indexCount; index++)
{
gcIndices[index] = indices[index];
}
return gcIndices;
}
catch (const V8Exception& exception)
{
exception.ThrowScriptEngineException();
}
}
//-------------------------------------------------------------------------
Object^ V8ObjectImpl::Invoke(array<Object^>^ gcArgs, bool asConstructor)
{
try
{
std::vector<V8Value> importedArgs;
ImportValues(gcArgs, importedArgs);
return V8ContextProxyImpl::ExportValue(V8ObjectHelpers::Invoke(GetHolder(), importedArgs, asConstructor));
}
catch (const V8Exception& exception)
{
exception.ThrowScriptEngineException();
}
}
//-------------------------------------------------------------------------
Object^ V8ObjectImpl::InvokeMethod(String^ gcName, array<Object^>^ gcArgs)
{
try
{
std::vector<V8Value> importedArgs;
ImportValues(gcArgs, importedArgs);
return V8ContextProxyImpl::ExportValue(V8ObjectHelpers::InvokeMethod(GetHolder(), StdString(gcName), importedArgs));
}
catch (const V8Exception& exception)
{
exception.ThrowScriptEngineException();
}
}
//-------------------------------------------------------------------------
bool V8ObjectImpl::IsArrayBufferOrView()
{
return m_Subtype != V8Value::Subtype::None;
}
//-------------------------------------------------------------------------
V8ArrayBufferOrViewKind V8ObjectImpl::GetArrayBufferOrViewKind()
{
auto kind = V8ArrayBufferOrViewKind::None;
if (m_Subtype == V8Value::Subtype::ArrayBuffer)
kind = V8ArrayBufferOrViewKind::ArrayBuffer;
else if (m_Subtype == V8Value::Subtype::DataView)
kind = V8ArrayBufferOrViewKind::DataView;
else if (m_Subtype == V8Value::Subtype::Uint8Array)
kind = V8ArrayBufferOrViewKind::Uint8Array;
else if (m_Subtype == V8Value::Subtype::Uint8ClampedArray)
kind = V8ArrayBufferOrViewKind::Uint8ClampedArray;
else if (m_Subtype == V8Value::Subtype::Int8Array)
kind = V8ArrayBufferOrViewKind::Int8Array;
else if (m_Subtype == V8Value::Subtype::Uint16Array)
kind = V8ArrayBufferOrViewKind::Uint16Array;
else if (m_Subtype == V8Value::Subtype::Int16Array)
kind = V8ArrayBufferOrViewKind::Int16Array;
else if (m_Subtype == V8Value::Subtype::Uint32Array)
kind = V8ArrayBufferOrViewKind::Uint32Array;
else if (m_Subtype == V8Value::Subtype::Int32Array)
kind = V8ArrayBufferOrViewKind::Int32Array;
else if (m_Subtype == V8Value::Subtype::Float32Array)
kind = V8ArrayBufferOrViewKind::Float32Array;
else if (m_Subtype == V8Value::Subtype::Float64Array)
kind = V8ArrayBufferOrViewKind::Float64Array;
return kind;
}
//-------------------------------------------------------------------------
V8ArrayBufferOrViewInfo^ V8ObjectImpl::GetArrayBufferOrViewInfo()
{
try
{
auto kind = GetArrayBufferOrViewKind();
if (kind != V8ArrayBufferOrViewKind::None)
{
V8Value arrayBuffer(V8Value::Null);
size_t offset;
size_t size;
size_t length;
V8ObjectHelpers::GetArrayBufferOrViewInfo(GetHolder(), arrayBuffer, offset, size, length);
return gcnew V8ArrayBufferOrViewInfo(kind, (IV8Object^)V8ContextProxyImpl::ExportValue(arrayBuffer), offset, size, length);
}
return nullptr;
}
catch (const V8Exception& exception)
{
exception.ThrowScriptEngineException();
}
}
//-------------------------------------------------------------------------
void V8ObjectImpl::InvokeWithArrayBufferOrViewData(Action<IntPtr>^ gcAction)
{
try
{
V8ObjectHelpers::InvokeWithArrayBufferOrViewData(GetHolder(), ArrayBufferOrViewDataCallback, &gcAction);
}
catch (const V8Exception& exception)
{
exception.ThrowScriptEngineException();
}
}
//-------------------------------------------------------------------------
SharedPtr<V8ObjectHolder> V8ObjectImpl::GetHolder()
{
BEGIN_LOCK_SCOPE(m_gcLock)
if (m_pspHolder == nullptr)
{
throw gcnew ObjectDisposedException(ToString());
}
return *m_pspHolder;
END_LOCK_SCOPE
}
//-------------------------------------------------------------------------
V8Value::Subtype V8ObjectImpl::GetSubtype()
{
return m_Subtype;
}
//-------------------------------------------------------------------------
V8ObjectImpl::~V8ObjectImpl()
{
SharedPtr<V8ObjectHolder> spHolder;
BEGIN_LOCK_SCOPE(m_gcLock)
if (m_pspHolder != nullptr)
{
// hold V8 object holder for destruction outside lock scope
spHolder = *m_pspHolder;
delete m_pspHolder;
m_pspHolder = nullptr;
}
END_LOCK_SCOPE
if (!spHolder.IsEmpty())
{
GC::SuppressFinalize(this);
}
}
//-------------------------------------------------------------------------
V8ObjectImpl::!V8ObjectImpl()
{
if (m_pspHolder != nullptr)
{
delete m_pspHolder;
m_pspHolder = nullptr;
}
}
//-------------------------------------------------------------------------
void V8ObjectImpl::ImportValues(array<Object^>^ gcValues, std::vector<V8Value>& importedValues)
{
importedValues.clear();
if (gcValues != nullptr)
{
auto valueCount = gcValues->Length;
importedValues.reserve(valueCount);
for (auto index = 0; index < valueCount; index++)
{
importedValues.push_back(V8ContextProxyImpl::ImportValue(gcValues[index]));
}
}
}
}}}