| 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 |  | 
| Tom Sepez | 3745841 | 2015-10-06 11:33:46 -0700 | [diff] [blame] | 7 | #include "JS_Runtime.h" | 
|  | 8 |  | 
| Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 9 | #include "../../../xfa/src/fxjse/src/value.h" | 
| Tom Sepez | 3745841 | 2015-10-06 11:33:46 -0700 | [diff] [blame] | 10 | #include "../../include/fpdfxfa/fpdfxfa_app.h" | 
|  | 11 | #include "../../include/fsdk_mgr.h"  // For CPDFDoc_Environment. | 
|  | 12 | #include "../../include/javascript/IJavaScript.h" | 
|  | 13 | #include "Consts.h" | 
|  | 14 | #include "Document.h" | 
|  | 15 | #include "Field.h" | 
|  | 16 | #include "Icon.h" | 
|  | 17 | #include "JS_Context.h" | 
|  | 18 | #include "JS_Define.h" | 
|  | 19 | #include "JS_EventHandler.h" | 
|  | 20 | #include "JS_GlobalData.h" | 
|  | 21 | #include "JS_Object.h" | 
|  | 22 | #include "JS_Value.h" | 
|  | 23 | #include "PublicMethods.h" | 
|  | 24 | #include "app.h" | 
|  | 25 | #include "color.h" | 
|  | 26 | #include "console.h" | 
|  | 27 | #include "event.h" | 
|  | 28 | #include "global.h" | 
|  | 29 | #include "report.h" | 
|  | 30 | #include "util.h" | 
| John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 31 |  | 
| John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 32 | /* ------------------------------ CJS_Runtime ------------------------------ */ | 
| Tom Sepez | 808a99e | 2015-09-10 12:28:37 -0700 | [diff] [blame] | 33 | v8::Global<v8::ObjectTemplate>& _getGlobalObjectTemplate(v8::Isolate* pIsolate); | 
|  | 34 |  | 
| Tom Sepez | 3745841 | 2015-10-06 11:33:46 -0700 | [diff] [blame] | 35 | IFXJS_Runtime* IFXJS_Runtime::Create(CPDFDoc_Environment* pEnv) { | 
|  | 36 | return new CJS_Runtime(pEnv); | 
|  | 37 | } | 
|  | 38 |  | 
| Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 39 | CJS_Runtime::CJS_Runtime(CPDFDoc_Environment* pApp) | 
|  | 40 | : m_pApp(pApp), | 
|  | 41 | m_pDocument(NULL), | 
|  | 42 | m_bBlocking(FALSE), | 
| Jochen Eisinger | 2900784 | 2015-08-05 09:02:13 +0200 | [diff] [blame] | 43 | m_isolate(NULL), | 
|  | 44 | m_isolateManaged(false) { | 
| Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 45 | if (CPDFXFA_App::GetInstance()->GetJSERuntime()) { | 
|  | 46 | // TODO(tsepez): CPDFXFA_App should also use the embedder provided isolate. | 
|  | 47 | m_isolate = (v8::Isolate*)CPDFXFA_App::GetInstance()->GetJSERuntime(); | 
| Tom Sepez | a72e8e2 | 2015-10-07 10:17:53 -0700 | [diff] [blame^] | 48 | } else { | 
|  | 49 | IPDF_JSPLATFORM* pPlatform = m_pApp->GetFormFillInfo()->m_pJsPlatform; | 
|  | 50 | if (pPlatform->version <= 2) { | 
|  | 51 | unsigned int embedderDataSlot = 0; | 
|  | 52 | v8::Isolate* pExternalIsolate = nullptr; | 
|  | 53 | if (pPlatform->version == 2) { | 
|  | 54 | pExternalIsolate = reinterpret_cast<v8::Isolate*>(pPlatform->m_isolate); | 
|  | 55 | embedderDataSlot = pPlatform->m_v8EmbedderSlot; | 
|  | 56 | } | 
|  | 57 | FXJS_Initialize(embedderDataSlot, pExternalIsolate); | 
|  | 58 | } | 
|  | 59 | m_isolateManaged = FXJS_GetIsolate(&m_isolate); | 
| Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 60 | } | 
| Tom Sepez | d2cc1b9 | 2015-04-30 15:19:03 -0700 | [diff] [blame] | 61 |  | 
| Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 62 | v8::Isolate* isolate = m_isolate; | 
|  | 63 | v8::Isolate::Scope isolate_scope(isolate); | 
|  | 64 | v8::Locker locker(isolate); | 
|  | 65 | v8::HandleScope handle_scope(isolate); | 
| Tom Sepez | ed7b2b5 | 2015-09-22 08:36:17 -0700 | [diff] [blame] | 66 | if (CPDFXFA_App::GetInstance()->IsJavaScriptInitialized()) { | 
| Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 67 | CJS_Context* pContext = (CJS_Context*)NewContext(); | 
| Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 68 | FXJS_InitializeRuntime(GetIsolate(), this, pContext, m_context); | 
| Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 69 | ReleaseContext(pContext); | 
| Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 70 | return; | 
|  | 71 | } | 
|  | 72 |  | 
| Tom Sepez | 570875c | 2015-09-11 08:35:03 -0700 | [diff] [blame] | 73 | DefineJSObjects(); | 
| Tom Sepez | ed7b2b5 | 2015-09-22 08:36:17 -0700 | [diff] [blame] | 74 | CPDFXFA_App::GetInstance()->SetJavaScriptInitialized(TRUE); | 
| Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 75 |  | 
|  | 76 | CJS_Context* pContext = (CJS_Context*)NewContext(); | 
| Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 77 | FXJS_InitializeRuntime(GetIsolate(), this, pContext, m_context); | 
| Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 78 | ReleaseContext(pContext); | 
| John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 79 | } | 
|  | 80 |  | 
| Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 81 | CJS_Runtime::~CJS_Runtime() { | 
| Lei Zhang | 2d5a0e1 | 2015-10-05 17:00:03 -0700 | [diff] [blame] | 82 | for (auto* obs : m_observers) | 
|  | 83 | obs->OnDestroyed(); | 
|  | 84 |  | 
| Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 85 | int size = m_ContextArray.GetSize(); | 
|  | 86 | for (int i = 0; i < size; i++) | 
|  | 87 | delete m_ContextArray.GetAt(i); | 
| John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 88 |  | 
| Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 89 | m_ContextArray.RemoveAll(); | 
| John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 90 |  | 
| Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 91 | m_pApp = NULL; | 
|  | 92 | m_pDocument = NULL; | 
| Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 93 | m_context.Reset(); | 
| John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 94 |  | 
| Jochen Eisinger | 2900784 | 2015-08-05 09:02:13 +0200 | [diff] [blame] | 95 | if (m_isolateManaged) | 
|  | 96 | m_isolate->Dispose(); | 
| Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 97 | m_isolate = NULL; | 
| John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 98 | } | 
|  | 99 |  | 
| Tom Sepez | 142165e | 2015-09-11 13:21:50 -0700 | [diff] [blame] | 100 | void CJS_Runtime::DefineJSObjects() { | 
| Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 101 | v8::Isolate::Scope isolate_scope(GetIsolate()); | 
|  | 102 | v8::Locker locker(GetIsolate()); | 
|  | 103 | v8::HandleScope handle_scope(GetIsolate()); | 
|  | 104 | v8::Local<v8::Context> context = v8::Context::New(GetIsolate()); | 
|  | 105 | v8::Context::Scope context_scope(context); | 
| Tom Sepez | 570875c | 2015-09-11 08:35:03 -0700 | [diff] [blame] | 106 |  | 
|  | 107 | // The call order determines the "ObjDefID" assigned to each class. | 
|  | 108 | // ObjDefIDs 0 - 2 | 
| Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame] | 109 | CJS_Border::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); | 
|  | 110 | CJS_Display::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); | 
|  | 111 | CJS_Font::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); | 
| John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 112 |  | 
| Tom Sepez | 570875c | 2015-09-11 08:35:03 -0700 | [diff] [blame] | 113 | // ObjDefIDs 3 - 5 | 
| Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame] | 114 | CJS_Highlight::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); | 
|  | 115 | CJS_Position::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); | 
|  | 116 | CJS_ScaleHow::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); | 
| John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 117 |  | 
| Tom Sepez | 570875c | 2015-09-11 08:35:03 -0700 | [diff] [blame] | 118 | // ObjDefIDs 6 - 8 | 
| Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame] | 119 | CJS_ScaleWhen::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); | 
|  | 120 | CJS_Style::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); | 
|  | 121 | CJS_Zoomtype::DefineJSObjects(GetIsolate(), 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 9 - 11 | 
| Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame] | 124 | CJS_App::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); | 
|  | 125 | CJS_Color::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); | 
|  | 126 | CJS_Console::DefineJSObjects(GetIsolate(), 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 12 - 14 | 
| Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame] | 129 | CJS_Document::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_GLOBAL); | 
|  | 130 | CJS_Event::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); | 
|  | 131 | CJS_Field::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_DYNAMIC); | 
| 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 15 - 17 | 
| Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame] | 134 | CJS_Global::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); | 
|  | 135 | CJS_Icon::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_DYNAMIC); | 
|  | 136 | CJS_Util::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_STATIC); | 
| Tom Sepez | 570875c | 2015-09-11 08:35:03 -0700 | [diff] [blame] | 137 |  | 
| Tom Sepez | 142165e | 2015-09-11 13:21:50 -0700 | [diff] [blame] | 138 | // ObjDefIDs 18 - 20 (these can't fail, return void). | 
|  | 139 | CJS_PublicMethods::DefineJSObjects(GetIsolate()); | 
|  | 140 | CJS_GlobalConsts::DefineJSObjects(GetIsolate()); | 
|  | 141 | CJS_GlobalArrays::DefineJSObjects(GetIsolate()); | 
| Tom Sepez | 570875c | 2015-09-11 08:35:03 -0700 | [diff] [blame] | 142 |  | 
| Tom Sepez | 142165e | 2015-09-11 13:21:50 -0700 | [diff] [blame] | 143 | // ObjDefIDs 21 - 22. | 
| Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame] | 144 | CJS_TimerObj::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_DYNAMIC); | 
|  | 145 | CJS_PrintParamsObj::DefineJSObjects(GetIsolate(), FXJSOBJTYPE_DYNAMIC); | 
| John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 146 | } | 
|  | 147 |  | 
| Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 148 | IFXJS_Context* CJS_Runtime::NewContext() { | 
|  | 149 | CJS_Context* p = new CJS_Context(this); | 
|  | 150 | m_ContextArray.Add(p); | 
|  | 151 | return p; | 
| John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 152 | } | 
|  | 153 |  | 
| Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 154 | void CJS_Runtime::ReleaseContext(IFXJS_Context* pContext) { | 
|  | 155 | CJS_Context* pJSContext = (CJS_Context*)pContext; | 
| John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 156 |  | 
| Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 157 | for (int i = 0, sz = m_ContextArray.GetSize(); i < sz; i++) { | 
|  | 158 | if (pJSContext == m_ContextArray.GetAt(i)) { | 
|  | 159 | delete pJSContext; | 
|  | 160 | m_ContextArray.RemoveAt(i); | 
|  | 161 | break; | 
|  | 162 | } | 
|  | 163 | } | 
|  | 164 | } | 
|  | 165 |  | 
|  | 166 | IFXJS_Context* CJS_Runtime::GetCurrentContext() { | 
|  | 167 | if (!m_ContextArray.GetSize()) | 
|  | 168 | return NULL; | 
|  | 169 | return m_ContextArray.GetAt(m_ContextArray.GetSize() - 1); | 
|  | 170 | } | 
|  | 171 |  | 
|  | 172 | void CJS_Runtime::SetReaderDocument(CPDFSDK_Document* pReaderDoc) { | 
|  | 173 | if (m_pDocument != pReaderDoc) { | 
|  | 174 | v8::Isolate::Scope isolate_scope(m_isolate); | 
|  | 175 | v8::Locker locker(m_isolate); | 
|  | 176 | v8::HandleScope handle_scope(m_isolate); | 
|  | 177 | v8::Local<v8::Context> context = | 
|  | 178 | v8::Local<v8::Context>::New(m_isolate, m_context); | 
|  | 179 | v8::Context::Scope context_scope(context); | 
|  | 180 |  | 
|  | 181 | m_pDocument = pReaderDoc; | 
| Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 182 | if (pReaderDoc) { | 
| Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 183 | v8::Local<v8::Object> pThis = FXJS_GetThisObj(GetIsolate()); | 
| Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 184 | if (!pThis.IsEmpty()) { | 
| Tom Sepez | cd56a7d | 2015-10-06 11:45:28 -0700 | [diff] [blame] | 185 | if (FXJS_GetObjDefnID(pThis) == CJS_Document::g_nObjDefnID) { | 
| Tom Sepez | 39bfe12 | 2015-09-17 15:25:23 -0700 | [diff] [blame] | 186 | if (CJS_Document* pJSDocument = | 
| Tom Sepez | d5a0e95 | 2015-09-17 15:40:06 -0700 | [diff] [blame] | 187 | (CJS_Document*)FXJS_GetPrivate(GetIsolate(), pThis)) { | 
| Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 188 | if (Document* pDocument = (Document*)pJSDocument->GetEmbedObject()) | 
|  | 189 | pDocument->AttachDoc(pReaderDoc); | 
|  | 190 | } | 
| Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 191 | } | 
| Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 192 | } | 
| Tom Sepez | 2f2ffec | 2015-07-23 14:42:09 -0700 | [diff] [blame] | 193 | } | 
| Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 194 | } | 
| John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 195 | } | 
|  | 196 |  | 
| Tom Sepez | 5d0e843 | 2015-09-22 15:50:03 -0700 | [diff] [blame] | 197 | bool CJS_Runtime::AddEventToSet(const FieldEvent& event) { | 
|  | 198 | return m_FieldEventSet.insert(event).second; | 
| John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 199 | } | 
|  | 200 |  | 
| Tom Sepez | 5d0e843 | 2015-09-22 15:50:03 -0700 | [diff] [blame] | 201 | void CJS_Runtime::RemoveEventFromSet(const FieldEvent& event) { | 
|  | 202 | m_FieldEventSet.erase(event); | 
| John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 203 | } | 
|  | 204 |  | 
| Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 205 | v8::Local<v8::Context> CJS_Runtime::NewJSContext() { | 
|  | 206 | return v8::Local<v8::Context>::New(m_isolate, m_context); | 
| John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 207 | } | 
|  | 208 |  | 
| Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 209 | CFX_WideString ChangeObjName(const CFX_WideString& str) { | 
|  | 210 | CFX_WideString sRet = str; | 
|  | 211 | sRet.Replace(L"_", L"."); | 
|  | 212 | return sRet; | 
| John Abd-El-Malek | 3f3b45c | 2014-05-23 17:28:10 -0700 | [diff] [blame] | 213 | } | 
| Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 214 | FX_BOOL CJS_Runtime::GetHValueByName(const CFX_ByteStringC& utf8Name, | 
|  | 215 | FXJSE_HVALUE hValue) { | 
|  | 216 | const FX_CHAR* name = utf8Name.GetCStr(); | 
| Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 217 |  | 
| Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 218 | v8::Locker lock(GetIsolate()); | 
|  | 219 | v8::Isolate::Scope isolate_scope(GetIsolate()); | 
|  | 220 | v8::HandleScope handle_scope(GetIsolate()); | 
|  | 221 | v8::Local<v8::Context> context = | 
|  | 222 | v8::Local<v8::Context>::New(GetIsolate(), m_context); | 
|  | 223 | v8::Context::Scope context_scope(context); | 
| Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 224 |  | 
| Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 225 | // v8::Local<v8::Context> tmpCotext = | 
|  | 226 | // v8::Local<v8::Context>::New(GetIsolate(), m_context); | 
|  | 227 | v8::Local<v8::Value> propvalue = | 
|  | 228 | context->Global()->Get(v8::String::NewFromUtf8( | 
|  | 229 | GetIsolate(), name, v8::String::kNormalString, utf8Name.GetLength())); | 
| Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 230 |  | 
| Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 231 | if (propvalue.IsEmpty()) { | 
|  | 232 | FXJSE_Value_SetUndefined(hValue); | 
|  | 233 | return FALSE; | 
|  | 234 | } | 
|  | 235 | ((CFXJSE_Value*)hValue)->ForceSetValue(propvalue); | 
| Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 236 |  | 
| Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 237 | return TRUE; | 
| Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 238 | } | 
| Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 239 | FX_BOOL CJS_Runtime::SetHValueByName(const CFX_ByteStringC& utf8Name, | 
|  | 240 | FXJSE_HVALUE hValue) { | 
|  | 241 | if (utf8Name.IsEmpty() || hValue == NULL) | 
|  | 242 | return FALSE; | 
|  | 243 | const FX_CHAR* name = utf8Name.GetCStr(); | 
|  | 244 | v8::Isolate* pIsolate = GetIsolate(); | 
|  | 245 | v8::Locker lock(pIsolate); | 
|  | 246 | v8::Isolate::Scope isolate_scope(pIsolate); | 
|  | 247 | v8::HandleScope handle_scope(pIsolate); | 
|  | 248 | v8::Local<v8::Context> context = | 
|  | 249 | v8::Local<v8::Context>::New(pIsolate, m_context); | 
|  | 250 | v8::Context::Scope context_scope(context); | 
| Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 251 |  | 
| Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 252 | // v8::Local<v8::Context> tmpCotext = | 
|  | 253 | // v8::Local<v8::Context>::New(GetIsolate(), m_context); | 
|  | 254 | v8::Local<v8::Value> propvalue = v8::Local<v8::Value>::New( | 
|  | 255 | GetIsolate(), ((CFXJSE_Value*)hValue)->DirectGetValue()); | 
|  | 256 | context->Global()->Set( | 
|  | 257 | v8::String::NewFromUtf8(pIsolate, name, v8::String::kNormalString, | 
|  | 258 | utf8Name.GetLength()), | 
|  | 259 | propvalue); | 
| Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 260 |  | 
| Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 261 | return TRUE; | 
| Bo Xu | fdc00a7 | 2014-10-28 23:03:33 -0700 | [diff] [blame] | 262 | } | 
| Lei Zhang | 2d5a0e1 | 2015-10-05 17:00:03 -0700 | [diff] [blame] | 263 |  | 
|  | 264 | void CJS_Runtime::AddObserver(Observer* observer) { | 
|  | 265 | ASSERT(m_observers.find(observer) == m_observers.end()); | 
|  | 266 | m_observers.insert(observer); | 
|  | 267 | } | 
|  | 268 |  | 
|  | 269 | void CJS_Runtime::RemoveObserver(Observer* observer) { | 
|  | 270 | ASSERT(m_observers.find(observer) != m_observers.end()); | 
|  | 271 | m_observers.erase(observer); | 
|  | 272 | } |