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" |
Tom Sepez | 41d04e1 | 2018-10-30 22:07:36 +0000 | [diff] [blame] | 15 | #include "fxjs/cfx_globaldata.h" |
| 16 | #include "fxjs/cfx_keyvalue.h" |
Dan Sinclair | e0345a4 | 2017-10-30 20:20:42 +0000 | [diff] [blame] | 17 | #include "fxjs/cjs_event_context.h" |
Tom Sepez | 7dbb85b | 2019-06-11 19:49:00 +0000 | [diff] [blame] | 18 | #include "fxjs/cjs_eventrecorder.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" |
Lei Zhang | 99f5bbb | 2018-10-09 21:31:28 +0000 | [diff] [blame] | 22 | #include "third_party/base/ptr_util.h" |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 23 | |
Dan Sinclair | 4b172c4 | 2017-10-23 11:22:31 -0400 | [diff] [blame] | 24 | namespace { |
| 25 | |
Adam Klein | 4c451ba | 2018-01-17 21:06:27 +0000 | [diff] [blame] | 26 | WideString PropFromV8Prop(v8::Isolate* pIsolate, |
| 27 | v8::Local<v8::String> property) { |
| 28 | v8::String::Utf8Value utf8_value(pIsolate, property); |
Dan Sinclair | 8f524d6 | 2017-10-25 13:30:31 -0400 | [diff] [blame] | 29 | return WideString::FromUTF8(ByteStringView(*utf8_value, utf8_value.length())); |
| 30 | } |
| 31 | |
Dan Sinclair | 4b172c4 | 2017-10-23 11:22:31 -0400 | [diff] [blame] | 32 | template <class Alt> |
| 33 | void JSSpecialPropQuery(const char*, |
| 34 | v8::Local<v8::String> property, |
| 35 | const v8::PropertyCallbackInfo<v8::Integer>& info) { |
Tom Sepez | 82999fa | 2018-07-16 22:17:46 +0000 | [diff] [blame] | 36 | auto pObj = JSGetObject<Alt>(info.Holder()); |
Lei Zhang | 6aa2190 | 2018-07-12 13:15:11 +0000 | [diff] [blame] | 37 | if (!pObj) |
Dan Sinclair | 4b172c4 | 2017-10-23 11:22:31 -0400 | [diff] [blame] | 38 | return; |
| 39 | |
Lei Zhang | 6aa2190 | 2018-07-12 13:15:11 +0000 | [diff] [blame] | 40 | CJS_Runtime* pRuntime = pObj->GetRuntime(); |
Tom Sepez | ddaa40f | 2018-06-06 18:30:15 +0000 | [diff] [blame] | 41 | if (!pRuntime) |
| 42 | return; |
| 43 | |
Tom Sepez | 3a6d058 | 2018-08-17 19:28:52 +0000 | [diff] [blame] | 44 | CJS_Result result = |
Lei Zhang | 6aa2190 | 2018-07-12 13:15:11 +0000 | [diff] [blame] | 45 | pObj->QueryProperty(PropFromV8Prop(info.GetIsolate(), property).c_str()); |
Tom Sepez | ddaa40f | 2018-06-06 18:30:15 +0000 | [diff] [blame] | 46 | |
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) { |
Tom Sepez | 82999fa | 2018-07-16 22:17:46 +0000 | [diff] [blame] | 54 | auto pObj = JSGetObject<Alt>(info.Holder()); |
Lei Zhang | 6aa2190 | 2018-07-12 13:15:11 +0000 | [diff] [blame] | 55 | if (!pObj) |
Dan Sinclair | 4b172c4 | 2017-10-23 11:22:31 -0400 | [diff] [blame] | 56 | return; |
| 57 | |
Lei Zhang | 6aa2190 | 2018-07-12 13:15:11 +0000 | [diff] [blame] | 58 | CJS_Runtime* pRuntime = pObj->GetRuntime(); |
Tom Sepez | ddaa40f | 2018-06-06 18:30:15 +0000 | [diff] [blame] | 59 | if (!pRuntime) |
| 60 | return; |
| 61 | |
Tom Sepez | 3a6d058 | 2018-08-17 19:28:52 +0000 | [diff] [blame] | 62 | CJS_Result result = pObj->GetProperty( |
Adam Klein | 4c451ba | 2018-01-17 21:06:27 +0000 | [diff] [blame] | 63 | pRuntime, PropFromV8Prop(info.GetIsolate(), property).c_str()); |
Tom Sepez | ddaa40f | 2018-06-06 18:30:15 +0000 | [diff] [blame] | 64 | |
Dan Sinclair | 8f524d6 | 2017-10-25 13:30:31 -0400 | [diff] [blame] | 65 | if (result.HasError()) { |
| 66 | pRuntime->Error( |
| 67 | JSFormatErrorString(class_name, "GetProperty", result.Error())); |
Dan Sinclair | 4b172c4 | 2017-10-23 11:22:31 -0400 | [diff] [blame] | 68 | return; |
| 69 | } |
Dan Sinclair | 8f524d6 | 2017-10-25 13:30:31 -0400 | [diff] [blame] | 70 | if (result.HasReturn()) |
| 71 | info.GetReturnValue().Set(result.Return()); |
Dan Sinclair | 4b172c4 | 2017-10-23 11:22:31 -0400 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | template <class Alt> |
| 75 | void JSSpecialPropPut(const char* class_name, |
| 76 | v8::Local<v8::String> property, |
| 77 | v8::Local<v8::Value> value, |
| 78 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
Tom Sepez | 82999fa | 2018-07-16 22:17:46 +0000 | [diff] [blame] | 79 | auto pObj = JSGetObject<Alt>(info.Holder()); |
Lei Zhang | 6aa2190 | 2018-07-12 13:15:11 +0000 | [diff] [blame] | 80 | if (!pObj) |
Dan Sinclair | 4b172c4 | 2017-10-23 11:22:31 -0400 | [diff] [blame] | 81 | return; |
| 82 | |
Lei Zhang | 6aa2190 | 2018-07-12 13:15:11 +0000 | [diff] [blame] | 83 | CJS_Runtime* pRuntime = pObj->GetRuntime(); |
Tom Sepez | ddaa40f | 2018-06-06 18:30:15 +0000 | [diff] [blame] | 84 | if (!pRuntime) |
| 85 | return; |
| 86 | |
Tom Sepez | 3a6d058 | 2018-08-17 19:28:52 +0000 | [diff] [blame] | 87 | CJS_Result result = pObj->SetProperty( |
Adam Klein | 4c451ba | 2018-01-17 21:06:27 +0000 | [diff] [blame] | 88 | pRuntime, PropFromV8Prop(info.GetIsolate(), property).c_str(), value); |
Tom Sepez | ddaa40f | 2018-06-06 18:30:15 +0000 | [diff] [blame] | 89 | |
Dan Sinclair | 8f524d6 | 2017-10-25 13:30:31 -0400 | [diff] [blame] | 90 | if (result.HasError()) { |
| 91 | pRuntime->Error( |
| 92 | JSFormatErrorString(class_name, "PutProperty", result.Error())); |
| 93 | } |
Dan Sinclair | 4b172c4 | 2017-10-23 11:22:31 -0400 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | template <class Alt> |
| 97 | void JSSpecialPropDel(const char* class_name, |
| 98 | v8::Local<v8::String> property, |
| 99 | const v8::PropertyCallbackInfo<v8::Boolean>& info) { |
Tom Sepez | 82999fa | 2018-07-16 22:17:46 +0000 | [diff] [blame] | 100 | auto pObj = JSGetObject<Alt>(info.Holder()); |
Lei Zhang | 6aa2190 | 2018-07-12 13:15:11 +0000 | [diff] [blame] | 101 | if (!pObj) |
Dan Sinclair | 4b172c4 | 2017-10-23 11:22:31 -0400 | [diff] [blame] | 102 | return; |
| 103 | |
Lei Zhang | 6aa2190 | 2018-07-12 13:15:11 +0000 | [diff] [blame] | 104 | CJS_Runtime* pRuntime = pObj->GetRuntime(); |
Tom Sepez | ddaa40f | 2018-06-06 18:30:15 +0000 | [diff] [blame] | 105 | if (!pRuntime) |
| 106 | return; |
| 107 | |
Tom Sepez | 3a6d058 | 2018-08-17 19:28:52 +0000 | [diff] [blame] | 108 | CJS_Result result = pObj->DelProperty( |
Adam Klein | 4c451ba | 2018-01-17 21:06:27 +0000 | [diff] [blame] | 109 | pRuntime, PropFromV8Prop(info.GetIsolate(), property).c_str()); |
Dan Sinclair | 8f524d6 | 2017-10-25 13:30:31 -0400 | [diff] [blame] | 110 | if (result.HasError()) { |
| 111 | // TODO(dsinclair): Should this set the pRuntime->Error result? |
Henrique Nakashima | 1150630 | 2017-12-06 22:09:40 +0000 | [diff] [blame] | 112 | // ByteString cbName = |
| 113 | // ByteString::Format("%s.%s", class_name, "DelProperty"); |
Dan Sinclair | 4b172c4 | 2017-10-23 11:22:31 -0400 | [diff] [blame] | 114 | } |
| 115 | } |
| 116 | |
Lei Zhang | 5b34656 | 2018-08-28 23:36:08 +0000 | [diff] [blame] | 117 | template <class T> |
| 118 | v8::Local<v8::String> GetV8StringFromProperty(v8::Local<v8::Name> property, |
| 119 | const T& info) { |
| 120 | return property->ToString(info.GetIsolate()->GetCurrentContext()) |
| 121 | .ToLocalChecked(); |
| 122 | } |
| 123 | |
Dan Sinclair | 4b172c4 | 2017-10-23 11:22:31 -0400 | [diff] [blame] | 124 | } // namespace |
| 125 | |
Tom Sepez | 4a463d5 | 2018-11-07 22:32:41 +0000 | [diff] [blame] | 126 | CJS_Global::JSGlobalData::JSGlobalData() = default; |
Dan Sinclair | f743552 | 2018-02-05 22:27:22 +0000 | [diff] [blame] | 127 | |
Tom Sepez | 4a463d5 | 2018-11-07 22:32:41 +0000 | [diff] [blame] | 128 | CJS_Global::JSGlobalData::~JSGlobalData() = default; |
Dan Sinclair | f743552 | 2018-02-05 22:27:22 +0000 | [diff] [blame] | 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(), |
Lei Zhang | 5b34656 | 2018-08-28 23:36:08 +0000 | [diff] [blame] | 150 | GetV8StringFromProperty(property, info)), |
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(), |
Lei Zhang | 5b34656 | 2018-08-28 23:36:08 +0000 | [diff] [blame] | 162 | GetV8StringFromProperty(property, info)), |
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(), |
Lei Zhang | 5b34656 | 2018-08-28 23:36:08 +0000 | [diff] [blame] | 175 | GetV8StringFromProperty(property, info)), |
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(), |
Lei Zhang | 5b34656 | 2018-08-28 23:36:08 +0000 | [diff] [blame] | 187 | GetV8StringFromProperty(property, info)), |
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) { |
Lei Zhang | 5b34656 | 2018-08-28 23:36:08 +0000 | [diff] [blame] | 213 | CPDFSDK_FormFillEnvironment* pFormFillEnv = GetRuntime()->GetFormFillEnv(); |
| 214 | m_pFormFillEnv.Reset(pFormFillEnv); |
Tom Sepez | eccfe0e | 2018-11-01 16:34:52 +0000 | [diff] [blame] | 215 | m_pGlobalData = CFX_GlobalData::GetRetainedInstance(nullptr); |
Lei Zhang | 5b34656 | 2018-08-28 23:36:08 +0000 | [diff] [blame] | 216 | UpdateGlobalPersistentVariables(); |
Tom Sepez | 83b259e | 2018-06-08 20:43:55 +0000 | [diff] [blame] | 217 | } |
Dan Sinclair | 998fee3 | 2018-02-05 21:43:19 +0000 | [diff] [blame] | 218 | |
Dan Sinclair | f743552 | 2018-02-05 22:27:22 +0000 | [diff] [blame] | 219 | CJS_Global::~CJS_Global() { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 220 | DestroyGlobalPersisitentVariables(); |
Tom Sepez | f458362 | 2015-09-14 15:06:53 -0700 | [diff] [blame] | 221 | m_pGlobalData->Release(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Tom Sepez | 3a6d058 | 2018-08-17 19:28:52 +0000 | [diff] [blame] | 224 | CJS_Result CJS_Global::QueryProperty(const wchar_t* propname) { |
Tom Sepez | 87ac6dc | 2018-11-30 23:10:17 +0000 | [diff] [blame] | 225 | if (WideString(propname).EqualsASCII("setPersistent")) |
| 226 | return CJS_Result::Success(); |
| 227 | |
| 228 | return CJS_Result::Failure(JSMessage::kUnknownProperty); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Tom Sepez | 3a6d058 | 2018-08-17 19:28:52 +0000 | [diff] [blame] | 231 | CJS_Result CJS_Global::DelProperty(CJS_Runtime* pRuntime, |
Dan Sinclair | f743552 | 2018-02-05 22:27:22 +0000 | [diff] [blame] | 232 | const wchar_t* propname) { |
Tom Sepez | 34dab07 | 2018-08-08 17:49:02 +0000 | [diff] [blame] | 233 | auto it = m_MapGlobal.find(WideString(propname).ToDefANSI()); |
Tom Sepez | a775723 | 2017-04-18 11:10:39 -0700 | [diff] [blame] | 234 | if (it == m_MapGlobal.end()) |
Tom Sepez | 3a6d058 | 2018-08-17 19:28:52 +0000 | [diff] [blame] | 235 | return CJS_Result::Failure(JSMessage::kUnknownProperty); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 236 | |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 237 | it->second->bDeleted = true; |
Tom Sepez | 3a6d058 | 2018-08-17 19:28:52 +0000 | [diff] [blame] | 238 | return CJS_Result::Success(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Tom Sepez | 3a6d058 | 2018-08-17 19:28:52 +0000 | [diff] [blame] | 241 | CJS_Result CJS_Global::GetProperty(CJS_Runtime* pRuntime, |
Dan Sinclair | f743552 | 2018-02-05 22:27:22 +0000 | [diff] [blame] | 242 | const wchar_t* propname) { |
Tom Sepez | 34dab07 | 2018-08-08 17:49:02 +0000 | [diff] [blame] | 243 | auto it = m_MapGlobal.find(WideString(propname).ToDefANSI()); |
dan sinclair | 5daf07a | 2017-10-24 21:46:57 -0400 | [diff] [blame] | 244 | if (it == m_MapGlobal.end()) |
Tom Sepez | 3a6d058 | 2018-08-17 19:28:52 +0000 | [diff] [blame] | 245 | return CJS_Result::Success(); |
dan sinclair | cbe23db | 2017-10-19 14:29:33 -0400 | [diff] [blame] | 246 | |
| 247 | JSGlobalData* pData = it->second.get(); |
dan sinclair | 5daf07a | 2017-10-24 21:46:57 -0400 | [diff] [blame] | 248 | if (pData->bDeleted) |
Tom Sepez | 3a6d058 | 2018-08-17 19:28:52 +0000 | [diff] [blame] | 249 | return CJS_Result::Success(); |
dan sinclair | cbe23db | 2017-10-19 14:29:33 -0400 | [diff] [blame] | 250 | |
| 251 | switch (pData->nType) { |
Tom Sepez | 4a463d5 | 2018-11-07 22:32:41 +0000 | [diff] [blame] | 252 | case CFX_Value::DataType::NUMBER: |
Tom Sepez | 3a6d058 | 2018-08-17 19:28:52 +0000 | [diff] [blame] | 253 | return CJS_Result::Success(pRuntime->NewNumber(pData->dData)); |
Tom Sepez | 4a463d5 | 2018-11-07 22:32:41 +0000 | [diff] [blame] | 254 | case CFX_Value::DataType::BOOLEAN: |
Tom Sepez | 3a6d058 | 2018-08-17 19:28:52 +0000 | [diff] [blame] | 255 | return CJS_Result::Success(pRuntime->NewBoolean(pData->bData)); |
Tom Sepez | 4a463d5 | 2018-11-07 22:32:41 +0000 | [diff] [blame] | 256 | case CFX_Value::DataType::STRING: |
Tom Sepez | 3a6d058 | 2018-08-17 19:28:52 +0000 | [diff] [blame] | 257 | return CJS_Result::Success(pRuntime->NewString( |
Tom Sepez | 4031714 | 2018-11-27 00:18:33 +0000 | [diff] [blame] | 258 | WideString::FromDefANSI(pData->sData.AsStringView()).AsStringView())); |
Tom Sepez | 4a463d5 | 2018-11-07 22:32:41 +0000 | [diff] [blame] | 259 | case CFX_Value::DataType::OBJECT: |
Tom Sepez | 3a6d058 | 2018-08-17 19:28:52 +0000 | [diff] [blame] | 260 | return CJS_Result::Success( |
Dan Sinclair | 8f524d6 | 2017-10-25 13:30:31 -0400 | [diff] [blame] | 261 | v8::Local<v8::Object>::New(pRuntime->GetIsolate(), pData->pData)); |
Tom Sepez | 4a463d5 | 2018-11-07 22:32:41 +0000 | [diff] [blame] | 262 | case CFX_Value::DataType::NULLOBJ: |
Tom Sepez | 3a6d058 | 2018-08-17 19:28:52 +0000 | [diff] [blame] | 263 | return CJS_Result::Success(pRuntime->NewNull()); |
dan sinclair | cbe23db | 2017-10-19 14:29:33 -0400 | [diff] [blame] | 264 | default: |
| 265 | break; |
| 266 | } |
Tom Sepez | 3a6d058 | 2018-08-17 19:28:52 +0000 | [diff] [blame] | 267 | return CJS_Result::Failure(JSMessage::kObjectTypeError); |
dan sinclair | cbe23db | 2017-10-19 14:29:33 -0400 | [diff] [blame] | 268 | } |
| 269 | |
Tom Sepez | 3a6d058 | 2018-08-17 19:28:52 +0000 | [diff] [blame] | 270 | CJS_Result CJS_Global::SetProperty(CJS_Runtime* pRuntime, |
Dan Sinclair | f743552 | 2018-02-05 22:27:22 +0000 | [diff] [blame] | 271 | const wchar_t* propname, |
| 272 | v8::Local<v8::Value> vp) { |
Tom Sepez | 34dab07 | 2018-08-08 17:49:02 +0000 | [diff] [blame] | 273 | ByteString sPropName = WideString(propname).ToDefANSI(); |
dan sinclair | 80435cb | 2017-10-24 21:40:24 -0400 | [diff] [blame] | 274 | if (vp->IsNumber()) { |
Tom Sepez | 4a463d5 | 2018-11-07 22:32:41 +0000 | [diff] [blame] | 275 | return SetGlobalVariables(sPropName, CFX_Value::DataType::NUMBER, |
Tom Sepez | 306ebab | 2018-12-03 22:12:55 +0000 | [diff] [blame] | 276 | pRuntime->ToDouble(vp), false, ByteString(), |
Dan Sinclair | 3cac360 | 2017-10-24 15:15:27 -0400 | [diff] [blame] | 277 | v8::Local<v8::Object>(), false); |
| 278 | } |
dan sinclair | 80435cb | 2017-10-24 21:40:24 -0400 | [diff] [blame] | 279 | if (vp->IsBoolean()) { |
Tom Sepez | 4a463d5 | 2018-11-07 22:32:41 +0000 | [diff] [blame] | 280 | return SetGlobalVariables(sPropName, CFX_Value::DataType::BOOLEAN, 0, |
Tom Sepez | 306ebab | 2018-12-03 22:12:55 +0000 | [diff] [blame] | 281 | pRuntime->ToBoolean(vp), ByteString(), |
Dan Sinclair | 3cac360 | 2017-10-24 15:15:27 -0400 | [diff] [blame] | 282 | v8::Local<v8::Object>(), false); |
| 283 | } |
dan sinclair | 80435cb | 2017-10-24 21:40:24 -0400 | [diff] [blame] | 284 | if (vp->IsString()) { |
Tom Sepez | 4a463d5 | 2018-11-07 22:32:41 +0000 | [diff] [blame] | 285 | return SetGlobalVariables(sPropName, CFX_Value::DataType::STRING, 0, false, |
| 286 | pRuntime->ToWideString(vp).ToDefANSI(), |
Tom Sepez | 34dab07 | 2018-08-08 17:49:02 +0000 | [diff] [blame] | 287 | v8::Local<v8::Object>(), false); |
Dan Sinclair | 3cac360 | 2017-10-24 15:15:27 -0400 | [diff] [blame] | 288 | } |
dan sinclair | 80435cb | 2017-10-24 21:40:24 -0400 | [diff] [blame] | 289 | if (vp->IsObject()) { |
Tom Sepez | 4a463d5 | 2018-11-07 22:32:41 +0000 | [diff] [blame] | 290 | return SetGlobalVariables(sPropName, CFX_Value::DataType::OBJECT, 0, false, |
Tom Sepez | 306ebab | 2018-12-03 22:12:55 +0000 | [diff] [blame] | 291 | ByteString(), 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()) { |
Tom Sepez | 4a463d5 | 2018-11-07 22:32:41 +0000 | [diff] [blame] | 294 | return SetGlobalVariables(sPropName, CFX_Value::DataType::NULLOBJ, 0, false, |
Tom Sepez | 306ebab | 2018-12-03 22:12:55 +0000 | [diff] [blame] | 295 | ByteString(), v8::Local<v8::Object>(), false); |
Dan Sinclair | 3cac360 | 2017-10-24 15:15:27 -0400 | [diff] [blame] | 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); |
Tom Sepez | 3a6d058 | 2018-08-17 19:28:52 +0000 | [diff] [blame] | 299 | return CJS_Result::Success(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 300 | } |
Tom Sepez | 3a6d058 | 2018-08-17 19:28:52 +0000 | [diff] [blame] | 301 | return CJS_Result::Failure(JSMessage::kObjectTypeError); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Tom Sepez | 3a6d058 | 2018-08-17 19:28:52 +0000 | [diff] [blame] | 304 | CJS_Result 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) |
Tom Sepez | 3a6d058 | 2018-08-17 19:28:52 +0000 | [diff] [blame] | 308 | return CJS_Result::Failure(JSMessage::kParamError); |
Dan Sinclair | 8f524d6 | 2017-10-25 13:30:31 -0400 | [diff] [blame] | 309 | |
Tom Sepez | 34dab07 | 2018-08-08 17:49:02 +0000 | [diff] [blame] | 310 | auto it = m_MapGlobal.find(pRuntime->ToWideString(params[0]).ToDefANSI()); |
Dan Sinclair | 8f524d6 | 2017-10-25 13:30:31 -0400 | [diff] [blame] | 311 | if (it == m_MapGlobal.end() || it->second->bDeleted) |
Tom Sepez | 3a6d058 | 2018-08-17 19:28:52 +0000 | [diff] [blame] | 312 | return CJS_Result::Failure(JSMessage::kGlobalNotFoundError); |
Dan Sinclair | 8f524d6 | 2017-10-25 13:30:31 -0400 | [diff] [blame] | 313 | |
dan sinclair | 80435cb | 2017-10-24 21:40:24 -0400 | [diff] [blame] | 314 | it->second->bPersistent = pRuntime->ToBoolean(params[1]); |
Tom Sepez | 3a6d058 | 2018-08-17 19:28:52 +0000 | [diff] [blame] | 315 | return CJS_Result::Success(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 316 | } |
| 317 | |
Dan Sinclair | f743552 | 2018-02-05 22:27:22 +0000 | [diff] [blame] | 318 | void CJS_Global::UpdateGlobalPersistentVariables() { |
Tom Sepez | 6ffd840 | 2018-06-06 20:51:04 +0000 | [diff] [blame] | 319 | CJS_Runtime* pRuntime = GetRuntime(); |
| 320 | if (!pRuntime) |
| 321 | return; |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 322 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 323 | for (int i = 0, sz = m_pGlobalData->GetSize(); i < sz; i++) { |
Tom Sepez | 41d04e1 | 2018-10-30 22:07:36 +0000 | [diff] [blame] | 324 | CFX_GlobalData::Element* pData = m_pGlobalData->GetAt(i); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 325 | switch (pData->data.nType) { |
Tom Sepez | 4a463d5 | 2018-11-07 22:32:41 +0000 | [diff] [blame] | 326 | case CFX_Value::DataType::NUMBER: |
| 327 | SetGlobalVariables(pData->data.sKey, CFX_Value::DataType::NUMBER, |
Tom Sepez | 306ebab | 2018-12-03 22:12:55 +0000 | [diff] [blame] | 328 | pData->data.dData, false, ByteString(), |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 329 | v8::Local<v8::Object>(), pData->bPersistent == 1); |
Tom Sepez | ca61078 | 2018-12-13 18:01:04 +0000 | [diff] [blame] | 330 | pRuntime->PutObjectProperty(ToV8Object(), |
| 331 | pData->data.sKey.AsStringView(), |
| 332 | pRuntime->NewNumber(pData->data.dData)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 333 | break; |
Tom Sepez | 4a463d5 | 2018-11-07 22:32:41 +0000 | [diff] [blame] | 334 | case CFX_Value::DataType::BOOLEAN: |
| 335 | SetGlobalVariables(pData->data.sKey, CFX_Value::DataType::BOOLEAN, 0, |
Tom Sepez | 306ebab | 2018-12-03 22:12:55 +0000 | [diff] [blame] | 336 | pData->data.bData == 1, ByteString(), |
| 337 | v8::Local<v8::Object>(), pData->bPersistent == 1); |
tsepez | e6cf013 | 2017-01-18 14:38:18 -0800 | [diff] [blame] | 338 | pRuntime->PutObjectProperty( |
Tom Sepez | ca61078 | 2018-12-13 18:01:04 +0000 | [diff] [blame] | 339 | ToV8Object(), pData->data.sKey.AsStringView(), |
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; |
Tom Sepez | 4a463d5 | 2018-11-07 22:32:41 +0000 | [diff] [blame] | 342 | case CFX_Value::DataType::STRING: |
| 343 | SetGlobalVariables(pData->data.sKey, CFX_Value::DataType::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( |
Tom Sepez | ca61078 | 2018-12-13 18:01:04 +0000 | [diff] [blame] | 347 | ToV8Object(), pData->data.sKey.AsStringView(), |
Tom Sepez | 3ef0242 | 2018-11-26 23:52:33 +0000 | [diff] [blame] | 348 | pRuntime->NewString( |
| 349 | WideString::FromUTF8(pData->data.sData.AsStringView()) |
| 350 | .AsStringView())); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 351 | break; |
Tom Sepez | 4a463d5 | 2018-11-07 22:32:41 +0000 | [diff] [blame] | 352 | case CFX_Value::DataType::OBJECT: { |
Tom Sepez | 9ad9a5f | 2018-02-07 21:07:24 +0000 | [diff] [blame] | 353 | v8::Local<v8::Object> pObj = pRuntime->NewObject(); |
Tom Sepez | 63b2fc7 | 2017-08-14 16:24:29 -0700 | [diff] [blame] | 354 | if (!pObj.IsEmpty()) { |
| 355 | PutObjectProperty(pObj, &pData->data); |
Tom Sepez | 4a463d5 | 2018-11-07 22:32:41 +0000 | [diff] [blame] | 356 | SetGlobalVariables(pData->data.sKey, CFX_Value::DataType::OBJECT, 0, |
Tom Sepez | 306ebab | 2018-12-03 22:12:55 +0000 | [diff] [blame] | 357 | false, ByteString(), pObj, |
| 358 | pData->bPersistent == 1); |
Tom Sepez | ca61078 | 2018-12-13 18:01:04 +0000 | [diff] [blame] | 359 | pRuntime->PutObjectProperty(ToV8Object(), |
| 360 | pData->data.sKey.AsStringView(), pObj); |
Tom Sepez | 63b2fc7 | 2017-08-14 16:24:29 -0700 | [diff] [blame] | 361 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 362 | } break; |
Tom Sepez | 4a463d5 | 2018-11-07 22:32:41 +0000 | [diff] [blame] | 363 | case CFX_Value::DataType::NULLOBJ: |
| 364 | SetGlobalVariables(pData->data.sKey, CFX_Value::DataType::NULLOBJ, 0, |
Tom Sepez | 306ebab | 2018-12-03 22:12:55 +0000 | [diff] [blame] | 365 | false, ByteString(), v8::Local<v8::Object>(), |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 366 | pData->bPersistent == 1); |
Tom Sepez | 3ef0242 | 2018-11-26 23:52:33 +0000 | [diff] [blame] | 367 | pRuntime->PutObjectProperty( |
Tom Sepez | ca61078 | 2018-12-13 18:01:04 +0000 | [diff] [blame] | 368 | ToV8Object(), pData->data.sKey.AsStringView(), pRuntime->NewNull()); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 369 | break; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 370 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 371 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 372 | } |
| 373 | |
Dan Sinclair | f743552 | 2018-02-05 22:27:22 +0000 | [diff] [blame] | 374 | void CJS_Global::CommitGlobalPersisitentVariables(CJS_Runtime* pRuntime) { |
Tom Sepez | a775723 | 2017-04-18 11:10:39 -0700 | [diff] [blame] | 375 | for (const auto& iter : m_MapGlobal) { |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 376 | ByteString name = iter.first; |
Tom Sepez | a775723 | 2017-04-18 11:10:39 -0700 | [diff] [blame] | 377 | JSGlobalData* pData = iter.second.get(); |
Tom Sepez | 09d33bc | 2015-08-19 09:49:24 -0700 | [diff] [blame] | 378 | if (pData->bDeleted) { |
| 379 | m_pGlobalData->DeleteGlobalVariable(name); |
Tom Sepez | a775723 | 2017-04-18 11:10:39 -0700 | [diff] [blame] | 380 | continue; |
| 381 | } |
| 382 | switch (pData->nType) { |
Tom Sepez | 4a463d5 | 2018-11-07 22:32:41 +0000 | [diff] [blame] | 383 | case CFX_Value::DataType::NUMBER: |
Tom Sepez | a775723 | 2017-04-18 11:10:39 -0700 | [diff] [blame] | 384 | m_pGlobalData->SetGlobalVariableNumber(name, pData->dData); |
| 385 | m_pGlobalData->SetGlobalVariablePersistent(name, pData->bPersistent); |
| 386 | break; |
Tom Sepez | 4a463d5 | 2018-11-07 22:32:41 +0000 | [diff] [blame] | 387 | case CFX_Value::DataType::BOOLEAN: |
Tom Sepez | a775723 | 2017-04-18 11:10:39 -0700 | [diff] [blame] | 388 | m_pGlobalData->SetGlobalVariableBoolean(name, pData->bData); |
| 389 | m_pGlobalData->SetGlobalVariablePersistent(name, pData->bPersistent); |
| 390 | break; |
Tom Sepez | 4a463d5 | 2018-11-07 22:32:41 +0000 | [diff] [blame] | 391 | case CFX_Value::DataType::STRING: |
Tom Sepez | a775723 | 2017-04-18 11:10:39 -0700 | [diff] [blame] | 392 | m_pGlobalData->SetGlobalVariableString(name, pData->sData); |
| 393 | m_pGlobalData->SetGlobalVariablePersistent(name, pData->bPersistent); |
| 394 | break; |
Tom Sepez | 4a463d5 | 2018-11-07 22:32:41 +0000 | [diff] [blame] | 395 | case CFX_Value::DataType::OBJECT: { |
Tom Sepez | 41d04e1 | 2018-10-30 22:07:36 +0000 | [diff] [blame] | 396 | CFX_GlobalArray array; |
Dan Sinclair | f743552 | 2018-02-05 22:27:22 +0000 | [diff] [blame] | 397 | v8::Local<v8::Object> obj = |
| 398 | v8::Local<v8::Object>::New(GetIsolate(), pData->pData); |
Lei Zhang | b1a4db5 | 2018-07-11 13:02:54 +0000 | [diff] [blame] | 399 | ObjectToArray(pRuntime, obj, &array); |
Tom Sepez | 436c9f1 | 2018-11-05 18:20:23 +0000 | [diff] [blame] | 400 | m_pGlobalData->SetGlobalVariableObject(name, std::move(array)); |
Tom Sepez | a775723 | 2017-04-18 11:10:39 -0700 | [diff] [blame] | 401 | m_pGlobalData->SetGlobalVariablePersistent(name, pData->bPersistent); |
| 402 | } break; |
Tom Sepez | 4a463d5 | 2018-11-07 22:32:41 +0000 | [diff] [blame] | 403 | case CFX_Value::DataType::NULLOBJ: |
Tom Sepez | a775723 | 2017-04-18 11:10:39 -0700 | [diff] [blame] | 404 | m_pGlobalData->SetGlobalVariableNull(name); |
| 405 | m_pGlobalData->SetGlobalVariablePersistent(name, pData->bPersistent); |
| 406 | break; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 407 | } |
| 408 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 409 | } |
| 410 | |
Dan Sinclair | f743552 | 2018-02-05 22:27:22 +0000 | [diff] [blame] | 411 | void CJS_Global::ObjectToArray(CJS_Runtime* pRuntime, |
| 412 | v8::Local<v8::Object> pObj, |
Tom Sepez | 41d04e1 | 2018-10-30 22:07:36 +0000 | [diff] [blame] | 413 | CFX_GlobalArray* pArray) { |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 414 | std::vector<WideString> pKeyList = pRuntime->GetObjectPropertyNames(pObj); |
tsepez | d0b6ed1 | 2016-08-11 19:50:57 -0700 | [diff] [blame] | 415 | for (const auto& ws : pKeyList) { |
Tom Sepez | b4c95fe | 2018-11-27 01:09:44 +0000 | [diff] [blame] | 416 | ByteString sKey = ws.ToUTF8(); |
Tom Sepez | ca61078 | 2018-12-13 18:01:04 +0000 | [diff] [blame] | 417 | v8::Local<v8::Value> v = |
| 418 | pRuntime->GetObjectProperty(pObj, sKey.AsStringView()); |
Dan Sinclair | 3cac360 | 2017-10-24 15:15:27 -0400 | [diff] [blame] | 419 | if (v->IsNumber()) { |
Tom Sepez | 41d04e1 | 2018-10-30 22:07:36 +0000 | [diff] [blame] | 420 | auto pObjElement = pdfium::MakeUnique<CFX_KeyValue>(); |
Tom Sepez | 4a463d5 | 2018-11-07 22:32:41 +0000 | [diff] [blame] | 421 | pObjElement->nType = CFX_Value::DataType::NUMBER; |
Dan Sinclair | 3cac360 | 2017-10-24 15:15:27 -0400 | [diff] [blame] | 422 | pObjElement->sKey = sKey; |
| 423 | pObjElement->dData = pRuntime->ToDouble(v); |
Tom Sepez | 8abd0df | 2018-09-14 23:07:11 +0000 | [diff] [blame] | 424 | pArray->Add(std::move(pObjElement)); |
Dan Sinclair | 3cac360 | 2017-10-24 15:15:27 -0400 | [diff] [blame] | 425 | continue; |
| 426 | } |
| 427 | if (v->IsBoolean()) { |
Tom Sepez | 41d04e1 | 2018-10-30 22:07:36 +0000 | [diff] [blame] | 428 | auto pObjElement = pdfium::MakeUnique<CFX_KeyValue>(); |
Tom Sepez | 4a463d5 | 2018-11-07 22:32:41 +0000 | [diff] [blame] | 429 | pObjElement->nType = CFX_Value::DataType::BOOLEAN; |
Dan Sinclair | 3cac360 | 2017-10-24 15:15:27 -0400 | [diff] [blame] | 430 | pObjElement->sKey = sKey; |
| 431 | pObjElement->dData = pRuntime->ToBoolean(v); |
Tom Sepez | 8abd0df | 2018-09-14 23:07:11 +0000 | [diff] [blame] | 432 | pArray->Add(std::move(pObjElement)); |
Dan Sinclair | 3cac360 | 2017-10-24 15:15:27 -0400 | [diff] [blame] | 433 | continue; |
| 434 | } |
| 435 | if (v->IsString()) { |
Tom Sepez | 34dab07 | 2018-08-08 17:49:02 +0000 | [diff] [blame] | 436 | ByteString sValue = pRuntime->ToWideString(v).ToDefANSI(); |
Tom Sepez | 41d04e1 | 2018-10-30 22:07:36 +0000 | [diff] [blame] | 437 | auto pObjElement = pdfium::MakeUnique<CFX_KeyValue>(); |
Tom Sepez | 4a463d5 | 2018-11-07 22:32:41 +0000 | [diff] [blame] | 438 | pObjElement->nType = CFX_Value::DataType::STRING; |
Dan Sinclair | 3cac360 | 2017-10-24 15:15:27 -0400 | [diff] [blame] | 439 | pObjElement->sKey = sKey; |
| 440 | pObjElement->sData = sValue; |
Tom Sepez | 8abd0df | 2018-09-14 23:07:11 +0000 | [diff] [blame] | 441 | pArray->Add(std::move(pObjElement)); |
Dan Sinclair | 3cac360 | 2017-10-24 15:15:27 -0400 | [diff] [blame] | 442 | continue; |
| 443 | } |
| 444 | if (v->IsObject()) { |
Tom Sepez | 41d04e1 | 2018-10-30 22:07:36 +0000 | [diff] [blame] | 445 | auto pObjElement = pdfium::MakeUnique<CFX_KeyValue>(); |
Tom Sepez | 4a463d5 | 2018-11-07 22:32:41 +0000 | [diff] [blame] | 446 | pObjElement->nType = CFX_Value::DataType::OBJECT; |
Dan Sinclair | 3cac360 | 2017-10-24 15:15:27 -0400 | [diff] [blame] | 447 | pObjElement->sKey = sKey; |
Lei Zhang | b1a4db5 | 2018-07-11 13:02:54 +0000 | [diff] [blame] | 448 | ObjectToArray(pRuntime, pRuntime->ToObject(v), &pObjElement->objData); |
Tom Sepez | 8abd0df | 2018-09-14 23:07:11 +0000 | [diff] [blame] | 449 | pArray->Add(std::move(pObjElement)); |
Dan Sinclair | 3cac360 | 2017-10-24 15:15:27 -0400 | [diff] [blame] | 450 | continue; |
| 451 | } |
| 452 | if (v->IsNull()) { |
Tom Sepez | 41d04e1 | 2018-10-30 22:07:36 +0000 | [diff] [blame] | 453 | auto pObjElement = pdfium::MakeUnique<CFX_KeyValue>(); |
Tom Sepez | 4a463d5 | 2018-11-07 22:32:41 +0000 | [diff] [blame] | 454 | pObjElement->nType = CFX_Value::DataType::NULLOBJ; |
Dan Sinclair | 3cac360 | 2017-10-24 15:15:27 -0400 | [diff] [blame] | 455 | pObjElement->sKey = sKey; |
Tom Sepez | 8abd0df | 2018-09-14 23:07:11 +0000 | [diff] [blame] | 456 | pArray->Add(std::move(pObjElement)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 457 | } |
| 458 | } |
| 459 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 460 | |
Dan Sinclair | f743552 | 2018-02-05 22:27:22 +0000 | [diff] [blame] | 461 | void CJS_Global::PutObjectProperty(v8::Local<v8::Object> pObj, |
Tom Sepez | 41d04e1 | 2018-10-30 22:07:36 +0000 | [diff] [blame] | 462 | CFX_KeyValue* pData) { |
Tom Sepez | 6ffd840 | 2018-06-06 20:51:04 +0000 | [diff] [blame] | 463 | CJS_Runtime* pRuntime = GetRuntime(); |
| 464 | if (pRuntime) |
| 465 | return; |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 466 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 467 | for (int i = 0, sz = pData->objData.Count(); i < sz; i++) { |
Tom Sepez | 41d04e1 | 2018-10-30 22:07:36 +0000 | [diff] [blame] | 468 | CFX_KeyValue* pObjData = pData->objData.GetAt(i); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 469 | switch (pObjData->nType) { |
Tom Sepez | 4a463d5 | 2018-11-07 22:32:41 +0000 | [diff] [blame] | 470 | case CFX_Value::DataType::NUMBER: |
Tom Sepez | ca61078 | 2018-12-13 18:01:04 +0000 | [diff] [blame] | 471 | pRuntime->PutObjectProperty(pObj, pObjData->sKey.AsStringView(), |
| 472 | pRuntime->NewNumber(pObjData->dData)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 473 | break; |
Tom Sepez | 4a463d5 | 2018-11-07 22:32:41 +0000 | [diff] [blame] | 474 | case CFX_Value::DataType::BOOLEAN: |
Tom Sepez | ca61078 | 2018-12-13 18:01:04 +0000 | [diff] [blame] | 475 | pRuntime->PutObjectProperty(pObj, pObjData->sKey.AsStringView(), |
| 476 | pRuntime->NewBoolean(pObjData->bData == 1)); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 477 | break; |
Tom Sepez | 4a463d5 | 2018-11-07 22:32:41 +0000 | [diff] [blame] | 478 | case CFX_Value::DataType::STRING: |
tsepez | e6cf013 | 2017-01-18 14:38:18 -0800 | [diff] [blame] | 479 | pRuntime->PutObjectProperty( |
Tom Sepez | ca61078 | 2018-12-13 18:01:04 +0000 | [diff] [blame] | 480 | pObj, pObjData->sKey.AsStringView(), |
Tom Sepez | 3ef0242 | 2018-11-26 23:52:33 +0000 | [diff] [blame] | 481 | pRuntime->NewString( |
| 482 | WideString::FromUTF8(pObjData->sData.AsStringView()) |
| 483 | .AsStringView())); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 484 | break; |
Tom Sepez | 4a463d5 | 2018-11-07 22:32:41 +0000 | [diff] [blame] | 485 | case CFX_Value::DataType::OBJECT: { |
Tom Sepez | 9ad9a5f | 2018-02-07 21:07:24 +0000 | [diff] [blame] | 486 | v8::Local<v8::Object> pNewObj = pRuntime->NewObject(); |
Tom Sepez | 63b2fc7 | 2017-08-14 16:24:29 -0700 | [diff] [blame] | 487 | if (!pNewObj.IsEmpty()) { |
| 488 | PutObjectProperty(pNewObj, pObjData); |
Tom Sepez | ca61078 | 2018-12-13 18:01:04 +0000 | [diff] [blame] | 489 | pRuntime->PutObjectProperty(pObj, pObjData->sKey.AsStringView(), |
| 490 | pNewObj); |
Tom Sepez | 63b2fc7 | 2017-08-14 16:24:29 -0700 | [diff] [blame] | 491 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 492 | } break; |
Tom Sepez | 4a463d5 | 2018-11-07 22:32:41 +0000 | [diff] [blame] | 493 | case CFX_Value::DataType::NULLOBJ: |
Tom Sepez | ca61078 | 2018-12-13 18:01:04 +0000 | [diff] [blame] | 494 | pRuntime->PutObjectProperty(pObj, pObjData->sKey.AsStringView(), |
| 495 | pRuntime->NewNull()); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 496 | break; |
| 497 | } |
| 498 | } |
| 499 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 500 | |
Dan Sinclair | f743552 | 2018-02-05 22:27:22 +0000 | [diff] [blame] | 501 | void CJS_Global::DestroyGlobalPersisitentVariables() { |
Tom Sepez | a775723 | 2017-04-18 11:10:39 -0700 | [diff] [blame] | 502 | m_MapGlobal.clear(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 503 | } |
| 504 | |
Tom Sepez | 3a6d058 | 2018-08-17 19:28:52 +0000 | [diff] [blame] | 505 | CJS_Result CJS_Global::SetGlobalVariables(const ByteString& propname, |
Tom Sepez | 4a463d5 | 2018-11-07 22:32:41 +0000 | [diff] [blame] | 506 | CFX_Value::DataType nType, |
Dan Sinclair | f743552 | 2018-02-05 22:27:22 +0000 | [diff] [blame] | 507 | double dData, |
| 508 | bool bData, |
| 509 | const ByteString& sData, |
| 510 | v8::Local<v8::Object> pData, |
| 511 | bool bDefaultPersistent) { |
tsepez | b4c9f3f | 2016-04-13 15:41:21 -0700 | [diff] [blame] | 512 | if (propname.IsEmpty()) |
Tom Sepez | 3a6d058 | 2018-08-17 19:28:52 +0000 | [diff] [blame] | 513 | return CJS_Result::Failure(JSMessage::kUnknownProperty); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 514 | |
Tom Sepez | a775723 | 2017-04-18 11:10:39 -0700 | [diff] [blame] | 515 | auto it = m_MapGlobal.find(propname); |
| 516 | if (it != m_MapGlobal.end()) { |
| 517 | JSGlobalData* pTemp = it->second.get(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 518 | if (pTemp->bDeleted || pTemp->nType != nType) { |
| 519 | pTemp->dData = 0; |
| 520 | pTemp->bData = 0; |
Lei Zhang | fe2cd4d | 2017-11-22 20:04:12 +0000 | [diff] [blame] | 521 | pTemp->sData.clear(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 522 | pTemp->nType = nType; |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 523 | } |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 524 | pTemp->bDeleted = false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 525 | switch (nType) { |
Tom Sepez | 4a463d5 | 2018-11-07 22:32:41 +0000 | [diff] [blame] | 526 | case CFX_Value::DataType::NUMBER: |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 527 | pTemp->dData = dData; |
Tom Sepez | a775723 | 2017-04-18 11:10:39 -0700 | [diff] [blame] | 528 | break; |
Tom Sepez | 4a463d5 | 2018-11-07 22:32:41 +0000 | [diff] [blame] | 529 | case CFX_Value::DataType::BOOLEAN: |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 530 | pTemp->bData = bData; |
Tom Sepez | a775723 | 2017-04-18 11:10:39 -0700 | [diff] [blame] | 531 | break; |
Tom Sepez | 4a463d5 | 2018-11-07 22:32:41 +0000 | [diff] [blame] | 532 | case CFX_Value::DataType::STRING: |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 533 | pTemp->sData = sData; |
Tom Sepez | a775723 | 2017-04-18 11:10:39 -0700 | [diff] [blame] | 534 | break; |
Tom Sepez | 4a463d5 | 2018-11-07 22:32:41 +0000 | [diff] [blame] | 535 | case CFX_Value::DataType::OBJECT: |
Tom Sepez | 371379d | 2015-11-06 08:29:39 -0800 | [diff] [blame] | 536 | pTemp->pData.Reset(pData->GetIsolate(), pData); |
Tom Sepez | a775723 | 2017-04-18 11:10:39 -0700 | [diff] [blame] | 537 | break; |
Tom Sepez | 4a463d5 | 2018-11-07 22:32:41 +0000 | [diff] [blame] | 538 | case CFX_Value::DataType::NULLOBJ: |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 539 | break; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 540 | default: |
Tom Sepez | 3a6d058 | 2018-08-17 19:28:52 +0000 | [diff] [blame] | 541 | return CJS_Result::Failure(JSMessage::kObjectTypeError); |
Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 542 | } |
Tom Sepez | 3a6d058 | 2018-08-17 19:28:52 +0000 | [diff] [blame] | 543 | return CJS_Result::Success(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 544 | } |
| 545 | |
Tom Sepez | a775723 | 2017-04-18 11:10:39 -0700 | [diff] [blame] | 546 | auto pNewData = pdfium::MakeUnique<JSGlobalData>(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 547 | switch (nType) { |
Tom Sepez | 4a463d5 | 2018-11-07 22:32:41 +0000 | [diff] [blame] | 548 | case CFX_Value::DataType::NUMBER: |
| 549 | pNewData->nType = CFX_Value::DataType::NUMBER; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 550 | pNewData->dData = dData; |
| 551 | pNewData->bPersistent = bDefaultPersistent; |
Tom Sepez | a775723 | 2017-04-18 11:10:39 -0700 | [diff] [blame] | 552 | break; |
Tom Sepez | 4a463d5 | 2018-11-07 22:32:41 +0000 | [diff] [blame] | 553 | case CFX_Value::DataType::BOOLEAN: |
| 554 | pNewData->nType = CFX_Value::DataType::BOOLEAN; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 555 | pNewData->bData = bData; |
| 556 | pNewData->bPersistent = bDefaultPersistent; |
Tom Sepez | a775723 | 2017-04-18 11:10:39 -0700 | [diff] [blame] | 557 | break; |
Tom Sepez | 4a463d5 | 2018-11-07 22:32:41 +0000 | [diff] [blame] | 558 | case CFX_Value::DataType::STRING: |
| 559 | pNewData->nType = CFX_Value::DataType::STRING; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 560 | pNewData->sData = sData; |
| 561 | pNewData->bPersistent = bDefaultPersistent; |
Tom Sepez | a775723 | 2017-04-18 11:10:39 -0700 | [diff] [blame] | 562 | break; |
Tom Sepez | 4a463d5 | 2018-11-07 22:32:41 +0000 | [diff] [blame] | 563 | case CFX_Value::DataType::OBJECT: |
| 564 | pNewData->nType = CFX_Value::DataType::OBJECT; |
Tom Sepez | 371379d | 2015-11-06 08:29:39 -0800 | [diff] [blame] | 565 | pNewData->pData.Reset(pData->GetIsolate(), pData); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 566 | pNewData->bPersistent = bDefaultPersistent; |
Tom Sepez | a775723 | 2017-04-18 11:10:39 -0700 | [diff] [blame] | 567 | break; |
Tom Sepez | 4a463d5 | 2018-11-07 22:32:41 +0000 | [diff] [blame] | 568 | case CFX_Value::DataType::NULLOBJ: |
| 569 | pNewData->nType = CFX_Value::DataType::NULLOBJ; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 570 | pNewData->bPersistent = bDefaultPersistent; |
Tom Sepez | a775723 | 2017-04-18 11:10:39 -0700 | [diff] [blame] | 571 | break; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 572 | default: |
Tom Sepez | 3a6d058 | 2018-08-17 19:28:52 +0000 | [diff] [blame] | 573 | return CJS_Result::Failure(JSMessage::kObjectTypeError); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 574 | } |
Tom Sepez | a775723 | 2017-04-18 11:10:39 -0700 | [diff] [blame] | 575 | m_MapGlobal[propname] = std::move(pNewData); |
Tom Sepez | 3a6d058 | 2018-08-17 19:28:52 +0000 | [diff] [blame] | 576 | return CJS_Result::Success(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 577 | } |