John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1 | // Copyright 2014 PDFium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 4 | |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 5 | // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | |
dsinclair | 4355468 | 2016-09-29 17:29:48 -0700 | [diff] [blame] | 7 | #include "fxjs/fxjs_v8.h" |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 8 | |
Dan Sinclair | 3ebd121 | 2016-03-09 09:59:23 -0500 | [diff] [blame] | 9 | #include <vector> |
| 10 | |
dsinclair | a52ab74 | 2016-09-29 13:59:29 -0700 | [diff] [blame] | 11 | #include "core/fxcrt/fx_basic.h" |
Lei Zhang | 4d4a442 | 2015-10-08 12:00:14 -0700 | [diff] [blame] | 12 | |
Tom Sepez | bd7fabf | 2015-09-28 10:31:27 -0700 | [diff] [blame] | 13 | // Keep this consistent with the values defined in gin/public/context_holder.h |
| 14 | // (without actually requiring a dependency on gin itself for the standalone |
| 15 | // embedders of PDFIum). The value we want to use is: |
| 16 | // kPerContextDataStartIndex + kEmbedderPDFium, which is 3. |
| 17 | static const unsigned int kPerContextDataIndex = 3u; |
Tom Sepez | a72e8e2 | 2015-10-07 10:17:53 -0700 | [diff] [blame] | 18 | static unsigned int g_embedderDataSlot = 1u; |
| 19 | static v8::Isolate* g_isolate = nullptr; |
Lei Zhang | 4d4a442 | 2015-10-08 12:00:14 -0700 | [diff] [blame] | 20 | static size_t g_isolate_ref_count = 0; |
Tom Sepez | a72e8e2 | 2015-10-07 10:17:53 -0700 | [diff] [blame] | 21 | static FXJS_ArrayBufferAllocator* g_arrayBufferAllocator = nullptr; |
Tom Sepez | 8b315b6 | 2015-10-02 08:59:57 -0700 | [diff] [blame] | 22 | static v8::Global<v8::ObjectTemplate>* g_DefaultGlobalObjectTemplate = nullptr; |
Tom Sepez | 3f72fb4 | 2017-02-27 11:43:55 -0800 | [diff] [blame] | 23 | static wchar_t kPerObjectDataTag[] = L"CFXJS_PerObjectData"; |
Tom Sepez | 016a347 | 2015-09-30 15:50:57 -0700 | [diff] [blame] | 24 | |
Tom Sepez | 6cf117c | 2015-11-06 14:52:10 -0800 | [diff] [blame] | 25 | class CFXJS_PerObjectData { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 26 | public: |
Lei Zhang | d88a364 | 2015-11-10 09:38:57 -0800 | [diff] [blame] | 27 | explicit CFXJS_PerObjectData(int nObjDefID) |
Tom Sepez | 6cf117c | 2015-11-06 14:52:10 -0800 | [diff] [blame] | 28 | : m_ObjDefID(nObjDefID), m_pPrivate(nullptr) {} |
Tom Sepez | ed7b2b5 | 2015-09-22 08:36:17 -0700 | [diff] [blame] | 29 | |
Tom Sepez | 3f72fb4 | 2017-02-27 11:43:55 -0800 | [diff] [blame] | 30 | static void SetInObject(CFXJS_PerObjectData* pData, |
| 31 | v8::Local<v8::Object> pObj) { |
| 32 | if (pObj->InternalFieldCount() == 2) { |
| 33 | pObj->SetAlignedPointerInInternalField(0, pData); |
| 34 | pObj->SetAlignedPointerInInternalField( |
| 35 | 1, static_cast<void*>(kPerObjectDataTag)); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | static CFXJS_PerObjectData* GetFromObject(v8::Local<v8::Object> pObj) { |
| 40 | if (pObj.IsEmpty() || pObj->InternalFieldCount() != 2 || |
| 41 | pObj->GetAlignedPointerFromInternalField(1) != |
| 42 | static_cast<void*>(kPerObjectDataTag)) { |
| 43 | return nullptr; |
| 44 | } |
| 45 | return static_cast<CFXJS_PerObjectData*>( |
| 46 | pObj->GetAlignedPointerFromInternalField(0)); |
| 47 | } |
| 48 | |
Lei Zhang | d88a364 | 2015-11-10 09:38:57 -0800 | [diff] [blame] | 49 | const int m_ObjDefID; |
Tom Sepez | 6cf117c | 2015-11-06 14:52:10 -0800 | [diff] [blame] | 50 | void* m_pPrivate; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 51 | }; |
| 52 | |
Tom Sepez | ed7b2b5 | 2015-09-22 08:36:17 -0700 | [diff] [blame] | 53 | class CFXJS_ObjDefinition { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 54 | public: |
Tom Sepez | ed7b2b5 | 2015-09-22 08:36:17 -0700 | [diff] [blame] | 55 | static int MaxID(v8::Isolate* pIsolate) { |
Lei Zhang | d88a364 | 2015-11-10 09:38:57 -0800 | [diff] [blame] | 56 | return FXJS_PerIsolateData::Get(pIsolate)->m_ObjectDefnArray.size(); |
Tom Sepez | ed7b2b5 | 2015-09-22 08:36:17 -0700 | [diff] [blame] | 57 | } |
Lei Zhang | d88a364 | 2015-11-10 09:38:57 -0800 | [diff] [blame] | 58 | |
Tom Sepez | ed7b2b5 | 2015-09-22 08:36:17 -0700 | [diff] [blame] | 59 | static CFXJS_ObjDefinition* ForID(v8::Isolate* pIsolate, int id) { |
| 60 | // Note: GetAt() halts if out-of-range even in release builds. |
weili | a4ad595 | 2016-09-22 10:38:53 -0700 | [diff] [blame] | 61 | return FXJS_PerIsolateData::Get(pIsolate)->m_ObjectDefnArray[id].get(); |
Tom Sepez | ed7b2b5 | 2015-09-22 08:36:17 -0700 | [diff] [blame] | 62 | } |
Lei Zhang | d88a364 | 2015-11-10 09:38:57 -0800 | [diff] [blame] | 63 | |
Tom Sepez | ed7b2b5 | 2015-09-22 08:36:17 -0700 | [diff] [blame] | 64 | CFXJS_ObjDefinition(v8::Isolate* isolate, |
Tom Sepez | 892d751 | 2017-02-21 13:57:13 -0800 | [diff] [blame] | 65 | const char* sObjName, |
Tom Sepez | ed7b2b5 | 2015-09-22 08:36:17 -0700 | [diff] [blame] | 66 | FXJSOBJTYPE eObjType, |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 67 | CFXJS_Engine::Constructor pConstructor, |
| 68 | CFXJS_Engine::Destructor pDestructor) |
Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame] | 69 | : m_ObjName(sObjName), |
| 70 | m_ObjType(eObjType), |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 71 | m_pConstructor(pConstructor), |
| 72 | m_pDestructor(pDestructor), |
Tom Sepez | 016a347 | 2015-09-30 15:50:57 -0700 | [diff] [blame] | 73 | m_pIsolate(isolate) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 74 | v8::Isolate::Scope isolate_scope(isolate); |
| 75 | v8::HandleScope handle_scope(isolate); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 76 | |
Tom Sepez | 016a347 | 2015-09-30 15:50:57 -0700 | [diff] [blame] | 77 | v8::Local<v8::FunctionTemplate> fun = v8::FunctionTemplate::New(isolate); |
| 78 | fun->InstanceTemplate()->SetInternalFieldCount(2); |
Tom Sepez | c5a1472 | 2017-02-24 15:31:12 -0800 | [diff] [blame] | 79 | fun->SetCallHandler([](const v8::FunctionCallbackInfo<v8::Value>& info) { |
| 80 | v8::Local<v8::Object> holder = info.Holder(); |
| 81 | ASSERT(holder->InternalFieldCount() == 2); |
| 82 | holder->SetAlignedPointerInInternalField(0, nullptr); |
| 83 | holder->SetAlignedPointerInInternalField(1, nullptr); |
| 84 | }); |
jochen | 7e6a848 | 2016-07-06 11:02:27 -0700 | [diff] [blame] | 85 | if (eObjType == FXJSOBJTYPE_GLOBAL) { |
| 86 | fun->InstanceTemplate()->Set( |
| 87 | v8::Symbol::GetToStringTag(isolate), |
| 88 | v8::String::NewFromUtf8(isolate, "global", v8::NewStringType::kNormal) |
| 89 | .ToLocalChecked()); |
| 90 | } |
Tom Sepez | 016a347 | 2015-09-30 15:50:57 -0700 | [diff] [blame] | 91 | m_FunctionTemplate.Reset(isolate, fun); |
| 92 | |
| 93 | v8::Local<v8::Signature> sig = v8::Signature::New(isolate, fun); |
| 94 | m_Signature.Reset(isolate, sig); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 95 | } |
Tom Sepez | 016a347 | 2015-09-30 15:50:57 -0700 | [diff] [blame] | 96 | |
| 97 | int AssignID() { |
| 98 | FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(m_pIsolate); |
weili | a4ad595 | 2016-09-22 10:38:53 -0700 | [diff] [blame] | 99 | pData->m_ObjectDefnArray.emplace_back(this); |
Lei Zhang | d88a364 | 2015-11-10 09:38:57 -0800 | [diff] [blame] | 100 | return pData->m_ObjectDefnArray.size() - 1; |
Tom Sepez | 016a347 | 2015-09-30 15:50:57 -0700 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | v8::Local<v8::ObjectTemplate> GetInstanceTemplate() { |
| 104 | v8::EscapableHandleScope scope(m_pIsolate); |
| 105 | v8::Local<v8::FunctionTemplate> function = |
| 106 | m_FunctionTemplate.Get(m_pIsolate); |
| 107 | return scope.Escape(function->InstanceTemplate()); |
| 108 | } |
| 109 | |
| 110 | v8::Local<v8::Signature> GetSignature() { |
| 111 | v8::EscapableHandleScope scope(m_pIsolate); |
| 112 | return scope.Escape(m_Signature.Get(m_pIsolate)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 113 | } |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 114 | |
Tom Sepez | 892d751 | 2017-02-21 13:57:13 -0800 | [diff] [blame] | 115 | const char* const m_ObjName; |
Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame] | 116 | const FXJSOBJTYPE m_ObjType; |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 117 | const CFXJS_Engine::Constructor m_pConstructor; |
| 118 | const CFXJS_Engine::Destructor m_pDestructor; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 119 | |
Tom Sepez | 016a347 | 2015-09-30 15:50:57 -0700 | [diff] [blame] | 120 | v8::Isolate* m_pIsolate; |
| 121 | v8::Global<v8::FunctionTemplate> m_FunctionTemplate; |
| 122 | v8::Global<v8::Signature> m_Signature; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 123 | }; |
| 124 | |
Tom Sepez | 016a347 | 2015-09-30 15:50:57 -0700 | [diff] [blame] | 125 | static v8::Local<v8::ObjectTemplate> GetGlobalObjectTemplate( |
| 126 | v8::Isolate* pIsolate) { |
| 127 | int maxID = CFXJS_ObjDefinition::MaxID(pIsolate); |
| 128 | for (int i = 0; i < maxID; ++i) { |
| 129 | CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i); |
Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame] | 130 | if (pObjDef->m_ObjType == FXJSOBJTYPE_GLOBAL) |
Tom Sepez | 016a347 | 2015-09-30 15:50:57 -0700 | [diff] [blame] | 131 | return pObjDef->GetInstanceTemplate(); |
| 132 | } |
Tom Sepez | 8b315b6 | 2015-10-02 08:59:57 -0700 | [diff] [blame] | 133 | if (!g_DefaultGlobalObjectTemplate) { |
jochen | 7e6a848 | 2016-07-06 11:02:27 -0700 | [diff] [blame] | 134 | v8::Local<v8::ObjectTemplate> hGlobalTemplate = |
| 135 | v8::ObjectTemplate::New(pIsolate); |
| 136 | hGlobalTemplate->Set( |
| 137 | v8::Symbol::GetToStringTag(pIsolate), |
| 138 | v8::String::NewFromUtf8(pIsolate, "global", v8::NewStringType::kNormal) |
| 139 | .ToLocalChecked()); |
| 140 | g_DefaultGlobalObjectTemplate = |
| 141 | new v8::Global<v8::ObjectTemplate>(pIsolate, hGlobalTemplate); |
Tom Sepez | 8b315b6 | 2015-10-02 08:59:57 -0700 | [diff] [blame] | 142 | } |
| 143 | return g_DefaultGlobalObjectTemplate->Get(pIsolate); |
Tom Sepez | 016a347 | 2015-09-30 15:50:57 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 146 | void* FXJS_ArrayBufferAllocator::Allocate(size_t length) { |
Tom Sepez | 7d0fcbf | 2015-09-15 15:30:34 -0700 | [diff] [blame] | 147 | return calloc(1, length); |
| 148 | } |
| 149 | |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 150 | void* FXJS_ArrayBufferAllocator::AllocateUninitialized(size_t length) { |
Tom Sepez | 7d0fcbf | 2015-09-15 15:30:34 -0700 | [diff] [blame] | 151 | return malloc(length); |
| 152 | } |
| 153 | |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 154 | void FXJS_ArrayBufferAllocator::Free(void* data, size_t length) { |
Tom Sepez | 7d0fcbf | 2015-09-15 15:30:34 -0700 | [diff] [blame] | 155 | free(data); |
| 156 | } |
| 157 | |
jinming_wang | 61dc96f | 2016-01-28 14:39:56 +0800 | [diff] [blame] | 158 | void V8TemplateMapTraits::Dispose(v8::Isolate* isolate, |
| 159 | v8::Global<v8::Object> value, |
| 160 | void* key) { |
| 161 | v8::Local<v8::Object> obj = value.Get(isolate); |
| 162 | if (obj.IsEmpty()) |
| 163 | return; |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 164 | CFXJS_Engine* pEngine = CFXJS_Engine::CurrentEngineFromIsolate(isolate); |
| 165 | int id = pEngine->GetObjDefnID(obj); |
jinming_wang | 61dc96f | 2016-01-28 14:39:56 +0800 | [diff] [blame] | 166 | if (id == -1) |
| 167 | return; |
jinming_wang | 61dc96f | 2016-01-28 14:39:56 +0800 | [diff] [blame] | 168 | CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(isolate, id); |
| 169 | if (!pObjDef) |
| 170 | return; |
| 171 | if (pObjDef->m_pDestructor) |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 172 | pObjDef->m_pDestructor(pEngine, obj); |
| 173 | CFXJS_Engine::FreeObjectPrivate(obj); |
jinming_wang | 61dc96f | 2016-01-28 14:39:56 +0800 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | V8TemplateMapTraits::MapType* V8TemplateMapTraits::MapFromWeakCallbackInfo( |
| 177 | const v8::WeakCallbackInfo<WeakCallbackDataType>& data) { |
| 178 | V8TemplateMap* pMap = |
weili | a4ad595 | 2016-09-22 10:38:53 -0700 | [diff] [blame] | 179 | (FXJS_PerIsolateData::Get(data.GetIsolate()))->m_pDynamicObjsMap.get(); |
jinming_wang | 61dc96f | 2016-01-28 14:39:56 +0800 | [diff] [blame] | 180 | return pMap ? &pMap->m_map : nullptr; |
| 181 | } |
| 182 | |
Tom Sepez | a72e8e2 | 2015-10-07 10:17:53 -0700 | [diff] [blame] | 183 | void FXJS_Initialize(unsigned int embedderDataSlot, v8::Isolate* pIsolate) { |
Lei Zhang | 4d4a442 | 2015-10-08 12:00:14 -0700 | [diff] [blame] | 184 | if (g_isolate) { |
| 185 | ASSERT(g_embedderDataSlot == embedderDataSlot); |
| 186 | ASSERT(g_isolate == pIsolate); |
| 187 | return; |
| 188 | } |
Tom Sepez | a72e8e2 | 2015-10-07 10:17:53 -0700 | [diff] [blame] | 189 | g_embedderDataSlot = embedderDataSlot; |
| 190 | g_isolate = pIsolate; |
| 191 | } |
| 192 | |
| 193 | void FXJS_Release() { |
Lei Zhang | 4d4a442 | 2015-10-08 12:00:14 -0700 | [diff] [blame] | 194 | ASSERT(!g_isolate || g_isolate_ref_count == 0); |
Lei Zhang | b391493 | 2015-10-08 12:08:47 -0700 | [diff] [blame] | 195 | delete g_DefaultGlobalObjectTemplate; |
Tom Sepez | a72e8e2 | 2015-10-07 10:17:53 -0700 | [diff] [blame] | 196 | g_DefaultGlobalObjectTemplate = nullptr; |
| 197 | g_isolate = nullptr; |
| 198 | |
| 199 | delete g_arrayBufferAllocator; |
| 200 | g_arrayBufferAllocator = nullptr; |
| 201 | } |
| 202 | |
| 203 | bool FXJS_GetIsolate(v8::Isolate** pResultIsolate) { |
| 204 | if (g_isolate) { |
| 205 | *pResultIsolate = g_isolate; |
| 206 | return false; |
| 207 | } |
| 208 | // Provide backwards compatibility when no external isolate. |
| 209 | if (!g_arrayBufferAllocator) |
| 210 | g_arrayBufferAllocator = new FXJS_ArrayBufferAllocator(); |
| 211 | v8::Isolate::CreateParams params; |
| 212 | params.array_buffer_allocator = g_arrayBufferAllocator; |
| 213 | *pResultIsolate = v8::Isolate::New(params); |
| 214 | return true; |
| 215 | } |
| 216 | |
Lei Zhang | 3fa115b | 2015-10-08 12:04:47 -0700 | [diff] [blame] | 217 | size_t FXJS_GlobalIsolateRefCount() { |
| 218 | return g_isolate_ref_count; |
| 219 | } |
| 220 | |
weili | 625ad66 | 2016-06-15 11:21:33 -0700 | [diff] [blame] | 221 | V8TemplateMap::V8TemplateMap(v8::Isolate* isolate) : m_map(isolate) {} |
| 222 | |
| 223 | V8TemplateMap::~V8TemplateMap() {} |
| 224 | |
| 225 | void V8TemplateMap::set(void* key, v8::Local<v8::Object> handle) { |
| 226 | ASSERT(!m_map.Contains(key)); |
| 227 | m_map.Set(key, handle); |
| 228 | } |
| 229 | |
| 230 | FXJS_PerIsolateData::~FXJS_PerIsolateData() {} |
| 231 | |
Tom Sepez | ed7b2b5 | 2015-09-22 08:36:17 -0700 | [diff] [blame] | 232 | // static |
| 233 | void FXJS_PerIsolateData::SetUp(v8::Isolate* pIsolate) { |
Tom Sepez | 7d0fcbf | 2015-09-15 15:30:34 -0700 | [diff] [blame] | 234 | if (!pIsolate->GetData(g_embedderDataSlot)) |
weili | a4ad595 | 2016-09-22 10:38:53 -0700 | [diff] [blame] | 235 | pIsolate->SetData(g_embedderDataSlot, new FXJS_PerIsolateData(pIsolate)); |
Tom Sepez | ed7b2b5 | 2015-09-22 08:36:17 -0700 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | // static |
| 239 | FXJS_PerIsolateData* FXJS_PerIsolateData::Get(v8::Isolate* pIsolate) { |
| 240 | return static_cast<FXJS_PerIsolateData*>( |
| 241 | pIsolate->GetData(g_embedderDataSlot)); |
Tom Sepez | 7d0fcbf | 2015-09-15 15:30:34 -0700 | [diff] [blame] | 242 | } |
| 243 | |
weili | a4ad595 | 2016-09-22 10:38:53 -0700 | [diff] [blame] | 244 | FXJS_PerIsolateData::FXJS_PerIsolateData(v8::Isolate* pIsolate) |
| 245 | : m_pDynamicObjsMap(new V8TemplateMap(pIsolate)) {} |
weili | 625ad66 | 2016-06-15 11:21:33 -0700 | [diff] [blame] | 246 | |
tsepez | a494191 | 2016-08-15 11:40:12 -0700 | [diff] [blame] | 247 | CFXJS_Engine::CFXJS_Engine() : m_isolate(nullptr) {} |
tsepez | a494191 | 2016-08-15 11:40:12 -0700 | [diff] [blame] | 248 | |
weili | 0b2a987 | 2016-09-21 11:50:43 -0700 | [diff] [blame] | 249 | CFXJS_Engine::CFXJS_Engine(v8::Isolate* pIsolate) : m_isolate(pIsolate) {} |
| 250 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 251 | CFXJS_Engine::~CFXJS_Engine() { |
| 252 | m_V8PersistentContext.Reset(); |
| 253 | } |
Tom Sepez | 7d0fcbf | 2015-09-15 15:30:34 -0700 | [diff] [blame] | 254 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 255 | // static |
| 256 | CFXJS_Engine* CFXJS_Engine::CurrentEngineFromIsolate(v8::Isolate* pIsolate) { |
| 257 | return static_cast<CFXJS_Engine*>( |
| 258 | pIsolate->GetCurrentContext()->GetAlignedPointerFromEmbedderData( |
| 259 | kPerContextDataIndex)); |
| 260 | } |
| 261 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 262 | // static |
| 263 | int CFXJS_Engine::GetObjDefnID(v8::Local<v8::Object> pObj) { |
Tom Sepez | 3f72fb4 | 2017-02-27 11:43:55 -0800 | [diff] [blame] | 264 | CFXJS_PerObjectData* pData = CFXJS_PerObjectData::GetFromObject(pObj); |
| 265 | return pData ? pData->m_ObjDefID : -1; |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | // static |
| 269 | void CFXJS_Engine::FreeObjectPrivate(void* pPerObjectData) { |
| 270 | delete static_cast<CFXJS_PerObjectData*>(pPerObjectData); |
| 271 | } |
| 272 | |
| 273 | // static |
| 274 | void CFXJS_Engine::FreeObjectPrivate(v8::Local<v8::Object> pObj) { |
Tom Sepez | 3f72fb4 | 2017-02-27 11:43:55 -0800 | [diff] [blame] | 275 | CFXJS_PerObjectData* pData = CFXJS_PerObjectData::GetFromObject(pObj); |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 276 | pObj->SetAlignedPointerInInternalField(0, nullptr); |
Tom Sepez | 3f72fb4 | 2017-02-27 11:43:55 -0800 | [diff] [blame] | 277 | pObj->SetAlignedPointerInInternalField(1, nullptr); |
| 278 | delete pData; |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 279 | } |
| 280 | |
Tom Sepez | 892d751 | 2017-02-21 13:57:13 -0800 | [diff] [blame] | 281 | int CFXJS_Engine::DefineObj(const char* sObjName, |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 282 | FXJSOBJTYPE eObjType, |
| 283 | CFXJS_Engine::Constructor pConstructor, |
| 284 | CFXJS_Engine::Destructor pDestructor) { |
| 285 | v8::Isolate::Scope isolate_scope(m_isolate); |
| 286 | v8::HandleScope handle_scope(m_isolate); |
| 287 | FXJS_PerIsolateData::SetUp(m_isolate); |
Tom Sepez | 016a347 | 2015-09-30 15:50:57 -0700 | [diff] [blame] | 288 | CFXJS_ObjDefinition* pObjDef = new CFXJS_ObjDefinition( |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 289 | m_isolate, sObjName, eObjType, pConstructor, pDestructor); |
Tom Sepez | 016a347 | 2015-09-30 15:50:57 -0700 | [diff] [blame] | 290 | return pObjDef->AssignID(); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 291 | } |
| 292 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 293 | void CFXJS_Engine::DefineObjMethod(int nObjDefnID, |
Tom Sepez | 9b99b63 | 2017-02-21 15:05:57 -0800 | [diff] [blame] | 294 | const char* sMethodName, |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 295 | v8::FunctionCallback pMethodCall) { |
| 296 | v8::Isolate::Scope isolate_scope(m_isolate); |
| 297 | v8::HandleScope handle_scope(m_isolate); |
Tom Sepez | ed7b2b5 | 2015-09-22 08:36:17 -0700 | [diff] [blame] | 298 | CFXJS_ObjDefinition* pObjDef = |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 299 | CFXJS_ObjDefinition::ForID(m_isolate, nObjDefnID); |
jochen | c4dedf3 | 2016-07-06 05:26:23 -0700 | [diff] [blame] | 300 | v8::Local<v8::FunctionTemplate> fun = v8::FunctionTemplate::New( |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 301 | m_isolate, pMethodCall, v8::Local<v8::Value>(), pObjDef->GetSignature()); |
jochen | c4dedf3 | 2016-07-06 05:26:23 -0700 | [diff] [blame] | 302 | fun->RemovePrototype(); |
Tom Sepez | 016a347 | 2015-09-30 15:50:57 -0700 | [diff] [blame] | 303 | pObjDef->GetInstanceTemplate()->Set( |
Tom Sepez | 9b99b63 | 2017-02-21 15:05:57 -0800 | [diff] [blame] | 304 | v8::String::NewFromUtf8(m_isolate, sMethodName, |
Dan Sinclair | f766ad2 | 2016-03-14 13:51:24 -0400 | [diff] [blame] | 305 | v8::NewStringType::kNormal) |
| 306 | .ToLocalChecked(), |
jochen | c4dedf3 | 2016-07-06 05:26:23 -0700 | [diff] [blame] | 307 | fun, v8::ReadOnly); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 308 | } |
| 309 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 310 | void CFXJS_Engine::DefineObjProperty(int nObjDefnID, |
Tom Sepez | 4d5b8c5 | 2017-02-21 15:17:07 -0800 | [diff] [blame] | 311 | const char* sPropName, |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 312 | v8::AccessorGetterCallback pPropGet, |
| 313 | v8::AccessorSetterCallback pPropPut) { |
| 314 | v8::Isolate::Scope isolate_scope(m_isolate); |
| 315 | v8::HandleScope handle_scope(m_isolate); |
Tom Sepez | ed7b2b5 | 2015-09-22 08:36:17 -0700 | [diff] [blame] | 316 | CFXJS_ObjDefinition* pObjDef = |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 317 | CFXJS_ObjDefinition::ForID(m_isolate, nObjDefnID); |
Tom Sepez | 016a347 | 2015-09-30 15:50:57 -0700 | [diff] [blame] | 318 | pObjDef->GetInstanceTemplate()->SetAccessor( |
Tom Sepez | 4d5b8c5 | 2017-02-21 15:17:07 -0800 | [diff] [blame] | 319 | v8::String::NewFromUtf8(m_isolate, sPropName, v8::NewStringType::kNormal) |
Dan Sinclair | f766ad2 | 2016-03-14 13:51:24 -0400 | [diff] [blame] | 320 | .ToLocalChecked(), |
Tom Sepez | 808a99e | 2015-09-10 12:28:37 -0700 | [diff] [blame] | 321 | pPropGet, pPropPut); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 322 | } |
| 323 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 324 | void CFXJS_Engine::DefineObjAllProperties( |
| 325 | int nObjDefnID, |
| 326 | v8::NamedPropertyQueryCallback pPropQurey, |
| 327 | v8::NamedPropertyGetterCallback pPropGet, |
| 328 | v8::NamedPropertySetterCallback pPropPut, |
| 329 | v8::NamedPropertyDeleterCallback pPropDel) { |
| 330 | v8::Isolate::Scope isolate_scope(m_isolate); |
| 331 | v8::HandleScope handle_scope(m_isolate); |
Tom Sepez | ed7b2b5 | 2015-09-22 08:36:17 -0700 | [diff] [blame] | 332 | CFXJS_ObjDefinition* pObjDef = |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 333 | CFXJS_ObjDefinition::ForID(m_isolate, nObjDefnID); |
Tom Sepez | 016a347 | 2015-09-30 15:50:57 -0700 | [diff] [blame] | 334 | pObjDef->GetInstanceTemplate()->SetNamedPropertyHandler(pPropGet, pPropPut, |
| 335 | pPropQurey, pPropDel); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 336 | } |
| 337 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 338 | void CFXJS_Engine::DefineObjConst(int nObjDefnID, |
Tom Sepez | 55db091 | 2017-02-21 15:26:52 -0800 | [diff] [blame] | 339 | const char* sConstName, |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 340 | v8::Local<v8::Value> pDefault) { |
| 341 | v8::Isolate::Scope isolate_scope(m_isolate); |
| 342 | v8::HandleScope handle_scope(m_isolate); |
Tom Sepez | ed7b2b5 | 2015-09-22 08:36:17 -0700 | [diff] [blame] | 343 | CFXJS_ObjDefinition* pObjDef = |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 344 | CFXJS_ObjDefinition::ForID(m_isolate, nObjDefnID); |
Tom Sepez | 55db091 | 2017-02-21 15:26:52 -0800 | [diff] [blame] | 345 | pObjDef->GetInstanceTemplate()->Set(m_isolate, sConstName, pDefault); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 346 | } |
| 347 | |
Tom Sepez | 9b99b63 | 2017-02-21 15:05:57 -0800 | [diff] [blame] | 348 | void CFXJS_Engine::DefineGlobalMethod(const char* sMethodName, |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 349 | v8::FunctionCallback pMethodCall) { |
| 350 | v8::Isolate::Scope isolate_scope(m_isolate); |
| 351 | v8::HandleScope handle_scope(m_isolate); |
jochen | c4dedf3 | 2016-07-06 05:26:23 -0700 | [diff] [blame] | 352 | v8::Local<v8::FunctionTemplate> fun = |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 353 | v8::FunctionTemplate::New(m_isolate, pMethodCall); |
jochen | c4dedf3 | 2016-07-06 05:26:23 -0700 | [diff] [blame] | 354 | fun->RemovePrototype(); |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 355 | GetGlobalObjectTemplate(m_isolate)->Set( |
Tom Sepez | 9b99b63 | 2017-02-21 15:05:57 -0800 | [diff] [blame] | 356 | v8::String::NewFromUtf8(m_isolate, sMethodName, |
jochen | c4dedf3 | 2016-07-06 05:26:23 -0700 | [diff] [blame] | 357 | v8::NewStringType::kNormal) |
| 358 | .ToLocalChecked(), |
| 359 | fun, v8::ReadOnly); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 360 | } |
| 361 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 362 | void CFXJS_Engine::DefineGlobalConst(const wchar_t* sConstName, |
| 363 | v8::FunctionCallback pConstGetter) { |
| 364 | v8::Isolate::Scope isolate_scope(m_isolate); |
| 365 | v8::HandleScope handle_scope(m_isolate); |
Tom Sepez | f0b6554 | 2017-02-13 10:26:01 -0800 | [diff] [blame] | 366 | CFX_ByteString bsConst = FX_UTF8Encode(CFX_WideStringC(sConstName)); |
jochen | c4dedf3 | 2016-07-06 05:26:23 -0700 | [diff] [blame] | 367 | v8::Local<v8::FunctionTemplate> fun = |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 368 | v8::FunctionTemplate::New(m_isolate, pConstGetter); |
jochen | c4dedf3 | 2016-07-06 05:26:23 -0700 | [diff] [blame] | 369 | fun->RemovePrototype(); |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 370 | GetGlobalObjectTemplate(m_isolate)->SetAccessorProperty( |
| 371 | v8::String::NewFromUtf8(m_isolate, bsConst.c_str(), |
jochen | c4dedf3 | 2016-07-06 05:26:23 -0700 | [diff] [blame] | 372 | v8::NewStringType::kNormal) |
| 373 | .ToLocalChecked(), |
| 374 | fun); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 375 | } |
| 376 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 377 | void CFXJS_Engine::InitializeEngine() { |
| 378 | if (m_isolate == g_isolate) |
Lei Zhang | 4d4a442 | 2015-10-08 12:00:14 -0700 | [diff] [blame] | 379 | ++g_isolate_ref_count; |
| 380 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 381 | v8::Isolate::Scope isolate_scope(m_isolate); |
| 382 | v8::HandleScope handle_scope(m_isolate); |
dsinclair | 7d554c9 | 2016-06-20 06:06:31 -0700 | [diff] [blame] | 383 | |
| 384 | // This has to happen before we call GetGlobalObjectTemplate because that |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 385 | // method gets the PerIsolateData from m_isolate. |
| 386 | FXJS_PerIsolateData::SetUp(m_isolate); |
dsinclair | 7d554c9 | 2016-06-20 06:06:31 -0700 | [diff] [blame] | 387 | |
Tom Sepez | 016a347 | 2015-09-30 15:50:57 -0700 | [diff] [blame] | 388 | v8::Local<v8::Context> v8Context = |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 389 | v8::Context::New(m_isolate, nullptr, GetGlobalObjectTemplate(m_isolate)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 390 | v8::Context::Scope context_scope(v8Context); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 391 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 392 | v8Context->SetAlignedPointerInEmbedderData(kPerContextDataIndex, this); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 393 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 394 | int maxID = CFXJS_ObjDefinition::MaxID(m_isolate); |
| 395 | m_StaticObjects.resize(maxID + 1); |
Tom Sepez | ed7b2b5 | 2015-09-22 08:36:17 -0700 | [diff] [blame] | 396 | for (int i = 0; i < maxID; ++i) { |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 397 | CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(m_isolate, i); |
Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame] | 398 | if (pObjDef->m_ObjType == FXJSOBJTYPE_GLOBAL) { |
Tom Sepez | 3f72fb4 | 2017-02-27 11:43:55 -0800 | [diff] [blame] | 399 | CFXJS_PerObjectData::SetInObject(new CFXJS_PerObjectData(i), |
| 400 | v8Context->Global() |
| 401 | ->GetPrototype() |
| 402 | ->ToObject(v8Context) |
| 403 | .ToLocalChecked()); |
| 404 | if (pObjDef->m_pConstructor) { |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 405 | pObjDef->m_pConstructor(this, v8Context->Global() |
| 406 | ->GetPrototype() |
| 407 | ->ToObject(v8Context) |
| 408 | .ToLocalChecked()); |
Tom Sepez | 3f72fb4 | 2017-02-27 11:43:55 -0800 | [diff] [blame] | 409 | } |
Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame] | 410 | } else if (pObjDef->m_ObjType == FXJSOBJTYPE_STATIC) { |
Tom Sepez | 892d751 | 2017-02-21 13:57:13 -0800 | [diff] [blame] | 411 | v8::Local<v8::String> pObjName = |
| 412 | v8::String::NewFromUtf8(m_isolate, pObjDef->m_ObjName, |
| 413 | v8::NewStringType::kNormal, |
| 414 | strlen(pObjDef->m_ObjName)) |
Dan Sinclair | f766ad2 | 2016-03-14 13:51:24 -0400 | [diff] [blame] | 415 | .ToLocalChecked(); |
Tom Sepez | 4237aed | 2015-11-10 15:19:17 -0800 | [diff] [blame] | 416 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 417 | v8::Local<v8::Object> obj = NewFxDynamicObj(i, true); |
Tom Sepez | 892d751 | 2017-02-21 13:57:13 -0800 | [diff] [blame] | 418 | v8Context->Global()->Set(v8Context, pObjName, obj).FromJust(); |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 419 | m_StaticObjects[i] = new v8::Global<v8::Object>(m_isolate, obj); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 420 | } |
| 421 | } |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 422 | m_V8PersistentContext.Reset(m_isolate, v8Context); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 423 | } |
| 424 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 425 | void CFXJS_Engine::ReleaseEngine() { |
| 426 | v8::Isolate::Scope isolate_scope(m_isolate); |
| 427 | v8::HandleScope handle_scope(m_isolate); |
Tom Sepez | 808a99e | 2015-09-10 12:28:37 -0700 | [diff] [blame] | 428 | v8::Local<v8::Context> context = |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 429 | v8::Local<v8::Context>::New(m_isolate, m_V8PersistentContext); |
Tom Sepez | 808a99e | 2015-09-10 12:28:37 -0700 | [diff] [blame] | 430 | v8::Context::Scope context_scope(context); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 431 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 432 | FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(m_isolate); |
Tom Sepez | 129a60e | 2016-03-24 09:19:20 -0700 | [diff] [blame] | 433 | if (!pData) |
| 434 | return; |
Tom Sepez | 129a60e | 2016-03-24 09:19:20 -0700 | [diff] [blame] | 435 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 436 | m_ConstArrays.clear(); |
| 437 | |
| 438 | int maxID = CFXJS_ObjDefinition::MaxID(m_isolate); |
Tom Sepez | ed7b2b5 | 2015-09-22 08:36:17 -0700 | [diff] [blame] | 439 | for (int i = 0; i < maxID; ++i) { |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 440 | CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(m_isolate, i); |
Oliver Chang | 8f16b69 | 2015-10-27 09:34:49 -0700 | [diff] [blame] | 441 | v8::Local<v8::Object> pObj; |
| 442 | if (pObjDef->m_ObjType == FXJSOBJTYPE_GLOBAL) { |
| 443 | pObj = |
| 444 | context->Global()->GetPrototype()->ToObject(context).ToLocalChecked(); |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 445 | } else if (m_StaticObjects[i] && !m_StaticObjects[i]->IsEmpty()) { |
| 446 | pObj = v8::Local<v8::Object>::New(m_isolate, *m_StaticObjects[i]); |
| 447 | delete m_StaticObjects[i]; |
| 448 | m_StaticObjects[i] = nullptr; |
Oliver Chang | 8f16b69 | 2015-10-27 09:34:49 -0700 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | if (!pObj.IsEmpty()) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 452 | if (pObjDef->m_pDestructor) |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 453 | pObjDef->m_pDestructor(this, pObj); |
| 454 | FreeObjectPrivate(pObj); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 455 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 456 | } |
Tom Sepez | 808a99e | 2015-09-10 12:28:37 -0700 | [diff] [blame] | 457 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 458 | m_V8PersistentContext.Reset(); |
| 459 | |
| 460 | if (m_isolate == g_isolate && --g_isolate_ref_count > 0) |
Tom Sepez | 4237aed | 2015-11-10 15:19:17 -0800 | [diff] [blame] | 461 | return; |
| 462 | |
Tom Sepez | 129a60e | 2016-03-24 09:19:20 -0700 | [diff] [blame] | 463 | delete pData; |
weili | a4ad595 | 2016-09-22 10:38:53 -0700 | [diff] [blame] | 464 | m_isolate->SetData(g_embedderDataSlot, nullptr); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 465 | } |
| 466 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 467 | int CFXJS_Engine::Execute(const CFX_WideString& script, FXJSErr* pError) { |
| 468 | v8::Isolate::Scope isolate_scope(m_isolate); |
| 469 | v8::TryCatch try_catch(m_isolate); |
dsinclair | 884b4f9 | 2016-06-06 09:49:32 -0700 | [diff] [blame] | 470 | CFX_ByteString bsScript = script.UTF8Encode(); |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 471 | v8::Local<v8::Context> context = m_isolate->GetCurrentContext(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 472 | v8::Local<v8::Script> compiled_script; |
Dan Sinclair | f766ad2 | 2016-03-14 13:51:24 -0400 | [diff] [blame] | 473 | if (!v8::Script::Compile(context, |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 474 | v8::String::NewFromUtf8(m_isolate, bsScript.c_str(), |
Dan Sinclair | f766ad2 | 2016-03-14 13:51:24 -0400 | [diff] [blame] | 475 | v8::NewStringType::kNormal, |
| 476 | bsScript.GetLength()) |
| 477 | .ToLocalChecked()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 478 | .ToLocal(&compiled_script)) { |
| 479 | v8::String::Utf8Value error(try_catch.Exception()); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 480 | // TODO(tsepez): return error via pError->message. |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 481 | return -1; |
| 482 | } |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 483 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 484 | v8::Local<v8::Value> result; |
| 485 | if (!compiled_script->Run(context).ToLocal(&result)) { |
| 486 | v8::String::Utf8Value error(try_catch.Exception()); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 487 | // TODO(tsepez): return error via pError->message. |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 488 | return -1; |
| 489 | } |
| 490 | return 0; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 491 | } |
| 492 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 493 | v8::Local<v8::Object> CFXJS_Engine::NewFxDynamicObj(int nObjDefnID, |
| 494 | bool bStatic) { |
| 495 | v8::Isolate::Scope isolate_scope(m_isolate); |
| 496 | v8::Local<v8::Context> context = m_isolate->GetCurrentContext(); |
Tom Sepez | ed7b2b5 | 2015-09-22 08:36:17 -0700 | [diff] [blame] | 497 | if (nObjDefnID == -1) { |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 498 | v8::Local<v8::ObjectTemplate> objTempl = v8::ObjectTemplate::New(m_isolate); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 499 | v8::Local<v8::Object> obj; |
Tom Sepez | 016a347 | 2015-09-30 15:50:57 -0700 | [diff] [blame] | 500 | if (!objTempl->NewInstance(context).ToLocal(&obj)) |
| 501 | return v8::Local<v8::Object>(); |
| 502 | return obj; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 503 | } |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 504 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 505 | FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(m_isolate); |
Tom Sepez | ed7b2b5 | 2015-09-22 08:36:17 -0700 | [diff] [blame] | 506 | if (!pData) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 507 | return v8::Local<v8::Object>(); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 508 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 509 | if (nObjDefnID < 0 || nObjDefnID >= CFXJS_ObjDefinition::MaxID(m_isolate)) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 510 | return v8::Local<v8::Object>(); |
Tom Sepez | ed7b2b5 | 2015-09-22 08:36:17 -0700 | [diff] [blame] | 511 | |
| 512 | CFXJS_ObjDefinition* pObjDef = |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 513 | CFXJS_ObjDefinition::ForID(m_isolate, nObjDefnID); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 514 | v8::Local<v8::Object> obj; |
Tom Sepez | 016a347 | 2015-09-30 15:50:57 -0700 | [diff] [blame] | 515 | if (!pObjDef->GetInstanceTemplate()->NewInstance(context).ToLocal(&obj)) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 516 | return v8::Local<v8::Object>(); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 517 | |
Tom Sepez | 3f72fb4 | 2017-02-27 11:43:55 -0800 | [diff] [blame] | 518 | CFXJS_PerObjectData* pObjData = new CFXJS_PerObjectData(nObjDefnID); |
| 519 | CFXJS_PerObjectData::SetInObject(pObjData, obj); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 520 | if (pObjDef->m_pConstructor) |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 521 | pObjDef->m_pConstructor(this, obj); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 522 | |
Tom Sepez | 3f72fb4 | 2017-02-27 11:43:55 -0800 | [diff] [blame] | 523 | if (!bStatic && FXJS_PerIsolateData::Get(m_isolate)->m_pDynamicObjsMap) |
| 524 | FXJS_PerIsolateData::Get(m_isolate)->m_pDynamicObjsMap->set(pObjData, obj); |
| 525 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 526 | return obj; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 527 | } |
| 528 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 529 | v8::Local<v8::Object> CFXJS_Engine::GetThisObj() { |
| 530 | v8::Isolate::Scope isolate_scope(m_isolate); |
| 531 | if (!FXJS_PerIsolateData::Get(m_isolate)) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 532 | return v8::Local<v8::Object>(); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 533 | |
Tom Sepez | ed7b2b5 | 2015-09-22 08:36:17 -0700 | [diff] [blame] | 534 | // Return the global object. |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 535 | v8::Local<v8::Context> context = m_isolate->GetCurrentContext(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 536 | return context->Global()->GetPrototype()->ToObject(context).ToLocalChecked(); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 537 | } |
| 538 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 539 | void CFXJS_Engine::Error(const CFX_WideString& message) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 540 | // Conversion from pdfium's wchar_t wide-strings to v8's uint16_t |
| 541 | // wide-strings isn't handled by v8, so use UTF8 as a common |
| 542 | // intermediate format. |
| 543 | CFX_ByteString utf8_message = message.UTF8Encode(); |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 544 | m_isolate->ThrowException(v8::String::NewFromUtf8(m_isolate, |
| 545 | utf8_message.c_str(), |
| 546 | v8::NewStringType::kNormal) |
| 547 | .ToLocalChecked()); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 548 | } |
| 549 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 550 | void CFXJS_Engine::SetObjectPrivate(v8::Local<v8::Object> pObj, void* p) { |
Tom Sepez | 3f72fb4 | 2017-02-27 11:43:55 -0800 | [diff] [blame] | 551 | CFXJS_PerObjectData* pPerObjectData = |
| 552 | CFXJS_PerObjectData::GetFromObject(pObj); |
Tom Sepez | 6cf117c | 2015-11-06 14:52:10 -0800 | [diff] [blame] | 553 | if (!pPerObjectData) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 554 | return; |
Tom Sepez | 6cf117c | 2015-11-06 14:52:10 -0800 | [diff] [blame] | 555 | pPerObjectData->m_pPrivate = p; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 556 | } |
| 557 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 558 | void* CFXJS_Engine::GetObjectPrivate(v8::Local<v8::Object> pObj) { |
Tom Sepez | 3f72fb4 | 2017-02-27 11:43:55 -0800 | [diff] [blame] | 559 | CFXJS_PerObjectData* pData = CFXJS_PerObjectData::GetFromObject(pObj); |
| 560 | if (!pData && !pObj.IsEmpty()) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 561 | // It could be a global proxy object. |
| 562 | v8::Local<v8::Value> v = pObj->GetPrototype(); |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 563 | v8::Local<v8::Context> context = m_isolate->GetCurrentContext(); |
Lei Zhang | 4d4a442 | 2015-10-08 12:00:14 -0700 | [diff] [blame] | 564 | if (v->IsObject()) { |
Tom Sepez | 3f72fb4 | 2017-02-27 11:43:55 -0800 | [diff] [blame] | 565 | pData = CFXJS_PerObjectData::GetFromObject( |
| 566 | v->ToObject(context).ToLocalChecked()); |
Lei Zhang | 4d4a442 | 2015-10-08 12:00:14 -0700 | [diff] [blame] | 567 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 568 | } |
Tom Sepez | 3f72fb4 | 2017-02-27 11:43:55 -0800 | [diff] [blame] | 569 | return pData ? pData->m_pPrivate : nullptr; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 570 | } |
| 571 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 572 | v8::Local<v8::Value> CFXJS_Engine::GetObjectProperty( |
tsepez | 018935c | 2016-04-15 13:15:12 -0700 | [diff] [blame] | 573 | v8::Local<v8::Object> pObj, |
| 574 | const CFX_WideString& wsPropertyName) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 575 | if (pObj.IsEmpty()) |
| 576 | return v8::Local<v8::Value>(); |
| 577 | v8::Local<v8::Value> val; |
Tom Sepez | c6dc69f | 2017-02-23 09:53:09 -0800 | [diff] [blame] | 578 | if (!pObj->Get(m_isolate->GetCurrentContext(), |
| 579 | NewString(wsPropertyName.AsStringC())) |
Dan Sinclair | f766ad2 | 2016-03-14 13:51:24 -0400 | [diff] [blame] | 580 | .ToLocal(&val)) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 581 | return v8::Local<v8::Value>(); |
| 582 | return val; |
| 583 | } |
| 584 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 585 | std::vector<CFX_WideString> CFXJS_Engine::GetObjectPropertyNames( |
tsepez | d0b6ed1 | 2016-08-11 19:50:57 -0700 | [diff] [blame] | 586 | v8::Local<v8::Object> pObj) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 587 | if (pObj.IsEmpty()) |
tsepez | d0b6ed1 | 2016-08-11 19:50:57 -0700 | [diff] [blame] | 588 | return std::vector<CFX_WideString>(); |
| 589 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 590 | v8::Local<v8::Array> val; |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 591 | v8::Local<v8::Context> context = m_isolate->GetCurrentContext(); |
| 592 | if (!pObj->GetPropertyNames(context).ToLocal(&val)) |
tsepez | d0b6ed1 | 2016-08-11 19:50:57 -0700 | [diff] [blame] | 593 | return std::vector<CFX_WideString>(); |
| 594 | |
| 595 | std::vector<CFX_WideString> result; |
| 596 | for (uint32_t i = 0; i < val->Length(); ++i) { |
tsepez | e6cf013 | 2017-01-18 14:38:18 -0800 | [diff] [blame] | 597 | result.push_back(ToWideString(val->Get(context, i).ToLocalChecked())); |
tsepez | d0b6ed1 | 2016-08-11 19:50:57 -0700 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | return result; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 601 | } |
| 602 | |
tsepez | e6cf013 | 2017-01-18 14:38:18 -0800 | [diff] [blame] | 603 | void CFXJS_Engine::PutObjectProperty(v8::Local<v8::Object> pObj, |
| 604 | const CFX_WideString& wsPropertyName, |
| 605 | v8::Local<v8::Value> pPut) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 606 | if (pObj.IsEmpty()) |
| 607 | return; |
Tom Sepez | c6dc69f | 2017-02-23 09:53:09 -0800 | [diff] [blame] | 608 | pObj->Set(m_isolate->GetCurrentContext(), |
| 609 | NewString(wsPropertyName.AsStringC()), pPut) |
Dan Sinclair | f766ad2 | 2016-03-14 13:51:24 -0400 | [diff] [blame] | 610 | .FromJust(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 611 | } |
| 612 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 613 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 614 | v8::Local<v8::Array> CFXJS_Engine::NewArray() { |
| 615 | return v8::Array::New(m_isolate); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 616 | } |
| 617 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 618 | unsigned CFXJS_Engine::PutArrayElement(v8::Local<v8::Array> pArray, |
| 619 | unsigned index, |
| 620 | v8::Local<v8::Value> pValue) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 621 | if (pArray.IsEmpty()) |
| 622 | return 0; |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 623 | if (pArray->Set(m_isolate->GetCurrentContext(), index, pValue).IsNothing()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 624 | return 0; |
| 625 | return 1; |
| 626 | } |
| 627 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 628 | v8::Local<v8::Value> CFXJS_Engine::GetArrayElement(v8::Local<v8::Array> pArray, |
| 629 | unsigned index) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 630 | if (pArray.IsEmpty()) |
| 631 | return v8::Local<v8::Value>(); |
| 632 | v8::Local<v8::Value> val; |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 633 | if (!pArray->Get(m_isolate->GetCurrentContext(), index).ToLocal(&val)) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 634 | return v8::Local<v8::Value>(); |
| 635 | return val; |
| 636 | } |
| 637 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 638 | unsigned CFXJS_Engine::GetArrayLength(v8::Local<v8::Array> pArray) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 639 | if (pArray.IsEmpty()) |
| 640 | return 0; |
| 641 | return pArray->Length(); |
| 642 | } |
| 643 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 644 | v8::Local<v8::Context> CFXJS_Engine::NewLocalContext() { |
| 645 | return v8::Local<v8::Context>::New(m_isolate, m_V8PersistentContext); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 646 | } |
| 647 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 648 | v8::Local<v8::Context> CFXJS_Engine::GetPersistentContext() { |
| 649 | return m_V8PersistentContext.Get(m_isolate); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 650 | } |
| 651 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 652 | v8::Local<v8::Value> CFXJS_Engine::NewNumber(int number) { |
| 653 | return v8::Int32::New(m_isolate, number); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 654 | } |
| 655 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 656 | v8::Local<v8::Value> CFXJS_Engine::NewNumber(double number) { |
| 657 | return v8::Number::New(m_isolate, number); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 658 | } |
| 659 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 660 | v8::Local<v8::Value> CFXJS_Engine::NewNumber(float number) { |
| 661 | return v8::Number::New(m_isolate, (float)number); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 662 | } |
| 663 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 664 | v8::Local<v8::Value> CFXJS_Engine::NewBoolean(bool b) { |
| 665 | return v8::Boolean::New(m_isolate, b); |
| 666 | } |
| 667 | |
Tom Sepez | c6dc69f | 2017-02-23 09:53:09 -0800 | [diff] [blame] | 668 | v8::Local<v8::Value> CFXJS_Engine::NewString(const CFX_ByteStringC& str) { |
| 669 | v8::Isolate* pIsolate = m_isolate ? m_isolate : v8::Isolate::GetCurrent(); |
| 670 | return v8::String::NewFromUtf8(pIsolate, str.c_str(), |
| 671 | v8::NewStringType::kNormal, str.GetLength()) |
| 672 | .ToLocalChecked(); |
| 673 | } |
| 674 | |
| 675 | v8::Local<v8::Value> CFXJS_Engine::NewString(const CFX_WideStringC& str) { |
| 676 | return NewString(FX_UTF8Encode(str).AsStringC()); |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | v8::Local<v8::Value> CFXJS_Engine::NewNull() { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 680 | return v8::Local<v8::Value>(); |
| 681 | } |
| 682 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 683 | v8::Local<v8::Date> CFXJS_Engine::NewDate(double d) { |
| 684 | return v8::Date::New(m_isolate->GetCurrentContext(), d) |
tsepez | 135b998 | 2016-08-05 09:32:50 -0700 | [diff] [blame] | 685 | .ToLocalChecked() |
| 686 | .As<v8::Date>(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 687 | } |
| 688 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 689 | int CFXJS_Engine::ToInt32(v8::Local<v8::Value> pValue) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 690 | if (pValue.IsEmpty()) |
| 691 | return 0; |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 692 | v8::Local<v8::Context> context = m_isolate->GetCurrentContext(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 693 | return pValue->ToInt32(context).ToLocalChecked()->Value(); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 694 | } |
| 695 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 696 | bool CFXJS_Engine::ToBoolean(v8::Local<v8::Value> pValue) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 697 | if (pValue.IsEmpty()) |
| 698 | return false; |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 699 | v8::Local<v8::Context> context = m_isolate->GetCurrentContext(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 700 | return pValue->ToBoolean(context).ToLocalChecked()->Value(); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 701 | } |
| 702 | |
tsepez | e6cf013 | 2017-01-18 14:38:18 -0800 | [diff] [blame] | 703 | double CFXJS_Engine::ToDouble(v8::Local<v8::Value> pValue) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 704 | if (pValue.IsEmpty()) |
| 705 | return 0.0; |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 706 | v8::Local<v8::Context> context = m_isolate->GetCurrentContext(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 707 | return pValue->ToNumber(context).ToLocalChecked()->Value(); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 708 | } |
| 709 | |
tsepez | e6cf013 | 2017-01-18 14:38:18 -0800 | [diff] [blame] | 710 | CFX_WideString CFXJS_Engine::ToWideString(v8::Local<v8::Value> pValue) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 711 | if (pValue.IsEmpty()) |
tsepez | e6cf013 | 2017-01-18 14:38:18 -0800 | [diff] [blame] | 712 | return CFX_WideString(); |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 713 | v8::Local<v8::Context> context = m_isolate->GetCurrentContext(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 714 | v8::String::Utf8Value s(pValue->ToString(context).ToLocalChecked()); |
tsepez | 6fe7d21 | 2016-04-06 10:51:14 -0700 | [diff] [blame] | 715 | return CFX_WideString::FromUTF8(CFX_ByteStringC(*s, s.length())); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 716 | } |
| 717 | |
tsepez | e6cf013 | 2017-01-18 14:38:18 -0800 | [diff] [blame] | 718 | v8::Local<v8::Object> CFXJS_Engine::ToObject(v8::Local<v8::Value> pValue) { |
| 719 | if (pValue.IsEmpty() || !pValue->IsObject()) |
| 720 | return v8::Local<v8::Object>(); |
| 721 | v8::Local<v8::Context> context = m_isolate->GetCurrentContext(); |
| 722 | return pValue->ToObject(context).ToLocalChecked(); |
| 723 | } |
| 724 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 725 | v8::Local<v8::Array> CFXJS_Engine::ToArray(v8::Local<v8::Value> pValue) { |
tsepez | e6cf013 | 2017-01-18 14:38:18 -0800 | [diff] [blame] | 726 | if (pValue.IsEmpty() || !pValue->IsArray()) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 727 | return v8::Local<v8::Array>(); |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 728 | v8::Local<v8::Context> context = m_isolate->GetCurrentContext(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 729 | return v8::Local<v8::Array>::Cast(pValue->ToObject(context).ToLocalChecked()); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 730 | } |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 731 | |
| 732 | void CFXJS_Engine::SetConstArray(const CFX_WideString& name, |
| 733 | v8::Local<v8::Array> array) { |
| 734 | m_ConstArrays[name] = v8::Global<v8::Array>(GetIsolate(), array); |
| 735 | } |
| 736 | |
| 737 | v8::Local<v8::Array> CFXJS_Engine::GetConstArray(const CFX_WideString& name) { |
| 738 | return v8::Local<v8::Array>::New(GetIsolate(), m_ConstArrays[name]); |
| 739 | } |