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