blob: c4d46a29f694c2346de6a9bcde619a5281bd5136 [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
Tom Sepezb7c7df62018-02-09 19:08:59 +00007#include "fxjs/cfxjs_engine.h"
John Abd-El-Malek5110c472014-05-17 22:33:34 -07008
Tom Sepez57e09772018-02-05 18:52:09 +00009#include <memory>
10#include <utility>
Dan Sinclair3ebd1212016-03-09 09:59:23 -050011#include <vector>
12
Tom Sepez98b356a2018-07-16 21:35:06 +000013#include "core/fxcrt/unowned_ptr.h"
Tom Sepez57e09772018-02-05 18:52:09 +000014#include "fxjs/cjs_object.h"
Tom Sepez70e52142018-12-13 20:07:50 +000015#include "fxjs/xfa/cfxjse_runtimedata.h"
Lei Zhang99f5bbb2018-10-09 21:31:28 +000016#include "third_party/base/ptr_util.h"
Lei Zhang35d45872018-10-12 19:07:03 +000017#include "third_party/base/stl_util.h"
Lei Zhang378ec542018-10-18 19:15:47 +000018#include "v8/include/v8-util.h"
Lei Zhang4d4a4422015-10-08 12:00:14 -070019
Tom Sepezdb8ef922018-02-13 18:23:28 +000020class CFXJS_PerObjectData;
21
Lei Zhangd3b028b2018-11-30 22:22:00 +000022namespace {
23
24unsigned int g_embedderDataSlot = 1u;
25v8::Isolate* g_isolate = nullptr;
26size_t g_isolate_ref_count = 0;
27CFX_V8ArrayBufferAllocator* g_arrayBufferAllocator = nullptr;
28v8::Global<v8::ObjectTemplate>* g_DefaultGlobalObjectTemplate = nullptr;
29const wchar_t kPerObjectDataTag[] = L"CFXJS_PerObjectData";
30
31void* GetAlignedPointerForPerObjectDataTag() {
32 return const_cast<void*>(static_cast<const void*>(kPerObjectDataTag));
33}
34
Lei Zhang696f5d32018-12-08 00:02:50 +000035std::pair<int, int> GetLineAndColumnFromError(v8::Local<v8::Message> message,
36 v8::Local<v8::Context> context) {
37 if (message.IsEmpty())
38 return std::make_pair(-1, -1);
39 return std::make_pair(message->GetLineNumber(context).FromMaybe(-1),
40 message->GetStartColumn());
41}
42
Lei Zhangd3b028b2018-11-30 22:22:00 +000043} // namespace
Tom Sepez016a3472015-09-30 15:50:57 -070044
Tom Sepezb7c7df62018-02-09 19:08:59 +000045// Global weak map to save dynamic objects.
Tom Sepez55865452018-08-27 20:18:04 +000046class V8TemplateMapTraits final
Tom Sepezdb8ef922018-02-13 18:23:28 +000047 : public v8::StdMapTraits<CFXJS_PerObjectData*, v8::Object> {
Tom Sepezb7c7df62018-02-09 19:08:59 +000048 public:
Tom Sepezdb8ef922018-02-13 18:23:28 +000049 using WeakCallbackDataType = CFXJS_PerObjectData;
50 using MapType = v8::
51 GlobalValueMap<WeakCallbackDataType*, v8::Object, V8TemplateMapTraits>;
Tom Sepeza0395e52018-02-09 19:28:39 +000052
53 static const v8::PersistentContainerCallbackType kCallbackType =
54 v8::kWeakWithInternalFields;
Tom Sepezb7c7df62018-02-09 19:08:59 +000055
Tom Sepezdb8ef922018-02-13 18:23:28 +000056 static WeakCallbackDataType* WeakCallbackParameter(
57 MapType* map,
58 WeakCallbackDataType* key,
59 v8::Local<v8::Object> value) {
Tom Sepezb7c7df62018-02-09 19:08:59 +000060 return key;
61 }
62 static MapType* MapFromWeakCallbackInfo(
63 const v8::WeakCallbackInfo<WeakCallbackDataType>&);
Tom Sepezdb8ef922018-02-13 18:23:28 +000064 static WeakCallbackDataType* KeyFromWeakCallbackInfo(
Tom Sepezb7c7df62018-02-09 19:08:59 +000065 const v8::WeakCallbackInfo<WeakCallbackDataType>& data) {
66 return data.GetParameter();
67 }
Tom Sepezb7c7df62018-02-09 19:08:59 +000068 static void OnWeakCallback(
69 const v8::WeakCallbackInfo<WeakCallbackDataType>& data) {}
Tom Sepeza0395e52018-02-09 19:28:39 +000070 static void DisposeWeak(
Tom Sepezdb8ef922018-02-13 18:23:28 +000071 const v8::WeakCallbackInfo<WeakCallbackDataType>& data);
Tom Sepezb7c7df62018-02-09 19:08:59 +000072 static void Dispose(v8::Isolate* isolate,
73 v8::Global<v8::Object> value,
Tom Sepezdb8ef922018-02-13 18:23:28 +000074 WeakCallbackDataType* key);
Tom Sepezb7c7df62018-02-09 19:08:59 +000075 static void DisposeCallbackData(WeakCallbackDataType* callbackData) {}
76};
77
78class V8TemplateMap {
79 public:
Tom Sepezdb8ef922018-02-13 18:23:28 +000080 using WeakCallbackDataType = CFXJS_PerObjectData;
81 using MapType = v8::
82 GlobalValueMap<WeakCallbackDataType*, v8::Object, V8TemplateMapTraits>;
Tom Sepezb7c7df62018-02-09 19:08:59 +000083
Tom Sepeza0395e52018-02-09 19:28:39 +000084 explicit V8TemplateMap(v8::Isolate* isolate) : m_map(isolate) {}
85 ~V8TemplateMap() = default;
Tom Sepezb7c7df62018-02-09 19:08:59 +000086
Tom Sepezdb8ef922018-02-13 18:23:28 +000087 void SetAndMakeWeak(WeakCallbackDataType* key, v8::Local<v8::Object> handle) {
Tom Sepeza0395e52018-02-09 19:28:39 +000088 ASSERT(!m_map.Contains(key));
89
90 // Inserting an object into a GlobalValueMap with the appropriate traits
91 // has the side-effect of making the object weak deep in the guts of V8,
92 // and arranges for it to be cleaned up by the methods in the traits.
93 m_map.Set(key, handle);
94 }
Tom Sepezb7c7df62018-02-09 19:08:59 +000095
96 friend class V8TemplateMapTraits;
97
98 private:
99 MapType m_map;
100};
101
Tom Sepez6cf117c2015-11-06 14:52:10 -0800102class CFXJS_PerObjectData {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700103 public:
Tom Sepez57e09772018-02-05 18:52:09 +0000104 explicit CFXJS_PerObjectData(int nObjDefID) : m_ObjDefID(nObjDefID) {}
105
106 ~CFXJS_PerObjectData() = default;
Tom Sepezed7b2b52015-09-22 08:36:17 -0700107
Tom Sepez3f72fb42017-02-27 11:43:55 -0800108 static void SetInObject(CFXJS_PerObjectData* pData,
109 v8::Local<v8::Object> pObj) {
110 if (pObj->InternalFieldCount() == 2) {
Tom Sepez3f72fb42017-02-27 11:43:55 -0800111 pObj->SetAlignedPointerInInternalField(
Lei Zhangd3b028b2018-11-30 22:22:00 +0000112 0, GetAlignedPointerForPerObjectDataTag());
Tom Sepez48627052018-04-05 22:41:53 +0000113 pObj->SetAlignedPointerInInternalField(1, pData);
Tom Sepez3f72fb42017-02-27 11:43:55 -0800114 }
115 }
116
117 static CFXJS_PerObjectData* GetFromObject(v8::Local<v8::Object> pObj) {
118 if (pObj.IsEmpty() || pObj->InternalFieldCount() != 2 ||
Tom Sepez48627052018-04-05 22:41:53 +0000119 pObj->GetAlignedPointerFromInternalField(0) !=
Lei Zhangd3b028b2018-11-30 22:22:00 +0000120 GetAlignedPointerForPerObjectDataTag()) {
Tom Sepez3f72fb42017-02-27 11:43:55 -0800121 return nullptr;
122 }
123 return static_cast<CFXJS_PerObjectData*>(
Tom Sepez48627052018-04-05 22:41:53 +0000124 pObj->GetAlignedPointerFromInternalField(1));
Tom Sepez3f72fb42017-02-27 11:43:55 -0800125 }
126
Lei Zhangd88a3642015-11-10 09:38:57 -0800127 const int m_ObjDefID;
Tom Sepez57e09772018-02-05 18:52:09 +0000128 std::unique_ptr<CJS_Object> m_pPrivate;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700129};
130
Tom Sepezed7b2b52015-09-22 08:36:17 -0700131class CFXJS_ObjDefinition {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700132 public:
Tom Sepezed7b2b52015-09-22 08:36:17 -0700133 CFXJS_ObjDefinition(v8::Isolate* isolate,
Tom Sepez892d7512017-02-21 13:57:13 -0800134 const char* sObjName,
Tom Sepezed7b2b52015-09-22 08:36:17 -0700135 FXJSOBJTYPE eObjType,
tsepezb4694242016-08-15 16:44:55 -0700136 CFXJS_Engine::Constructor pConstructor,
137 CFXJS_Engine::Destructor pDestructor)
Tom Sepezcd56a7d2015-10-06 11:45:28 -0700138 : m_ObjName(sObjName),
139 m_ObjType(eObjType),
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700140 m_pConstructor(pConstructor),
141 m_pDestructor(pDestructor),
Tom Sepez016a3472015-09-30 15:50:57 -0700142 m_pIsolate(isolate) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700143 v8::Isolate::Scope isolate_scope(isolate);
144 v8::HandleScope handle_scope(isolate);
Tom Sepez016a3472015-09-30 15:50:57 -0700145 v8::Local<v8::FunctionTemplate> fun = v8::FunctionTemplate::New(isolate);
146 fun->InstanceTemplate()->SetInternalFieldCount(2);
Tom Sepezd8680512018-10-24 00:15:53 +0000147 fun->SetCallHandler(CallHandler, v8::Number::New(isolate, eObjType));
jochen7e6a8482016-07-06 11:02:27 -0700148 if (eObjType == FXJSOBJTYPE_GLOBAL) {
149 fun->InstanceTemplate()->Set(
150 v8::Symbol::GetToStringTag(isolate),
151 v8::String::NewFromUtf8(isolate, "global", v8::NewStringType::kNormal)
152 .ToLocalChecked());
153 }
Tom Sepez016a3472015-09-30 15:50:57 -0700154 m_FunctionTemplate.Reset(isolate, fun);
Tom Sepezd8680512018-10-24 00:15:53 +0000155 m_Signature.Reset(isolate, v8::Signature::New(isolate, fun));
156 }
Tom Sepez016a3472015-09-30 15:50:57 -0700157
Tom Sepezd8680512018-10-24 00:15:53 +0000158 static void CallHandler(const v8::FunctionCallbackInfo<v8::Value>& info) {
159 v8::Isolate* isolate = info.GetIsolate();
160 if (!info.IsConstructCall()) {
161 isolate->ThrowException(
162 v8::String::NewFromUtf8(isolate, "illegal constructor",
163 v8::NewStringType::kNormal)
164 .ToLocalChecked());
165 return;
166 }
167 if (info.Data().As<v8::Int32>()->Value() != FXJSOBJTYPE_DYNAMIC) {
168 isolate->ThrowException(
169 v8::String::NewFromUtf8(isolate, "not a dynamic object",
170 v8::NewStringType::kNormal)
171 .ToLocalChecked());
172 return;
173 }
174 v8::Local<v8::Object> holder = info.Holder();
175 ASSERT(holder->InternalFieldCount() == 2);
176 holder->SetAlignedPointerInInternalField(0, nullptr);
177 holder->SetAlignedPointerInInternalField(1, nullptr);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700178 }
Tom Sepez016a3472015-09-30 15:50:57 -0700179
Tom Sepez98b356a2018-07-16 21:35:06 +0000180 v8::Isolate* GetIsolate() const { return m_pIsolate.Get(); }
181
Tom Sepez5e873f52018-06-11 18:08:07 +0000182 void DefineConst(const char* sConstName, v8::Local<v8::Value> pDefault) {
Tom Sepez98b356a2018-07-16 21:35:06 +0000183 GetInstanceTemplate()->Set(GetIsolate(), sConstName, pDefault);
Tom Sepez5e873f52018-06-11 18:08:07 +0000184 }
185
186 void DefineProperty(v8::Local<v8::String> sPropName,
187 v8::AccessorGetterCallback pPropGet,
188 v8::AccessorSetterCallback pPropPut) {
189 GetInstanceTemplate()->SetAccessor(sPropName, pPropGet, pPropPut);
190 }
191
192 void DefineMethod(v8::Local<v8::String> sMethodName,
193 v8::FunctionCallback pMethodCall) {
194 v8::Local<v8::FunctionTemplate> fun = v8::FunctionTemplate::New(
Tom Sepez98b356a2018-07-16 21:35:06 +0000195 GetIsolate(), pMethodCall, v8::Local<v8::Value>(), GetSignature());
Tom Sepez5e873f52018-06-11 18:08:07 +0000196 fun->RemovePrototype();
197 GetInstanceTemplate()->Set(sMethodName, fun, v8::ReadOnly);
198 }
199
200 void DefineAllProperties(v8::GenericNamedPropertyQueryCallback pPropQurey,
201 v8::GenericNamedPropertyGetterCallback pPropGet,
202 v8::GenericNamedPropertySetterCallback pPropPut,
203 v8::GenericNamedPropertyDeleterCallback pPropDel) {
204 GetInstanceTemplate()->SetHandler(v8::NamedPropertyHandlerConfiguration(
205 pPropGet, pPropPut, pPropQurey, pPropDel, nullptr,
206 v8::Local<v8::Value>(),
207 v8::PropertyHandlerFlags::kOnlyInterceptStrings));
208 }
209
Tom Sepez016a3472015-09-30 15:50:57 -0700210 v8::Local<v8::ObjectTemplate> GetInstanceTemplate() {
Tom Sepez98b356a2018-07-16 21:35:06 +0000211 v8::EscapableHandleScope scope(GetIsolate());
Tom Sepez016a3472015-09-30 15:50:57 -0700212 v8::Local<v8::FunctionTemplate> function =
Tom Sepez98b356a2018-07-16 21:35:06 +0000213 m_FunctionTemplate.Get(GetIsolate());
Tom Sepez016a3472015-09-30 15:50:57 -0700214 return scope.Escape(function->InstanceTemplate());
215 }
216
217 v8::Local<v8::Signature> GetSignature() {
Tom Sepez98b356a2018-07-16 21:35:06 +0000218 v8::EscapableHandleScope scope(GetIsolate());
219 return scope.Escape(m_Signature.Get(GetIsolate()));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700220 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700221
Tom Sepez892d7512017-02-21 13:57:13 -0800222 const char* const m_ObjName;
Tom Sepezcd56a7d2015-10-06 11:45:28 -0700223 const FXJSOBJTYPE m_ObjType;
tsepezb4694242016-08-15 16:44:55 -0700224 const CFXJS_Engine::Constructor m_pConstructor;
225 const CFXJS_Engine::Destructor m_pDestructor;
Tom Sepez98b356a2018-07-16 21:35:06 +0000226 UnownedPtr<v8::Isolate> m_pIsolate;
Tom Sepez016a3472015-09-30 15:50:57 -0700227 v8::Global<v8::FunctionTemplate> m_FunctionTemplate;
228 v8::Global<v8::Signature> m_Signature;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700229};
230
Tom Sepez016a3472015-09-30 15:50:57 -0700231static v8::Local<v8::ObjectTemplate> GetGlobalObjectTemplate(
232 v8::Isolate* pIsolate) {
Tom Sepeze3b782f2018-06-11 17:48:17 +0000233 FXJS_PerIsolateData* pIsolateData = FXJS_PerIsolateData::Get(pIsolate);
234 for (int i = 0; i < pIsolateData->MaxObjDefinitionID(); ++i) {
235 CFXJS_ObjDefinition* pObjDef = pIsolateData->ObjDefinitionForID(i);
Tom Sepezcd56a7d2015-10-06 11:45:28 -0700236 if (pObjDef->m_ObjType == FXJSOBJTYPE_GLOBAL)
Tom Sepez016a3472015-09-30 15:50:57 -0700237 return pObjDef->GetInstanceTemplate();
238 }
Tom Sepez8b315b62015-10-02 08:59:57 -0700239 if (!g_DefaultGlobalObjectTemplate) {
jochen7e6a8482016-07-06 11:02:27 -0700240 v8::Local<v8::ObjectTemplate> hGlobalTemplate =
241 v8::ObjectTemplate::New(pIsolate);
242 hGlobalTemplate->Set(
243 v8::Symbol::GetToStringTag(pIsolate),
244 v8::String::NewFromUtf8(pIsolate, "global", v8::NewStringType::kNormal)
245 .ToLocalChecked());
246 g_DefaultGlobalObjectTemplate =
247 new v8::Global<v8::ObjectTemplate>(pIsolate, hGlobalTemplate);
Tom Sepez8b315b62015-10-02 08:59:57 -0700248 }
249 return g_DefaultGlobalObjectTemplate->Get(pIsolate);
Tom Sepez016a3472015-09-30 15:50:57 -0700250}
251
jinming_wang61dc96f2016-01-28 14:39:56 +0800252void V8TemplateMapTraits::Dispose(v8::Isolate* isolate,
253 v8::Global<v8::Object> value,
Tom Sepezdb8ef922018-02-13 18:23:28 +0000254 WeakCallbackDataType* key) {
jinming_wang61dc96f2016-01-28 14:39:56 +0800255 v8::Local<v8::Object> obj = value.Get(isolate);
256 if (obj.IsEmpty())
257 return;
Tom Sepezce84f712017-05-30 17:17:33 -0700258 int id = CFXJS_Engine::GetObjDefnID(obj);
jinming_wang61dc96f2016-01-28 14:39:56 +0800259 if (id == -1)
260 return;
Tom Sepeze3b782f2018-06-11 17:48:17 +0000261 FXJS_PerIsolateData* pIsolateData = FXJS_PerIsolateData::Get(isolate);
262 CFXJS_ObjDefinition* pObjDef = pIsolateData->ObjDefinitionForID(id);
jinming_wang61dc96f2016-01-28 14:39:56 +0800263 if (!pObjDef)
264 return;
Tom Sepezb3f10462018-02-08 20:16:09 +0000265 if (pObjDef->m_pDestructor)
266 pObjDef->m_pDestructor(obj);
tsepezb4694242016-08-15 16:44:55 -0700267 CFXJS_Engine::FreeObjectPrivate(obj);
jinming_wang61dc96f2016-01-28 14:39:56 +0800268}
269
Tom Sepezdb8ef922018-02-13 18:23:28 +0000270void V8TemplateMapTraits::DisposeWeak(
271 const v8::WeakCallbackInfo<WeakCallbackDataType>& data) {
272 // TODO(tsepez): this is expected be called during GC.
273}
274
jinming_wang61dc96f2016-01-28 14:39:56 +0800275V8TemplateMapTraits::MapType* V8TemplateMapTraits::MapFromWeakCallbackInfo(
276 const v8::WeakCallbackInfo<WeakCallbackDataType>& data) {
277 V8TemplateMap* pMap =
Tom Sepezdb8ef922018-02-13 18:23:28 +0000278 FXJS_PerIsolateData::Get(data.GetIsolate())->m_pDynamicObjsMap.get();
jinming_wang61dc96f2016-01-28 14:39:56 +0800279 return pMap ? &pMap->m_map : nullptr;
280}
281
Tom Sepeza72e8e22015-10-07 10:17:53 -0700282void FXJS_Initialize(unsigned int embedderDataSlot, v8::Isolate* pIsolate) {
Lei Zhang4d4a4422015-10-08 12:00:14 -0700283 if (g_isolate) {
284 ASSERT(g_embedderDataSlot == embedderDataSlot);
285 ASSERT(g_isolate == pIsolate);
286 return;
287 }
Tom Sepeza72e8e22015-10-07 10:17:53 -0700288 g_embedderDataSlot = embedderDataSlot;
289 g_isolate = pIsolate;
290}
291
292void FXJS_Release() {
Lei Zhang4d4a4422015-10-08 12:00:14 -0700293 ASSERT(!g_isolate || g_isolate_ref_count == 0);
Lei Zhangb3914932015-10-08 12:08:47 -0700294 delete g_DefaultGlobalObjectTemplate;
Tom Sepeza72e8e22015-10-07 10:17:53 -0700295 g_DefaultGlobalObjectTemplate = nullptr;
296 g_isolate = nullptr;
297
298 delete g_arrayBufferAllocator;
299 g_arrayBufferAllocator = nullptr;
300}
301
302bool FXJS_GetIsolate(v8::Isolate** pResultIsolate) {
303 if (g_isolate) {
304 *pResultIsolate = g_isolate;
305 return false;
306 }
307 // Provide backwards compatibility when no external isolate.
308 if (!g_arrayBufferAllocator)
Tom Sepez3f8ee5e2018-02-09 18:26:09 +0000309 g_arrayBufferAllocator = new CFX_V8ArrayBufferAllocator();
Tom Sepeza72e8e22015-10-07 10:17:53 -0700310 v8::Isolate::CreateParams params;
311 params.array_buffer_allocator = g_arrayBufferAllocator;
312 *pResultIsolate = v8::Isolate::New(params);
313 return true;
314}
315
Lei Zhang3fa115b2015-10-08 12:04:47 -0700316size_t FXJS_GlobalIsolateRefCount() {
317 return g_isolate_ref_count;
318}
319
weili625ad662016-06-15 11:21:33 -0700320FXJS_PerIsolateData::~FXJS_PerIsolateData() {}
321
Tom Sepezed7b2b52015-09-22 08:36:17 -0700322// static
323void FXJS_PerIsolateData::SetUp(v8::Isolate* pIsolate) {
Tom Sepez7d0fcbf2015-09-15 15:30:34 -0700324 if (!pIsolate->GetData(g_embedderDataSlot))
weilia4ad5952016-09-22 10:38:53 -0700325 pIsolate->SetData(g_embedderDataSlot, new FXJS_PerIsolateData(pIsolate));
Tom Sepezed7b2b52015-09-22 08:36:17 -0700326}
327
328// static
329FXJS_PerIsolateData* FXJS_PerIsolateData::Get(v8::Isolate* pIsolate) {
330 return static_cast<FXJS_PerIsolateData*>(
331 pIsolate->GetData(g_embedderDataSlot));
Tom Sepez7d0fcbf2015-09-15 15:30:34 -0700332}
333
Lei Zhang35d45872018-10-12 19:07:03 +0000334int FXJS_PerIsolateData::MaxObjDefinitionID() const {
335 return pdfium::CollectionSize<int>(m_ObjectDefnArray);
336}
337
weilia4ad5952016-09-22 10:38:53 -0700338FXJS_PerIsolateData::FXJS_PerIsolateData(v8::Isolate* pIsolate)
339 : m_pDynamicObjsMap(new V8TemplateMap(pIsolate)) {}
weili625ad662016-06-15 11:21:33 -0700340
Tom Sepeze3b782f2018-06-11 17:48:17 +0000341CFXJS_ObjDefinition* FXJS_PerIsolateData::ObjDefinitionForID(int id) const {
342 return (id >= 0 && id < MaxObjDefinitionID()) ? m_ObjectDefnArray[id].get()
343 : nullptr;
344}
345
346int FXJS_PerIsolateData::AssignIDForObjDefinition(
347 std::unique_ptr<CFXJS_ObjDefinition> pDefn) {
348 m_ObjectDefnArray.push_back(std::move(pDefn));
349 return m_ObjectDefnArray.size() - 1;
350}
351
Tom Sepeze0518bf2018-02-08 21:44:59 +0000352CFXJS_Engine::CFXJS_Engine() : CFX_V8(nullptr) {}
tsepeza4941912016-08-15 11:40:12 -0700353
Tom Sepeze0518bf2018-02-08 21:44:59 +0000354CFXJS_Engine::CFXJS_Engine(v8::Isolate* pIsolate) : CFX_V8(pIsolate) {}
weili0b2a9872016-09-21 11:50:43 -0700355
Dan Sinclair4bd2ad82017-12-06 03:12:09 +0000356CFXJS_Engine::~CFXJS_Engine() = default;
Tom Sepez7d0fcbf2015-09-15 15:30:34 -0700357
tsepezb4694242016-08-15 16:44:55 -0700358// static
tsepezb4694242016-08-15 16:44:55 -0700359int CFXJS_Engine::GetObjDefnID(v8::Local<v8::Object> pObj) {
Tom Sepez3f72fb42017-02-27 11:43:55 -0800360 CFXJS_PerObjectData* pData = CFXJS_PerObjectData::GetFromObject(pObj);
361 return pData ? pData->m_ObjDefID : -1;
tsepezb4694242016-08-15 16:44:55 -0700362}
363
364// static
Tom Sepezb3f10462018-02-08 20:16:09 +0000365void CFXJS_Engine::SetObjectPrivate(v8::Local<v8::Object> pObj,
366 std::unique_ptr<CJS_Object> p) {
367 CFXJS_PerObjectData* pPerObjectData =
368 CFXJS_PerObjectData::GetFromObject(pObj);
369 if (!pPerObjectData)
370 return;
371 pPerObjectData->m_pPrivate = std::move(p);
372}
373
374// static
tsepezb4694242016-08-15 16:44:55 -0700375void CFXJS_Engine::FreeObjectPrivate(v8::Local<v8::Object> pObj) {
Tom Sepez3f72fb42017-02-27 11:43:55 -0800376 CFXJS_PerObjectData* pData = CFXJS_PerObjectData::GetFromObject(pObj);
tsepezb4694242016-08-15 16:44:55 -0700377 pObj->SetAlignedPointerInInternalField(0, nullptr);
Tom Sepez3f72fb42017-02-27 11:43:55 -0800378 pObj->SetAlignedPointerInInternalField(1, nullptr);
379 delete pData;
tsepezb4694242016-08-15 16:44:55 -0700380}
381
Tom Sepez892d7512017-02-21 13:57:13 -0800382int CFXJS_Engine::DefineObj(const char* sObjName,
tsepezb4694242016-08-15 16:44:55 -0700383 FXJSOBJTYPE eObjType,
384 CFXJS_Engine::Constructor pConstructor,
385 CFXJS_Engine::Destructor pDestructor) {
Dan Sinclair4bd2ad82017-12-06 03:12:09 +0000386 v8::Isolate::Scope isolate_scope(GetIsolate());
387 v8::HandleScope handle_scope(GetIsolate());
388 FXJS_PerIsolateData::SetUp(GetIsolate());
Tom Sepeze3b782f2018-06-11 17:48:17 +0000389 FXJS_PerIsolateData* pIsolateData = FXJS_PerIsolateData::Get(GetIsolate());
390 return pIsolateData->AssignIDForObjDefinition(
391 pdfium::MakeUnique<CFXJS_ObjDefinition>(GetIsolate(), sObjName, eObjType,
392 pConstructor, pDestructor));
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700393}
394
tsepezb4694242016-08-15 16:44:55 -0700395void CFXJS_Engine::DefineObjMethod(int nObjDefnID,
Tom Sepez9b99b632017-02-21 15:05:57 -0800396 const char* sMethodName,
tsepezb4694242016-08-15 16:44:55 -0700397 v8::FunctionCallback pMethodCall) {
Dan Sinclair4bd2ad82017-12-06 03:12:09 +0000398 v8::Isolate::Scope isolate_scope(GetIsolate());
399 v8::HandleScope handle_scope(GetIsolate());
Tom Sepeze3b782f2018-06-11 17:48:17 +0000400 FXJS_PerIsolateData* pIsolateData = FXJS_PerIsolateData::Get(GetIsolate());
401 CFXJS_ObjDefinition* pObjDef = pIsolateData->ObjDefinitionForID(nObjDefnID);
Tom Sepez5e873f52018-06-11 18:08:07 +0000402 pObjDef->DefineMethod(NewString(sMethodName), pMethodCall);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700403}
404
tsepezb4694242016-08-15 16:44:55 -0700405void CFXJS_Engine::DefineObjProperty(int nObjDefnID,
Tom Sepez4d5b8c52017-02-21 15:17:07 -0800406 const char* sPropName,
tsepezb4694242016-08-15 16:44:55 -0700407 v8::AccessorGetterCallback pPropGet,
408 v8::AccessorSetterCallback pPropPut) {
Dan Sinclair4bd2ad82017-12-06 03:12:09 +0000409 v8::Isolate::Scope isolate_scope(GetIsolate());
410 v8::HandleScope handle_scope(GetIsolate());
Tom Sepeze3b782f2018-06-11 17:48:17 +0000411 FXJS_PerIsolateData* pIsolateData = FXJS_PerIsolateData::Get(GetIsolate());
412 CFXJS_ObjDefinition* pObjDef = pIsolateData->ObjDefinitionForID(nObjDefnID);
Tom Sepez5e873f52018-06-11 18:08:07 +0000413 pObjDef->DefineProperty(NewString(sPropName), pPropGet, pPropPut);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700414}
415
tsepezb4694242016-08-15 16:44:55 -0700416void CFXJS_Engine::DefineObjAllProperties(
417 int nObjDefnID,
Franziska Hinkelmann9a635e82018-03-21 12:58:25 +0000418 v8::GenericNamedPropertyQueryCallback pPropQurey,
419 v8::GenericNamedPropertyGetterCallback pPropGet,
420 v8::GenericNamedPropertySetterCallback pPropPut,
421 v8::GenericNamedPropertyDeleterCallback pPropDel) {
Dan Sinclair4bd2ad82017-12-06 03:12:09 +0000422 v8::Isolate::Scope isolate_scope(GetIsolate());
423 v8::HandleScope handle_scope(GetIsolate());
Tom Sepeze3b782f2018-06-11 17:48:17 +0000424 FXJS_PerIsolateData* pIsolateData = FXJS_PerIsolateData::Get(GetIsolate());
425 CFXJS_ObjDefinition* pObjDef = pIsolateData->ObjDefinitionForID(nObjDefnID);
Tom Sepez5e873f52018-06-11 18:08:07 +0000426 pObjDef->DefineAllProperties(pPropQurey, pPropGet, pPropPut, pPropDel);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700427}
428
tsepezb4694242016-08-15 16:44:55 -0700429void CFXJS_Engine::DefineObjConst(int nObjDefnID,
Tom Sepez55db0912017-02-21 15:26:52 -0800430 const char* sConstName,
tsepezb4694242016-08-15 16:44:55 -0700431 v8::Local<v8::Value> pDefault) {
Dan Sinclair4bd2ad82017-12-06 03:12:09 +0000432 v8::Isolate::Scope isolate_scope(GetIsolate());
433 v8::HandleScope handle_scope(GetIsolate());
Tom Sepeze3b782f2018-06-11 17:48:17 +0000434 FXJS_PerIsolateData* pIsolateData = FXJS_PerIsolateData::Get(GetIsolate());
435 CFXJS_ObjDefinition* pObjDef = pIsolateData->ObjDefinitionForID(nObjDefnID);
Tom Sepez5e873f52018-06-11 18:08:07 +0000436 pObjDef->DefineConst(sConstName, pDefault);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700437}
438
Tom Sepez9b99b632017-02-21 15:05:57 -0800439void CFXJS_Engine::DefineGlobalMethod(const char* sMethodName,
tsepezb4694242016-08-15 16:44:55 -0700440 v8::FunctionCallback pMethodCall) {
Dan Sinclair4bd2ad82017-12-06 03:12:09 +0000441 v8::Isolate::Scope isolate_scope(GetIsolate());
442 v8::HandleScope handle_scope(GetIsolate());
jochenc4dedf32016-07-06 05:26:23 -0700443 v8::Local<v8::FunctionTemplate> fun =
Dan Sinclair4bd2ad82017-12-06 03:12:09 +0000444 v8::FunctionTemplate::New(GetIsolate(), pMethodCall);
jochenc4dedf32016-07-06 05:26:23 -0700445 fun->RemovePrototype();
Dan Sinclair4bd2ad82017-12-06 03:12:09 +0000446 GetGlobalObjectTemplate(GetIsolate())
447 ->Set(NewString(sMethodName), fun, v8::ReadOnly);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700448}
449
tsepezb4694242016-08-15 16:44:55 -0700450void CFXJS_Engine::DefineGlobalConst(const wchar_t* sConstName,
451 v8::FunctionCallback pConstGetter) {
Dan Sinclair4bd2ad82017-12-06 03:12:09 +0000452 v8::Isolate::Scope isolate_scope(GetIsolate());
453 v8::HandleScope handle_scope(GetIsolate());
jochenc4dedf32016-07-06 05:26:23 -0700454 v8::Local<v8::FunctionTemplate> fun =
Dan Sinclair4bd2ad82017-12-06 03:12:09 +0000455 v8::FunctionTemplate::New(GetIsolate(), pConstGetter);
jochenc4dedf32016-07-06 05:26:23 -0700456 fun->RemovePrototype();
Dan Sinclair4bd2ad82017-12-06 03:12:09 +0000457 GetGlobalObjectTemplate(GetIsolate())
458 ->SetAccessorProperty(NewString(sConstName), fun);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700459}
460
tsepezb4694242016-08-15 16:44:55 -0700461void CFXJS_Engine::InitializeEngine() {
Dan Sinclair4bd2ad82017-12-06 03:12:09 +0000462 if (GetIsolate() == g_isolate)
Lei Zhang4d4a4422015-10-08 12:00:14 -0700463 ++g_isolate_ref_count;
464
Dan Sinclair4bd2ad82017-12-06 03:12:09 +0000465 v8::Isolate::Scope isolate_scope(GetIsolate());
466 v8::HandleScope handle_scope(GetIsolate());
dsinclair7d554c92016-06-20 06:06:31 -0700467
468 // This has to happen before we call GetGlobalObjectTemplate because that
Dan Sinclair4bd2ad82017-12-06 03:12:09 +0000469 // method gets the PerIsolateData from GetIsolate().
470 FXJS_PerIsolateData::SetUp(GetIsolate());
dsinclair7d554c92016-06-20 06:06:31 -0700471
Dan Sinclair4bd2ad82017-12-06 03:12:09 +0000472 v8::Local<v8::Context> v8Context = v8::Context::New(
473 GetIsolate(), nullptr, GetGlobalObjectTemplate(GetIsolate()));
Tom Sepez2aa01f52018-04-06 17:40:45 +0000474
475 // May not have the internal fields when called from tests.
476 v8::Local<v8::Object> pThisProxy = v8Context->Global();
477 if (pThisProxy->InternalFieldCount() == 2) {
478 pThisProxy->SetAlignedPointerInInternalField(0, nullptr);
479 pThisProxy->SetAlignedPointerInInternalField(1, nullptr);
480 }
481 v8::Local<v8::Object> pThis = pThisProxy->GetPrototype().As<v8::Object>();
482 if (pThis->InternalFieldCount() == 2) {
483 pThis->SetAlignedPointerInInternalField(0, nullptr);
484 pThis->SetAlignedPointerInInternalField(1, nullptr);
485 }
486
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700487 v8::Context::Scope context_scope(v8Context);
Tom Sepeze3b782f2018-06-11 17:48:17 +0000488 FXJS_PerIsolateData* pIsolateData = FXJS_PerIsolateData::Get(GetIsolate());
489 int maxID = pIsolateData->MaxObjDefinitionID();
tsepezb4694242016-08-15 16:44:55 -0700490 m_StaticObjects.resize(maxID + 1);
Tom Sepezed7b2b52015-09-22 08:36:17 -0700491 for (int i = 0; i < maxID; ++i) {
Tom Sepeze3b782f2018-06-11 17:48:17 +0000492 CFXJS_ObjDefinition* pObjDef = pIsolateData->ObjDefinitionForID(i);
Tom Sepezcd56a7d2015-10-06 11:45:28 -0700493 if (pObjDef->m_ObjType == FXJSOBJTYPE_GLOBAL) {
Tom Sepez3f72fb42017-02-27 11:43:55 -0800494 CFXJS_PerObjectData::SetInObject(new CFXJS_PerObjectData(i),
495 v8Context->Global()
496 ->GetPrototype()
497 ->ToObject(v8Context)
498 .ToLocalChecked());
499 if (pObjDef->m_pConstructor) {
tsepezb4694242016-08-15 16:44:55 -0700500 pObjDef->m_pConstructor(this, v8Context->Global()
501 ->GetPrototype()
502 ->ToObject(v8Context)
503 .ToLocalChecked());
Tom Sepez3f72fb42017-02-27 11:43:55 -0800504 }
Tom Sepezcd56a7d2015-10-06 11:45:28 -0700505 } else if (pObjDef->m_ObjType == FXJSOBJTYPE_STATIC) {
Tom Sepezf6ca07b2017-06-01 09:17:18 -0700506 v8::Local<v8::String> pObjName = NewString(pObjDef->m_ObjName);
Tom Sepez55ccb522018-08-14 23:40:10 +0000507 v8::Local<v8::Object> obj = NewFXJSBoundObject(i, FXJSOBJTYPE_STATIC);
Tom Sepez63b2fc72017-08-14 16:24:29 -0700508 if (!obj.IsEmpty()) {
509 v8Context->Global()->Set(v8Context, pObjName, obj).FromJust();
Tom Sepezf3f18692018-02-06 20:44:05 +0000510 m_StaticObjects[i] = v8::Global<v8::Object>(GetIsolate(), obj);
Tom Sepez63b2fc72017-08-14 16:24:29 -0700511 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700512 }
513 }
Tom Sepez1258f7f2018-02-02 17:37:37 +0000514 m_V8Context.Reset(GetIsolate(), v8Context);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700515}
516
tsepezb4694242016-08-15 16:44:55 -0700517void CFXJS_Engine::ReleaseEngine() {
Dan Sinclair4bd2ad82017-12-06 03:12:09 +0000518 v8::Isolate::Scope isolate_scope(GetIsolate());
519 v8::HandleScope handle_scope(GetIsolate());
Tom Sepez1258f7f2018-02-02 17:37:37 +0000520 v8::Local<v8::Context> context = GetV8Context();
Tom Sepez808a99e2015-09-10 12:28:37 -0700521 v8::Context::Scope context_scope(context);
Tom Sepeze3b782f2018-06-11 17:48:17 +0000522 FXJS_PerIsolateData* pIsolateData = FXJS_PerIsolateData::Get(GetIsolate());
523 if (!pIsolateData)
Tom Sepez129a60e2016-03-24 09:19:20 -0700524 return;
Tom Sepez129a60e2016-03-24 09:19:20 -0700525
Tom Sepezb5b79722018-02-05 15:13:09 +0000526 m_ConstArrays.clear();
tsepezb4694242016-08-15 16:44:55 -0700527
Tom Sepeze3b782f2018-06-11 17:48:17 +0000528 for (int i = 0; i < pIsolateData->MaxObjDefinitionID(); ++i) {
529 CFXJS_ObjDefinition* pObjDef = pIsolateData->ObjDefinitionForID(i);
Oliver Chang8f16b692015-10-27 09:34:49 -0700530 v8::Local<v8::Object> pObj;
531 if (pObjDef->m_ObjType == FXJSOBJTYPE_GLOBAL) {
532 pObj =
533 context->Global()->GetPrototype()->ToObject(context).ToLocalChecked();
Tom Sepezf3f18692018-02-06 20:44:05 +0000534 } else if (!m_StaticObjects[i].IsEmpty()) {
535 pObj = v8::Local<v8::Object>::New(GetIsolate(), m_StaticObjects[i]);
536 m_StaticObjects[i].Reset();
Oliver Chang8f16b692015-10-27 09:34:49 -0700537 }
Oliver Chang8f16b692015-10-27 09:34:49 -0700538 if (!pObj.IsEmpty()) {
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700539 if (pObjDef->m_pDestructor)
Tom Sepezb3f10462018-02-08 20:16:09 +0000540 pObjDef->m_pDestructor(pObj);
tsepezb4694242016-08-15 16:44:55 -0700541 FreeObjectPrivate(pObj);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700542 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700543 }
Tom Sepez808a99e2015-09-10 12:28:37 -0700544
Tom Sepez1258f7f2018-02-02 17:37:37 +0000545 m_V8Context.Reset();
tsepezb4694242016-08-15 16:44:55 -0700546
Dan Sinclair4bd2ad82017-12-06 03:12:09 +0000547 if (GetIsolate() == g_isolate && --g_isolate_ref_count > 0)
Tom Sepez4237aed2015-11-10 15:19:17 -0800548 return;
549
Tom Sepeze3b782f2018-06-11 17:48:17 +0000550 delete pIsolateData;
Dan Sinclair4bd2ad82017-12-06 03:12:09 +0000551 GetIsolate()->SetData(g_embedderDataSlot, nullptr);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700552}
553
Dan Sinclairdc5d88b2018-05-17 13:53:52 +0000554Optional<IJS_Runtime::JS_Error> CFXJS_Engine::Execute(
555 const WideString& script) {
Dan Sinclair4bd2ad82017-12-06 03:12:09 +0000556 v8::Isolate::Scope isolate_scope(GetIsolate());
557 v8::TryCatch try_catch(GetIsolate());
558 v8::Local<v8::Context> context = GetIsolate()->GetCurrentContext();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700559 v8::Local<v8::Script> compiled_script;
Ryan Harrison275e2602017-09-18 14:23:18 -0400560 if (!v8::Script::Compile(context, NewString(script.AsStringView()))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700561 .ToLocal(&compiled_script)) {
Adam Klein4c451ba2018-01-17 21:06:27 +0000562 v8::String::Utf8Value error(GetIsolate(), try_catch.Exception());
Dan Sinclairdc5d88b2018-05-17 13:53:52 +0000563 v8::Local<v8::Message> msg = try_catch.Message();
Lei Zhang696f5d32018-12-08 00:02:50 +0000564 int line = -1;
565 int column = -1;
566 std::tie(line, column) = GetLineAndColumnFromError(msg, context);
567 return IJS_Runtime::JS_Error(line, column, WideString::FromUTF8(*error));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700568 }
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700569
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700570 v8::Local<v8::Value> result;
571 if (!compiled_script->Run(context).ToLocal(&result)) {
Adam Klein4c451ba2018-01-17 21:06:27 +0000572 v8::String::Utf8Value error(GetIsolate(), try_catch.Exception());
Dan Sinclairdc5d88b2018-05-17 13:53:52 +0000573 auto msg = try_catch.Message();
Lei Zhang696f5d32018-12-08 00:02:50 +0000574 int line = -1;
575 int column = -1;
576 std::tie(line, column) = GetLineAndColumnFromError(msg, context);
577 return IJS_Runtime::JS_Error(line, column, WideString::FromUTF8(*error));
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700578 }
Dan Sinclairdc5d88b2018-05-17 13:53:52 +0000579 return pdfium::nullopt;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700580}
581
Tom Sepez9ad9a5f2018-02-07 21:07:24 +0000582v8::Local<v8::Object> CFXJS_Engine::NewFXJSBoundObject(int nObjDefnID,
Tom Sepez55ccb522018-08-14 23:40:10 +0000583 FXJSOBJTYPE type) {
Dan Sinclair4bd2ad82017-12-06 03:12:09 +0000584 v8::Isolate::Scope isolate_scope(GetIsolate());
585 v8::Local<v8::Context> context = GetIsolate()->GetCurrentContext();
Dan Sinclair4bd2ad82017-12-06 03:12:09 +0000586 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(GetIsolate());
Tom Sepezed7b2b52015-09-22 08:36:17 -0700587 if (!pData)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700588 return v8::Local<v8::Object>();
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700589
Tom Sepeze3b782f2018-06-11 17:48:17 +0000590 CFXJS_ObjDefinition* pObjDef = pData->ObjDefinitionForID(nObjDefnID);
591 if (!pObjDef)
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700592 return v8::Local<v8::Object>();
Tom Sepezed7b2b52015-09-22 08:36:17 -0700593
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700594 v8::Local<v8::Object> obj;
Tom Sepez016a3472015-09-30 15:50:57 -0700595 if (!pObjDef->GetInstanceTemplate()->NewInstance(context).ToLocal(&obj))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700596 return v8::Local<v8::Object>();
Tom Sepez39bfe122015-09-17 15:25:23 -0700597
Tom Sepez3f72fb42017-02-27 11:43:55 -0800598 CFXJS_PerObjectData* pObjData = new CFXJS_PerObjectData(nObjDefnID);
599 CFXJS_PerObjectData::SetInObject(pObjData, obj);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700600 if (pObjDef->m_pConstructor)
tsepezb4694242016-08-15 16:44:55 -0700601 pObjDef->m_pConstructor(this, obj);
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700602
Tom Sepez55ccb522018-08-14 23:40:10 +0000603 if (type == FXJSOBJTYPE_DYNAMIC) {
Tom Sepeza0395e52018-02-09 19:28:39 +0000604 auto* pIsolateData = FXJS_PerIsolateData::Get(GetIsolate());
605 if (pIsolateData->m_pDynamicObjsMap)
606 pIsolateData->m_pDynamicObjsMap->SetAndMakeWeak(pObjData, obj);
607 }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700608 return obj;
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700609}
610
tsepezb4694242016-08-15 16:44:55 -0700611v8::Local<v8::Object> CFXJS_Engine::GetThisObj() {
Dan Sinclair4bd2ad82017-12-06 03:12:09 +0000612 v8::Isolate::Scope isolate_scope(GetIsolate());
613 if (!FXJS_PerIsolateData::Get(GetIsolate()))
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700614 return v8::Local<v8::Object>();
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700615
Tom Sepezed7b2b52015-09-22 08:36:17 -0700616 // Return the global object.
Dan Sinclair4bd2ad82017-12-06 03:12:09 +0000617 v8::Local<v8::Context> context = GetIsolate()->GetCurrentContext();
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700618 return context->Global()->GetPrototype()->ToObject(context).ToLocalChecked();
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700619}
620
Ryan Harrison275e2602017-09-18 14:23:18 -0400621void CFXJS_Engine::Error(const WideString& message) {
Dan Sinclair4bd2ad82017-12-06 03:12:09 +0000622 GetIsolate()->ThrowException(NewString(message.AsStringView()));
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700623}
624
Lei Zhang378ec542018-10-18 19:15:47 +0000625v8::Local<v8::Context> CFXJS_Engine::GetV8Context() {
626 return v8::Local<v8::Context>::New(GetIsolate(), m_V8Context);
627}
628
Tom Sepezddaa40f2018-06-06 18:30:15 +0000629// static
Tom Sepez57e09772018-02-05 18:52:09 +0000630CJS_Object* CFXJS_Engine::GetObjectPrivate(v8::Local<v8::Object> pObj) {
Tom Sepezff402c22018-07-17 00:12:56 +0000631 auto* pData = CFXJS_PerObjectData::GetFromObject(pObj);
632 if (pData)
633 return pData->m_pPrivate.get();
634
635 if (pObj.IsEmpty())
636 return nullptr;
637
638 // It could be a global proxy object, in which case the prototype holds
639 // the actual bound object.
640 v8::Local<v8::Value> val = pObj->GetPrototype();
641 if (!val->IsObject())
642 return nullptr;
643
644 auto* pProtoData = CFXJS_PerObjectData::GetFromObject(val.As<v8::Object>());
645 if (!pProtoData)
646 return nullptr;
647
648 auto* pIsolateData = FXJS_PerIsolateData::Get(v8::Isolate::GetCurrent());
649 if (!pIsolateData)
650 return nullptr;
651
652 CFXJS_ObjDefinition* pObjDef =
653 pIsolateData->ObjDefinitionForID(pProtoData->m_ObjDefID);
654 if (!pObjDef || pObjDef->m_ObjType != FXJSOBJTYPE_GLOBAL)
655 return nullptr;
656
657 return pProtoData->m_pPrivate.get();
John Abd-El-Malek5110c472014-05-17 22:33:34 -0700658}
Tom Sepezb5b79722018-02-05 15:13:09 +0000659
660v8::Local<v8::Array> CFXJS_Engine::GetConstArray(const WideString& name) {
661 return v8::Local<v8::Array>::New(GetIsolate(), m_ConstArrays[name]);
662}
663
664void CFXJS_Engine::SetConstArray(const WideString& name,
665 v8::Local<v8::Array> array) {
666 m_ConstArrays[name] = v8::Global<v8::Array>(GetIsolate(), array);
667}