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