Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(280)

Side by Side Diff: fxjs/fxjs_v8.cpp

Issue 2245863002: Push v8::Isolate into CFXJS_Engine class (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: Make members private Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « fpdfsdk/javascript/util.cpp ('k') | fxjs/fxjs_v8_embeddertest.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 PDFium Authors. All rights reserved. 1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 6
7 #include "fxjs/include/fxjs_v8.h" 7 #include "fxjs/include/fxjs_v8.h"
8 8
9 #include <vector> 9 #include <vector>
10 10
(...skipping 26 matching lines...) Expand all
37 } 37 }
38 38
39 static CFXJS_ObjDefinition* ForID(v8::Isolate* pIsolate, int id) { 39 static CFXJS_ObjDefinition* ForID(v8::Isolate* pIsolate, int id) {
40 // Note: GetAt() halts if out-of-range even in release builds. 40 // Note: GetAt() halts if out-of-range even in release builds.
41 return FXJS_PerIsolateData::Get(pIsolate)->m_ObjectDefnArray[id]; 41 return FXJS_PerIsolateData::Get(pIsolate)->m_ObjectDefnArray[id];
42 } 42 }
43 43
44 CFXJS_ObjDefinition(v8::Isolate* isolate, 44 CFXJS_ObjDefinition(v8::Isolate* isolate,
45 const wchar_t* sObjName, 45 const wchar_t* sObjName,
46 FXJSOBJTYPE eObjType, 46 FXJSOBJTYPE eObjType,
47 FXJS_CONSTRUCTOR pConstructor, 47 CFXJS_Engine::Constructor pConstructor,
48 FXJS_DESTRUCTOR pDestructor) 48 CFXJS_Engine::Destructor pDestructor)
49 : m_ObjName(sObjName), 49 : m_ObjName(sObjName),
50 m_ObjType(eObjType), 50 m_ObjType(eObjType),
51 m_pConstructor(pConstructor), 51 m_pConstructor(pConstructor),
52 m_pDestructor(pDestructor), 52 m_pDestructor(pDestructor),
53 m_pIsolate(isolate) { 53 m_pIsolate(isolate) {
54 v8::Isolate::Scope isolate_scope(isolate); 54 v8::Isolate::Scope isolate_scope(isolate);
55 v8::HandleScope handle_scope(isolate); 55 v8::HandleScope handle_scope(isolate);
56 56
57 v8::Local<v8::FunctionTemplate> fun = v8::FunctionTemplate::New(isolate); 57 v8::Local<v8::FunctionTemplate> fun = v8::FunctionTemplate::New(isolate);
58 fun->InstanceTemplate()->SetInternalFieldCount(2); 58 fun->InstanceTemplate()->SetInternalFieldCount(2);
(...skipping 22 matching lines...) Expand all
81 return scope.Escape(function->InstanceTemplate()); 81 return scope.Escape(function->InstanceTemplate());
82 } 82 }
83 83
84 v8::Local<v8::Signature> GetSignature() { 84 v8::Local<v8::Signature> GetSignature() {
85 v8::EscapableHandleScope scope(m_pIsolate); 85 v8::EscapableHandleScope scope(m_pIsolate);
86 return scope.Escape(m_Signature.Get(m_pIsolate)); 86 return scope.Escape(m_Signature.Get(m_pIsolate));
87 } 87 }
88 88
89 const wchar_t* const m_ObjName; 89 const wchar_t* const m_ObjName;
90 const FXJSOBJTYPE m_ObjType; 90 const FXJSOBJTYPE m_ObjType;
91 const FXJS_CONSTRUCTOR m_pConstructor; 91 const CFXJS_Engine::Constructor m_pConstructor;
92 const FXJS_DESTRUCTOR m_pDestructor; 92 const CFXJS_Engine::Destructor m_pDestructor;
93 93
94 v8::Isolate* m_pIsolate; 94 v8::Isolate* m_pIsolate;
95 v8::Global<v8::FunctionTemplate> m_FunctionTemplate; 95 v8::Global<v8::FunctionTemplate> m_FunctionTemplate;
96 v8::Global<v8::Signature> m_Signature; 96 v8::Global<v8::Signature> m_Signature;
97 }; 97 };
98 98
99 static v8::Local<v8::ObjectTemplate> GetGlobalObjectTemplate( 99 static v8::Local<v8::ObjectTemplate> GetGlobalObjectTemplate(
100 v8::Isolate* pIsolate) { 100 v8::Isolate* pIsolate) {
101 int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); 101 int maxID = CFXJS_ObjDefinition::MaxID(pIsolate);
102 for (int i = 0; i < maxID; ++i) { 102 for (int i = 0; i < maxID; ++i) {
(...skipping 25 matching lines...) Expand all
128 void FXJS_ArrayBufferAllocator::Free(void* data, size_t length) { 128 void FXJS_ArrayBufferAllocator::Free(void* data, size_t length) {
129 free(data); 129 free(data);
130 } 130 }
131 131
132 void V8TemplateMapTraits::Dispose(v8::Isolate* isolate, 132 void V8TemplateMapTraits::Dispose(v8::Isolate* isolate,
133 v8::Global<v8::Object> value, 133 v8::Global<v8::Object> value,
134 void* key) { 134 void* key) {
135 v8::Local<v8::Object> obj = value.Get(isolate); 135 v8::Local<v8::Object> obj = value.Get(isolate);
136 if (obj.IsEmpty()) 136 if (obj.IsEmpty())
137 return; 137 return;
138 int id = FXJS_GetObjDefnID(obj); 138 CFXJS_Engine* pEngine = CFXJS_Engine::CurrentEngineFromIsolate(isolate);
139 int id = pEngine->GetObjDefnID(obj);
139 if (id == -1) 140 if (id == -1)
140 return; 141 return;
141
142 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(isolate, id); 142 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(isolate, id);
143 if (!pObjDef) 143 if (!pObjDef)
144 return; 144 return;
145 if (pObjDef->m_pDestructor) 145 if (pObjDef->m_pDestructor)
146 pObjDef->m_pDestructor(obj); 146 pObjDef->m_pDestructor(pEngine, obj);
147 FXJS_FreePrivate(obj); 147 CFXJS_Engine::FreeObjectPrivate(obj);
148 } 148 }
149 149
150 V8TemplateMapTraits::MapType* V8TemplateMapTraits::MapFromWeakCallbackInfo( 150 V8TemplateMapTraits::MapType* V8TemplateMapTraits::MapFromWeakCallbackInfo(
151 const v8::WeakCallbackInfo<WeakCallbackDataType>& data) { 151 const v8::WeakCallbackInfo<WeakCallbackDataType>& data) {
152 V8TemplateMap* pMap = 152 V8TemplateMap* pMap =
153 (FXJS_PerIsolateData::Get(data.GetIsolate()))->m_pDynamicObjsMap; 153 (FXJS_PerIsolateData::Get(data.GetIsolate()))->m_pDynamicObjsMap;
154 return pMap ? &pMap->m_map : nullptr; 154 return pMap ? &pMap->m_map : nullptr;
155 } 155 }
156 156
157 void FXJS_Initialize(unsigned int embedderDataSlot, v8::Isolate* pIsolate) { 157 void FXJS_Initialize(unsigned int embedderDataSlot, v8::Isolate* pIsolate) {
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
215 pIsolate->GetData(g_embedderDataSlot)); 215 pIsolate->GetData(g_embedderDataSlot));
216 } 216 }
217 217
218 #ifndef PDF_ENABLE_XFA 218 #ifndef PDF_ENABLE_XFA
219 FXJS_PerIsolateData::FXJS_PerIsolateData() : m_pDynamicObjsMap(nullptr) {} 219 FXJS_PerIsolateData::FXJS_PerIsolateData() : m_pDynamicObjsMap(nullptr) {}
220 #else // PDF_ENABLE_XFA 220 #else // PDF_ENABLE_XFA
221 FXJS_PerIsolateData::FXJS_PerIsolateData() 221 FXJS_PerIsolateData::FXJS_PerIsolateData()
222 : m_pFXJSERuntimeData(nullptr), m_pDynamicObjsMap(nullptr) {} 222 : m_pFXJSERuntimeData(nullptr), m_pDynamicObjsMap(nullptr) {}
223 #endif // PDF_ENABLE_XFA 223 #endif // PDF_ENABLE_XFA
224 224
225 CFXJS_Engine::CFXJS_Engine() : m_isolate(nullptr) {} 225 CFXJS_Engine::CFXJS_Engine() : m_isolate(nullptr) {}
dsinclair 2016/08/15 20:38:37 Now that most of this is moved over, should we ren
Tom Sepez 2016/08/15 20:46:15 Probably someday. Or cfxjs_engine_v8, since there
226 CFXJS_Engine::~CFXJS_Engine() {}
227 226
228 int FXJS_DefineObj(v8::Isolate* pIsolate, 227 CFXJS_Engine::~CFXJS_Engine() {
229 const wchar_t* sObjName, 228 m_V8PersistentContext.Reset();
230 FXJSOBJTYPE eObjType, 229 }
231 FXJS_CONSTRUCTOR pConstructor,
232 FXJS_DESTRUCTOR pDestructor) {
233 v8::Isolate::Scope isolate_scope(pIsolate);
234 v8::HandleScope handle_scope(pIsolate);
235 230
236 FXJS_PerIsolateData::SetUp(pIsolate); 231 int CFXJS_Engine::DefineObj(const wchar_t* sObjName,
232 FXJSOBJTYPE eObjType,
233 CFXJS_Engine::Constructor pConstructor,
234 CFXJS_Engine::Destructor pDestructor) {
235 v8::Isolate::Scope isolate_scope(m_isolate);
236 v8::HandleScope handle_scope(m_isolate);
237 FXJS_PerIsolateData::SetUp(m_isolate);
237 CFXJS_ObjDefinition* pObjDef = new CFXJS_ObjDefinition( 238 CFXJS_ObjDefinition* pObjDef = new CFXJS_ObjDefinition(
238 pIsolate, sObjName, eObjType, pConstructor, pDestructor); 239 m_isolate, sObjName, eObjType, pConstructor, pDestructor);
239 return pObjDef->AssignID(); 240 return pObjDef->AssignID();
240 } 241 }
241 242
242 void FXJS_DefineObjMethod(v8::Isolate* pIsolate, 243 void CFXJS_Engine::DefineObjMethod(int nObjDefnID,
243 int nObjDefnID, 244 const wchar_t* sMethodName,
244 const wchar_t* sMethodName, 245 v8::FunctionCallback pMethodCall) {
245 v8::FunctionCallback pMethodCall) { 246 v8::Isolate::Scope isolate_scope(m_isolate);
246 v8::Isolate::Scope isolate_scope(pIsolate); 247 v8::HandleScope handle_scope(m_isolate);
247 v8::HandleScope handle_scope(pIsolate);
248 CFX_ByteString bsMethodName = CFX_WideString(sMethodName).UTF8Encode(); 248 CFX_ByteString bsMethodName = CFX_WideString(sMethodName).UTF8Encode();
249 CFXJS_ObjDefinition* pObjDef = 249 CFXJS_ObjDefinition* pObjDef =
250 CFXJS_ObjDefinition::ForID(pIsolate, nObjDefnID); 250 CFXJS_ObjDefinition::ForID(m_isolate, nObjDefnID);
251 v8::Local<v8::FunctionTemplate> fun = v8::FunctionTemplate::New( 251 v8::Local<v8::FunctionTemplate> fun = v8::FunctionTemplate::New(
252 pIsolate, pMethodCall, v8::Local<v8::Value>(), pObjDef->GetSignature()); 252 m_isolate, pMethodCall, v8::Local<v8::Value>(), pObjDef->GetSignature());
253 fun->RemovePrototype(); 253 fun->RemovePrototype();
254 pObjDef->GetInstanceTemplate()->Set( 254 pObjDef->GetInstanceTemplate()->Set(
255 v8::String::NewFromUtf8(pIsolate, bsMethodName.c_str(), 255 v8::String::NewFromUtf8(m_isolate, bsMethodName.c_str(),
256 v8::NewStringType::kNormal) 256 v8::NewStringType::kNormal)
257 .ToLocalChecked(), 257 .ToLocalChecked(),
258 fun, v8::ReadOnly); 258 fun, v8::ReadOnly);
259 } 259 }
260 260
261 void FXJS_DefineObjProperty(v8::Isolate* pIsolate, 261 void CFXJS_Engine::DefineObjProperty(int nObjDefnID,
262 int nObjDefnID, 262 const wchar_t* sPropName,
263 const wchar_t* sPropName, 263 v8::AccessorGetterCallback pPropGet,
264 v8::AccessorGetterCallback pPropGet, 264 v8::AccessorSetterCallback pPropPut) {
265 v8::AccessorSetterCallback pPropPut) { 265 v8::Isolate::Scope isolate_scope(m_isolate);
266 v8::Isolate::Scope isolate_scope(pIsolate); 266 v8::HandleScope handle_scope(m_isolate);
267 v8::HandleScope handle_scope(pIsolate);
268 CFX_ByteString bsPropertyName = CFX_WideString(sPropName).UTF8Encode(); 267 CFX_ByteString bsPropertyName = CFX_WideString(sPropName).UTF8Encode();
269 CFXJS_ObjDefinition* pObjDef = 268 CFXJS_ObjDefinition* pObjDef =
270 CFXJS_ObjDefinition::ForID(pIsolate, nObjDefnID); 269 CFXJS_ObjDefinition::ForID(m_isolate, nObjDefnID);
271 pObjDef->GetInstanceTemplate()->SetAccessor( 270 pObjDef->GetInstanceTemplate()->SetAccessor(
272 v8::String::NewFromUtf8(pIsolate, bsPropertyName.c_str(), 271 v8::String::NewFromUtf8(m_isolate, bsPropertyName.c_str(),
273 v8::NewStringType::kNormal) 272 v8::NewStringType::kNormal)
274 .ToLocalChecked(), 273 .ToLocalChecked(),
275 pPropGet, pPropPut); 274 pPropGet, pPropPut);
276 } 275 }
277 276
278 void FXJS_DefineObjAllProperties(v8::Isolate* pIsolate, 277 void CFXJS_Engine::DefineObjAllProperties(
279 int nObjDefnID, 278 int nObjDefnID,
280 v8::NamedPropertyQueryCallback pPropQurey, 279 v8::NamedPropertyQueryCallback pPropQurey,
281 v8::NamedPropertyGetterCallback pPropGet, 280 v8::NamedPropertyGetterCallback pPropGet,
282 v8::NamedPropertySetterCallback pPropPut, 281 v8::NamedPropertySetterCallback pPropPut,
283 v8::NamedPropertyDeleterCallback pPropDel) { 282 v8::NamedPropertyDeleterCallback pPropDel) {
284 v8::Isolate::Scope isolate_scope(pIsolate); 283 v8::Isolate::Scope isolate_scope(m_isolate);
285 v8::HandleScope handle_scope(pIsolate); 284 v8::HandleScope handle_scope(m_isolate);
286 CFXJS_ObjDefinition* pObjDef = 285 CFXJS_ObjDefinition* pObjDef =
287 CFXJS_ObjDefinition::ForID(pIsolate, nObjDefnID); 286 CFXJS_ObjDefinition::ForID(m_isolate, nObjDefnID);
288 pObjDef->GetInstanceTemplate()->SetNamedPropertyHandler(pPropGet, pPropPut, 287 pObjDef->GetInstanceTemplate()->SetNamedPropertyHandler(pPropGet, pPropPut,
289 pPropQurey, pPropDel); 288 pPropQurey, pPropDel);
290 } 289 }
291 290
292 void FXJS_DefineObjConst(v8::Isolate* pIsolate, 291 void CFXJS_Engine::DefineObjConst(int nObjDefnID,
293 int nObjDefnID, 292 const wchar_t* sConstName,
294 const wchar_t* sConstName, 293 v8::Local<v8::Value> pDefault) {
295 v8::Local<v8::Value> pDefault) { 294 v8::Isolate::Scope isolate_scope(m_isolate);
296 v8::Isolate::Scope isolate_scope(pIsolate); 295 v8::HandleScope handle_scope(m_isolate);
297 v8::HandleScope handle_scope(pIsolate);
298 CFX_ByteString bsConstName = CFX_WideString(sConstName).UTF8Encode(); 296 CFX_ByteString bsConstName = CFX_WideString(sConstName).UTF8Encode();
299 CFXJS_ObjDefinition* pObjDef = 297 CFXJS_ObjDefinition* pObjDef =
300 CFXJS_ObjDefinition::ForID(pIsolate, nObjDefnID); 298 CFXJS_ObjDefinition::ForID(m_isolate, nObjDefnID);
301 pObjDef->GetInstanceTemplate()->Set(pIsolate, bsConstName.c_str(), pDefault); 299 pObjDef->GetInstanceTemplate()->Set(m_isolate, bsConstName.c_str(), pDefault);
302 } 300 }
303 301
304 void FXJS_DefineGlobalMethod(v8::Isolate* pIsolate, 302 void CFXJS_Engine::DefineGlobalMethod(const wchar_t* sMethodName,
305 const wchar_t* sMethodName, 303 v8::FunctionCallback pMethodCall) {
306 v8::FunctionCallback pMethodCall) { 304 v8::Isolate::Scope isolate_scope(m_isolate);
307 v8::Isolate::Scope isolate_scope(pIsolate); 305 v8::HandleScope handle_scope(m_isolate);
308 v8::HandleScope handle_scope(pIsolate);
309 CFX_ByteString bsMethodName = CFX_WideString(sMethodName).UTF8Encode(); 306 CFX_ByteString bsMethodName = CFX_WideString(sMethodName).UTF8Encode();
310 v8::Local<v8::FunctionTemplate> fun = 307 v8::Local<v8::FunctionTemplate> fun =
311 v8::FunctionTemplate::New(pIsolate, pMethodCall); 308 v8::FunctionTemplate::New(m_isolate, pMethodCall);
312 fun->RemovePrototype(); 309 fun->RemovePrototype();
313 GetGlobalObjectTemplate(pIsolate)->Set( 310 GetGlobalObjectTemplate(m_isolate)->Set(
314 v8::String::NewFromUtf8(pIsolate, bsMethodName.c_str(), 311 v8::String::NewFromUtf8(m_isolate, bsMethodName.c_str(),
315 v8::NewStringType::kNormal) 312 v8::NewStringType::kNormal)
316 .ToLocalChecked(), 313 .ToLocalChecked(),
317 fun, v8::ReadOnly); 314 fun, v8::ReadOnly);
318 } 315 }
319 316
320 void FXJS_DefineGlobalConst(v8::Isolate* pIsolate, 317 void CFXJS_Engine::DefineGlobalConst(const wchar_t* sConstName,
321 const wchar_t* sConstName, 318 v8::FunctionCallback pConstGetter) {
322 v8::FunctionCallback pConstGetter) { 319 v8::Isolate::Scope isolate_scope(m_isolate);
323 v8::Isolate::Scope isolate_scope(pIsolate); 320 v8::HandleScope handle_scope(m_isolate);
324 v8::HandleScope handle_scope(pIsolate);
325 CFX_ByteString bsConst = CFX_WideString(sConstName).UTF8Encode(); 321 CFX_ByteString bsConst = CFX_WideString(sConstName).UTF8Encode();
326 v8::Local<v8::FunctionTemplate> fun = 322 v8::Local<v8::FunctionTemplate> fun =
327 v8::FunctionTemplate::New(pIsolate, pConstGetter); 323 v8::FunctionTemplate::New(m_isolate, pConstGetter);
328 fun->RemovePrototype(); 324 fun->RemovePrototype();
329 GetGlobalObjectTemplate(pIsolate)->SetAccessorProperty( 325 GetGlobalObjectTemplate(m_isolate)->SetAccessorProperty(
330 v8::String::NewFromUtf8(pIsolate, bsConst.c_str(), 326 v8::String::NewFromUtf8(m_isolate, bsConst.c_str(),
331 v8::NewStringType::kNormal) 327 v8::NewStringType::kNormal)
332 .ToLocalChecked(), 328 .ToLocalChecked(),
333 fun); 329 fun);
334 } 330 }
335 331
336 void FXJS_InitializeEngine( 332 void CFXJS_Engine::InitializeEngine() {
337 v8::Isolate* pIsolate, 333 if (m_isolate == g_isolate)
338 CFXJS_Engine* pEngine,
339 v8::Global<v8::Context>* pV8PersistentContext,
340 std::vector<v8::Global<v8::Object>*>* pStaticObjects) {
341 if (pIsolate == g_isolate)
342 ++g_isolate_ref_count; 334 ++g_isolate_ref_count;
343 335
344 v8::Isolate::Scope isolate_scope(pIsolate); 336 v8::Isolate::Scope isolate_scope(m_isolate);
345 v8::HandleScope handle_scope(pIsolate); 337 v8::HandleScope handle_scope(m_isolate);
346 338
347 // This has to happen before we call GetGlobalObjectTemplate because that 339 // This has to happen before we call GetGlobalObjectTemplate because that
348 // method gets the PerIsolateData from pIsolate. 340 // method gets the PerIsolateData from m_isolate.
349 FXJS_PerIsolateData::SetUp(pIsolate); 341 FXJS_PerIsolateData::SetUp(m_isolate);
350 342
351 v8::Local<v8::Context> v8Context = 343 v8::Local<v8::Context> v8Context =
352 v8::Context::New(pIsolate, nullptr, GetGlobalObjectTemplate(pIsolate)); 344 v8::Context::New(m_isolate, nullptr, GetGlobalObjectTemplate(m_isolate));
353 v8::Context::Scope context_scope(v8Context); 345 v8::Context::Scope context_scope(v8Context);
354 346
355 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate); 347 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(m_isolate);
356 if (!pData) 348 if (!pData)
357 return; 349 return;
358 pData->CreateDynamicObjsMap(pIsolate); 350 pData->CreateDynamicObjsMap(m_isolate);
359 v8Context->SetAlignedPointerInEmbedderData(kPerContextDataIndex, pEngine); 351 v8Context->SetAlignedPointerInEmbedderData(kPerContextDataIndex, this);
360 352
361 int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); 353 int maxID = CFXJS_ObjDefinition::MaxID(m_isolate);
362 pStaticObjects->resize(maxID + 1); 354 m_StaticObjects.resize(maxID + 1);
363 for (int i = 0; i < maxID; ++i) { 355 for (int i = 0; i < maxID; ++i) {
364 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i); 356 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(m_isolate, i);
365 if (pObjDef->m_ObjType == FXJSOBJTYPE_GLOBAL) { 357 if (pObjDef->m_ObjType == FXJSOBJTYPE_GLOBAL) {
366 v8Context->Global() 358 v8Context->Global()
367 ->GetPrototype() 359 ->GetPrototype()
368 ->ToObject(v8Context) 360 ->ToObject(v8Context)
369 .ToLocalChecked() 361 .ToLocalChecked()
370 ->SetAlignedPointerInInternalField(0, new CFXJS_PerObjectData(i)); 362 ->SetAlignedPointerInInternalField(0, new CFXJS_PerObjectData(i));
371 363
372 if (pObjDef->m_pConstructor) 364 if (pObjDef->m_pConstructor)
373 pObjDef->m_pConstructor(pEngine, v8Context->Global() 365 pObjDef->m_pConstructor(this, v8Context->Global()
374 ->GetPrototype() 366 ->GetPrototype()
375 ->ToObject(v8Context) 367 ->ToObject(v8Context)
376 .ToLocalChecked()); 368 .ToLocalChecked());
377 } else if (pObjDef->m_ObjType == FXJSOBJTYPE_STATIC) { 369 } else if (pObjDef->m_ObjType == FXJSOBJTYPE_STATIC) {
378 CFX_ByteString bs = CFX_WideString(pObjDef->m_ObjName).UTF8Encode(); 370 CFX_ByteString bs = CFX_WideString(pObjDef->m_ObjName).UTF8Encode();
379 v8::Local<v8::String> m_ObjName = 371 v8::Local<v8::String> m_ObjName =
380 v8::String::NewFromUtf8(pIsolate, bs.c_str(), 372 v8::String::NewFromUtf8(m_isolate, bs.c_str(),
381 v8::NewStringType::kNormal, bs.GetLength()) 373 v8::NewStringType::kNormal, bs.GetLength())
382 .ToLocalChecked(); 374 .ToLocalChecked();
383 375
384 v8::Local<v8::Object> obj = 376 v8::Local<v8::Object> obj = NewFxDynamicObj(i, true);
385 FXJS_NewFxDynamicObj(pIsolate, pEngine, i, true);
386 v8Context->Global()->Set(v8Context, m_ObjName, obj).FromJust(); 377 v8Context->Global()->Set(v8Context, m_ObjName, obj).FromJust();
387 pStaticObjects->at(i) = new v8::Global<v8::Object>(pIsolate, obj); 378 m_StaticObjects[i] = new v8::Global<v8::Object>(m_isolate, obj);
388 } 379 }
389 } 380 }
390 pV8PersistentContext->Reset(pIsolate, v8Context); 381 m_V8PersistentContext.Reset(m_isolate, v8Context);
391 } 382 }
392 383
393 void FXJS_ReleaseEngine(v8::Isolate* pIsolate, 384 void CFXJS_Engine::ReleaseEngine() {
394 v8::Global<v8::Context>* pV8PersistentContext, 385 v8::Isolate::Scope isolate_scope(m_isolate);
395 std::vector<v8::Global<v8::Object>*>* pStaticObjects) { 386 v8::HandleScope handle_scope(m_isolate);
396 v8::Isolate::Scope isolate_scope(pIsolate);
397 v8::HandleScope handle_scope(pIsolate);
398 v8::Local<v8::Context> context = 387 v8::Local<v8::Context> context =
399 v8::Local<v8::Context>::New(pIsolate, *pV8PersistentContext); 388 v8::Local<v8::Context>::New(m_isolate, m_V8PersistentContext);
400 v8::Context::Scope context_scope(context); 389 v8::Context::Scope context_scope(context);
401 390
402 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate); 391 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(m_isolate);
403 if (!pData) 392 if (!pData)
404 return; 393 return;
405 394
406 int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); 395 int maxID = CFXJS_ObjDefinition::MaxID(m_isolate);
407 for (int i = 0; i < maxID; ++i) { 396 for (int i = 0; i < maxID; ++i) {
408 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i); 397 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(m_isolate, i);
409 v8::Local<v8::Object> pObj; 398 v8::Local<v8::Object> pObj;
410 if (pObjDef->m_ObjType == FXJSOBJTYPE_GLOBAL) { 399 if (pObjDef->m_ObjType == FXJSOBJTYPE_GLOBAL) {
411 pObj = 400 pObj =
412 context->Global()->GetPrototype()->ToObject(context).ToLocalChecked(); 401 context->Global()->GetPrototype()->ToObject(context).ToLocalChecked();
413 } else if (pStaticObjects->at(i) && !pStaticObjects->at(i)->IsEmpty()) { 402 } else if (m_StaticObjects[i] && !m_StaticObjects[i]->IsEmpty()) {
414 pObj = v8::Local<v8::Object>::New(pIsolate, *pStaticObjects->at(i)); 403 pObj = v8::Local<v8::Object>::New(m_isolate, *m_StaticObjects[i]);
415 delete pStaticObjects->at(i); 404 delete m_StaticObjects[i];
416 pStaticObjects->at(i) = nullptr; 405 m_StaticObjects[i] = nullptr;
417 } 406 }
418 407
419 if (!pObj.IsEmpty()) { 408 if (!pObj.IsEmpty()) {
420 if (pObjDef->m_pDestructor) 409 if (pObjDef->m_pDestructor)
421 pObjDef->m_pDestructor(pObj); 410 pObjDef->m_pDestructor(this, pObj);
422 FXJS_FreePrivate(pObj); 411 FreeObjectPrivate(pObj);
423 } 412 }
424 } 413 }
425 414
426 if (pIsolate == g_isolate && --g_isolate_ref_count > 0) 415 if (m_isolate == g_isolate && --g_isolate_ref_count > 0)
427 return; 416 return;
428 417
429 pData->ReleaseDynamicObjsMap(); 418 pData->ReleaseDynamicObjsMap();
430 for (int i = 0; i < maxID; ++i) 419 for (int i = 0; i < maxID; ++i)
431 delete CFXJS_ObjDefinition::ForID(pIsolate, i); 420 delete CFXJS_ObjDefinition::ForID(m_isolate, i);
432 421
433 pIsolate->SetData(g_embedderDataSlot, nullptr); 422 m_isolate->SetData(g_embedderDataSlot, nullptr);
434 delete pData; 423 delete pData;
435 } 424 }
436 425
437 CFXJS_Engine* FXJS_GetCurrentEngineFromIsolate(v8::Isolate* pIsolate) { 426 // static
dsinclair 2016/08/15 20:38:37 statics at the top
Tom Sepez 2016/08/15 20:46:15 Done.
438 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); 427 CFXJS_Engine* CFXJS_Engine::CurrentEngineFromIsolate(v8::Isolate* pIsolate) {
439 return static_cast<CFXJS_Engine*>( 428 return static_cast<CFXJS_Engine*>(
440 context->GetAlignedPointerFromEmbedderData(kPerContextDataIndex)); 429 pIsolate->GetCurrentContext()->GetAlignedPointerFromEmbedderData(
430 kPerContextDataIndex));
441 } 431 }
442 432
443 #ifdef PDF_ENABLE_XFA 433 #ifdef PDF_ENABLE_XFA
444 void FXJS_SetEngineForV8Context(v8::Local<v8::Context> v8Context, 434 // static
445 CFXJS_Engine* pEngine) { 435 void CFXJS_Engine::SetForV8Context(v8::Local<v8::Context> v8Context,
436 CFXJS_Engine* pEngine) {
446 v8Context->SetAlignedPointerInEmbedderData(kPerContextDataIndex, pEngine); 437 v8Context->SetAlignedPointerInEmbedderData(kPerContextDataIndex, pEngine);
447 } 438 }
448 #endif // PDF_ENABLE_XFA 439 #endif // PDF_ENABLE_XFA
449 440
450 int FXJS_Execute(v8::Isolate* pIsolate, 441 int CFXJS_Engine::Execute(const CFX_WideString& script, FXJSErr* pError) {
451 const CFX_WideString& script, 442 v8::Isolate::Scope isolate_scope(m_isolate);
452 FXJSErr* pError) { 443 v8::TryCatch try_catch(m_isolate);
453 v8::Isolate::Scope isolate_scope(pIsolate);
454 v8::TryCatch try_catch(pIsolate);
455 CFX_ByteString bsScript = script.UTF8Encode(); 444 CFX_ByteString bsScript = script.UTF8Encode();
456 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); 445 v8::Local<v8::Context> context = m_isolate->GetCurrentContext();
457 v8::Local<v8::Script> compiled_script; 446 v8::Local<v8::Script> compiled_script;
458 if (!v8::Script::Compile(context, 447 if (!v8::Script::Compile(context,
459 v8::String::NewFromUtf8(pIsolate, bsScript.c_str(), 448 v8::String::NewFromUtf8(m_isolate, bsScript.c_str(),
460 v8::NewStringType::kNormal, 449 v8::NewStringType::kNormal,
461 bsScript.GetLength()) 450 bsScript.GetLength())
462 .ToLocalChecked()) 451 .ToLocalChecked())
463 .ToLocal(&compiled_script)) { 452 .ToLocal(&compiled_script)) {
464 v8::String::Utf8Value error(try_catch.Exception()); 453 v8::String::Utf8Value error(try_catch.Exception());
465 // TODO(tsepez): return error via pError->message. 454 // TODO(tsepez): return error via pError->message.
466 return -1; 455 return -1;
467 } 456 }
468 457
469 v8::Local<v8::Value> result; 458 v8::Local<v8::Value> result;
470 if (!compiled_script->Run(context).ToLocal(&result)) { 459 if (!compiled_script->Run(context).ToLocal(&result)) {
471 v8::String::Utf8Value error(try_catch.Exception()); 460 v8::String::Utf8Value error(try_catch.Exception());
472 // TODO(tsepez): return error via pError->message. 461 // TODO(tsepez): return error via pError->message.
473 return -1; 462 return -1;
474 } 463 }
475 return 0; 464 return 0;
476 } 465 }
477 466
478 v8::Local<v8::Object> FXJS_NewFxDynamicObj(v8::Isolate* pIsolate, 467 v8::Local<v8::Object> CFXJS_Engine::NewFxDynamicObj(int nObjDefnID,
479 CFXJS_Engine* pEngine, 468 bool bStatic) {
480 int nObjDefnID, 469 v8::Isolate::Scope isolate_scope(m_isolate);
481 bool bStatic) { 470 v8::Local<v8::Context> context = m_isolate->GetCurrentContext();
482 v8::Isolate::Scope isolate_scope(pIsolate);
483 v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
484 if (nObjDefnID == -1) { 471 if (nObjDefnID == -1) {
485 v8::Local<v8::ObjectTemplate> objTempl = v8::ObjectTemplate::New(pIsolate); 472 v8::Local<v8::ObjectTemplate> objTempl = v8::ObjectTemplate::New(m_isolate);
486 v8::Local<v8::Object> obj; 473 v8::Local<v8::Object> obj;
487 if (!objTempl->NewInstance(context).ToLocal(&obj)) 474 if (!objTempl->NewInstance(context).ToLocal(&obj))
488 return v8::Local<v8::Object>(); 475 return v8::Local<v8::Object>();
489 return obj; 476 return obj;
490 } 477 }
491 478
492 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate); 479 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(m_isolate);
493 if (!pData) 480 if (!pData)
494 return v8::Local<v8::Object>(); 481 return v8::Local<v8::Object>();
495 482
496 if (nObjDefnID < 0 || nObjDefnID >= CFXJS_ObjDefinition::MaxID(pIsolate)) 483 if (nObjDefnID < 0 || nObjDefnID >= CFXJS_ObjDefinition::MaxID(m_isolate))
497 return v8::Local<v8::Object>(); 484 return v8::Local<v8::Object>();
498 485
499 CFXJS_ObjDefinition* pObjDef = 486 CFXJS_ObjDefinition* pObjDef =
500 CFXJS_ObjDefinition::ForID(pIsolate, nObjDefnID); 487 CFXJS_ObjDefinition::ForID(m_isolate, nObjDefnID);
501 v8::Local<v8::Object> obj; 488 v8::Local<v8::Object> obj;
502 if (!pObjDef->GetInstanceTemplate()->NewInstance(context).ToLocal(&obj)) 489 if (!pObjDef->GetInstanceTemplate()->NewInstance(context).ToLocal(&obj))
503 return v8::Local<v8::Object>(); 490 return v8::Local<v8::Object>();
504 491
505 CFXJS_PerObjectData* pPerObjData = new CFXJS_PerObjectData(nObjDefnID); 492 CFXJS_PerObjectData* pPerObjData = new CFXJS_PerObjectData(nObjDefnID);
506 obj->SetAlignedPointerInInternalField(0, pPerObjData); 493 obj->SetAlignedPointerInInternalField(0, pPerObjData);
507 if (pObjDef->m_pConstructor) 494 if (pObjDef->m_pConstructor)
508 pObjDef->m_pConstructor(pEngine, obj); 495 pObjDef->m_pConstructor(this, obj);
509 496
510 if (!bStatic && FXJS_PerIsolateData::Get(pIsolate)->m_pDynamicObjsMap) { 497 if (!bStatic && FXJS_PerIsolateData::Get(m_isolate)->m_pDynamicObjsMap) {
511 FXJS_PerIsolateData::Get(pIsolate)->m_pDynamicObjsMap->set(pPerObjData, 498 FXJS_PerIsolateData::Get(m_isolate)->m_pDynamicObjsMap->set(pPerObjData,
512 obj); 499 obj);
513 } 500 }
514 return obj; 501 return obj;
515 } 502 }
516 503
517 v8::Local<v8::Object> FXJS_GetThisObj(v8::Isolate* pIsolate) { 504 v8::Local<v8::Object> CFXJS_Engine::GetThisObj() {
518 v8::Isolate::Scope isolate_scope(pIsolate); 505 v8::Isolate::Scope isolate_scope(m_isolate);
519 if (!FXJS_PerIsolateData::Get(pIsolate)) 506 if (!FXJS_PerIsolateData::Get(m_isolate))
520 return v8::Local<v8::Object>(); 507 return v8::Local<v8::Object>();
521 508
522 // Return the global object. 509 // Return the global object.
523 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); 510 v8::Local<v8::Context> context = m_isolate->GetCurrentContext();
524 return context->Global()->GetPrototype()->ToObject(context).ToLocalChecked(); 511 return context->Global()->GetPrototype()->ToObject(context).ToLocalChecked();
525 } 512 }
526 513
527 int FXJS_GetObjDefnID(v8::Local<v8::Object> pObj) { 514 // static
dsinclair 2016/08/15 20:38:37 ditto
515 int CFXJS_Engine::GetObjDefnID(v8::Local<v8::Object> pObj) {
528 if (pObj.IsEmpty() || !pObj->InternalFieldCount()) 516 if (pObj.IsEmpty() || !pObj->InternalFieldCount())
529 return -1; 517 return -1;
530 CFXJS_PerObjectData* pPerObjectData = static_cast<CFXJS_PerObjectData*>( 518 CFXJS_PerObjectData* pPerObjectData = static_cast<CFXJS_PerObjectData*>(
531 pObj->GetAlignedPointerFromInternalField(0)); 519 pObj->GetAlignedPointerFromInternalField(0));
532 if (pPerObjectData) 520 if (!pPerObjectData)
533 return pPerObjectData->m_ObjDefID; 521 return -1;
534 return -1; 522 return pPerObjectData->m_ObjDefID;
535 } 523 }
536 524
537 void FXJS_Error(v8::Isolate* pIsolate, const CFX_WideString& message) { 525 void CFXJS_Engine::Error(const CFX_WideString& message) {
538 // Conversion from pdfium's wchar_t wide-strings to v8's uint16_t 526 // Conversion from pdfium's wchar_t wide-strings to v8's uint16_t
539 // wide-strings isn't handled by v8, so use UTF8 as a common 527 // wide-strings isn't handled by v8, so use UTF8 as a common
540 // intermediate format. 528 // intermediate format.
541 CFX_ByteString utf8_message = message.UTF8Encode(); 529 CFX_ByteString utf8_message = message.UTF8Encode();
542 pIsolate->ThrowException(v8::String::NewFromUtf8(pIsolate, 530 m_isolate->ThrowException(v8::String::NewFromUtf8(m_isolate,
543 utf8_message.c_str(), 531 utf8_message.c_str(),
544 v8::NewStringType::kNormal) 532 v8::NewStringType::kNormal)
545 .ToLocalChecked()); 533 .ToLocalChecked());
546 } 534 }
547 535
548 void FXJS_SetPrivate(v8::Isolate* pIsolate, 536 void CFXJS_Engine::SetObjectPrivate(v8::Local<v8::Object> pObj, void* p) {
549 v8::Local<v8::Object> pObj,
550 void* p) {
551 if (pObj.IsEmpty() || !pObj->InternalFieldCount()) 537 if (pObj.IsEmpty() || !pObj->InternalFieldCount())
552 return; 538 return;
553 CFXJS_PerObjectData* pPerObjectData = static_cast<CFXJS_PerObjectData*>( 539 CFXJS_PerObjectData* pPerObjectData = static_cast<CFXJS_PerObjectData*>(
554 pObj->GetAlignedPointerFromInternalField(0)); 540 pObj->GetAlignedPointerFromInternalField(0));
555 if (!pPerObjectData) 541 if (!pPerObjectData)
556 return; 542 return;
557 pPerObjectData->m_pPrivate = p; 543 pPerObjectData->m_pPrivate = p;
558 } 544 }
559 545
560 void* FXJS_GetPrivate(v8::Isolate* pIsolate, v8::Local<v8::Object> pObj) { 546 void* CFXJS_Engine::GetObjectPrivate(v8::Local<v8::Object> pObj) {
561 if (pObj.IsEmpty()) 547 if (pObj.IsEmpty())
562 return nullptr; 548 return nullptr;
563 CFXJS_PerObjectData* pPerObjectData = nullptr; 549 CFXJS_PerObjectData* pPerObjectData = nullptr;
564 if (pObj->InternalFieldCount()) { 550 if (pObj->InternalFieldCount()) {
565 pPerObjectData = static_cast<CFXJS_PerObjectData*>( 551 pPerObjectData = static_cast<CFXJS_PerObjectData*>(
566 pObj->GetAlignedPointerFromInternalField(0)); 552 pObj->GetAlignedPointerFromInternalField(0));
567 } else { 553 } else {
568 // It could be a global proxy object. 554 // It could be a global proxy object.
569 v8::Local<v8::Value> v = pObj->GetPrototype(); 555 v8::Local<v8::Value> v = pObj->GetPrototype();
570 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); 556 v8::Local<v8::Context> context = m_isolate->GetCurrentContext();
571 if (v->IsObject()) { 557 if (v->IsObject()) {
572 pPerObjectData = static_cast<CFXJS_PerObjectData*>( 558 pPerObjectData = static_cast<CFXJS_PerObjectData*>(
573 v->ToObject(context) 559 v->ToObject(context)
574 .ToLocalChecked() 560 .ToLocalChecked()
575 ->GetAlignedPointerFromInternalField(0)); 561 ->GetAlignedPointerFromInternalField(0));
576 } 562 }
577 } 563 }
578 return pPerObjectData ? pPerObjectData->m_pPrivate : nullptr; 564 return pPerObjectData ? pPerObjectData->m_pPrivate : nullptr;
579 } 565 }
580 566
581 void FXJS_FreePrivate(void* pPerObjectData) { 567 // static
dsinclair 2016/08/15 20:38:37 ...
Tom Sepez 2016/08/15 20:46:15 Done.
568 void CFXJS_Engine::FreeObjectPrivate(void* pPerObjectData) {
582 delete static_cast<CFXJS_PerObjectData*>(pPerObjectData); 569 delete static_cast<CFXJS_PerObjectData*>(pPerObjectData);
583 } 570 }
584 571
585 void FXJS_FreePrivate(v8::Local<v8::Object> pObj) { 572 // static
573 void CFXJS_Engine::FreeObjectPrivate(v8::Local<v8::Object> pObj) {
586 if (pObj.IsEmpty() || !pObj->InternalFieldCount()) 574 if (pObj.IsEmpty() || !pObj->InternalFieldCount())
587 return; 575 return;
588 FXJS_FreePrivate(pObj->GetAlignedPointerFromInternalField(0)); 576 FreeObjectPrivate(pObj->GetAlignedPointerFromInternalField(0));
589 pObj->SetAlignedPointerInInternalField(0, nullptr); 577 pObj->SetAlignedPointerInInternalField(0, nullptr);
590 } 578 }
591 579
592 v8::Local<v8::String> FXJS_WSToJSString(v8::Isolate* pIsolate, 580 v8::Local<v8::String> CFXJS_Engine::WSToJSString(
593 const CFX_WideString& wsPropertyName) { 581 const CFX_WideString& wsPropertyName) {
582 v8::Isolate* pIsolate = m_isolate ? m_isolate : v8::Isolate::GetCurrent();
594 CFX_ByteString bs = wsPropertyName.UTF8Encode(); 583 CFX_ByteString bs = wsPropertyName.UTF8Encode();
595 if (!pIsolate)
596 pIsolate = v8::Isolate::GetCurrent();
597 return v8::String::NewFromUtf8(pIsolate, bs.c_str(), 584 return v8::String::NewFromUtf8(pIsolate, bs.c_str(),
598 v8::NewStringType::kNormal, bs.GetLength()) 585 v8::NewStringType::kNormal, bs.GetLength())
599 .ToLocalChecked(); 586 .ToLocalChecked();
600 } 587 }
601 588
602 v8::Local<v8::Value> FXJS_GetObjectProperty( 589 v8::Local<v8::Value> CFXJS_Engine::GetObjectProperty(
603 v8::Isolate* pIsolate,
604 v8::Local<v8::Object> pObj, 590 v8::Local<v8::Object> pObj,
605 const CFX_WideString& wsPropertyName) { 591 const CFX_WideString& wsPropertyName) {
606 if (pObj.IsEmpty()) 592 if (pObj.IsEmpty())
607 return v8::Local<v8::Value>(); 593 return v8::Local<v8::Value>();
608 v8::Local<v8::Value> val; 594 v8::Local<v8::Value> val;
609 if (!pObj->Get(pIsolate->GetCurrentContext(), 595 if (!pObj->Get(m_isolate->GetCurrentContext(), WSToJSString(wsPropertyName))
610 FXJS_WSToJSString(pIsolate, wsPropertyName))
611 .ToLocal(&val)) 596 .ToLocal(&val))
612 return v8::Local<v8::Value>(); 597 return v8::Local<v8::Value>();
613 return val; 598 return val;
614 } 599 }
615 600
616 std::vector<CFX_WideString> FXJS_GetObjectPropertyNames( 601 std::vector<CFX_WideString> CFXJS_Engine::GetObjectPropertyNames(
617 v8::Isolate* pIsolate,
618 v8::Local<v8::Object> pObj) { 602 v8::Local<v8::Object> pObj) {
619 if (pObj.IsEmpty()) 603 if (pObj.IsEmpty())
620 return std::vector<CFX_WideString>(); 604 return std::vector<CFX_WideString>();
621 605
622 v8::Local<v8::Array> val; 606 v8::Local<v8::Array> val;
623 if (!pObj->GetPropertyNames(pIsolate->GetCurrentContext()).ToLocal(&val)) 607 v8::Local<v8::Context> context = m_isolate->GetCurrentContext();
608 if (!pObj->GetPropertyNames(context).ToLocal(&val))
624 return std::vector<CFX_WideString>(); 609 return std::vector<CFX_WideString>();
625 610
626 std::vector<CFX_WideString> result; 611 std::vector<CFX_WideString> result;
627 for (uint32_t i = 0; i < val->Length(); ++i) { 612 for (uint32_t i = 0; i < val->Length(); ++i) {
628 result.push_back(FXJS_ToString( 613 result.push_back(ToString(val->Get(context, i).ToLocalChecked()));
629 pIsolate, val->Get(pIsolate->GetCurrentContext(), i).ToLocalChecked()));
630 } 614 }
631 615
632 return result; 616 return result;
633 } 617 }
634 618
635 void FXJS_PutObjectString(v8::Isolate* pIsolate, 619 void CFXJS_Engine::PutObjectString(v8::Local<v8::Object> pObj,
636 v8::Local<v8::Object> pObj, 620 const CFX_WideString& wsPropertyName,
637 const CFX_WideString& wsPropertyName, 621 const CFX_WideString& wsValue) {
638 const CFX_WideString& wsValue) { 622 if (pObj.IsEmpty())
639 if (pObj.IsEmpty()) 623 return;
640 return; 624 pObj->Set(m_isolate->GetCurrentContext(), WSToJSString(wsPropertyName),
641 pObj->Set(pIsolate->GetCurrentContext(), 625 WSToJSString(wsValue))
642 FXJS_WSToJSString(pIsolate, wsPropertyName), 626 .FromJust();
643 FXJS_WSToJSString(pIsolate, wsValue)) 627 }
644 .FromJust(); 628
645 } 629 void CFXJS_Engine::PutObjectNumber(v8::Local<v8::Object> pObj,
646 630 const CFX_WideString& wsPropertyName,
647 void FXJS_PutObjectNumber(v8::Isolate* pIsolate, 631 int nValue) {
648 v8::Local<v8::Object> pObj, 632 if (pObj.IsEmpty())
649 const CFX_WideString& wsPropertyName, 633 return;
650 int nValue) { 634 pObj->Set(m_isolate->GetCurrentContext(), WSToJSString(wsPropertyName),
651 if (pObj.IsEmpty()) 635 v8::Int32::New(m_isolate, nValue))
652 return; 636 .FromJust();
653 pObj->Set(pIsolate->GetCurrentContext(), 637 }
654 FXJS_WSToJSString(pIsolate, wsPropertyName), 638
655 v8::Int32::New(pIsolate, nValue)) 639 void CFXJS_Engine::PutObjectNumber(v8::Local<v8::Object> pObj,
656 .FromJust(); 640 const CFX_WideString& wsPropertyName,
657 } 641 float fValue) {
658 642 if (pObj.IsEmpty())
659 void FXJS_PutObjectNumber(v8::Isolate* pIsolate, 643 return;
660 v8::Local<v8::Object> pObj, 644 pObj->Set(m_isolate->GetCurrentContext(), WSToJSString(wsPropertyName),
661 const CFX_WideString& wsPropertyName, 645 v8::Number::New(m_isolate, (double)fValue))
662 float fValue) { 646 .FromJust();
663 if (pObj.IsEmpty()) 647 }
664 return; 648
665 pObj->Set(pIsolate->GetCurrentContext(), 649 void CFXJS_Engine::PutObjectNumber(v8::Local<v8::Object> pObj,
666 FXJS_WSToJSString(pIsolate, wsPropertyName), 650 const CFX_WideString& wsPropertyName,
667 v8::Number::New(pIsolate, (double)fValue)) 651 double dValue) {
668 .FromJust(); 652 if (pObj.IsEmpty())
669 } 653 return;
670 654 pObj->Set(m_isolate->GetCurrentContext(), WSToJSString(wsPropertyName),
671 void FXJS_PutObjectNumber(v8::Isolate* pIsolate, 655 v8::Number::New(m_isolate, (double)dValue))
672 v8::Local<v8::Object> pObj, 656 .FromJust();
673 const CFX_WideString& wsPropertyName, 657 }
674 double dValue) { 658
675 if (pObj.IsEmpty()) 659 void CFXJS_Engine::PutObjectBoolean(v8::Local<v8::Object> pObj,
676 return; 660 const CFX_WideString& wsPropertyName,
677 pObj->Set(pIsolate->GetCurrentContext(), 661 bool bValue) {
678 FXJS_WSToJSString(pIsolate, wsPropertyName), 662 if (pObj.IsEmpty())
679 v8::Number::New(pIsolate, (double)dValue)) 663 return;
680 .FromJust(); 664 pObj->Set(m_isolate->GetCurrentContext(), WSToJSString(wsPropertyName),
681 } 665 v8::Boolean::New(m_isolate, bValue))
682 666 .FromJust();
683 void FXJS_PutObjectBoolean(v8::Isolate* pIsolate, 667 }
684 v8::Local<v8::Object> pObj, 668
685 const CFX_WideString& wsPropertyName, 669 void CFXJS_Engine::PutObjectObject(v8::Local<v8::Object> pObj,
686 bool bValue) { 670 const CFX_WideString& wsPropertyName,
687 if (pObj.IsEmpty()) 671 v8::Local<v8::Object> pPut) {
688 return; 672 if (pObj.IsEmpty())
689 pObj->Set(pIsolate->GetCurrentContext(), 673 return;
690 FXJS_WSToJSString(pIsolate, wsPropertyName), 674 pObj->Set(m_isolate->GetCurrentContext(), WSToJSString(wsPropertyName), pPut)
691 v8::Boolean::New(pIsolate, bValue)) 675 .FromJust();
692 .FromJust(); 676 }
693 } 677
694 678 void CFXJS_Engine::PutObjectNull(v8::Local<v8::Object> pObj,
695 void FXJS_PutObjectObject(v8::Isolate* pIsolate, 679 const CFX_WideString& wsPropertyName) {
696 v8::Local<v8::Object> pObj, 680 if (pObj.IsEmpty())
697 const CFX_WideString& wsPropertyName, 681 return;
698 v8::Local<v8::Object> pPut) { 682 pObj->Set(m_isolate->GetCurrentContext(), WSToJSString(wsPropertyName),
699 if (pObj.IsEmpty())
700 return;
701 pObj->Set(pIsolate->GetCurrentContext(),
702 FXJS_WSToJSString(pIsolate, wsPropertyName), pPut)
703 .FromJust();
704 }
705
706 void FXJS_PutObjectNull(v8::Isolate* pIsolate,
707 v8::Local<v8::Object> pObj,
708 const CFX_WideString& wsPropertyName) {
709 if (pObj.IsEmpty())
710 return;
711 pObj->Set(pIsolate->GetCurrentContext(),
712 FXJS_WSToJSString(pIsolate, wsPropertyName),
713 v8::Local<v8::Object>()) 683 v8::Local<v8::Object>())
714 .FromJust(); 684 .FromJust();
715 } 685 }
716 686
717 v8::Local<v8::Array> FXJS_NewArray(v8::Isolate* pIsolate) { 687 v8::Local<v8::Array> CFXJS_Engine::NewArray() {
718 return v8::Array::New(pIsolate); 688 return v8::Array::New(m_isolate);
719 } 689 }
720 690
721 unsigned FXJS_PutArrayElement(v8::Isolate* pIsolate, 691 unsigned CFXJS_Engine::PutArrayElement(v8::Local<v8::Array> pArray,
722 v8::Local<v8::Array> pArray, 692 unsigned index,
723 unsigned index, 693 v8::Local<v8::Value> pValue) {
724 v8::Local<v8::Value> pValue) {
725 if (pArray.IsEmpty()) 694 if (pArray.IsEmpty())
726 return 0; 695 return 0;
727 if (pArray->Set(pIsolate->GetCurrentContext(), index, pValue).IsNothing()) 696 if (pArray->Set(m_isolate->GetCurrentContext(), index, pValue).IsNothing())
728 return 0; 697 return 0;
729 return 1; 698 return 1;
730 } 699 }
731 700
732 v8::Local<v8::Value> FXJS_GetArrayElement(v8::Isolate* pIsolate, 701 v8::Local<v8::Value> CFXJS_Engine::GetArrayElement(v8::Local<v8::Array> pArray,
733 v8::Local<v8::Array> pArray, 702 unsigned index) {
734 unsigned index) {
735 if (pArray.IsEmpty()) 703 if (pArray.IsEmpty())
736 return v8::Local<v8::Value>(); 704 return v8::Local<v8::Value>();
737 v8::Local<v8::Value> val; 705 v8::Local<v8::Value> val;
738 if (!pArray->Get(pIsolate->GetCurrentContext(), index).ToLocal(&val)) 706 if (!pArray->Get(m_isolate->GetCurrentContext(), index).ToLocal(&val))
739 return v8::Local<v8::Value>(); 707 return v8::Local<v8::Value>();
740 return val; 708 return val;
741 } 709 }
742 710
743 unsigned FXJS_GetArrayLength(v8::Local<v8::Array> pArray) { 711 unsigned CFXJS_Engine::GetArrayLength(v8::Local<v8::Array> pArray) {
744 if (pArray.IsEmpty()) 712 if (pArray.IsEmpty())
745 return 0; 713 return 0;
746 return pArray->Length(); 714 return pArray->Length();
747 } 715 }
748 716
749 v8::Local<v8::Value> FXJS_NewNumber(v8::Isolate* pIsolate, int number) { 717 v8::Local<v8::Context> CFXJS_Engine::NewLocalContext() {
750 return v8::Int32::New(pIsolate, number); 718 return v8::Local<v8::Context>::New(m_isolate, m_V8PersistentContext);
751 } 719 }
752 720
753 v8::Local<v8::Value> FXJS_NewNumber(v8::Isolate* pIsolate, double number) { 721 v8::Local<v8::Context> CFXJS_Engine::GetPersistentContext() {
754 return v8::Number::New(pIsolate, number); 722 return m_V8PersistentContext.Get(m_isolate);
755 } 723 }
756 724
757 v8::Local<v8::Value> FXJS_NewNumber(v8::Isolate* pIsolate, float number) { 725 v8::Local<v8::Value> CFXJS_Engine::NewNumber(int number) {
758 return v8::Number::New(pIsolate, (float)number); 726 return v8::Int32::New(m_isolate, number);
759 } 727 }
760 728
761 v8::Local<v8::Value> FXJS_NewBoolean(v8::Isolate* pIsolate, bool b) { 729 v8::Local<v8::Value> CFXJS_Engine::NewNumber(double number) {
762 return v8::Boolean::New(pIsolate, b); 730 return v8::Number::New(m_isolate, number);
763 } 731 }
764 732
765 v8::Local<v8::Value> FXJS_NewString(v8::Isolate* pIsolate, const wchar_t* str) { 733 v8::Local<v8::Value> CFXJS_Engine::NewNumber(float number) {
766 return FXJS_WSToJSString(pIsolate, str); 734 return v8::Number::New(m_isolate, (float)number);
767 } 735 }
768 736
769 v8::Local<v8::Value> FXJS_NewNull(v8::Isolate* pIsolate) { 737 v8::Local<v8::Value> CFXJS_Engine::NewBoolean(bool b) {
738 return v8::Boolean::New(m_isolate, b);
739 }
740
741 v8::Local<v8::Value> CFXJS_Engine::NewString(const wchar_t* str) {
742 return WSToJSString(str);
743 }
744
745 v8::Local<v8::Value> CFXJS_Engine::NewNull() {
770 return v8::Local<v8::Value>(); 746 return v8::Local<v8::Value>();
771 } 747 }
772 748
773 v8::Local<v8::Date> FXJS_NewDate(v8::Isolate* pIsolate, double d) { 749 v8::Local<v8::Date> CFXJS_Engine::NewDate(double d) {
774 return v8::Date::New(pIsolate->GetCurrentContext(), d) 750 return v8::Date::New(m_isolate->GetCurrentContext(), d)
775 .ToLocalChecked() 751 .ToLocalChecked()
776 .As<v8::Date>(); 752 .As<v8::Date>();
777 } 753 }
778 754
779 int FXJS_ToInt32(v8::Isolate* pIsolate, v8::Local<v8::Value> pValue) { 755 int CFXJS_Engine::ToInt32(v8::Local<v8::Value> pValue) {
780 if (pValue.IsEmpty()) 756 if (pValue.IsEmpty())
781 return 0; 757 return 0;
782 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); 758 v8::Local<v8::Context> context = m_isolate->GetCurrentContext();
783 return pValue->ToInt32(context).ToLocalChecked()->Value(); 759 return pValue->ToInt32(context).ToLocalChecked()->Value();
784 } 760 }
785 761
786 bool FXJS_ToBoolean(v8::Isolate* pIsolate, v8::Local<v8::Value> pValue) { 762 bool CFXJS_Engine::ToBoolean(v8::Local<v8::Value> pValue) {
787 if (pValue.IsEmpty()) 763 if (pValue.IsEmpty())
788 return false; 764 return false;
789 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); 765 v8::Local<v8::Context> context = m_isolate->GetCurrentContext();
790 return pValue->ToBoolean(context).ToLocalChecked()->Value(); 766 return pValue->ToBoolean(context).ToLocalChecked()->Value();
791 } 767 }
792 768
793 double FXJS_ToNumber(v8::Isolate* pIsolate, v8::Local<v8::Value> pValue) { 769 double CFXJS_Engine::ToNumber(v8::Local<v8::Value> pValue) {
794 if (pValue.IsEmpty()) 770 if (pValue.IsEmpty())
795 return 0.0; 771 return 0.0;
796 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); 772 v8::Local<v8::Context> context = m_isolate->GetCurrentContext();
797 return pValue->ToNumber(context).ToLocalChecked()->Value(); 773 return pValue->ToNumber(context).ToLocalChecked()->Value();
798 } 774 }
799 775
800 v8::Local<v8::Object> FXJS_ToObject(v8::Isolate* pIsolate, 776 v8::Local<v8::Object> CFXJS_Engine::ToObject(v8::Local<v8::Value> pValue) {
801 v8::Local<v8::Value> pValue) {
802 if (pValue.IsEmpty()) 777 if (pValue.IsEmpty())
803 return v8::Local<v8::Object>(); 778 return v8::Local<v8::Object>();
804 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); 779 v8::Local<v8::Context> context = m_isolate->GetCurrentContext();
805 return pValue->ToObject(context).ToLocalChecked(); 780 return pValue->ToObject(context).ToLocalChecked();
806 } 781 }
807 782
808 CFX_WideString FXJS_ToString(v8::Isolate* pIsolate, 783 CFX_WideString CFXJS_Engine::ToString(v8::Local<v8::Value> pValue) {
809 v8::Local<v8::Value> pValue) {
810 if (pValue.IsEmpty()) 784 if (pValue.IsEmpty())
811 return L""; 785 return L"";
812 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); 786 v8::Local<v8::Context> context = m_isolate->GetCurrentContext();
813 v8::String::Utf8Value s(pValue->ToString(context).ToLocalChecked()); 787 v8::String::Utf8Value s(pValue->ToString(context).ToLocalChecked());
814 return CFX_WideString::FromUTF8(CFX_ByteStringC(*s, s.length())); 788 return CFX_WideString::FromUTF8(CFX_ByteStringC(*s, s.length()));
815 } 789 }
816 790
817 v8::Local<v8::Array> FXJS_ToArray(v8::Isolate* pIsolate, 791 v8::Local<v8::Array> CFXJS_Engine::ToArray(v8::Local<v8::Value> pValue) {
818 v8::Local<v8::Value> pValue) {
819 if (pValue.IsEmpty()) 792 if (pValue.IsEmpty())
820 return v8::Local<v8::Array>(); 793 return v8::Local<v8::Array>();
821 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); 794 v8::Local<v8::Context> context = m_isolate->GetCurrentContext();
822 return v8::Local<v8::Array>::Cast(pValue->ToObject(context).ToLocalChecked()); 795 return v8::Local<v8::Array>::Cast(pValue->ToObject(context).ToLocalChecked());
823 } 796 }
797
798 void CFXJS_Engine::SetConstArray(const CFX_WideString& name,
799 v8::Local<v8::Array> array) {
800 m_ConstArrays[name] = v8::Global<v8::Array>(GetIsolate(), array);
801 }
802
803 v8::Local<v8::Array> CFXJS_Engine::GetConstArray(const CFX_WideString& name) {
804 return v8::Local<v8::Array>::New(GetIsolate(), m_ConstArrays[name]);
805 }
OLDNEW
« no previous file with comments | « fpdfsdk/javascript/util.cpp ('k') | fxjs/fxjs_v8_embeddertest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698