John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -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 | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 5 | // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | |
Dan Sinclair | e0345a4 | 2017-10-30 20:20:42 +0000 | [diff] [blame] | 7 | #include "fxjs/cjs_global.h" |
Tom Sepez | 3745841 | 2015-10-06 11:33:46 -0700 | [diff] [blame] | 8 | |
Dan Sinclair | 4b172c4 | 2017-10-23 11:22:31 -0400 | [diff] [blame] | 9 | #include <map> |
| 10 | #include <memory> |
Tom Sepez | a775723 | 2017-04-18 11:10:39 -0700 | [diff] [blame] | 11 | #include <utility> |
Dan Sinclair | 3ebd121 | 2016-03-09 09:59:23 -0500 | [diff] [blame] | 12 | #include <vector> |
| 13 | |
Dan Sinclair | cfb1944 | 2017-04-20 13:13:04 -0400 | [diff] [blame] | 14 | #include "core/fxcrt/fx_extension.h" |
Dan Sinclair | e0345a4 | 2017-10-30 20:20:42 +0000 | [diff] [blame] | 15 | #include "fxjs/JS_Define.h" |
Dan Sinclair | e0345a4 | 2017-10-30 20:20:42 +0000 | [diff] [blame] | 16 | #include "fxjs/cjs_event_context.h" |
| 17 | #include "fxjs/cjs_eventhandler.h" |
Dan Sinclair | a325462 | 2017-10-30 20:24:14 +0000 | [diff] [blame] | 18 | #include "fxjs/cjs_globaldata.h" |
| 19 | #include "fxjs/cjs_keyvalue.h" |
Dan Sinclair | e0345a4 | 2017-10-30 20:20:42 +0000 | [diff] [blame] | 20 | #include "fxjs/cjs_object.h" |
| 21 | #include "fxjs/js_resources.h" |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 22 | |
Dan Sinclair | 4b172c4 | 2017-10-23 11:22:31 -0400 | [diff] [blame] | 23 | namespace { |
| 24 | |
Adam Klein | 4c451ba | 2018-01-17 21:06:27 +0000 | [diff] [blame^] | 25 | WideString PropFromV8Prop(v8::Isolate* pIsolate, |
| 26 | v8::Local<v8::String> property) { |
| 27 | v8::String::Utf8Value utf8_value(pIsolate, property); |
Dan Sinclair | 8f524d6 | 2017-10-25 13:30:31 -0400 | [diff] [blame] | 28 | return WideString::FromUTF8(ByteStringView(*utf8_value, utf8_value.length())); |
| 29 | } |
| 30 | |
Dan Sinclair | 4b172c4 | 2017-10-23 11:22:31 -0400 | [diff] [blame] | 31 | template <class Alt> |
| 32 | void JSSpecialPropQuery(const char*, |
| 33 | v8::Local<v8::String> property, |
| 34 | const v8::PropertyCallbackInfo<v8::Integer>& info) { |
| 35 | CJS_Runtime* pRuntime = |
| 36 | CJS_Runtime::CurrentRuntimeFromIsolate(info.GetIsolate()); |
| 37 | if (!pRuntime) |
| 38 | return; |
| 39 | |
| 40 | CJS_Object* pJSObj = |
| 41 | static_cast<CJS_Object*>(pRuntime->GetObjectPrivate(info.Holder())); |
| 42 | if (!pJSObj) |
| 43 | return; |
| 44 | |
| 45 | Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject()); |
Adam Klein | 4c451ba | 2018-01-17 21:06:27 +0000 | [diff] [blame^] | 46 | CJS_Return result = |
| 47 | pObj->QueryProperty(PropFromV8Prop(info.GetIsolate(), property).c_str()); |
Dan Sinclair | 8f524d6 | 2017-10-25 13:30:31 -0400 | [diff] [blame] | 48 | info.GetReturnValue().Set(!result.HasError() ? 4 : 0); |
Dan Sinclair | 4b172c4 | 2017-10-23 11:22:31 -0400 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | template <class Alt> |
| 52 | void JSSpecialPropGet(const char* class_name, |
| 53 | v8::Local<v8::String> property, |
| 54 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 55 | CJS_Runtime* pRuntime = |
| 56 | CJS_Runtime::CurrentRuntimeFromIsolate(info.GetIsolate()); |
| 57 | if (!pRuntime) |
| 58 | return; |
| 59 | |
| 60 | CJS_Object* pJSObj = |
| 61 | static_cast<CJS_Object*>(pRuntime->GetObjectPrivate(info.Holder())); |
| 62 | if (!pJSObj) |
| 63 | return; |
| 64 | |
| 65 | Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject()); |
Adam Klein | 4c451ba | 2018-01-17 21:06:27 +0000 | [diff] [blame^] | 66 | CJS_Return result = pObj->GetProperty( |
| 67 | pRuntime, PropFromV8Prop(info.GetIsolate(), property).c_str()); |
Dan Sinclair | 8f524d6 | 2017-10-25 13:30:31 -0400 | [diff] [blame] | 68 | if (result.HasError()) { |
| 69 | pRuntime->Error( |
| 70 | JSFormatErrorString(class_name, "GetProperty", result.Error())); |
Dan Sinclair | 4b172c4 | 2017-10-23 11:22:31 -0400 | [diff] [blame] | 71 | return; |
| 72 | } |
Dan Sinclair | 8f524d6 | 2017-10-25 13:30:31 -0400 | [diff] [blame] | 73 | |
| 74 | if (result.HasReturn()) |
| 75 | info.GetReturnValue().Set(result.Return()); |
Dan Sinclair | 4b172c4 | 2017-10-23 11:22:31 -0400 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | template <class Alt> |
| 79 | void JSSpecialPropPut(const char* class_name, |
| 80 | v8::Local<v8::String> property, |
| 81 | v8::Local<v8::Value> value, |
| 82 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 83 | CJS_Runtime* pRuntime = |
| 84 | CJS_Runtime::CurrentRuntimeFromIsolate(info.GetIsolate()); |
| 85 | if (!pRuntime) |
| 86 | return; |
| 87 | |
| 88 | CJS_Object* pJSObj = |
| 89 | static_cast<CJS_Object*>(pRuntime->GetObjectPrivate(info.Holder())); |
| 90 | if (!pJSObj) |
| 91 | return; |
| 92 | |
| 93 | Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject()); |
Adam Klein | 4c451ba | 2018-01-17 21:06:27 +0000 | [diff] [blame^] | 94 | CJS_Return result = pObj->SetProperty( |
| 95 | pRuntime, PropFromV8Prop(info.GetIsolate(), property).c_str(), value); |
Dan Sinclair | 8f524d6 | 2017-10-25 13:30:31 -0400 | [diff] [blame] | 96 | if (result.HasError()) { |
| 97 | pRuntime->Error( |
| 98 | JSFormatErrorString(class_name, "PutProperty", result.Error())); |
| 99 | } |
Dan Sinclair | 4b172c4 | 2017-10-23 11:22:31 -0400 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | template <class Alt> |
| 103 | void JSSpecialPropDel(const char* class_name, |
| 104 | v8::Local<v8::String> property, |
| 105 | const v8::PropertyCallbackInfo<v8::Boolean>& info) { |
| 106 | CJS_Runtime* pRuntime = |
| 107 | CJS_Runtime::CurrentRuntimeFromIsolate(info.GetIsolate()); |
| 108 | if (!pRuntime) |
| 109 | return; |
| 110 | |
| 111 | CJS_Object* pJSObj = |
| 112 | static_cast<CJS_Object*>(pRuntime->GetObjectPrivate(info.Holder())); |
| 113 | if (!pJSObj) |
| 114 | return; |
| 115 | |
| 116 | Alt* pObj = reinterpret_cast<Alt*>(pJSObj->GetEmbedObject()); |
Adam Klein | 4c451ba | 2018-01-17 21:06:27 +0000 | [diff] [blame^] | 117 | CJS_Return result = pObj->DelProperty( |
| 118 | pRuntime, PropFromV8Prop(info.GetIsolate(), property).c_str()); |
Dan Sinclair | 8f524d6 | 2017-10-25 13:30:31 -0400 | [diff] [blame] | 119 | if (result.HasError()) { |
| 120 | // TODO(dsinclair): Should this set the pRuntime->Error result? |
Henrique Nakashima | 1150630 | 2017-12-06 22:09:40 +0000 | [diff] [blame] | 121 | // ByteString cbName = |
| 122 | // ByteString::Format("%s.%s", class_name, "DelProperty"); |
Dan Sinclair | 4b172c4 | 2017-10-23 11:22:31 -0400 | [diff] [blame] | 123 | } |
| 124 | } |
| 125 | |
| 126 | struct JSGlobalData { |
| 127 | JSGlobalData(); |
| 128 | ~JSGlobalData(); |
| 129 | |
| 130 | JS_GlobalDataType nType; |
| 131 | double dData; |
| 132 | bool bData; |
| 133 | ByteString sData; |
| 134 | v8::Global<v8::Object> pData; |
| 135 | bool bPersistent; |
| 136 | bool bDeleted; |
| 137 | }; |
| 138 | |
| 139 | class JSGlobalAlternate : public CJS_EmbedObj { |
| 140 | public: |
| 141 | explicit JSGlobalAlternate(CJS_Object* pJSObject); |
| 142 | ~JSGlobalAlternate() override; |
| 143 | |
Dan Sinclair | 8f524d6 | 2017-10-25 13:30:31 -0400 | [diff] [blame] | 144 | CJS_Return setPersistent(CJS_Runtime* pRuntime, |
| 145 | const std::vector<v8::Local<v8::Value>>& params); |
| 146 | CJS_Return QueryProperty(const wchar_t* propname); |
| 147 | CJS_Return GetProperty(CJS_Runtime* pRuntime, const wchar_t* propname); |
| 148 | CJS_Return SetProperty(CJS_Runtime* pRuntime, |
| 149 | const wchar_t* propname, |
| 150 | v8::Local<v8::Value> vp); |
| 151 | CJS_Return DelProperty(CJS_Runtime* pRuntime, const wchar_t* propname); |
Dan Sinclair | 4b172c4 | 2017-10-23 11:22:31 -0400 | [diff] [blame] | 152 | void Initial(CPDFSDK_FormFillEnvironment* pFormFillEnv); |
| 153 | |
| 154 | private: |
| 155 | void UpdateGlobalPersistentVariables(); |
| 156 | void CommitGlobalPersisitentVariables(CJS_Runtime* pRuntime); |
| 157 | void DestroyGlobalPersisitentVariables(); |
Dan Sinclair | 8f524d6 | 2017-10-25 13:30:31 -0400 | [diff] [blame] | 158 | CJS_Return SetGlobalVariables(const ByteString& propname, |
| 159 | JS_GlobalDataType nType, |
| 160 | double dData, |
| 161 | bool bData, |
| 162 | const ByteString& sData, |
| 163 | v8::Local<v8::Object> pData, |
| 164 | bool bDefaultPersistent); |
Dan Sinclair | 4b172c4 | 2017-10-23 11:22:31 -0400 | [diff] [blame] | 165 | void ObjectToArray(CJS_Runtime* pRuntime, |
| 166 | v8::Local<v8::Object> pObj, |
| 167 | CJS_GlobalVariableArray& array); |
| 168 | void PutObjectProperty(v8::Local<v8::Object> obj, CJS_KeyValue* pData); |
| 169 | |
| 170 | std::map<ByteString, std::unique_ptr<JSGlobalData>> m_MapGlobal; |
| 171 | WideString m_sFilePath; |
| 172 | CJS_GlobalData* m_pGlobalData; |
| 173 | CPDFSDK_FormFillEnvironment::ObservedPtr m_pFormFillEnv; |
| 174 | }; |
| 175 | |
| 176 | } // namespace |
| 177 | |
Dan Sinclair | c94a793 | 2017-10-26 16:48:57 -0400 | [diff] [blame] | 178 | const JSMethodSpec CJS_Global::MethodSpecs[] = { |
Dan Sinclair | 909fa2d | 2017-12-12 01:53:28 +0000 | [diff] [blame] | 179 | {"setPersistent", setPersistent_static}}; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 180 | |
Dan Sinclair | ef29953 | 2017-10-26 16:48:30 -0400 | [diff] [blame] | 181 | int CJS_Global::ObjDefnID = -1; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 182 | |
Dan Sinclair | 4b172c4 | 2017-10-23 11:22:31 -0400 | [diff] [blame] | 183 | // static |
| 184 | void CJS_Global::setPersistent_static( |
| 185 | const v8::FunctionCallbackInfo<v8::Value>& info) { |
| 186 | JSMethod<JSGlobalAlternate, &JSGlobalAlternate::setPersistent>( |
| 187 | "setPersistent", "global", info); |
| 188 | } |
| 189 | |
Dan Sinclair | ef29953 | 2017-10-26 16:48:30 -0400 | [diff] [blame] | 190 | // static |
Dan Sinclair | 89d26c8 | 2017-10-26 12:21:28 -0400 | [diff] [blame] | 191 | void CJS_Global::queryprop_static( |
| 192 | v8::Local<v8::String> property, |
| 193 | const v8::PropertyCallbackInfo<v8::Integer>& info) { |
| 194 | JSSpecialPropQuery<JSGlobalAlternate>("global", property, info); |
| 195 | } |
| 196 | |
Dan Sinclair | ef29953 | 2017-10-26 16:48:30 -0400 | [diff] [blame] | 197 | // static |
Dan Sinclair | 89d26c8 | 2017-10-26 12:21:28 -0400 | [diff] [blame] | 198 | void CJS_Global::getprop_static( |
| 199 | v8::Local<v8::String> property, |
| 200 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 201 | JSSpecialPropGet<JSGlobalAlternate>("global", property, info); |
| 202 | } |
| 203 | |
Dan Sinclair | ef29953 | 2017-10-26 16:48:30 -0400 | [diff] [blame] | 204 | // static |
Dan Sinclair | 89d26c8 | 2017-10-26 12:21:28 -0400 | [diff] [blame] | 205 | void CJS_Global::putprop_static( |
| 206 | v8::Local<v8::String> property, |
| 207 | v8::Local<v8::Value> value, |
| 208 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 209 | JSSpecialPropPut<JSGlobalAlternate>("global", property, value, info); |
| 210 | } |
| 211 | |
Dan Sinclair | ef29953 | 2017-10-26 16:48:30 -0400 | [diff] [blame] | 212 | // static |
Dan Sinclair | 89d26c8 | 2017-10-26 12:21:28 -0400 | [diff] [blame] | 213 | void CJS_Global::delprop_static( |
| 214 | v8::Local<v8::String> property, |
| 215 | const v8::PropertyCallbackInfo<v8::Boolean>& info) { |
| 216 | JSSpecialPropDel<JSGlobalAlternate>("global", property, info); |
| 217 | } |
| 218 | |
Dan Sinclair | ef29953 | 2017-10-26 16:48:30 -0400 | [diff] [blame] | 219 | // static |
Dan Sinclair | 89d26c8 | 2017-10-26 12:21:28 -0400 | [diff] [blame] | 220 | void CJS_Global::DefineAllProperties(CFXJS_Engine* pEngine) { |
| 221 | pEngine->DefineObjAllProperties( |
Dan Sinclair | ef29953 | 2017-10-26 16:48:30 -0400 | [diff] [blame] | 222 | ObjDefnID, CJS_Global::queryprop_static, CJS_Global::getprop_static, |
Dan Sinclair | 89d26c8 | 2017-10-26 12:21:28 -0400 | [diff] [blame] | 223 | CJS_Global::putprop_static, CJS_Global::delprop_static); |
| 224 | } |
| 225 | |
Dan Sinclair | ef29953 | 2017-10-26 16:48:30 -0400 | [diff] [blame] | 226 | // static |
Dan Sinclair | bef4d3e | 2017-10-26 16:49:38 -0400 | [diff] [blame] | 227 | void CJS_Global::DefineJSObjects(CFXJS_Engine* pEngine) { |
| 228 | ObjDefnID = pEngine->DefineObj("global", FXJSOBJTYPE_STATIC, |
Dan Sinclair | ef29953 | 2017-10-26 16:48:30 -0400 | [diff] [blame] | 229 | JSConstructor<CJS_Global, JSGlobalAlternate>, |
| 230 | JSDestructor<CJS_Global>); |
Dan Sinclair | 909fa2d | 2017-12-12 01:53:28 +0000 | [diff] [blame] | 231 | DefineMethods(pEngine, ObjDefnID, MethodSpecs, FX_ArraySize(MethodSpecs)); |
Dan Sinclair | 89d26c8 | 2017-10-26 12:21:28 -0400 | [diff] [blame] | 232 | DefineAllProperties(pEngine); |
| 233 | } |
| 234 | |
Dan Sinclair | ef29953 | 2017-10-26 16:48:30 -0400 | [diff] [blame] | 235 | void CJS_Global::InitInstance(IJS_Runtime* pIRuntime) { |
| 236 | CJS_Runtime* pRuntime = static_cast<CJS_Runtime*>(pIRuntime); |
| 237 | JSGlobalAlternate* pGlobal = |
| 238 | static_cast<JSGlobalAlternate*>(GetEmbedObject()); |
| 239 | pGlobal->Initial(pRuntime->GetFormFillEnv()); |
| 240 | } |
| 241 | |
weili | 625ad66 | 2016-06-15 11:21:33 -0700 | [diff] [blame] | 242 | JSGlobalData::JSGlobalData() |
weili | 47228ac | 2016-07-20 10:35:31 -0700 | [diff] [blame] | 243 | : nType(JS_GlobalDataType::NUMBER), |
weili | 625ad66 | 2016-06-15 11:21:33 -0700 | [diff] [blame] | 244 | dData(0), |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 245 | bData(false), |
weili | 625ad66 | 2016-06-15 11:21:33 -0700 | [diff] [blame] | 246 | sData(""), |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 247 | bPersistent(false), |
| 248 | bDeleted(false) {} |
weili | 625ad66 | 2016-06-15 11:21:33 -0700 | [diff] [blame] | 249 | |
| 250 | JSGlobalData::~JSGlobalData() { |
| 251 | pData.Reset(); |
| 252 | } |
| 253 | |
Tom Sepez | 7dc5cc1 | 2015-08-17 12:15:26 -0700 | [diff] [blame] | 254 | JSGlobalAlternate::JSGlobalAlternate(CJS_Object* pJSObject) |
dsinclair | 8779fa8 | 2016-10-12 12:05:44 -0700 | [diff] [blame] | 255 | : CJS_EmbedObj(pJSObject), m_pFormFillEnv(nullptr) {} |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 256 | |
Tom Sepez | 7dc5cc1 | 2015-08-17 12:15:26 -0700 | [diff] [blame] | 257 | JSGlobalAlternate::~JSGlobalAlternate() { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 258 | DestroyGlobalPersisitentVariables(); |
Tom Sepez | f458362 | 2015-09-14 15:06:53 -0700 | [diff] [blame] | 259 | m_pGlobalData->Release(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 260 | } |
| 261 | |
dsinclair | 8779fa8 | 2016-10-12 12:05:44 -0700 | [diff] [blame] | 262 | void JSGlobalAlternate::Initial(CPDFSDK_FormFillEnvironment* pFormFillEnv) { |
Tom Sepez | 77f6d0f | 2017-02-23 12:14:10 -0800 | [diff] [blame] | 263 | m_pFormFillEnv.Reset(pFormFillEnv); |
dsinclair | 8779fa8 | 2016-10-12 12:05:44 -0700 | [diff] [blame] | 264 | m_pGlobalData = CJS_GlobalData::GetRetainedInstance(pFormFillEnv); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 265 | UpdateGlobalPersistentVariables(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 266 | } |
Tom Sepez | bdeeb8a | 2015-05-27 12:25:00 -0700 | [diff] [blame] | 267 | |
Dan Sinclair | 8f524d6 | 2017-10-25 13:30:31 -0400 | [diff] [blame] | 268 | CJS_Return JSGlobalAlternate::QueryProperty(const wchar_t* propname) { |
| 269 | return CJS_Return(WideString(propname) != L"setPersistent"); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 270 | } |
| 271 | |
Dan Sinclair | 8f524d6 | 2017-10-25 13:30:31 -0400 | [diff] [blame] | 272 | CJS_Return JSGlobalAlternate::DelProperty(CJS_Runtime* pRuntime, |
| 273 | const wchar_t* propname) { |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 274 | auto it = m_MapGlobal.find(ByteString::FromUnicode(propname)); |
Tom Sepez | a775723 | 2017-04-18 11:10:39 -0700 | [diff] [blame] | 275 | if (it == m_MapGlobal.end()) |
Dan Sinclair | 8f524d6 | 2017-10-25 13:30:31 -0400 | [diff] [blame] | 276 | return CJS_Return(false); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 277 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 278 | it->second->bDeleted = true; |
Dan Sinclair | 8f524d6 | 2017-10-25 13:30:31 -0400 | [diff] [blame] | 279 | return CJS_Return(true); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 280 | } |
| 281 | |
Dan Sinclair | 8f524d6 | 2017-10-25 13:30:31 -0400 | [diff] [blame] | 282 | CJS_Return JSGlobalAlternate::GetProperty(CJS_Runtime* pRuntime, |
| 283 | const wchar_t* propname) { |
dan sinclair | cbe23db | 2017-10-19 14:29:33 -0400 | [diff] [blame] | 284 | auto it = m_MapGlobal.find(ByteString::FromUnicode(propname)); |
dan sinclair | 5daf07a | 2017-10-24 21:46:57 -0400 | [diff] [blame] | 285 | if (it == m_MapGlobal.end()) |
Dan Sinclair | 8f524d6 | 2017-10-25 13:30:31 -0400 | [diff] [blame] | 286 | return CJS_Return(true); |
dan sinclair | cbe23db | 2017-10-19 14:29:33 -0400 | [diff] [blame] | 287 | |
| 288 | JSGlobalData* pData = it->second.get(); |
dan sinclair | 5daf07a | 2017-10-24 21:46:57 -0400 | [diff] [blame] | 289 | if (pData->bDeleted) |
Dan Sinclair | 8f524d6 | 2017-10-25 13:30:31 -0400 | [diff] [blame] | 290 | return CJS_Return(true); |
dan sinclair | cbe23db | 2017-10-19 14:29:33 -0400 | [diff] [blame] | 291 | |
| 292 | switch (pData->nType) { |
| 293 | case JS_GlobalDataType::NUMBER: |
Dan Sinclair | 8f524d6 | 2017-10-25 13:30:31 -0400 | [diff] [blame] | 294 | return CJS_Return(pRuntime->NewNumber(pData->dData)); |
dan sinclair | cbe23db | 2017-10-19 14:29:33 -0400 | [diff] [blame] | 295 | case JS_GlobalDataType::BOOLEAN: |
Dan Sinclair | 8f524d6 | 2017-10-25 13:30:31 -0400 | [diff] [blame] | 296 | return CJS_Return(pRuntime->NewBoolean(pData->bData)); |
dan sinclair | cbe23db | 2017-10-19 14:29:33 -0400 | [diff] [blame] | 297 | case JS_GlobalDataType::STRING: |
Dan Sinclair | 8f524d6 | 2017-10-25 13:30:31 -0400 | [diff] [blame] | 298 | return CJS_Return(pRuntime->NewString( |
Dan Sinclair | e497492 | 2017-10-24 09:36:16 -0400 | [diff] [blame] | 299 | WideString::FromLocal(pData->sData.c_str()).c_str())); |
Dan Sinclair | 8f524d6 | 2017-10-25 13:30:31 -0400 | [diff] [blame] | 300 | case JS_GlobalDataType::OBJECT: |
| 301 | return CJS_Return( |
| 302 | v8::Local<v8::Object>::New(pRuntime->GetIsolate(), pData->pData)); |
dan sinclair | cbe23db | 2017-10-19 14:29:33 -0400 | [diff] [blame] | 303 | case JS_GlobalDataType::NULLOBJ: |
Dan Sinclair | 8f524d6 | 2017-10-25 13:30:31 -0400 | [diff] [blame] | 304 | return CJS_Return(pRuntime->NewNull()); |
dan sinclair | cbe23db | 2017-10-19 14:29:33 -0400 | [diff] [blame] | 305 | default: |
| 306 | break; |
| 307 | } |
Dan Sinclair | 8f524d6 | 2017-10-25 13:30:31 -0400 | [diff] [blame] | 308 | return CJS_Return(false); |
dan sinclair | cbe23db | 2017-10-19 14:29:33 -0400 | [diff] [blame] | 309 | } |
| 310 | |
Dan Sinclair | 8f524d6 | 2017-10-25 13:30:31 -0400 | [diff] [blame] | 311 | CJS_Return JSGlobalAlternate::SetProperty(CJS_Runtime* pRuntime, |
| 312 | const wchar_t* propname, |
| 313 | v8::Local<v8::Value> vp) { |
dan sinclair | cbe23db | 2017-10-19 14:29:33 -0400 | [diff] [blame] | 314 | ByteString sPropName = ByteString::FromUnicode(propname); |
dan sinclair | 80435cb | 2017-10-24 21:40:24 -0400 | [diff] [blame] | 315 | if (vp->IsNumber()) { |
Dan Sinclair | 3cac360 | 2017-10-24 15:15:27 -0400 | [diff] [blame] | 316 | return SetGlobalVariables(sPropName, JS_GlobalDataType::NUMBER, |
dan sinclair | 80435cb | 2017-10-24 21:40:24 -0400 | [diff] [blame] | 317 | pRuntime->ToDouble(vp), false, "", |
Dan Sinclair | 3cac360 | 2017-10-24 15:15:27 -0400 | [diff] [blame] | 318 | v8::Local<v8::Object>(), false); |
| 319 | } |
dan sinclair | 80435cb | 2017-10-24 21:40:24 -0400 | [diff] [blame] | 320 | if (vp->IsBoolean()) { |
Dan Sinclair | 3cac360 | 2017-10-24 15:15:27 -0400 | [diff] [blame] | 321 | return SetGlobalVariables(sPropName, JS_GlobalDataType::BOOLEAN, 0, |
dan sinclair | 80435cb | 2017-10-24 21:40:24 -0400 | [diff] [blame] | 322 | pRuntime->ToBoolean(vp), "", |
Dan Sinclair | 3cac360 | 2017-10-24 15:15:27 -0400 | [diff] [blame] | 323 | v8::Local<v8::Object>(), false); |
| 324 | } |
dan sinclair | 80435cb | 2017-10-24 21:40:24 -0400 | [diff] [blame] | 325 | if (vp->IsString()) { |
Dan Sinclair | 3cac360 | 2017-10-24 15:15:27 -0400 | [diff] [blame] | 326 | return SetGlobalVariables( |
| 327 | sPropName, JS_GlobalDataType::STRING, 0, false, |
dan sinclair | 80435cb | 2017-10-24 21:40:24 -0400 | [diff] [blame] | 328 | ByteString::FromUnicode(pRuntime->ToWideString(vp)), |
Dan Sinclair | 3cac360 | 2017-10-24 15:15:27 -0400 | [diff] [blame] | 329 | v8::Local<v8::Object>(), false); |
| 330 | } |
dan sinclair | 80435cb | 2017-10-24 21:40:24 -0400 | [diff] [blame] | 331 | if (vp->IsObject()) { |
Dan Sinclair | 3cac360 | 2017-10-24 15:15:27 -0400 | [diff] [blame] | 332 | return SetGlobalVariables(sPropName, JS_GlobalDataType::OBJECT, 0, false, |
dan sinclair | 80435cb | 2017-10-24 21:40:24 -0400 | [diff] [blame] | 333 | "", pRuntime->ToObject(vp), false); |
Dan Sinclair | 3cac360 | 2017-10-24 15:15:27 -0400 | [diff] [blame] | 334 | } |
dan sinclair | 80435cb | 2017-10-24 21:40:24 -0400 | [diff] [blame] | 335 | if (vp->IsNull()) { |
Dan Sinclair | 3cac360 | 2017-10-24 15:15:27 -0400 | [diff] [blame] | 336 | return SetGlobalVariables(sPropName, JS_GlobalDataType::NULLOBJ, 0, false, |
| 337 | "", v8::Local<v8::Object>(), false); |
| 338 | } |
dan sinclair | 80435cb | 2017-10-24 21:40:24 -0400 | [diff] [blame] | 339 | if (vp->IsUndefined()) { |
Dan Sinclair | 3cac360 | 2017-10-24 15:15:27 -0400 | [diff] [blame] | 340 | DelProperty(pRuntime, propname); |
Dan Sinclair | 8f524d6 | 2017-10-25 13:30:31 -0400 | [diff] [blame] | 341 | return CJS_Return(true); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 342 | } |
Dan Sinclair | 8f524d6 | 2017-10-25 13:30:31 -0400 | [diff] [blame] | 343 | return CJS_Return(false); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 344 | } |
| 345 | |
Dan Sinclair | 8f524d6 | 2017-10-25 13:30:31 -0400 | [diff] [blame] | 346 | CJS_Return JSGlobalAlternate::setPersistent( |
dan sinclair | 80435cb | 2017-10-24 21:40:24 -0400 | [diff] [blame] | 347 | CJS_Runtime* pRuntime, |
Dan Sinclair | 8f524d6 | 2017-10-25 13:30:31 -0400 | [diff] [blame] | 348 | const std::vector<v8::Local<v8::Value>>& params) { |
| 349 | if (params.size() != 2) |
Dan Sinclair | d6e9cfa | 2017-10-30 21:19:42 +0000 | [diff] [blame] | 350 | return CJS_Return(JSGetStringFromID(JSMessage::kParamError)); |
Dan Sinclair | 8f524d6 | 2017-10-25 13:30:31 -0400 | [diff] [blame] | 351 | |
Dan Sinclair | 1b2a18e | 2017-10-24 13:56:29 -0400 | [diff] [blame] | 352 | auto it = m_MapGlobal.find( |
dan sinclair | 80435cb | 2017-10-24 21:40:24 -0400 | [diff] [blame] | 353 | ByteString::FromUnicode(pRuntime->ToWideString(params[0]))); |
Dan Sinclair | 8f524d6 | 2017-10-25 13:30:31 -0400 | [diff] [blame] | 354 | if (it == m_MapGlobal.end() || it->second->bDeleted) |
Dan Sinclair | d6e9cfa | 2017-10-30 21:19:42 +0000 | [diff] [blame] | 355 | return CJS_Return(JSGetStringFromID(JSMessage::kGlobalNotFoundError)); |
Dan Sinclair | 8f524d6 | 2017-10-25 13:30:31 -0400 | [diff] [blame] | 356 | |
dan sinclair | 80435cb | 2017-10-24 21:40:24 -0400 | [diff] [blame] | 357 | it->second->bPersistent = pRuntime->ToBoolean(params[1]); |
Dan Sinclair | 8f524d6 | 2017-10-25 13:30:31 -0400 | [diff] [blame] | 358 | return CJS_Return(true); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 359 | } |
| 360 | |
Tom Sepez | 7dc5cc1 | 2015-08-17 12:15:26 -0700 | [diff] [blame] | 361 | void JSGlobalAlternate::UpdateGlobalPersistentVariables() { |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 362 | CJS_Runtime* pRuntime = |
| 363 | static_cast<CJS_Runtime*>(CFXJS_Engine::CurrentEngineFromIsolate( |
| 364 | m_pJSObject->ToV8Object()->GetIsolate())); |
| 365 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 366 | for (int i = 0, sz = m_pGlobalData->GetSize(); i < sz; i++) { |
| 367 | CJS_GlobalData_Element* pData = m_pGlobalData->GetAt(i); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 368 | switch (pData->data.nType) { |
weili | 47228ac | 2016-07-20 10:35:31 -0700 | [diff] [blame] | 369 | case JS_GlobalDataType::NUMBER: |
| 370 | SetGlobalVariables(pData->data.sKey, JS_GlobalDataType::NUMBER, |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 371 | pData->data.dData, false, "", |
| 372 | v8::Local<v8::Object>(), pData->bPersistent == 1); |
tsepez | e6cf013 | 2017-01-18 14:38:18 -0800 | [diff] [blame] | 373 | pRuntime->PutObjectProperty(m_pJSObject->ToV8Object(), |
| 374 | pData->data.sKey.UTF8Decode(), |
| 375 | pRuntime->NewNumber(pData->data.dData)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 376 | break; |
weili | 47228ac | 2016-07-20 10:35:31 -0700 | [diff] [blame] | 377 | case JS_GlobalDataType::BOOLEAN: |
| 378 | SetGlobalVariables(pData->data.sKey, JS_GlobalDataType::BOOLEAN, 0, |
tsepez | e6cf013 | 2017-01-18 14:38:18 -0800 | [diff] [blame] | 379 | pData->data.bData == 1, "", v8::Local<v8::Object>(), |
| 380 | pData->bPersistent == 1); |
| 381 | pRuntime->PutObjectProperty( |
| 382 | m_pJSObject->ToV8Object(), pData->data.sKey.UTF8Decode(), |
| 383 | pRuntime->NewBoolean(pData->data.bData == 1)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 384 | break; |
weili | 47228ac | 2016-07-20 10:35:31 -0700 | [diff] [blame] | 385 | case JS_GlobalDataType::STRING: |
| 386 | SetGlobalVariables(pData->data.sKey, JS_GlobalDataType::STRING, 0, |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 387 | false, pData->data.sData, v8::Local<v8::Object>(), |
| 388 | pData->bPersistent == 1); |
tsepez | e6cf013 | 2017-01-18 14:38:18 -0800 | [diff] [blame] | 389 | pRuntime->PutObjectProperty( |
| 390 | m_pJSObject->ToV8Object(), pData->data.sKey.UTF8Decode(), |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 391 | pRuntime->NewString(pData->data.sData.UTF8Decode().AsStringView())); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 392 | break; |
weili | 47228ac | 2016-07-20 10:35:31 -0700 | [diff] [blame] | 393 | case JS_GlobalDataType::OBJECT: { |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 394 | v8::Local<v8::Object> pObj = pRuntime->NewFxDynamicObj(-1); |
Tom Sepez | 63b2fc7 | 2017-08-14 16:24:29 -0700 | [diff] [blame] | 395 | if (!pObj.IsEmpty()) { |
| 396 | PutObjectProperty(pObj, &pData->data); |
| 397 | SetGlobalVariables(pData->data.sKey, JS_GlobalDataType::OBJECT, 0, |
| 398 | false, "", pObj, pData->bPersistent == 1); |
| 399 | pRuntime->PutObjectProperty(m_pJSObject->ToV8Object(), |
| 400 | pData->data.sKey.UTF8Decode(), pObj); |
| 401 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 402 | } break; |
weili | 47228ac | 2016-07-20 10:35:31 -0700 | [diff] [blame] | 403 | case JS_GlobalDataType::NULLOBJ: |
| 404 | SetGlobalVariables(pData->data.sKey, JS_GlobalDataType::NULLOBJ, 0, |
| 405 | false, "", v8::Local<v8::Object>(), |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 406 | pData->bPersistent == 1); |
tsepez | e6cf013 | 2017-01-18 14:38:18 -0800 | [diff] [blame] | 407 | pRuntime->PutObjectProperty(m_pJSObject->ToV8Object(), |
| 408 | pData->data.sKey.UTF8Decode(), |
| 409 | pRuntime->NewNull()); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 410 | break; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 411 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 412 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 413 | } |
| 414 | |
Tom Sepez | b1670b5 | 2017-02-16 17:01:00 -0800 | [diff] [blame] | 415 | void JSGlobalAlternate::CommitGlobalPersisitentVariables( |
| 416 | CJS_Runtime* pRuntime) { |
Tom Sepez | a775723 | 2017-04-18 11:10:39 -0700 | [diff] [blame] | 417 | for (const auto& iter : m_MapGlobal) { |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 418 | ByteString name = iter.first; |
Tom Sepez | a775723 | 2017-04-18 11:10:39 -0700 | [diff] [blame] | 419 | JSGlobalData* pData = iter.second.get(); |
Tom Sepez | 09d33bc | 2015-08-19 09:49:24 -0700 | [diff] [blame] | 420 | if (pData->bDeleted) { |
| 421 | m_pGlobalData->DeleteGlobalVariable(name); |
Tom Sepez | a775723 | 2017-04-18 11:10:39 -0700 | [diff] [blame] | 422 | continue; |
| 423 | } |
| 424 | switch (pData->nType) { |
| 425 | case JS_GlobalDataType::NUMBER: |
| 426 | m_pGlobalData->SetGlobalVariableNumber(name, pData->dData); |
| 427 | m_pGlobalData->SetGlobalVariablePersistent(name, pData->bPersistent); |
| 428 | break; |
| 429 | case JS_GlobalDataType::BOOLEAN: |
| 430 | m_pGlobalData->SetGlobalVariableBoolean(name, pData->bData); |
| 431 | m_pGlobalData->SetGlobalVariablePersistent(name, pData->bPersistent); |
| 432 | break; |
| 433 | case JS_GlobalDataType::STRING: |
| 434 | m_pGlobalData->SetGlobalVariableString(name, pData->sData); |
| 435 | m_pGlobalData->SetGlobalVariablePersistent(name, pData->bPersistent); |
| 436 | break; |
| 437 | case JS_GlobalDataType::OBJECT: { |
| 438 | CJS_GlobalVariableArray array; |
| 439 | v8::Local<v8::Object> obj = v8::Local<v8::Object>::New( |
| 440 | GetJSObject()->GetIsolate(), pData->pData); |
| 441 | ObjectToArray(pRuntime, obj, array); |
| 442 | m_pGlobalData->SetGlobalVariableObject(name, array); |
| 443 | m_pGlobalData->SetGlobalVariablePersistent(name, pData->bPersistent); |
| 444 | } break; |
| 445 | case JS_GlobalDataType::NULLOBJ: |
| 446 | m_pGlobalData->SetGlobalVariableNull(name); |
| 447 | m_pGlobalData->SetGlobalVariablePersistent(name, pData->bPersistent); |
| 448 | break; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 449 | } |
| 450 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 451 | } |
| 452 | |
Tom Sepez | b1670b5 | 2017-02-16 17:01:00 -0800 | [diff] [blame] | 453 | void JSGlobalAlternate::ObjectToArray(CJS_Runtime* pRuntime, |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 454 | v8::Local<v8::Object> pObj, |
Tom Sepez | 7dc5cc1 | 2015-08-17 12:15:26 -0700 | [diff] [blame] | 455 | CJS_GlobalVariableArray& array) { |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 456 | std::vector<WideString> pKeyList = pRuntime->GetObjectPropertyNames(pObj); |
tsepez | d0b6ed1 | 2016-08-11 19:50:57 -0700 | [diff] [blame] | 457 | for (const auto& ws : pKeyList) { |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 458 | ByteString sKey = ws.UTF8Encode(); |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 459 | v8::Local<v8::Value> v = pRuntime->GetObjectProperty(pObj, ws); |
Dan Sinclair | 3cac360 | 2017-10-24 15:15:27 -0400 | [diff] [blame] | 460 | if (v->IsNumber()) { |
| 461 | CJS_KeyValue* pObjElement = new CJS_KeyValue; |
| 462 | pObjElement->nType = JS_GlobalDataType::NUMBER; |
| 463 | pObjElement->sKey = sKey; |
| 464 | pObjElement->dData = pRuntime->ToDouble(v); |
| 465 | array.Add(pObjElement); |
| 466 | continue; |
| 467 | } |
| 468 | if (v->IsBoolean()) { |
| 469 | CJS_KeyValue* pObjElement = new CJS_KeyValue; |
| 470 | pObjElement->nType = JS_GlobalDataType::BOOLEAN; |
| 471 | pObjElement->sKey = sKey; |
| 472 | pObjElement->dData = pRuntime->ToBoolean(v); |
| 473 | array.Add(pObjElement); |
| 474 | continue; |
| 475 | } |
| 476 | if (v->IsString()) { |
| 477 | ByteString sValue = ByteString::FromUnicode(pRuntime->ToWideString(v)); |
| 478 | CJS_KeyValue* pObjElement = new CJS_KeyValue; |
| 479 | pObjElement->nType = JS_GlobalDataType::STRING; |
| 480 | pObjElement->sKey = sKey; |
| 481 | pObjElement->sData = sValue; |
| 482 | array.Add(pObjElement); |
| 483 | continue; |
| 484 | } |
| 485 | if (v->IsObject()) { |
| 486 | CJS_KeyValue* pObjElement = new CJS_KeyValue; |
| 487 | pObjElement->nType = JS_GlobalDataType::OBJECT; |
| 488 | pObjElement->sKey = sKey; |
| 489 | ObjectToArray(pRuntime, pRuntime->ToObject(v), pObjElement->objData); |
| 490 | array.Add(pObjElement); |
| 491 | continue; |
| 492 | } |
| 493 | if (v->IsNull()) { |
| 494 | CJS_KeyValue* pObjElement = new CJS_KeyValue; |
| 495 | pObjElement->nType = JS_GlobalDataType::NULLOBJ; |
| 496 | pObjElement->sKey = sKey; |
| 497 | array.Add(pObjElement); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 498 | } |
| 499 | } |
| 500 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 501 | |
Tom Sepez | 7dc5cc1 | 2015-08-17 12:15:26 -0700 | [diff] [blame] | 502 | void JSGlobalAlternate::PutObjectProperty(v8::Local<v8::Object> pObj, |
| 503 | CJS_KeyValue* pData) { |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 504 | CJS_Runtime* pRuntime = CJS_Runtime::CurrentRuntimeFromIsolate( |
| 505 | m_pJSObject->ToV8Object()->GetIsolate()); |
| 506 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 507 | for (int i = 0, sz = pData->objData.Count(); i < sz; i++) { |
| 508 | CJS_KeyValue* pObjData = pData->objData.GetAt(i); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 509 | switch (pObjData->nType) { |
weili | 47228ac | 2016-07-20 10:35:31 -0700 | [diff] [blame] | 510 | case JS_GlobalDataType::NUMBER: |
tsepez | e6cf013 | 2017-01-18 14:38:18 -0800 | [diff] [blame] | 511 | pRuntime->PutObjectProperty(pObj, pObjData->sKey.UTF8Decode(), |
| 512 | pRuntime->NewNumber(pObjData->dData)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 513 | break; |
weili | 47228ac | 2016-07-20 10:35:31 -0700 | [diff] [blame] | 514 | case JS_GlobalDataType::BOOLEAN: |
tsepez | e6cf013 | 2017-01-18 14:38:18 -0800 | [diff] [blame] | 515 | pRuntime->PutObjectProperty(pObj, pObjData->sKey.UTF8Decode(), |
| 516 | pRuntime->NewBoolean(pObjData->bData == 1)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 517 | break; |
weili | 47228ac | 2016-07-20 10:35:31 -0700 | [diff] [blame] | 518 | case JS_GlobalDataType::STRING: |
tsepez | e6cf013 | 2017-01-18 14:38:18 -0800 | [diff] [blame] | 519 | pRuntime->PutObjectProperty( |
| 520 | pObj, pObjData->sKey.UTF8Decode(), |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 521 | pRuntime->NewString(pObjData->sData.UTF8Decode().AsStringView())); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 522 | break; |
weili | 47228ac | 2016-07-20 10:35:31 -0700 | [diff] [blame] | 523 | case JS_GlobalDataType::OBJECT: { |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 524 | v8::Local<v8::Object> pNewObj = pRuntime->NewFxDynamicObj(-1); |
Tom Sepez | 63b2fc7 | 2017-08-14 16:24:29 -0700 | [diff] [blame] | 525 | if (!pNewObj.IsEmpty()) { |
| 526 | PutObjectProperty(pNewObj, pObjData); |
| 527 | pRuntime->PutObjectProperty(pObj, pObjData->sKey.UTF8Decode(), |
| 528 | pNewObj); |
| 529 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 530 | } break; |
weili | 47228ac | 2016-07-20 10:35:31 -0700 | [diff] [blame] | 531 | case JS_GlobalDataType::NULLOBJ: |
tsepez | e6cf013 | 2017-01-18 14:38:18 -0800 | [diff] [blame] | 532 | pRuntime->PutObjectProperty(pObj, pObjData->sKey.UTF8Decode(), |
| 533 | pRuntime->NewNull()); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 534 | break; |
| 535 | } |
| 536 | } |
| 537 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 538 | |
Tom Sepez | 7dc5cc1 | 2015-08-17 12:15:26 -0700 | [diff] [blame] | 539 | void JSGlobalAlternate::DestroyGlobalPersisitentVariables() { |
Tom Sepez | a775723 | 2017-04-18 11:10:39 -0700 | [diff] [blame] | 540 | m_MapGlobal.clear(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 541 | } |
| 542 | |
Dan Sinclair | 8f524d6 | 2017-10-25 13:30:31 -0400 | [diff] [blame] | 543 | CJS_Return JSGlobalAlternate::SetGlobalVariables(const ByteString& propname, |
| 544 | JS_GlobalDataType nType, |
| 545 | double dData, |
| 546 | bool bData, |
| 547 | const ByteString& sData, |
| 548 | v8::Local<v8::Object> pData, |
| 549 | bool bDefaultPersistent) { |
tsepez | b4c9f3f | 2016-04-13 15:41:21 -0700 | [diff] [blame] | 550 | if (propname.IsEmpty()) |
Dan Sinclair | 8f524d6 | 2017-10-25 13:30:31 -0400 | [diff] [blame] | 551 | return CJS_Return(false); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 552 | |
Tom Sepez | a775723 | 2017-04-18 11:10:39 -0700 | [diff] [blame] | 553 | auto it = m_MapGlobal.find(propname); |
| 554 | if (it != m_MapGlobal.end()) { |
| 555 | JSGlobalData* pTemp = it->second.get(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 556 | if (pTemp->bDeleted || pTemp->nType != nType) { |
| 557 | pTemp->dData = 0; |
| 558 | pTemp->bData = 0; |
Lei Zhang | fe2cd4d | 2017-11-22 20:04:12 +0000 | [diff] [blame] | 559 | pTemp->sData.clear(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 560 | pTemp->nType = nType; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 561 | } |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 562 | pTemp->bDeleted = false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 563 | switch (nType) { |
Tom Sepez | a775723 | 2017-04-18 11:10:39 -0700 | [diff] [blame] | 564 | case JS_GlobalDataType::NUMBER: |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 565 | pTemp->dData = dData; |
Tom Sepez | a775723 | 2017-04-18 11:10:39 -0700 | [diff] [blame] | 566 | break; |
| 567 | case JS_GlobalDataType::BOOLEAN: |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 568 | pTemp->bData = bData; |
Tom Sepez | a775723 | 2017-04-18 11:10:39 -0700 | [diff] [blame] | 569 | break; |
| 570 | case JS_GlobalDataType::STRING: |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 571 | pTemp->sData = sData; |
Tom Sepez | a775723 | 2017-04-18 11:10:39 -0700 | [diff] [blame] | 572 | break; |
| 573 | case JS_GlobalDataType::OBJECT: |
Tom Sepez | 371379d | 2015-11-06 08:29:39 -0800 | [diff] [blame] | 574 | pTemp->pData.Reset(pData->GetIsolate(), pData); |
Tom Sepez | a775723 | 2017-04-18 11:10:39 -0700 | [diff] [blame] | 575 | break; |
weili | 47228ac | 2016-07-20 10:35:31 -0700 | [diff] [blame] | 576 | case JS_GlobalDataType::NULLOBJ: |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 577 | break; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 578 | default: |
Dan Sinclair | 8f524d6 | 2017-10-25 13:30:31 -0400 | [diff] [blame] | 579 | return CJS_Return(false); |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 580 | } |
Dan Sinclair | 8f524d6 | 2017-10-25 13:30:31 -0400 | [diff] [blame] | 581 | return CJS_Return(true); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 582 | } |
| 583 | |
Tom Sepez | a775723 | 2017-04-18 11:10:39 -0700 | [diff] [blame] | 584 | auto pNewData = pdfium::MakeUnique<JSGlobalData>(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 585 | switch (nType) { |
Tom Sepez | a775723 | 2017-04-18 11:10:39 -0700 | [diff] [blame] | 586 | case JS_GlobalDataType::NUMBER: |
weili | 47228ac | 2016-07-20 10:35:31 -0700 | [diff] [blame] | 587 | pNewData->nType = JS_GlobalDataType::NUMBER; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 588 | pNewData->dData = dData; |
| 589 | pNewData->bPersistent = bDefaultPersistent; |
Tom Sepez | a775723 | 2017-04-18 11:10:39 -0700 | [diff] [blame] | 590 | break; |
| 591 | case JS_GlobalDataType::BOOLEAN: |
weili | 47228ac | 2016-07-20 10:35:31 -0700 | [diff] [blame] | 592 | pNewData->nType = JS_GlobalDataType::BOOLEAN; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 593 | pNewData->bData = bData; |
| 594 | pNewData->bPersistent = bDefaultPersistent; |
Tom Sepez | a775723 | 2017-04-18 11:10:39 -0700 | [diff] [blame] | 595 | break; |
| 596 | case JS_GlobalDataType::STRING: |
weili | 47228ac | 2016-07-20 10:35:31 -0700 | [diff] [blame] | 597 | pNewData->nType = JS_GlobalDataType::STRING; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 598 | pNewData->sData = sData; |
| 599 | pNewData->bPersistent = bDefaultPersistent; |
Tom Sepez | a775723 | 2017-04-18 11:10:39 -0700 | [diff] [blame] | 600 | break; |
| 601 | case JS_GlobalDataType::OBJECT: |
weili | 47228ac | 2016-07-20 10:35:31 -0700 | [diff] [blame] | 602 | pNewData->nType = JS_GlobalDataType::OBJECT; |
Tom Sepez | 371379d | 2015-11-06 08:29:39 -0800 | [diff] [blame] | 603 | pNewData->pData.Reset(pData->GetIsolate(), pData); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 604 | pNewData->bPersistent = bDefaultPersistent; |
Tom Sepez | a775723 | 2017-04-18 11:10:39 -0700 | [diff] [blame] | 605 | break; |
| 606 | case JS_GlobalDataType::NULLOBJ: |
weili | 47228ac | 2016-07-20 10:35:31 -0700 | [diff] [blame] | 607 | pNewData->nType = JS_GlobalDataType::NULLOBJ; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 608 | pNewData->bPersistent = bDefaultPersistent; |
Tom Sepez | a775723 | 2017-04-18 11:10:39 -0700 | [diff] [blame] | 609 | break; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 610 | default: |
Dan Sinclair | 8f524d6 | 2017-10-25 13:30:31 -0400 | [diff] [blame] | 611 | return CJS_Return(false); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 612 | } |
Tom Sepez | a775723 | 2017-04-18 11:10:39 -0700 | [diff] [blame] | 613 | m_MapGlobal[propname] = std::move(pNewData); |
Dan Sinclair | 8f524d6 | 2017-10-25 13:30:31 -0400 | [diff] [blame] | 614 | return CJS_Return(true); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 615 | } |