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