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 | |
dsinclair | 64376be | 2016-03-31 20:03:24 -0700 | [diff] [blame] | 7 | #include "fpdfsdk/javascript/cjs_runtime.h" |
Tom Sepez | 3745841 | 2015-10-06 11:33:46 -0700 | [diff] [blame] | 8 | |
tsepez | 41a53ad | 2016-03-28 16:59:30 -0700 | [diff] [blame] | 9 | #include <algorithm> |
| 10 | |
dsinclair | 735606d | 2016-10-05 15:47:02 -0700 | [diff] [blame] | 11 | #include "fpdfsdk/cpdfsdk_formfillenvironment.h" |
Dan Sinclair | f766ad2 | 2016-03-14 13:51:24 -0400 | [diff] [blame] | 12 | #include "fpdfsdk/javascript/Document.h" |
| 13 | #include "fpdfsdk/javascript/Field.h" |
| 14 | #include "fpdfsdk/javascript/Icon.h" |
Dan Sinclair | f766ad2 | 2016-03-14 13:51:24 -0400 | [diff] [blame] | 15 | #include "fpdfsdk/javascript/JS_Define.h" |
| 16 | #include "fpdfsdk/javascript/JS_EventHandler.h" |
| 17 | #include "fpdfsdk/javascript/JS_GlobalData.h" |
| 18 | #include "fpdfsdk/javascript/JS_Object.h" |
| 19 | #include "fpdfsdk/javascript/JS_Value.h" |
| 20 | #include "fpdfsdk/javascript/PublicMethods.h" |
| 21 | #include "fpdfsdk/javascript/app.h" |
dan sinclair | 95460d8 | 2017-10-25 20:25:04 -0400 | [diff] [blame] | 22 | #include "fpdfsdk/javascript/cjs_annot.h" |
dan sinclair | 993a199 | 2017-10-25 20:21:09 -0400 | [diff] [blame] | 23 | #include "fpdfsdk/javascript/cjs_border.h" |
| 24 | #include "fpdfsdk/javascript/cjs_display.h" |
Tom Sepez | d6ae2af | 2017-02-16 11:49:55 -0800 | [diff] [blame] | 25 | #include "fpdfsdk/javascript/cjs_event_context.h" |
dan sinclair | 993a199 | 2017-10-25 20:21:09 -0400 | [diff] [blame] | 26 | #include "fpdfsdk/javascript/cjs_font.h" |
dan sinclair | d7ac26c | 2017-10-25 20:30:02 -0400 | [diff] [blame^] | 27 | #include "fpdfsdk/javascript/cjs_global.h" |
dan sinclair | 993a199 | 2017-10-25 20:21:09 -0400 | [diff] [blame] | 28 | #include "fpdfsdk/javascript/cjs_globalarrays.h" |
| 29 | #include "fpdfsdk/javascript/cjs_globalconsts.h" |
| 30 | #include "fpdfsdk/javascript/cjs_highlight.h" |
| 31 | #include "fpdfsdk/javascript/cjs_position.h" |
| 32 | #include "fpdfsdk/javascript/cjs_scalehow.h" |
| 33 | #include "fpdfsdk/javascript/cjs_scalewhen.h" |
| 34 | #include "fpdfsdk/javascript/cjs_style.h" |
| 35 | #include "fpdfsdk/javascript/cjs_zoomtype.h" |
Dan Sinclair | f766ad2 | 2016-03-14 13:51:24 -0400 | [diff] [blame] | 36 | #include "fpdfsdk/javascript/color.h" |
| 37 | #include "fpdfsdk/javascript/console.h" |
| 38 | #include "fpdfsdk/javascript/event.h" |
Dan Sinclair | f766ad2 | 2016-03-14 13:51:24 -0400 | [diff] [blame] | 39 | #include "fpdfsdk/javascript/report.h" |
| 40 | #include "fpdfsdk/javascript/util.h" |
dsinclair | f34518b | 2016-09-13 12:03:48 -0700 | [diff] [blame] | 41 | #include "public/fpdf_formfill.h" |
Lei Zhang | cd2bb30 | 2015-12-22 13:49:44 -0800 | [diff] [blame] | 42 | #include "third_party/base/stl_util.h" |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 43 | |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 44 | #ifdef PDF_ENABLE_XFA |
dsinclair | 4355468 | 2016-09-29 17:29:48 -0700 | [diff] [blame] | 45 | #include "fxjs/cfxjse_value.h" |
Tom Sepez | 40e9ff3 | 2015-11-30 12:39:54 -0800 | [diff] [blame] | 46 | #endif // PDF_ENABLE_XFA |
| 47 | |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 48 | // static |
Tom Sepez | 452b4f3 | 2015-10-13 09:27:27 -0700 | [diff] [blame] | 49 | void IJS_Runtime::Initialize(unsigned int slot, void* isolate) { |
| 50 | FXJS_Initialize(slot, reinterpret_cast<v8::Isolate*>(isolate)); |
| 51 | } |
| 52 | |
| 53 | // static |
thestig | 2d6dda1 | 2016-06-28 07:39:09 -0700 | [diff] [blame] | 54 | void IJS_Runtime::Destroy() { |
| 55 | FXJS_Release(); |
| 56 | } |
| 57 | |
| 58 | // static |
dsinclair | 82e1767 | 2016-10-11 12:38:01 -0700 | [diff] [blame] | 59 | IJS_Runtime* IJS_Runtime::Create(CPDFSDK_FormFillEnvironment* pFormFillEnv) { |
| 60 | return new CJS_Runtime(pFormFillEnv); |
Tom Sepez | 3745841 | 2015-10-06 11:33:46 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 63 | // static |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 64 | CJS_Runtime* CJS_Runtime::CurrentRuntimeFromIsolate(v8::Isolate* pIsolate) { |
| 65 | return static_cast<CJS_Runtime*>( |
| 66 | CFXJS_Engine::CurrentEngineFromIsolate(pIsolate)); |
| 67 | } |
| 68 | |
dsinclair | 82e1767 | 2016-10-11 12:38:01 -0700 | [diff] [blame] | 69 | CJS_Runtime::CJS_Runtime(CPDFSDK_FormFillEnvironment* pFormFillEnv) |
| 70 | : m_pFormFillEnv(pFormFillEnv), |
tsepez | a494191 | 2016-08-15 11:40:12 -0700 | [diff] [blame] | 71 | m_bBlocking(false), |
Jochen Eisinger | 2900784 | 2015-08-05 09:02:13 +0200 | [diff] [blame] | 72 | m_isolateManaged(false) { |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 73 | v8::Isolate* pIsolate = nullptr; |
dsinclair | 8837c91 | 2016-11-01 11:22:37 -0700 | [diff] [blame] | 74 | |
dsinclair | 82e1767 | 2016-10-11 12:38:01 -0700 | [diff] [blame] | 75 | IPDF_JSPLATFORM* pPlatform = m_pFormFillEnv->GetFormFillInfo()->m_pJsPlatform; |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 76 | if (pPlatform->version <= 2) { |
| 77 | unsigned int embedderDataSlot = 0; |
| 78 | v8::Isolate* pExternalIsolate = nullptr; |
| 79 | if (pPlatform->version == 2) { |
| 80 | pExternalIsolate = reinterpret_cast<v8::Isolate*>(pPlatform->m_isolate); |
| 81 | embedderDataSlot = pPlatform->m_v8EmbedderSlot; |
dsinclair | d71bae0 | 2016-06-09 14:21:20 -0700 | [diff] [blame] | 82 | } |
| 83 | FXJS_Initialize(embedderDataSlot, pExternalIsolate); |
| 84 | } |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 85 | m_isolateManaged = FXJS_GetIsolate(&pIsolate); |
| 86 | SetIsolate(pIsolate); |
Tom Sepez | d2cc1b9 | 2015-04-30 15:19:03 -0700 | [diff] [blame] | 87 | |
dsinclair | 8837c91 | 2016-11-01 11:22:37 -0700 | [diff] [blame] | 88 | #ifdef PDF_ENABLE_XFA |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 89 | v8::Isolate::Scope isolate_scope(pIsolate); |
| 90 | v8::HandleScope handle_scope(pIsolate); |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 91 | #endif |
dsinclair | d71bae0 | 2016-06-09 14:21:20 -0700 | [diff] [blame] | 92 | |
Lei Zhang | 3fa115b | 2015-10-08 12:04:47 -0700 | [diff] [blame] | 93 | if (m_isolateManaged || FXJS_GlobalIsolateRefCount() == 0) |
| 94 | DefineJSObjects(); |
| 95 | |
Tom Sepez | d6ae2af | 2017-02-16 11:49:55 -0800 | [diff] [blame] | 96 | IJS_EventContext* pContext = NewEventContext(); |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 97 | InitializeEngine(); |
Tom Sepez | d6ae2af | 2017-02-16 11:49:55 -0800 | [diff] [blame] | 98 | ReleaseEventContext(pContext); |
dsinclair | 82e1767 | 2016-10-11 12:38:01 -0700 | [diff] [blame] | 99 | SetFormFillEnvToDocument(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 102 | CJS_Runtime::~CJS_Runtime() { |
tsepez | 1c62054 | 2016-09-12 09:47:52 -0700 | [diff] [blame] | 103 | NotifyObservedPtrs(); |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 104 | ReleaseEngine(); |
| 105 | if (m_isolateManaged) { |
| 106 | GetIsolate()->Dispose(); |
| 107 | SetIsolate(nullptr); |
| 108 | } |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Tom Sepez | 142165e | 2015-09-11 13:21:50 -0700 | [diff] [blame] | 111 | void CJS_Runtime::DefineJSObjects() { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 112 | v8::Isolate::Scope isolate_scope(GetIsolate()); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 113 | v8::HandleScope handle_scope(GetIsolate()); |
| 114 | v8::Local<v8::Context> context = v8::Context::New(GetIsolate()); |
| 115 | v8::Context::Scope context_scope(context); |
Tom Sepez | 570875c | 2015-09-11 08:35:03 -0700 | [diff] [blame] | 116 | |
| 117 | // The call order determines the "ObjDefID" assigned to each class. |
| 118 | // ObjDefIDs 0 - 2 |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 119 | CJS_Border::DefineJSObjects(this, FXJSOBJTYPE_STATIC); |
| 120 | CJS_Display::DefineJSObjects(this, FXJSOBJTYPE_STATIC); |
| 121 | CJS_Font::DefineJSObjects(this, FXJSOBJTYPE_STATIC); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 122 | |
Tom Sepez | 570875c | 2015-09-11 08:35:03 -0700 | [diff] [blame] | 123 | // ObjDefIDs 3 - 5 |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 124 | CJS_Highlight::DefineJSObjects(this, FXJSOBJTYPE_STATIC); |
| 125 | CJS_Position::DefineJSObjects(this, FXJSOBJTYPE_STATIC); |
| 126 | CJS_ScaleHow::DefineJSObjects(this, FXJSOBJTYPE_STATIC); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 127 | |
Tom Sepez | 570875c | 2015-09-11 08:35:03 -0700 | [diff] [blame] | 128 | // ObjDefIDs 6 - 8 |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 129 | CJS_ScaleWhen::DefineJSObjects(this, FXJSOBJTYPE_STATIC); |
| 130 | CJS_Style::DefineJSObjects(this, FXJSOBJTYPE_STATIC); |
| 131 | CJS_Zoomtype::DefineJSObjects(this, FXJSOBJTYPE_STATIC); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 132 | |
Tom Sepez | 570875c | 2015-09-11 08:35:03 -0700 | [diff] [blame] | 133 | // ObjDefIDs 9 - 11 |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 134 | CJS_App::DefineJSObjects(this, FXJSOBJTYPE_STATIC); |
| 135 | CJS_Color::DefineJSObjects(this, FXJSOBJTYPE_STATIC); |
| 136 | CJS_Console::DefineJSObjects(this, FXJSOBJTYPE_STATIC); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 137 | |
Tom Sepez | 570875c | 2015-09-11 08:35:03 -0700 | [diff] [blame] | 138 | // ObjDefIDs 12 - 14 |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 139 | CJS_Document::DefineJSObjects(this, FXJSOBJTYPE_GLOBAL); |
| 140 | CJS_Event::DefineJSObjects(this, FXJSOBJTYPE_STATIC); |
| 141 | CJS_Field::DefineJSObjects(this, FXJSOBJTYPE_DYNAMIC); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 142 | |
Tom Sepez | 570875c | 2015-09-11 08:35:03 -0700 | [diff] [blame] | 143 | // ObjDefIDs 15 - 17 |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 144 | CJS_Global::DefineJSObjects(this, FXJSOBJTYPE_STATIC); |
| 145 | CJS_Icon::DefineJSObjects(this, FXJSOBJTYPE_DYNAMIC); |
| 146 | CJS_Util::DefineJSObjects(this, FXJSOBJTYPE_STATIC); |
Tom Sepez | 570875c | 2015-09-11 08:35:03 -0700 | [diff] [blame] | 147 | |
Tom Sepez | 142165e | 2015-09-11 13:21:50 -0700 | [diff] [blame] | 148 | // ObjDefIDs 18 - 20 (these can't fail, return void). |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 149 | CJS_PublicMethods::DefineJSObjects(this); |
Tom Sepez | 67fd5df | 2015-10-08 12:24:19 -0700 | [diff] [blame] | 150 | CJS_GlobalConsts::DefineJSObjects(this); |
| 151 | CJS_GlobalArrays::DefineJSObjects(this); |
Tom Sepez | 570875c | 2015-09-11 08:35:03 -0700 | [diff] [blame] | 152 | |
tonikitoo | 618cb1f | 2016-08-18 20:10:17 -0700 | [diff] [blame] | 153 | // ObjDefIDs 21 - 23. |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 154 | CJS_TimerObj::DefineJSObjects(this, FXJSOBJTYPE_DYNAMIC); |
| 155 | CJS_PrintParamsObj::DefineJSObjects(this, FXJSOBJTYPE_DYNAMIC); |
tonikitoo | 618cb1f | 2016-08-18 20:10:17 -0700 | [diff] [blame] | 156 | CJS_Annot::DefineJSObjects(this, FXJSOBJTYPE_DYNAMIC); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Tom Sepez | d6ae2af | 2017-02-16 11:49:55 -0800 | [diff] [blame] | 159 | IJS_EventContext* CJS_Runtime::NewEventContext() { |
Dan Sinclair | 0bb1333 | 2017-03-30 16:12:02 -0400 | [diff] [blame] | 160 | m_EventContextArray.push_back(pdfium::MakeUnique<CJS_EventContext>(this)); |
Tom Sepez | d6ae2af | 2017-02-16 11:49:55 -0800 | [diff] [blame] | 161 | return m_EventContextArray.back().get(); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 162 | } |
| 163 | |
Tom Sepez | d6ae2af | 2017-02-16 11:49:55 -0800 | [diff] [blame] | 164 | void CJS_Runtime::ReleaseEventContext(IJS_EventContext* pContext) { |
| 165 | auto it = std::find(m_EventContextArray.begin(), m_EventContextArray.end(), |
| 166 | pdfium::FakeUniquePtr<CJS_EventContext>( |
| 167 | static_cast<CJS_EventContext*>(pContext))); |
| 168 | if (it != m_EventContextArray.end()) |
| 169 | m_EventContextArray.erase(it); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Tom Sepez | b1670b5 | 2017-02-16 17:01:00 -0800 | [diff] [blame] | 172 | CJS_EventContext* CJS_Runtime::GetCurrentEventContext() const { |
Tom Sepez | d6ae2af | 2017-02-16 11:49:55 -0800 | [diff] [blame] | 173 | return m_EventContextArray.empty() ? nullptr |
| 174 | : m_EventContextArray.back().get(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 175 | } |
| 176 | |
dsinclair | 82e1767 | 2016-10-11 12:38:01 -0700 | [diff] [blame] | 177 | void CJS_Runtime::SetFormFillEnvToDocument() { |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 178 | v8::Isolate::Scope isolate_scope(GetIsolate()); |
| 179 | v8::HandleScope handle_scope(GetIsolate()); |
| 180 | v8::Local<v8::Context> context = NewLocalContext(); |
| 181 | v8::Context::Scope context_scope(context); |
| 182 | |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 183 | v8::Local<v8::Object> pThis = GetThisObj(); |
| 184 | if (pThis.IsEmpty()) |
| 185 | return; |
| 186 | |
| 187 | if (CFXJS_Engine::GetObjDefnID(pThis) != CJS_Document::g_nObjDefnID) |
| 188 | return; |
| 189 | |
| 190 | CJS_Document* pJSDocument = |
| 191 | static_cast<CJS_Document*>(GetObjectPrivate(pThis)); |
| 192 | if (!pJSDocument) |
| 193 | return; |
| 194 | |
| 195 | Document* pDocument = static_cast<Document*>(pJSDocument->GetEmbedObject()); |
| 196 | if (!pDocument) |
| 197 | return; |
| 198 | |
Tom Sepez | 77f6d0f | 2017-02-23 12:14:10 -0800 | [diff] [blame] | 199 | pDocument->SetFormFillEnv(m_pFormFillEnv.Get()); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 200 | } |
| 201 | |
dsinclair | 82e1767 | 2016-10-11 12:38:01 -0700 | [diff] [blame] | 202 | CPDFSDK_FormFillEnvironment* CJS_Runtime::GetFormFillEnv() const { |
Tom Sepez | 77f6d0f | 2017-02-23 12:14:10 -0800 | [diff] [blame] | 203 | return m_pFormFillEnv.Get(); |
weili | 625ad66 | 2016-06-15 11:21:33 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 206 | int CJS_Runtime::ExecuteScript(const WideString& script, WideString* info) { |
Tom Sepez | 3342090 | 2015-10-13 15:00:10 -0700 | [diff] [blame] | 207 | FXJSErr error = {}; |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 208 | int nRet = Execute(script, &error); |
Tom Sepez | 3342090 | 2015-10-13 15:00:10 -0700 | [diff] [blame] | 209 | if (nRet < 0) { |
| 210 | info->Format(L"[ Line: %05d { %s } ] : %s", error.linnum - 1, error.srcline, |
| 211 | error.message); |
| 212 | } |
| 213 | return nRet; |
| 214 | } |
| 215 | |
Tom Sepez | 5d0e843 | 2015-09-22 15:50:03 -0700 | [diff] [blame] | 216 | bool CJS_Runtime::AddEventToSet(const FieldEvent& event) { |
| 217 | return m_FieldEventSet.insert(event).second; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Tom Sepez | 5d0e843 | 2015-09-22 15:50:03 -0700 | [diff] [blame] | 220 | void CJS_Runtime::RemoveEventFromSet(const FieldEvent& event) { |
| 221 | m_FieldEventSet.erase(event); |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 224 | #ifdef PDF_ENABLE_XFA |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 225 | WideString ChangeObjName(const WideString& str) { |
| 226 | WideString sRet = str; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 227 | sRet.Replace(L"_", L"."); |
| 228 | return sRet; |
John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 229 | } |
Tom Sepez | 33b42e4 | 2017-07-19 13:19:12 -0700 | [diff] [blame] | 230 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 231 | bool CJS_Runtime::GetValueByName(const ByteStringView& utf8Name, |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 232 | CFXJSE_Value* pValue) { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 233 | v8::Isolate::Scope isolate_scope(GetIsolate()); |
| 234 | v8::HandleScope handle_scope(GetIsolate()); |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 235 | v8::Local<v8::Context> context = NewLocalContext(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 236 | v8::Context::Scope context_scope(context); |
Tom Sepez | 33b42e4 | 2017-07-19 13:19:12 -0700 | [diff] [blame] | 237 | v8::Local<v8::Value> propvalue = context->Global()->Get( |
| 238 | v8::String::NewFromUtf8(GetIsolate(), utf8Name.unterminated_c_str(), |
| 239 | v8::String::kNormalString, utf8Name.GetLength())); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 240 | if (propvalue.IsEmpty()) { |
dsinclair | f27aeec | 2016-06-07 19:36:18 -0700 | [diff] [blame] | 241 | pValue->SetUndefined(); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 242 | return false; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 243 | } |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 244 | pValue->ForceSetValue(propvalue); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 245 | return true; |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 246 | } |
Tom Sepez | 33b42e4 | 2017-07-19 13:19:12 -0700 | [diff] [blame] | 247 | |
Ryan Harrison | 275e260 | 2017-09-18 14:23:18 -0400 | [diff] [blame] | 248 | bool CJS_Runtime::SetValueByName(const ByteStringView& utf8Name, |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 249 | CFXJSE_Value* pValue) { |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 250 | if (utf8Name.IsEmpty() || !pValue) |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 251 | return false; |
Tom Sepez | 33b42e4 | 2017-07-19 13:19:12 -0700 | [diff] [blame] | 252 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 253 | v8::Isolate* pIsolate = GetIsolate(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 254 | v8::Isolate::Scope isolate_scope(pIsolate); |
| 255 | v8::HandleScope handle_scope(pIsolate); |
tsepez | b469424 | 2016-08-15 16:44:55 -0700 | [diff] [blame] | 256 | v8::Local<v8::Context> context = NewLocalContext(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 257 | v8::Context::Scope context_scope(context); |
dsinclair | 12a6b0c | 2016-05-26 11:14:08 -0700 | [diff] [blame] | 258 | v8::Local<v8::Value> propvalue = |
| 259 | v8::Local<v8::Value>::New(GetIsolate(), pValue->DirectGetValue()); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 260 | context->Global()->Set( |
Tom Sepez | 33b42e4 | 2017-07-19 13:19:12 -0700 | [diff] [blame] | 261 | v8::String::NewFromUtf8(pIsolate, utf8Name.unterminated_c_str(), |
| 262 | v8::String::kNormalString, utf8Name.GetLength()), |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 263 | propvalue); |
tsepez | 4cf5515 | 2016-11-02 14:37:54 -0700 | [diff] [blame] | 264 | return true; |
Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 265 | } |
Tom Sepez | 51da093 | 2015-11-25 16:05:49 -0800 | [diff] [blame] | 266 | #endif |
Dan Sinclair | e85107b | 2017-10-24 15:29:22 -0400 | [diff] [blame] | 267 | |
| 268 | v8::Local<v8::Value> CJS_Runtime::MaybeCoerceToNumber( |
dan sinclair | 80435cb | 2017-10-24 21:40:24 -0400 | [diff] [blame] | 269 | v8::Local<v8::Value> value) { |
Dan Sinclair | e85107b | 2017-10-24 15:29:22 -0400 | [diff] [blame] | 270 | bool bAllowNaN = false; |
| 271 | if (value->IsString()) { |
| 272 | ByteString bstr = ByteString::FromUnicode(ToWideString(value)); |
| 273 | if (bstr.GetLength() == 0) |
| 274 | return value; |
| 275 | if (bstr == "NaN") |
| 276 | bAllowNaN = true; |
| 277 | } |
| 278 | |
| 279 | v8::Isolate* pIsolate = GetIsolate(); |
| 280 | v8::TryCatch try_catch(pIsolate); |
| 281 | v8::MaybeLocal<v8::Number> maybeNum = |
| 282 | value->ToNumber(pIsolate->GetCurrentContext()); |
| 283 | if (maybeNum.IsEmpty()) |
| 284 | return value; |
| 285 | |
| 286 | v8::Local<v8::Number> num = maybeNum.ToLocalChecked(); |
| 287 | if (std::isnan(num->Value()) && !bAllowNaN) |
| 288 | return value; |
| 289 | |
| 290 | return num; |
| 291 | } |