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

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

Issue 1382263002: Store object definition ID in each js_class. (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: Remove lookup by name 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 "../../../core/include/fxcrt/fx_basic.h" 7 #include "../../../core/include/fxcrt/fx_basic.h"
8 #include "../../include/jsapi/fxjs_v8.h" 8 #include "../../include/jsapi/fxjs_v8.h"
9 9
10 const wchar_t kFXJSValueNameString[] = L"string"; 10 const wchar_t kFXJSValueNameString[] = L"string";
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
43 static CFXJS_ObjDefinition* ForID(v8::Isolate* pIsolate, int id) { 43 static CFXJS_ObjDefinition* ForID(v8::Isolate* pIsolate, int id) {
44 // Note: GetAt() halts if out-of-range even in release builds. 44 // Note: GetAt() halts if out-of-range even in release builds.
45 return static_cast<CFXJS_ObjDefinition*>( 45 return static_cast<CFXJS_ObjDefinition*>(
46 FXJS_PerIsolateData::Get(pIsolate)->m_ObjectDefnArray.GetAt(id)); 46 FXJS_PerIsolateData::Get(pIsolate)->m_ObjectDefnArray.GetAt(id));
47 } 47 }
48 CFXJS_ObjDefinition(v8::Isolate* isolate, 48 CFXJS_ObjDefinition(v8::Isolate* isolate,
49 const wchar_t* sObjName, 49 const wchar_t* sObjName,
50 FXJSOBJTYPE eObjType, 50 FXJSOBJTYPE eObjType,
51 FXJS_CONSTRUCTOR pConstructor, 51 FXJS_CONSTRUCTOR pConstructor,
52 FXJS_DESTRUCTOR pDestructor) 52 FXJS_DESTRUCTOR pDestructor)
53 : objName(sObjName), 53 : m_ObjName(sObjName),
54 objType(eObjType), 54 m_ObjType(eObjType),
55 m_pConstructor(pConstructor), 55 m_pConstructor(pConstructor),
56 m_pDestructor(pDestructor), 56 m_pDestructor(pDestructor),
57 m_bSetAsGlobalObject(FALSE),
58 m_pIsolate(isolate) { 57 m_pIsolate(isolate) {
59 v8::Isolate::Scope isolate_scope(isolate); 58 v8::Isolate::Scope isolate_scope(isolate);
60 v8::HandleScope handle_scope(isolate); 59 v8::HandleScope handle_scope(isolate);
61 60
62 v8::Local<v8::FunctionTemplate> fun = v8::FunctionTemplate::New(isolate); 61 v8::Local<v8::FunctionTemplate> fun = v8::FunctionTemplate::New(isolate);
63 fun->InstanceTemplate()->SetInternalFieldCount(2); 62 fun->InstanceTemplate()->SetInternalFieldCount(2);
64 m_FunctionTemplate.Reset(isolate, fun); 63 m_FunctionTemplate.Reset(isolate, fun);
65 64
66 v8::Local<v8::Signature> sig = v8::Signature::New(isolate, fun); 65 v8::Local<v8::Signature> sig = v8::Signature::New(isolate, fun);
67 m_Signature.Reset(isolate, sig); 66 m_Signature.Reset(isolate, sig);
68
69 // Document as the global object.
70 if (FXSYS_wcscmp(sObjName, L"Document") == 0) {
71 m_bSetAsGlobalObject = TRUE;
72 }
73 } 67 }
74 68
75 int AssignID() { 69 int AssignID() {
76 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(m_pIsolate); 70 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(m_pIsolate);
77 pData->m_ObjectDefnArray.Add(this); 71 pData->m_ObjectDefnArray.Add(this);
78 return pData->m_ObjectDefnArray.GetSize() - 1; 72 return pData->m_ObjectDefnArray.GetSize() - 1;
79 } 73 }
80 74
81 v8::Local<v8::ObjectTemplate> GetInstanceTemplate() { 75 v8::Local<v8::ObjectTemplate> GetInstanceTemplate() {
82 v8::EscapableHandleScope scope(m_pIsolate); 76 v8::EscapableHandleScope scope(m_pIsolate);
83 v8::Local<v8::FunctionTemplate> function = 77 v8::Local<v8::FunctionTemplate> function =
84 m_FunctionTemplate.Get(m_pIsolate); 78 m_FunctionTemplate.Get(m_pIsolate);
85 return scope.Escape(function->InstanceTemplate()); 79 return scope.Escape(function->InstanceTemplate());
86 } 80 }
87 81
88 v8::Local<v8::Signature> GetSignature() { 82 v8::Local<v8::Signature> GetSignature() {
89 v8::EscapableHandleScope scope(m_pIsolate); 83 v8::EscapableHandleScope scope(m_pIsolate);
90 return scope.Escape(m_Signature.Get(m_pIsolate)); 84 return scope.Escape(m_Signature.Get(m_pIsolate));
91 } 85 }
92 86
93 const wchar_t* objName; 87 const wchar_t* m_ObjName;
94 const FXJSOBJTYPE objType; 88 const FXJSOBJTYPE m_ObjType;
95 const FXJS_CONSTRUCTOR m_pConstructor; 89 const FXJS_CONSTRUCTOR m_pConstructor;
96 const FXJS_DESTRUCTOR m_pDestructor; 90 const FXJS_DESTRUCTOR m_pDestructor;
97 FX_BOOL m_bSetAsGlobalObject;
98 91
99 v8::Isolate* m_pIsolate; 92 v8::Isolate* m_pIsolate;
100 v8::Global<v8::FunctionTemplate> m_FunctionTemplate; 93 v8::Global<v8::FunctionTemplate> m_FunctionTemplate;
101 v8::Global<v8::Signature> m_Signature; 94 v8::Global<v8::Signature> m_Signature;
102 v8::Global<v8::Object> m_StaticObj; 95 v8::Global<v8::Object> m_StaticObj;
103 }; 96 };
104 97
105 static v8::Local<v8::ObjectTemplate> GetGlobalObjectTemplate( 98 static v8::Local<v8::ObjectTemplate> GetGlobalObjectTemplate(
106 v8::Isolate* pIsolate) { 99 v8::Isolate* pIsolate) {
107 int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); 100 int maxID = CFXJS_ObjDefinition::MaxID(pIsolate);
108 for (int i = 0; i < maxID; ++i) { 101 for (int i = 0; i < maxID; ++i) {
109 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i); 102 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i);
110 if (pObjDef->m_bSetAsGlobalObject) 103 if (pObjDef->m_ObjType == FXJSOBJTYPE_GLOBAL)
111 return pObjDef->GetInstanceTemplate(); 104 return pObjDef->GetInstanceTemplate();
112 } 105 }
113
114 if (!g_DefaultGlobalObjectTemplate) { 106 if (!g_DefaultGlobalObjectTemplate) {
115 g_DefaultGlobalObjectTemplate = new v8::Global<v8::ObjectTemplate>; 107 g_DefaultGlobalObjectTemplate = new v8::Global<v8::ObjectTemplate>;
116 g_DefaultGlobalObjectTemplate->Reset(pIsolate, 108 g_DefaultGlobalObjectTemplate->Reset(pIsolate,
117 v8::ObjectTemplate::New(pIsolate)); 109 v8::ObjectTemplate::New(pIsolate));
118 } 110 }
119 return g_DefaultGlobalObjectTemplate->Get(pIsolate); 111 return g_DefaultGlobalObjectTemplate->Get(pIsolate);
120 } 112 }
121 113
122 void* FXJS_ArrayBufferAllocator::Allocate(size_t length) { 114 void* FXJS_ArrayBufferAllocator::Allocate(size_t length) {
123 return calloc(1, length); 115 return calloc(1, length);
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
256 v8::Local<v8::Context> v8Context = 248 v8::Local<v8::Context> v8Context =
257 v8::Context::New(pIsolate, NULL, GetGlobalObjectTemplate(pIsolate)); 249 v8::Context::New(pIsolate, NULL, GetGlobalObjectTemplate(pIsolate));
258 v8::Context::Scope context_scope(v8Context); 250 v8::Context::Scope context_scope(v8Context);
259 251
260 FXJS_PerIsolateData::SetUp(pIsolate); 252 FXJS_PerIsolateData::SetUp(pIsolate);
261 v8Context->SetAlignedPointerInEmbedderData(kPerContextDataIndex, pFXRuntime); 253 v8Context->SetAlignedPointerInEmbedderData(kPerContextDataIndex, pFXRuntime);
262 254
263 int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); 255 int maxID = CFXJS_ObjDefinition::MaxID(pIsolate);
264 for (int i = 0; i < maxID; ++i) { 256 for (int i = 0; i < maxID; ++i) {
265 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i); 257 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i);
266 CFX_WideString ws = CFX_WideString(pObjDef->objName); 258 CFX_ByteString bs = CFX_WideString(pObjDef->m_ObjName).UTF8Encode();
267 CFX_ByteString bs = ws.UTF8Encode(); 259 v8::Local<v8::String> m_ObjName =
268 v8::Local<v8::String> objName =
269 v8::String::NewFromUtf8(pIsolate, bs.c_str(), 260 v8::String::NewFromUtf8(pIsolate, bs.c_str(),
270 v8::NewStringType::kNormal, 261 v8::NewStringType::kNormal,
271 bs.GetLength()).ToLocalChecked(); 262 bs.GetLength()).ToLocalChecked();
272 263
273 if (pObjDef->objType == FXJS_DYNAMIC) { 264 if (pObjDef->m_ObjType == FXJSOBJTYPE_GLOBAL) {
274 // Document is set as global object, need to construct it first. 265 v8Context->Global()
275 if (ws.Equal(L"Document")) { 266 ->GetPrototype()
276 v8Context->Global() 267 ->ToObject(v8Context)
277 ->GetPrototype() 268 .ToLocalChecked()
278 ->ToObject(v8Context) 269 ->SetAlignedPointerInInternalField(0, new CFXJS_PrivateData(i));
279 .ToLocalChecked()
280 ->SetAlignedPointerInInternalField(0, new CFXJS_PrivateData(i));
281 270
282 if (pObjDef->m_pConstructor) 271 if (pObjDef->m_pConstructor)
283 pObjDef->m_pConstructor(context, v8Context->Global() 272 pObjDef->m_pConstructor(context, v8Context->Global()
284 ->GetPrototype() 273 ->GetPrototype()
285 ->ToObject(v8Context) 274 ->ToObject(v8Context)
286 .ToLocalChecked(), 275 .ToLocalChecked(),
287 v8Context->Global() 276 v8Context->Global()
288 ->GetPrototype() 277 ->GetPrototype()
289 ->ToObject(v8Context) 278 ->ToObject(v8Context)
290 .ToLocalChecked()); 279 .ToLocalChecked());
291 } 280 } else if (pObjDef->m_ObjType == FXJSOBJTYPE_STATIC) {
292 } else {
293 v8::Local<v8::Object> obj = FXJS_NewFxDynamicObj(pIsolate, context, i); 281 v8::Local<v8::Object> obj = FXJS_NewFxDynamicObj(pIsolate, context, i);
294 v8Context->Global()->Set(v8Context, objName, obj).FromJust(); 282 v8Context->Global()->Set(v8Context, m_ObjName, obj).FromJust();
295 pObjDef->m_StaticObj.Reset(pIsolate, obj); 283 pObjDef->m_StaticObj.Reset(pIsolate, obj);
296 } 284 }
297 } 285 }
298 v8PersistentContext.Reset(pIsolate, v8Context); 286 v8PersistentContext.Reset(pIsolate, v8Context);
299 } 287 }
300 288
301 void FXJS_ReleaseRuntime(v8::Isolate* pIsolate, 289 void FXJS_ReleaseRuntime(v8::Isolate* pIsolate,
302 v8::Global<v8::Context>& v8PersistentContext) { 290 v8::Global<v8::Context>& v8PersistentContext) {
303 v8::Isolate::Scope isolate_scope(pIsolate); 291 v8::Isolate::Scope isolate_scope(pIsolate);
304 v8::HandleScope handle_scope(pIsolate); 292 v8::HandleScope handle_scope(pIsolate);
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
419 407
420 v8::Isolate* FXJS_GetRuntime(v8::Local<v8::Object> pObj) { 408 v8::Isolate* FXJS_GetRuntime(v8::Local<v8::Object> pObj) {
421 if (pObj.IsEmpty()) 409 if (pObj.IsEmpty())
422 return NULL; 410 return NULL;
423 v8::Local<v8::Context> context = pObj->CreationContext(); 411 v8::Local<v8::Context> context = pObj->CreationContext();
424 if (context.IsEmpty()) 412 if (context.IsEmpty())
425 return NULL; 413 return NULL;
426 return context->GetIsolate(); 414 return context->GetIsolate();
427 } 415 }
428 416
429 int FXJS_GetObjDefnID(v8::Isolate* pIsolate, const wchar_t* pObjName) {
430 v8::Isolate::Scope isolate_scope(pIsolate);
431 if (!FXJS_PerIsolateData::Get(pIsolate))
432 return -1;
433
434 int maxID = CFXJS_ObjDefinition::MaxID(pIsolate);
435 for (int i = 0; i < maxID; ++i) {
436 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i);
437 if (FXSYS_wcscmp(pObjDef->objName, pObjName) == 0)
438 return i;
439 }
440 return -1;
441 }
442
443 void FXJS_Error(v8::Isolate* pIsolate, const CFX_WideString& message) { 417 void FXJS_Error(v8::Isolate* pIsolate, const CFX_WideString& message) {
444 // Conversion from pdfium's wchar_t wide-strings to v8's uint16_t 418 // Conversion from pdfium's wchar_t wide-strings to v8's uint16_t
445 // wide-strings isn't handled by v8, so use UTF8 as a common 419 // wide-strings isn't handled by v8, so use UTF8 as a common
446 // intermediate format. 420 // intermediate format.
447 CFX_ByteString utf8_message = message.UTF8Encode(); 421 CFX_ByteString utf8_message = message.UTF8Encode();
448 pIsolate->ThrowException( 422 pIsolate->ThrowException(
449 v8::String::NewFromUtf8(pIsolate, utf8_message.c_str(), 423 v8::String::NewFromUtf8(pIsolate, utf8_message.c_str(),
450 v8::NewStringType::kNormal).ToLocalChecked()); 424 v8::NewStringType::kNormal).ToLocalChecked());
451 } 425 }
452 426
(...skipping 287 matching lines...) Expand 10 before | Expand all | Expand 10 after
740 return v8::Local<v8::Array>(); 714 return v8::Local<v8::Array>();
741 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); 715 v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
742 return v8::Local<v8::Array>::Cast(pValue->ToObject(context).ToLocalChecked()); 716 return v8::Local<v8::Array>::Cast(pValue->ToObject(context).ToLocalChecked());
743 } 717 }
744 718
745 void FXJS_ValueCopy(v8::Local<v8::Value>& pTo, v8::Local<v8::Value> pFrom) { 719 void FXJS_ValueCopy(v8::Local<v8::Value>& pTo, v8::Local<v8::Value> pFrom) {
746 pTo = pFrom; 720 pTo = pFrom;
747 } 721 }
748 722
749 723
OLDNEW
« fpdfsdk/include/javascript/JS_Define.h ('K') | « fpdfsdk/src/javascript/event.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698