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