blob: b0e1a1b260bc1312548c48470cba1001ea2176c0 [file] [log] [blame]
John Abd-El-Malek5110c472014-05-17 22:33:34 -07001// 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 Zhanga6d9f0e2015-06-13 00:48:38 -07004
John Abd-El-Malek5110c472014-05-17 22:33:34 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
dsinclair43554682016-09-29 17:29:48 -07007#include "fxjs/fxjs_v8.h"
John Abd-El-Malek5110c472014-05-17 22:33:34 -07008
Dan Sinclair3ebd1212016-03-09 09:59:23 -05009#include <vector>
10
dsinclaira52ab742016-09-29 13:59:29 -070011#include "core/fxcrt/fx_basic.h"
Lei Zhang4d4a4422015-10-08 12:00:14 -070012
Tom Sepezbd7fabf2015-09-28 10:31:27 -070013// 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.
17static const unsigned int kPerContextDataIndex = 3u;
Tom Sepeza72e8e22015-10-07 10:17:53 -070018static unsigned int g_embedderDataSlot = 1u;
19static v8::Isolate* g_isolate = nullptr;
Lei Zhang4d4a4422015-10-08 12:00:14 -070020static size_t g_isolate_ref_count = 0;
Tom Sepeza72e8e22015-10-07 10:17:53 -070021static FXJS_ArrayBufferAllocator* g_arrayBufferAllocator = nullptr;
Tom Sepez8b315b62015-10-02 08:59:57 -070022static v8::Global<v8::ObjectTemplate>* g_DefaultGlobalObjectTemplate = nullptr;
Tom Sepez3f72fb42017-02-27 11:43:55 -080023static wchar_t kPerObjectDataTag[] = L"CFXJS_PerObjectData";
Tom Sepez016a3472015-09-30 15:50:57 -070024
Tom Sepez6cf117c2015-11-06 14:52:10 -080025class CFXJS_PerObjectData {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070026 public:
Lei Zhangd88a3642015-11-10 09:38:57 -080027 explicit CFXJS_PerObjectData(int nObjDefID)
Tom Sepez6cf117c2015-11-06 14:52:10 -080028 : m_ObjDefID(nObjDefID), m_pPrivate(nullptr) {}
Tom Sepezed7b2b52015-09-22 08:36:17 -070029
Tom Sepez3f72fb42017-02-27 11:43:55 -080030 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 Zhangd88a3642015-11-10 09:38:57 -080049 const int m_ObjDefID;
Tom Sepez6cf117c2015-11-06 14:52:10 -080050 void* m_pPrivate;
John Abd-El-Malek5110c472014-05-17 22:33:34 -070051};
52
Tom Sepezed7b2b52015-09-22 08:36:17 -070053class CFXJS_ObjDefinition {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070054 public:
Tom Sepezed7b2b52015-09-22 08:36:17 -070055 static int MaxID(v8::Isolate* pIsolate) {
Lei Zhangd88a3642015-11-10 09:38:57 -080056 return FXJS_PerIsolateData::Get(pIsolate)->m_ObjectDefnArray.size();
Tom Sepezed7b2b52015-09-22 08:36:17 -070057 }
Lei Zhangd88a3642015-11-10 09:38:57 -080058
Tom Sepezed7b2b52015-09-22 08:36:17 -070059 static CFXJS_ObjDefinition* ForID(v8::Isolate* pIsolate, int id) {
60 // Note: GetAt() halts if out-of-range even in release builds.
weilia4ad5952016-09-22 10:38:53 -070061 return FXJS_PerIsolateData::Get(pIsolate)->m_ObjectDefnArray[id].get();
Tom Sepezed7b2b52015-09-22 08:36:17 -070062 }
Lei Zhangd88a3642015-11-10 09:38:57 -080063
Tom Sepezed7b2b52015-09-22 08:36:17 -070064 CFXJS_ObjDefinition(v8::Isolate* isolate,
Tom Sepez892d7512017-02-21 13:57:13 -080065 const char* sObjName,
Tom Sepezed7b2b52015-09-22 08:36:17 -070066 FXJSOBJTYPE eObjType,
tsepezb4694242016-08-15 16:44:55 -070067 CFXJS_Engine::Constructor pConstructor,
68 CFXJS_Engine::Destructor pDestructor)
Tom Sepezcd56a7d2015-10-06 11:45:28 -070069 : m_ObjName(sObjName),
70 m_ObjType(eObjType),
Nico Weber9d8ec5a2015-08-04 13:00:21 -070071 m_pConstructor(pConstructor),
72 m_pDestructor(pDestructor),
Tom Sepez016a3472015-09-30 15:50:57 -070073 m_pIsolate(isolate) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070074 v8::Isolate::Scope isolate_scope(isolate);
75 v8::HandleScope handle_scope(isolate);
John Abd-El-Malek5110c472014-05-17 22:33:34 -070076
Tom Sepez016a3472015-09-30 15:50:57 -070077 v8::Local<v8::FunctionTemplate> fun = v8::FunctionTemplate::New(isolate);
78 fun->InstanceTemplate()->SetInternalFieldCount(2);
Tom Sepezc5a14722017-02-24 15:31:12 -080079 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 });
jochen7e6a8482016-07-06 11:02:27 -070085 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 Sepez016a3472015-09-30 15:50:57 -070091 m_FunctionTemplate.Reset(isolate, fun);
92
93 v8::Local<v8::Signature> sig = v8::Signature::New(isolate, fun);
94 m_Signature.Reset(isolate, sig);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070095 }
Tom Sepez016a3472015-09-30 15:50:57 -070096
97 int AssignID() {
98 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(m_pIsolate);
weilia4ad5952016-09-22 10:38:53 -070099 pData->m_ObjectDefnArray.emplace_back(this);
Lei Zhangd88a3642015-11-10 09:38:57 -0800100 return pData->m_ObjectDefnArray.size() - 1;
Tom Sepez016a3472015-09-30 15:50:57 -0700101 }
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 Weber9d8ec5a2015-08-04 13:00:21 -0700113 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700114
Tom Sepez892d7512017-02-21 13:57:13 -0800115 const char* const m_ObjName;
Tom Sepezcd56a7d2015-10-06 11:45:28 -0700116 const FXJSOBJTYPE m_ObjType;
tsepezb4694242016-08-15 16:44:55 -0700117 const CFXJS_Engine::Constructor m_pConstructor;
118 const CFXJS_Engine::Destructor m_pDestructor;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700119
Tom Sepez016a3472015-09-30 15:50:57 -0700120 v8::Isolate* m_pIsolate;
121 v8::Global<v8::FunctionTemplate> m_FunctionTemplate;
122 v8::Global<v8::Signature> m_Signature;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700123};
124
Tom Sepez016a3472015-09-30 15:50:57 -0700125static 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 Sepezcd56a7d2015-10-06 11:45:28 -0700130 if (pObjDef->m_ObjType == FXJSOBJTYPE_GLOBAL)
Tom Sepez016a3472015-09-30 15:50:57 -0700131 return pObjDef->GetInstanceTemplate();
132 }
Tom Sepez8b315b62015-10-02 08:59:57 -0700133 if (!g_DefaultGlobalObjectTemplate) {
jochen7e6a8482016-07-06 11:02:27 -0700134 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 Sepez8b315b62015-10-02 08:59:57 -0700142 }
143 return g_DefaultGlobalObjectTemplate->Get(pIsolate);
Tom Sepez016a3472015-09-30 15:50:57 -0700144}
145
Tom Sepez39bfe122015-09-17 15:25:23 -0700146void* FXJS_ArrayBufferAllocator::Allocate(size_t length) {
Tom Sepez7d0fcbf2015-09-15 15:30:34 -0700147 return calloc(1, length);
148}
149
Tom Sepez39bfe122015-09-17 15:25:23 -0700150void* FXJS_ArrayBufferAllocator::AllocateUninitialized(size_t length) {
Tom Sepez7d0fcbf2015-09-15 15:30:34 -0700151 return malloc(length);
152}
153
Tom Sepez39bfe122015-09-17 15:25:23 -0700154void FXJS_ArrayBufferAllocator::Free(void* data, size_t length) {
Tom Sepez7d0fcbf2015-09-15 15:30:34 -0700155 free(data);
156}
157
jinming_wang61dc96f2016-01-28 14:39:56 +0800158void 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;
tsepezb4694242016-08-15 16:44:55 -0700164 CFXJS_Engine* pEngine = CFXJS_Engine::CurrentEngineFromIsolate(isolate);
165 int id = pEngine->GetObjDefnID(obj);
jinming_wang61dc96f2016-01-28 14:39:56 +0800166 if (id == -1)
167 return;
jinming_wang61dc96f2016-01-28 14:39:56 +0800168 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(isolate, id);
169 if (!pObjDef)
170 return;
171 if (pObjDef->m_pDestructor)
tsepezb4694242016-08-15 16:44:55 -0700172 pObjDef->m_pDestructor(pEngine, obj);
173 CFXJS_Engine::FreeObjectPrivate(obj);
jinming_wang61dc96f2016-01-28 14:39:56 +0800174}
175
176V8TemplateMapTraits::MapType* V8TemplateMapTraits::MapFromWeakCallbackInfo(
177 const v8::WeakCallbackInfo<WeakCallbackDataType>& data) {
178 V8TemplateMap* pMap =
weilia4ad5952016-09-22 10:38:53 -0700179 (FXJS_PerIsolateData::Get(data.GetIsolate()))->m_pDynamicObjsMap.get();
jinming_wang61dc96f2016-01-28 14:39:56 +0800180 return pMap ? &pMap->m_map : nullptr;
181}
182
Tom Sepeza72e8e22015-10-07 10:17:53 -0700183void FXJS_Initialize(unsigned int embedderDataSlot, v8::Isolate* pIsolate) {
Lei Zhang4d4a4422015-10-08 12:00:14 -0700184 if (g_isolate) {
185 ASSERT(g_embedderDataSlot == embedderDataSlot);
186 ASSERT(g_isolate == pIsolate);
187 return;
188 }
Tom Sepeza72e8e22015-10-07 10:17:53 -0700189 g_embedderDataSlot = embedderDataSlot;
190 g_isolate = pIsolate;
191}
192
193void FXJS_Release() {
Lei Zhang4d4a4422015-10-08 12:00:14 -0700194 ASSERT(!g_isolate || g_isolate_ref_count == 0);
Lei Zhangb3914932015-10-08 12:08:47 -0700195 delete g_DefaultGlobalObjectTemplate;
Tom Sepeza72e8e22015-10-07 10:17:53 -0700196 g_DefaultGlobalObjectTemplate = nullptr;
197 g_isolate = nullptr;
198
199 delete g_arrayBufferAllocator;
200 g_arrayBufferAllocator = nullptr;
201}
202
203bool 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 Zhang3fa115b2015-10-08 12:04:47 -0700217size_t FXJS_GlobalIsolateRefCount() {
218 return g_isolate_ref_count;
219}
220
weili625ad662016-06-15 11:21:33 -0700221V8TemplateMap::V8TemplateMap(v8::Isolate* isolate) : m_map(isolate) {}
222
223V8TemplateMap::~V8TemplateMap() {}
224
225void V8TemplateMap::set(void* key, v8::Local<v8::Object> handle) {
226 ASSERT(!m_map.Contains(key));
227 m_map.Set(key, handle);
228}
229
230FXJS_PerIsolateData::~FXJS_PerIsolateData() {}
231
Tom Sepezed7b2b52015-09-22 08:36:17 -0700232// static
233void FXJS_PerIsolateData::SetUp(v8::Isolate* pIsolate) {
Tom Sepez7d0fcbf2015-09-15 15:30:34 -0700234 if (!pIsolate->GetData(g_embedderDataSlot))
weilia4ad5952016-09-22 10:38:53 -0700235 pIsolate->SetData(g_embedderDataSlot, new FXJS_PerIsolateData(pIsolate));
Tom Sepezed7b2b52015-09-22 08:36:17 -0700236}
237
238// static
239FXJS_PerIsolateData* FXJS_PerIsolateData::Get(v8::Isolate* pIsolate) {
240 return static_cast<FXJS_PerIsolateData*>(
241 pIsolate->GetData(g_embedderDataSlot));
Tom Sepez7d0fcbf2015-09-15 15:30:34 -0700242}
243
weilia4ad5952016-09-22 10:38:53 -0700244FXJS_PerIsolateData::FXJS_PerIsolateData(v8::Isolate* pIsolate)
245 : m_pDynamicObjsMap(new V8TemplateMap(pIsolate)) {}
weili625ad662016-06-15 11:21:33 -0700246
tsepeza4941912016-08-15 11:40:12 -0700247CFXJS_Engine::CFXJS_Engine() : m_isolate(nullptr) {}
tsepeza4941912016-08-15 11:40:12 -0700248
weili0b2a9872016-09-21 11:50:43 -0700249CFXJS_Engine::CFXJS_Engine(v8::Isolate* pIsolate) : m_isolate(pIsolate) {}
250
tsepezb4694242016-08-15 16:44:55 -0700251CFXJS_Engine::~CFXJS_Engine() {
252 m_V8PersistentContext.Reset();
253}
Tom Sepez7d0fcbf2015-09-15 15:30:34 -0700254
tsepezb4694242016-08-15 16:44:55 -0700255// static
256CFXJS_Engine* CFXJS_Engine::CurrentEngineFromIsolate(v8::Isolate* pIsolate) {
257 return static_cast<CFXJS_Engine*>(
258 pIsolate->GetCurrentContext()->GetAlignedPointerFromEmbedderData(
259 kPerContextDataIndex));
260}
261
tsepezb4694242016-08-15 16:44:55 -0700262// static
263int CFXJS_Engine::GetObjDefnID(v8::Local<v8::Object> pObj) {
Tom Sepez3f72fb42017-02-27 11:43:55 -0800264 CFXJS_PerObjectData* pData = CFXJS_PerObjectData::GetFromObject(pObj);
265 return pData ? pData->m_ObjDefID : -1;
tsepezb4694242016-08-15 16:44:55 -0700266}
267
268// static
269void CFXJS_Engine::FreeObjectPrivate(void* pPerObjectData) {
270 delete static_cast<CFXJS_PerObjectData*>(pPerObjectData);
271}
272
273// static
274void CFXJS_Engine::FreeObjectPrivate(v8::Local<v8::Object> pObj) {
Tom Sepez3f72fb42017-02-27 11:43:55 -0800275 CFXJS_PerObjectData* pData = CFXJS_PerObjectData::GetFromObject(pObj);
tsepezb4694242016-08-15 16:44:55 -0700276 pObj->SetAlignedPointerInInternalField(0, nullptr);
Tom Sepez3f72fb42017-02-27 11:43:55 -0800277 pObj->SetAlignedPointerInInternalField(1, nullptr);
278 delete pData;
tsepezb4694242016-08-15 16:44:55 -0700279}
280
Tom Sepez892d7512017-02-21 13:57:13 -0800281int CFXJS_Engine::DefineObj(const char* sObjName,
tsepezb4694242016-08-15 16:44:55 -0700282 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 Sepez016a3472015-09-30 15:50:57 -0700288 CFXJS_ObjDefinition* pObjDef = new CFXJS_ObjDefinition(
tsepezb4694242016-08-15 16:44:55 -0700289 m_isolate, sObjName, eObjType, pConstructor, pDestructor);
Tom Sepez016a3472015-09-30 15:50:57 -0700290 return pObjDef->AssignID();
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700291}
292
tsepezb4694242016-08-15 16:44:55 -0700293void CFXJS_Engine::DefineObjMethod(int nObjDefnID,
Tom Sepez9b99b632017-02-21 15:05:57 -0800294 const char* sMethodName,
tsepezb4694242016-08-15 16:44:55 -0700295 v8::FunctionCallback pMethodCall) {
296 v8::Isolate::Scope isolate_scope(m_isolate);
297 v8::HandleScope handle_scope(m_isolate);
Tom Sepezed7b2b52015-09-22 08:36:17 -0700298 CFXJS_ObjDefinition* pObjDef =
tsepezb4694242016-08-15 16:44:55 -0700299 CFXJS_ObjDefinition::ForID(m_isolate, nObjDefnID);
jochenc4dedf32016-07-06 05:26:23 -0700300 v8::Local<v8::FunctionTemplate> fun = v8::FunctionTemplate::New(
tsepezb4694242016-08-15 16:44:55 -0700301 m_isolate, pMethodCall, v8::Local<v8::Value>(), pObjDef->GetSignature());
jochenc4dedf32016-07-06 05:26:23 -0700302 fun->RemovePrototype();
Tom Sepez016a3472015-09-30 15:50:57 -0700303 pObjDef->GetInstanceTemplate()->Set(
Tom Sepez9b99b632017-02-21 15:05:57 -0800304 v8::String::NewFromUtf8(m_isolate, sMethodName,
Dan Sinclairf766ad22016-03-14 13:51:24 -0400305 v8::NewStringType::kNormal)
306 .ToLocalChecked(),
jochenc4dedf32016-07-06 05:26:23 -0700307 fun, v8::ReadOnly);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700308}
309
tsepezb4694242016-08-15 16:44:55 -0700310void CFXJS_Engine::DefineObjProperty(int nObjDefnID,
Tom Sepez4d5b8c52017-02-21 15:17:07 -0800311 const char* sPropName,
tsepezb4694242016-08-15 16:44:55 -0700312 v8::AccessorGetterCallback pPropGet,
313 v8::AccessorSetterCallback pPropPut) {
314 v8::Isolate::Scope isolate_scope(m_isolate);
315 v8::HandleScope handle_scope(m_isolate);
Tom Sepezed7b2b52015-09-22 08:36:17 -0700316 CFXJS_ObjDefinition* pObjDef =
tsepezb4694242016-08-15 16:44:55 -0700317 CFXJS_ObjDefinition::ForID(m_isolate, nObjDefnID);
Tom Sepez016a3472015-09-30 15:50:57 -0700318 pObjDef->GetInstanceTemplate()->SetAccessor(
Tom Sepez4d5b8c52017-02-21 15:17:07 -0800319 v8::String::NewFromUtf8(m_isolate, sPropName, v8::NewStringType::kNormal)
Dan Sinclairf766ad22016-03-14 13:51:24 -0400320 .ToLocalChecked(),
Tom Sepez808a99e2015-09-10 12:28:37 -0700321 pPropGet, pPropPut);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700322}
323
tsepezb4694242016-08-15 16:44:55 -0700324void 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 Sepezed7b2b52015-09-22 08:36:17 -0700332 CFXJS_ObjDefinition* pObjDef =
tsepezb4694242016-08-15 16:44:55 -0700333 CFXJS_ObjDefinition::ForID(m_isolate, nObjDefnID);
Tom Sepez016a3472015-09-30 15:50:57 -0700334 pObjDef->GetInstanceTemplate()->SetNamedPropertyHandler(pPropGet, pPropPut,
335 pPropQurey, pPropDel);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700336}
337
tsepezb4694242016-08-15 16:44:55 -0700338void CFXJS_Engine::DefineObjConst(int nObjDefnID,
Tom Sepez55db0912017-02-21 15:26:52 -0800339 const char* sConstName,
tsepezb4694242016-08-15 16:44:55 -0700340 v8::Local<v8::Value> pDefault) {
341 v8::Isolate::Scope isolate_scope(m_isolate);
342 v8::HandleScope handle_scope(m_isolate);
Tom Sepezed7b2b52015-09-22 08:36:17 -0700343 CFXJS_ObjDefinition* pObjDef =
tsepezb4694242016-08-15 16:44:55 -0700344 CFXJS_ObjDefinition::ForID(m_isolate, nObjDefnID);
Tom Sepez55db0912017-02-21 15:26:52 -0800345 pObjDef->GetInstanceTemplate()->Set(m_isolate, sConstName, pDefault);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700346}
347
Tom Sepez9b99b632017-02-21 15:05:57 -0800348void CFXJS_Engine::DefineGlobalMethod(const char* sMethodName,
tsepezb4694242016-08-15 16:44:55 -0700349 v8::FunctionCallback pMethodCall) {
350 v8::Isolate::Scope isolate_scope(m_isolate);
351 v8::HandleScope handle_scope(m_isolate);
jochenc4dedf32016-07-06 05:26:23 -0700352 v8::Local<v8::FunctionTemplate> fun =
tsepezb4694242016-08-15 16:44:55 -0700353 v8::FunctionTemplate::New(m_isolate, pMethodCall);
jochenc4dedf32016-07-06 05:26:23 -0700354 fun->RemovePrototype();
tsepezb4694242016-08-15 16:44:55 -0700355 GetGlobalObjectTemplate(m_isolate)->Set(
Tom Sepez9b99b632017-02-21 15:05:57 -0800356 v8::String::NewFromUtf8(m_isolate, sMethodName,
jochenc4dedf32016-07-06 05:26:23 -0700357 v8::NewStringType::kNormal)
358 .ToLocalChecked(),
359 fun, v8::ReadOnly);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700360}
361
tsepezb4694242016-08-15 16:44:55 -0700362void 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 Sepezf0b65542017-02-13 10:26:01 -0800366 CFX_ByteString bsConst = FX_UTF8Encode(CFX_WideStringC(sConstName));
jochenc4dedf32016-07-06 05:26:23 -0700367 v8::Local<v8::FunctionTemplate> fun =
tsepezb4694242016-08-15 16:44:55 -0700368 v8::FunctionTemplate::New(m_isolate, pConstGetter);
jochenc4dedf32016-07-06 05:26:23 -0700369 fun->RemovePrototype();
tsepezb4694242016-08-15 16:44:55 -0700370 GetGlobalObjectTemplate(m_isolate)->SetAccessorProperty(
371 v8::String::NewFromUtf8(m_isolate, bsConst.c_str(),
jochenc4dedf32016-07-06 05:26:23 -0700372 v8::NewStringType::kNormal)
373 .ToLocalChecked(),
374 fun);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700375}
376
tsepezb4694242016-08-15 16:44:55 -0700377void CFXJS_Engine::InitializeEngine() {
378 if (m_isolate == g_isolate)
Lei Zhang4d4a4422015-10-08 12:00:14 -0700379 ++g_isolate_ref_count;
380
tsepezb4694242016-08-15 16:44:55 -0700381 v8::Isolate::Scope isolate_scope(m_isolate);
382 v8::HandleScope handle_scope(m_isolate);
dsinclair7d554c92016-06-20 06:06:31 -0700383
384 // This has to happen before we call GetGlobalObjectTemplate because that
tsepezb4694242016-08-15 16:44:55 -0700385 // method gets the PerIsolateData from m_isolate.
386 FXJS_PerIsolateData::SetUp(m_isolate);
dsinclair7d554c92016-06-20 06:06:31 -0700387
Tom Sepez016a3472015-09-30 15:50:57 -0700388 v8::Local<v8::Context> v8Context =
tsepezb4694242016-08-15 16:44:55 -0700389 v8::Context::New(m_isolate, nullptr, GetGlobalObjectTemplate(m_isolate));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700390 v8::Context::Scope context_scope(v8Context);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700391
tsepezb4694242016-08-15 16:44:55 -0700392 v8Context->SetAlignedPointerInEmbedderData(kPerContextDataIndex, this);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700393
tsepezb4694242016-08-15 16:44:55 -0700394 int maxID = CFXJS_ObjDefinition::MaxID(m_isolate);
395 m_StaticObjects.resize(maxID + 1);
Tom Sepezed7b2b52015-09-22 08:36:17 -0700396 for (int i = 0; i < maxID; ++i) {
tsepezb4694242016-08-15 16:44:55 -0700397 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(m_isolate, i);
Tom Sepezcd56a7d2015-10-06 11:45:28 -0700398 if (pObjDef->m_ObjType == FXJSOBJTYPE_GLOBAL) {
Tom Sepez3f72fb42017-02-27 11:43:55 -0800399 CFXJS_PerObjectData::SetInObject(new CFXJS_PerObjectData(i),
400 v8Context->Global()
401 ->GetPrototype()
402 ->ToObject(v8Context)
403 .ToLocalChecked());
404 if (pObjDef->m_pConstructor) {
tsepezb4694242016-08-15 16:44:55 -0700405 pObjDef->m_pConstructor(this, v8Context->Global()
406 ->GetPrototype()
407 ->ToObject(v8Context)
408 .ToLocalChecked());
Tom Sepez3f72fb42017-02-27 11:43:55 -0800409 }
Tom Sepezcd56a7d2015-10-06 11:45:28 -0700410 } else if (pObjDef->m_ObjType == FXJSOBJTYPE_STATIC) {
Tom Sepez892d7512017-02-21 13:57:13 -0800411 v8::Local<v8::String> pObjName =
412 v8::String::NewFromUtf8(m_isolate, pObjDef->m_ObjName,
413 v8::NewStringType::kNormal,
414 strlen(pObjDef->m_ObjName))
Dan Sinclairf766ad22016-03-14 13:51:24 -0400415 .ToLocalChecked();
Tom Sepez4237aed2015-11-10 15:19:17 -0800416
tsepezb4694242016-08-15 16:44:55 -0700417 v8::Local<v8::Object> obj = NewFxDynamicObj(i, true);
Tom Sepez892d7512017-02-21 13:57:13 -0800418 v8Context->Global()->Set(v8Context, pObjName, obj).FromJust();
tsepezb4694242016-08-15 16:44:55 -0700419 m_StaticObjects[i] = new v8::Global<v8::Object>(m_isolate, obj);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700420 }
421 }
tsepezb4694242016-08-15 16:44:55 -0700422 m_V8PersistentContext.Reset(m_isolate, v8Context);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700423}
424
tsepezb4694242016-08-15 16:44:55 -0700425void CFXJS_Engine::ReleaseEngine() {
426 v8::Isolate::Scope isolate_scope(m_isolate);
427 v8::HandleScope handle_scope(m_isolate);
Tom Sepez808a99e2015-09-10 12:28:37 -0700428 v8::Local<v8::Context> context =
tsepezb4694242016-08-15 16:44:55 -0700429 v8::Local<v8::Context>::New(m_isolate, m_V8PersistentContext);
Tom Sepez808a99e2015-09-10 12:28:37 -0700430 v8::Context::Scope context_scope(context);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700431
tsepezb4694242016-08-15 16:44:55 -0700432 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(m_isolate);
Tom Sepez129a60e2016-03-24 09:19:20 -0700433 if (!pData)
434 return;
Tom Sepez129a60e2016-03-24 09:19:20 -0700435
tsepezb4694242016-08-15 16:44:55 -0700436 m_ConstArrays.clear();
437
438 int maxID = CFXJS_ObjDefinition::MaxID(m_isolate);
Tom Sepezed7b2b52015-09-22 08:36:17 -0700439 for (int i = 0; i < maxID; ++i) {
tsepezb4694242016-08-15 16:44:55 -0700440 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(m_isolate, i);
Oliver Chang8f16b692015-10-27 09:34:49 -0700441 v8::Local<v8::Object> pObj;
442 if (pObjDef->m_ObjType == FXJSOBJTYPE_GLOBAL) {
443 pObj =
444 context->Global()->GetPrototype()->ToObject(context).ToLocalChecked();
tsepezb4694242016-08-15 16:44:55 -0700445 } 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 Chang8f16b692015-10-27 09:34:49 -0700449 }
450
451 if (!pObj.IsEmpty()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700452 if (pObjDef->m_pDestructor)
tsepezb4694242016-08-15 16:44:55 -0700453 pObjDef->m_pDestructor(this, pObj);
454 FreeObjectPrivate(pObj);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700455 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700456 }
Tom Sepez808a99e2015-09-10 12:28:37 -0700457
tsepezb4694242016-08-15 16:44:55 -0700458 m_V8PersistentContext.Reset();
459
460 if (m_isolate == g_isolate && --g_isolate_ref_count > 0)
Tom Sepez4237aed2015-11-10 15:19:17 -0800461 return;
462
Tom Sepez129a60e2016-03-24 09:19:20 -0700463 delete pData;
weilia4ad5952016-09-22 10:38:53 -0700464 m_isolate->SetData(g_embedderDataSlot, nullptr);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700465}
466
tsepezb4694242016-08-15 16:44:55 -0700467int CFXJS_Engine::Execute(const CFX_WideString& script, FXJSErr* pError) {
468 v8::Isolate::Scope isolate_scope(m_isolate);
469 v8::TryCatch try_catch(m_isolate);
dsinclair884b4f92016-06-06 09:49:32 -0700470 CFX_ByteString bsScript = script.UTF8Encode();
tsepezb4694242016-08-15 16:44:55 -0700471 v8::Local<v8::Context> context = m_isolate->GetCurrentContext();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700472 v8::Local<v8::Script> compiled_script;
Dan Sinclairf766ad22016-03-14 13:51:24 -0400473 if (!v8::Script::Compile(context,
tsepezb4694242016-08-15 16:44:55 -0700474 v8::String::NewFromUtf8(m_isolate, bsScript.c_str(),
Dan Sinclairf766ad22016-03-14 13:51:24 -0400475 v8::NewStringType::kNormal,
476 bsScript.GetLength())
477 .ToLocalChecked())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700478 .ToLocal(&compiled_script)) {
479 v8::String::Utf8Value error(try_catch.Exception());
Tom Sepez39bfe122015-09-17 15:25:23 -0700480 // TODO(tsepez): return error via pError->message.
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700481 return -1;
482 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700483
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700484 v8::Local<v8::Value> result;
485 if (!compiled_script->Run(context).ToLocal(&result)) {
486 v8::String::Utf8Value error(try_catch.Exception());
Tom Sepez39bfe122015-09-17 15:25:23 -0700487 // TODO(tsepez): return error via pError->message.
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700488 return -1;
489 }
490 return 0;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700491}
492
tsepezb4694242016-08-15 16:44:55 -0700493v8::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 Sepezed7b2b52015-09-22 08:36:17 -0700497 if (nObjDefnID == -1) {
tsepezb4694242016-08-15 16:44:55 -0700498 v8::Local<v8::ObjectTemplate> objTempl = v8::ObjectTemplate::New(m_isolate);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700499 v8::Local<v8::Object> obj;
Tom Sepez016a3472015-09-30 15:50:57 -0700500 if (!objTempl->NewInstance(context).ToLocal(&obj))
501 return v8::Local<v8::Object>();
502 return obj;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700503 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700504
tsepezb4694242016-08-15 16:44:55 -0700505 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(m_isolate);
Tom Sepezed7b2b52015-09-22 08:36:17 -0700506 if (!pData)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700507 return v8::Local<v8::Object>();
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700508
tsepezb4694242016-08-15 16:44:55 -0700509 if (nObjDefnID < 0 || nObjDefnID >= CFXJS_ObjDefinition::MaxID(m_isolate))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700510 return v8::Local<v8::Object>();
Tom Sepezed7b2b52015-09-22 08:36:17 -0700511
512 CFXJS_ObjDefinition* pObjDef =
tsepezb4694242016-08-15 16:44:55 -0700513 CFXJS_ObjDefinition::ForID(m_isolate, nObjDefnID);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700514 v8::Local<v8::Object> obj;
Tom Sepez016a3472015-09-30 15:50:57 -0700515 if (!pObjDef->GetInstanceTemplate()->NewInstance(context).ToLocal(&obj))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700516 return v8::Local<v8::Object>();
Tom Sepez39bfe122015-09-17 15:25:23 -0700517
Tom Sepez3f72fb42017-02-27 11:43:55 -0800518 CFXJS_PerObjectData* pObjData = new CFXJS_PerObjectData(nObjDefnID);
519 CFXJS_PerObjectData::SetInObject(pObjData, obj);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700520 if (pObjDef->m_pConstructor)
tsepezb4694242016-08-15 16:44:55 -0700521 pObjDef->m_pConstructor(this, obj);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700522
Tom Sepez3f72fb42017-02-27 11:43:55 -0800523 if (!bStatic && FXJS_PerIsolateData::Get(m_isolate)->m_pDynamicObjsMap)
524 FXJS_PerIsolateData::Get(m_isolate)->m_pDynamicObjsMap->set(pObjData, obj);
525
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700526 return obj;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700527}
528
tsepezb4694242016-08-15 16:44:55 -0700529v8::Local<v8::Object> CFXJS_Engine::GetThisObj() {
530 v8::Isolate::Scope isolate_scope(m_isolate);
531 if (!FXJS_PerIsolateData::Get(m_isolate))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700532 return v8::Local<v8::Object>();
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700533
Tom Sepezed7b2b52015-09-22 08:36:17 -0700534 // Return the global object.
tsepezb4694242016-08-15 16:44:55 -0700535 v8::Local<v8::Context> context = m_isolate->GetCurrentContext();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700536 return context->Global()->GetPrototype()->ToObject(context).ToLocalChecked();
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700537}
538
tsepezb4694242016-08-15 16:44:55 -0700539void CFXJS_Engine::Error(const CFX_WideString& message) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700540 // 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();
tsepezb4694242016-08-15 16:44:55 -0700544 m_isolate->ThrowException(v8::String::NewFromUtf8(m_isolate,
545 utf8_message.c_str(),
546 v8::NewStringType::kNormal)
547 .ToLocalChecked());
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700548}
549
tsepezb4694242016-08-15 16:44:55 -0700550void CFXJS_Engine::SetObjectPrivate(v8::Local<v8::Object> pObj, void* p) {
Tom Sepez3f72fb42017-02-27 11:43:55 -0800551 CFXJS_PerObjectData* pPerObjectData =
552 CFXJS_PerObjectData::GetFromObject(pObj);
Tom Sepez6cf117c2015-11-06 14:52:10 -0800553 if (!pPerObjectData)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700554 return;
Tom Sepez6cf117c2015-11-06 14:52:10 -0800555 pPerObjectData->m_pPrivate = p;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700556}
557
tsepezb4694242016-08-15 16:44:55 -0700558void* CFXJS_Engine::GetObjectPrivate(v8::Local<v8::Object> pObj) {
Tom Sepez3f72fb42017-02-27 11:43:55 -0800559 CFXJS_PerObjectData* pData = CFXJS_PerObjectData::GetFromObject(pObj);
560 if (!pData && !pObj.IsEmpty()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700561 // It could be a global proxy object.
562 v8::Local<v8::Value> v = pObj->GetPrototype();
tsepezb4694242016-08-15 16:44:55 -0700563 v8::Local<v8::Context> context = m_isolate->GetCurrentContext();
Lei Zhang4d4a4422015-10-08 12:00:14 -0700564 if (v->IsObject()) {
Tom Sepez3f72fb42017-02-27 11:43:55 -0800565 pData = CFXJS_PerObjectData::GetFromObject(
566 v->ToObject(context).ToLocalChecked());
Lei Zhang4d4a4422015-10-08 12:00:14 -0700567 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700568 }
Tom Sepez3f72fb42017-02-27 11:43:55 -0800569 return pData ? pData->m_pPrivate : nullptr;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700570}
571
tsepezb4694242016-08-15 16:44:55 -0700572v8::Local<v8::Value> CFXJS_Engine::GetObjectProperty(
tsepez018935c2016-04-15 13:15:12 -0700573 v8::Local<v8::Object> pObj,
574 const CFX_WideString& wsPropertyName) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700575 if (pObj.IsEmpty())
576 return v8::Local<v8::Value>();
577 v8::Local<v8::Value> val;
Tom Sepezc6dc69f2017-02-23 09:53:09 -0800578 if (!pObj->Get(m_isolate->GetCurrentContext(),
579 NewString(wsPropertyName.AsStringC()))
Dan Sinclairf766ad22016-03-14 13:51:24 -0400580 .ToLocal(&val))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700581 return v8::Local<v8::Value>();
582 return val;
583}
584
tsepezb4694242016-08-15 16:44:55 -0700585std::vector<CFX_WideString> CFXJS_Engine::GetObjectPropertyNames(
tsepezd0b6ed12016-08-11 19:50:57 -0700586 v8::Local<v8::Object> pObj) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700587 if (pObj.IsEmpty())
tsepezd0b6ed12016-08-11 19:50:57 -0700588 return std::vector<CFX_WideString>();
589
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700590 v8::Local<v8::Array> val;
tsepezb4694242016-08-15 16:44:55 -0700591 v8::Local<v8::Context> context = m_isolate->GetCurrentContext();
592 if (!pObj->GetPropertyNames(context).ToLocal(&val))
tsepezd0b6ed12016-08-11 19:50:57 -0700593 return std::vector<CFX_WideString>();
594
595 std::vector<CFX_WideString> result;
596 for (uint32_t i = 0; i < val->Length(); ++i) {
tsepeze6cf0132017-01-18 14:38:18 -0800597 result.push_back(ToWideString(val->Get(context, i).ToLocalChecked()));
tsepezd0b6ed12016-08-11 19:50:57 -0700598 }
599
600 return result;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700601}
602
tsepeze6cf0132017-01-18 14:38:18 -0800603void CFXJS_Engine::PutObjectProperty(v8::Local<v8::Object> pObj,
604 const CFX_WideString& wsPropertyName,
605 v8::Local<v8::Value> pPut) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700606 if (pObj.IsEmpty())
607 return;
Tom Sepezc6dc69f2017-02-23 09:53:09 -0800608 pObj->Set(m_isolate->GetCurrentContext(),
609 NewString(wsPropertyName.AsStringC()), pPut)
Dan Sinclairf766ad22016-03-14 13:51:24 -0400610 .FromJust();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700611}
612
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700613
tsepezb4694242016-08-15 16:44:55 -0700614v8::Local<v8::Array> CFXJS_Engine::NewArray() {
615 return v8::Array::New(m_isolate);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700616}
617
tsepezb4694242016-08-15 16:44:55 -0700618unsigned CFXJS_Engine::PutArrayElement(v8::Local<v8::Array> pArray,
619 unsigned index,
620 v8::Local<v8::Value> pValue) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700621 if (pArray.IsEmpty())
622 return 0;
tsepezb4694242016-08-15 16:44:55 -0700623 if (pArray->Set(m_isolate->GetCurrentContext(), index, pValue).IsNothing())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700624 return 0;
625 return 1;
626}
627
tsepezb4694242016-08-15 16:44:55 -0700628v8::Local<v8::Value> CFXJS_Engine::GetArrayElement(v8::Local<v8::Array> pArray,
629 unsigned index) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700630 if (pArray.IsEmpty())
631 return v8::Local<v8::Value>();
632 v8::Local<v8::Value> val;
tsepezb4694242016-08-15 16:44:55 -0700633 if (!pArray->Get(m_isolate->GetCurrentContext(), index).ToLocal(&val))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700634 return v8::Local<v8::Value>();
635 return val;
636}
637
tsepezb4694242016-08-15 16:44:55 -0700638unsigned CFXJS_Engine::GetArrayLength(v8::Local<v8::Array> pArray) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700639 if (pArray.IsEmpty())
640 return 0;
641 return pArray->Length();
642}
643
tsepezb4694242016-08-15 16:44:55 -0700644v8::Local<v8::Context> CFXJS_Engine::NewLocalContext() {
645 return v8::Local<v8::Context>::New(m_isolate, m_V8PersistentContext);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700646}
647
tsepezb4694242016-08-15 16:44:55 -0700648v8::Local<v8::Context> CFXJS_Engine::GetPersistentContext() {
649 return m_V8PersistentContext.Get(m_isolate);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700650}
651
tsepezb4694242016-08-15 16:44:55 -0700652v8::Local<v8::Value> CFXJS_Engine::NewNumber(int number) {
653 return v8::Int32::New(m_isolate, number);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700654}
655
tsepezb4694242016-08-15 16:44:55 -0700656v8::Local<v8::Value> CFXJS_Engine::NewNumber(double number) {
657 return v8::Number::New(m_isolate, number);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700658}
659
tsepezb4694242016-08-15 16:44:55 -0700660v8::Local<v8::Value> CFXJS_Engine::NewNumber(float number) {
661 return v8::Number::New(m_isolate, (float)number);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700662}
663
tsepezb4694242016-08-15 16:44:55 -0700664v8::Local<v8::Value> CFXJS_Engine::NewBoolean(bool b) {
665 return v8::Boolean::New(m_isolate, b);
666}
667
Tom Sepezc6dc69f2017-02-23 09:53:09 -0800668v8::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
675v8::Local<v8::Value> CFXJS_Engine::NewString(const CFX_WideStringC& str) {
676 return NewString(FX_UTF8Encode(str).AsStringC());
tsepezb4694242016-08-15 16:44:55 -0700677}
678
679v8::Local<v8::Value> CFXJS_Engine::NewNull() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700680 return v8::Local<v8::Value>();
681}
682
tsepezb4694242016-08-15 16:44:55 -0700683v8::Local<v8::Date> CFXJS_Engine::NewDate(double d) {
684 return v8::Date::New(m_isolate->GetCurrentContext(), d)
tsepez135b9982016-08-05 09:32:50 -0700685 .ToLocalChecked()
686 .As<v8::Date>();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700687}
688
tsepezb4694242016-08-15 16:44:55 -0700689int CFXJS_Engine::ToInt32(v8::Local<v8::Value> pValue) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700690 if (pValue.IsEmpty())
691 return 0;
tsepezb4694242016-08-15 16:44:55 -0700692 v8::Local<v8::Context> context = m_isolate->GetCurrentContext();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700693 return pValue->ToInt32(context).ToLocalChecked()->Value();
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700694}
695
tsepezb4694242016-08-15 16:44:55 -0700696bool CFXJS_Engine::ToBoolean(v8::Local<v8::Value> pValue) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700697 if (pValue.IsEmpty())
698 return false;
tsepezb4694242016-08-15 16:44:55 -0700699 v8::Local<v8::Context> context = m_isolate->GetCurrentContext();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700700 return pValue->ToBoolean(context).ToLocalChecked()->Value();
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700701}
702
tsepeze6cf0132017-01-18 14:38:18 -0800703double CFXJS_Engine::ToDouble(v8::Local<v8::Value> pValue) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700704 if (pValue.IsEmpty())
705 return 0.0;
tsepezb4694242016-08-15 16:44:55 -0700706 v8::Local<v8::Context> context = m_isolate->GetCurrentContext();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700707 return pValue->ToNumber(context).ToLocalChecked()->Value();
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700708}
709
tsepeze6cf0132017-01-18 14:38:18 -0800710CFX_WideString CFXJS_Engine::ToWideString(v8::Local<v8::Value> pValue) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700711 if (pValue.IsEmpty())
tsepeze6cf0132017-01-18 14:38:18 -0800712 return CFX_WideString();
tsepezb4694242016-08-15 16:44:55 -0700713 v8::Local<v8::Context> context = m_isolate->GetCurrentContext();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700714 v8::String::Utf8Value s(pValue->ToString(context).ToLocalChecked());
tsepez6fe7d212016-04-06 10:51:14 -0700715 return CFX_WideString::FromUTF8(CFX_ByteStringC(*s, s.length()));
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700716}
717
tsepeze6cf0132017-01-18 14:38:18 -0800718v8::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
tsepezb4694242016-08-15 16:44:55 -0700725v8::Local<v8::Array> CFXJS_Engine::ToArray(v8::Local<v8::Value> pValue) {
tsepeze6cf0132017-01-18 14:38:18 -0800726 if (pValue.IsEmpty() || !pValue->IsArray())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700727 return v8::Local<v8::Array>();
tsepezb4694242016-08-15 16:44:55 -0700728 v8::Local<v8::Context> context = m_isolate->GetCurrentContext();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700729 return v8::Local<v8::Array>::Cast(pValue->ToObject(context).ToLocalChecked());
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700730}
tsepezb4694242016-08-15 16:44:55 -0700731
732void 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
737v8::Local<v8::Array> CFXJS_Engine::GetConstArray(const CFX_WideString& name) {
738 return v8::Local<v8::Array>::New(GetIsolate(), m_ConstArrays[name]);
739}