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