blob: 05986b4eb839dea5c25fb32e817b96e3ab36f2b9 [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"
Chris Palmere4b035b2017-03-26 15:48:34 -070012#include "third_party/base/allocator/partition_allocator/partition_alloc.h"
Lei Zhang4d4a4422015-10-08 12:00:14 -070013
Tom Sepezbd7fabf2015-09-28 10:31:27 -070014// Keep this consistent with the values defined in gin/public/context_holder.h
15// (without actually requiring a dependency on gin itself for the standalone
16// embedders of PDFIum). The value we want to use is:
17// kPerContextDataStartIndex + kEmbedderPDFium, which is 3.
18static const unsigned int kPerContextDataIndex = 3u;
Tom Sepeza72e8e22015-10-07 10:17:53 -070019static unsigned int g_embedderDataSlot = 1u;
20static v8::Isolate* g_isolate = nullptr;
Lei Zhang4d4a4422015-10-08 12:00:14 -070021static size_t g_isolate_ref_count = 0;
Tom Sepeza72e8e22015-10-07 10:17:53 -070022static FXJS_ArrayBufferAllocator* g_arrayBufferAllocator = nullptr;
Tom Sepez8b315b62015-10-02 08:59:57 -070023static v8::Global<v8::ObjectTemplate>* g_DefaultGlobalObjectTemplate = nullptr;
Tom Sepez3f72fb42017-02-27 11:43:55 -080024static wchar_t kPerObjectDataTag[] = L"CFXJS_PerObjectData";
Tom Sepez016a3472015-09-30 15:50:57 -070025
Tom Sepez6cf117c2015-11-06 14:52:10 -080026class CFXJS_PerObjectData {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070027 public:
Lei Zhangd88a3642015-11-10 09:38:57 -080028 explicit CFXJS_PerObjectData(int nObjDefID)
Tom Sepez6cf117c2015-11-06 14:52:10 -080029 : m_ObjDefID(nObjDefID), m_pPrivate(nullptr) {}
Tom Sepezed7b2b52015-09-22 08:36:17 -070030
Tom Sepez3f72fb42017-02-27 11:43:55 -080031 static void SetInObject(CFXJS_PerObjectData* pData,
32 v8::Local<v8::Object> pObj) {
33 if (pObj->InternalFieldCount() == 2) {
34 pObj->SetAlignedPointerInInternalField(0, pData);
35 pObj->SetAlignedPointerInInternalField(
36 1, static_cast<void*>(kPerObjectDataTag));
37 }
38 }
39
40 static CFXJS_PerObjectData* GetFromObject(v8::Local<v8::Object> pObj) {
41 if (pObj.IsEmpty() || pObj->InternalFieldCount() != 2 ||
42 pObj->GetAlignedPointerFromInternalField(1) !=
43 static_cast<void*>(kPerObjectDataTag)) {
44 return nullptr;
45 }
46 return static_cast<CFXJS_PerObjectData*>(
47 pObj->GetAlignedPointerFromInternalField(0));
48 }
49
Lei Zhangd88a3642015-11-10 09:38:57 -080050 const int m_ObjDefID;
Tom Sepez6cf117c2015-11-06 14:52:10 -080051 void* m_pPrivate;
John Abd-El-Malek5110c472014-05-17 22:33:34 -070052};
53
Tom Sepezed7b2b52015-09-22 08:36:17 -070054class CFXJS_ObjDefinition {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070055 public:
Tom Sepezed7b2b52015-09-22 08:36:17 -070056 static int MaxID(v8::Isolate* pIsolate) {
Lei Zhangd88a3642015-11-10 09:38:57 -080057 return FXJS_PerIsolateData::Get(pIsolate)->m_ObjectDefnArray.size();
Tom Sepezed7b2b52015-09-22 08:36:17 -070058 }
Lei Zhangd88a3642015-11-10 09:38:57 -080059
Tom Sepezed7b2b52015-09-22 08:36:17 -070060 static CFXJS_ObjDefinition* ForID(v8::Isolate* pIsolate, int id) {
61 // Note: GetAt() halts if out-of-range even in release builds.
weilia4ad5952016-09-22 10:38:53 -070062 return FXJS_PerIsolateData::Get(pIsolate)->m_ObjectDefnArray[id].get();
Tom Sepezed7b2b52015-09-22 08:36:17 -070063 }
Lei Zhangd88a3642015-11-10 09:38:57 -080064
Tom Sepezed7b2b52015-09-22 08:36:17 -070065 CFXJS_ObjDefinition(v8::Isolate* isolate,
Tom Sepez892d7512017-02-21 13:57:13 -080066 const char* sObjName,
Tom Sepezed7b2b52015-09-22 08:36:17 -070067 FXJSOBJTYPE eObjType,
tsepezb4694242016-08-15 16:44:55 -070068 CFXJS_Engine::Constructor pConstructor,
69 CFXJS_Engine::Destructor pDestructor)
Tom Sepezcd56a7d2015-10-06 11:45:28 -070070 : m_ObjName(sObjName),
71 m_ObjType(eObjType),
Nico Weber9d8ec5a2015-08-04 13:00:21 -070072 m_pConstructor(pConstructor),
73 m_pDestructor(pDestructor),
Tom Sepez016a3472015-09-30 15:50:57 -070074 m_pIsolate(isolate) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -070075 v8::Isolate::Scope isolate_scope(isolate);
76 v8::HandleScope handle_scope(isolate);
John Abd-El-Malek5110c472014-05-17 22:33:34 -070077
Tom Sepez016a3472015-09-30 15:50:57 -070078 v8::Local<v8::FunctionTemplate> fun = v8::FunctionTemplate::New(isolate);
79 fun->InstanceTemplate()->SetInternalFieldCount(2);
Tom Sepezc5a14722017-02-24 15:31:12 -080080 fun->SetCallHandler([](const v8::FunctionCallbackInfo<v8::Value>& info) {
81 v8::Local<v8::Object> holder = info.Holder();
82 ASSERT(holder->InternalFieldCount() == 2);
83 holder->SetAlignedPointerInInternalField(0, nullptr);
84 holder->SetAlignedPointerInInternalField(1, nullptr);
85 });
jochen7e6a8482016-07-06 11:02:27 -070086 if (eObjType == FXJSOBJTYPE_GLOBAL) {
87 fun->InstanceTemplate()->Set(
88 v8::Symbol::GetToStringTag(isolate),
89 v8::String::NewFromUtf8(isolate, "global", v8::NewStringType::kNormal)
90 .ToLocalChecked());
91 }
Tom Sepez016a3472015-09-30 15:50:57 -070092 m_FunctionTemplate.Reset(isolate, fun);
93
94 v8::Local<v8::Signature> sig = v8::Signature::New(isolate, fun);
95 m_Signature.Reset(isolate, sig);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070096 }
Tom Sepez016a3472015-09-30 15:50:57 -070097
98 int AssignID() {
99 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(m_pIsolate);
weilia4ad5952016-09-22 10:38:53 -0700100 pData->m_ObjectDefnArray.emplace_back(this);
Lei Zhangd88a3642015-11-10 09:38:57 -0800101 return pData->m_ObjectDefnArray.size() - 1;
Tom Sepez016a3472015-09-30 15:50:57 -0700102 }
103
104 v8::Local<v8::ObjectTemplate> GetInstanceTemplate() {
105 v8::EscapableHandleScope scope(m_pIsolate);
106 v8::Local<v8::FunctionTemplate> function =
107 m_FunctionTemplate.Get(m_pIsolate);
108 return scope.Escape(function->InstanceTemplate());
109 }
110
111 v8::Local<v8::Signature> GetSignature() {
112 v8::EscapableHandleScope scope(m_pIsolate);
113 return scope.Escape(m_Signature.Get(m_pIsolate));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700114 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700115
Tom Sepez892d7512017-02-21 13:57:13 -0800116 const char* const m_ObjName;
Tom Sepezcd56a7d2015-10-06 11:45:28 -0700117 const FXJSOBJTYPE m_ObjType;
tsepezb4694242016-08-15 16:44:55 -0700118 const CFXJS_Engine::Constructor m_pConstructor;
119 const CFXJS_Engine::Destructor m_pDestructor;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700120
Tom Sepez016a3472015-09-30 15:50:57 -0700121 v8::Isolate* m_pIsolate;
122 v8::Global<v8::FunctionTemplate> m_FunctionTemplate;
123 v8::Global<v8::Signature> m_Signature;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700124};
125
Tom Sepez016a3472015-09-30 15:50:57 -0700126static v8::Local<v8::ObjectTemplate> GetGlobalObjectTemplate(
127 v8::Isolate* pIsolate) {
128 int maxID = CFXJS_ObjDefinition::MaxID(pIsolate);
129 for (int i = 0; i < maxID; ++i) {
130 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(pIsolate, i);
Tom Sepezcd56a7d2015-10-06 11:45:28 -0700131 if (pObjDef->m_ObjType == FXJSOBJTYPE_GLOBAL)
Tom Sepez016a3472015-09-30 15:50:57 -0700132 return pObjDef->GetInstanceTemplate();
133 }
Tom Sepez8b315b62015-10-02 08:59:57 -0700134 if (!g_DefaultGlobalObjectTemplate) {
jochen7e6a8482016-07-06 11:02:27 -0700135 v8::Local<v8::ObjectTemplate> hGlobalTemplate =
136 v8::ObjectTemplate::New(pIsolate);
137 hGlobalTemplate->Set(
138 v8::Symbol::GetToStringTag(pIsolate),
139 v8::String::NewFromUtf8(pIsolate, "global", v8::NewStringType::kNormal)
140 .ToLocalChecked());
141 g_DefaultGlobalObjectTemplate =
142 new v8::Global<v8::ObjectTemplate>(pIsolate, hGlobalTemplate);
Tom Sepez8b315b62015-10-02 08:59:57 -0700143 }
144 return g_DefaultGlobalObjectTemplate->Get(pIsolate);
Tom Sepez016a3472015-09-30 15:50:57 -0700145}
146
Tom Sepez39bfe122015-09-17 15:25:23 -0700147void* FXJS_ArrayBufferAllocator::Allocate(size_t length) {
Chris Palmere4b035b2017-03-26 15:48:34 -0700148 if (length > kMaxAllowedBytes)
149 return nullptr;
150 void* p = AllocateUninitialized(length);
151 if (p)
152 memset(p, 0, length);
153 return p;
Tom Sepez7d0fcbf2015-09-15 15:30:34 -0700154}
155
Tom Sepez39bfe122015-09-17 15:25:23 -0700156void* FXJS_ArrayBufferAllocator::AllocateUninitialized(size_t length) {
Chris Palmere4b035b2017-03-26 15:48:34 -0700157 if (length > kMaxAllowedBytes)
158 return nullptr;
159 return pdfium::base::PartitionAllocGeneric(
160 gArrayBufferPartitionAllocator.root(), length, "FXJS_ArrayBuffer");
Tom Sepez7d0fcbf2015-09-15 15:30:34 -0700161}
162
Tom Sepez39bfe122015-09-17 15:25:23 -0700163void FXJS_ArrayBufferAllocator::Free(void* data, size_t length) {
Chris Palmer5ff8c882017-03-31 16:09:54 -0700164 pdfium::base::PartitionFreeGeneric(gArrayBufferPartitionAllocator.root(),
165 data);
Tom Sepez7d0fcbf2015-09-15 15:30:34 -0700166}
167
jinming_wang61dc96f2016-01-28 14:39:56 +0800168void V8TemplateMapTraits::Dispose(v8::Isolate* isolate,
169 v8::Global<v8::Object> value,
170 void* key) {
171 v8::Local<v8::Object> obj = value.Get(isolate);
172 if (obj.IsEmpty())
173 return;
Tom Sepezce84f712017-05-30 17:17:33 -0700174 int id = CFXJS_Engine::GetObjDefnID(obj);
jinming_wang61dc96f2016-01-28 14:39:56 +0800175 if (id == -1)
176 return;
jinming_wang61dc96f2016-01-28 14:39:56 +0800177 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(isolate, id);
178 if (!pObjDef)
179 return;
Tom Sepezce84f712017-05-30 17:17:33 -0700180 if (pObjDef->m_pDestructor) {
181 pObjDef->m_pDestructor(CFXJS_Engine::CurrentEngineFromIsolate(isolate),
182 obj);
183 }
tsepezb4694242016-08-15 16:44:55 -0700184 CFXJS_Engine::FreeObjectPrivate(obj);
jinming_wang61dc96f2016-01-28 14:39:56 +0800185}
186
187V8TemplateMapTraits::MapType* V8TemplateMapTraits::MapFromWeakCallbackInfo(
188 const v8::WeakCallbackInfo<WeakCallbackDataType>& data) {
189 V8TemplateMap* pMap =
weilia4ad5952016-09-22 10:38:53 -0700190 (FXJS_PerIsolateData::Get(data.GetIsolate()))->m_pDynamicObjsMap.get();
jinming_wang61dc96f2016-01-28 14:39:56 +0800191 return pMap ? &pMap->m_map : nullptr;
192}
193
Tom Sepeza72e8e22015-10-07 10:17:53 -0700194void FXJS_Initialize(unsigned int embedderDataSlot, v8::Isolate* pIsolate) {
Lei Zhang4d4a4422015-10-08 12:00:14 -0700195 if (g_isolate) {
196 ASSERT(g_embedderDataSlot == embedderDataSlot);
197 ASSERT(g_isolate == pIsolate);
198 return;
199 }
Tom Sepeza72e8e22015-10-07 10:17:53 -0700200 g_embedderDataSlot = embedderDataSlot;
201 g_isolate = pIsolate;
202}
203
204void FXJS_Release() {
Lei Zhang4d4a4422015-10-08 12:00:14 -0700205 ASSERT(!g_isolate || g_isolate_ref_count == 0);
Lei Zhangb3914932015-10-08 12:08:47 -0700206 delete g_DefaultGlobalObjectTemplate;
Tom Sepeza72e8e22015-10-07 10:17:53 -0700207 g_DefaultGlobalObjectTemplate = nullptr;
208 g_isolate = nullptr;
209
210 delete g_arrayBufferAllocator;
211 g_arrayBufferAllocator = nullptr;
212}
213
214bool FXJS_GetIsolate(v8::Isolate** pResultIsolate) {
215 if (g_isolate) {
216 *pResultIsolate = g_isolate;
217 return false;
218 }
219 // Provide backwards compatibility when no external isolate.
220 if (!g_arrayBufferAllocator)
221 g_arrayBufferAllocator = new FXJS_ArrayBufferAllocator();
222 v8::Isolate::CreateParams params;
223 params.array_buffer_allocator = g_arrayBufferAllocator;
224 *pResultIsolate = v8::Isolate::New(params);
225 return true;
226}
227
Lei Zhang3fa115b2015-10-08 12:04:47 -0700228size_t FXJS_GlobalIsolateRefCount() {
229 return g_isolate_ref_count;
230}
231
weili625ad662016-06-15 11:21:33 -0700232V8TemplateMap::V8TemplateMap(v8::Isolate* isolate) : m_map(isolate) {}
233
234V8TemplateMap::~V8TemplateMap() {}
235
236void V8TemplateMap::set(void* key, v8::Local<v8::Object> handle) {
237 ASSERT(!m_map.Contains(key));
238 m_map.Set(key, handle);
239}
240
241FXJS_PerIsolateData::~FXJS_PerIsolateData() {}
242
Tom Sepezed7b2b52015-09-22 08:36:17 -0700243// static
244void FXJS_PerIsolateData::SetUp(v8::Isolate* pIsolate) {
Tom Sepez7d0fcbf2015-09-15 15:30:34 -0700245 if (!pIsolate->GetData(g_embedderDataSlot))
weilia4ad5952016-09-22 10:38:53 -0700246 pIsolate->SetData(g_embedderDataSlot, new FXJS_PerIsolateData(pIsolate));
Tom Sepezed7b2b52015-09-22 08:36:17 -0700247}
248
249// static
250FXJS_PerIsolateData* FXJS_PerIsolateData::Get(v8::Isolate* pIsolate) {
251 return static_cast<FXJS_PerIsolateData*>(
252 pIsolate->GetData(g_embedderDataSlot));
Tom Sepez7d0fcbf2015-09-15 15:30:34 -0700253}
254
weilia4ad5952016-09-22 10:38:53 -0700255FXJS_PerIsolateData::FXJS_PerIsolateData(v8::Isolate* pIsolate)
256 : m_pDynamicObjsMap(new V8TemplateMap(pIsolate)) {}
weili625ad662016-06-15 11:21:33 -0700257
tsepeza4941912016-08-15 11:40:12 -0700258CFXJS_Engine::CFXJS_Engine() : m_isolate(nullptr) {}
tsepeza4941912016-08-15 11:40:12 -0700259
weili0b2a9872016-09-21 11:50:43 -0700260CFXJS_Engine::CFXJS_Engine(v8::Isolate* pIsolate) : m_isolate(pIsolate) {}
261
tsepezb4694242016-08-15 16:44:55 -0700262CFXJS_Engine::~CFXJS_Engine() {
263 m_V8PersistentContext.Reset();
264}
Tom Sepez7d0fcbf2015-09-15 15:30:34 -0700265
tsepezb4694242016-08-15 16:44:55 -0700266// static
267CFXJS_Engine* CFXJS_Engine::CurrentEngineFromIsolate(v8::Isolate* pIsolate) {
268 return static_cast<CFXJS_Engine*>(
269 pIsolate->GetCurrentContext()->GetAlignedPointerFromEmbedderData(
270 kPerContextDataIndex));
271}
272
tsepezb4694242016-08-15 16:44:55 -0700273// static
274int CFXJS_Engine::GetObjDefnID(v8::Local<v8::Object> pObj) {
Tom Sepez3f72fb42017-02-27 11:43:55 -0800275 CFXJS_PerObjectData* pData = CFXJS_PerObjectData::GetFromObject(pObj);
276 return pData ? pData->m_ObjDefID : -1;
tsepezb4694242016-08-15 16:44:55 -0700277}
278
279// static
280void CFXJS_Engine::FreeObjectPrivate(void* pPerObjectData) {
281 delete static_cast<CFXJS_PerObjectData*>(pPerObjectData);
282}
283
284// static
285void CFXJS_Engine::FreeObjectPrivate(v8::Local<v8::Object> pObj) {
Tom Sepez3f72fb42017-02-27 11:43:55 -0800286 CFXJS_PerObjectData* pData = CFXJS_PerObjectData::GetFromObject(pObj);
tsepezb4694242016-08-15 16:44:55 -0700287 pObj->SetAlignedPointerInInternalField(0, nullptr);
Tom Sepez3f72fb42017-02-27 11:43:55 -0800288 pObj->SetAlignedPointerInInternalField(1, nullptr);
289 delete pData;
tsepezb4694242016-08-15 16:44:55 -0700290}
291
Tom Sepez892d7512017-02-21 13:57:13 -0800292int CFXJS_Engine::DefineObj(const char* sObjName,
tsepezb4694242016-08-15 16:44:55 -0700293 FXJSOBJTYPE eObjType,
294 CFXJS_Engine::Constructor pConstructor,
295 CFXJS_Engine::Destructor pDestructor) {
296 v8::Isolate::Scope isolate_scope(m_isolate);
297 v8::HandleScope handle_scope(m_isolate);
298 FXJS_PerIsolateData::SetUp(m_isolate);
Tom Sepez016a3472015-09-30 15:50:57 -0700299 CFXJS_ObjDefinition* pObjDef = new CFXJS_ObjDefinition(
tsepezb4694242016-08-15 16:44:55 -0700300 m_isolate, sObjName, eObjType, pConstructor, pDestructor);
Tom Sepez016a3472015-09-30 15:50:57 -0700301 return pObjDef->AssignID();
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700302}
303
tsepezb4694242016-08-15 16:44:55 -0700304void CFXJS_Engine::DefineObjMethod(int nObjDefnID,
Tom Sepez9b99b632017-02-21 15:05:57 -0800305 const char* sMethodName,
tsepezb4694242016-08-15 16:44:55 -0700306 v8::FunctionCallback pMethodCall) {
307 v8::Isolate::Scope isolate_scope(m_isolate);
308 v8::HandleScope handle_scope(m_isolate);
Tom Sepezed7b2b52015-09-22 08:36:17 -0700309 CFXJS_ObjDefinition* pObjDef =
tsepezb4694242016-08-15 16:44:55 -0700310 CFXJS_ObjDefinition::ForID(m_isolate, nObjDefnID);
jochenc4dedf32016-07-06 05:26:23 -0700311 v8::Local<v8::FunctionTemplate> fun = v8::FunctionTemplate::New(
tsepezb4694242016-08-15 16:44:55 -0700312 m_isolate, pMethodCall, v8::Local<v8::Value>(), pObjDef->GetSignature());
jochenc4dedf32016-07-06 05:26:23 -0700313 fun->RemovePrototype();
Tom Sepezf6ca07b2017-06-01 09:17:18 -0700314 pObjDef->GetInstanceTemplate()->Set(NewString(sMethodName), fun,
315 v8::ReadOnly);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700316}
317
tsepezb4694242016-08-15 16:44:55 -0700318void CFXJS_Engine::DefineObjProperty(int nObjDefnID,
Tom Sepez4d5b8c52017-02-21 15:17:07 -0800319 const char* sPropName,
tsepezb4694242016-08-15 16:44:55 -0700320 v8::AccessorGetterCallback pPropGet,
321 v8::AccessorSetterCallback pPropPut) {
322 v8::Isolate::Scope isolate_scope(m_isolate);
323 v8::HandleScope handle_scope(m_isolate);
Tom Sepezed7b2b52015-09-22 08:36:17 -0700324 CFXJS_ObjDefinition* pObjDef =
tsepezb4694242016-08-15 16:44:55 -0700325 CFXJS_ObjDefinition::ForID(m_isolate, nObjDefnID);
Tom Sepezf6ca07b2017-06-01 09:17:18 -0700326 pObjDef->GetInstanceTemplate()->SetAccessor(NewString(sPropName), pPropGet,
327 pPropPut);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700328}
329
tsepezb4694242016-08-15 16:44:55 -0700330void CFXJS_Engine::DefineObjAllProperties(
331 int nObjDefnID,
332 v8::NamedPropertyQueryCallback pPropQurey,
333 v8::NamedPropertyGetterCallback pPropGet,
334 v8::NamedPropertySetterCallback pPropPut,
335 v8::NamedPropertyDeleterCallback pPropDel) {
336 v8::Isolate::Scope isolate_scope(m_isolate);
337 v8::HandleScope handle_scope(m_isolate);
Tom Sepezed7b2b52015-09-22 08:36:17 -0700338 CFXJS_ObjDefinition* pObjDef =
tsepezb4694242016-08-15 16:44:55 -0700339 CFXJS_ObjDefinition::ForID(m_isolate, nObjDefnID);
Tom Sepez016a3472015-09-30 15:50:57 -0700340 pObjDef->GetInstanceTemplate()->SetNamedPropertyHandler(pPropGet, pPropPut,
341 pPropQurey, pPropDel);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700342}
343
tsepezb4694242016-08-15 16:44:55 -0700344void CFXJS_Engine::DefineObjConst(int nObjDefnID,
Tom Sepez55db0912017-02-21 15:26:52 -0800345 const char* sConstName,
tsepezb4694242016-08-15 16:44:55 -0700346 v8::Local<v8::Value> pDefault) {
347 v8::Isolate::Scope isolate_scope(m_isolate);
348 v8::HandleScope handle_scope(m_isolate);
Tom Sepezed7b2b52015-09-22 08:36:17 -0700349 CFXJS_ObjDefinition* pObjDef =
tsepezb4694242016-08-15 16:44:55 -0700350 CFXJS_ObjDefinition::ForID(m_isolate, nObjDefnID);
Tom Sepez55db0912017-02-21 15:26:52 -0800351 pObjDef->GetInstanceTemplate()->Set(m_isolate, sConstName, pDefault);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700352}
353
Tom Sepez9b99b632017-02-21 15:05:57 -0800354void CFXJS_Engine::DefineGlobalMethod(const char* sMethodName,
tsepezb4694242016-08-15 16:44:55 -0700355 v8::FunctionCallback pMethodCall) {
356 v8::Isolate::Scope isolate_scope(m_isolate);
357 v8::HandleScope handle_scope(m_isolate);
jochenc4dedf32016-07-06 05:26:23 -0700358 v8::Local<v8::FunctionTemplate> fun =
tsepezb4694242016-08-15 16:44:55 -0700359 v8::FunctionTemplate::New(m_isolate, pMethodCall);
jochenc4dedf32016-07-06 05:26:23 -0700360 fun->RemovePrototype();
Tom Sepezf6ca07b2017-06-01 09:17:18 -0700361 GetGlobalObjectTemplate(m_isolate)->Set(NewString(sMethodName), fun,
362 v8::ReadOnly);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700363}
364
tsepezb4694242016-08-15 16:44:55 -0700365void CFXJS_Engine::DefineGlobalConst(const wchar_t* sConstName,
366 v8::FunctionCallback pConstGetter) {
367 v8::Isolate::Scope isolate_scope(m_isolate);
368 v8::HandleScope handle_scope(m_isolate);
jochenc4dedf32016-07-06 05:26:23 -0700369 v8::Local<v8::FunctionTemplate> fun =
tsepezb4694242016-08-15 16:44:55 -0700370 v8::FunctionTemplate::New(m_isolate, pConstGetter);
jochenc4dedf32016-07-06 05:26:23 -0700371 fun->RemovePrototype();
Tom Sepezf6ca07b2017-06-01 09:17:18 -0700372 GetGlobalObjectTemplate(m_isolate)->SetAccessorProperty(NewString(sConstName),
373 fun);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700374}
375
tsepezb4694242016-08-15 16:44:55 -0700376void CFXJS_Engine::InitializeEngine() {
377 if (m_isolate == g_isolate)
Lei Zhang4d4a4422015-10-08 12:00:14 -0700378 ++g_isolate_ref_count;
379
tsepezb4694242016-08-15 16:44:55 -0700380 v8::Isolate::Scope isolate_scope(m_isolate);
381 v8::HandleScope handle_scope(m_isolate);
dsinclair7d554c92016-06-20 06:06:31 -0700382
383 // This has to happen before we call GetGlobalObjectTemplate because that
tsepezb4694242016-08-15 16:44:55 -0700384 // method gets the PerIsolateData from m_isolate.
385 FXJS_PerIsolateData::SetUp(m_isolate);
dsinclair7d554c92016-06-20 06:06:31 -0700386
Tom Sepez016a3472015-09-30 15:50:57 -0700387 v8::Local<v8::Context> v8Context =
tsepezb4694242016-08-15 16:44:55 -0700388 v8::Context::New(m_isolate, nullptr, GetGlobalObjectTemplate(m_isolate));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700389 v8::Context::Scope context_scope(v8Context);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700390
tsepezb4694242016-08-15 16:44:55 -0700391 v8Context->SetAlignedPointerInEmbedderData(kPerContextDataIndex, this);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700392
tsepezb4694242016-08-15 16:44:55 -0700393 int maxID = CFXJS_ObjDefinition::MaxID(m_isolate);
394 m_StaticObjects.resize(maxID + 1);
Tom Sepezed7b2b52015-09-22 08:36:17 -0700395 for (int i = 0; i < maxID; ++i) {
tsepezb4694242016-08-15 16:44:55 -0700396 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(m_isolate, i);
Tom Sepezcd56a7d2015-10-06 11:45:28 -0700397 if (pObjDef->m_ObjType == FXJSOBJTYPE_GLOBAL) {
Tom Sepez3f72fb42017-02-27 11:43:55 -0800398 CFXJS_PerObjectData::SetInObject(new CFXJS_PerObjectData(i),
399 v8Context->Global()
400 ->GetPrototype()
401 ->ToObject(v8Context)
402 .ToLocalChecked());
403 if (pObjDef->m_pConstructor) {
tsepezb4694242016-08-15 16:44:55 -0700404 pObjDef->m_pConstructor(this, v8Context->Global()
405 ->GetPrototype()
406 ->ToObject(v8Context)
407 .ToLocalChecked());
Tom Sepez3f72fb42017-02-27 11:43:55 -0800408 }
Tom Sepezcd56a7d2015-10-06 11:45:28 -0700409 } else if (pObjDef->m_ObjType == FXJSOBJTYPE_STATIC) {
Tom Sepezf6ca07b2017-06-01 09:17:18 -0700410 v8::Local<v8::String> pObjName = NewString(pObjDef->m_ObjName);
tsepezb4694242016-08-15 16:44:55 -0700411 v8::Local<v8::Object> obj = NewFxDynamicObj(i, true);
Tom Sepez63b2fc72017-08-14 16:24:29 -0700412 if (!obj.IsEmpty()) {
413 v8Context->Global()->Set(v8Context, pObjName, obj).FromJust();
414 m_StaticObjects[i] = new v8::Global<v8::Object>(m_isolate, obj);
415 } else {
416 m_StaticObjects[i] = nullptr;
417 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700418 }
419 }
tsepezb4694242016-08-15 16:44:55 -0700420 m_V8PersistentContext.Reset(m_isolate, v8Context);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700421}
422
tsepezb4694242016-08-15 16:44:55 -0700423void CFXJS_Engine::ReleaseEngine() {
424 v8::Isolate::Scope isolate_scope(m_isolate);
425 v8::HandleScope handle_scope(m_isolate);
Tom Sepez808a99e2015-09-10 12:28:37 -0700426 v8::Local<v8::Context> context =
tsepezb4694242016-08-15 16:44:55 -0700427 v8::Local<v8::Context>::New(m_isolate, m_V8PersistentContext);
Tom Sepez808a99e2015-09-10 12:28:37 -0700428 v8::Context::Scope context_scope(context);
tsepezb4694242016-08-15 16:44:55 -0700429 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(m_isolate);
Tom Sepez129a60e2016-03-24 09:19:20 -0700430 if (!pData)
431 return;
Tom Sepez129a60e2016-03-24 09:19:20 -0700432
tsepezb4694242016-08-15 16:44:55 -0700433 m_ConstArrays.clear();
434
435 int maxID = CFXJS_ObjDefinition::MaxID(m_isolate);
Tom Sepezed7b2b52015-09-22 08:36:17 -0700436 for (int i = 0; i < maxID; ++i) {
tsepezb4694242016-08-15 16:44:55 -0700437 CFXJS_ObjDefinition* pObjDef = CFXJS_ObjDefinition::ForID(m_isolate, i);
Oliver Chang8f16b692015-10-27 09:34:49 -0700438 v8::Local<v8::Object> pObj;
439 if (pObjDef->m_ObjType == FXJSOBJTYPE_GLOBAL) {
440 pObj =
441 context->Global()->GetPrototype()->ToObject(context).ToLocalChecked();
tsepezb4694242016-08-15 16:44:55 -0700442 } else if (m_StaticObjects[i] && !m_StaticObjects[i]->IsEmpty()) {
443 pObj = v8::Local<v8::Object>::New(m_isolate, *m_StaticObjects[i]);
444 delete m_StaticObjects[i];
445 m_StaticObjects[i] = nullptr;
Oliver Chang8f16b692015-10-27 09:34:49 -0700446 }
447
448 if (!pObj.IsEmpty()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700449 if (pObjDef->m_pDestructor)
tsepezb4694242016-08-15 16:44:55 -0700450 pObjDef->m_pDestructor(this, pObj);
451 FreeObjectPrivate(pObj);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700452 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700453 }
Tom Sepez808a99e2015-09-10 12:28:37 -0700454
tsepezb4694242016-08-15 16:44:55 -0700455 m_V8PersistentContext.Reset();
456
457 if (m_isolate == g_isolate && --g_isolate_ref_count > 0)
Tom Sepez4237aed2015-11-10 15:19:17 -0800458 return;
459
Tom Sepez129a60e2016-03-24 09:19:20 -0700460 delete pData;
weilia4ad5952016-09-22 10:38:53 -0700461 m_isolate->SetData(g_embedderDataSlot, nullptr);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700462}
463
tsepezb4694242016-08-15 16:44:55 -0700464int CFXJS_Engine::Execute(const CFX_WideString& script, FXJSErr* pError) {
465 v8::Isolate::Scope isolate_scope(m_isolate);
466 v8::TryCatch try_catch(m_isolate);
tsepezb4694242016-08-15 16:44:55 -0700467 v8::Local<v8::Context> context = m_isolate->GetCurrentContext();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700468 v8::Local<v8::Script> compiled_script;
Tom Sepezf6ca07b2017-06-01 09:17:18 -0700469 if (!v8::Script::Compile(context, NewString(script.AsStringC()))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700470 .ToLocal(&compiled_script)) {
471 v8::String::Utf8Value error(try_catch.Exception());
Tom Sepez39bfe122015-09-17 15:25:23 -0700472 // TODO(tsepez): return error via pError->message.
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700473 return -1;
474 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700475
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700476 v8::Local<v8::Value> result;
477 if (!compiled_script->Run(context).ToLocal(&result)) {
478 v8::String::Utf8Value error(try_catch.Exception());
Tom Sepez39bfe122015-09-17 15:25:23 -0700479 // TODO(tsepez): return error via pError->message.
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700480 return -1;
481 }
482 return 0;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700483}
484
tsepezb4694242016-08-15 16:44:55 -0700485v8::Local<v8::Object> CFXJS_Engine::NewFxDynamicObj(int nObjDefnID,
486 bool bStatic) {
487 v8::Isolate::Scope isolate_scope(m_isolate);
488 v8::Local<v8::Context> context = m_isolate->GetCurrentContext();
Tom Sepezed7b2b52015-09-22 08:36:17 -0700489 if (nObjDefnID == -1) {
tsepezb4694242016-08-15 16:44:55 -0700490 v8::Local<v8::ObjectTemplate> objTempl = v8::ObjectTemplate::New(m_isolate);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700491 v8::Local<v8::Object> obj;
Tom Sepez016a3472015-09-30 15:50:57 -0700492 if (!objTempl->NewInstance(context).ToLocal(&obj))
493 return v8::Local<v8::Object>();
494 return obj;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700495 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700496
tsepezb4694242016-08-15 16:44:55 -0700497 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(m_isolate);
Tom Sepezed7b2b52015-09-22 08:36:17 -0700498 if (!pData)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700499 return v8::Local<v8::Object>();
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700500
tsepezb4694242016-08-15 16:44:55 -0700501 if (nObjDefnID < 0 || nObjDefnID >= CFXJS_ObjDefinition::MaxID(m_isolate))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700502 return v8::Local<v8::Object>();
Tom Sepezed7b2b52015-09-22 08:36:17 -0700503
504 CFXJS_ObjDefinition* pObjDef =
tsepezb4694242016-08-15 16:44:55 -0700505 CFXJS_ObjDefinition::ForID(m_isolate, nObjDefnID);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700506 v8::Local<v8::Object> obj;
Tom Sepez016a3472015-09-30 15:50:57 -0700507 if (!pObjDef->GetInstanceTemplate()->NewInstance(context).ToLocal(&obj))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700508 return v8::Local<v8::Object>();
Tom Sepez39bfe122015-09-17 15:25:23 -0700509
Tom Sepez3f72fb42017-02-27 11:43:55 -0800510 CFXJS_PerObjectData* pObjData = new CFXJS_PerObjectData(nObjDefnID);
511 CFXJS_PerObjectData::SetInObject(pObjData, obj);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700512 if (pObjDef->m_pConstructor)
tsepezb4694242016-08-15 16:44:55 -0700513 pObjDef->m_pConstructor(this, obj);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700514
Tom Sepez3f72fb42017-02-27 11:43:55 -0800515 if (!bStatic && FXJS_PerIsolateData::Get(m_isolate)->m_pDynamicObjsMap)
516 FXJS_PerIsolateData::Get(m_isolate)->m_pDynamicObjsMap->set(pObjData, obj);
517
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700518 return obj;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700519}
520
tsepezb4694242016-08-15 16:44:55 -0700521v8::Local<v8::Object> CFXJS_Engine::GetThisObj() {
522 v8::Isolate::Scope isolate_scope(m_isolate);
523 if (!FXJS_PerIsolateData::Get(m_isolate))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700524 return v8::Local<v8::Object>();
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700525
Tom Sepezed7b2b52015-09-22 08:36:17 -0700526 // Return the global object.
tsepezb4694242016-08-15 16:44:55 -0700527 v8::Local<v8::Context> context = m_isolate->GetCurrentContext();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700528 return context->Global()->GetPrototype()->ToObject(context).ToLocalChecked();
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700529}
530
tsepezb4694242016-08-15 16:44:55 -0700531void CFXJS_Engine::Error(const CFX_WideString& message) {
Tom Sepezf6ca07b2017-06-01 09:17:18 -0700532 m_isolate->ThrowException(NewString(message.AsStringC()));
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700533}
534
tsepezb4694242016-08-15 16:44:55 -0700535void CFXJS_Engine::SetObjectPrivate(v8::Local<v8::Object> pObj, void* p) {
Tom Sepez3f72fb42017-02-27 11:43:55 -0800536 CFXJS_PerObjectData* pPerObjectData =
537 CFXJS_PerObjectData::GetFromObject(pObj);
Tom Sepez6cf117c2015-11-06 14:52:10 -0800538 if (!pPerObjectData)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700539 return;
Tom Sepez6cf117c2015-11-06 14:52:10 -0800540 pPerObjectData->m_pPrivate = p;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700541}
542
tsepezb4694242016-08-15 16:44:55 -0700543void* CFXJS_Engine::GetObjectPrivate(v8::Local<v8::Object> pObj) {
Tom Sepez3f72fb42017-02-27 11:43:55 -0800544 CFXJS_PerObjectData* pData = CFXJS_PerObjectData::GetFromObject(pObj);
545 if (!pData && !pObj.IsEmpty()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700546 // It could be a global proxy object.
547 v8::Local<v8::Value> v = pObj->GetPrototype();
tsepezb4694242016-08-15 16:44:55 -0700548 v8::Local<v8::Context> context = m_isolate->GetCurrentContext();
Lei Zhang4d4a4422015-10-08 12:00:14 -0700549 if (v->IsObject()) {
Tom Sepez3f72fb42017-02-27 11:43:55 -0800550 pData = CFXJS_PerObjectData::GetFromObject(
551 v->ToObject(context).ToLocalChecked());
Lei Zhang4d4a4422015-10-08 12:00:14 -0700552 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700553 }
Tom Sepez3f72fb42017-02-27 11:43:55 -0800554 return pData ? pData->m_pPrivate : nullptr;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700555}
556
tsepezb4694242016-08-15 16:44:55 -0700557v8::Local<v8::Value> CFXJS_Engine::GetObjectProperty(
tsepez018935c2016-04-15 13:15:12 -0700558 v8::Local<v8::Object> pObj,
559 const CFX_WideString& wsPropertyName) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700560 if (pObj.IsEmpty())
561 return v8::Local<v8::Value>();
562 v8::Local<v8::Value> val;
Tom Sepezc6dc69f2017-02-23 09:53:09 -0800563 if (!pObj->Get(m_isolate->GetCurrentContext(),
564 NewString(wsPropertyName.AsStringC()))
Dan Sinclairf766ad22016-03-14 13:51:24 -0400565 .ToLocal(&val))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700566 return v8::Local<v8::Value>();
567 return val;
568}
569
tsepezb4694242016-08-15 16:44:55 -0700570std::vector<CFX_WideString> CFXJS_Engine::GetObjectPropertyNames(
tsepezd0b6ed12016-08-11 19:50:57 -0700571 v8::Local<v8::Object> pObj) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700572 if (pObj.IsEmpty())
tsepezd0b6ed12016-08-11 19:50:57 -0700573 return std::vector<CFX_WideString>();
574
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700575 v8::Local<v8::Array> val;
tsepezb4694242016-08-15 16:44:55 -0700576 v8::Local<v8::Context> context = m_isolate->GetCurrentContext();
577 if (!pObj->GetPropertyNames(context).ToLocal(&val))
tsepezd0b6ed12016-08-11 19:50:57 -0700578 return std::vector<CFX_WideString>();
579
580 std::vector<CFX_WideString> result;
581 for (uint32_t i = 0; i < val->Length(); ++i) {
tsepeze6cf0132017-01-18 14:38:18 -0800582 result.push_back(ToWideString(val->Get(context, i).ToLocalChecked()));
tsepezd0b6ed12016-08-11 19:50:57 -0700583 }
584
585 return result;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700586}
587
tsepeze6cf0132017-01-18 14:38:18 -0800588void CFXJS_Engine::PutObjectProperty(v8::Local<v8::Object> pObj,
589 const CFX_WideString& wsPropertyName,
590 v8::Local<v8::Value> pPut) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700591 if (pObj.IsEmpty())
592 return;
Tom Sepezc6dc69f2017-02-23 09:53:09 -0800593 pObj->Set(m_isolate->GetCurrentContext(),
594 NewString(wsPropertyName.AsStringC()), pPut)
Dan Sinclairf766ad22016-03-14 13:51:24 -0400595 .FromJust();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700596}
597
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700598
tsepezb4694242016-08-15 16:44:55 -0700599v8::Local<v8::Array> CFXJS_Engine::NewArray() {
600 return v8::Array::New(m_isolate);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700601}
602
tsepezb4694242016-08-15 16:44:55 -0700603unsigned CFXJS_Engine::PutArrayElement(v8::Local<v8::Array> pArray,
604 unsigned index,
605 v8::Local<v8::Value> pValue) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700606 if (pArray.IsEmpty())
607 return 0;
tsepezb4694242016-08-15 16:44:55 -0700608 if (pArray->Set(m_isolate->GetCurrentContext(), index, pValue).IsNothing())
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700609 return 0;
610 return 1;
611}
612
tsepezb4694242016-08-15 16:44:55 -0700613v8::Local<v8::Value> CFXJS_Engine::GetArrayElement(v8::Local<v8::Array> pArray,
614 unsigned index) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700615 if (pArray.IsEmpty())
616 return v8::Local<v8::Value>();
617 v8::Local<v8::Value> val;
tsepezb4694242016-08-15 16:44:55 -0700618 if (!pArray->Get(m_isolate->GetCurrentContext(), index).ToLocal(&val))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700619 return v8::Local<v8::Value>();
620 return val;
621}
622
tsepezb4694242016-08-15 16:44:55 -0700623unsigned CFXJS_Engine::GetArrayLength(v8::Local<v8::Array> pArray) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700624 if (pArray.IsEmpty())
625 return 0;
626 return pArray->Length();
627}
628
tsepezb4694242016-08-15 16:44:55 -0700629v8::Local<v8::Context> CFXJS_Engine::NewLocalContext() {
630 return v8::Local<v8::Context>::New(m_isolate, m_V8PersistentContext);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700631}
632
tsepezb4694242016-08-15 16:44:55 -0700633v8::Local<v8::Context> CFXJS_Engine::GetPersistentContext() {
634 return m_V8PersistentContext.Get(m_isolate);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700635}
636
Tom Sepezf6ca07b2017-06-01 09:17:18 -0700637v8::Local<v8::Number> CFXJS_Engine::NewNumber(int number) {
tsepezb4694242016-08-15 16:44:55 -0700638 return v8::Int32::New(m_isolate, number);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700639}
640
Tom Sepezf6ca07b2017-06-01 09:17:18 -0700641v8::Local<v8::Number> CFXJS_Engine::NewNumber(double number) {
tsepezb4694242016-08-15 16:44:55 -0700642 return v8::Number::New(m_isolate, number);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700643}
644
Tom Sepezf6ca07b2017-06-01 09:17:18 -0700645v8::Local<v8::Number> CFXJS_Engine::NewNumber(float number) {
tsepezb4694242016-08-15 16:44:55 -0700646 return v8::Number::New(m_isolate, (float)number);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700647}
648
Tom Sepezf6ca07b2017-06-01 09:17:18 -0700649v8::Local<v8::Boolean> CFXJS_Engine::NewBoolean(bool b) {
tsepezb4694242016-08-15 16:44:55 -0700650 return v8::Boolean::New(m_isolate, b);
651}
652
Tom Sepezf6ca07b2017-06-01 09:17:18 -0700653v8::Local<v8::String> CFXJS_Engine::NewString(const CFX_ByteStringC& str) {
Tom Sepezc6dc69f2017-02-23 09:53:09 -0800654 v8::Isolate* pIsolate = m_isolate ? m_isolate : v8::Isolate::GetCurrent();
Tom Sepez33b42e42017-07-19 13:19:12 -0700655 return v8::String::NewFromUtf8(pIsolate, str.unterminated_c_str(),
Tom Sepezc6dc69f2017-02-23 09:53:09 -0800656 v8::NewStringType::kNormal, str.GetLength())
657 .ToLocalChecked();
658}
659
Tom Sepezf6ca07b2017-06-01 09:17:18 -0700660v8::Local<v8::String> CFXJS_Engine::NewString(const CFX_WideStringC& str) {
661 // Conversion from pdfium's wchar_t wide-strings to v8's uint16_t
662 // wide-strings isn't handled by v8, so use UTF8 as a common
663 // intermediate format.
Tom Sepezc6dc69f2017-02-23 09:53:09 -0800664 return NewString(FX_UTF8Encode(str).AsStringC());
tsepezb4694242016-08-15 16:44:55 -0700665}
666
667v8::Local<v8::Value> CFXJS_Engine::NewNull() {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700668 return v8::Local<v8::Value>();
669}
670
tsepezb4694242016-08-15 16:44:55 -0700671v8::Local<v8::Date> CFXJS_Engine::NewDate(double d) {
672 return v8::Date::New(m_isolate->GetCurrentContext(), d)
tsepez135b9982016-08-05 09:32:50 -0700673 .ToLocalChecked()
674 .As<v8::Date>();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700675}
676
tsepezb4694242016-08-15 16:44:55 -0700677int CFXJS_Engine::ToInt32(v8::Local<v8::Value> pValue) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700678 if (pValue.IsEmpty())
679 return 0;
tsepezb4694242016-08-15 16:44:55 -0700680 v8::Local<v8::Context> context = m_isolate->GetCurrentContext();
Tom Sepezfb7021c2017-05-31 10:29:25 -0700681 v8::MaybeLocal<v8::Int32> maybe_int32 = pValue->ToInt32(context);
682 if (maybe_int32.IsEmpty())
683 return 0;
684 return maybe_int32.ToLocalChecked()->Value();
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700685}
686
tsepezb4694242016-08-15 16:44:55 -0700687bool CFXJS_Engine::ToBoolean(v8::Local<v8::Value> pValue) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700688 if (pValue.IsEmpty())
689 return false;
tsepezb4694242016-08-15 16:44:55 -0700690 v8::Local<v8::Context> context = m_isolate->GetCurrentContext();
Tom Sepezfb7021c2017-05-31 10:29:25 -0700691 v8::MaybeLocal<v8::Boolean> maybe_boolean = pValue->ToBoolean(context);
692 if (maybe_boolean.IsEmpty())
693 return false;
694 return maybe_boolean.ToLocalChecked()->Value();
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700695}
696
tsepeze6cf0132017-01-18 14:38:18 -0800697double CFXJS_Engine::ToDouble(v8::Local<v8::Value> pValue) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700698 if (pValue.IsEmpty())
699 return 0.0;
tsepezb4694242016-08-15 16:44:55 -0700700 v8::Local<v8::Context> context = m_isolate->GetCurrentContext();
Tom Sepezfb7021c2017-05-31 10:29:25 -0700701 v8::MaybeLocal<v8::Number> maybe_number = pValue->ToNumber(context);
702 if (maybe_number.IsEmpty())
703 return 0.0;
704 return maybe_number.ToLocalChecked()->Value();
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700705}
706
tsepeze6cf0132017-01-18 14:38:18 -0800707CFX_WideString CFXJS_Engine::ToWideString(v8::Local<v8::Value> pValue) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700708 if (pValue.IsEmpty())
tsepeze6cf0132017-01-18 14:38:18 -0800709 return CFX_WideString();
tsepezb4694242016-08-15 16:44:55 -0700710 v8::Local<v8::Context> context = m_isolate->GetCurrentContext();
Tom Sepezfb7021c2017-05-31 10:29:25 -0700711 v8::MaybeLocal<v8::String> maybe_string = pValue->ToString(context);
712 if (maybe_string.IsEmpty())
713 return CFX_WideString();
714 v8::String::Utf8Value s(maybe_string.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}