| 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* isolate, |
| 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), |
| 58 m_pIsolate(isolate) { |
| 56 v8::Isolate::Scope isolate_scope(isolate); | 59 v8::Isolate::Scope isolate_scope(isolate); |
| 57 v8::HandleScope handle_scope(isolate); | 60 v8::HandleScope handle_scope(isolate); |
| 58 | 61 |
| 59 v8::Local<v8::ObjectTemplate> objTemplate = | 62 v8::Local<v8::FunctionTemplate> fun = v8::FunctionTemplate::New(isolate); |
| 60 v8::ObjectTemplate::New(isolate); | 63 fun->InstanceTemplate()->SetInternalFieldCount(2); |
| 61 objTemplate->SetInternalFieldCount(2); | 64 m_FunctionTemplate.Reset(isolate, fun); |
| 62 m_objTemplate.Reset(isolate, objTemplate); | 65 |
| 66 v8::Local<v8::Signature> sig = v8::Signature::New(isolate, fun); |
| 67 m_Signature.Reset(isolate, sig); |
| 63 | 68 |
| 64 // Document as the global object. | 69 // Document as the global object. |
| 65 if (FXSYS_wcscmp(sObjName, L"Document") == 0) { | 70 if (FXSYS_wcscmp(sObjName, L"Document") == 0) { |
| 66 m_bSetAsGlobalObject = TRUE; | 71 m_bSetAsGlobalObject = TRUE; |
| 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> GetInstanceTemplate() { |
| 82 v8::EscapableHandleScope scope(m_pIsolate); |
| 83 v8::Local<v8::FunctionTemplate> function = |
| 84 m_FunctionTemplate.Get(m_pIsolate); |
| 85 return scope.Escape(function->InstanceTemplate()); |
| 86 } |
| 87 |
| 88 v8::Local<v8::Signature> GetSignature() { |
| 89 v8::EscapableHandleScope scope(m_pIsolate); |
| 90 return scope.Escape(m_Signature.Get(m_pIsolate)); |
| 72 } | 91 } |
| 73 | 92 |
| 74 const wchar_t* objName; | 93 const wchar_t* objName; |
| 75 FXJSOBJTYPE objType; | 94 const FXJSOBJTYPE objType; |
| 76 FXJS_CONSTRUCTOR m_pConstructor; | 95 const FXJS_CONSTRUCTOR m_pConstructor; |
| 77 FXJS_DESTRUCTOR m_pDestructor; | 96 const FXJS_DESTRUCTOR m_pDestructor; |
| 78 FX_BOOL m_bSetAsGlobalObject; | 97 FX_BOOL m_bSetAsGlobalObject; |
| 79 | 98 |
| 80 v8::Global<v8::ObjectTemplate> m_objTemplate; | 99 v8::Isolate* m_pIsolate; |
| 100 v8::Global<v8::FunctionTemplate> m_FunctionTemplate; |
| 101 v8::Global<v8::Signature> m_Signature; |
| 81 v8::Global<v8::Object> m_StaticObj; | 102 v8::Global<v8::Object> m_StaticObj; |
| 82 }; | 103 }; |
| 83 | 104 |
| 105 static v8::Local<v8::ObjectTemplate> GetGlobalObjectTemplate( |
| 106 v8::Isolate* pIsolate) { |
| 107 int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); |
| 108 for (int i = 0; i < maxID; ++i) { |
| 109 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i); |
| 110 if (pObjDef->m_bSetAsGlobalObject) |
| 111 return pObjDef->GetInstanceTemplate(); |
| 112 } |
| 113 |
| 114 if (g_DefaultGlobalObjectTemplate.IsEmpty()) |
| 115 g_DefaultGlobalObjectTemplate.Reset(pIsolate, |
| 116 v8::ObjectTemplate::New(pIsolate)); |
| 117 |
| 118 return g_DefaultGlobalObjectTemplate.Get(pIsolate); |
| 119 } |
| 120 |
| 84 void* FXJS_ArrayBufferAllocator::Allocate(size_t length) { | 121 void* FXJS_ArrayBufferAllocator::Allocate(size_t length) { |
| 85 return calloc(1, length); | 122 return calloc(1, length); |
| 86 } | 123 } |
| 87 | 124 |
| 88 void* FXJS_ArrayBufferAllocator::AllocateUninitialized(size_t length) { | 125 void* FXJS_ArrayBufferAllocator::AllocateUninitialized(size_t length) { |
| 89 return malloc(length); | 126 return malloc(length); |
| 90 } | 127 } |
| 91 | 128 |
| 92 void FXJS_ArrayBufferAllocator::Free(void* data, size_t length) { | 129 void FXJS_ArrayBufferAllocator::Free(void* data, size_t length) { |
| 93 free(data); | 130 free(data); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 114 | 151 |
| 115 int FXJS_DefineObj(v8::Isolate* pIsolate, | 152 int FXJS_DefineObj(v8::Isolate* pIsolate, |
| 116 const wchar_t* sObjName, | 153 const wchar_t* sObjName, |
| 117 FXJSOBJTYPE eObjType, | 154 FXJSOBJTYPE eObjType, |
| 118 FXJS_CONSTRUCTOR pConstructor, | 155 FXJS_CONSTRUCTOR pConstructor, |
| 119 FXJS_DESTRUCTOR pDestructor) { | 156 FXJS_DESTRUCTOR pDestructor) { |
| 120 v8::Isolate::Scope isolate_scope(pIsolate); | 157 v8::Isolate::Scope isolate_scope(pIsolate); |
| 121 v8::HandleScope handle_scope(pIsolate); | 158 v8::HandleScope handle_scope(pIsolate); |
| 122 | 159 |
| 123 FXJS_PerIsolateData::SetUp(pIsolate); | 160 FXJS_PerIsolateData::SetUp(pIsolate); |
| 124 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate); | 161 CFXJS_ObjDefinition* pObjDef = new CFXJS_ObjDefinition( |
| 125 pData->m_ObjectDefnArray.Add(new CFXJS_ObjDefinition( | 162 pIsolate, sObjName, eObjType, pConstructor, pDestructor); |
| 126 pIsolate, sObjName, eObjType, pConstructor, pDestructor)); | 163 return pObjDef->AssignID(); |
| 127 return pData->m_ObjectDefnArray.GetSize() - 1; | |
| 128 } | 164 } |
| 129 | 165 |
| 130 void FXJS_DefineObjMethod(v8::Isolate* pIsolate, | 166 void FXJS_DefineObjMethod(v8::Isolate* pIsolate, |
| 131 int nObjDefnID, | 167 int nObjDefnID, |
| 132 const wchar_t* sMethodName, | 168 const wchar_t* sMethodName, |
| 133 v8::FunctionCallback pMethodCall) { | 169 v8::FunctionCallback pMethodCall) { |
| 134 v8::Isolate::Scope isolate_scope(pIsolate); | 170 v8::Isolate::Scope isolate_scope(pIsolate); |
| 135 v8::HandleScope handle_scope(pIsolate); | 171 v8::HandleScope handle_scope(pIsolate); |
| 136 | |
| 137 CFX_ByteString bsMethodName = CFX_WideString(sMethodName).UTF8Encode(); | 172 CFX_ByteString bsMethodName = CFX_WideString(sMethodName).UTF8Encode(); |
| 138 CFXJS_ObjDefinition* pObjDef = | 173 CFXJS_ObjDefinition* pObjDef = |
| 139 CFXJS_ObjDefinition::ForID(pIsolate, nObjDefnID); | 174 CFXJS_ObjDefinition::ForID(pIsolate, nObjDefnID); |
| 140 v8::Local<v8::ObjectTemplate> objTemp = | 175 pObjDef->GetInstanceTemplate()->Set( |
| 141 v8::Local<v8::ObjectTemplate>::New(pIsolate, pObjDef->m_objTemplate); | |
| 142 | |
| 143 objTemp->Set( | |
| 144 v8::String::NewFromUtf8(pIsolate, bsMethodName.c_str(), | 176 v8::String::NewFromUtf8(pIsolate, bsMethodName.c_str(), |
| 145 v8::NewStringType::kNormal).ToLocalChecked(), | 177 v8::NewStringType::kNormal).ToLocalChecked(), |
| 146 v8::FunctionTemplate::New(pIsolate, pMethodCall), v8::ReadOnly); | 178 v8::FunctionTemplate::New(pIsolate, pMethodCall, v8::Local<v8::Value>(), |
| 147 pObjDef->m_objTemplate.Reset(pIsolate, objTemp); | 179 pObjDef->GetSignature()), |
| 180 v8::ReadOnly); |
| 148 } | 181 } |
| 149 | 182 |
| 150 void FXJS_DefineObjProperty(v8::Isolate* pIsolate, | 183 void FXJS_DefineObjProperty(v8::Isolate* pIsolate, |
| 151 int nObjDefnID, | 184 int nObjDefnID, |
| 152 const wchar_t* sPropName, | 185 const wchar_t* sPropName, |
| 153 v8::AccessorGetterCallback pPropGet, | 186 v8::AccessorGetterCallback pPropGet, |
| 154 v8::AccessorSetterCallback pPropPut) { | 187 v8::AccessorSetterCallback pPropPut) { |
| 155 v8::Isolate::Scope isolate_scope(pIsolate); | 188 v8::Isolate::Scope isolate_scope(pIsolate); |
| 156 v8::HandleScope handle_scope(pIsolate); | 189 v8::HandleScope handle_scope(pIsolate); |
| 157 | |
| 158 CFX_ByteString bsPropertyName = CFX_WideString(sPropName).UTF8Encode(); | 190 CFX_ByteString bsPropertyName = CFX_WideString(sPropName).UTF8Encode(); |
| 159 CFXJS_ObjDefinition* pObjDef = | 191 CFXJS_ObjDefinition* pObjDef = |
| 160 CFXJS_ObjDefinition::ForID(pIsolate, nObjDefnID); | 192 CFXJS_ObjDefinition::ForID(pIsolate, nObjDefnID); |
| 161 v8::Local<v8::ObjectTemplate> objTemp = | 193 pObjDef->GetInstanceTemplate()->SetAccessor( |
| 162 v8::Local<v8::ObjectTemplate>::New(pIsolate, pObjDef->m_objTemplate); | |
| 163 objTemp->SetAccessor( | |
| 164 v8::String::NewFromUtf8(pIsolate, bsPropertyName.c_str(), | 194 v8::String::NewFromUtf8(pIsolate, bsPropertyName.c_str(), |
| 165 v8::NewStringType::kNormal).ToLocalChecked(), | 195 v8::NewStringType::kNormal).ToLocalChecked(), |
| 166 pPropGet, pPropPut); | 196 pPropGet, pPropPut); |
| 167 pObjDef->m_objTemplate.Reset(pIsolate, objTemp); | |
| 168 } | 197 } |
| 169 | 198 |
| 170 void FXJS_DefineObjAllProperties(v8::Isolate* pIsolate, | 199 void FXJS_DefineObjAllProperties(v8::Isolate* pIsolate, |
| 171 int nObjDefnID, | 200 int nObjDefnID, |
| 172 v8::NamedPropertyQueryCallback pPropQurey, | 201 v8::NamedPropertyQueryCallback pPropQurey, |
| 173 v8::NamedPropertyGetterCallback pPropGet, | 202 v8::NamedPropertyGetterCallback pPropGet, |
| 174 v8::NamedPropertySetterCallback pPropPut, | 203 v8::NamedPropertySetterCallback pPropPut, |
| 175 v8::NamedPropertyDeleterCallback pPropDel) { | 204 v8::NamedPropertyDeleterCallback pPropDel) { |
| 176 v8::Isolate::Scope isolate_scope(pIsolate); | 205 v8::Isolate::Scope isolate_scope(pIsolate); |
| 177 v8::HandleScope handle_scope(pIsolate); | 206 v8::HandleScope handle_scope(pIsolate); |
| 178 | |
| 179 CFXJS_ObjDefinition* pObjDef = | 207 CFXJS_ObjDefinition* pObjDef = |
| 180 CFXJS_ObjDefinition::ForID(pIsolate, nObjDefnID); | 208 CFXJS_ObjDefinition::ForID(pIsolate, nObjDefnID); |
| 181 v8::Local<v8::ObjectTemplate> objTemp = | 209 pObjDef->GetInstanceTemplate()->SetNamedPropertyHandler(pPropGet, pPropPut, |
| 182 v8::Local<v8::ObjectTemplate>::New(pIsolate, pObjDef->m_objTemplate); | 210 pPropQurey, pPropDel); |
| 183 objTemp->SetNamedPropertyHandler(pPropGet, pPropPut, pPropQurey, pPropDel); | |
| 184 pObjDef->m_objTemplate.Reset(pIsolate, objTemp); | |
| 185 } | 211 } |
| 186 | 212 |
| 187 void FXJS_DefineObjConst(v8::Isolate* pIsolate, | 213 void FXJS_DefineObjConst(v8::Isolate* pIsolate, |
| 188 int nObjDefnID, | 214 int nObjDefnID, |
| 189 const wchar_t* sConstName, | 215 const wchar_t* sConstName, |
| 190 v8::Local<v8::Value> pDefault) { | 216 v8::Local<v8::Value> pDefault) { |
| 191 v8::Isolate::Scope isolate_scope(pIsolate); | 217 v8::Isolate::Scope isolate_scope(pIsolate); |
| 192 v8::HandleScope handle_scope(pIsolate); | 218 v8::HandleScope handle_scope(pIsolate); |
| 193 | |
| 194 CFX_ByteString bsConstName = CFX_WideString(sConstName).UTF8Encode(); | 219 CFX_ByteString bsConstName = CFX_WideString(sConstName).UTF8Encode(); |
| 195 CFXJS_ObjDefinition* pObjDef = | 220 CFXJS_ObjDefinition* pObjDef = |
| 196 CFXJS_ObjDefinition::ForID(pIsolate, nObjDefnID); | 221 CFXJS_ObjDefinition::ForID(pIsolate, nObjDefnID); |
| 197 v8::Local<v8::ObjectTemplate> objTemp = | 222 pObjDef->GetInstanceTemplate()->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 } | 223 } |
| 217 | 224 |
| 218 void FXJS_DefineGlobalMethod(v8::Isolate* pIsolate, | 225 void FXJS_DefineGlobalMethod(v8::Isolate* pIsolate, |
| 219 const wchar_t* sMethodName, | 226 const wchar_t* sMethodName, |
| 220 v8::FunctionCallback pMethodCall) { | 227 v8::FunctionCallback pMethodCall) { |
| 221 v8::Isolate::Scope isolate_scope(pIsolate); | 228 v8::Isolate::Scope isolate_scope(pIsolate); |
| 222 v8::HandleScope handle_scope(pIsolate); | 229 v8::HandleScope handle_scope(pIsolate); |
| 223 | |
| 224 CFX_ByteString bsMethodName = CFX_WideString(sMethodName).UTF8Encode(); | 230 CFX_ByteString bsMethodName = CFX_WideString(sMethodName).UTF8Encode(); |
| 225 v8::Local<v8::FunctionTemplate> funTempl = | 231 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(), | 232 v8::String::NewFromUtf8(pIsolate, bsMethodName.c_str(), |
| 237 v8::NewStringType::kNormal).ToLocalChecked(), | 233 v8::NewStringType::kNormal).ToLocalChecked(), |
| 238 funTempl, v8::ReadOnly); | 234 v8::FunctionTemplate::New(pIsolate, pMethodCall), v8::ReadOnly); |
| 239 | |
| 240 globalObjTemp.Reset(pIsolate, objTemp); | |
| 241 } | 235 } |
| 242 | 236 |
| 243 void FXJS_DefineGlobalConst(v8::Isolate* pIsolate, | 237 void FXJS_DefineGlobalConst(v8::Isolate* pIsolate, |
| 244 const wchar_t* sConstName, | 238 const wchar_t* sConstName, |
| 245 v8::Local<v8::Value> pDefault) { | 239 v8::Local<v8::Value> pDefault) { |
| 246 v8::Isolate::Scope isolate_scope(pIsolate); | 240 v8::Isolate::Scope isolate_scope(pIsolate); |
| 247 v8::HandleScope handle_scope(pIsolate); | 241 v8::HandleScope handle_scope(pIsolate); |
| 248 | 242 CFX_ByteString bsConst = CFX_WideString(sConstName).UTF8Encode(); |
| 249 CFX_WideString ws = CFX_WideString(sConstName); | 243 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(), | 244 v8::String::NewFromUtf8(pIsolate, bsConst.c_str(), |
| 262 v8::NewStringType::kNormal).ToLocalChecked(), | 245 v8::NewStringType::kNormal).ToLocalChecked(), |
| 263 pDefault, v8::ReadOnly); | 246 pDefault, v8::ReadOnly); |
| 264 | |
| 265 globalObjTemp.Reset(pIsolate, objTemp); | |
| 266 } | 247 } |
| 267 | 248 |
| 268 void FXJS_InitializeRuntime(v8::Isolate* pIsolate, | 249 void FXJS_InitializeRuntime(v8::Isolate* pIsolate, |
| 269 IFXJS_Runtime* pFXRuntime, | 250 IFXJS_Runtime* pFXRuntime, |
| 270 IFXJS_Context* context, | 251 IFXJS_Context* context, |
| 271 v8::Global<v8::Context>& v8PersistentContext) { | 252 v8::Global<v8::Context>& v8PersistentContext) { |
| 272 v8::Isolate::Scope isolate_scope(pIsolate); | 253 v8::Isolate::Scope isolate_scope(pIsolate); |
| 273 v8::HandleScope handle_scope(pIsolate); | 254 v8::HandleScope handle_scope(pIsolate); |
| 274 | 255 v8::Local<v8::Context> v8Context = |
| 275 v8::Global<v8::ObjectTemplate>& globalObjTemp = | 256 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); | 257 v8::Context::Scope context_scope(v8Context); |
| 281 | 258 |
| 282 FXJS_PerIsolateData::SetUp(pIsolate); | 259 FXJS_PerIsolateData::SetUp(pIsolate); |
| 283 v8Context->SetAlignedPointerInEmbedderData(kPerContextDataIndex, pFXRuntime); | 260 v8Context->SetAlignedPointerInEmbedderData(kPerContextDataIndex, pFXRuntime); |
| 284 | 261 |
| 285 int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); | 262 int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); |
| 286 for (int i = 0; i < maxID; ++i) { | 263 for (int i = 0; i < maxID; ++i) { |
| 287 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i); | 264 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i); |
| 288 CFX_WideString ws = CFX_WideString(pObjDef->objName); | 265 CFX_WideString ws = CFX_WideString(pObjDef->objName); |
| 289 CFX_ByteString bs = ws.UTF8Encode(); | 266 CFX_ByteString bs = ws.UTF8Encode(); |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 355 context->GetAlignedPointerFromEmbedderData(kPerContextDataIndex)); | 332 context->GetAlignedPointerFromEmbedderData(kPerContextDataIndex)); |
| 356 } | 333 } |
| 357 | 334 |
| 358 int FXJS_Execute(v8::Isolate* pIsolate, | 335 int FXJS_Execute(v8::Isolate* pIsolate, |
| 359 IFXJS_Context* pJSContext, | 336 IFXJS_Context* pJSContext, |
| 360 const wchar_t* script, | 337 const wchar_t* script, |
| 361 long length, | 338 long length, |
| 362 FXJSErr* pError) { | 339 FXJSErr* pError) { |
| 363 v8::Isolate::Scope isolate_scope(pIsolate); | 340 v8::Isolate::Scope isolate_scope(pIsolate); |
| 364 v8::TryCatch try_catch(pIsolate); | 341 v8::TryCatch try_catch(pIsolate); |
| 365 | 342 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(); | 343 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); |
| 370 v8::Local<v8::Script> compiled_script; | 344 v8::Local<v8::Script> compiled_script; |
| 371 if (!v8::Script::Compile( | 345 if (!v8::Script::Compile( |
| 372 context, v8::String::NewFromUtf8( | 346 context, v8::String::NewFromUtf8( |
| 373 pIsolate, bsScript.c_str(), v8::NewStringType::kNormal, | 347 pIsolate, bsScript.c_str(), v8::NewStringType::kNormal, |
| 374 bsScript.GetLength()).ToLocalChecked()) | 348 bsScript.GetLength()).ToLocalChecked()) |
| 375 .ToLocal(&compiled_script)) { | 349 .ToLocal(&compiled_script)) { |
| 376 v8::String::Utf8Value error(try_catch.Exception()); | 350 v8::String::Utf8Value error(try_catch.Exception()); |
| 377 // TODO(tsepez): return error via pError->message. | 351 // TODO(tsepez): return error via pError->message. |
| 378 return -1; | 352 return -1; |
| 379 } | 353 } |
| 380 | 354 |
| 381 v8::Local<v8::Value> result; | 355 v8::Local<v8::Value> result; |
| 382 if (!compiled_script->Run(context).ToLocal(&result)) { | 356 if (!compiled_script->Run(context).ToLocal(&result)) { |
| 383 v8::String::Utf8Value error(try_catch.Exception()); | 357 v8::String::Utf8Value error(try_catch.Exception()); |
| 384 // TODO(tsepez): return error via pError->message. | 358 // TODO(tsepez): return error via pError->message. |
| 385 return -1; | 359 return -1; |
| 386 } | 360 } |
| 387 return 0; | 361 return 0; |
| 388 } | 362 } |
| 389 | 363 |
| 390 v8::Local<v8::Object> FXJS_NewFxDynamicObj(v8::Isolate* pIsolate, | 364 v8::Local<v8::Object> FXJS_NewFxDynamicObj(v8::Isolate* pIsolate, |
| 391 IFXJS_Context* pJSContext, | 365 IFXJS_Context* pJSContext, |
| 392 int nObjDefnID) { | 366 int nObjDefnID) { |
| 393 v8::Isolate::Scope isolate_scope(pIsolate); | 367 v8::Isolate::Scope isolate_scope(pIsolate); |
| 394 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); | 368 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); |
| 395 if (nObjDefnID == -1) { | 369 if (nObjDefnID == -1) { |
| 396 v8::Local<v8::ObjectTemplate> objTempl = v8::ObjectTemplate::New(pIsolate); | 370 v8::Local<v8::ObjectTemplate> objTempl = v8::ObjectTemplate::New(pIsolate); |
| 397 v8::Local<v8::Object> obj; | 371 v8::Local<v8::Object> obj; |
| 398 if (objTempl->NewInstance(context).ToLocal(&obj)) | 372 if (!objTempl->NewInstance(context).ToLocal(&obj)) |
| 399 return obj; | 373 return v8::Local<v8::Object>(); |
| 400 return v8::Local<v8::Object>(); | 374 return obj; |
| 401 } | 375 } |
| 402 | 376 |
| 403 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate); | 377 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(pIsolate); |
| 404 if (!pData) | 378 if (!pData) |
| 405 return v8::Local<v8::Object>(); | 379 return v8::Local<v8::Object>(); |
| 406 | 380 |
| 407 if (nObjDefnID < 0 || nObjDefnID >= CFXJS_ObjDefinition::MaxID(pIsolate)) | 381 if (nObjDefnID < 0 || nObjDefnID >= CFXJS_ObjDefinition::MaxID(pIsolate)) |
| 408 return v8::Local<v8::Object>(); | 382 return v8::Local<v8::Object>(); |
| 409 | 383 |
| 410 CFXJS_ObjDefinition* pObjDef = | 384 CFXJS_ObjDefinition* pObjDef = |
| 411 CFXJS_ObjDefinition::ForID(pIsolate, nObjDefnID); | 385 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; | 386 v8::Local<v8::Object> obj; |
| 416 if (!objTemp->NewInstance(context).ToLocal(&obj)) | 387 if (!pObjDef->GetInstanceTemplate()->NewInstance(context).ToLocal(&obj)) |
| 417 return v8::Local<v8::Object>(); | 388 return v8::Local<v8::Object>(); |
| 418 | 389 |
| 419 obj->SetAlignedPointerInInternalField(0, new CFXJS_PrivateData(nObjDefnID)); | 390 obj->SetAlignedPointerInInternalField(0, new CFXJS_PrivateData(nObjDefnID)); |
| 420 if (pObjDef->m_pConstructor) | 391 if (pObjDef->m_pConstructor) |
| 421 pObjDef->m_pConstructor( | 392 pObjDef->m_pConstructor( |
| 422 pJSContext, obj, | 393 pJSContext, obj, |
| 423 context->Global()->GetPrototype()->ToObject(context).ToLocalChecked()); | 394 context->Global()->GetPrototype()->ToObject(context).ToLocalChecked()); |
| 424 | 395 |
| 425 return obj; | 396 return obj; |
| 426 } | 397 } |
| (...skipping 341 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 768 return v8::Local<v8::Array>(); | 739 return v8::Local<v8::Array>(); |
| 769 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); | 740 v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); |
| 770 return v8::Local<v8::Array>::Cast(pValue->ToObject(context).ToLocalChecked()); | 741 return v8::Local<v8::Array>::Cast(pValue->ToObject(context).ToLocalChecked()); |
| 771 } | 742 } |
| 772 | 743 |
| 773 void FXJS_ValueCopy(v8::Local<v8::Value>& pTo, v8::Local<v8::Value> pFrom) { | 744 void FXJS_ValueCopy(v8::Local<v8::Value>& pTo, v8::Local<v8::Value> pFrom) { |
| 774 pTo = pFrom; | 745 pTo = pFrom; |
| 775 } | 746 } |
| 776 | 747 |
| 777 | 748 |
| OLD | NEW |