Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
cfxjs_engine.cpp
Go to the documentation of this file.
1// Copyright 2014 The PDFium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7#include "fxjs/cfxjs_engine.h"
8
9#include <memory>
10#include <utility>
11
12#include "core/fxcrt/stl_util.h"
13#include "core/fxcrt/unowned_ptr.h"
14#include "fxjs/cfx_v8_array_buffer_allocator.h"
15#include "fxjs/cjs_object.h"
16#include "fxjs/fxv8.h"
17#include "fxjs/xfa/cfxjse_runtimedata.h"
18#include "third_party/base/check.h"
19#include "third_party/base/check_op.h"
20#include "v8/include/v8-context.h"
21#include "v8/include/v8-exception.h"
22#include "v8/include/v8-isolate.h"
23#include "v8/include/v8-message.h"
24#include "v8/include/v8-primitive.h"
25#include "v8/include/v8-script.h"
26#include "v8/include/v8-util.h"
27
28namespace {
29
30unsigned int g_embedderDataSlot = 1u;
31v8::Isolate* g_isolate = nullptr;
32size_t g_isolate_ref_count = 0;
33CFX_V8ArrayBufferAllocator* g_arrayBufferAllocator = nullptr;
34v8::Global<v8::ObjectTemplate>* g_DefaultGlobalObjectTemplate = nullptr;
35
36// Only the address matters, values are for humans debugging. ASLR should
37// ensure that these values are unlikely to arise otherwise. Keep these
38// wchar_t to prevent the compiler from doing something clever, like
39// aligning them on a byte boundary to save space, which would make them
40// incompatible for use as V8 aligned pointers.
41const wchar_t kPerObjectDataTag[] = L"CFXJS_PerObjectData";
42const wchar_t kPerIsolateDataTag[] = L"FXJS_PerIsolateData";
43
44void* GetAlignedPointerForPerObjectDataTag() {
45 return const_cast<void*>(static_cast<const void*>(kPerObjectDataTag));
46}
47
48std::pair<int, int> GetLineAndColumnFromError(v8::Local<v8::Message> message,
49 v8::Local<v8::Context> context) {
50 if (message.IsEmpty())
51 return std::make_pair(-1, -1);
52 return std::make_pair(message->GetLineNumber(context).FromMaybe(-1),
53 message->GetStartColumn());
54}
55
56} // namespace
57
59 public:
60 ~CFXJS_PerObjectData() = default;
61
62 static void SetNewDataInObject(uint32_t nObjDefnID,
63 v8::Local<v8::Object> pObj) {
64 if (pObj->InternalFieldCount() == 2) {
65 pObj->SetAlignedPointerInInternalField(
66 0, GetAlignedPointerForPerObjectDataTag());
67 pObj->SetAlignedPointerInInternalField(
68 1, new CFXJS_PerObjectData(nObjDefnID));
69 }
70 }
71
72 static CFXJS_PerObjectData* GetFromObject(v8::Local<v8::Object> pObj) {
73 if (pObj.IsEmpty() || pObj->InternalFieldCount() != 2 ||
74 pObj->GetAlignedPointerFromInternalField(0) !=
75 GetAlignedPointerForPerObjectDataTag()) {
76 return nullptr;
77 }
78 return static_cast<CFXJS_PerObjectData*>(
79 pObj->GetAlignedPointerFromInternalField(1));
80 }
81
82 uint32_t GetObjDefnID() const { return m_ObjDefnID; }
83 CJS_Object* GetPrivate() { return m_pPrivate.get(); }
84 void SetPrivate(std::unique_ptr<CJS_Object> p) { m_pPrivate = std::move(p); }
85
86 private:
87 explicit CFXJS_PerObjectData(uint32_t nObjDefnID) : m_ObjDefnID(nObjDefnID) {}
88
89 const uint32_t m_ObjDefnID;
90 std::unique_ptr<CJS_Object> m_pPrivate;
91};
92
93// Global weak map to save dynamic objects.
94class V8TemplateMapTraits final
95 : public v8::StdMapTraits<CFXJS_PerObjectData*, v8::Object> {
96 public:
97 using WeakCallbackDataType = CFXJS_PerObjectData;
98 using MapType = v8::
99 GlobalValueMap<WeakCallbackDataType*, v8::Object, V8TemplateMapTraits>;
100
102 v8::kWeakWithInternalFields;
103
104 static WeakCallbackDataType* WeakCallbackParameter(
105 MapType* map,
106 WeakCallbackDataType* key,
107 v8::Local<v8::Object> value) {
108 return key;
109 }
111 const v8::WeakCallbackInfo<WeakCallbackDataType>&);
112 static WeakCallbackDataType* KeyFromWeakCallbackInfo(
113 const v8::WeakCallbackInfo<WeakCallbackDataType>& data) {
114 return data.GetParameter();
115 }
116 static void OnWeakCallback(
117 const v8::WeakCallbackInfo<WeakCallbackDataType>& data) {}
118 static void DisposeWeak(
119 const v8::WeakCallbackInfo<WeakCallbackDataType>& data);
120 static void Dispose(v8::Isolate* isolate,
121 v8::Global<v8::Object> value,
122 WeakCallbackDataType* key);
123 static void DisposeCallbackData(WeakCallbackDataType* callbackData) {}
124};
125
127 public:
128 using WeakCallbackDataType = CFXJS_PerObjectData;
129 using MapType = v8::
131
132 explicit V8TemplateMap(v8::Isolate* isolate) : m_map(isolate) {}
133 ~V8TemplateMap() = default;
134
135 void SetAndMakeWeak(v8::Local<v8::Object> handle) {
136 WeakCallbackDataType* key = CFXJS_PerObjectData::GetFromObject(handle);
137 DCHECK(!m_map.Contains(key));
138
139 // Inserting an object into a GlobalValueMap with the appropriate traits
140 // has the side-effect of making the object weak deep in the guts of V8,
141 // and arranges for it to be cleaned up by the methods in the traits.
142 m_map.Set(key, handle);
143 }
144
145 MapType* GetMap() { return &m_map; }
146
147 private:
148 MapType m_map;
149};
150
152 public:
153 CFXJS_ObjDefinition(v8::Isolate* isolate,
154 const char* sObjName,
155 FXJSOBJTYPE eObjType,
156 CFXJS_Engine::Constructor pConstructor,
157 CFXJS_Engine::Destructor pDestructor)
159 m_ObjType(eObjType),
163 v8::Isolate::Scope isolate_scope(isolate);
164 v8::HandleScope handle_scope(isolate);
165 v8::Local<v8::FunctionTemplate> fn = v8::FunctionTemplate::New(isolate);
166 fn->InstanceTemplate()->SetInternalFieldCount(2);
167 fn->InstanceTemplate()->SetImmutableProto();
168 fn->SetCallHandler(CallHandler, v8::Number::New(isolate, eObjType));
169 if (eObjType == FXJSOBJTYPE_GLOBAL) {
170 fn->InstanceTemplate()->Set(v8::Symbol::GetToStringTag(isolate),
171 fxv8::NewStringHelper(isolate, "global"));
172 }
173 m_FunctionTemplate.Reset(isolate, fn);
174 m_Signature.Reset(isolate, v8::Signature::New(isolate, fn));
175 }
176
177 static void CallHandler(const v8::FunctionCallbackInfo<v8::Value>& info) {
178 v8::Isolate* isolate = info.GetIsolate();
179 if (!info.IsConstructCall()) {
180 fxv8::ThrowExceptionHelper(isolate, "illegal constructor");
181 return;
182 }
183 if (info.Data().As<v8::Int32>()->Value() != FXJSOBJTYPE_DYNAMIC) {
184 fxv8::ThrowExceptionHelper(isolate, "not a dynamic object");
185 return;
186 }
187 v8::Local<v8::Object> holder = info.Holder();
188 DCHECK_EQ(holder->InternalFieldCount(), 2);
189 holder->SetAlignedPointerInInternalField(0, nullptr);
190 holder->SetAlignedPointerInInternalField(1, nullptr);
191 }
192
193 FXJSOBJTYPE GetObjType() const { return m_ObjType; }
194 const char* GetObjName() const { return m_ObjName; }
195 v8::Isolate* GetIsolate() const { return m_pIsolate; }
196
197 void DefineConst(const char* sConstName, v8::Local<v8::Value> pDefault) {
198 GetInstanceTemplate()->Set(GetIsolate(), sConstName, pDefault);
199 }
200
201 void DefineProperty(v8::Local<v8::String> sPropName,
202 v8::AccessorGetterCallback pPropGet,
203 v8::AccessorSetterCallback pPropPut) {
204 GetInstanceTemplate()->SetAccessor(sPropName, pPropGet, pPropPut);
205 }
206
207 void DefineMethod(v8::Local<v8::String> sMethodName,
208 v8::FunctionCallback pMethodCall) {
209 v8::Local<v8::FunctionTemplate> fun = v8::FunctionTemplate::New(
210 GetIsolate(), pMethodCall, v8::Local<v8::Value>(), GetSignature());
211 fun->RemovePrototype();
212 GetInstanceTemplate()->Set(sMethodName, fun, v8::ReadOnly);
213 }
214
216 v8::GenericNamedPropertyQueryCallback pPropQurey,
217 v8::GenericNamedPropertyGetterCallback pPropGet,
218 v8::GenericNamedPropertySetterCallback pPropPut,
219 v8::GenericNamedPropertyDeleterCallback pPropDel,
220 v8::GenericNamedPropertyEnumeratorCallback pPropEnum) {
221 GetInstanceTemplate()->SetHandler(v8::NamedPropertyHandlerConfiguration(
222 pPropGet, pPropPut, pPropQurey, pPropDel, pPropEnum,
223 v8::Local<v8::Value>(),
224 v8::PropertyHandlerFlags::kOnlyInterceptStrings));
225 }
226
228 v8::EscapableHandleScope scope(GetIsolate());
229 v8::Local<v8::FunctionTemplate> function =
230 m_FunctionTemplate.Get(GetIsolate());
231 return scope.Escape(function->InstanceTemplate());
232 }
233
235 v8::EscapableHandleScope scope(GetIsolate());
236 return scope.Escape(m_Signature.Get(GetIsolate()));
237 }
238
240 v8::Local<v8::Object> obj,
241 v8::Local<v8::Object> proxy) {
242 if (m_pConstructor)
243 m_pConstructor(pEngine, obj, proxy);
244 }
245
246 void RunDestructor(v8::Local<v8::Object> obj) {
247 if (m_pDestructor)
248 m_pDestructor(obj);
249 }
250
251 private:
252 UnownedPtr<const char> const m_ObjName;
253 const FXJSOBJTYPE m_ObjType;
254 const CFXJS_Engine::Constructor m_pConstructor;
255 const CFXJS_Engine::Destructor m_pDestructor;
256 UnownedPtr<v8::Isolate> m_pIsolate;
257 v8::Global<v8::FunctionTemplate> m_FunctionTemplate;
258 v8::Global<v8::Signature> m_Signature;
259};
260
262 v8::Isolate* pIsolate) {
263 FXJS_PerIsolateData* pIsolateData = FXJS_PerIsolateData::Get(pIsolate);
264 for (uint32_t i = 1; i <= pIsolateData->CurrentMaxObjDefinitionID(); ++i) {
265 CFXJS_ObjDefinition* pObjDef = pIsolateData->ObjDefinitionForID(i);
267 return pObjDef->GetInstanceTemplate();
268 }
269 if (!g_DefaultGlobalObjectTemplate) {
270 v8::Local<v8::ObjectTemplate> hGlobalTemplate =
271 v8::ObjectTemplate::New(pIsolate);
272 hGlobalTemplate->Set(v8::Symbol::GetToStringTag(pIsolate),
273 fxv8::NewStringHelper(pIsolate, "global"));
274 g_DefaultGlobalObjectTemplate =
275 new v8::Global<v8::ObjectTemplate>(pIsolate, hGlobalTemplate);
276 }
277 return g_DefaultGlobalObjectTemplate->Get(pIsolate);
278}
279
280void V8TemplateMapTraits::Dispose(v8::Isolate* isolate,
281 v8::Global<v8::Object> value,
282 WeakCallbackDataType* key) {
283 v8::Local<v8::Object> obj = value.Get(isolate);
284 if (obj.IsEmpty())
285 return;
286 uint32_t id = CFXJS_Engine::GetObjDefnID(obj);
287 if (id == 0)
288 return;
289 FXJS_PerIsolateData* pIsolateData = FXJS_PerIsolateData::Get(isolate);
290 CFXJS_ObjDefinition* pObjDef = pIsolateData->ObjDefinitionForID(id);
291 if (!pObjDef)
292 return;
293 pObjDef->RunDestructor(obj);
294 CFXJS_Engine::FreeObjectPrivate(obj);
295}
296
297void V8TemplateMapTraits::DisposeWeak(
298 const v8::WeakCallbackInfo<WeakCallbackDataType>& data) {
299 // TODO(tsepez): this is expected be called during GC.
300}
301
302V8TemplateMapTraits::MapType* V8TemplateMapTraits::MapFromWeakCallbackInfo(
303 const v8::WeakCallbackInfo<WeakCallbackDataType>& info) {
304 auto* pIsolateData = FXJS_PerIsolateData::Get(info.GetIsolate());
305 V8TemplateMap* pObjsMap = pIsolateData->GetDynamicObjsMap();
306 return pObjsMap ? pObjsMap->GetMap() : nullptr;
307}
308
309void FXJS_Initialize(unsigned int embedderDataSlot, v8::Isolate* pIsolate) {
310 if (g_isolate) {
311 DCHECK_EQ(g_embedderDataSlot, embedderDataSlot);
312 DCHECK_EQ(g_isolate, pIsolate);
313 return;
314 }
315 g_embedderDataSlot = embedderDataSlot;
316 g_isolate = pIsolate;
317}
318
320 DCHECK(!g_isolate || g_isolate_ref_count == 0);
321 delete g_DefaultGlobalObjectTemplate;
322 g_DefaultGlobalObjectTemplate = nullptr;
323 g_isolate = nullptr;
324
325 delete g_arrayBufferAllocator;
326 g_arrayBufferAllocator = nullptr;
327}
328
329bool FXJS_GetIsolate(v8::Isolate** pResultIsolate) {
330 if (g_isolate) {
331 *pResultIsolate = g_isolate;
332 return false;
333 }
334 // Provide backwards compatibility when no external isolate.
335 if (!g_arrayBufferAllocator)
336 g_arrayBufferAllocator = new CFX_V8ArrayBufferAllocator();
337 v8::Isolate::CreateParams params;
338 params.array_buffer_allocator = g_arrayBufferAllocator;
339 *pResultIsolate = v8::Isolate::New(params);
340 return true;
341}
342
344 return g_isolate_ref_count;
345}
346
347// static
348void FXJS_PerIsolateData::SetUp(v8::Isolate* pIsolate) {
349 if (!pIsolate->GetData(g_embedderDataSlot))
350 pIsolate->SetData(g_embedderDataSlot, new FXJS_PerIsolateData(pIsolate));
351}
352
353// static
354FXJS_PerIsolateData* FXJS_PerIsolateData::Get(v8::Isolate* pIsolate) {
355 auto* result =
356 static_cast<FXJS_PerIsolateData*>(pIsolate->GetData(g_embedderDataSlot));
357 CHECK(result->m_Tag == kPerIsolateDataTag);
358 return result;
359}
360
361FXJS_PerIsolateData::FXJS_PerIsolateData(v8::Isolate* pIsolate)
362 : m_Tag(kPerIsolateDataTag),
363 m_pDynamicObjsMap(std::make_unique<V8TemplateMap>(pIsolate)) {}
364
366
368 return fxcrt::CollectionSize<uint32_t>(m_ObjectDefnArray);
369}
370
372 uint32_t id) const {
373 return id > 0 && id <= CurrentMaxObjDefinitionID()
374 ? m_ObjectDefnArray[id - 1].get()
375 : nullptr;
376}
377
379 std::unique_ptr<CFXJS_ObjDefinition> pDefn) {
380 m_ObjectDefnArray.push_back(std::move(pDefn));
382}
383
385
386CFXJS_Engine::CFXJS_Engine(v8::Isolate* pIsolate) : CFX_V8(pIsolate) {}
387
388CFXJS_Engine::~CFXJS_Engine() = default;
389
390// static
391uint32_t CFXJS_Engine::GetObjDefnID(v8::Local<v8::Object> pObj) {
392 CFXJS_PerObjectData* pData = CFXJS_PerObjectData::GetFromObject(pObj);
393 return pData ? pData->GetObjDefnID() : 0;
394}
395
396// static
397void CFXJS_Engine::SetObjectPrivate(v8::Local<v8::Object> pObj,
398 std::unique_ptr<CJS_Object> p) {
399 CFXJS_PerObjectData* pPerObjectData =
400 CFXJS_PerObjectData::GetFromObject(pObj);
401 if (pPerObjectData)
402 pPerObjectData->SetPrivate(std::move(p));
403}
404
405// static
406void CFXJS_Engine::FreeObjectPrivate(v8::Local<v8::Object> pObj) {
407 CFXJS_PerObjectData* pData = CFXJS_PerObjectData::GetFromObject(pObj);
408 pObj->SetAlignedPointerInInternalField(0, nullptr);
409 pObj->SetAlignedPointerInInternalField(1, nullptr);
410 delete pData;
411}
412
413uint32_t CFXJS_Engine::DefineObj(const char* sObjName,
414 FXJSOBJTYPE eObjType,
415 CFXJS_Engine::Constructor pConstructor,
416 CFXJS_Engine::Destructor pDestructor) {
417 v8::Isolate::Scope isolate_scope(GetIsolate());
418 v8::HandleScope handle_scope(GetIsolate());
419 FXJS_PerIsolateData::SetUp(GetIsolate());
420 FXJS_PerIsolateData* pIsolateData = FXJS_PerIsolateData::Get(GetIsolate());
421 return pIsolateData->AssignIDForObjDefinition(
422 std::make_unique<CFXJS_ObjDefinition>(GetIsolate(), sObjName, eObjType,
423 pConstructor, pDestructor));
424}
425
426void CFXJS_Engine::DefineObjMethod(uint32_t nObjDefnID,
427 const char* sMethodName,
428 v8::FunctionCallback pMethodCall) {
429 v8::Isolate::Scope isolate_scope(GetIsolate());
430 v8::HandleScope handle_scope(GetIsolate());
431 FXJS_PerIsolateData* pIsolateData = FXJS_PerIsolateData::Get(GetIsolate());
432 CFXJS_ObjDefinition* pObjDef = pIsolateData->ObjDefinitionForID(nObjDefnID);
433 pObjDef->DefineMethod(NewString(sMethodName), pMethodCall);
434}
435
436void CFXJS_Engine::DefineObjProperty(uint32_t nObjDefnID,
437 const char* sPropName,
438 v8::AccessorGetterCallback pPropGet,
439 v8::AccessorSetterCallback pPropPut) {
440 v8::Isolate::Scope isolate_scope(GetIsolate());
441 v8::HandleScope handle_scope(GetIsolate());
442 FXJS_PerIsolateData* pIsolateData = FXJS_PerIsolateData::Get(GetIsolate());
443 CFXJS_ObjDefinition* pObjDef = pIsolateData->ObjDefinitionForID(nObjDefnID);
444 pObjDef->DefineProperty(NewString(sPropName), pPropGet, pPropPut);
445}
446
448 uint32_t nObjDefnID,
449 v8::GenericNamedPropertyQueryCallback pPropQurey,
450 v8::GenericNamedPropertyGetterCallback pPropGet,
451 v8::GenericNamedPropertySetterCallback pPropPut,
452 v8::GenericNamedPropertyDeleterCallback pPropDel,
453 v8::GenericNamedPropertyEnumeratorCallback pPropEnum) {
454 v8::Isolate::Scope isolate_scope(GetIsolate());
455 v8::HandleScope handle_scope(GetIsolate());
456 FXJS_PerIsolateData* pIsolateData = FXJS_PerIsolateData::Get(GetIsolate());
457 CFXJS_ObjDefinition* pObjDef = pIsolateData->ObjDefinitionForID(nObjDefnID);
458 pObjDef->DefineAllProperties(pPropQurey, pPropGet, pPropPut, pPropDel,
459 pPropEnum);
460}
461
462void CFXJS_Engine::DefineObjConst(uint32_t nObjDefnID,
463 const char* sConstName,
464 v8::Local<v8::Value> pDefault) {
465 v8::Isolate::Scope isolate_scope(GetIsolate());
466 v8::HandleScope handle_scope(GetIsolate());
467 FXJS_PerIsolateData* pIsolateData = FXJS_PerIsolateData::Get(GetIsolate());
468 CFXJS_ObjDefinition* pObjDef = pIsolateData->ObjDefinitionForID(nObjDefnID);
469 pObjDef->DefineConst(sConstName, pDefault);
470}
471
472void CFXJS_Engine::DefineGlobalMethod(const char* sMethodName,
473 v8::FunctionCallback pMethodCall) {
474 v8::Isolate::Scope isolate_scope(GetIsolate());
475 v8::HandleScope handle_scope(GetIsolate());
476 v8::Local<v8::FunctionTemplate> fun =
477 v8::FunctionTemplate::New(GetIsolate(), pMethodCall);
478 fun->RemovePrototype();
479 GetGlobalObjectTemplate(GetIsolate())
480 ->Set(NewString(sMethodName), fun, v8::ReadOnly);
481}
482
483void CFXJS_Engine::DefineGlobalConst(const wchar_t* sConstName,
484 v8::FunctionCallback pConstGetter) {
485 v8::Isolate::Scope isolate_scope(GetIsolate());
486 v8::HandleScope handle_scope(GetIsolate());
487 v8::Local<v8::FunctionTemplate> fun =
488 v8::FunctionTemplate::New(GetIsolate(), pConstGetter);
489 fun->RemovePrototype();
490 GetGlobalObjectTemplate(GetIsolate())
491 ->SetAccessorProperty(NewString(sConstName), fun);
492}
493
495 if (GetIsolate() == g_isolate)
496 ++g_isolate_ref_count;
497
498 v8::Isolate::Scope isolate_scope(GetIsolate());
499 v8::HandleScope handle_scope(GetIsolate());
500
501 // This has to happen before we call GetGlobalObjectTemplate because that
502 // method gets the PerIsolateData from GetIsolate().
503 FXJS_PerIsolateData::SetUp(GetIsolate());
504
505 v8::Local<v8::Context> v8Context = v8::Context::New(
506 GetIsolate(), nullptr, GetGlobalObjectTemplate(GetIsolate()));
507
508 // May not have the internal fields when called from tests.
509 v8::Local<v8::Object> pThisProxy = v8Context->Global();
510 if (pThisProxy->InternalFieldCount() == 2) {
511 pThisProxy->SetAlignedPointerInInternalField(0, nullptr);
512 pThisProxy->SetAlignedPointerInInternalField(1, nullptr);
513 }
514 v8::Local<v8::Object> pThis = pThisProxy->GetPrototype().As<v8::Object>();
515 if (pThis->InternalFieldCount() == 2) {
516 pThis->SetAlignedPointerInInternalField(0, nullptr);
517 pThis->SetAlignedPointerInInternalField(1, nullptr);
518 }
519
520 v8::Context::Scope context_scope(v8Context);
521 FXJS_PerIsolateData* pIsolateData = FXJS_PerIsolateData::Get(GetIsolate());
522 uint32_t maxID = pIsolateData->CurrentMaxObjDefinitionID();
523 m_StaticObjects.resize(maxID + 1);
524 for (uint32_t i = 1; i <= maxID; ++i) {
525 CFXJS_ObjDefinition* pObjDef = pIsolateData->ObjDefinitionForID(i);
526 if (pObjDef->GetObjType() == FXJSOBJTYPE_GLOBAL) {
527 CFXJS_PerObjectData::SetNewDataInObject(i, pThis);
528 pObjDef->RunConstructor(this, pThis, pThisProxy);
529 } else if (pObjDef->GetObjType() == FXJSOBJTYPE_STATIC) {
530 v8::Local<v8::String> pObjName = NewString(pObjDef->GetObjName());
531 v8::Local<v8::Object> obj = NewFXJSBoundObject(i, FXJSOBJTYPE_STATIC);
532 if (!obj.IsEmpty()) {
533 v8Context->Global()->Set(v8Context, pObjName, obj).FromJust();
534 m_StaticObjects[i] = v8::Global<v8::Object>(GetIsolate(), obj);
535 }
536 }
537 }
538 m_V8Context.Reset(GetIsolate(), v8Context);
539}
540
542 v8::Isolate::Scope isolate_scope(GetIsolate());
543 v8::HandleScope handle_scope(GetIsolate());
544 v8::Local<v8::Context> context = GetV8Context();
545 v8::Context::Scope context_scope(context);
546 FXJS_PerIsolateData* pIsolateData = FXJS_PerIsolateData::Get(GetIsolate());
547 if (!pIsolateData)
548 return;
549
550 m_ConstArrays.clear();
551
552 for (uint32_t i = 1; i <= pIsolateData->CurrentMaxObjDefinitionID(); ++i) {
553 CFXJS_ObjDefinition* pObjDef = pIsolateData->ObjDefinitionForID(i);
554 v8::Local<v8::Object> pObj;
555 if (pObjDef->GetObjType() == FXJSOBJTYPE_GLOBAL) {
556 pObj =
557 context->Global()->GetPrototype()->ToObject(context).ToLocalChecked();
558 } else if (!m_StaticObjects[i].IsEmpty()) {
559 pObj = v8::Local<v8::Object>::New(GetIsolate(), m_StaticObjects[i]);
560 m_StaticObjects[i].Reset();
561 }
562 if (!pObj.IsEmpty()) {
563 pObjDef->RunDestructor(pObj);
564 FreeObjectPrivate(pObj);
565 }
566 }
567
568 m_V8Context.Reset();
569
570 if (GetIsolate() == g_isolate && --g_isolate_ref_count > 0)
571 return;
572
573 delete pIsolateData;
574 GetIsolate()->SetData(g_embedderDataSlot, nullptr);
575}
576
578 const WideString& script) {
579 v8::Isolate::Scope isolate_scope(GetIsolate());
580 v8::TryCatch try_catch(GetIsolate());
581 v8::Local<v8::Context> context = GetIsolate()->GetCurrentContext();
582 v8::Local<v8::Script> compiled_script;
583 if (!v8::Script::Compile(context, NewString(script.AsStringView()))
584 .ToLocal(&compiled_script)) {
585 v8::String::Utf8Value error(GetIsolate(), try_catch.Exception());
586 v8::Local<v8::Message> msg = try_catch.Message();
587 int line = -1;
588 int column = -1;
589 std::tie(line, column) = GetLineAndColumnFromError(msg, context);
590 return IJS_Runtime::JS_Error(line, column, WideString::FromUTF8(*error));
591 }
592
593 v8::Local<v8::Value> result;
594 if (!compiled_script->Run(context).ToLocal(&result)) {
595 v8::String::Utf8Value error(GetIsolate(), try_catch.Exception());
596 auto msg = try_catch.Message();
597 int line = -1;
598 int column = -1;
599 std::tie(line, column) = GetLineAndColumnFromError(msg, context);
600 return IJS_Runtime::JS_Error(line, column, WideString::FromUTF8(*error));
601 }
602 return absl::nullopt;
603}
604
606 FXJSOBJTYPE type) {
607 v8::Isolate::Scope isolate_scope(GetIsolate());
608 v8::Local<v8::Context> context = GetIsolate()->GetCurrentContext();
609 FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(GetIsolate());
610 if (!pData)
611 return v8::Local<v8::Object>();
612
613 CFXJS_ObjDefinition* pObjDef = pData->ObjDefinitionForID(nObjDefnID);
614 if (!pObjDef)
615 return v8::Local<v8::Object>();
616
617 v8::Local<v8::Object> obj;
618 if (!pObjDef->GetInstanceTemplate()->NewInstance(context).ToLocal(&obj))
619 return v8::Local<v8::Object>();
620
621 CFXJS_PerObjectData::SetNewDataInObject(nObjDefnID, obj);
622 pObjDef->RunConstructor(this, obj, obj);
623 if (type == FXJSOBJTYPE_DYNAMIC) {
624 auto* pIsolateData = FXJS_PerIsolateData::Get(GetIsolate());
625 V8TemplateMap* pObjsMap = pIsolateData->GetDynamicObjsMap();
626 if (pObjsMap)
627 pObjsMap->SetAndMakeWeak(obj);
628 }
629 return obj;
630}
631
633 v8::Isolate::Scope isolate_scope(GetIsolate());
634 if (!FXJS_PerIsolateData::Get(GetIsolate()))
635 return v8::Local<v8::Object>();
636
637 // Return the global object.
638 v8::Local<v8::Context> context = GetIsolate()->GetCurrentContext();
639 return context->Global()->GetPrototype()->ToObject(context).ToLocalChecked();
640}
641
642void CFXJS_Engine::Error(const WideString& message) {
643 fxv8::ThrowExceptionHelper(GetIsolate(), message.AsStringView());
644}
645
647 return v8::Local<v8::Context>::New(GetIsolate(), m_V8Context);
648}
649
650// static
651CJS_Object* CFXJS_Engine::GetObjectPrivate(v8::Isolate* pIsolate,
652 v8::Local<v8::Object> pObj) {
653 auto* pData = CFXJS_PerObjectData::GetFromObject(pObj);
654 if (pData)
655 return pData->GetPrivate();
656
657 if (pObj.IsEmpty())
658 return nullptr;
659
660 // It could be a global proxy object, in which case the prototype holds
661 // the actual bound object.
662 v8::Local<v8::Value> val = pObj->GetPrototype();
663 if (!val->IsObject())
664 return nullptr;
665
666 auto* pProtoData = CFXJS_PerObjectData::GetFromObject(val.As<v8::Object>());
667 if (!pProtoData)
668 return nullptr;
669
670 auto* pIsolateData = FXJS_PerIsolateData::Get(pIsolate);
671 if (!pIsolateData)
672 return nullptr;
673
674 CFXJS_ObjDefinition* pObjDef =
675 pIsolateData->ObjDefinitionForID(pProtoData->GetObjDefnID());
676 if (!pObjDef || pObjDef->GetObjType() != FXJSOBJTYPE_GLOBAL)
677 return nullptr;
678
679 return pProtoData->GetPrivate();
680}
681
682v8::Local<v8::Array> CFXJS_Engine::GetConstArray(const WideString& name) {
683 return v8::Local<v8::Array>::New(GetIsolate(), m_ConstArrays[name]);
684}
685
686void CFXJS_Engine::SetConstArray(const WideString& name,
687 v8::Local<v8::Array> array) {
688 m_ConstArrays[name] = v8::Global<v8::Array>(GetIsolate(), array);
689}
bool FXJS_GetIsolate(v8::Isolate **pResultIsolate)
size_t FXJS_GlobalIsolateRefCount()
void FXJS_Initialize(unsigned int embedderDataSlot, v8::Isolate *pIsolate)
static v8::Local< v8::ObjectTemplate > GetGlobalObjectTemplate(v8::Isolate *pIsolate)
void FXJS_Release()
FXJSOBJTYPE
@ FXJSOBJTYPE_DYNAMIC
@ FXJSOBJTYPE_STATIC
@ FXJSOBJTYPE_GLOBAL
void DefineObjMethod(uint32_t nObjDefnID, const char *sMethodName, v8::FunctionCallback pMethodCall)
void DefineObjProperty(uint32_t nObjDefnID, const char *sPropName, v8::AccessorGetterCallback pPropGet, v8::AccessorSetterCallback pPropPut)
v8::Local< v8::Context > GetV8Context()
void DefineObjConst(uint32_t nObjDefnID, const char *sConstName, v8::Local< v8::Value > pDefault)
~CFXJS_Engine() override
v8::Local< v8::Object > GetThisObj()
void DefineObjAllProperties(uint32_t nObjDefnID, v8::GenericNamedPropertyQueryCallback pPropQurey, v8::GenericNamedPropertyGetterCallback pPropGet, v8::GenericNamedPropertySetterCallback pPropPut, v8::GenericNamedPropertyDeleterCallback pPropDel, v8::GenericNamedPropertyEnumeratorCallback pPropEnum)
absl::optional< IJS_Runtime::JS_Error > Execute(const WideString &script)
void SetConstArray(const WideString &name, v8::Local< v8::Array > array)
v8::Local< v8::Array > GetConstArray(const WideString &name)
uint32_t DefineObj(const char *sObjName, FXJSOBJTYPE eObjType, Constructor pConstructor, Destructor pDestructor)
CFXJS_Engine(v8::Isolate *pIsolate)
void Error(const WideString &message)
v8::Local< v8::Object > NewFXJSBoundObject(uint32_t nObjDefnID, FXJSOBJTYPE type)
void InitializeEngine()
void DefineGlobalConst(const wchar_t *sConstName, v8::FunctionCallback pConstGetter)
void DefineGlobalMethod(const char *sMethodName, v8::FunctionCallback pMethodCall)
void RunConstructor(CFXJS_Engine *pEngine, v8::Local< v8::Object > obj, v8::Local< v8::Object > proxy)
const char * GetObjName() const
void DefineAllProperties(v8::GenericNamedPropertyQueryCallback pPropQurey, v8::GenericNamedPropertyGetterCallback pPropGet, v8::GenericNamedPropertySetterCallback pPropPut, v8::GenericNamedPropertyDeleterCallback pPropDel, v8::GenericNamedPropertyEnumeratorCallback pPropEnum)
void DefineConst(const char *sConstName, v8::Local< v8::Value > pDefault)
static void CallHandler(const v8::FunctionCallbackInfo< v8::Value > &info)
v8::Isolate * GetIsolate() const
void DefineMethod(v8::Local< v8::String > sMethodName, v8::FunctionCallback pMethodCall)
void RunDestructor(v8::Local< v8::Object > obj)
CFXJS_ObjDefinition(v8::Isolate *isolate, const char *sObjName, FXJSOBJTYPE eObjType, CFXJS_Engine::Constructor pConstructor, CFXJS_Engine::Destructor pDestructor)
FXJSOBJTYPE GetObjType() const
v8::Local< v8::ObjectTemplate > GetInstanceTemplate()
void DefineProperty(v8::Local< v8::String > sPropName, v8::AccessorGetterCallback pPropGet, v8::AccessorSetterCallback pPropPut)
v8::Local< v8::Signature > GetSignature()
void SetPrivate(std::unique_ptr< CJS_Object > p)
~CFXJS_PerObjectData()=default
static void SetNewDataInObject(uint32_t nObjDefnID, v8::Local< v8::Object > pObj)
CJS_Object * GetPrivate()
static CFXJS_PerObjectData * GetFromObject(v8::Local< v8::Object > pObj)
uint32_t GetObjDefnID() const
uint32_t CurrentMaxObjDefinitionID() const
CFXJS_ObjDefinition * ObjDefinitionForID(uint32_t id) const
uint32_t AssignIDForObjDefinition(std::unique_ptr< CFXJS_ObjDefinition > pDefn)
static void OnWeakCallback(const v8::WeakCallbackInfo< WeakCallbackDataType > &data)
static void DisposeCallbackData(WeakCallbackDataType *callbackData)
static void Dispose(v8::Isolate *isolate, v8::Global< v8::Object > value, WeakCallbackDataType *key)
static WeakCallbackDataType * KeyFromWeakCallbackInfo(const v8::WeakCallbackInfo< WeakCallbackDataType > &data)
static MapType * MapFromWeakCallbackInfo(const v8::WeakCallbackInfo< WeakCallbackDataType > &)
static WeakCallbackDataType * WeakCallbackParameter(MapType *map, WeakCallbackDataType *key, v8::Local< v8::Object > value)
static const v8::PersistentContainerCallbackType kCallbackType
static void DisposeWeak(const v8::WeakCallbackInfo< WeakCallbackDataType > &data)
MapType * GetMap()
~V8TemplateMap()=default
V8TemplateMap(v8::Isolate *isolate)
void SetAndMakeWeak(v8::Local< v8::Object > handle)
Definition fxv8.h:22
void ThrowExceptionHelper(v8::Isolate *pIsolate, WideStringView str)
Definition fxv8.cpp:333
#define CHECK(cvref)