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

Side by Side Diff: fpdfsdk/src/jsapi/fxjs_v8.cpp

Issue 1389163007: Pass IJS_Runtime, not IJS_Context, to native object constructors. (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: comment Created 5 years, 2 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
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 "../../include/jsapi/fxjs_v8.h" 7 #include "../../include/jsapi/fxjs_v8.h"
8 8
9 #include "../../../core/include/fxcrt/fx_basic.h" 9 #include "../../../core/include/fxcrt/fx_basic.h"
10 10
(...skipping 255 matching lines...) Expand 10 before | Expand all | Expand 10 after
266 v8::Isolate::Scope isolate_scope(pIsolate); 266 v8::Isolate::Scope isolate_scope(pIsolate);
267 v8::HandleScope handle_scope(pIsolate); 267 v8::HandleScope handle_scope(pIsolate);
268 CFX_ByteString bsConst = CFX_WideString(sConstName).UTF8Encode(); 268 CFX_ByteString bsConst = CFX_WideString(sConstName).UTF8Encode();
269 GetGlobalObjectTemplate(pIsolate)->Set( 269 GetGlobalObjectTemplate(pIsolate)->Set(
270 v8::String::NewFromUtf8(pIsolate, bsConst.c_str(), 270 v8::String::NewFromUtf8(pIsolate, bsConst.c_str(),
271 v8::NewStringType::kNormal).ToLocalChecked(), 271 v8::NewStringType::kNormal).ToLocalChecked(),
272 pDefault, v8::ReadOnly); 272 pDefault, v8::ReadOnly);
273 } 273 }
274 274
275 void FXJS_InitializeRuntime(v8::Isolate* pIsolate, 275 void FXJS_InitializeRuntime(v8::Isolate* pIsolate,
276 IJS_Runtime* pFXRuntime, 276 IJS_Runtime* pIRuntime,
277 IJS_Context* context,
278 v8::Global<v8::Context>& v8PersistentContext) { 277 v8::Global<v8::Context>& v8PersistentContext) {
279 if (pIsolate == g_isolate) 278 if (pIsolate == g_isolate)
280 ++g_isolate_ref_count; 279 ++g_isolate_ref_count;
281 280
282 v8::Isolate::Scope isolate_scope(pIsolate); 281 v8::Isolate::Scope isolate_scope(pIsolate);
283 v8::HandleScope handle_scope(pIsolate); 282 v8::HandleScope handle_scope(pIsolate);
284 v8::Local<v8::Context> v8Context = 283 v8::Local<v8::Context> v8Context =
285 v8::Context::New(pIsolate, NULL, GetGlobalObjectTemplate(pIsolate)); 284 v8::Context::New(pIsolate, NULL, GetGlobalObjectTemplate(pIsolate));
286 v8::Context::Scope context_scope(v8Context); 285 v8::Context::Scope context_scope(v8Context);
287 286
288 FXJS_PerIsolateData::SetUp(pIsolate); 287 FXJS_PerIsolateData::SetUp(pIsolate);
289 v8Context->SetAlignedPointerInEmbedderData(kPerContextDataIndex, pFXRuntime); 288 v8Context->SetAlignedPointerInEmbedderData(kPerContextDataIndex, pIRuntime);
290 289
291 int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); 290 int maxID = CFXJS_ObjDefinition::MaxID(pIsolate);
292 for (int i = 0; i < maxID; ++i) { 291 for (int i = 0; i < maxID; ++i) {
293 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i); 292 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i);
294 CFX_ByteString bs = CFX_WideString(pObjDef->m_ObjName).UTF8Encode(); 293 CFX_ByteString bs = CFX_WideString(pObjDef->m_ObjName).UTF8Encode();
295 v8::Local<v8::String> m_ObjName = 294 v8::Local<v8::String> m_ObjName =
296 v8::String::NewFromUtf8(pIsolate, bs.c_str(), 295 v8::String::NewFromUtf8(pIsolate, bs.c_str(),
297 v8::NewStringType::kNormal, 296 v8::NewStringType::kNormal,
298 bs.GetLength()).ToLocalChecked(); 297 bs.GetLength()).ToLocalChecked();
299 298
300 if (pObjDef->m_ObjType == FXJSOBJTYPE_GLOBAL) { 299 if (pObjDef->m_ObjType == FXJSOBJTYPE_GLOBAL) {
301 v8Context->Global() 300 v8Context->Global()
302 ->GetPrototype() 301 ->GetPrototype()
303 ->ToObject(v8Context) 302 ->ToObject(v8Context)
304 .ToLocalChecked() 303 .ToLocalChecked()
305 ->SetAlignedPointerInInternalField(0, new CFXJS_PrivateData(i)); 304 ->SetAlignedPointerInInternalField(0, new CFXJS_PrivateData(i));
306 305
307 if (pObjDef->m_pConstructor) 306 if (pObjDef->m_pConstructor)
308 pObjDef->m_pConstructor(context, v8Context->Global() 307 pObjDef->m_pConstructor(pIRuntime, v8Context->Global()
309 ->GetPrototype() 308 ->GetPrototype()
310 ->ToObject(v8Context) 309 ->ToObject(v8Context)
311 .ToLocalChecked()); 310 .ToLocalChecked());
312 } else if (pObjDef->m_ObjType == FXJSOBJTYPE_STATIC) { 311 } else if (pObjDef->m_ObjType == FXJSOBJTYPE_STATIC) {
313 v8::Local<v8::Object> obj = FXJS_NewFxDynamicObj(pIsolate, context, i); 312 v8::Local<v8::Object> obj = FXJS_NewFxDynamicObj(pIsolate, pIRuntime, i);
314 v8Context->Global()->Set(v8Context, m_ObjName, obj).FromJust(); 313 v8Context->Global()->Set(v8Context, m_ObjName, obj).FromJust();
315 pObjDef->m_StaticObj.Reset(pIsolate, obj); 314 pObjDef->m_StaticObj.Reset(pIsolate, obj);
316 } 315 }
317 } 316 }
318 v8PersistentContext.Reset(pIsolate, v8Context); 317 v8PersistentContext.Reset(pIsolate, v8Context);
319 } 318 }
320 319
321 void FXJS_ReleaseRuntime(v8::Isolate* pIsolate, 320 void FXJS_ReleaseRuntime(v8::Isolate* pIsolate,
322 v8::Global<v8::Context>& v8PersistentContext) { 321 v8::Global<v8::Context>& v8PersistentContext) {
323 if (pIsolate == g_isolate && --g_isolate_ref_count > 0) 322 if (pIsolate == g_isolate && --g_isolate_ref_count > 0)
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
379 v8::Local<v8::Value> result; 378 v8::Local<v8::Value> result;
380 if (!compiled_script->Run(context).ToLocal(&result)) { 379 if (!compiled_script->Run(context).ToLocal(&result)) {
381 v8::String::Utf8Value error(try_catch.Exception()); 380 v8::String::Utf8Value error(try_catch.Exception());
382 // TODO(tsepez): return error via pError->message. 381 // TODO(tsepez): return error via pError->message.
383 return -1; 382 return -1;
384 } 383 }
385 return 0; 384 return 0;
386 } 385 }
387 386
388 v8::Local<v8::Object> FXJS_NewFxDynamicObj(v8::Isolate* pIsolate, 387 v8::Local<v8::Object> FXJS_NewFxDynamicObj(v8::Isolate* pIsolate,
389 IJS_Context* pJSContext, 388 IJS_Runtime* pIRuntime,
390 int nObjDefnID) { 389 int nObjDefnID) {
391 v8::Isolate::Scope isolate_scope(pIsolate); 390 v8::Isolate::Scope isolate_scope(pIsolate);
392 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); 391 v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
393 if (nObjDefnID == -1) { 392 if (nObjDefnID == -1) {
394 v8::Local<v8::ObjectTemplate> objTempl = v8::ObjectTemplate::New(pIsolate); 393 v8::Local<v8::ObjectTemplate> objTempl = v8::ObjectTemplate::New(pIsolate);
395 v8::Local<v8::Object> obj; 394 v8::Local<v8::Object> obj;
396 if (!objTempl->NewInstance(context).ToLocal(&obj)) 395 if (!objTempl->NewInstance(context).ToLocal(&obj))
397 return v8::Local<v8::Object>(); 396 return v8::Local<v8::Object>();
398 return obj; 397 return obj;
399 } 398 }
400 399
401 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate); 400 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate);
402 if (!pData) 401 if (!pData)
403 return v8::Local<v8::Object>(); 402 return v8::Local<v8::Object>();
404 403
405 if (nObjDefnID < 0 || nObjDefnID >= CFXJS_ObjDefinition::MaxID(pIsolate)) 404 if (nObjDefnID < 0 || nObjDefnID >= CFXJS_ObjDefinition::MaxID(pIsolate))
406 return v8::Local<v8::Object>(); 405 return v8::Local<v8::Object>();
407 406
408 CFXJS_ObjDefinition* pObjDef = 407 CFXJS_ObjDefinition* pObjDef =
409 CFXJS_ObjDefinition::ForID(pIsolate, nObjDefnID); 408 CFXJS_ObjDefinition::ForID(pIsolate, nObjDefnID);
410 v8::Local<v8::Object> obj; 409 v8::Local<v8::Object> obj;
411 if (!pObjDef->GetInstanceTemplate()->NewInstance(context).ToLocal(&obj)) 410 if (!pObjDef->GetInstanceTemplate()->NewInstance(context).ToLocal(&obj))
412 return v8::Local<v8::Object>(); 411 return v8::Local<v8::Object>();
413 412
414 obj->SetAlignedPointerInInternalField(0, new CFXJS_PrivateData(nObjDefnID)); 413 obj->SetAlignedPointerInInternalField(0, new CFXJS_PrivateData(nObjDefnID));
415 if (pObjDef->m_pConstructor) 414 if (pObjDef->m_pConstructor)
416 pObjDef->m_pConstructor(pJSContext, obj); 415 pObjDef->m_pConstructor(pIRuntime, obj);
417 416
418 return obj; 417 return obj;
419 } 418 }
420 419
421 v8::Local<v8::Object> FXJS_GetThisObj(v8::Isolate* pIsolate) { 420 v8::Local<v8::Object> FXJS_GetThisObj(v8::Isolate* pIsolate) {
422 v8::Isolate::Scope isolate_scope(pIsolate); 421 v8::Isolate::Scope isolate_scope(pIsolate);
423 if (!FXJS_PerIsolateData::Get(pIsolate)) 422 if (!FXJS_PerIsolateData::Get(pIsolate))
424 return v8::Local<v8::Object>(); 423 return v8::Local<v8::Object>();
425 424
426 // Return the global object. 425 // Return the global object.
(...skipping 321 matching lines...) Expand 10 before | Expand all | Expand 10 after
748 return v8::Local<v8::Array>(); 747 return v8::Local<v8::Array>();
749 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); 748 v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
750 return v8::Local<v8::Array>::Cast(pValue->ToObject(context).ToLocalChecked()); 749 return v8::Local<v8::Array>::Cast(pValue->ToObject(context).ToLocalChecked());
751 } 750 }
752 751
753 void FXJS_ValueCopy(v8::Local<v8::Value>& pTo, v8::Local<v8::Value> pFrom) { 752 void FXJS_ValueCopy(v8::Local<v8::Value>& pTo, v8::Local<v8::Value> pFrom) {
754 pTo = pFrom; 753 pTo = pFrom;
755 } 754 }
756 755
757 756
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698