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" | 8 #include "../../include/fsdk_define.h" |
9 #include "../../include/jsapi/fxjs_v8.h" | 9 #include "../../include/jsapi/fxjs_v8.h" |
10 | 10 |
11 const wchar_t kFXJSValueNameString[] = L"string"; | 11 const wchar_t kFXJSValueNameString[] = L"string"; |
12 const wchar_t kFXJSValueNameNumber[] = L"number"; | 12 const wchar_t kFXJSValueNameNumber[] = L"number"; |
13 const wchar_t kFXJSValueNameBoolean[] = L"boolean"; | 13 const wchar_t kFXJSValueNameBoolean[] = L"boolean"; |
14 const wchar_t kFXJSValueNameDate[] = L"date"; | 14 const wchar_t kFXJSValueNameDate[] = L"date"; |
15 const wchar_t kFXJSValueNameObject[] = L"object"; | 15 const wchar_t kFXJSValueNameObject[] = L"object"; |
16 const wchar_t kFXJSValueNameFxobj[] = L"fxobj"; | 16 const wchar_t kFXJSValueNameFxobj[] = L"fxobj"; |
17 const wchar_t kFXJSValueNameNull[] = L"null"; | 17 const wchar_t kFXJSValueNameNull[] = L"null"; |
18 const wchar_t kFXJSValueNameUndefined[] = L"undefined"; | 18 const wchar_t kFXJSValueNameUndefined[] = L"undefined"; |
19 | 19 |
20 static unsigned int g_embedderDataSlot = 0u; | 20 static unsigned int g_embedderDataSlot = 1u; |
21 | 21 |
22 class CFXJS_PrivateData { | 22 class CFXJS_PrivateData { |
23 public: | 23 public: |
24 CFXJS_PrivateData() : ObjDefID(-1), pPrivate(NULL) {} | 24 CFXJS_PrivateData(int nObjDefID) : ObjDefID(nObjDefID), pPrivate(NULL) {} |
| 25 |
25 int ObjDefID; | 26 int ObjDefID; |
26 void* pPrivate; | 27 void* pPrivate; |
27 }; | 28 }; |
28 | 29 |
29 class CFXJS_ObjDefintion { | 30 class CFXJS_ObjDefinition { |
30 public: | 31 public: |
31 CFXJS_ObjDefintion(v8::Isolate* isolate, | 32 static int MaxID(v8::Isolate* pIsolate) { |
32 const wchar_t* sObjName, | 33 return static_cast<int>( |
33 FXJSOBJTYPE eObjType, | 34 FXJS_PerIsolateData::Get(pIsolate)->m_ObjectDefnArray.GetSize()); |
34 FXJS_CONSTRUCTOR pConstructor, | 35 } |
35 FXJS_DESTRUCTOR pDestructor) | 36 static CFXJS_ObjDefinition* ForID(v8::Isolate* pIsolate, int id) { |
| 37 // Note: GetAt() halts if out-of-range even in release builds. |
| 38 return static_cast<CFXJS_ObjDefinition*>( |
| 39 FXJS_PerIsolateData::Get(pIsolate)->m_ObjectDefnArray.GetAt(id)); |
| 40 } |
| 41 CFXJS_ObjDefinition(v8::Isolate* isolate, |
| 42 const wchar_t* sObjName, |
| 43 FXJSOBJTYPE eObjType, |
| 44 FXJS_CONSTRUCTOR pConstructor, |
| 45 FXJS_DESTRUCTOR pDestructor) |
36 : objName(sObjName), | 46 : objName(sObjName), |
37 objType(eObjType), | 47 objType(eObjType), |
38 m_pConstructor(pConstructor), | 48 m_pConstructor(pConstructor), |
39 m_pDestructor(pDestructor), | 49 m_pDestructor(pDestructor), |
40 m_bSetAsGlobalObject(FALSE) { | 50 m_bSetAsGlobalObject(FALSE) { |
41 v8::Isolate::Scope isolate_scope(isolate); | 51 v8::Isolate::Scope isolate_scope(isolate); |
42 v8::HandleScope handle_scope(isolate); | 52 v8::HandleScope handle_scope(isolate); |
43 | 53 |
44 v8::Local<v8::ObjectTemplate> objTemplate = | 54 v8::Local<v8::ObjectTemplate> objTemplate = |
45 v8::ObjectTemplate::New(isolate); | 55 v8::ObjectTemplate::New(isolate); |
46 objTemplate->SetInternalFieldCount(2); | 56 objTemplate->SetInternalFieldCount(2); |
47 m_objTemplate.Reset(isolate, objTemplate); | 57 m_objTemplate.Reset(isolate, objTemplate); |
48 | 58 |
49 // Document as the global object. | 59 // Document as the global object. |
50 if (FXSYS_wcscmp(sObjName, L"Document") == 0) { | 60 if (FXSYS_wcscmp(sObjName, L"Document") == 0) { |
51 m_bSetAsGlobalObject = TRUE; | 61 m_bSetAsGlobalObject = TRUE; |
52 } | 62 } |
53 } | 63 } |
54 ~CFXJS_ObjDefintion() { | 64 ~CFXJS_ObjDefinition() { |
55 m_objTemplate.Reset(); | 65 m_objTemplate.Reset(); |
56 m_StaticObj.Reset(); | 66 m_StaticObj.Reset(); |
57 } | 67 } |
58 | 68 |
59 public: | |
60 const wchar_t* objName; | 69 const wchar_t* objName; |
61 FXJSOBJTYPE objType; | 70 FXJSOBJTYPE objType; |
62 FXJS_CONSTRUCTOR m_pConstructor; | 71 FXJS_CONSTRUCTOR m_pConstructor; |
63 FXJS_DESTRUCTOR m_pDestructor; | 72 FXJS_DESTRUCTOR m_pDestructor; |
64 FX_BOOL m_bSetAsGlobalObject; | 73 FX_BOOL m_bSetAsGlobalObject; |
65 | 74 |
66 v8::Global<v8::ObjectTemplate> m_objTemplate; | 75 v8::Global<v8::ObjectTemplate> m_objTemplate; |
67 v8::Global<v8::Object> m_StaticObj; | 76 v8::Global<v8::Object> m_StaticObj; |
68 }; | 77 }; |
69 | 78 |
70 void* FXJS_ArrayBufferAllocator::Allocate(size_t length) { | 79 void* FXJS_ArrayBufferAllocator::Allocate(size_t length) { |
71 return calloc(1, length); | 80 return calloc(1, length); |
72 } | 81 } |
73 | 82 |
74 void* FXJS_ArrayBufferAllocator::AllocateUninitialized(size_t length) { | 83 void* FXJS_ArrayBufferAllocator::AllocateUninitialized(size_t length) { |
75 return malloc(length); | 84 return malloc(length); |
76 } | 85 } |
77 | 86 |
78 void FXJS_ArrayBufferAllocator::Free(void* data, size_t length) { | 87 void FXJS_ArrayBufferAllocator::Free(void* data, size_t length) { |
79 free(data); | 88 free(data); |
80 } | 89 } |
81 | 90 |
82 void FXJS_PrepareIsolate(v8::Isolate* pIsolate) { | 91 // static |
| 92 void FXJS_PerIsolateData::SetUp(v8::Isolate* pIsolate) { |
83 if (!pIsolate->GetData(g_embedderDataSlot)) | 93 if (!pIsolate->GetData(g_embedderDataSlot)) |
84 pIsolate->SetData(g_embedderDataSlot, new CFX_PtrArray()); | 94 pIsolate->SetData(g_embedderDataSlot, new FXJS_PerIsolateData()); |
| 95 } |
| 96 |
| 97 // static |
| 98 FXJS_PerIsolateData* FXJS_PerIsolateData::Get(v8::Isolate* pIsolate) { |
| 99 return static_cast<FXJS_PerIsolateData*>( |
| 100 pIsolate->GetData(g_embedderDataSlot)); |
85 } | 101 } |
86 | 102 |
87 int FXJS_DefineObj(v8::Isolate* pIsolate, | 103 int FXJS_DefineObj(v8::Isolate* pIsolate, |
88 const wchar_t* sObjName, | 104 const wchar_t* sObjName, |
89 FXJSOBJTYPE eObjType, | 105 FXJSOBJTYPE eObjType, |
90 FXJS_CONSTRUCTOR pConstructor, | 106 FXJS_CONSTRUCTOR pConstructor, |
91 FXJS_DESTRUCTOR pDestructor) { | 107 FXJS_DESTRUCTOR pDestructor) { |
92 v8::Isolate::Scope isolate_scope(pIsolate); | 108 v8::Isolate::Scope isolate_scope(pIsolate); |
93 v8::HandleScope handle_scope(pIsolate); | 109 v8::HandleScope handle_scope(pIsolate); |
94 | 110 |
95 FXJS_PrepareIsolate(pIsolate); | 111 FXJS_PerIsolateData::SetUp(pIsolate); |
96 CFX_PtrArray* pArray = (CFX_PtrArray*)pIsolate->GetData(g_embedderDataSlot); | 112 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate); |
97 CFXJS_ObjDefintion* pObjDef = new CFXJS_ObjDefintion( | 113 pData->m_ObjectDefnArray.Add(new CFXJS_ObjDefinition( |
98 pIsolate, sObjName, eObjType, pConstructor, pDestructor); | 114 pIsolate, sObjName, eObjType, pConstructor, pDestructor)); |
99 pArray->Add(pObjDef); | 115 return pData->m_ObjectDefnArray.GetSize() - 1; |
100 return pArray->GetSize() - 1; | |
101 } | 116 } |
102 | 117 |
103 void FXJS_DefineObjMethod(v8::Isolate* pIsolate, | 118 void FXJS_DefineObjMethod(v8::Isolate* pIsolate, |
104 int nObjDefnID, | 119 int nObjDefnID, |
105 const wchar_t* sMethodName, | 120 const wchar_t* sMethodName, |
106 v8::FunctionCallback pMethodCall) { | 121 v8::FunctionCallback pMethodCall) { |
107 v8::Isolate::Scope isolate_scope(pIsolate); | 122 v8::Isolate::Scope isolate_scope(pIsolate); |
108 v8::HandleScope handle_scope(pIsolate); | 123 v8::HandleScope handle_scope(pIsolate); |
109 | 124 |
110 CFX_WideString ws = CFX_WideString(sMethodName); | 125 CFX_ByteString bsMethodName = CFX_WideString(sMethodName).UTF8Encode(); |
111 CFX_ByteString bsMethodName = ws.UTF8Encode(); | 126 CFXJS_ObjDefinition* pObjDef = |
112 CFX_PtrArray* pArray = (CFX_PtrArray*)pIsolate->GetData(g_embedderDataSlot); | 127 CFXJS_ObjDefinition::ForID(pIsolate, nObjDefnID); |
113 | |
114 // Note: GetAt() halts if out-of-range even in release builds. | |
115 CFXJS_ObjDefintion* pObjDef = (CFXJS_ObjDefintion*)pArray->GetAt(nObjDefnID); | |
116 v8::Local<v8::ObjectTemplate> objTemp = | 128 v8::Local<v8::ObjectTemplate> objTemp = |
117 v8::Local<v8::ObjectTemplate>::New(pIsolate, pObjDef->m_objTemplate); | 129 v8::Local<v8::ObjectTemplate>::New(pIsolate, pObjDef->m_objTemplate); |
| 130 |
118 objTemp->Set( | 131 objTemp->Set( |
119 v8::String::NewFromUtf8(pIsolate, bsMethodName.c_str(), | 132 v8::String::NewFromUtf8(pIsolate, bsMethodName.c_str(), |
120 v8::NewStringType::kNormal).ToLocalChecked(), | 133 v8::NewStringType::kNormal).ToLocalChecked(), |
121 v8::FunctionTemplate::New(pIsolate, pMethodCall), v8::ReadOnly); | 134 v8::FunctionTemplate::New(pIsolate, pMethodCall), v8::ReadOnly); |
122 pObjDef->m_objTemplate.Reset(pIsolate, objTemp); | 135 pObjDef->m_objTemplate.Reset(pIsolate, objTemp); |
123 } | 136 } |
124 | 137 |
125 void FXJS_DefineObjProperty(v8::Isolate* pIsolate, | 138 void FXJS_DefineObjProperty(v8::Isolate* pIsolate, |
126 int nObjDefnID, | 139 int nObjDefnID, |
127 const wchar_t* sPropName, | 140 const wchar_t* sPropName, |
128 v8::AccessorGetterCallback pPropGet, | 141 v8::AccessorGetterCallback pPropGet, |
129 v8::AccessorSetterCallback pPropPut) { | 142 v8::AccessorSetterCallback pPropPut) { |
130 v8::Isolate::Scope isolate_scope(pIsolate); | 143 v8::Isolate::Scope isolate_scope(pIsolate); |
131 v8::HandleScope handle_scope(pIsolate); | 144 v8::HandleScope handle_scope(pIsolate); |
132 | 145 |
133 CFX_WideString ws = CFX_WideString(sPropName); | 146 CFX_ByteString bsPropertyName = CFX_WideString(sPropName).UTF8Encode(); |
134 CFX_ByteString bsPropertyName = ws.UTF8Encode(); | 147 CFXJS_ObjDefinition* pObjDef = |
135 CFX_PtrArray* pArray = (CFX_PtrArray*)pIsolate->GetData(g_embedderDataSlot); | 148 CFXJS_ObjDefinition::ForID(pIsolate, nObjDefnID); |
136 | |
137 // Note: GetAt() halts if out-of-range even in release builds. | |
138 CFXJS_ObjDefintion* pObjDef = (CFXJS_ObjDefintion*)pArray->GetAt(nObjDefnID); | |
139 v8::Local<v8::ObjectTemplate> objTemp = | 149 v8::Local<v8::ObjectTemplate> objTemp = |
140 v8::Local<v8::ObjectTemplate>::New(pIsolate, pObjDef->m_objTemplate); | 150 v8::Local<v8::ObjectTemplate>::New(pIsolate, pObjDef->m_objTemplate); |
141 objTemp->SetAccessor( | 151 objTemp->SetAccessor( |
142 v8::String::NewFromUtf8(pIsolate, bsPropertyName.c_str(), | 152 v8::String::NewFromUtf8(pIsolate, bsPropertyName.c_str(), |
143 v8::NewStringType::kNormal).ToLocalChecked(), | 153 v8::NewStringType::kNormal).ToLocalChecked(), |
144 pPropGet, pPropPut); | 154 pPropGet, pPropPut); |
145 pObjDef->m_objTemplate.Reset(pIsolate, objTemp); | 155 pObjDef->m_objTemplate.Reset(pIsolate, objTemp); |
146 } | 156 } |
147 | 157 |
148 void FXJS_DefineObjAllProperties(v8::Isolate* pIsolate, | 158 void FXJS_DefineObjAllProperties(v8::Isolate* pIsolate, |
149 int nObjDefnID, | 159 int nObjDefnID, |
150 v8::NamedPropertyQueryCallback pPropQurey, | 160 v8::NamedPropertyQueryCallback pPropQurey, |
151 v8::NamedPropertyGetterCallback pPropGet, | 161 v8::NamedPropertyGetterCallback pPropGet, |
152 v8::NamedPropertySetterCallback pPropPut, | 162 v8::NamedPropertySetterCallback pPropPut, |
153 v8::NamedPropertyDeleterCallback pPropDel) { | 163 v8::NamedPropertyDeleterCallback pPropDel) { |
154 v8::Isolate::Scope isolate_scope(pIsolate); | 164 v8::Isolate::Scope isolate_scope(pIsolate); |
155 v8::HandleScope handle_scope(pIsolate); | 165 v8::HandleScope handle_scope(pIsolate); |
156 CFX_PtrArray* pArray = (CFX_PtrArray*)pIsolate->GetData(g_embedderDataSlot); | |
157 | 166 |
158 // Note: GetAt() halts if out-of-range even in release builds. | 167 CFXJS_ObjDefinition* pObjDef = |
159 CFXJS_ObjDefintion* pObjDef = (CFXJS_ObjDefintion*)pArray->GetAt(nObjDefnID); | 168 CFXJS_ObjDefinition::ForID(pIsolate, nObjDefnID); |
160 v8::Local<v8::ObjectTemplate> objTemp = | 169 v8::Local<v8::ObjectTemplate> objTemp = |
161 v8::Local<v8::ObjectTemplate>::New(pIsolate, pObjDef->m_objTemplate); | 170 v8::Local<v8::ObjectTemplate>::New(pIsolate, pObjDef->m_objTemplate); |
162 objTemp->SetNamedPropertyHandler(pPropGet, pPropPut, pPropQurey, pPropDel); | 171 objTemp->SetNamedPropertyHandler(pPropGet, pPropPut, pPropQurey, pPropDel); |
163 pObjDef->m_objTemplate.Reset(pIsolate, objTemp); | 172 pObjDef->m_objTemplate.Reset(pIsolate, objTemp); |
164 } | 173 } |
165 | 174 |
166 void FXJS_DefineObjConst(v8::Isolate* pIsolate, | 175 void FXJS_DefineObjConst(v8::Isolate* pIsolate, |
167 int nObjDefnID, | 176 int nObjDefnID, |
168 const wchar_t* sConstName, | 177 const wchar_t* sConstName, |
169 v8::Local<v8::Value> pDefault) { | 178 v8::Local<v8::Value> pDefault) { |
170 v8::Isolate::Scope isolate_scope(pIsolate); | 179 v8::Isolate::Scope isolate_scope(pIsolate); |
171 v8::HandleScope handle_scope(pIsolate); | 180 v8::HandleScope handle_scope(pIsolate); |
172 | 181 |
173 CFX_WideString ws = CFX_WideString(sConstName); | 182 CFX_ByteString bsConstName = CFX_WideString(sConstName).UTF8Encode(); |
174 CFX_ByteString bsConstName = ws.UTF8Encode(); | 183 CFXJS_ObjDefinition* pObjDef = |
175 CFX_PtrArray* pArray = (CFX_PtrArray*)pIsolate->GetData(g_embedderDataSlot); | 184 CFXJS_ObjDefinition::ForID(pIsolate, nObjDefnID); |
176 | |
177 // Note: GetAt() halts if out-of-range even in release builds. | |
178 CFXJS_ObjDefintion* pObjDef = (CFXJS_ObjDefintion*)pArray->GetAt(nObjDefnID); | |
179 v8::Local<v8::ObjectTemplate> objTemp = | 185 v8::Local<v8::ObjectTemplate> objTemp = |
180 v8::Local<v8::ObjectTemplate>::New(pIsolate, pObjDef->m_objTemplate); | 186 v8::Local<v8::ObjectTemplate>::New(pIsolate, pObjDef->m_objTemplate); |
181 objTemp->Set(pIsolate, bsConstName.c_str(), pDefault); | 187 objTemp->Set(pIsolate, bsConstName.c_str(), pDefault); |
182 pObjDef->m_objTemplate.Reset(pIsolate, objTemp); | 188 pObjDef->m_objTemplate.Reset(pIsolate, objTemp); |
183 } | 189 } |
184 | 190 |
185 static v8::Global<v8::ObjectTemplate>& _getGlobalObjectTemplate( | 191 static v8::Global<v8::ObjectTemplate>& _getGlobalObjectTemplate( |
186 v8::Isolate* pIsolate) { | 192 v8::Isolate* pIsolate) { |
187 v8::Isolate::Scope isolate_scope(pIsolate); | 193 v8::Isolate::Scope isolate_scope(pIsolate); |
188 v8::HandleScope handle_scope(pIsolate); | 194 v8::HandleScope handle_scope(pIsolate); |
189 | 195 |
190 CFX_PtrArray* pArray = (CFX_PtrArray*)pIsolate->GetData(g_embedderDataSlot); | 196 int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); |
191 ASSERT(pArray != NULL); | 197 for (int i = 0; i < maxID; ++i) { |
192 for (int i = 0; i < pArray->GetSize(); i++) { | 198 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i); |
193 CFXJS_ObjDefintion* pObjDef = (CFXJS_ObjDefintion*)pArray->GetAt(i); | |
194 if (pObjDef->m_bSetAsGlobalObject) | 199 if (pObjDef->m_bSetAsGlobalObject) |
195 return pObjDef->m_objTemplate; | 200 return pObjDef->m_objTemplate; |
196 } | 201 } |
197 static v8::Global<v8::ObjectTemplate> gloabalObjectTemplate; | 202 static v8::Global<v8::ObjectTemplate> gloabalObjectTemplate; |
198 return gloabalObjectTemplate; | 203 return gloabalObjectTemplate; |
199 } | 204 } |
200 | 205 |
201 void FXJS_DefineGlobalMethod(v8::Isolate* pIsolate, | 206 void FXJS_DefineGlobalMethod(v8::Isolate* pIsolate, |
202 const wchar_t* sMethodName, | 207 const wchar_t* sMethodName, |
203 v8::FunctionCallback pMethodCall) { | 208 v8::FunctionCallback pMethodCall) { |
204 v8::Isolate::Scope isolate_scope(pIsolate); | 209 v8::Isolate::Scope isolate_scope(pIsolate); |
205 v8::HandleScope handle_scope(pIsolate); | 210 v8::HandleScope handle_scope(pIsolate); |
206 | 211 |
207 CFX_WideString ws = CFX_WideString(sMethodName); | 212 CFX_ByteString bsMethodName = CFX_WideString(sMethodName).UTF8Encode(); |
208 CFX_ByteString bsMethodName = ws.UTF8Encode(); | |
209 | |
210 v8::Local<v8::FunctionTemplate> funTempl = | 213 v8::Local<v8::FunctionTemplate> funTempl = |
211 v8::FunctionTemplate::New(pIsolate, pMethodCall); | 214 v8::FunctionTemplate::New(pIsolate, pMethodCall); |
212 v8::Local<v8::ObjectTemplate> objTemp; | 215 v8::Local<v8::ObjectTemplate> objTemp; |
213 | 216 |
214 v8::Global<v8::ObjectTemplate>& globalObjTemp = | 217 v8::Global<v8::ObjectTemplate>& globalObjTemp = |
215 _getGlobalObjectTemplate(pIsolate); | 218 _getGlobalObjectTemplate(pIsolate); |
216 if (globalObjTemp.IsEmpty()) | 219 if (globalObjTemp.IsEmpty()) |
217 objTemp = v8::ObjectTemplate::New(pIsolate); | 220 objTemp = v8::ObjectTemplate::New(pIsolate); |
218 else | 221 else |
219 objTemp = v8::Local<v8::ObjectTemplate>::New(pIsolate, globalObjTemp); | 222 objTemp = v8::Local<v8::ObjectTemplate>::New(pIsolate, globalObjTemp); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
258 v8::Locker locker(pIsolate); | 261 v8::Locker locker(pIsolate); |
259 v8::HandleScope handle_scope(pIsolate); | 262 v8::HandleScope handle_scope(pIsolate); |
260 | 263 |
261 v8::Global<v8::ObjectTemplate>& globalObjTemp = | 264 v8::Global<v8::ObjectTemplate>& globalObjTemp = |
262 _getGlobalObjectTemplate(pIsolate); | 265 _getGlobalObjectTemplate(pIsolate); |
263 v8::Local<v8::Context> v8Context = v8::Context::New( | 266 v8::Local<v8::Context> v8Context = v8::Context::New( |
264 pIsolate, NULL, | 267 pIsolate, NULL, |
265 v8::Local<v8::ObjectTemplate>::New(pIsolate, globalObjTemp)); | 268 v8::Local<v8::ObjectTemplate>::New(pIsolate, globalObjTemp)); |
266 v8::Context::Scope context_scope(v8Context); | 269 v8::Context::Scope context_scope(v8Context); |
267 | 270 |
268 // v8::Local<External> ptr = External::New(isolate, pFXRuntime); | 271 FXJS_PerIsolateData::SetUp(pIsolate); |
269 // v8Context->SetEmbedderData(1, ptr); | 272 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate); |
270 // TODO(tsepez): Don't use more than one embedder data slot. | 273 pData->m_pFXJSRuntime = pFXRuntime; |
271 pIsolate->SetData(2, pFXRuntime); | |
272 | 274 |
273 CFX_PtrArray* pArray = (CFX_PtrArray*)pIsolate->GetData(g_embedderDataSlot); | 275 int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); |
274 if (!pArray) | 276 for (int i = 0; i < maxID; ++i) { |
275 return; | 277 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i); |
276 | |
277 for (int i = 0; i < pArray->GetSize(); i++) { | |
278 CFXJS_ObjDefintion* pObjDef = (CFXJS_ObjDefintion*)pArray->GetAt(i); | |
279 CFX_WideString ws = CFX_WideString(pObjDef->objName); | 278 CFX_WideString ws = CFX_WideString(pObjDef->objName); |
280 CFX_ByteString bs = ws.UTF8Encode(); | 279 CFX_ByteString bs = ws.UTF8Encode(); |
281 v8::Local<v8::String> objName = | 280 v8::Local<v8::String> objName = |
282 v8::String::NewFromUtf8(pIsolate, bs.c_str(), | 281 v8::String::NewFromUtf8(pIsolate, bs.c_str(), |
283 v8::NewStringType::kNormal, | 282 v8::NewStringType::kNormal, |
284 bs.GetLength()).ToLocalChecked(); | 283 bs.GetLength()).ToLocalChecked(); |
285 | 284 |
286 if (pObjDef->objType == FXJS_DYNAMIC) { | 285 if (pObjDef->objType == FXJS_DYNAMIC) { |
287 // Document is set as global object, need to construct it first. | 286 // Document is set as global object, need to construct it first. |
288 if (ws.Equal(L"Document")) { | 287 if (ws.Equal(L"Document")) { |
289 CFXJS_PrivateData* pPrivateData = new CFXJS_PrivateData; | |
290 pPrivateData->ObjDefID = i; | |
291 | |
292 v8Context->Global() | 288 v8Context->Global() |
293 ->GetPrototype() | 289 ->GetPrototype() |
294 ->ToObject(v8Context) | 290 ->ToObject(v8Context) |
295 .ToLocalChecked() | 291 .ToLocalChecked() |
296 ->SetAlignedPointerInInternalField(0, pPrivateData); | 292 ->SetAlignedPointerInInternalField(0, new CFXJS_PrivateData(i)); |
297 | 293 |
298 if (pObjDef->m_pConstructor) | 294 if (pObjDef->m_pConstructor) |
299 pObjDef->m_pConstructor(context, v8Context->Global() | 295 pObjDef->m_pConstructor(context, v8Context->Global() |
300 ->GetPrototype() | 296 ->GetPrototype() |
301 ->ToObject(v8Context) | 297 ->ToObject(v8Context) |
302 .ToLocalChecked(), | 298 .ToLocalChecked(), |
303 v8Context->Global() | 299 v8Context->Global() |
304 ->GetPrototype() | 300 ->GetPrototype() |
305 ->ToObject(v8Context) | 301 ->ToObject(v8Context) |
306 .ToLocalChecked()); | 302 .ToLocalChecked()); |
307 } | 303 } |
308 } else { | 304 } else { |
309 v8::Local<v8::Object> obj = FXJS_NewFxDynamicObj(pIsolate, context, i); | 305 v8::Local<v8::Object> obj = FXJS_NewFxDynamicObj(pIsolate, context, i); |
310 v8Context->Global()->Set(v8Context, objName, obj).FromJust(); | 306 v8Context->Global()->Set(v8Context, objName, obj).FromJust(); |
311 pObjDef->m_StaticObj.Reset(pIsolate, obj); | 307 pObjDef->m_StaticObj.Reset(pIsolate, obj); |
312 } | 308 } |
313 } | 309 } |
314 v8PersistentContext.Reset(pIsolate, v8Context); | 310 v8PersistentContext.Reset(pIsolate, v8Context); |
315 } | 311 } |
316 | 312 |
317 void FXJS_ReleaseRuntime(v8::Isolate* pIsolate, | 313 void FXJS_ReleaseRuntime(v8::Isolate* pIsolate, |
318 v8::Global<v8::Context>& v8PersistentContext) { | 314 v8::Global<v8::Context>& v8PersistentContext) { |
319 v8::Isolate::Scope isolate_scope(pIsolate); | 315 v8::Isolate::Scope isolate_scope(pIsolate); |
320 v8::Locker locker(pIsolate); | 316 v8::Locker locker(pIsolate); |
321 v8::HandleScope handle_scope(pIsolate); | 317 v8::HandleScope handle_scope(pIsolate); |
322 v8::Local<v8::Context> context = | 318 v8::Local<v8::Context> context = |
323 v8::Local<v8::Context>::New(pIsolate, v8PersistentContext); | 319 v8::Local<v8::Context>::New(pIsolate, v8PersistentContext); |
324 v8::Context::Scope context_scope(context); | 320 v8::Context::Scope context_scope(context); |
325 | 321 |
326 CFX_PtrArray* pArray = (CFX_PtrArray*)pIsolate->GetData(g_embedderDataSlot); | 322 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate); |
327 if (!pArray) | 323 if (!pData) |
328 return; | 324 return; |
329 | 325 |
330 for (int i = 0; i < pArray->GetSize(); i++) { | 326 // XFA, if present, should have already cleaned itself up. |
331 CFXJS_ObjDefintion* pObjDef = (CFXJS_ObjDefintion*)pArray->GetAt(i); | 327 FXSYS_assert(!pData->m_pFXJSERuntimeData); |
| 328 |
| 329 int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); |
| 330 for (int i = 0; i < maxID; ++i) { |
| 331 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i); |
332 if (!pObjDef->m_StaticObj.IsEmpty()) { | 332 if (!pObjDef->m_StaticObj.IsEmpty()) { |
333 v8::Local<v8::Object> pObj = | 333 v8::Local<v8::Object> pObj = |
334 v8::Local<v8::Object>::New(pIsolate, pObjDef->m_StaticObj); | 334 v8::Local<v8::Object>::New(pIsolate, pObjDef->m_StaticObj); |
335 if (pObjDef->m_pDestructor) | 335 if (pObjDef->m_pDestructor) |
336 pObjDef->m_pDestructor(pObj); | 336 pObjDef->m_pDestructor(pObj); |
337 FXJS_FreePrivate(pObj); | 337 FXJS_FreePrivate(pObj); |
338 } | 338 } |
339 delete pObjDef; | 339 delete pObjDef; |
340 } | 340 } |
341 delete pArray; | |
342 | 341 |
343 pIsolate->SetData(1, NULL); | 342 pIsolate->SetData(g_embedderDataSlot, nullptr); |
344 pIsolate->SetData(g_embedderDataSlot, NULL); | 343 delete pData; |
345 // TODO(tsepez): Don't use more than one embedder data slot. | |
346 pIsolate->SetData(2, NULL); | |
347 } | 344 } |
348 | 345 |
349 void FXJS_Initialize(unsigned int embedderDataSlot) { | 346 void FXJS_Initialize(unsigned int embedderDataSlot) { |
350 g_embedderDataSlot = embedderDataSlot; | 347 g_embedderDataSlot = embedderDataSlot; |
351 } | 348 } |
352 | 349 |
353 void FXJS_Release() { | 350 void FXJS_Release() { |
354 } | 351 } |
355 | 352 |
356 int FXJS_Execute(v8::Isolate* pIsolate, | 353 int FXJS_Execute(v8::Isolate* pIsolate, |
(...skipping 26 matching lines...) Expand all Loading... |
383 return -1; | 380 return -1; |
384 } | 381 } |
385 return 0; | 382 return 0; |
386 } | 383 } |
387 | 384 |
388 v8::Local<v8::Object> FXJS_NewFxDynamicObj(v8::Isolate* pIsolate, | 385 v8::Local<v8::Object> FXJS_NewFxDynamicObj(v8::Isolate* pIsolate, |
389 IFXJS_Context* pJSContext, | 386 IFXJS_Context* pJSContext, |
390 int nObjDefnID) { | 387 int nObjDefnID) { |
391 v8::Isolate::Scope isolate_scope(pIsolate); | 388 v8::Isolate::Scope isolate_scope(pIsolate); |
392 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); | 389 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); |
393 if (-1 == nObjDefnID) { | 390 if (nObjDefnID == -1) { |
394 v8::Local<v8::ObjectTemplate> objTempl = v8::ObjectTemplate::New(pIsolate); | 391 v8::Local<v8::ObjectTemplate> objTempl = v8::ObjectTemplate::New(pIsolate); |
395 v8::Local<v8::Object> obj; | 392 v8::Local<v8::Object> obj; |
396 if (objTempl->NewInstance(context).ToLocal(&obj)) | 393 if (objTempl->NewInstance(context).ToLocal(&obj)) |
397 return obj; | 394 return obj; |
398 return v8::Local<v8::Object>(); | 395 return v8::Local<v8::Object>(); |
399 } | 396 } |
400 | 397 |
401 CFX_PtrArray* pArray = (CFX_PtrArray*)pIsolate->GetData(g_embedderDataSlot); | 398 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate); |
402 if (!pArray) | 399 if (!pData) |
403 return v8::Local<v8::Object>(); | 400 return v8::Local<v8::Object>(); |
404 | 401 |
405 if (nObjDefnID < 0 || nObjDefnID >= pArray->GetSize()) | 402 if (nObjDefnID < 0 || nObjDefnID >= CFXJS_ObjDefinition::MaxID(pIsolate)) |
406 return v8::Local<v8::Object>(); | 403 return v8::Local<v8::Object>(); |
407 CFXJS_ObjDefintion* pObjDef = (CFXJS_ObjDefintion*)pArray->GetAt(nObjDefnID); | 404 |
| 405 CFXJS_ObjDefinition* pObjDef = |
| 406 CFXJS_ObjDefinition::ForID(pIsolate, nObjDefnID); |
408 | 407 |
409 v8::Local<v8::ObjectTemplate> objTemp = | 408 v8::Local<v8::ObjectTemplate> objTemp = |
410 v8::Local<v8::ObjectTemplate>::New(pIsolate, pObjDef->m_objTemplate); | 409 v8::Local<v8::ObjectTemplate>::New(pIsolate, pObjDef->m_objTemplate); |
411 v8::Local<v8::Object> obj; | 410 v8::Local<v8::Object> obj; |
412 if (!objTemp->NewInstance(context).ToLocal(&obj)) | 411 if (!objTemp->NewInstance(context).ToLocal(&obj)) |
413 return v8::Local<v8::Object>(); | 412 return v8::Local<v8::Object>(); |
414 | 413 |
415 CFXJS_PrivateData* pPrivateData = new CFXJS_PrivateData; | 414 obj->SetAlignedPointerInInternalField(0, new CFXJS_PrivateData(nObjDefnID)); |
416 pPrivateData->ObjDefID = nObjDefnID; | |
417 | |
418 obj->SetAlignedPointerInInternalField(0, pPrivateData); | |
419 if (pObjDef->m_pConstructor) | 415 if (pObjDef->m_pConstructor) |
420 pObjDef->m_pConstructor( | 416 pObjDef->m_pConstructor( |
421 pJSContext, obj, | 417 pJSContext, obj, |
422 context->Global()->GetPrototype()->ToObject(context).ToLocalChecked()); | 418 context->Global()->GetPrototype()->ToObject(context).ToLocalChecked()); |
423 | 419 |
424 return obj; | 420 return obj; |
425 } | 421 } |
426 | 422 |
427 v8::Local<v8::Object> FXJS_GetThisObj(v8::Isolate* pIsolate) { | 423 v8::Local<v8::Object> FXJS_GetThisObj(v8::Isolate* pIsolate) { |
428 // Return the global object. | |
429 v8::Isolate::Scope isolate_scope(pIsolate); | 424 v8::Isolate::Scope isolate_scope(pIsolate); |
430 CFX_PtrArray* pArray = (CFX_PtrArray*)pIsolate->GetData(g_embedderDataSlot); | 425 if (!FXJS_PerIsolateData::Get(pIsolate)) |
431 if (!pArray) | |
432 return v8::Local<v8::Object>(); | 426 return v8::Local<v8::Object>(); |
433 | 427 |
| 428 // Return the global object. |
434 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); | 429 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); |
435 return context->Global()->GetPrototype()->ToObject(context).ToLocalChecked(); | 430 return context->Global()->GetPrototype()->ToObject(context).ToLocalChecked(); |
436 } | 431 } |
437 | 432 |
438 int FXJS_GetObjDefnID(v8::Local<v8::Object> pObj) { | 433 int FXJS_GetObjDefnID(v8::Local<v8::Object> pObj) { |
439 if (pObj.IsEmpty() || !pObj->InternalFieldCount()) | 434 if (pObj.IsEmpty() || !pObj->InternalFieldCount()) |
440 return -1; | 435 return -1; |
441 CFXJS_PrivateData* pPrivateData = | 436 CFXJS_PrivateData* pPrivateData = |
442 (CFXJS_PrivateData*)pObj->GetAlignedPointerFromInternalField(0); | 437 (CFXJS_PrivateData*)pObj->GetAlignedPointerFromInternalField(0); |
443 if (pPrivateData) | 438 if (pPrivateData) |
444 return pPrivateData->ObjDefID; | 439 return pPrivateData->ObjDefID; |
445 return -1; | 440 return -1; |
446 } | 441 } |
447 | 442 |
448 v8::Isolate* FXJS_GetRuntime(v8::Local<v8::Object> pObj) { | 443 v8::Isolate* FXJS_GetRuntime(v8::Local<v8::Object> pObj) { |
449 if (pObj.IsEmpty()) | 444 if (pObj.IsEmpty()) |
450 return NULL; | 445 return NULL; |
451 v8::Local<v8::Context> context = pObj->CreationContext(); | 446 v8::Local<v8::Context> context = pObj->CreationContext(); |
452 if (context.IsEmpty()) | 447 if (context.IsEmpty()) |
453 return NULL; | 448 return NULL; |
454 return context->GetIsolate(); | 449 return context->GetIsolate(); |
455 } | 450 } |
456 | 451 |
457 int FXJS_GetObjDefnID(v8::Isolate* pIsolate, const wchar_t* pObjName) { | 452 int FXJS_GetObjDefnID(v8::Isolate* pIsolate, const wchar_t* pObjName) { |
458 v8::Isolate::Scope isolate_scope(pIsolate); | 453 v8::Isolate::Scope isolate_scope(pIsolate); |
459 CFX_PtrArray* pArray = (CFX_PtrArray*)pIsolate->GetData(g_embedderDataSlot); | 454 if (!FXJS_PerIsolateData::Get(pIsolate)) |
460 if (!pArray) | |
461 return -1; | 455 return -1; |
462 | 456 |
463 for (int i = 0; i < pArray->GetSize(); i++) { | 457 int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); |
464 CFXJS_ObjDefintion* pObjDef = (CFXJS_ObjDefintion*)pArray->GetAt(i); | 458 for (int i = 0; i < maxID; ++i) { |
| 459 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i); |
465 if (FXSYS_wcscmp(pObjDef->objName, pObjName) == 0) | 460 if (FXSYS_wcscmp(pObjDef->objName, pObjName) == 0) |
466 return i; | 461 return i; |
467 } | 462 } |
468 return -1; | 463 return -1; |
469 } | 464 } |
470 | 465 |
471 void FXJS_Error(v8::Isolate* pIsolate, const CFX_WideString& message) { | 466 void FXJS_Error(v8::Isolate* pIsolate, const CFX_WideString& message) { |
472 // Conversion from pdfium's wchar_t wide-strings to v8's uint16_t | 467 // Conversion from pdfium's wchar_t wide-strings to v8's uint16_t |
473 // wide-strings isn't handled by v8, so use UTF8 as a common | 468 // wide-strings isn't handled by v8, so use UTF8 as a common |
474 // intermediate format. | 469 // intermediate format. |
(...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
768 return v8::Local<v8::Array>(); | 763 return v8::Local<v8::Array>(); |
769 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); | 764 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); |
770 return v8::Local<v8::Array>::Cast(pValue->ToObject(context).ToLocalChecked()); | 765 return v8::Local<v8::Array>::Cast(pValue->ToObject(context).ToLocalChecked()); |
771 } | 766 } |
772 | 767 |
773 void FXJS_ValueCopy(v8::Local<v8::Value>& pTo, v8::Local<v8::Value> pFrom) { | 768 void FXJS_ValueCopy(v8::Local<v8::Value>& pTo, v8::Local<v8::Value> pFrom) { |
774 pTo = pFrom; | 769 pTo = pFrom; |
775 } | 770 } |
776 | 771 |
777 | 772 |
OLD | NEW |