OLD | NEW |
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/fsdk_define.h" | |
9 #include "../../include/jsapi/fxjs_v8.h" | 8 #include "../../include/jsapi/fxjs_v8.h" |
10 | 9 |
11 const wchar_t kFXJSValueNameString[] = L"string"; | 10 const wchar_t kFXJSValueNameString[] = L"string"; |
12 const wchar_t kFXJSValueNameNumber[] = L"number"; | 11 const wchar_t kFXJSValueNameNumber[] = L"number"; |
13 const wchar_t kFXJSValueNameBoolean[] = L"boolean"; | 12 const wchar_t kFXJSValueNameBoolean[] = L"boolean"; |
14 const wchar_t kFXJSValueNameDate[] = L"date"; | 13 const wchar_t kFXJSValueNameDate[] = L"date"; |
15 const wchar_t kFXJSValueNameObject[] = L"object"; | 14 const wchar_t kFXJSValueNameObject[] = L"object"; |
16 const wchar_t kFXJSValueNameFxobj[] = L"fxobj"; | 15 const wchar_t kFXJSValueNameFxobj[] = L"fxobj"; |
17 const wchar_t kFXJSValueNameNull[] = L"null"; | 16 const wchar_t kFXJSValueNameNull[] = L"null"; |
18 const wchar_t kFXJSValueNameUndefined[] = L"undefined"; | 17 const wchar_t kFXJSValueNameUndefined[] = L"undefined"; |
19 | 18 |
20 static unsigned int g_embedderDataSlot = 1u; | 19 static unsigned int g_embedderDataSlot = 1u; |
21 | 20 |
| 21 // Keep this consistent with the values defined in gin/public/context_holder.h |
| 22 // (without actually requiring a dependency on gin itself for the standalone |
| 23 // embedders of PDFIum). The value we want to use is: |
| 24 // kPerContextDataStartIndex + kEmbedderPDFium, which is 3. |
| 25 static const unsigned int kPerContextDataIndex = 3u; |
| 26 |
22 class CFXJS_PrivateData { | 27 class CFXJS_PrivateData { |
23 public: | 28 public: |
24 CFXJS_PrivateData(int nObjDefID) : ObjDefID(nObjDefID), pPrivate(NULL) {} | 29 CFXJS_PrivateData(int nObjDefID) : ObjDefID(nObjDefID), pPrivate(NULL) {} |
25 | 30 |
26 int ObjDefID; | 31 int ObjDefID; |
27 void* pPrivate; | 32 void* pPrivate; |
28 }; | 33 }; |
29 | 34 |
30 class CFXJS_ObjDefinition { | 35 class CFXJS_ObjDefinition { |
31 public: | 36 public: |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 if (!pIsolate->GetData(g_embedderDataSlot)) | 98 if (!pIsolate->GetData(g_embedderDataSlot)) |
94 pIsolate->SetData(g_embedderDataSlot, new FXJS_PerIsolateData()); | 99 pIsolate->SetData(g_embedderDataSlot, new FXJS_PerIsolateData()); |
95 } | 100 } |
96 | 101 |
97 // static | 102 // static |
98 FXJS_PerIsolateData* FXJS_PerIsolateData::Get(v8::Isolate* pIsolate) { | 103 FXJS_PerIsolateData* FXJS_PerIsolateData::Get(v8::Isolate* pIsolate) { |
99 return static_cast<FXJS_PerIsolateData*>( | 104 return static_cast<FXJS_PerIsolateData*>( |
100 pIsolate->GetData(g_embedderDataSlot)); | 105 pIsolate->GetData(g_embedderDataSlot)); |
101 } | 106 } |
102 | 107 |
| 108 void FXJS_Initialize(unsigned int embedderDataSlot) { |
| 109 g_embedderDataSlot = embedderDataSlot; |
| 110 } |
| 111 |
| 112 void FXJS_Release() { |
| 113 } |
| 114 |
103 int FXJS_DefineObj(v8::Isolate* pIsolate, | 115 int FXJS_DefineObj(v8::Isolate* pIsolate, |
104 const wchar_t* sObjName, | 116 const wchar_t* sObjName, |
105 FXJSOBJTYPE eObjType, | 117 FXJSOBJTYPE eObjType, |
106 FXJS_CONSTRUCTOR pConstructor, | 118 FXJS_CONSTRUCTOR pConstructor, |
107 FXJS_DESTRUCTOR pDestructor) { | 119 FXJS_DESTRUCTOR pDestructor) { |
108 v8::Isolate::Scope isolate_scope(pIsolate); | 120 v8::Isolate::Scope isolate_scope(pIsolate); |
109 v8::HandleScope handle_scope(pIsolate); | 121 v8::HandleScope handle_scope(pIsolate); |
110 | 122 |
111 FXJS_PerIsolateData::SetUp(pIsolate); | 123 FXJS_PerIsolateData::SetUp(pIsolate); |
112 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate); | 124 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate); |
(...skipping 149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
262 v8::HandleScope handle_scope(pIsolate); | 274 v8::HandleScope handle_scope(pIsolate); |
263 | 275 |
264 v8::Global<v8::ObjectTemplate>& globalObjTemp = | 276 v8::Global<v8::ObjectTemplate>& globalObjTemp = |
265 _getGlobalObjectTemplate(pIsolate); | 277 _getGlobalObjectTemplate(pIsolate); |
266 v8::Local<v8::Context> v8Context = v8::Context::New( | 278 v8::Local<v8::Context> v8Context = v8::Context::New( |
267 pIsolate, NULL, | 279 pIsolate, NULL, |
268 v8::Local<v8::ObjectTemplate>::New(pIsolate, globalObjTemp)); | 280 v8::Local<v8::ObjectTemplate>::New(pIsolate, globalObjTemp)); |
269 v8::Context::Scope context_scope(v8Context); | 281 v8::Context::Scope context_scope(v8Context); |
270 | 282 |
271 FXJS_PerIsolateData::SetUp(pIsolate); | 283 FXJS_PerIsolateData::SetUp(pIsolate); |
272 v8::Local<v8::External> ptr = v8::External::New(pIsolate, pFXRuntime); | 284 v8Context->SetAlignedPointerInEmbedderData(kPerContextDataIndex, pFXRuntime); |
273 v8Context->SetEmbedderData(1, ptr); | |
274 | 285 |
275 int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); | 286 int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); |
276 for (int i = 0; i < maxID; ++i) { | 287 for (int i = 0; i < maxID; ++i) { |
277 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i); | 288 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i); |
278 CFX_WideString ws = CFX_WideString(pObjDef->objName); | 289 CFX_WideString ws = CFX_WideString(pObjDef->objName); |
279 CFX_ByteString bs = ws.UTF8Encode(); | 290 CFX_ByteString bs = ws.UTF8Encode(); |
280 v8::Local<v8::String> objName = | 291 v8::Local<v8::String> objName = |
281 v8::String::NewFromUtf8(pIsolate, bs.c_str(), | 292 v8::String::NewFromUtf8(pIsolate, bs.c_str(), |
282 v8::NewStringType::kNormal, | 293 v8::NewStringType::kNormal, |
283 bs.GetLength()).ToLocalChecked(); | 294 bs.GetLength()).ToLocalChecked(); |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
336 pObjDef->m_pDestructor(pObj); | 347 pObjDef->m_pDestructor(pObj); |
337 FXJS_FreePrivate(pObj); | 348 FXJS_FreePrivate(pObj); |
338 } | 349 } |
339 delete pObjDef; | 350 delete pObjDef; |
340 } | 351 } |
341 | 352 |
342 pIsolate->SetData(g_embedderDataSlot, nullptr); | 353 pIsolate->SetData(g_embedderDataSlot, nullptr); |
343 delete pData; | 354 delete pData; |
344 } | 355 } |
345 | 356 |
346 void FXJS_Initialize(unsigned int embedderDataSlot) { | 357 IFXJS_Runtime* FXJS_GetRuntimeFromIsolate(v8::Isolate* pIsolate) { |
347 g_embedderDataSlot = embedderDataSlot; | 358 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); |
348 } | 359 return static_cast<IFXJS_Runtime*>( |
349 | 360 context->GetAlignedPointerFromEmbedderData(kPerContextDataIndex)); |
350 void FXJS_Release() { | |
351 } | 361 } |
352 | 362 |
353 int FXJS_Execute(v8::Isolate* pIsolate, | 363 int FXJS_Execute(v8::Isolate* pIsolate, |
354 IFXJS_Context* pJSContext, | 364 IFXJS_Context* pJSContext, |
355 const wchar_t* script, | 365 const wchar_t* script, |
356 long length, | 366 long length, |
357 FXJSErr* pError) { | 367 FXJSErr* pError) { |
358 v8::Isolate::Scope isolate_scope(pIsolate); | 368 v8::Isolate::Scope isolate_scope(pIsolate); |
359 v8::TryCatch try_catch(pIsolate); | 369 v8::TryCatch try_catch(pIsolate); |
360 | 370 |
(...skipping 402 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
763 return v8::Local<v8::Array>(); | 773 return v8::Local<v8::Array>(); |
764 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); | 774 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); |
765 return v8::Local<v8::Array>::Cast(pValue->ToObject(context).ToLocalChecked()); | 775 return v8::Local<v8::Array>::Cast(pValue->ToObject(context).ToLocalChecked()); |
766 } | 776 } |
767 | 777 |
768 void FXJS_ValueCopy(v8::Local<v8::Value>& pTo, v8::Local<v8::Value> pFrom) { | 778 void FXJS_ValueCopy(v8::Local<v8::Value>& pTo, v8::Local<v8::Value> pFrom) { |
769 pTo = pFrom; | 779 pTo = pFrom; |
770 } | 780 } |
771 | 781 |
772 | 782 |
OLD | NEW |