John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1 | // Copyright 2014 PDFium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 4 | |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 5 | // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | |
Tom Sepez | b7c7df6 | 2018-02-09 19:08:59 +0000 | [diff] [blame] | 7 | #include "fxjs/cfxjs_engine.h" |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 8 | |
Tom Sepez | 57e0977 | 2018-02-05 18:52:09 +0000 | [diff] [blame] | 9 | #include <memory> |
| 10 | #include <utility> |
Dan Sinclair | 3ebd121 | 2016-03-09 09:59:23 -0500 | [diff] [blame] | 11 | #include <vector> |
| 12 | |
Tom Sepez | 98b356a | 2018-07-16 21:35:06 +0000 | [diff] [blame] | 13 | #include "core/fxcrt/unowned_ptr.h" |
Tom Sepez | 57e0977 | 2018-02-05 18:52:09 +0000 | [diff] [blame] | 14 | #include "fxjs/cjs_object.h" |
Tom Sepez | 70e5214 | 2018-12-13 20:07:50 +0000 | [diff] [blame] | 15 | #include "fxjs/xfa/cfxjse_runtimedata.h" |
Lei Zhang | 99f5bbb | 2018-10-09 21:31:28 +0000 | [diff] [blame] | 16 | #include "third_party/base/ptr_util.h" |
Lei Zhang | 35d4587 | 2018-10-12 19:07:03 +0000 | [diff] [blame] | 17 | #include "third_party/base/stl_util.h" |
Lei Zhang | 378ec54 | 2018-10-18 19:15:47 +0000 | [diff] [blame] | 18 | #include "v8/include/v8-util.h" |
Lei Zhang | 4d4a442 | 2015-10-08 12:00:14 -0700 | [diff] [blame] | 19 | |
Tom Sepez | db8ef92 | 2018-02-13 18:23:28 +0000 | [diff] [blame] | 20 | class CFXJS_PerObjectData; |
| 21 | |
Lei Zhang | d3b028b | 2018-11-30 22:22:00 +0000 | [diff] [blame] | 22 | namespace { |
| 23 | |
| 24 | unsigned int g_embedderDataSlot = 1u; |
| 25 | v8::Isolate* g_isolate = nullptr; |
| 26 | size_t g_isolate_ref_count = 0; |
| 27 | CFX_V8ArrayBufferAllocator* g_arrayBufferAllocator = nullptr; |
| 28 | v8::Global<v8::ObjectTemplate>* g_DefaultGlobalObjectTemplate = nullptr; |
| 29 | const wchar_t kPerObjectDataTag[] = L"CFXJS_PerObjectData"; |
| 30 | |
| 31 | void* GetAlignedPointerForPerObjectDataTag() { |
| 32 | return const_cast<void*>(static_cast<const void*>(kPerObjectDataTag)); |
| 33 | } |
| 34 | |
Lei Zhang | 696f5d3 | 2018-12-08 00:02:50 +0000 | [diff] [blame] | 35 | std::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 Zhang | d3b028b | 2018-11-30 22:22:00 +0000 | [diff] [blame] | 43 | } // namespace |
Tom Sepez | 016a347 | 2015-09-30 15:50:57 -0700 | [diff] [blame] | 44 | |
Tom Sepez | b7c7df6 | 2018-02-09 19:08:59 +0000 | [diff] [blame] | 45 | // Global weak map to save dynamic objects. |
Tom Sepez | 5586545 | 2018-08-27 20:18:04 +0000 | [diff] [blame] | 46 | class V8TemplateMapTraits final |
Tom Sepez | db8ef92 | 2018-02-13 18:23:28 +0000 | [diff] [blame] | 47 | : public v8::StdMapTraits<CFXJS_PerObjectData*, v8::Object> { |
Tom Sepez | b7c7df6 | 2018-02-09 19:08:59 +0000 | [diff] [blame] | 48 | public: |
Tom Sepez | db8ef92 | 2018-02-13 18:23:28 +0000 | [diff] [blame] | 49 | using WeakCallbackDataType = CFXJS_PerObjectData; |
| 50 | using MapType = v8:: |
| 51 | GlobalValueMap<WeakCallbackDataType*, v8::Object, V8TemplateMapTraits>; |
Tom Sepez | a0395e5 | 2018-02-09 19:28:39 +0000 | [diff] [blame] | 52 | |
| 53 | static const v8::PersistentContainerCallbackType kCallbackType = |
| 54 | v8::kWeakWithInternalFields; |
Tom Sepez | b7c7df6 | 2018-02-09 19:08:59 +0000 | [diff] [blame] | 55 | |
Tom Sepez | db8ef92 | 2018-02-13 18:23:28 +0000 | [diff] [blame] | 56 | static WeakCallbackDataType* WeakCallbackParameter( |
| 57 | MapType* map, |
| 58 | WeakCallbackDataType* key, |
| 59 | v8::Local<v8::Object> value) { |
Tom Sepez | b7c7df6 | 2018-02-09 19:08:59 +0000 | [diff] [blame] | 60 | return key; |
| 61 | } |
| 62 | static MapType* MapFromWeakCallbackInfo( |
| 63 | const v8::WeakCallbackInfo<WeakCallbackDataType>&); |
Tom Sepez | db8ef92 | 2018-02-13 18:23:28 +0000 | [diff] [blame] | 64 | static WeakCallbackDataType* KeyFromWeakCallbackInfo( |
Tom Sepez | b7c7df6 | 2018-02-09 19:08:59 +0000 | [diff] [blame] | 65 | const v8::WeakCallbackInfo<WeakCallbackDataType>& data) { |
| 66 | return data.GetParameter(); |
| 67 | } |
Tom Sepez | b7c7df6 | 2018-02-09 19:08:59 +0000 | [diff] [blame] | 68 | static void OnWeakCallback( |
| 69 | const v8::WeakCallbackInfo<WeakCallbackDataType>& data) {} |
Tom Sepez | a0395e5 | 2018-02-09 19:28:39 +0000 | [diff] [blame] | 70 | static void DisposeWeak( |
Tom Sepez | db8ef92 | 2018-02-13 18:23:28 +0000 | [diff] [blame] | 71 | const v8::WeakCallbackInfo<WeakCallbackDataType>& data); |
Tom Sepez | b7c7df6 | 2018-02-09 19:08:59 +0000 | [diff] [blame] | 72 | static void Dispose(v8::Isolate* isolate, |
| 73 | v8::Global<v8::Object> value, |
Tom Sepez | db8ef92 | 2018-02-13 18:23:28 +0000 | [diff] [blame] | 74 | WeakCallbackDataType* key); |
Tom Sepez | b7c7df6 | 2018-02-09 19:08:59 +0000 | [diff] [blame] | 75 | static void DisposeCallbackData(WeakCallbackDataType* callbackData) {} |
| 76 | }; |
| 77 | |
| 78 | class V8TemplateMap { |
| 79 | public: |
Tom Sepez | db8ef92 | 2018-02-13 18:23:28 +0000 | [diff] [blame] | 80 | using WeakCallbackDataType = CFXJS_PerObjectData; |
| 81 | using MapType = v8:: |
| 82 | GlobalValueMap<WeakCallbackDataType*, v8::Object, V8TemplateMapTraits>; |
Tom Sepez | b7c7df6 | 2018-02-09 19:08:59 +0000 | [diff] [blame] | 83 | |
Tom Sepez | a0395e5 | 2018-02-09 19:28:39 +0000 | [diff] [blame] | 84 | explicit V8TemplateMap(v8::Isolate* isolate) : m_map(isolate) {} |
| 85 | ~V8TemplateMap() = default; |
Tom Sepez | b7c7df6 | 2018-02-09 19:08:59 +0000 | [diff] [blame] | 86 | |
Tom Sepez | db8ef92 | 2018-02-13 18:23:28 +0000 | [diff] [blame] | 87 | void SetAndMakeWeak(WeakCallbackDataType* key, v8::Local<v8::Object> handle) { |
Tom Sepez | a0395e5 | 2018-02-09 19:28:39 +0000 | [diff] [blame] | 88 | 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 Sepez | b7c7df6 | 2018-02-09 19:08:59 +0000 | [diff] [blame] | 95 | |
| 96 | friend class V8TemplateMapTraits; |
| 97 | |
| 98 | private: |
| 99 | MapType m_map; |
| 100 | }; |
| 101 | |
Tom Sepez | 6cf117c | 2015-11-06 14:52:10 -0800 | [diff] [blame] | 102 | class CFXJS_PerObjectData { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 103 | public: |
Tom Sepez | 57e0977 | 2018-02-05 18:52:09 +0000 | [diff] [blame] | 104 | explicit CFXJS_PerObjectData(int nObjDefID) : m_ObjDefID(nObjDefID) {} |
| 105 | |
| 106 | ~CFXJS_PerObjectData() = default; |
Tom Sepez | ed7b2b5 | 2015-09-22 08:36:17 -0700 | [diff] [blame] | 107 | |
Tom Sepez | 3f72fb4 | 2017-02-27 11:43:55 -0800 | [diff] [blame] | 108 | static void SetInObject(CFXJS_PerObjectData* pData, |
| 109 | v8::Local<v8::Object> pObj) { |
| 110 | if (pObj->InternalFieldCount() == 2) { |
Tom Sepez | 3f72fb4 | 2017-02-27 11:43:55 -0800 | [diff] [blame] | 111 | pObj->SetAlignedPointerInInternalField( |
Lei Zhang | d3b028b | 2018-11-30 22:22:00 +0000 | [diff] [blame] | 112 | 0, GetAlignedPointerForPerObjectDataTag()); |
Tom Sepez | 4862705 | 2018-04-05 22:41:53 +0000 | [diff] [blame] | 113 | pObj->SetAlignedPointerInInternalField(1, pData); |
Tom Sepez | 3f72fb4 | 2017-02-27 11:43:55 -0800 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | |
| 117 | static CFXJS_PerObjectData* GetFromObject(v8::Local<v8::Object> pObj) { |
| 118 | if (pObj.IsEmpty() || pObj->InternalFieldCount() != 2 || |
Tom Sepez | 4862705 | 2018-04-05 22:41:53 +0000 | [diff] [blame] | 119 | pObj->GetAlignedPointerFromInternalField(0) != |
Lei Zhang | d3b028b | 2018-11-30 22:22:00 +0000 | [diff] [blame] | 120 | GetAlignedPointerForPerObjectDataTag()) { |
Tom Sepez | 3f72fb4 | 2017-02-27 11:43:55 -0800 | [diff] [blame] | 121 | return nullptr; |
| 122 | } |
| 123 | return static_cast<CFXJS_PerObjectData*>( |
Tom Sepez | 4862705 | 2018-04-05 22:41:53 +0000 | [diff] [blame] | 124 | pObj->GetAlignedPointerFromInternalField(1)); |
Tom Sepez | 3f72fb4 | 2017-02-27 11:43:55 -0800 | [diff] [blame] | 125 | } |
| 126 | |
Lei Zhang | d88a364 | 2015-11-10 09:38:57 -0800 | [diff] [blame] | 127 | const int m_ObjDefID; |
Tom Sepez | 57e0977 | 2018-02-05 18:52:09 +0000 | [diff] [blame] | 128 | std::unique_ptr<CJS_Object> m_pPrivate; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 129 | }; |
| 130 | |
Tom Sepez | ed7b2b5 | 2015-09-22 08:36:17 -0700 | [diff] [blame] | 131 | class CFXJS_ObjDefinition { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 132 | public: |
Tom Sepez | ed7b2b5 | 2015-09-22 08:36:17 -0700 | [diff] [blame] | 133 | CFXJS_ObjDefinition(v8::Isolate* isolate, |
Tom Sepez | 892d751 | 2017-02-21 13:57:13 -0800 | [diff] [blame] | 134 | const char* sObjName, |
Tom Sepez | ed7b2b5 | 2015-09-22 08:36:17 -0700 | [diff] [blame] | 135 | FXJSOBJTYPE eObjType, |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 136 | CFXJS_Engine::Constructor pConstructor, |
| 137 | CFXJS_Engine::Destructor pDestructor) |
Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame] | 138 | : m_ObjName(sObjName), |
| 139 | m_ObjType(eObjType), |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 140 | m_pConstructor(pConstructor), |
| 141 | m_pDestructor(pDestructor), |
Tom Sepez | 016a347 | 2015-09-30 15:50:57 -0700 | [diff] [blame] | 142 | m_pIsolate(isolate) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 143 | v8::Isolate::Scope isolate_scope(isolate); |
| 144 | v8::HandleScope handle_scope(isolate); |
Tom Sepez | 016a347 | 2015-09-30 15:50:57 -0700 | [diff] [blame] | 145 | v8::Local<v8::FunctionTemplate> fun = v8::FunctionTemplate::New(isolate); |
| 146 | fun->InstanceTemplate()->SetInternalFieldCount(2); |
Tom Sepez | d868051 | 2018-10-24 00:15:53 +0000 | [diff] [blame] | 147 | fun->SetCallHandler(CallHandler, v8::Number::New(isolate, eObjType)); |
jochen | 7e6a848 | 2016-07-06 11:02:27 -0700 | [diff] [blame] | 148 | 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 Sepez | 016a347 | 2015-09-30 15:50:57 -0700 | [diff] [blame] | 154 | m_FunctionTemplate.Reset(isolate, fun); |
Tom Sepez | d868051 | 2018-10-24 00:15:53 +0000 | [diff] [blame] | 155 | m_Signature.Reset(isolate, v8::Signature::New(isolate, fun)); |
| 156 | } |
Tom Sepez | 016a347 | 2015-09-30 15:50:57 -0700 | [diff] [blame] | 157 | |
Tom Sepez | d868051 | 2018-10-24 00:15:53 +0000 | [diff] [blame] | 158 | 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 Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 178 | } |
Tom Sepez | 016a347 | 2015-09-30 15:50:57 -0700 | [diff] [blame] | 179 | |
Tom Sepez | 98b356a | 2018-07-16 21:35:06 +0000 | [diff] [blame] | 180 | v8::Isolate* GetIsolate() const { return m_pIsolate.Get(); } |
| 181 | |
Tom Sepez | 5e873f5 | 2018-06-11 18:08:07 +0000 | [diff] [blame] | 182 | void DefineConst(const char* sConstName, v8::Local<v8::Value> pDefault) { |
Tom Sepez | 98b356a | 2018-07-16 21:35:06 +0000 | [diff] [blame] | 183 | GetInstanceTemplate()->Set(GetIsolate(), sConstName, pDefault); |
Tom Sepez | 5e873f5 | 2018-06-11 18:08:07 +0000 | [diff] [blame] | 184 | } |
| 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 Sepez | 98b356a | 2018-07-16 21:35:06 +0000 | [diff] [blame] | 195 | GetIsolate(), pMethodCall, v8::Local<v8::Value>(), GetSignature()); |
Tom Sepez | 5e873f5 | 2018-06-11 18:08:07 +0000 | [diff] [blame] | 196 | 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 Sepez | 016a347 | 2015-09-30 15:50:57 -0700 | [diff] [blame] | 210 | v8::Local<v8::ObjectTemplate> GetInstanceTemplate() { |
Tom Sepez | 98b356a | 2018-07-16 21:35:06 +0000 | [diff] [blame] | 211 | v8::EscapableHandleScope scope(GetIsolate()); |
Tom Sepez | 016a347 | 2015-09-30 15:50:57 -0700 | [diff] [blame] | 212 | v8::Local<v8::FunctionTemplate> function = |
Tom Sepez | 98b356a | 2018-07-16 21:35:06 +0000 | [diff] [blame] | 213 | m_FunctionTemplate.Get(GetIsolate()); |
Tom Sepez | 016a347 | 2015-09-30 15:50:57 -0700 | [diff] [blame] | 214 | return scope.Escape(function->InstanceTemplate()); |
| 215 | } |
| 216 | |
| 217 | v8::Local<v8::Signature> GetSignature() { |
Tom Sepez | 98b356a | 2018-07-16 21:35:06 +0000 | [diff] [blame] | 218 | v8::EscapableHandleScope scope(GetIsolate()); |
| 219 | return scope.Escape(m_Signature.Get(GetIsolate())); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 220 | } |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 221 | |
Tom Sepez | 892d751 | 2017-02-21 13:57:13 -0800 | [diff] [blame] | 222 | const char* const m_ObjName; |
Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame] | 223 | const FXJSOBJTYPE m_ObjType; |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 224 | const CFXJS_Engine::Constructor m_pConstructor; |
| 225 | const CFXJS_Engine::Destructor m_pDestructor; |
Tom Sepez | 98b356a | 2018-07-16 21:35:06 +0000 | [diff] [blame] | 226 | UnownedPtr<v8::Isolate> m_pIsolate; |
Tom Sepez | 016a347 | 2015-09-30 15:50:57 -0700 | [diff] [blame] | 227 | v8::Global<v8::FunctionTemplate> m_FunctionTemplate; |
| 228 | v8::Global<v8::Signature> m_Signature; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 229 | }; |
| 230 | |
Tom Sepez | 016a347 | 2015-09-30 15:50:57 -0700 | [diff] [blame] | 231 | static v8::Local<v8::ObjectTemplate> GetGlobalObjectTemplate( |
| 232 | v8::Isolate* pIsolate) { |
Tom Sepez | e3b782f | 2018-06-11 17:48:17 +0000 | [diff] [blame] | 233 | FXJS_PerIsolateData* pIsolateData = FXJS_PerIsolateData::Get(pIsolate); |
| 234 | for (int i = 0; i < pIsolateData->MaxObjDefinitionID(); ++i) { |
| 235 | CFXJS_ObjDefinition* pObjDef = pIsolateData->ObjDefinitionForID(i); |
Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame] | 236 | if (pObjDef->m_ObjType == FXJSOBJTYPE_GLOBAL) |
Tom Sepez | 016a347 | 2015-09-30 15:50:57 -0700 | [diff] [blame] | 237 | return pObjDef->GetInstanceTemplate(); |
| 238 | } |
Tom Sepez | 8b315b6 | 2015-10-02 08:59:57 -0700 | [diff] [blame] | 239 | if (!g_DefaultGlobalObjectTemplate) { |
jochen | 7e6a848 | 2016-07-06 11:02:27 -0700 | [diff] [blame] | 240 | 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 Sepez | 8b315b6 | 2015-10-02 08:59:57 -0700 | [diff] [blame] | 248 | } |
| 249 | return g_DefaultGlobalObjectTemplate->Get(pIsolate); |
Tom Sepez | 016a347 | 2015-09-30 15:50:57 -0700 | [diff] [blame] | 250 | } |
| 251 | |
jinming_wang | 61dc96f | 2016-01-28 14:39:56 +0800 | [diff] [blame] | 252 | void V8TemplateMapTraits::Dispose(v8::Isolate* isolate, |
| 253 | v8::Global<v8::Object> value, |
Tom Sepez | db8ef92 | 2018-02-13 18:23:28 +0000 | [diff] [blame] | 254 | WeakCallbackDataType* key) { |
jinming_wang | 61dc96f | 2016-01-28 14:39:56 +0800 | [diff] [blame] | 255 | v8::Local<v8::Object> obj = value.Get(isolate); |
| 256 | if (obj.IsEmpty()) |
| 257 | return; |
Tom Sepez | ce84f71 | 2017-05-30 17:17:33 -0700 | [diff] [blame] | 258 | int id = CFXJS_Engine::GetObjDefnID(obj); |
jinming_wang | 61dc96f | 2016-01-28 14:39:56 +0800 | [diff] [blame] | 259 | if (id == -1) |
| 260 | return; |
Tom Sepez | e3b782f | 2018-06-11 17:48:17 +0000 | [diff] [blame] | 261 | FXJS_PerIsolateData* pIsolateData = FXJS_PerIsolateData::Get(isolate); |
| 262 | CFXJS_ObjDefinition* pObjDef = pIsolateData->ObjDefinitionForID(id); |
jinming_wang | 61dc96f | 2016-01-28 14:39:56 +0800 | [diff] [blame] | 263 | if (!pObjDef) |
| 264 | return; |
Tom Sepez | b3f1046 | 2018-02-08 20:16:09 +0000 | [diff] [blame] | 265 | if (pObjDef->m_pDestructor) |
| 266 | pObjDef->m_pDestructor(obj); |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 267 | CFXJS_Engine::FreeObjectPrivate(obj); |
jinming_wang | 61dc96f | 2016-01-28 14:39:56 +0800 | [diff] [blame] | 268 | } |
| 269 | |
Tom Sepez | db8ef92 | 2018-02-13 18:23:28 +0000 | [diff] [blame] | 270 | void V8TemplateMapTraits::DisposeWeak( |
| 271 | const v8::WeakCallbackInfo<WeakCallbackDataType>& data) { |
| 272 | // TODO(tsepez): this is expected be called during GC. |
| 273 | } |
| 274 | |
jinming_wang | 61dc96f | 2016-01-28 14:39:56 +0800 | [diff] [blame] | 275 | V8TemplateMapTraits::MapType* V8TemplateMapTraits::MapFromWeakCallbackInfo( |
| 276 | const v8::WeakCallbackInfo<WeakCallbackDataType>& data) { |
| 277 | V8TemplateMap* pMap = |
Tom Sepez | db8ef92 | 2018-02-13 18:23:28 +0000 | [diff] [blame] | 278 | FXJS_PerIsolateData::Get(data.GetIsolate())->m_pDynamicObjsMap.get(); |
jinming_wang | 61dc96f | 2016-01-28 14:39:56 +0800 | [diff] [blame] | 279 | return pMap ? &pMap->m_map : nullptr; |
| 280 | } |
| 281 | |
Tom Sepez | a72e8e2 | 2015-10-07 10:17:53 -0700 | [diff] [blame] | 282 | void FXJS_Initialize(unsigned int embedderDataSlot, v8::Isolate* pIsolate) { |
Lei Zhang | 4d4a442 | 2015-10-08 12:00:14 -0700 | [diff] [blame] | 283 | if (g_isolate) { |
| 284 | ASSERT(g_embedderDataSlot == embedderDataSlot); |
| 285 | ASSERT(g_isolate == pIsolate); |
| 286 | return; |
| 287 | } |
Tom Sepez | a72e8e2 | 2015-10-07 10:17:53 -0700 | [diff] [blame] | 288 | g_embedderDataSlot = embedderDataSlot; |
| 289 | g_isolate = pIsolate; |
| 290 | } |
| 291 | |
| 292 | void FXJS_Release() { |
Lei Zhang | 4d4a442 | 2015-10-08 12:00:14 -0700 | [diff] [blame] | 293 | ASSERT(!g_isolate || g_isolate_ref_count == 0); |
Lei Zhang | b391493 | 2015-10-08 12:08:47 -0700 | [diff] [blame] | 294 | delete g_DefaultGlobalObjectTemplate; |
Tom Sepez | a72e8e2 | 2015-10-07 10:17:53 -0700 | [diff] [blame] | 295 | g_DefaultGlobalObjectTemplate = nullptr; |
| 296 | g_isolate = nullptr; |
| 297 | |
| 298 | delete g_arrayBufferAllocator; |
| 299 | g_arrayBufferAllocator = nullptr; |
| 300 | } |
| 301 | |
| 302 | bool 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 Sepez | 3f8ee5e | 2018-02-09 18:26:09 +0000 | [diff] [blame] | 309 | g_arrayBufferAllocator = new CFX_V8ArrayBufferAllocator(); |
Tom Sepez | a72e8e2 | 2015-10-07 10:17:53 -0700 | [diff] [blame] | 310 | v8::Isolate::CreateParams params; |
| 311 | params.array_buffer_allocator = g_arrayBufferAllocator; |
| 312 | *pResultIsolate = v8::Isolate::New(params); |
| 313 | return true; |
| 314 | } |
| 315 | |
Lei Zhang | 3fa115b | 2015-10-08 12:04:47 -0700 | [diff] [blame] | 316 | size_t FXJS_GlobalIsolateRefCount() { |
| 317 | return g_isolate_ref_count; |
| 318 | } |
| 319 | |
weili | 625ad66 | 2016-06-15 11:21:33 -0700 | [diff] [blame] | 320 | FXJS_PerIsolateData::~FXJS_PerIsolateData() {} |
| 321 | |
Tom Sepez | ed7b2b5 | 2015-09-22 08:36:17 -0700 | [diff] [blame] | 322 | // static |
| 323 | void FXJS_PerIsolateData::SetUp(v8::Isolate* pIsolate) { |
Tom Sepez | 7d0fcbf | 2015-09-15 15:30:34 -0700 | [diff] [blame] | 324 | if (!pIsolate->GetData(g_embedderDataSlot)) |
weili | a4ad595 | 2016-09-22 10:38:53 -0700 | [diff] [blame] | 325 | pIsolate->SetData(g_embedderDataSlot, new FXJS_PerIsolateData(pIsolate)); |
Tom Sepez | ed7b2b5 | 2015-09-22 08:36:17 -0700 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | // static |
| 329 | FXJS_PerIsolateData* FXJS_PerIsolateData::Get(v8::Isolate* pIsolate) { |
| 330 | return static_cast<FXJS_PerIsolateData*>( |
| 331 | pIsolate->GetData(g_embedderDataSlot)); |
Tom Sepez | 7d0fcbf | 2015-09-15 15:30:34 -0700 | [diff] [blame] | 332 | } |
| 333 | |
Lei Zhang | 35d4587 | 2018-10-12 19:07:03 +0000 | [diff] [blame] | 334 | int FXJS_PerIsolateData::MaxObjDefinitionID() const { |
| 335 | return pdfium::CollectionSize<int>(m_ObjectDefnArray); |
| 336 | } |
| 337 | |
weili | a4ad595 | 2016-09-22 10:38:53 -0700 | [diff] [blame] | 338 | FXJS_PerIsolateData::FXJS_PerIsolateData(v8::Isolate* pIsolate) |
| 339 | : m_pDynamicObjsMap(new V8TemplateMap(pIsolate)) {} |
weili | 625ad66 | 2016-06-15 11:21:33 -0700 | [diff] [blame] | 340 | |
Tom Sepez | e3b782f | 2018-06-11 17:48:17 +0000 | [diff] [blame] | 341 | CFXJS_ObjDefinition* FXJS_PerIsolateData::ObjDefinitionForID(int id) const { |
| 342 | return (id >= 0 && id < MaxObjDefinitionID()) ? m_ObjectDefnArray[id].get() |
| 343 | : nullptr; |
| 344 | } |
| 345 | |
| 346 | int 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 Sepez | e0518bf | 2018-02-08 21:44:59 +0000 | [diff] [blame] | 352 | CFXJS_Engine::CFXJS_Engine() : CFX_V8(nullptr) {} |
tsepez | a494191 | 2016-08-15 11:40:12 -0700 | [diff] [blame] | 353 | |
Tom Sepez | e0518bf | 2018-02-08 21:44:59 +0000 | [diff] [blame] | 354 | CFXJS_Engine::CFXJS_Engine(v8::Isolate* pIsolate) : CFX_V8(pIsolate) {} |
weili | 0b2a987 | 2016-09-21 11:50:43 -0700 | [diff] [blame] | 355 | |
Dan Sinclair | 4bd2ad8 | 2017-12-06 03:12:09 +0000 | [diff] [blame] | 356 | CFXJS_Engine::~CFXJS_Engine() = default; |
Tom Sepez | 7d0fcbf | 2015-09-15 15:30:34 -0700 | [diff] [blame] | 357 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 358 | // static |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 359 | int CFXJS_Engine::GetObjDefnID(v8::Local<v8::Object> pObj) { |
Tom Sepez | 3f72fb4 | 2017-02-27 11:43:55 -0800 | [diff] [blame] | 360 | CFXJS_PerObjectData* pData = CFXJS_PerObjectData::GetFromObject(pObj); |
| 361 | return pData ? pData->m_ObjDefID : -1; |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 362 | } |
| 363 | |
| 364 | // static |
Tom Sepez | b3f1046 | 2018-02-08 20:16:09 +0000 | [diff] [blame] | 365 | void 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 |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 375 | void CFXJS_Engine::FreeObjectPrivate(v8::Local<v8::Object> pObj) { |
Tom Sepez | 3f72fb4 | 2017-02-27 11:43:55 -0800 | [diff] [blame] | 376 | CFXJS_PerObjectData* pData = CFXJS_PerObjectData::GetFromObject(pObj); |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 377 | pObj->SetAlignedPointerInInternalField(0, nullptr); |
Tom Sepez | 3f72fb4 | 2017-02-27 11:43:55 -0800 | [diff] [blame] | 378 | pObj->SetAlignedPointerInInternalField(1, nullptr); |
| 379 | delete pData; |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 380 | } |
| 381 | |
Tom Sepez | 892d751 | 2017-02-21 13:57:13 -0800 | [diff] [blame] | 382 | int CFXJS_Engine::DefineObj(const char* sObjName, |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 383 | FXJSOBJTYPE eObjType, |
| 384 | CFXJS_Engine::Constructor pConstructor, |
| 385 | CFXJS_Engine::Destructor pDestructor) { |
Dan Sinclair | 4bd2ad8 | 2017-12-06 03:12:09 +0000 | [diff] [blame] | 386 | v8::Isolate::Scope isolate_scope(GetIsolate()); |
| 387 | v8::HandleScope handle_scope(GetIsolate()); |
| 388 | FXJS_PerIsolateData::SetUp(GetIsolate()); |
Tom Sepez | e3b782f | 2018-06-11 17:48:17 +0000 | [diff] [blame] | 389 | 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 393 | } |
| 394 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 395 | void CFXJS_Engine::DefineObjMethod(int nObjDefnID, |
Tom Sepez | 9b99b63 | 2017-02-21 15:05:57 -0800 | [diff] [blame] | 396 | const char* sMethodName, |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 397 | v8::FunctionCallback pMethodCall) { |
Dan Sinclair | 4bd2ad8 | 2017-12-06 03:12:09 +0000 | [diff] [blame] | 398 | v8::Isolate::Scope isolate_scope(GetIsolate()); |
| 399 | v8::HandleScope handle_scope(GetIsolate()); |
Tom Sepez | e3b782f | 2018-06-11 17:48:17 +0000 | [diff] [blame] | 400 | FXJS_PerIsolateData* pIsolateData = FXJS_PerIsolateData::Get(GetIsolate()); |
| 401 | CFXJS_ObjDefinition* pObjDef = pIsolateData->ObjDefinitionForID(nObjDefnID); |
Tom Sepez | 5e873f5 | 2018-06-11 18:08:07 +0000 | [diff] [blame] | 402 | pObjDef->DefineMethod(NewString(sMethodName), pMethodCall); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 403 | } |
| 404 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 405 | void CFXJS_Engine::DefineObjProperty(int nObjDefnID, |
Tom Sepez | 4d5b8c5 | 2017-02-21 15:17:07 -0800 | [diff] [blame] | 406 | const char* sPropName, |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 407 | v8::AccessorGetterCallback pPropGet, |
| 408 | v8::AccessorSetterCallback pPropPut) { |
Dan Sinclair | 4bd2ad8 | 2017-12-06 03:12:09 +0000 | [diff] [blame] | 409 | v8::Isolate::Scope isolate_scope(GetIsolate()); |
| 410 | v8::HandleScope handle_scope(GetIsolate()); |
Tom Sepez | e3b782f | 2018-06-11 17:48:17 +0000 | [diff] [blame] | 411 | FXJS_PerIsolateData* pIsolateData = FXJS_PerIsolateData::Get(GetIsolate()); |
| 412 | CFXJS_ObjDefinition* pObjDef = pIsolateData->ObjDefinitionForID(nObjDefnID); |
Tom Sepez | 5e873f5 | 2018-06-11 18:08:07 +0000 | [diff] [blame] | 413 | pObjDef->DefineProperty(NewString(sPropName), pPropGet, pPropPut); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 414 | } |
| 415 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 416 | void CFXJS_Engine::DefineObjAllProperties( |
| 417 | int nObjDefnID, |
Franziska Hinkelmann | 9a635e8 | 2018-03-21 12:58:25 +0000 | [diff] [blame] | 418 | v8::GenericNamedPropertyQueryCallback pPropQurey, |
| 419 | v8::GenericNamedPropertyGetterCallback pPropGet, |
| 420 | v8::GenericNamedPropertySetterCallback pPropPut, |
| 421 | v8::GenericNamedPropertyDeleterCallback pPropDel) { |
Dan Sinclair | 4bd2ad8 | 2017-12-06 03:12:09 +0000 | [diff] [blame] | 422 | v8::Isolate::Scope isolate_scope(GetIsolate()); |
| 423 | v8::HandleScope handle_scope(GetIsolate()); |
Tom Sepez | e3b782f | 2018-06-11 17:48:17 +0000 | [diff] [blame] | 424 | FXJS_PerIsolateData* pIsolateData = FXJS_PerIsolateData::Get(GetIsolate()); |
| 425 | CFXJS_ObjDefinition* pObjDef = pIsolateData->ObjDefinitionForID(nObjDefnID); |
Tom Sepez | 5e873f5 | 2018-06-11 18:08:07 +0000 | [diff] [blame] | 426 | pObjDef->DefineAllProperties(pPropQurey, pPropGet, pPropPut, pPropDel); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 427 | } |
| 428 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 429 | void CFXJS_Engine::DefineObjConst(int nObjDefnID, |
Tom Sepez | 55db091 | 2017-02-21 15:26:52 -0800 | [diff] [blame] | 430 | const char* sConstName, |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 431 | v8::Local<v8::Value> pDefault) { |
Dan Sinclair | 4bd2ad8 | 2017-12-06 03:12:09 +0000 | [diff] [blame] | 432 | v8::Isolate::Scope isolate_scope(GetIsolate()); |
| 433 | v8::HandleScope handle_scope(GetIsolate()); |
Tom Sepez | e3b782f | 2018-06-11 17:48:17 +0000 | [diff] [blame] | 434 | FXJS_PerIsolateData* pIsolateData = FXJS_PerIsolateData::Get(GetIsolate()); |
| 435 | CFXJS_ObjDefinition* pObjDef = pIsolateData->ObjDefinitionForID(nObjDefnID); |
Tom Sepez | 5e873f5 | 2018-06-11 18:08:07 +0000 | [diff] [blame] | 436 | pObjDef->DefineConst(sConstName, pDefault); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 437 | } |
| 438 | |
Tom Sepez | 9b99b63 | 2017-02-21 15:05:57 -0800 | [diff] [blame] | 439 | void CFXJS_Engine::DefineGlobalMethod(const char* sMethodName, |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 440 | v8::FunctionCallback pMethodCall) { |
Dan Sinclair | 4bd2ad8 | 2017-12-06 03:12:09 +0000 | [diff] [blame] | 441 | v8::Isolate::Scope isolate_scope(GetIsolate()); |
| 442 | v8::HandleScope handle_scope(GetIsolate()); |
jochen | c4dedf3 | 2016-07-06 05:26:23 -0700 | [diff] [blame] | 443 | v8::Local<v8::FunctionTemplate> fun = |
Dan Sinclair | 4bd2ad8 | 2017-12-06 03:12:09 +0000 | [diff] [blame] | 444 | v8::FunctionTemplate::New(GetIsolate(), pMethodCall); |
jochen | c4dedf3 | 2016-07-06 05:26:23 -0700 | [diff] [blame] | 445 | fun->RemovePrototype(); |
Dan Sinclair | 4bd2ad8 | 2017-12-06 03:12:09 +0000 | [diff] [blame] | 446 | GetGlobalObjectTemplate(GetIsolate()) |
| 447 | ->Set(NewString(sMethodName), fun, v8::ReadOnly); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 448 | } |
| 449 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 450 | void CFXJS_Engine::DefineGlobalConst(const wchar_t* sConstName, |
| 451 | v8::FunctionCallback pConstGetter) { |
Dan Sinclair | 4bd2ad8 | 2017-12-06 03:12:09 +0000 | [diff] [blame] | 452 | v8::Isolate::Scope isolate_scope(GetIsolate()); |
| 453 | v8::HandleScope handle_scope(GetIsolate()); |
jochen | c4dedf3 | 2016-07-06 05:26:23 -0700 | [diff] [blame] | 454 | v8::Local<v8::FunctionTemplate> fun = |
Dan Sinclair | 4bd2ad8 | 2017-12-06 03:12:09 +0000 | [diff] [blame] | 455 | v8::FunctionTemplate::New(GetIsolate(), pConstGetter); |
jochen | c4dedf3 | 2016-07-06 05:26:23 -0700 | [diff] [blame] | 456 | fun->RemovePrototype(); |
Dan Sinclair | 4bd2ad8 | 2017-12-06 03:12:09 +0000 | [diff] [blame] | 457 | GetGlobalObjectTemplate(GetIsolate()) |
| 458 | ->SetAccessorProperty(NewString(sConstName), fun); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 459 | } |
| 460 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 461 | void CFXJS_Engine::InitializeEngine() { |
Dan Sinclair | 4bd2ad8 | 2017-12-06 03:12:09 +0000 | [diff] [blame] | 462 | if (GetIsolate() == g_isolate) |
Lei Zhang | 4d4a442 | 2015-10-08 12:00:14 -0700 | [diff] [blame] | 463 | ++g_isolate_ref_count; |
| 464 | |
Dan Sinclair | 4bd2ad8 | 2017-12-06 03:12:09 +0000 | [diff] [blame] | 465 | v8::Isolate::Scope isolate_scope(GetIsolate()); |
| 466 | v8::HandleScope handle_scope(GetIsolate()); |
dsinclair | 7d554c9 | 2016-06-20 06:06:31 -0700 | [diff] [blame] | 467 | |
| 468 | // This has to happen before we call GetGlobalObjectTemplate because that |
Dan Sinclair | 4bd2ad8 | 2017-12-06 03:12:09 +0000 | [diff] [blame] | 469 | // method gets the PerIsolateData from GetIsolate(). |
| 470 | FXJS_PerIsolateData::SetUp(GetIsolate()); |
dsinclair | 7d554c9 | 2016-06-20 06:06:31 -0700 | [diff] [blame] | 471 | |
Dan Sinclair | 4bd2ad8 | 2017-12-06 03:12:09 +0000 | [diff] [blame] | 472 | v8::Local<v8::Context> v8Context = v8::Context::New( |
| 473 | GetIsolate(), nullptr, GetGlobalObjectTemplate(GetIsolate())); |
Tom Sepez | 2aa01f5 | 2018-04-06 17:40:45 +0000 | [diff] [blame] | 474 | |
| 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 Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 487 | v8::Context::Scope context_scope(v8Context); |
Tom Sepez | e3b782f | 2018-06-11 17:48:17 +0000 | [diff] [blame] | 488 | FXJS_PerIsolateData* pIsolateData = FXJS_PerIsolateData::Get(GetIsolate()); |
| 489 | int maxID = pIsolateData->MaxObjDefinitionID(); |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 490 | m_StaticObjects.resize(maxID + 1); |
Tom Sepez | ed7b2b5 | 2015-09-22 08:36:17 -0700 | [diff] [blame] | 491 | for (int i = 0; i < maxID; ++i) { |
Tom Sepez | e3b782f | 2018-06-11 17:48:17 +0000 | [diff] [blame] | 492 | CFXJS_ObjDefinition* pObjDef = pIsolateData->ObjDefinitionForID(i); |
Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame] | 493 | if (pObjDef->m_ObjType == FXJSOBJTYPE_GLOBAL) { |
Tom Sepez | 3f72fb4 | 2017-02-27 11:43:55 -0800 | [diff] [blame] | 494 | CFXJS_PerObjectData::SetInObject(new CFXJS_PerObjectData(i), |
| 495 | v8Context->Global() |
| 496 | ->GetPrototype() |
| 497 | ->ToObject(v8Context) |
| 498 | .ToLocalChecked()); |
| 499 | if (pObjDef->m_pConstructor) { |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 500 | pObjDef->m_pConstructor(this, v8Context->Global() |
| 501 | ->GetPrototype() |
| 502 | ->ToObject(v8Context) |
| 503 | .ToLocalChecked()); |
Tom Sepez | 3f72fb4 | 2017-02-27 11:43:55 -0800 | [diff] [blame] | 504 | } |
Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame] | 505 | } else if (pObjDef->m_ObjType == FXJSOBJTYPE_STATIC) { |
Tom Sepez | f6ca07b | 2017-06-01 09:17:18 -0700 | [diff] [blame] | 506 | v8::Local<v8::String> pObjName = NewString(pObjDef->m_ObjName); |
Tom Sepez | 55ccb52 | 2018-08-14 23:40:10 +0000 | [diff] [blame] | 507 | v8::Local<v8::Object> obj = NewFXJSBoundObject(i, FXJSOBJTYPE_STATIC); |
Tom Sepez | 63b2fc7 | 2017-08-14 16:24:29 -0700 | [diff] [blame] | 508 | if (!obj.IsEmpty()) { |
| 509 | v8Context->Global()->Set(v8Context, pObjName, obj).FromJust(); |
Tom Sepez | f3f1869 | 2018-02-06 20:44:05 +0000 | [diff] [blame] | 510 | m_StaticObjects[i] = v8::Global<v8::Object>(GetIsolate(), obj); |
Tom Sepez | 63b2fc7 | 2017-08-14 16:24:29 -0700 | [diff] [blame] | 511 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 512 | } |
| 513 | } |
Tom Sepez | 1258f7f | 2018-02-02 17:37:37 +0000 | [diff] [blame] | 514 | m_V8Context.Reset(GetIsolate(), v8Context); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 515 | } |
| 516 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 517 | void CFXJS_Engine::ReleaseEngine() { |
Dan Sinclair | 4bd2ad8 | 2017-12-06 03:12:09 +0000 | [diff] [blame] | 518 | v8::Isolate::Scope isolate_scope(GetIsolate()); |
| 519 | v8::HandleScope handle_scope(GetIsolate()); |
Tom Sepez | 1258f7f | 2018-02-02 17:37:37 +0000 | [diff] [blame] | 520 | v8::Local<v8::Context> context = GetV8Context(); |
Tom Sepez | 808a99e | 2015-09-10 12:28:37 -0700 | [diff] [blame] | 521 | v8::Context::Scope context_scope(context); |
Tom Sepez | e3b782f | 2018-06-11 17:48:17 +0000 | [diff] [blame] | 522 | FXJS_PerIsolateData* pIsolateData = FXJS_PerIsolateData::Get(GetIsolate()); |
| 523 | if (!pIsolateData) |
Tom Sepez | 129a60e | 2016-03-24 09:19:20 -0700 | [diff] [blame] | 524 | return; |
Tom Sepez | 129a60e | 2016-03-24 09:19:20 -0700 | [diff] [blame] | 525 | |
Tom Sepez | b5b7972 | 2018-02-05 15:13:09 +0000 | [diff] [blame] | 526 | m_ConstArrays.clear(); |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 527 | |
Tom Sepez | e3b782f | 2018-06-11 17:48:17 +0000 | [diff] [blame] | 528 | for (int i = 0; i < pIsolateData->MaxObjDefinitionID(); ++i) { |
| 529 | CFXJS_ObjDefinition* pObjDef = pIsolateData->ObjDefinitionForID(i); |
Oliver Chang | 8f16b69 | 2015-10-27 09:34:49 -0700 | [diff] [blame] | 530 | v8::Local<v8::Object> pObj; |
| 531 | if (pObjDef->m_ObjType == FXJSOBJTYPE_GLOBAL) { |
| 532 | pObj = |
| 533 | context->Global()->GetPrototype()->ToObject(context).ToLocalChecked(); |
Tom Sepez | f3f1869 | 2018-02-06 20:44:05 +0000 | [diff] [blame] | 534 | } else if (!m_StaticObjects[i].IsEmpty()) { |
| 535 | pObj = v8::Local<v8::Object>::New(GetIsolate(), m_StaticObjects[i]); |
| 536 | m_StaticObjects[i].Reset(); |
Oliver Chang | 8f16b69 | 2015-10-27 09:34:49 -0700 | [diff] [blame] | 537 | } |
Oliver Chang | 8f16b69 | 2015-10-27 09:34:49 -0700 | [diff] [blame] | 538 | if (!pObj.IsEmpty()) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 539 | if (pObjDef->m_pDestructor) |
Tom Sepez | b3f1046 | 2018-02-08 20:16:09 +0000 | [diff] [blame] | 540 | pObjDef->m_pDestructor(pObj); |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 541 | FreeObjectPrivate(pObj); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 542 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 543 | } |
Tom Sepez | 808a99e | 2015-09-10 12:28:37 -0700 | [diff] [blame] | 544 | |
Tom Sepez | 1258f7f | 2018-02-02 17:37:37 +0000 | [diff] [blame] | 545 | m_V8Context.Reset(); |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 546 | |
Dan Sinclair | 4bd2ad8 | 2017-12-06 03:12:09 +0000 | [diff] [blame] | 547 | if (GetIsolate() == g_isolate && --g_isolate_ref_count > 0) |
Tom Sepez | 4237aed | 2015-11-10 15:19:17 -0800 | [diff] [blame] | 548 | return; |
| 549 | |
Tom Sepez | e3b782f | 2018-06-11 17:48:17 +0000 | [diff] [blame] | 550 | delete pIsolateData; |
Dan Sinclair | 4bd2ad8 | 2017-12-06 03:12:09 +0000 | [diff] [blame] | 551 | GetIsolate()->SetData(g_embedderDataSlot, nullptr); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 552 | } |
| 553 | |
Dan Sinclair | dc5d88b | 2018-05-17 13:53:52 +0000 | [diff] [blame] | 554 | Optional<IJS_Runtime::JS_Error> CFXJS_Engine::Execute( |
| 555 | const WideString& script) { |
Dan Sinclair | 4bd2ad8 | 2017-12-06 03:12:09 +0000 | [diff] [blame] | 556 | v8::Isolate::Scope isolate_scope(GetIsolate()); |
| 557 | v8::TryCatch try_catch(GetIsolate()); |
| 558 | v8::Local<v8::Context> context = GetIsolate()->GetCurrentContext(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 559 | v8::Local<v8::Script> compiled_script; |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 560 | if (!v8::Script::Compile(context, NewString(script.AsStringView())) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 561 | .ToLocal(&compiled_script)) { |
Adam Klein | 4c451ba | 2018-01-17 21:06:27 +0000 | [diff] [blame] | 562 | v8::String::Utf8Value error(GetIsolate(), try_catch.Exception()); |
Dan Sinclair | dc5d88b | 2018-05-17 13:53:52 +0000 | [diff] [blame] | 563 | v8::Local<v8::Message> msg = try_catch.Message(); |
Lei Zhang | 696f5d3 | 2018-12-08 00:02:50 +0000 | [diff] [blame] | 564 | 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 Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 568 | } |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 569 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 570 | v8::Local<v8::Value> result; |
| 571 | if (!compiled_script->Run(context).ToLocal(&result)) { |
Adam Klein | 4c451ba | 2018-01-17 21:06:27 +0000 | [diff] [blame] | 572 | v8::String::Utf8Value error(GetIsolate(), try_catch.Exception()); |
Dan Sinclair | dc5d88b | 2018-05-17 13:53:52 +0000 | [diff] [blame] | 573 | auto msg = try_catch.Message(); |
Lei Zhang | 696f5d3 | 2018-12-08 00:02:50 +0000 | [diff] [blame] | 574 | 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 Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 578 | } |
Dan Sinclair | dc5d88b | 2018-05-17 13:53:52 +0000 | [diff] [blame] | 579 | return pdfium::nullopt; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 580 | } |
| 581 | |
Tom Sepez | 9ad9a5f | 2018-02-07 21:07:24 +0000 | [diff] [blame] | 582 | v8::Local<v8::Object> CFXJS_Engine::NewFXJSBoundObject(int nObjDefnID, |
Tom Sepez | 55ccb52 | 2018-08-14 23:40:10 +0000 | [diff] [blame] | 583 | FXJSOBJTYPE type) { |
Dan Sinclair | 4bd2ad8 | 2017-12-06 03:12:09 +0000 | [diff] [blame] | 584 | v8::Isolate::Scope isolate_scope(GetIsolate()); |
| 585 | v8::Local<v8::Context> context = GetIsolate()->GetCurrentContext(); |
Dan Sinclair | 4bd2ad8 | 2017-12-06 03:12:09 +0000 | [diff] [blame] | 586 | FXJS_PerIsolateData* pData = FXJS_PerIsolateData::Get(GetIsolate()); |
Tom Sepez | ed7b2b5 | 2015-09-22 08:36:17 -0700 | [diff] [blame] | 587 | if (!pData) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 588 | return v8::Local<v8::Object>(); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 589 | |
Tom Sepez | e3b782f | 2018-06-11 17:48:17 +0000 | [diff] [blame] | 590 | CFXJS_ObjDefinition* pObjDef = pData->ObjDefinitionForID(nObjDefnID); |
| 591 | if (!pObjDef) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 592 | return v8::Local<v8::Object>(); |
Tom Sepez | ed7b2b5 | 2015-09-22 08:36:17 -0700 | [diff] [blame] | 593 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 594 | v8::Local<v8::Object> obj; |
Tom Sepez | 016a347 | 2015-09-30 15:50:57 -0700 | [diff] [blame] | 595 | if (!pObjDef->GetInstanceTemplate()->NewInstance(context).ToLocal(&obj)) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 596 | return v8::Local<v8::Object>(); |
Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 597 | |
Tom Sepez | 3f72fb4 | 2017-02-27 11:43:55 -0800 | [diff] [blame] | 598 | CFXJS_PerObjectData* pObjData = new CFXJS_PerObjectData(nObjDefnID); |
| 599 | CFXJS_PerObjectData::SetInObject(pObjData, obj); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 600 | if (pObjDef->m_pConstructor) |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 601 | pObjDef->m_pConstructor(this, obj); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 602 | |
Tom Sepez | 55ccb52 | 2018-08-14 23:40:10 +0000 | [diff] [blame] | 603 | if (type == FXJSOBJTYPE_DYNAMIC) { |
Tom Sepez | a0395e5 | 2018-02-09 19:28:39 +0000 | [diff] [blame] | 604 | auto* pIsolateData = FXJS_PerIsolateData::Get(GetIsolate()); |
| 605 | if (pIsolateData->m_pDynamicObjsMap) |
| 606 | pIsolateData->m_pDynamicObjsMap->SetAndMakeWeak(pObjData, obj); |
| 607 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 608 | return obj; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 609 | } |
| 610 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 611 | v8::Local<v8::Object> CFXJS_Engine::GetThisObj() { |
Dan Sinclair | 4bd2ad8 | 2017-12-06 03:12:09 +0000 | [diff] [blame] | 612 | v8::Isolate::Scope isolate_scope(GetIsolate()); |
| 613 | if (!FXJS_PerIsolateData::Get(GetIsolate())) |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 614 | return v8::Local<v8::Object>(); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 615 | |
Tom Sepez | ed7b2b5 | 2015-09-22 08:36:17 -0700 | [diff] [blame] | 616 | // Return the global object. |
Dan Sinclair | 4bd2ad8 | 2017-12-06 03:12:09 +0000 | [diff] [blame] | 617 | v8::Local<v8::Context> context = GetIsolate()->GetCurrentContext(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 618 | return context->Global()->GetPrototype()->ToObject(context).ToLocalChecked(); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 619 | } |
| 620 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 621 | void CFXJS_Engine::Error(const WideString& message) { |
Dan Sinclair | 4bd2ad8 | 2017-12-06 03:12:09 +0000 | [diff] [blame] | 622 | GetIsolate()->ThrowException(NewString(message.AsStringView())); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 623 | } |
| 624 | |
Lei Zhang | 378ec54 | 2018-10-18 19:15:47 +0000 | [diff] [blame] | 625 | v8::Local<v8::Context> CFXJS_Engine::GetV8Context() { |
| 626 | return v8::Local<v8::Context>::New(GetIsolate(), m_V8Context); |
| 627 | } |
| 628 | |
Tom Sepez | ddaa40f | 2018-06-06 18:30:15 +0000 | [diff] [blame] | 629 | // static |
Tom Sepez | 57e0977 | 2018-02-05 18:52:09 +0000 | [diff] [blame] | 630 | CJS_Object* CFXJS_Engine::GetObjectPrivate(v8::Local<v8::Object> pObj) { |
Tom Sepez | ff402c2 | 2018-07-17 00:12:56 +0000 | [diff] [blame] | 631 | 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-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 658 | } |
Tom Sepez | b5b7972 | 2018-02-05 15:13:09 +0000 | [diff] [blame] | 659 | |
| 660 | v8::Local<v8::Array> CFXJS_Engine::GetConstArray(const WideString& name) { |
| 661 | return v8::Local<v8::Array>::New(GetIsolate(), m_ConstArrays[name]); |
| 662 | } |
| 663 | |
| 664 | void CFXJS_Engine::SetConstArray(const WideString& name, |
| 665 | v8::Local<v8::Array> array) { |
| 666 | m_ConstArrays[name] = v8::Global<v8::Array>(GetIsolate(), array); |
| 667 | } |