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