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/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"; |
11 const wchar_t kFXJSValueNameNumber[] = L"number"; | 11 const wchar_t kFXJSValueNameNumber[] = L"number"; |
12 const wchar_t kFXJSValueNameBoolean[] = L"boolean"; | 12 const wchar_t kFXJSValueNameBoolean[] = L"boolean"; |
13 const wchar_t kFXJSValueNameDate[] = L"date"; | 13 const wchar_t kFXJSValueNameDate[] = L"date"; |
14 const wchar_t kFXJSValueNameObject[] = L"object"; | 14 const wchar_t kFXJSValueNameObject[] = L"object"; |
15 const wchar_t kFXJSValueNameFxobj[] = L"fxobj"; | 15 const wchar_t kFXJSValueNameFxobj[] = L"fxobj"; |
16 const wchar_t kFXJSValueNameNull[] = L"null"; | 16 const wchar_t kFXJSValueNameNull[] = L"null"; |
17 const wchar_t kFXJSValueNameUndefined[] = L"undefined"; | 17 const wchar_t kFXJSValueNameUndefined[] = L"undefined"; |
18 | 18 |
19 static unsigned int g_embedderDataSlot = 1u; | 19 static unsigned int g_embedderDataSlot = 1u; |
20 | 20 |
21 // Keep this consistent with the values defined in gin/public/context_holder.h | 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 | 22 // (without actually requiring a dependency on gin itself for the standalone |
23 // embedders of PDFIum). The value we want to use is: | 23 // embedders of PDFIum). The value we want to use is: |
24 // kPerContextDataStartIndex + kEmbedderPDFium, which is 3. | 24 // kPerContextDataStartIndex + kEmbedderPDFium, which is 3. |
25 static const unsigned int kPerContextDataIndex = 3u; | 25 static const unsigned int kPerContextDataIndex = 3u; |
26 | 26 |
27 static v8::Global<v8::ObjectTemplate> g_DefaultGlobalObjectTemplate; | |
28 | |
27 class CFXJS_PrivateData { | 29 class CFXJS_PrivateData { |
28 public: | 30 public: |
29 CFXJS_PrivateData(int nObjDefID) : ObjDefID(nObjDefID), pPrivate(NULL) {} | 31 CFXJS_PrivateData(int nObjDefID) : ObjDefID(nObjDefID), pPrivate(NULL) {} |
30 | 32 |
31 int ObjDefID; | 33 int ObjDefID; |
32 void* pPrivate; | 34 void* pPrivate; |
33 }; | 35 }; |
34 | 36 |
35 class CFXJS_ObjDefinition { | 37 class CFXJS_ObjDefinition { |
36 public: | 38 public: |
37 static int MaxID(v8::Isolate* pIsolate) { | 39 static int MaxID(v8::Isolate* pIsolate) { |
38 return static_cast<int>( | 40 return static_cast<int>( |
39 FXJS_PerIsolateData::Get(pIsolate)->m_ObjectDefnArray.GetSize()); | 41 FXJS_PerIsolateData::Get(pIsolate)->m_ObjectDefnArray.GetSize()); |
40 } | 42 } |
41 static CFXJS_ObjDefinition* ForID(v8::Isolate* pIsolate, int id) { | 43 static CFXJS_ObjDefinition* ForID(v8::Isolate* pIsolate, int id) { |
42 // Note: GetAt() halts if out-of-range even in release builds. | 44 // Note: GetAt() halts if out-of-range even in release builds. |
43 return static_cast<CFXJS_ObjDefinition*>( | 45 return static_cast<CFXJS_ObjDefinition*>( |
44 FXJS_PerIsolateData::Get(pIsolate)->m_ObjectDefnArray.GetAt(id)); | 46 FXJS_PerIsolateData::Get(pIsolate)->m_ObjectDefnArray.GetAt(id)); |
45 } | 47 } |
46 CFXJS_ObjDefinition(v8::Isolate* isolate, | 48 CFXJS_ObjDefinition(v8::Isolate* pIsolate, |
47 const wchar_t* sObjName, | 49 const wchar_t* sObjName, |
48 FXJSOBJTYPE eObjType, | 50 FXJSOBJTYPE eObjType, |
49 FXJS_CONSTRUCTOR pConstructor, | 51 FXJS_CONSTRUCTOR pConstructor, |
50 FXJS_DESTRUCTOR pDestructor) | 52 FXJS_DESTRUCTOR pDestructor) |
51 : objName(sObjName), | 53 : objName(sObjName), |
52 objType(eObjType), | 54 objType(eObjType), |
53 m_pConstructor(pConstructor), | 55 m_pConstructor(pConstructor), |
54 m_pDestructor(pDestructor), | 56 m_pDestructor(pDestructor), |
55 m_bSetAsGlobalObject(FALSE) { | 57 m_bSetAsGlobalObject(FALSE), |
56 v8::Isolate::Scope isolate_scope(isolate); | 58 m_pIsolate(pIsolate) { |
57 v8::HandleScope handle_scope(isolate); | 59 v8::Isolate::Scope isolate_scope(pIsolate); |
60 v8::HandleScope handle_scope(pIsolate); | |
58 | 61 |
59 v8::Local<v8::ObjectTemplate> objTemplate = | 62 v8::Local<v8::FunctionTemplate> fun = v8::FunctionTemplate::New(pIsolate); |
60 v8::ObjectTemplate::New(isolate); | 63 fun->InstanceTemplate()->SetInternalFieldCount(2); |
61 objTemplate->SetInternalFieldCount(2); | 64 fun->PrototypeTemplate()->SetInternalFieldCount(2); |
jochen (gone - plz use gerrit)
2015/09/30 08:30:20
this shouldn't be required (setting it on the inst
| |
62 m_objTemplate.Reset(isolate, objTemplate); | 65 m_FunctionTemplate.Reset(pIsolate, fun); |
63 | 66 |
64 // Document as the global object. | |
65 if (FXSYS_wcscmp(sObjName, L"Document") == 0) { | 67 if (FXSYS_wcscmp(sObjName, L"Document") == 0) { |
66 m_bSetAsGlobalObject = TRUE; | 68 m_bSetAsGlobalObject = TRUE; |
69 } else { | |
70 v8::Local<v8::Signature> sig = v8::Signature::New(pIsolate, fun); | |
jochen (gone - plz use gerrit)
2015/09/29 07:30:47
you appear to put the functions on the prototype (
Tom Sepez
2015/09/29 15:50:34
Hmm. Signature ctor wants a function template, no
jochen (gone - plz use gerrit)
2015/09/30 08:30:20
my bad, ignore this comment
| |
71 m_Signature.Reset(pIsolate, sig); | |
67 } | 72 } |
68 } | 73 } |
69 ~CFXJS_ObjDefinition() { | 74 |
70 m_objTemplate.Reset(); | 75 int AssignID() { |
71 m_StaticObj.Reset(); | 76 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(m_pIsolate); |
77 pData->m_ObjectDefnArray.Add(this); | |
78 return pData->m_ObjectDefnArray.GetSize() - 1; | |
79 } | |
80 | |
81 v8::Local<v8::ObjectTemplate> GetPrototypeTemplate() { | |
jochen (gone - plz use gerrit)
2015/09/30 08:30:20
this method should not be needed - replace all cal
| |
82 v8::EscapableHandleScope scope(m_pIsolate); | |
83 v8::Local<v8::FunctionTemplate> function = | |
84 m_FunctionTemplate.Get(m_pIsolate); | |
85 return scope.Escape(function->PrototypeTemplate()); | |
86 } | |
87 | |
88 v8::Local<v8::ObjectTemplate> GetInstanceTemplate() { | |
89 v8::EscapableHandleScope scope(m_pIsolate); | |
90 v8::Local<v8::FunctionTemplate> function = | |
91 m_FunctionTemplate.Get(m_pIsolate); | |
92 return scope.Escape(function->InstanceTemplate()); | |
93 } | |
94 | |
95 v8::Local<v8::Signature> GetSignature() { | |
96 v8::EscapableHandleScope scope(m_pIsolate); | |
97 return scope.Escape(m_Signature.Get(m_pIsolate)); | |
72 } | 98 } |
73 | 99 |
74 const wchar_t* objName; | 100 const wchar_t* objName; |
75 FXJSOBJTYPE objType; | 101 const FXJSOBJTYPE objType; |
76 FXJS_CONSTRUCTOR m_pConstructor; | 102 const FXJS_CONSTRUCTOR m_pConstructor; |
77 FXJS_DESTRUCTOR m_pDestructor; | 103 const FXJS_DESTRUCTOR m_pDestructor; |
78 FX_BOOL m_bSetAsGlobalObject; | 104 FX_BOOL m_bSetAsGlobalObject; |
79 | 105 |
80 v8::Global<v8::ObjectTemplate> m_objTemplate; | 106 v8::Isolate* m_pIsolate; |
107 v8::Global<v8::FunctionTemplate> m_FunctionTemplate; | |
108 v8::Global<v8::Signature> m_Signature; | |
81 v8::Global<v8::Object> m_StaticObj; | 109 v8::Global<v8::Object> m_StaticObj; |
82 }; | 110 }; |
83 | 111 |
112 static v8::Local<v8::ObjectTemplate> GetGlobalObjectTemplate( | |
113 v8::Isolate* pIsolate) { | |
114 int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); | |
115 for (int i = 0; i < maxID; ++i) { | |
116 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i); | |
117 if (pObjDef->m_bSetAsGlobalObject) | |
118 return pObjDef->GetPrototypeTemplate(); | |
119 } | |
120 | |
121 if (g_DefaultGlobalObjectTemplate.IsEmpty()) | |
122 g_DefaultGlobalObjectTemplate.Reset(pIsolate, | |
123 v8::ObjectTemplate::New(pIsolate)); | |
124 | |
125 return g_DefaultGlobalObjectTemplate.Get(pIsolate); | |
126 } | |
127 | |
84 void* FXJS_ArrayBufferAllocator::Allocate(size_t length) { | 128 void* FXJS_ArrayBufferAllocator::Allocate(size_t length) { |
85 return calloc(1, length); | 129 return calloc(1, length); |
86 } | 130 } |
87 | 131 |
88 void* FXJS_ArrayBufferAllocator::AllocateUninitialized(size_t length) { | 132 void* FXJS_ArrayBufferAllocator::AllocateUninitialized(size_t length) { |
89 return malloc(length); | 133 return malloc(length); |
90 } | 134 } |
91 | 135 |
92 void FXJS_ArrayBufferAllocator::Free(void* data, size_t length) { | 136 void FXJS_ArrayBufferAllocator::Free(void* data, size_t length) { |
93 free(data); | 137 free(data); |
(...skipping 20 matching lines...) Expand all Loading... | |
114 | 158 |
115 int FXJS_DefineObj(v8::Isolate* pIsolate, | 159 int FXJS_DefineObj(v8::Isolate* pIsolate, |
116 const wchar_t* sObjName, | 160 const wchar_t* sObjName, |
117 FXJSOBJTYPE eObjType, | 161 FXJSOBJTYPE eObjType, |
118 FXJS_CONSTRUCTOR pConstructor, | 162 FXJS_CONSTRUCTOR pConstructor, |
119 FXJS_DESTRUCTOR pDestructor) { | 163 FXJS_DESTRUCTOR pDestructor) { |
120 v8::Isolate::Scope isolate_scope(pIsolate); | 164 v8::Isolate::Scope isolate_scope(pIsolate); |
121 v8::HandleScope handle_scope(pIsolate); | 165 v8::HandleScope handle_scope(pIsolate); |
122 | 166 |
123 FXJS_PerIsolateData::SetUp(pIsolate); | 167 FXJS_PerIsolateData::SetUp(pIsolate); |
124 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate); | 168 CFXJS_ObjDefinition* pObjDef = new CFXJS_ObjDefinition( |
125 pData->m_ObjectDefnArray.Add(new CFXJS_ObjDefinition( | 169 pIsolate, sObjName, eObjType, pConstructor, pDestructor); |
126 pIsolate, sObjName, eObjType, pConstructor, pDestructor)); | 170 return pObjDef->AssignID(); |
127 return pData->m_ObjectDefnArray.GetSize() - 1; | |
128 } | 171 } |
129 | 172 |
130 void FXJS_DefineObjMethod(v8::Isolate* pIsolate, | 173 void FXJS_DefineObjMethod(v8::Isolate* pIsolate, |
131 int nObjDefnID, | 174 int nObjDefnID, |
132 const wchar_t* sMethodName, | 175 const wchar_t* sMethodName, |
133 v8::FunctionCallback pMethodCall) { | 176 v8::FunctionCallback pMethodCall) { |
134 v8::Isolate::Scope isolate_scope(pIsolate); | 177 v8::Isolate::Scope isolate_scope(pIsolate); |
135 v8::HandleScope handle_scope(pIsolate); | 178 v8::HandleScope handle_scope(pIsolate); |
136 | |
137 CFX_ByteString bsMethodName = CFX_WideString(sMethodName).UTF8Encode(); | 179 CFX_ByteString bsMethodName = CFX_WideString(sMethodName).UTF8Encode(); |
138 CFXJS_ObjDefinition* pObjDef = | 180 CFXJS_ObjDefinition* pObjDef = |
139 CFXJS_ObjDefinition::ForID(pIsolate, nObjDefnID); | 181 CFXJS_ObjDefinition::ForID(pIsolate, nObjDefnID); |
140 v8::Local<v8::ObjectTemplate> objTemp = | 182 pObjDef->GetPrototypeTemplate()->Set( |
141 v8::Local<v8::ObjectTemplate>::New(pIsolate, pObjDef->m_objTemplate); | |
142 | |
143 objTemp->Set( | |
144 v8::String::NewFromUtf8(pIsolate, bsMethodName.c_str(), | 183 v8::String::NewFromUtf8(pIsolate, bsMethodName.c_str(), |
145 v8::NewStringType::kNormal).ToLocalChecked(), | 184 v8::NewStringType::kNormal).ToLocalChecked(), |
146 v8::FunctionTemplate::New(pIsolate, pMethodCall), v8::ReadOnly); | 185 v8::FunctionTemplate::New(pIsolate, pMethodCall, v8::Local<v8::Value>(), |
147 pObjDef->m_objTemplate.Reset(pIsolate, objTemp); | 186 pObjDef->GetSignature()), |
jochen (gone - plz use gerrit)
2015/09/29 07:30:47
here ^^^
| |
187 v8::ReadOnly); | |
148 } | 188 } |
149 | 189 |
150 void FXJS_DefineObjProperty(v8::Isolate* pIsolate, | 190 void FXJS_DefineObjProperty(v8::Isolate* pIsolate, |
151 int nObjDefnID, | 191 int nObjDefnID, |
152 const wchar_t* sPropName, | 192 const wchar_t* sPropName, |
153 v8::AccessorGetterCallback pPropGet, | 193 v8::AccessorGetterCallback pPropGet, |
154 v8::AccessorSetterCallback pPropPut) { | 194 v8::AccessorSetterCallback pPropPut) { |
155 v8::Isolate::Scope isolate_scope(pIsolate); | 195 v8::Isolate::Scope isolate_scope(pIsolate); |
156 v8::HandleScope handle_scope(pIsolate); | 196 v8::HandleScope handle_scope(pIsolate); |
157 | |
158 CFX_ByteString bsPropertyName = CFX_WideString(sPropName).UTF8Encode(); | 197 CFX_ByteString bsPropertyName = CFX_WideString(sPropName).UTF8Encode(); |
159 CFXJS_ObjDefinition* pObjDef = | 198 CFXJS_ObjDefinition* pObjDef = |
160 CFXJS_ObjDefinition::ForID(pIsolate, nObjDefnID); | 199 CFXJS_ObjDefinition::ForID(pIsolate, nObjDefnID); |
161 v8::Local<v8::ObjectTemplate> objTemp = | 200 pObjDef->GetPrototypeTemplate()->SetAccessor( |
162 v8::Local<v8::ObjectTemplate>::New(pIsolate, pObjDef->m_objTemplate); | |
163 objTemp->SetAccessor( | |
164 v8::String::NewFromUtf8(pIsolate, bsPropertyName.c_str(), | 201 v8::String::NewFromUtf8(pIsolate, bsPropertyName.c_str(), |
165 v8::NewStringType::kNormal).ToLocalChecked(), | 202 v8::NewStringType::kNormal).ToLocalChecked(), |
166 pPropGet, pPropPut); | 203 pPropGet, pPropPut); |
167 pObjDef->m_objTemplate.Reset(pIsolate, objTemp); | |
168 } | 204 } |
169 | 205 |
170 void FXJS_DefineObjAllProperties(v8::Isolate* pIsolate, | 206 void FXJS_DefineObjAllProperties(v8::Isolate* pIsolate, |
171 int nObjDefnID, | 207 int nObjDefnID, |
172 v8::NamedPropertyQueryCallback pPropQurey, | 208 v8::NamedPropertyQueryCallback pPropQurey, |
173 v8::NamedPropertyGetterCallback pPropGet, | 209 v8::NamedPropertyGetterCallback pPropGet, |
174 v8::NamedPropertySetterCallback pPropPut, | 210 v8::NamedPropertySetterCallback pPropPut, |
175 v8::NamedPropertyDeleterCallback pPropDel) { | 211 v8::NamedPropertyDeleterCallback pPropDel) { |
176 v8::Isolate::Scope isolate_scope(pIsolate); | 212 v8::Isolate::Scope isolate_scope(pIsolate); |
177 v8::HandleScope handle_scope(pIsolate); | 213 v8::HandleScope handle_scope(pIsolate); |
178 | |
179 CFXJS_ObjDefinition* pObjDef = | 214 CFXJS_ObjDefinition* pObjDef = |
180 CFXJS_ObjDefinition::ForID(pIsolate, nObjDefnID); | 215 CFXJS_ObjDefinition::ForID(pIsolate, nObjDefnID); |
181 v8::Local<v8::ObjectTemplate> objTemp = | 216 pObjDef->GetPrototypeTemplate()->SetNamedPropertyHandler( |
182 v8::Local<v8::ObjectTemplate>::New(pIsolate, pObjDef->m_objTemplate); | 217 pPropGet, pPropPut, pPropQurey, pPropDel); |
183 objTemp->SetNamedPropertyHandler(pPropGet, pPropPut, pPropQurey, pPropDel); | |
184 pObjDef->m_objTemplate.Reset(pIsolate, objTemp); | |
185 } | 218 } |
186 | 219 |
187 void FXJS_DefineObjConst(v8::Isolate* pIsolate, | 220 void FXJS_DefineObjConst(v8::Isolate* pIsolate, |
188 int nObjDefnID, | 221 int nObjDefnID, |
189 const wchar_t* sConstName, | 222 const wchar_t* sConstName, |
190 v8::Local<v8::Value> pDefault) { | 223 v8::Local<v8::Value> pDefault) { |
191 v8::Isolate::Scope isolate_scope(pIsolate); | 224 v8::Isolate::Scope isolate_scope(pIsolate); |
192 v8::HandleScope handle_scope(pIsolate); | 225 v8::HandleScope handle_scope(pIsolate); |
193 | |
194 CFX_ByteString bsConstName = CFX_WideString(sConstName).UTF8Encode(); | 226 CFX_ByteString bsConstName = CFX_WideString(sConstName).UTF8Encode(); |
195 CFXJS_ObjDefinition* pObjDef = | 227 CFXJS_ObjDefinition* pObjDef = |
196 CFXJS_ObjDefinition::ForID(pIsolate, nObjDefnID); | 228 CFXJS_ObjDefinition::ForID(pIsolate, nObjDefnID); |
197 v8::Local<v8::ObjectTemplate> objTemp = | 229 pObjDef->GetPrototypeTemplate()->Set(pIsolate, bsConstName.c_str(), pDefault); |
198 v8::Local<v8::ObjectTemplate>::New(pIsolate, pObjDef->m_objTemplate); | |
199 objTemp->Set(pIsolate, bsConstName.c_str(), pDefault); | |
200 pObjDef->m_objTemplate.Reset(pIsolate, objTemp); | |
201 } | |
202 | |
203 static v8::Global<v8::ObjectTemplate>& _getGlobalObjectTemplate( | |
204 v8::Isolate* pIsolate) { | |
205 v8::Isolate::Scope isolate_scope(pIsolate); | |
206 v8::HandleScope handle_scope(pIsolate); | |
207 | |
208 int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); | |
209 for (int i = 0; i < maxID; ++i) { | |
210 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i); | |
211 if (pObjDef->m_bSetAsGlobalObject) | |
212 return pObjDef->m_objTemplate; | |
213 } | |
214 static v8::Global<v8::ObjectTemplate> gloabalObjectTemplate; | |
215 return gloabalObjectTemplate; | |
216 } | 230 } |
217 | 231 |
218 void FXJS_DefineGlobalMethod(v8::Isolate* pIsolate, | 232 void FXJS_DefineGlobalMethod(v8::Isolate* pIsolate, |
219 const wchar_t* sMethodName, | 233 const wchar_t* sMethodName, |
220 v8::FunctionCallback pMethodCall) { | 234 v8::FunctionCallback pMethodCall) { |
221 v8::Isolate::Scope isolate_scope(pIsolate); | 235 v8::Isolate::Scope isolate_scope(pIsolate); |
222 v8::HandleScope handle_scope(pIsolate); | 236 v8::HandleScope handle_scope(pIsolate); |
223 | |
224 CFX_ByteString bsMethodName = CFX_WideString(sMethodName).UTF8Encode(); | 237 CFX_ByteString bsMethodName = CFX_WideString(sMethodName).UTF8Encode(); |
225 v8::Local<v8::FunctionTemplate> funTempl = | 238 GetGlobalObjectTemplate(pIsolate)->Set( |
226 v8::FunctionTemplate::New(pIsolate, pMethodCall); | |
227 v8::Local<v8::ObjectTemplate> objTemp; | |
228 | |
229 v8::Global<v8::ObjectTemplate>& globalObjTemp = | |
230 _getGlobalObjectTemplate(pIsolate); | |
231 if (globalObjTemp.IsEmpty()) | |
232 objTemp = v8::ObjectTemplate::New(pIsolate); | |
233 else | |
234 objTemp = v8::Local<v8::ObjectTemplate>::New(pIsolate, globalObjTemp); | |
235 objTemp->Set( | |
236 v8::String::NewFromUtf8(pIsolate, bsMethodName.c_str(), | 239 v8::String::NewFromUtf8(pIsolate, bsMethodName.c_str(), |
237 v8::NewStringType::kNormal).ToLocalChecked(), | 240 v8::NewStringType::kNormal).ToLocalChecked(), |
238 funTempl, v8::ReadOnly); | 241 v8::FunctionTemplate::New(pIsolate, pMethodCall), v8::ReadOnly); |
239 | |
240 globalObjTemp.Reset(pIsolate, objTemp); | |
241 } | 242 } |
242 | 243 |
243 void FXJS_DefineGlobalConst(v8::Isolate* pIsolate, | 244 void FXJS_DefineGlobalConst(v8::Isolate* pIsolate, |
244 const wchar_t* sConstName, | 245 const wchar_t* sConstName, |
245 v8::Local<v8::Value> pDefault) { | 246 v8::Local<v8::Value> pDefault) { |
246 v8::Isolate::Scope isolate_scope(pIsolate); | 247 v8::Isolate::Scope isolate_scope(pIsolate); |
247 v8::HandleScope handle_scope(pIsolate); | 248 v8::HandleScope handle_scope(pIsolate); |
248 | 249 CFX_ByteString bsConst = CFX_WideString(sConstName).UTF8Encode(); |
249 CFX_WideString ws = CFX_WideString(sConstName); | 250 GetGlobalObjectTemplate(pIsolate)->Set( |
250 CFX_ByteString bsConst = ws.UTF8Encode(); | |
251 | |
252 v8::Local<v8::ObjectTemplate> objTemp; | |
253 | |
254 v8::Global<v8::ObjectTemplate>& globalObjTemp = | |
255 _getGlobalObjectTemplate(pIsolate); | |
256 if (globalObjTemp.IsEmpty()) | |
257 objTemp = v8::ObjectTemplate::New(pIsolate); | |
258 else | |
259 objTemp = v8::Local<v8::ObjectTemplate>::New(pIsolate, globalObjTemp); | |
260 objTemp->Set( | |
261 v8::String::NewFromUtf8(pIsolate, bsConst.c_str(), | 251 v8::String::NewFromUtf8(pIsolate, bsConst.c_str(), |
262 v8::NewStringType::kNormal).ToLocalChecked(), | 252 v8::NewStringType::kNormal).ToLocalChecked(), |
263 pDefault, v8::ReadOnly); | 253 pDefault, v8::ReadOnly); |
264 | |
265 globalObjTemp.Reset(pIsolate, objTemp); | |
266 } | 254 } |
267 | 255 |
268 void FXJS_InitializeRuntime(v8::Isolate* pIsolate, | 256 void FXJS_InitializeRuntime(v8::Isolate* pIsolate, |
269 IFXJS_Runtime* pFXRuntime, | 257 IFXJS_Runtime* pFXRuntime, |
270 IFXJS_Context* context, | 258 IFXJS_Context* context, |
271 v8::Global<v8::Context>& v8PersistentContext) { | 259 v8::Global<v8::Context>& v8PersistentContext) { |
272 v8::Isolate::Scope isolate_scope(pIsolate); | 260 v8::Isolate::Scope isolate_scope(pIsolate); |
273 v8::HandleScope handle_scope(pIsolate); | 261 v8::HandleScope handle_scope(pIsolate); |
274 | 262 v8::Local<v8::Context> v8Context = |
275 v8::Global<v8::ObjectTemplate>& globalObjTemp = | 263 v8::Context::New(pIsolate, NULL, GetGlobalObjectTemplate(pIsolate)); |
276 _getGlobalObjectTemplate(pIsolate); | |
277 v8::Local<v8::Context> v8Context = v8::Context::New( | |
278 pIsolate, NULL, | |
279 v8::Local<v8::ObjectTemplate>::New(pIsolate, globalObjTemp)); | |
280 v8::Context::Scope context_scope(v8Context); | 264 v8::Context::Scope context_scope(v8Context); |
281 | 265 |
282 FXJS_PerIsolateData::SetUp(pIsolate); | 266 FXJS_PerIsolateData::SetUp(pIsolate); |
283 v8Context->SetAlignedPointerInEmbedderData(kPerContextDataIndex, pFXRuntime); | 267 v8Context->SetAlignedPointerInEmbedderData(kPerContextDataIndex, pFXRuntime); |
284 | 268 |
285 int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); | 269 int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); |
286 for (int i = 0; i < maxID; ++i) { | 270 for (int i = 0; i < maxID; ++i) { |
287 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i); | 271 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i); |
288 CFX_WideString ws = CFX_WideString(pObjDef->objName); | 272 CFX_WideString ws = CFX_WideString(pObjDef->objName); |
289 CFX_ByteString bs = ws.UTF8Encode(); | 273 CFX_ByteString bs = ws.UTF8Encode(); |
290 v8::Local<v8::String> objName = | 274 v8::Local<v8::String> objName = |
291 v8::String::NewFromUtf8(pIsolate, bs.c_str(), | 275 v8::String::NewFromUtf8(pIsolate, bs.c_str(), |
292 v8::NewStringType::kNormal, | 276 v8::NewStringType::kNormal, |
293 bs.GetLength()).ToLocalChecked(); | 277 bs.GetLength()).ToLocalChecked(); |
294 | 278 |
295 if (pObjDef->objType == FXJS_DYNAMIC) { | 279 if (pObjDef->objType == FXJS_DYNAMIC) { |
296 // Document is set as global object, need to construct it first. | 280 if (pObjDef->m_bSetAsGlobalObject) { |
297 if (ws.Equal(L"Document")) { | |
298 v8Context->Global() | 281 v8Context->Global() |
299 ->GetPrototype() | 282 ->GetPrototype() |
300 ->ToObject(v8Context) | 283 ->ToObject(v8Context) |
301 .ToLocalChecked() | 284 .ToLocalChecked() |
302 ->SetAlignedPointerInInternalField(0, new CFXJS_PrivateData(i)); | 285 ->SetAlignedPointerInInternalField(0, new CFXJS_PrivateData(i)); |
303 | 286 |
304 if (pObjDef->m_pConstructor) | 287 if (pObjDef->m_pConstructor) |
305 pObjDef->m_pConstructor(context, v8Context->Global() | 288 pObjDef->m_pConstructor(context, v8Context->Global() |
306 ->GetPrototype() | 289 ->GetPrototype() |
307 ->ToObject(v8Context) | 290 ->ToObject(v8Context) |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
355 context->GetAlignedPointerFromEmbedderData(kPerContextDataIndex)); | 338 context->GetAlignedPointerFromEmbedderData(kPerContextDataIndex)); |
356 } | 339 } |
357 | 340 |
358 int FXJS_Execute(v8::Isolate* pIsolate, | 341 int FXJS_Execute(v8::Isolate* pIsolate, |
359 IFXJS_Context* pJSContext, | 342 IFXJS_Context* pJSContext, |
360 const wchar_t* script, | 343 const wchar_t* script, |
361 long length, | 344 long length, |
362 FXJSErr* pError) { | 345 FXJSErr* pError) { |
363 v8::Isolate::Scope isolate_scope(pIsolate); | 346 v8::Isolate::Scope isolate_scope(pIsolate); |
364 v8::TryCatch try_catch(pIsolate); | 347 v8::TryCatch try_catch(pIsolate); |
365 | 348 CFX_ByteString bsScript = CFX_WideString(script).UTF8Encode(); |
366 CFX_WideString wsScript(script); | |
367 CFX_ByteString bsScript = wsScript.UTF8Encode(); | |
368 | |
369 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); | 349 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); |
370 v8::Local<v8::Script> compiled_script; | 350 v8::Local<v8::Script> compiled_script; |
371 if (!v8::Script::Compile( | 351 if (!v8::Script::Compile( |
372 context, v8::String::NewFromUtf8( | 352 context, v8::String::NewFromUtf8( |
373 pIsolate, bsScript.c_str(), v8::NewStringType::kNormal, | 353 pIsolate, bsScript.c_str(), v8::NewStringType::kNormal, |
374 bsScript.GetLength()).ToLocalChecked()) | 354 bsScript.GetLength()).ToLocalChecked()) |
375 .ToLocal(&compiled_script)) { | 355 .ToLocal(&compiled_script)) { |
376 v8::String::Utf8Value error(try_catch.Exception()); | 356 v8::String::Utf8Value error(try_catch.Exception()); |
377 // TODO(tsepez): return error via pError->message. | 357 // TODO(tsepez): return error via pError->message. |
378 return -1; | 358 return -1; |
379 } | 359 } |
380 | 360 |
381 v8::Local<v8::Value> result; | 361 v8::Local<v8::Value> result; |
382 if (!compiled_script->Run(context).ToLocal(&result)) { | 362 if (!compiled_script->Run(context).ToLocal(&result)) { |
383 v8::String::Utf8Value error(try_catch.Exception()); | 363 v8::String::Utf8Value error(try_catch.Exception()); |
384 // TODO(tsepez): return error via pError->message. | 364 // TODO(tsepez): return error via pError->message. |
385 return -1; | 365 return -1; |
386 } | 366 } |
387 return 0; | 367 return 0; |
388 } | 368 } |
389 | 369 |
390 v8::Local<v8::Object> FXJS_NewFxDynamicObj(v8::Isolate* pIsolate, | 370 v8::Local<v8::Object> FXJS_NewFxDynamicObj(v8::Isolate* pIsolate, |
391 IFXJS_Context* pJSContext, | 371 IFXJS_Context* pJSContext, |
392 int nObjDefnID) { | 372 int nObjDefnID) { |
393 v8::Isolate::Scope isolate_scope(pIsolate); | 373 v8::Isolate::Scope isolate_scope(pIsolate); |
394 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); | 374 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); |
395 if (nObjDefnID == -1) { | 375 if (nObjDefnID == -1) { |
396 v8::Local<v8::ObjectTemplate> objTempl = v8::ObjectTemplate::New(pIsolate); | 376 v8::Local<v8::ObjectTemplate> objTempl = v8::ObjectTemplate::New(pIsolate); |
397 v8::Local<v8::Object> obj; | 377 v8::Local<v8::Object> obj; |
398 if (objTempl->NewInstance(context).ToLocal(&obj)) | 378 if (!objTempl->NewInstance(context).ToLocal(&obj)) |
399 return obj; | 379 return v8::Local<v8::Object>(); |
400 return v8::Local<v8::Object>(); | 380 return obj; |
401 } | 381 } |
402 | 382 |
403 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate); | 383 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate); |
404 if (!pData) | 384 if (!pData) |
405 return v8::Local<v8::Object>(); | 385 return v8::Local<v8::Object>(); |
406 | 386 |
407 if (nObjDefnID < 0 || nObjDefnID >= CFXJS_ObjDefinition::MaxID(pIsolate)) | 387 if (nObjDefnID < 0 || nObjDefnID >= CFXJS_ObjDefinition::MaxID(pIsolate)) |
408 return v8::Local<v8::Object>(); | 388 return v8::Local<v8::Object>(); |
409 | 389 |
410 CFXJS_ObjDefinition* pObjDef = | 390 CFXJS_ObjDefinition* pObjDef = |
411 CFXJS_ObjDefinition::ForID(pIsolate, nObjDefnID); | 391 CFXJS_ObjDefinition::ForID(pIsolate, nObjDefnID); |
412 | |
413 v8::Local<v8::ObjectTemplate> objTemp = | |
414 v8::Local<v8::ObjectTemplate>::New(pIsolate, pObjDef->m_objTemplate); | |
415 v8::Local<v8::Object> obj; | 392 v8::Local<v8::Object> obj; |
416 if (!objTemp->NewInstance(context).ToLocal(&obj)) | 393 if (!pObjDef->GetInstanceTemplate()->NewInstance(context).ToLocal(&obj)) |
417 return v8::Local<v8::Object>(); | 394 return v8::Local<v8::Object>(); |
418 | 395 |
419 obj->SetAlignedPointerInInternalField(0, new CFXJS_PrivateData(nObjDefnID)); | 396 obj->SetAlignedPointerInInternalField(0, new CFXJS_PrivateData(nObjDefnID)); |
420 if (pObjDef->m_pConstructor) | 397 if (pObjDef->m_pConstructor) |
421 pObjDef->m_pConstructor( | 398 pObjDef->m_pConstructor( |
422 pJSContext, obj, | 399 pJSContext, obj, |
423 context->Global()->GetPrototype()->ToObject(context).ToLocalChecked()); | 400 context->Global()->GetPrototype()->ToObject(context).ToLocalChecked()); |
424 | 401 |
425 return obj; | 402 return obj; |
426 } | 403 } |
(...skipping 341 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
768 return v8::Local<v8::Array>(); | 745 return v8::Local<v8::Array>(); |
769 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); | 746 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); |
770 return v8::Local<v8::Array>::Cast(pValue->ToObject(context).ToLocalChecked()); | 747 return v8::Local<v8::Array>::Cast(pValue->ToObject(context).ToLocalChecked()); |
771 } | 748 } |
772 | 749 |
773 void FXJS_ValueCopy(v8::Local<v8::Value>& pTo, v8::Local<v8::Value> pFrom) { | 750 void FXJS_ValueCopy(v8::Local<v8::Value>& pTo, v8::Local<v8::Value> pFrom) { |
774 pTo = pFrom; | 751 pTo = pFrom; |
775 } | 752 } |
776 | 753 |
777 | 754 |
OLD | NEW |