John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -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 | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 5 | // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | |
| 7 | #include "../../../core/include/fxcrt/fx_basic.h" |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 8 | #include "../../../core/include/fxcrt/fx_ext.h" |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 9 | #include "../../include/jsapi/fxjs_v8.h" |
| 10 | #include "../../include/fsdk_define.h" |
| 11 | #include "time.h" |
| 12 | #include <cmath> |
| 13 | #include <limits> |
| 14 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 15 | #define VALUE_NAME_STRING L"string" |
| 16 | #define VALUE_NAME_NUMBER L"number" |
| 17 | #define VALUE_NAME_BOOLEAN L"boolean" |
| 18 | #define VALUE_NAME_DATE L"date" |
| 19 | #define VALUE_NAME_OBJECT L"object" |
| 20 | #define VALUE_NAME_FXOBJ L"fxobj" |
| 21 | #define VALUE_NAME_NULL L"null" |
| 22 | #define VALUE_NAME_UNDEFINED L"undefined" |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 23 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 24 | const static FX_DWORD g_nan[2] = {0, 0x7FF80000}; |
| 25 | static double GetNan() { |
Bruce Dawson | 87e9598 | 2015-01-02 12:05:17 -0800 | [diff] [blame] | 26 | return *(double*)g_nan; |
| 27 | } |
Jochen Eisinger | 06b6002 | 2015-07-30 17:44:35 +0200 | [diff] [blame] | 28 | static unsigned int g_embedderDataSlot = 0u; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 29 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 30 | class CJS_PrivateData { |
| 31 | public: |
| 32 | CJS_PrivateData() : ObjDefID(-1), pPrivate(NULL) {} |
| 33 | int ObjDefID; |
| 34 | void* pPrivate; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 35 | }; |
| 36 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 37 | class CJS_ObjDefintion { |
| 38 | public: |
| 39 | CJS_ObjDefintion(v8::Isolate* isolate, |
| 40 | const wchar_t* sObjName, |
| 41 | FXJSOBJTYPE eObjType, |
| 42 | LP_CONSTRUCTOR pConstructor, |
| 43 | LP_DESTRUCTOR pDestructor) |
| 44 | : objName(sObjName), |
| 45 | objType(eObjType), |
| 46 | m_pConstructor(pConstructor), |
| 47 | m_pDestructor(pDestructor), |
| 48 | m_bSetAsGlobalObject(FALSE) { |
| 49 | v8::Isolate::Scope isolate_scope(isolate); |
| 50 | v8::HandleScope handle_scope(isolate); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 51 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 52 | v8::Local<v8::ObjectTemplate> objTemplate = |
| 53 | v8::ObjectTemplate::New(isolate); |
| 54 | objTemplate->SetInternalFieldCount(2); |
| 55 | m_objTemplate.Reset(isolate, objTemplate); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 56 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 57 | // Document as the global object. |
| 58 | if (FXSYS_wcscmp(sObjName, L"Document") == 0) { |
| 59 | m_bSetAsGlobalObject = TRUE; |
| 60 | } |
| 61 | } |
| 62 | ~CJS_ObjDefintion() { |
| 63 | m_objTemplate.Reset(); |
| 64 | m_StaticObj.Reset(); |
| 65 | } |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 66 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 67 | public: |
| 68 | const wchar_t* objName; |
| 69 | FXJSOBJTYPE objType; |
| 70 | LP_CONSTRUCTOR m_pConstructor; |
| 71 | LP_DESTRUCTOR m_pDestructor; |
| 72 | FX_BOOL m_bSetAsGlobalObject; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 73 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 74 | v8::Global<v8::ObjectTemplate> m_objTemplate; |
| 75 | v8::Global<v8::Object> m_StaticObj; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 76 | }; |
| 77 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 78 | int JS_DefineObj(IJS_Runtime* pJSRuntime, |
| 79 | const wchar_t* sObjName, |
| 80 | FXJSOBJTYPE eObjType, |
| 81 | LP_CONSTRUCTOR pConstructor, |
| 82 | LP_DESTRUCTOR pDestructor) { |
| 83 | v8::Isolate* isolate = (v8::Isolate*)pJSRuntime; |
| 84 | v8::Isolate::Scope isolate_scope(isolate); |
| 85 | v8::HandleScope handle_scope(isolate); |
| 86 | CFX_PtrArray* pArray = (CFX_PtrArray*)isolate->GetData(g_embedderDataSlot); |
| 87 | if (!pArray) { |
Tom Sepez | ae51c81 | 2015-08-05 12:34:06 -0700 | [diff] [blame] | 88 | pArray = new CFX_PtrArray(); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 89 | isolate->SetData(g_embedderDataSlot, pArray); |
| 90 | } |
Nico Weber | 077f1a3 | 2015-08-06 15:08:57 -0700 | [diff] [blame] | 91 | CJS_ObjDefintion* pObjDef = new CJS_ObjDefintion(isolate, sObjName, eObjType, |
| 92 | pConstructor, pDestructor); |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 93 | pArray->Add(pObjDef); |
| 94 | return pArray->GetSize() - 1; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 97 | int JS_DefineObjMethod(IJS_Runtime* pJSRuntime, |
| 98 | int nObjDefnID, |
| 99 | const wchar_t* sMethodName, |
| 100 | v8::FunctionCallback pMethodCall) { |
| 101 | v8::Isolate* isolate = (v8::Isolate*)pJSRuntime; |
| 102 | v8::Isolate::Scope isolate_scope(isolate); |
| 103 | v8::HandleScope handle_scope(isolate); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 104 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 105 | CFX_WideString ws = CFX_WideString(sMethodName); |
| 106 | CFX_ByteString bsMethodName = ws.UTF8Encode(); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 107 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 108 | CFX_PtrArray* pArray = (CFX_PtrArray*)isolate->GetData(g_embedderDataSlot); |
| 109 | if (!pArray) |
| 110 | return 0; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 111 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 112 | if (nObjDefnID < 0 || nObjDefnID >= pArray->GetSize()) |
| 113 | return 0; |
| 114 | CJS_ObjDefintion* pObjDef = (CJS_ObjDefintion*)pArray->GetAt(nObjDefnID); |
| 115 | v8::Local<v8::ObjectTemplate> objTemp = |
| 116 | v8::Local<v8::ObjectTemplate>::New(isolate, pObjDef->m_objTemplate); |
| 117 | objTemp->Set(v8::String::NewFromUtf8(isolate, bsMethodName.c_str(), |
| 118 | v8::NewStringType::kNormal) |
| 119 | .ToLocalChecked(), |
| 120 | v8::FunctionTemplate::New(isolate, pMethodCall), v8::ReadOnly); |
| 121 | pObjDef->m_objTemplate.Reset(isolate, objTemp); |
| 122 | return 0; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 123 | } |
| 124 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 125 | int JS_DefineObjProperty(IJS_Runtime* pJSRuntime, |
| 126 | int nObjDefnID, |
| 127 | const wchar_t* sPropName, |
| 128 | v8::AccessorGetterCallback pPropGet, |
| 129 | v8::AccessorSetterCallback pPropPut) { |
| 130 | v8::Isolate* isolate = (v8::Isolate*)pJSRuntime; |
| 131 | v8::Isolate::Scope isolate_scope(isolate); |
| 132 | v8::HandleScope handle_scope(isolate); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 133 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 134 | CFX_WideString ws = CFX_WideString(sPropName); |
| 135 | CFX_ByteString bsPropertyName = ws.UTF8Encode(); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 136 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 137 | CFX_PtrArray* pArray = (CFX_PtrArray*)isolate->GetData(g_embedderDataSlot); |
| 138 | if (!pArray) |
| 139 | return 0; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 140 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 141 | if (nObjDefnID < 0 || nObjDefnID >= pArray->GetSize()) |
| 142 | return 0; |
| 143 | CJS_ObjDefintion* pObjDef = (CJS_ObjDefintion*)pArray->GetAt(nObjDefnID); |
| 144 | v8::Local<v8::ObjectTemplate> objTemp = |
| 145 | v8::Local<v8::ObjectTemplate>::New(isolate, pObjDef->m_objTemplate); |
| 146 | objTemp->SetAccessor(v8::String::NewFromUtf8(isolate, bsPropertyName.c_str(), |
| 147 | v8::NewStringType::kNormal) |
| 148 | .ToLocalChecked(), |
| 149 | pPropGet, pPropPut); |
| 150 | pObjDef->m_objTemplate.Reset(isolate, objTemp); |
| 151 | return 0; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 154 | int JS_DefineObjAllProperties(IJS_Runtime* pJSRuntime, |
| 155 | int nObjDefnID, |
| 156 | v8::NamedPropertyQueryCallback pPropQurey, |
| 157 | v8::NamedPropertyGetterCallback pPropGet, |
| 158 | v8::NamedPropertySetterCallback pPropPut, |
| 159 | v8::NamedPropertyDeleterCallback pPropDel) { |
| 160 | v8::Isolate* isolate = (v8::Isolate*)pJSRuntime; |
| 161 | v8::Isolate::Scope isolate_scope(isolate); |
| 162 | v8::HandleScope handle_scope(isolate); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 163 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 164 | CFX_PtrArray* pArray = (CFX_PtrArray*)isolate->GetData(g_embedderDataSlot); |
| 165 | if (!pArray) |
| 166 | return 0; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 167 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 168 | if (nObjDefnID < 0 || nObjDefnID >= pArray->GetSize()) |
| 169 | return 0; |
| 170 | CJS_ObjDefintion* pObjDef = (CJS_ObjDefintion*)pArray->GetAt(nObjDefnID); |
| 171 | v8::Local<v8::ObjectTemplate> objTemp = |
| 172 | v8::Local<v8::ObjectTemplate>::New(isolate, pObjDef->m_objTemplate); |
| 173 | objTemp->SetNamedPropertyHandler(pPropGet, pPropPut, pPropQurey, pPropDel); |
| 174 | pObjDef->m_objTemplate.Reset(isolate, objTemp); |
| 175 | return 0; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 176 | } |
| 177 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 178 | int JS_DefineObjConst(IJS_Runtime* pJSRuntime, |
| 179 | int nObjDefnID, |
| 180 | const wchar_t* sConstName, |
| 181 | v8::Local<v8::Value> pDefault) { |
| 182 | v8::Isolate* isolate = (v8::Isolate*)pJSRuntime; |
| 183 | v8::Isolate::Scope isolate_scope(isolate); |
| 184 | v8::HandleScope handle_scope(isolate); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 185 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 186 | CFX_PtrArray* pArray = (CFX_PtrArray*)isolate->GetData(g_embedderDataSlot); |
| 187 | if (!pArray) |
| 188 | return 0; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 189 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 190 | CFX_WideString ws = CFX_WideString(sConstName); |
| 191 | CFX_ByteString bsConstName = ws.UTF8Encode(); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 192 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 193 | if (nObjDefnID < 0 || nObjDefnID >= pArray->GetSize()) |
| 194 | return 0; |
| 195 | CJS_ObjDefintion* pObjDef = (CJS_ObjDefintion*)pArray->GetAt(nObjDefnID); |
| 196 | v8::Local<v8::ObjectTemplate> objTemp = |
| 197 | v8::Local<v8::ObjectTemplate>::New(isolate, pObjDef->m_objTemplate); |
| 198 | objTemp->Set(isolate, bsConstName.c_str(), pDefault); |
| 199 | pObjDef->m_objTemplate.Reset(isolate, objTemp); |
| 200 | return 0; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 203 | static v8::Global<v8::ObjectTemplate>& _getGlobalObjectTemplate( |
| 204 | IJS_Runtime* pJSRuntime) { |
| 205 | v8::Isolate* isolate = (v8::Isolate*)pJSRuntime; |
| 206 | v8::Isolate::Scope isolate_scope(isolate); |
| 207 | v8::HandleScope handle_scope(isolate); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 208 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 209 | CFX_PtrArray* pArray = (CFX_PtrArray*)isolate->GetData(g_embedderDataSlot); |
| 210 | ASSERT(pArray != NULL); |
| 211 | for (int i = 0; i < pArray->GetSize(); i++) { |
| 212 | CJS_ObjDefintion* pObjDef = (CJS_ObjDefintion*)pArray->GetAt(i); |
| 213 | if (pObjDef->m_bSetAsGlobalObject) |
| 214 | return pObjDef->m_objTemplate; |
| 215 | } |
| 216 | static v8::Global<v8::ObjectTemplate> gloabalObjectTemplate; |
| 217 | return gloabalObjectTemplate; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 220 | int JS_DefineGlobalMethod(IJS_Runtime* pJSRuntime, |
| 221 | const wchar_t* sMethodName, |
| 222 | v8::FunctionCallback pMethodCall) { |
| 223 | v8::Isolate* isolate = (v8::Isolate*)pJSRuntime; |
| 224 | v8::Isolate::Scope isolate_scope(isolate); |
| 225 | v8::HandleScope handle_scope(isolate); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 226 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 227 | CFX_WideString ws = CFX_WideString(sMethodName); |
| 228 | CFX_ByteString bsMethodName = ws.UTF8Encode(); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 229 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 230 | v8::Local<v8::FunctionTemplate> funTempl = |
| 231 | v8::FunctionTemplate::New(isolate, pMethodCall); |
| 232 | v8::Local<v8::ObjectTemplate> objTemp; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 233 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 234 | v8::Global<v8::ObjectTemplate>& globalObjTemp = |
| 235 | _getGlobalObjectTemplate(pJSRuntime); |
| 236 | if (globalObjTemp.IsEmpty()) |
| 237 | objTemp = v8::ObjectTemplate::New(isolate); |
| 238 | else |
| 239 | objTemp = v8::Local<v8::ObjectTemplate>::New(isolate, globalObjTemp); |
| 240 | objTemp->Set(v8::String::NewFromUtf8(isolate, bsMethodName.c_str(), |
| 241 | v8::NewStringType::kNormal) |
| 242 | .ToLocalChecked(), |
| 243 | funTempl, v8::ReadOnly); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 244 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 245 | globalObjTemp.Reset(isolate, objTemp); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 246 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 247 | return 0; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 248 | } |
| 249 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 250 | int JS_DefineGlobalConst(IJS_Runtime* pJSRuntime, |
| 251 | const wchar_t* sConstName, |
| 252 | v8::Local<v8::Value> pDefault) { |
| 253 | v8::Isolate* isolate = (v8::Isolate*)pJSRuntime; |
| 254 | v8::Isolate::Scope isolate_scope(isolate); |
| 255 | v8::HandleScope handle_scope(isolate); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 256 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 257 | CFX_WideString ws = CFX_WideString(sConstName); |
| 258 | CFX_ByteString bsConst = ws.UTF8Encode(); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 259 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 260 | v8::Local<v8::ObjectTemplate> objTemp; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 261 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 262 | v8::Global<v8::ObjectTemplate>& globalObjTemp = |
| 263 | _getGlobalObjectTemplate(pJSRuntime); |
| 264 | if (globalObjTemp.IsEmpty()) |
| 265 | objTemp = v8::ObjectTemplate::New(isolate); |
| 266 | else |
| 267 | objTemp = v8::Local<v8::ObjectTemplate>::New(isolate, globalObjTemp); |
| 268 | objTemp->Set(v8::String::NewFromUtf8(isolate, bsConst.c_str(), |
| 269 | v8::NewStringType::kNormal) |
| 270 | .ToLocalChecked(), |
| 271 | pDefault, v8::ReadOnly); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 272 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 273 | globalObjTemp.Reset(isolate, objTemp); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 274 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 275 | return 0; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 276 | } |
| 277 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 278 | void JS_InitialRuntime(IJS_Runtime* pJSRuntime, |
| 279 | IFXJS_Runtime* pFXRuntime, |
| 280 | IFXJS_Context* context, |
| 281 | v8::Global<v8::Context>& v8PersistentContext) { |
| 282 | v8::Isolate* isolate = (v8::Isolate*)pJSRuntime; |
| 283 | v8::Isolate::Scope isolate_scope(isolate); |
| 284 | v8::Locker locker(isolate); |
| 285 | v8::HandleScope handle_scope(isolate); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 286 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 287 | v8::Global<v8::ObjectTemplate>& globalObjTemp = |
| 288 | _getGlobalObjectTemplate(pJSRuntime); |
| 289 | v8::Local<v8::Context> v8Context = v8::Context::New( |
| 290 | isolate, NULL, |
| 291 | v8::Local<v8::ObjectTemplate>::New(isolate, globalObjTemp)); |
| 292 | v8::Context::Scope context_scope(v8Context); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 293 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 294 | // v8::Local<External> ptr = External::New(isolate, pFXRuntime); |
| 295 | // v8Context->SetEmbedderData(1, ptr); |
| 296 | // TODO(tsepez): Don't use more than one embedder data slot. |
| 297 | isolate->SetData(2, pFXRuntime); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 298 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 299 | CFX_PtrArray* pArray = (CFX_PtrArray*)isolate->GetData(g_embedderDataSlot); |
| 300 | if (!pArray) |
| 301 | return; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 302 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 303 | for (int i = 0; i < pArray->GetSize(); i++) { |
| 304 | CJS_ObjDefintion* pObjDef = (CJS_ObjDefintion*)pArray->GetAt(i); |
| 305 | CFX_WideString ws = CFX_WideString(pObjDef->objName); |
| 306 | CFX_ByteString bs = ws.UTF8Encode(); |
| 307 | v8::Local<v8::String> objName = |
| 308 | v8::String::NewFromUtf8(isolate, bs.c_str(), v8::NewStringType::kNormal, |
| 309 | bs.GetLength()) |
| 310 | .ToLocalChecked(); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 311 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 312 | if (pObjDef->objType == JS_DYNAMIC) { |
| 313 | // Document is set as global object, need to construct it first. |
| 314 | CFX_WideString wsString(L"Document"); |
| 315 | if (ws.Equal(wsString)) { |
Tom Sepez | ae51c81 | 2015-08-05 12:34:06 -0700 | [diff] [blame] | 316 | CJS_PrivateData* pPrivateData = new CJS_PrivateData; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 317 | pPrivateData->ObjDefID = i; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 318 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 319 | v8Context->Global() |
| 320 | ->GetPrototype() |
| 321 | ->ToObject(v8Context) |
| 322 | .ToLocalChecked() |
| 323 | ->SetAlignedPointerInInternalField(0, pPrivateData); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 324 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 325 | if (pObjDef->m_pConstructor) |
| 326 | pObjDef->m_pConstructor(context, v8Context->Global() |
| 327 | ->GetPrototype() |
| 328 | ->ToObject(v8Context) |
| 329 | .ToLocalChecked(), |
| 330 | v8Context->Global() |
| 331 | ->GetPrototype() |
| 332 | ->ToObject(v8Context) |
| 333 | .ToLocalChecked()); |
| 334 | } |
| 335 | } else { |
| 336 | v8::Local<v8::Object> obj = JS_NewFxDynamicObj(pJSRuntime, context, i); |
| 337 | v8Context->Global()->Set(v8Context, objName, obj).FromJust(); |
| 338 | pObjDef->m_StaticObj.Reset(isolate, obj); |
| 339 | } |
| 340 | } |
| 341 | v8PersistentContext.Reset(isolate, v8Context); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 342 | } |
| 343 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 344 | void JS_ReleaseRuntime(IJS_Runtime* pJSRuntime, |
| 345 | v8::Global<v8::Context>& v8PersistentContext) { |
| 346 | v8::Isolate* isolate = (v8::Isolate*)pJSRuntime; |
| 347 | v8::Isolate::Scope isolate_scope(isolate); |
| 348 | v8::Locker locker(isolate); |
| 349 | v8::HandleScope handle_scope(isolate); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 350 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 351 | CFX_PtrArray* pArray = (CFX_PtrArray*)isolate->GetData(g_embedderDataSlot); |
| 352 | if (!pArray) |
| 353 | return; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 354 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 355 | for (int i = 0; i < pArray->GetSize(); i++) { |
| 356 | CJS_ObjDefintion* pObjDef = (CJS_ObjDefintion*)pArray->GetAt(i); |
| 357 | if (!pObjDef->m_StaticObj.IsEmpty()) { |
| 358 | v8::Local<v8::Object> pObj = |
| 359 | v8::Local<v8::Object>::New(isolate, pObjDef->m_StaticObj); |
| 360 | if (pObjDef->m_pDestructor) |
| 361 | pObjDef->m_pDestructor(pObj); |
| 362 | JS_FreePrivate(pObj); |
| 363 | } |
| 364 | delete pObjDef; |
| 365 | } |
| 366 | delete pArray; |
| 367 | isolate->SetData(1, NULL); |
| 368 | isolate->SetData(g_embedderDataSlot, NULL); |
| 369 | // TODO(tsepez): Don't use more than one embedder data slot. |
| 370 | isolate->SetData(2, NULL); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 371 | } |
| 372 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 373 | void JS_Initial(unsigned int embedderDataSlot) { |
| 374 | g_embedderDataSlot = embedderDataSlot; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 375 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 376 | void JS_Release() {} |
| 377 | int JS_Parse(IJS_Runtime* pJSRuntime, |
| 378 | IFXJS_Context* pJSContext, |
| 379 | const wchar_t* script, |
| 380 | long length, |
| 381 | FXJSErr* perror) { |
| 382 | v8::Isolate* isolate = (v8::Isolate*)pJSRuntime; |
| 383 | v8::Isolate::Scope isolate_scope(isolate); |
| 384 | v8::TryCatch try_catch(isolate); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 385 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 386 | CFX_WideString wsScript(script); |
| 387 | CFX_ByteString bsScript = wsScript.UTF8Encode(); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 388 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 389 | v8::Local<v8::Context> context = isolate->GetCurrentContext(); |
| 390 | v8::Local<v8::Script> compiled_script; |
| 391 | if (!v8::Script::Compile(context, |
| 392 | v8::String::NewFromUtf8(isolate, bsScript.c_str(), |
| 393 | v8::NewStringType::kNormal, |
| 394 | bsScript.GetLength()) |
| 395 | .ToLocalChecked()) |
| 396 | .ToLocal(&compiled_script)) { |
| 397 | v8::String::Utf8Value error(try_catch.Exception()); |
| 398 | return -1; |
| 399 | } |
| 400 | return 0; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 401 | } |
| 402 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 403 | int JS_Execute(IJS_Runtime* pJSRuntime, |
| 404 | IFXJS_Context* pJSContext, |
| 405 | const wchar_t* script, |
| 406 | long length, |
| 407 | FXJSErr* perror) { |
| 408 | v8::Isolate* isolate = (v8::Isolate*)pJSRuntime; |
| 409 | v8::Isolate::Scope isolate_scope(isolate); |
| 410 | v8::TryCatch try_catch(isolate); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 411 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 412 | CFX_WideString wsScript(script); |
| 413 | CFX_ByteString bsScript = wsScript.UTF8Encode(); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 414 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 415 | v8::Local<v8::Context> context = isolate->GetCurrentContext(); |
| 416 | v8::Local<v8::Script> compiled_script; |
| 417 | if (!v8::Script::Compile(context, |
| 418 | v8::String::NewFromUtf8(isolate, bsScript.c_str(), |
| 419 | v8::NewStringType::kNormal, |
| 420 | bsScript.GetLength()) |
| 421 | .ToLocalChecked()) |
| 422 | .ToLocal(&compiled_script)) { |
| 423 | v8::String::Utf8Value error(try_catch.Exception()); |
| 424 | return -1; |
| 425 | } |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 426 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 427 | v8::Local<v8::Value> result; |
| 428 | if (!compiled_script->Run(context).ToLocal(&result)) { |
| 429 | v8::String::Utf8Value error(try_catch.Exception()); |
| 430 | return -1; |
| 431 | } |
| 432 | return 0; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 433 | } |
| 434 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 435 | v8::Local<v8::Object> JS_NewFxDynamicObj(IJS_Runtime* pJSRuntime, |
| 436 | IFXJS_Context* pJSContext, |
| 437 | int nObjDefnID) { |
| 438 | v8::Isolate* isolate = (v8::Isolate*)pJSRuntime; |
| 439 | v8::Isolate::Scope isolate_scope(isolate); |
| 440 | v8::Local<v8::Context> context = isolate->GetCurrentContext(); |
| 441 | if (-1 == nObjDefnID) { |
| 442 | v8::Local<v8::ObjectTemplate> objTempl = v8::ObjectTemplate::New(isolate); |
| 443 | v8::Local<v8::Object> obj; |
| 444 | if (objTempl->NewInstance(context).ToLocal(&obj)) |
| 445 | return obj; |
| 446 | return v8::Local<v8::Object>(); |
| 447 | } |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 448 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 449 | CFX_PtrArray* pArray = (CFX_PtrArray*)isolate->GetData(g_embedderDataSlot); |
| 450 | if (!pArray) |
| 451 | return v8::Local<v8::Object>(); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 452 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 453 | if (nObjDefnID < 0 || nObjDefnID >= pArray->GetSize()) |
| 454 | return v8::Local<v8::Object>(); |
| 455 | CJS_ObjDefintion* pObjDef = (CJS_ObjDefintion*)pArray->GetAt(nObjDefnID); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 456 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 457 | v8::Local<v8::ObjectTemplate> objTemp = |
| 458 | v8::Local<v8::ObjectTemplate>::New(isolate, pObjDef->m_objTemplate); |
| 459 | v8::Local<v8::Object> obj; |
| 460 | if (!objTemp->NewInstance(context).ToLocal(&obj)) |
| 461 | return v8::Local<v8::Object>(); |
Tom Sepez | ae51c81 | 2015-08-05 12:34:06 -0700 | [diff] [blame] | 462 | CJS_PrivateData* pPrivateData = new CJS_PrivateData; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 463 | pPrivateData->ObjDefID = nObjDefnID; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 464 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 465 | obj->SetAlignedPointerInInternalField(0, pPrivateData); |
| 466 | if (pObjDef->m_pConstructor) |
| 467 | pObjDef->m_pConstructor( |
| 468 | pJSContext, obj, |
| 469 | context->Global()->GetPrototype()->ToObject(context).ToLocalChecked()); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 470 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 471 | return obj; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 472 | } |
| 473 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 474 | v8::Local<v8::Object> JS_GetStaticObj(IJS_Runtime* pJSRuntime, int nObjDefnID) { |
| 475 | v8::Isolate* isolate = (v8::Isolate*)pJSRuntime; |
| 476 | v8::Isolate::Scope isolate_scope(isolate); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 477 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 478 | CFX_PtrArray* pArray = (CFX_PtrArray*)isolate->GetData(g_embedderDataSlot); |
| 479 | if (!pArray) |
| 480 | return v8::Local<v8::Object>(); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 481 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 482 | if (nObjDefnID < 0 || nObjDefnID >= pArray->GetSize()) |
| 483 | return v8::Local<v8::Object>(); |
| 484 | CJS_ObjDefintion* pObjDef = (CJS_ObjDefintion*)pArray->GetAt(nObjDefnID); |
| 485 | v8::Local<v8::Object> obj = |
| 486 | v8::Local<v8::Object>::New(isolate, pObjDef->m_StaticObj); |
| 487 | return obj; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 488 | } |
| 489 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 490 | void JS_SetThisObj(IJS_Runtime* pJSRuntime, int nThisObjID) { |
| 491 | // Do nothing. |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 492 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 493 | v8::Local<v8::Object> JS_GetThisObj(IJS_Runtime* pJSRuntime) { |
| 494 | // Return the global object. |
| 495 | v8::Isolate* isolate = (v8::Isolate*)pJSRuntime; |
| 496 | v8::Isolate::Scope isolate_scope(isolate); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 497 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 498 | CFX_PtrArray* pArray = (CFX_PtrArray*)isolate->GetData(g_embedderDataSlot); |
| 499 | if (!pArray) |
| 500 | return v8::Local<v8::Object>(); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 501 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 502 | v8::Local<v8::Context> context = isolate->GetCurrentContext(); |
| 503 | return context->Global()->GetPrototype()->ToObject(context).ToLocalChecked(); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 504 | } |
| 505 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 506 | int JS_GetObjDefnID(v8::Local<v8::Object> pObj) { |
| 507 | if (pObj.IsEmpty() || !pObj->InternalFieldCount()) |
| 508 | return -1; |
| 509 | CJS_PrivateData* pPrivateData = |
| 510 | (CJS_PrivateData*)pObj->GetAlignedPointerFromInternalField(0); |
| 511 | if (pPrivateData) |
| 512 | return pPrivateData->ObjDefID; |
| 513 | return -1; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 514 | } |
| 515 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 516 | IJS_Runtime* JS_GetRuntime(v8::Local<v8::Object> pObj) { |
| 517 | if (pObj.IsEmpty()) |
| 518 | return NULL; |
| 519 | v8::Local<v8::Context> context = pObj->CreationContext(); |
| 520 | if (context.IsEmpty()) |
| 521 | return NULL; |
| 522 | return context->GetIsolate(); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 523 | } |
| 524 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 525 | int JS_GetObjDefnID(IJS_Runtime* pJSRuntime, const wchar_t* pObjName) { |
| 526 | v8::Isolate* isolate = (v8::Isolate*)pJSRuntime; |
| 527 | v8::Isolate::Scope isolate_scope(isolate); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 528 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 529 | CFX_PtrArray* pArray = (CFX_PtrArray*)isolate->GetData(g_embedderDataSlot); |
| 530 | if (!pArray) |
| 531 | return -1; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 532 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 533 | for (int i = 0; i < pArray->GetSize(); i++) { |
| 534 | CJS_ObjDefintion* pObjDef = (CJS_ObjDefintion*)pArray->GetAt(i); |
| 535 | if (FXSYS_wcscmp(pObjDef->objName, pObjName) == 0) |
| 536 | return i; |
| 537 | } |
| 538 | return -1; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 539 | } |
| 540 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 541 | void JS_Error(v8::Isolate* isolate, const CFX_WideString& message) { |
| 542 | // Conversion from pdfium's wchar_t wide-strings to v8's uint16_t |
| 543 | // wide-strings isn't handled by v8, so use UTF8 as a common |
| 544 | // intermediate format. |
| 545 | CFX_ByteString utf8_message = message.UTF8Encode(); |
| 546 | isolate->ThrowException(v8::String::NewFromUtf8(isolate, utf8_message.c_str(), |
| 547 | v8::NewStringType::kNormal) |
| 548 | .ToLocalChecked()); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 549 | } |
| 550 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 551 | unsigned JS_CalcHash(const wchar_t* main, unsigned nLen) { |
| 552 | return (unsigned)FX_HashCode_String_GetW((const FX_WCHAR*)main, nLen); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 553 | } |
| 554 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 555 | unsigned JS_CalcHash(const wchar_t* main) { |
| 556 | return (unsigned)FX_HashCode_String_GetW((const FX_WCHAR*)main, |
| 557 | FXSYS_wcslen(main)); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 558 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 559 | const wchar_t* JS_GetTypeof(v8::Local<v8::Value> pObj) { |
| 560 | if (pObj.IsEmpty()) |
| 561 | return NULL; |
| 562 | if (pObj->IsString()) |
| 563 | return VALUE_NAME_STRING; |
| 564 | if (pObj->IsNumber()) |
| 565 | return VALUE_NAME_NUMBER; |
| 566 | if (pObj->IsBoolean()) |
| 567 | return VALUE_NAME_BOOLEAN; |
| 568 | if (pObj->IsDate()) |
| 569 | return VALUE_NAME_DATE; |
| 570 | if (pObj->IsObject()) |
| 571 | return VALUE_NAME_OBJECT; |
| 572 | if (pObj->IsNull()) |
| 573 | return VALUE_NAME_NULL; |
| 574 | if (pObj->IsUndefined()) |
| 575 | return VALUE_NAME_UNDEFINED; |
| 576 | return NULL; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 577 | } |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 578 | void JS_SetPrivate(v8::Local<v8::Object> pObj, void* p) { |
| 579 | JS_SetPrivate(NULL, pObj, p); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 580 | } |
| 581 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 582 | void* JS_GetPrivate(v8::Local<v8::Object> pObj) { |
| 583 | return JS_GetPrivate(NULL, pObj); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 584 | } |
| 585 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 586 | void JS_SetPrivate(IJS_Runtime* pJSRuntime, |
| 587 | v8::Local<v8::Object> pObj, |
| 588 | void* p) { |
| 589 | if (pObj.IsEmpty() || !pObj->InternalFieldCount()) |
| 590 | return; |
| 591 | CJS_PrivateData* pPrivateData = |
| 592 | (CJS_PrivateData*)pObj->GetAlignedPointerFromInternalField(0); |
| 593 | if (!pPrivateData) |
| 594 | return; |
| 595 | pPrivateData->pPrivate = p; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 596 | } |
| 597 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 598 | void* JS_GetPrivate(IJS_Runtime* pJSRuntime, v8::Local<v8::Object> pObj) { |
| 599 | if (pObj.IsEmpty()) |
| 600 | return NULL; |
| 601 | CJS_PrivateData* pPrivateData = NULL; |
| 602 | if (pObj->InternalFieldCount()) |
| 603 | pPrivateData = |
| 604 | (CJS_PrivateData*)pObj->GetAlignedPointerFromInternalField(0); |
| 605 | else { |
| 606 | // It could be a global proxy object. |
| 607 | v8::Local<v8::Value> v = pObj->GetPrototype(); |
| 608 | v8::Isolate* isolate = (v8::Isolate*)pJSRuntime; |
| 609 | v8::Local<v8::Context> context = isolate->GetCurrentContext(); |
| 610 | if (v->IsObject()) |
| 611 | pPrivateData = (CJS_PrivateData*)v->ToObject(context) |
| 612 | .ToLocalChecked() |
| 613 | ->GetAlignedPointerFromInternalField(0); |
| 614 | } |
| 615 | if (!pPrivateData) |
| 616 | return NULL; |
| 617 | return pPrivateData->pPrivate; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 618 | } |
| 619 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 620 | void JS_FreePrivate(void* pPrivateData) { |
| 621 | delete (CJS_PrivateData*)pPrivateData; |
Jochen Eisinger | dfa2c99 | 2015-05-19 00:38:00 +0200 | [diff] [blame] | 622 | } |
| 623 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 624 | void JS_FreePrivate(v8::Local<v8::Object> pObj) { |
| 625 | if (pObj.IsEmpty() || !pObj->InternalFieldCount()) |
| 626 | return; |
| 627 | JS_FreePrivate(pObj->GetAlignedPointerFromInternalField(0)); |
| 628 | pObj->SetAlignedPointerInInternalField(0, NULL); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 629 | } |
| 630 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 631 | v8::Local<v8::Value> JS_GetObjectValue(v8::Local<v8::Object> pObj) { |
| 632 | return pObj; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 633 | } |
| 634 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 635 | v8::Local<v8::String> WSToJSString(IJS_Runtime* pJSRuntime, |
| 636 | const wchar_t* PropertyName, |
| 637 | int Len = -1) { |
| 638 | CFX_WideString ws = CFX_WideString(PropertyName, Len); |
| 639 | CFX_ByteString bs = ws.UTF8Encode(); |
| 640 | if (!pJSRuntime) |
| 641 | pJSRuntime = v8::Isolate::GetCurrent(); |
| 642 | return v8::String::NewFromUtf8(pJSRuntime, bs.c_str(), |
| 643 | v8::NewStringType::kNormal) |
| 644 | .ToLocalChecked(); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 645 | } |
| 646 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 647 | v8::Local<v8::Value> JS_GetObjectElement(IJS_Runtime* pJSRuntime, |
| 648 | v8::Local<v8::Object> pObj, |
| 649 | const wchar_t* PropertyName) { |
| 650 | if (pObj.IsEmpty()) |
| 651 | return v8::Local<v8::Value>(); |
| 652 | v8::Local<v8::Value> val; |
| 653 | if (!pObj->Get(pJSRuntime->GetCurrentContext(), |
| 654 | WSToJSString(pJSRuntime, PropertyName)) |
| 655 | .ToLocal(&val)) |
| 656 | return v8::Local<v8::Value>(); |
| 657 | return val; |
| 658 | } |
| 659 | |
| 660 | v8::Local<v8::Array> JS_GetObjectElementNames(IJS_Runtime* pJSRuntime, |
| 661 | v8::Local<v8::Object> pObj) { |
| 662 | if (pObj.IsEmpty()) |
| 663 | return v8::Local<v8::Array>(); |
| 664 | v8::Local<v8::Array> val; |
| 665 | if (!pObj->GetPropertyNames(pJSRuntime->GetCurrentContext()).ToLocal(&val)) |
| 666 | return v8::Local<v8::Array>(); |
| 667 | return val; |
| 668 | } |
| 669 | |
| 670 | void JS_PutObjectString(IJS_Runtime* pJSRuntime, |
| 671 | v8::Local<v8::Object> pObj, |
| 672 | const wchar_t* PropertyName, |
| 673 | const wchar_t* sValue) // VT_string |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 674 | { |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 675 | if (pObj.IsEmpty()) |
| 676 | return; |
| 677 | pObj->Set(pJSRuntime->GetCurrentContext(), |
| 678 | WSToJSString(pJSRuntime, PropertyName), |
| 679 | WSToJSString(pJSRuntime, sValue)) |
| 680 | .FromJust(); |
| 681 | } |
| 682 | |
| 683 | void JS_PutObjectNumber(IJS_Runtime* pJSRuntime, |
| 684 | v8::Local<v8::Object> pObj, |
| 685 | const wchar_t* PropertyName, |
| 686 | int nValue) { |
| 687 | if (pObj.IsEmpty()) |
| 688 | return; |
| 689 | pObj->Set(pJSRuntime->GetCurrentContext(), |
| 690 | WSToJSString(pJSRuntime, PropertyName), |
| 691 | v8::Int32::New(pJSRuntime, nValue)) |
| 692 | .FromJust(); |
| 693 | } |
| 694 | |
| 695 | void JS_PutObjectNumber(IJS_Runtime* pJSRuntime, |
| 696 | v8::Local<v8::Object> pObj, |
| 697 | const wchar_t* PropertyName, |
| 698 | float fValue) { |
| 699 | if (pObj.IsEmpty()) |
| 700 | return; |
| 701 | pObj->Set(pJSRuntime->GetCurrentContext(), |
| 702 | WSToJSString(pJSRuntime, PropertyName), |
| 703 | v8::Number::New(pJSRuntime, (double)fValue)) |
| 704 | .FromJust(); |
| 705 | } |
| 706 | |
| 707 | void JS_PutObjectNumber(IJS_Runtime* pJSRuntime, |
| 708 | v8::Local<v8::Object> pObj, |
| 709 | const wchar_t* PropertyName, |
| 710 | double dValue) { |
| 711 | if (pObj.IsEmpty()) |
| 712 | return; |
| 713 | pObj->Set(pJSRuntime->GetCurrentContext(), |
| 714 | WSToJSString(pJSRuntime, PropertyName), |
| 715 | v8::Number::New(pJSRuntime, (double)dValue)) |
| 716 | .FromJust(); |
| 717 | } |
| 718 | |
| 719 | void JS_PutObjectBoolean(IJS_Runtime* pJSRuntime, |
| 720 | v8::Local<v8::Object> pObj, |
| 721 | const wchar_t* PropertyName, |
| 722 | bool bValue) { |
| 723 | if (pObj.IsEmpty()) |
| 724 | return; |
| 725 | pObj->Set(pJSRuntime->GetCurrentContext(), |
| 726 | WSToJSString(pJSRuntime, PropertyName), |
| 727 | v8::Boolean::New(pJSRuntime, bValue)) |
| 728 | .FromJust(); |
| 729 | } |
| 730 | |
| 731 | void JS_PutObjectObject(IJS_Runtime* pJSRuntime, |
| 732 | v8::Local<v8::Object> pObj, |
| 733 | const wchar_t* PropertyName, |
| 734 | v8::Local<v8::Object> pPut) { |
| 735 | if (pObj.IsEmpty()) |
| 736 | return; |
| 737 | pObj->Set(pJSRuntime->GetCurrentContext(), |
| 738 | WSToJSString(pJSRuntime, PropertyName), pPut) |
| 739 | .FromJust(); |
| 740 | } |
| 741 | |
| 742 | void JS_PutObjectNull(IJS_Runtime* pJSRuntime, |
| 743 | v8::Local<v8::Object> pObj, |
| 744 | const wchar_t* PropertyName) { |
| 745 | if (pObj.IsEmpty()) |
| 746 | return; |
| 747 | pObj->Set(pJSRuntime->GetCurrentContext(), |
| 748 | WSToJSString(pJSRuntime, PropertyName), v8::Local<v8::Object>()) |
| 749 | .FromJust(); |
| 750 | } |
| 751 | |
| 752 | v8::Local<v8::Array> JS_NewArray(IJS_Runtime* pJSRuntime) { |
| 753 | return v8::Array::New(pJSRuntime); |
| 754 | } |
| 755 | |
| 756 | unsigned JS_PutArrayElement(IJS_Runtime* pJSRuntime, |
| 757 | v8::Local<v8::Array> pArray, |
| 758 | unsigned index, |
| 759 | v8::Local<v8::Value> pValue, |
| 760 | FXJSVALUETYPE eType) { |
| 761 | if (pArray.IsEmpty()) |
| 762 | return 0; |
| 763 | if (pArray->Set(pJSRuntime->GetCurrentContext(), index, pValue).IsNothing()) |
| 764 | return 0; |
| 765 | return 1; |
| 766 | } |
| 767 | |
| 768 | v8::Local<v8::Value> JS_GetArrayElement(IJS_Runtime* pJSRuntime, |
| 769 | v8::Local<v8::Array> pArray, |
| 770 | unsigned index) { |
| 771 | if (pArray.IsEmpty()) |
| 772 | return v8::Local<v8::Value>(); |
| 773 | v8::Local<v8::Value> val; |
| 774 | if (pArray->Get(pJSRuntime->GetCurrentContext(), index).ToLocal(&val)) |
| 775 | return v8::Local<v8::Value>(); |
| 776 | return val; |
| 777 | } |
| 778 | |
| 779 | unsigned JS_GetArrayLength(v8::Local<v8::Array> pArray) { |
| 780 | if (pArray.IsEmpty()) |
| 781 | return 0; |
| 782 | return pArray->Length(); |
| 783 | } |
| 784 | |
| 785 | v8::Local<v8::Value> JS_NewNumber(IJS_Runtime* pJSRuntime, int number) { |
| 786 | return v8::Int32::New(pJSRuntime, number); |
| 787 | } |
| 788 | |
| 789 | v8::Local<v8::Value> JS_NewNumber(IJS_Runtime* pJSRuntime, double number) { |
| 790 | return v8::Number::New(pJSRuntime, number); |
| 791 | } |
| 792 | |
| 793 | v8::Local<v8::Value> JS_NewNumber(IJS_Runtime* pJSRuntime, float number) { |
| 794 | return v8::Number::New(pJSRuntime, (float)number); |
| 795 | } |
| 796 | |
| 797 | v8::Local<v8::Value> JS_NewBoolean(IJS_Runtime* pJSRuntime, bool b) { |
| 798 | return v8::Boolean::New(pJSRuntime, b); |
| 799 | } |
| 800 | |
| 801 | v8::Local<v8::Value> JS_NewObject(IJS_Runtime* pJSRuntime, |
| 802 | v8::Local<v8::Object> pObj) { |
| 803 | if (pObj.IsEmpty()) |
| 804 | return v8::Local<v8::Value>(); |
| 805 | return pObj->Clone(); |
| 806 | } |
| 807 | |
| 808 | v8::Local<v8::Value> JS_NewObject2(IJS_Runtime* pJSRuntime, |
| 809 | v8::Local<v8::Array> pObj) { |
| 810 | if (pObj.IsEmpty()) |
| 811 | return v8::Local<v8::Value>(); |
| 812 | return pObj->Clone(); |
| 813 | } |
| 814 | |
| 815 | v8::Local<v8::Value> JS_NewString(IJS_Runtime* pJSRuntime, |
| 816 | const wchar_t* string) { |
| 817 | return WSToJSString(pJSRuntime, string); |
| 818 | } |
| 819 | |
| 820 | v8::Local<v8::Value> JS_NewString(IJS_Runtime* pJSRuntime, |
| 821 | const wchar_t* string, |
| 822 | unsigned nLen) { |
| 823 | return WSToJSString(pJSRuntime, string, nLen); |
| 824 | } |
| 825 | |
| 826 | v8::Local<v8::Value> JS_NewNull() { |
| 827 | return v8::Local<v8::Value>(); |
| 828 | } |
| 829 | |
| 830 | v8::Local<v8::Value> JS_NewDate(IJS_Runtime* pJSRuntime, double d) { |
| 831 | return v8::Date::New(pJSRuntime->GetCurrentContext(), d).ToLocalChecked(); |
| 832 | } |
| 833 | |
| 834 | v8::Local<v8::Value> JS_NewValue(IJS_Runtime* pJSRuntime) { |
| 835 | return v8::Local<v8::Value>(); |
| 836 | } |
| 837 | |
| 838 | v8::Local<v8::Value> JS_GetListValue(IJS_Runtime* pJSRuntime, |
| 839 | v8::Local<v8::Value> pList, |
| 840 | int index) { |
| 841 | v8::Local<v8::Context> context = pJSRuntime->GetCurrentContext(); |
| 842 | if (!pList.IsEmpty() && pList->IsObject()) { |
| 843 | v8::Local<v8::Object> obj; |
| 844 | if (pList->ToObject(context).ToLocal(&obj)) { |
| 845 | v8::Local<v8::Value> val; |
| 846 | if (obj->Get(context, index).ToLocal(&val)) |
Jochen Eisinger | dfa2c99 | 2015-05-19 00:38:00 +0200 | [diff] [blame] | 847 | return val; |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 848 | } |
| 849 | } |
| 850 | return v8::Local<v8::Value>(); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 851 | } |
| 852 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 853 | int JS_ToInt32(IJS_Runtime* pJSRuntime, v8::Local<v8::Value> pValue) { |
| 854 | if (pValue.IsEmpty()) |
| 855 | return 0; |
| 856 | v8::Local<v8::Context> context = pJSRuntime->GetCurrentContext(); |
| 857 | return pValue->ToInt32(context).ToLocalChecked()->Value(); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 858 | } |
| 859 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 860 | bool JS_ToBoolean(IJS_Runtime* pJSRuntime, v8::Local<v8::Value> pValue) { |
| 861 | if (pValue.IsEmpty()) |
| 862 | return false; |
| 863 | v8::Local<v8::Context> context = pJSRuntime->GetCurrentContext(); |
| 864 | return pValue->ToBoolean(context).ToLocalChecked()->Value(); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 865 | } |
| 866 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 867 | double JS_ToNumber(IJS_Runtime* pJSRuntime, v8::Local<v8::Value> pValue) { |
| 868 | if (pValue.IsEmpty()) |
| 869 | return 0.0; |
| 870 | v8::Local<v8::Context> context = pJSRuntime->GetCurrentContext(); |
| 871 | return pValue->ToNumber(context).ToLocalChecked()->Value(); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 872 | } |
| 873 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 874 | v8::Local<v8::Object> JS_ToObject(IJS_Runtime* pJSRuntime, |
| 875 | v8::Local<v8::Value> pValue) { |
| 876 | if (pValue.IsEmpty()) |
| 877 | return v8::Local<v8::Object>(); |
| 878 | v8::Local<v8::Context> context = pJSRuntime->GetCurrentContext(); |
| 879 | return pValue->ToObject(context).ToLocalChecked(); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 880 | } |
| 881 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 882 | CFX_WideString JS_ToString(IJS_Runtime* pJSRuntime, |
| 883 | v8::Local<v8::Value> pValue) { |
| 884 | if (pValue.IsEmpty()) |
| 885 | return L""; |
| 886 | v8::Local<v8::Context> context = pJSRuntime->GetCurrentContext(); |
| 887 | v8::String::Utf8Value s(pValue->ToString(context).ToLocalChecked()); |
| 888 | return CFX_WideString::FromUTF8(*s, s.length()); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 889 | } |
| 890 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 891 | v8::Local<v8::Array> JS_ToArray(IJS_Runtime* pJSRuntime, |
| 892 | v8::Local<v8::Value> pValue) { |
| 893 | if (pValue.IsEmpty()) |
| 894 | return v8::Local<v8::Array>(); |
| 895 | v8::Local<v8::Context> context = pJSRuntime->GetCurrentContext(); |
| 896 | return v8::Local<v8::Array>::Cast(pValue->ToObject(context).ToLocalChecked()); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 897 | } |
| 898 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 899 | void JS_ValueCopy(v8::Local<v8::Value>& pTo, v8::Local<v8::Value> pFrom) { |
| 900 | pTo = pFrom; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 901 | } |
| 902 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 903 | // JavaScript time implement begin. |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 904 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 905 | double _getLocalTZA() { |
| 906 | if (!FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS)) |
| 907 | return 0; |
| 908 | time_t t = 0; |
| 909 | time(&t); |
| 910 | localtime(&t); |
Bruce Dawson | 06e015f | 2015-04-09 11:10:17 -0700 | [diff] [blame] | 911 | #if _MSC_VER >= 1900 |
| 912 | // In gcc and in Visual Studio prior to VS 2015 'timezone' is a global |
| 913 | // variable declared in time.h. That variable was deprecated and in VS 2015 |
| 914 | // is removed, with _get_timezone replacing it. |
| 915 | long timezone = 0; |
| 916 | _get_timezone(&timezone); |
| 917 | #endif |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 918 | return (double)(-(timezone * 1000)); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 919 | } |
| 920 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 921 | int _getDaylightSavingTA(double d) { |
| 922 | if (!FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS)) |
| 923 | return 0; |
| 924 | time_t t = (time_t)(d / 1000); |
| 925 | struct tm* tmp = localtime(&t); |
| 926 | if (tmp == NULL) |
| 927 | return 0; |
| 928 | if (tmp->tm_isdst > 0) |
| 929 | // One hour. |
| 930 | return (int)60 * 60 * 1000; |
| 931 | return 0; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 932 | } |
| 933 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 934 | double _Mod(double x, double y) { |
| 935 | double r = fmod(x, y); |
| 936 | if (r < 0) |
| 937 | r += y; |
| 938 | return r; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 939 | } |
| 940 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 941 | int _isfinite(double v) { |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 942 | #if _MSC_VER |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 943 | return ::_finite(v); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 944 | #else |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 945 | return std::fabs(v) < std::numeric_limits<double>::max(); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 946 | #endif |
| 947 | } |
| 948 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 949 | double _toInteger(double n) { |
| 950 | return (n >= 0) ? FXSYS_floor(n) : -FXSYS_floor(-n); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 951 | } |
| 952 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 953 | bool _isLeapYear(int year) { |
| 954 | return (year % 4 == 0) && ((year % 100 != 0) || (year % 400 != 0)); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 955 | } |
| 956 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 957 | int _DayFromYear(int y) { |
| 958 | return (int)(365 * (y - 1970.0) + FXSYS_floor((y - 1969.0) / 4) - |
| 959 | FXSYS_floor((y - 1901.0) / 100) + |
| 960 | FXSYS_floor((y - 1601.0) / 400)); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 961 | } |
| 962 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 963 | double _TimeFromYear(int y) { |
| 964 | return ((double)86400000) * _DayFromYear(y); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 965 | } |
| 966 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 967 | double _TimeFromYearMonth(int y, int m) { |
| 968 | static int daysMonth[12] = {0, 31, 59, 90, 120, 151, |
| 969 | 181, 212, 243, 273, 304, 334}; |
| 970 | static int leapDaysMonth[12] = {0, 31, 60, 91, 121, 152, |
| 971 | 182, 213, 244, 274, 305, 335}; |
| 972 | int* pMonth = daysMonth; |
| 973 | if (_isLeapYear(y)) |
| 974 | pMonth = leapDaysMonth; |
| 975 | return _TimeFromYear(y) + ((double)pMonth[m]) * 86400000; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 976 | } |
| 977 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 978 | int _Day(double t) { |
| 979 | return (int)FXSYS_floor(t / 86400000); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 980 | } |
| 981 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 982 | int _YearFromTime(double t) { |
| 983 | // estimate the time. |
| 984 | int y = 1970 + (int)(t / (365.0 * 86400000)); |
| 985 | if (_TimeFromYear(y) <= t) { |
| 986 | while (_TimeFromYear(y + 1) <= t) |
| 987 | y++; |
| 988 | } else |
| 989 | while (_TimeFromYear(y - 1) > t) |
| 990 | y--; |
| 991 | return y; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 992 | } |
| 993 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 994 | int _DayWithinYear(double t) { |
| 995 | int year = _YearFromTime(t); |
| 996 | int day = _Day(t); |
| 997 | return day - _DayFromYear(year); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 998 | } |
| 999 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1000 | int _MonthFromTime(double t) { |
| 1001 | int day = _DayWithinYear(t); |
| 1002 | int year = _YearFromTime(t); |
| 1003 | if (0 <= day && day < 31) |
| 1004 | return 0; |
| 1005 | if (31 <= day && day < 59 + _isLeapYear(year)) |
| 1006 | return 1; |
| 1007 | if ((59 + _isLeapYear(year)) <= day && day < (90 + _isLeapYear(year))) |
| 1008 | return 2; |
| 1009 | if ((90 + _isLeapYear(year)) <= day && day < (120 + _isLeapYear(year))) |
| 1010 | return 3; |
| 1011 | if ((120 + _isLeapYear(year)) <= day && day < (151 + _isLeapYear(year))) |
| 1012 | return 4; |
| 1013 | if ((151 + _isLeapYear(year)) <= day && day < (181 + _isLeapYear(year))) |
| 1014 | return 5; |
| 1015 | if ((181 + _isLeapYear(year)) <= day && day < (212 + _isLeapYear(year))) |
| 1016 | return 6; |
| 1017 | if ((212 + _isLeapYear(year)) <= day && day < (243 + _isLeapYear(year))) |
| 1018 | return 7; |
| 1019 | if ((243 + _isLeapYear(year)) <= day && day < (273 + _isLeapYear(year))) |
| 1020 | return 8; |
| 1021 | if ((273 + _isLeapYear(year)) <= day && day < (304 + _isLeapYear(year))) |
| 1022 | return 9; |
| 1023 | if ((304 + _isLeapYear(year)) <= day && day < (334 + _isLeapYear(year))) |
| 1024 | return 10; |
| 1025 | if ((334 + _isLeapYear(year)) <= day && day < (365 + _isLeapYear(year))) |
| 1026 | return 11; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1027 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1028 | return -1; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1029 | } |
| 1030 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1031 | int _DateFromTime(double t) { |
| 1032 | int day = _DayWithinYear(t); |
| 1033 | int year = _YearFromTime(t); |
| 1034 | bool leap = _isLeapYear(year); |
| 1035 | int month = _MonthFromTime(t); |
| 1036 | switch (month) { |
| 1037 | case 0: |
| 1038 | return day + 1; |
| 1039 | case 1: |
| 1040 | return day - 30; |
| 1041 | case 2: |
| 1042 | return day - 58 - leap; |
| 1043 | case 3: |
| 1044 | return day - 89 - leap; |
| 1045 | case 4: |
| 1046 | return day - 119 - leap; |
| 1047 | case 5: |
| 1048 | return day - 150 - leap; |
| 1049 | case 6: |
| 1050 | return day - 180 - leap; |
| 1051 | case 7: |
| 1052 | return day - 211 - leap; |
| 1053 | case 8: |
| 1054 | return day - 242 - leap; |
| 1055 | case 9: |
| 1056 | return day - 272 - leap; |
| 1057 | case 10: |
| 1058 | return day - 303 - leap; |
| 1059 | case 11: |
| 1060 | return day - 333 - leap; |
| 1061 | default: |
| 1062 | return 0; |
| 1063 | } |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1064 | } |
| 1065 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1066 | double JS_GetDateTime() { |
| 1067 | if (!FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS)) |
| 1068 | return 0; |
| 1069 | time_t t = time(NULL); |
| 1070 | struct tm* pTm = localtime(&t); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1071 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1072 | int year = pTm->tm_year + 1900; |
| 1073 | double t1 = _TimeFromYear(year); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1074 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1075 | return t1 + pTm->tm_yday * 86400000.0 + pTm->tm_hour * 3600000.0 + |
| 1076 | pTm->tm_min * 60000.0 + pTm->tm_sec * 1000.0; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1077 | } |
| 1078 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1079 | int JS_GetYearFromTime(double dt) { |
| 1080 | return _YearFromTime(dt); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1081 | } |
| 1082 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1083 | int JS_GetMonthFromTime(double dt) { |
| 1084 | return _MonthFromTime(dt); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1085 | } |
| 1086 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1087 | int JS_GetDayFromTime(double dt) { |
| 1088 | return _DateFromTime(dt); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1089 | } |
| 1090 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1091 | int JS_GetHourFromTime(double dt) { |
| 1092 | return (int)_Mod(FXSYS_floor((double)(dt / (60 * 60 * 1000))), 24); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1093 | } |
| 1094 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1095 | int JS_GetMinFromTime(double dt) { |
| 1096 | return (int)_Mod(FXSYS_floor((double)(dt / (60 * 1000))), 60); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1097 | } |
| 1098 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1099 | int JS_GetSecFromTime(double dt) { |
| 1100 | return (int)_Mod(FXSYS_floor((double)(dt / 1000)), 60); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1101 | } |
| 1102 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1103 | double JS_DateParse(const wchar_t* string) { |
| 1104 | v8::Isolate* pIsolate = v8::Isolate::GetCurrent(); |
| 1105 | v8::Isolate::Scope isolate_scope(pIsolate); |
| 1106 | v8::HandleScope scope(pIsolate); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1107 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1108 | v8::Local<v8::Context> context = pIsolate->GetCurrentContext(); |
Lei Zhang | a6d9f0e | 2015-06-13 00:48:38 -0700 | [diff] [blame] | 1109 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1110 | // Use the built-in object method. |
| 1111 | v8::Local<v8::Value> v = |
| 1112 | context->Global() |
| 1113 | ->Get(context, v8::String::NewFromUtf8(pIsolate, "Date", |
| 1114 | v8::NewStringType::kNormal) |
| 1115 | .ToLocalChecked()) |
| 1116 | .ToLocalChecked(); |
| 1117 | if (v->IsObject()) { |
| 1118 | v8::Local<v8::Object> o = v->ToObject(context).ToLocalChecked(); |
| 1119 | v = o->Get(context, v8::String::NewFromUtf8(pIsolate, "parse", |
| 1120 | v8::NewStringType::kNormal) |
| 1121 | .ToLocalChecked()) |
| 1122 | .ToLocalChecked(); |
| 1123 | if (v->IsFunction()) { |
| 1124 | v8::Local<v8::Function> funC = v8::Local<v8::Function>::Cast(v); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1125 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1126 | const int argc = 1; |
| 1127 | v8::Local<v8::String> timeStr = WSToJSString(pIsolate, string); |
| 1128 | v8::Local<v8::Value> argv[argc] = {timeStr}; |
| 1129 | v = funC->Call(context, context->Global(), argc, argv).ToLocalChecked(); |
| 1130 | if (v->IsNumber()) { |
| 1131 | double date = v->ToNumber(context).ToLocalChecked()->Value(); |
| 1132 | if (!_isfinite(date)) |
| 1133 | return date; |
| 1134 | return date + _getLocalTZA() + _getDaylightSavingTA(date); |
| 1135 | } |
| 1136 | } |
| 1137 | } |
| 1138 | return 0; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1139 | } |
| 1140 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1141 | double JS_MakeDay(int nYear, int nMonth, int nDate) { |
| 1142 | if (!_isfinite(nYear) || !_isfinite(nMonth) || !_isfinite(nDate)) |
| 1143 | return GetNan(); |
| 1144 | double y = _toInteger(nYear); |
| 1145 | double m = _toInteger(nMonth); |
| 1146 | double dt = _toInteger(nDate); |
| 1147 | double ym = y + FXSYS_floor((double)m / 12); |
| 1148 | double mn = _Mod(m, 12); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1149 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1150 | double t = _TimeFromYearMonth((int)ym, (int)mn); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1151 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1152 | if (_YearFromTime(t) != ym || _MonthFromTime(t) != mn || |
| 1153 | _DateFromTime(t) != 1) |
| 1154 | return GetNan(); |
| 1155 | return _Day(t) + dt - 1; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1156 | } |
| 1157 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1158 | double JS_MakeTime(int nHour, int nMin, int nSec, int nMs) { |
| 1159 | if (!_isfinite(nHour) || !_isfinite(nMin) || !_isfinite(nSec) || |
| 1160 | !_isfinite(nMs)) |
| 1161 | return GetNan(); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1162 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1163 | double h = _toInteger(nHour); |
| 1164 | double m = _toInteger(nMin); |
| 1165 | double s = _toInteger(nSec); |
| 1166 | double milli = _toInteger(nMs); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1167 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1168 | return h * 3600000 + m * 60000 + s * 1000 + milli; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1169 | } |
| 1170 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1171 | double JS_MakeDate(double day, double time) { |
| 1172 | if (!_isfinite(day) || !_isfinite(time)) |
| 1173 | return GetNan(); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1174 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1175 | return day * 86400000 + time; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1176 | } |
| 1177 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1178 | bool JS_PortIsNan(double d) { |
| 1179 | return d != d; |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1180 | } |
| 1181 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1182 | double JS_LocalTime(double d) { |
| 1183 | return JS_GetDateTime() + _getDaylightSavingTA(d); |
John Abd-El-Malek | 5110c47 | 2014-05-17 22:33:34 -0700 | [diff] [blame] | 1184 | } |
| 1185 | |
Nico Weber | 9d8ec5a | 2015-08-04 13:00:21 -0700 | [diff] [blame] | 1186 | // JavaScript time implement End. |