Remove dead code found by Scythe.
[pdfium.git] / fpdfsdk / src / jsapi / fxjs_v8.cpp
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.
4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7 #include "../../../core/include/fxcrt/fx_basic.h"
8 #include "../../../core/include/fxcrt/fx_ext.h"
9 #include "../../include/jsapi/fxjs_v8.h"
10 #include "../../include/fsdk_define.h"
11 #include "time.h"
12 #include <cmath>
13 #include <limits>
14
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"
23
24 const static FX_DWORD g_nan[2] = {0,0x7FF80000 };
25 static double GetNan()
26 {
27   return *(double*)g_nan;
28 }
29 static unsigned int g_embedderDataSlot = 0u;
30
31
32 class CJS_PrivateData
33 {
34 public:
35         CJS_PrivateData():ObjDefID(-1), pPrivate(NULL) {}
36         int ObjDefID;
37         void*   pPrivate;
38 };
39
40
41 class CJS_ObjDefintion
42 {
43 public:
44         CJS_ObjDefintion(v8::Isolate* isolate, const wchar_t* sObjName, FXJSOBJTYPE eObjType, LP_CONSTRUCTOR pConstructor, LP_DESTRUCTOR pDestructor):
45           objName(sObjName), objType(eObjType), m_pConstructor(pConstructor), m_pDestructor(pDestructor),m_bSetAsGlobalObject(FALSE)
46           {
47                   v8::Isolate::Scope isolate_scope(isolate);
48                   v8::HandleScope handle_scope(isolate);
49
50                   v8::Local<v8::ObjectTemplate> objTemplate = v8::ObjectTemplate::New(isolate);
51                   objTemplate->SetInternalFieldCount(2);
52                   m_objTemplate.Reset(isolate, objTemplate);
53
54                  //Document as the global object.
55                   if(FXSYS_wcscmp(sObjName, L"Document") == 0)
56                   {
57                          m_bSetAsGlobalObject = TRUE;
58                   }
59
60           }
61           ~CJS_ObjDefintion()
62           {
63                   m_objTemplate.Reset();
64                   m_StaticObj.Reset();
65           }
66 public:
67         const wchar_t* objName;
68         FXJSOBJTYPE objType;
69         LP_CONSTRUCTOR m_pConstructor;
70         LP_DESTRUCTOR m_pDestructor;
71         FX_BOOL m_bSetAsGlobalObject;
72
73         v8::Global<v8::ObjectTemplate> m_objTemplate;
74         v8::Global<v8::Object> m_StaticObj;
75 };
76
77 int JS_DefineObj(IJS_Runtime* pJSRuntime, const wchar_t* sObjName, FXJSOBJTYPE eObjType, LP_CONSTRUCTOR pConstructor, LP_DESTRUCTOR pDestructor)
78 {
79         v8::Isolate* isolate = (v8::Isolate*)pJSRuntime;
80         v8::Isolate::Scope isolate_scope(isolate);
81         v8::HandleScope handle_scope(isolate);
82         CFX_PtrArray* pArray = (CFX_PtrArray*)isolate->GetData(g_embedderDataSlot);
83         if(!pArray)
84         {
85                 pArray = new CFX_PtrArray();
86                 isolate->SetData(g_embedderDataSlot, pArray);
87         }
88         CJS_ObjDefintion* pObjDef = new CJS_ObjDefintion(isolate, sObjName, eObjType, pConstructor, pDestructor);
89         pArray->Add(pObjDef);
90         return pArray->GetSize()-1;
91 }
92
93 int JS_DefineObjMethod(IJS_Runtime* pJSRuntime, int nObjDefnID, const wchar_t* sMethodName, v8::FunctionCallback pMethodCall)
94 {
95         v8::Isolate* isolate = (v8::Isolate*)pJSRuntime;
96         v8::Isolate::Scope isolate_scope(isolate);
97         v8::HandleScope handle_scope(isolate);
98
99         CFX_WideString ws = CFX_WideString(sMethodName);
100         CFX_ByteString bsMethodName = ws.UTF8Encode();
101
102         CFX_PtrArray* pArray = (CFX_PtrArray*)isolate->GetData(g_embedderDataSlot);
103         if(!pArray) return 0;
104
105         if(nObjDefnID<0 || nObjDefnID>= pArray->GetSize()) return 0;
106         CJS_ObjDefintion* pObjDef = (CJS_ObjDefintion*)pArray->GetAt(nObjDefnID);
107         v8::Local<v8::ObjectTemplate> objTemp = v8::Local<v8::ObjectTemplate>::New(isolate, pObjDef->m_objTemplate);
108         objTemp->Set(v8::String::NewFromUtf8(isolate, bsMethodName.c_str(), v8::NewStringType::kNormal).ToLocalChecked(), v8::FunctionTemplate::New(isolate, pMethodCall), v8::ReadOnly);
109         pObjDef->m_objTemplate.Reset(isolate,objTemp);
110         return 0;
111 }
112
113 int JS_DefineObjProperty(IJS_Runtime* pJSRuntime, int nObjDefnID, const wchar_t* sPropName, v8::AccessorGetterCallback pPropGet, v8::AccessorSetterCallback pPropPut)
114 {
115         v8::Isolate* isolate = (v8::Isolate*)pJSRuntime;
116         v8::Isolate::Scope isolate_scope(isolate);
117         v8::HandleScope handle_scope(isolate);
118
119         CFX_WideString ws = CFX_WideString(sPropName);
120         CFX_ByteString bsPropertyName = ws.UTF8Encode();
121
122         CFX_PtrArray* pArray = (CFX_PtrArray*)isolate->GetData(g_embedderDataSlot);
123         if(!pArray) return 0;
124
125         if(nObjDefnID<0 || nObjDefnID>= pArray->GetSize()) return 0;
126         CJS_ObjDefintion* pObjDef = (CJS_ObjDefintion*)pArray->GetAt(nObjDefnID);
127         v8::Local<v8::ObjectTemplate> objTemp = v8::Local<v8::ObjectTemplate>::New(isolate, pObjDef->m_objTemplate);
128         objTemp->SetAccessor(v8::String::NewFromUtf8(isolate, bsPropertyName.c_str(), v8::NewStringType::kNormal).ToLocalChecked(), pPropGet, pPropPut);
129         pObjDef->m_objTemplate.Reset(isolate,objTemp);
130         return 0;
131 }
132
133 int     JS_DefineObjAllProperties(IJS_Runtime* pJSRuntime, int nObjDefnID, v8::NamedPropertyQueryCallback pPropQurey, v8::NamedPropertyGetterCallback pPropGet, v8::NamedPropertySetterCallback pPropPut, v8::NamedPropertyDeleterCallback pPropDel)
134 {
135         v8::Isolate* isolate = (v8::Isolate*)pJSRuntime;
136         v8::Isolate::Scope isolate_scope(isolate);
137         v8::HandleScope handle_scope(isolate);
138
139         CFX_PtrArray* pArray = (CFX_PtrArray*)isolate->GetData(g_embedderDataSlot);
140         if(!pArray) return 0;
141
142         if(nObjDefnID<0 || nObjDefnID>= pArray->GetSize()) return 0;
143         CJS_ObjDefintion* pObjDef = (CJS_ObjDefintion*)pArray->GetAt(nObjDefnID);
144         v8::Local<v8::ObjectTemplate> objTemp = v8::Local<v8::ObjectTemplate>::New(isolate, pObjDef->m_objTemplate);
145         objTemp->SetNamedPropertyHandler(pPropGet, pPropPut, pPropQurey, pPropDel);
146         pObjDef->m_objTemplate.Reset(isolate,objTemp);
147         return 0;
148 }
149
150 int JS_DefineObjConst(IJS_Runtime* pJSRuntime, int nObjDefnID, const wchar_t* sConstName, v8::Local<v8::Value> pDefault)
151 {
152         v8::Isolate* isolate = (v8::Isolate*)pJSRuntime;
153         v8::Isolate::Scope isolate_scope(isolate);
154         v8::HandleScope handle_scope(isolate);
155
156         CFX_PtrArray* pArray = (CFX_PtrArray*)isolate->GetData(g_embedderDataSlot);
157         if(!pArray) return 0;
158
159         CFX_WideString ws = CFX_WideString(sConstName);
160         CFX_ByteString bsConstName = ws.UTF8Encode();
161
162         if(nObjDefnID<0 || nObjDefnID>= pArray->GetSize()) return 0;
163         CJS_ObjDefintion* pObjDef = (CJS_ObjDefintion*)pArray->GetAt(nObjDefnID);
164         v8::Local<v8::ObjectTemplate> objTemp = v8::Local<v8::ObjectTemplate>::New(isolate, pObjDef->m_objTemplate);
165         objTemp->Set(isolate, bsConstName.c_str(), pDefault);
166         pObjDef->m_objTemplate.Reset(isolate,objTemp);
167         return 0;
168 }
169
170 static v8::Global<v8::ObjectTemplate>& _getGlobalObjectTemplate(IJS_Runtime* pJSRuntime)
171 {
172         v8::Isolate* isolate = (v8::Isolate*)pJSRuntime;
173         v8::Isolate::Scope isolate_scope(isolate);
174         v8::HandleScope handle_scope(isolate);
175
176         CFX_PtrArray* pArray = (CFX_PtrArray*)isolate->GetData(g_embedderDataSlot);
177         ASSERT(pArray != NULL);
178         for(int i=0; i<pArray->GetSize(); i++)
179         {
180                 CJS_ObjDefintion* pObjDef = (CJS_ObjDefintion*)pArray->GetAt(i);
181                 if(pObjDef->m_bSetAsGlobalObject)
182                         return pObjDef->m_objTemplate;
183         }
184         static v8::Global<v8::ObjectTemplate> gloabalObjectTemplate;
185         return gloabalObjectTemplate;
186 }
187
188 int JS_DefineGlobalMethod(IJS_Runtime* pJSRuntime, const wchar_t* sMethodName, v8::FunctionCallback pMethodCall)
189 {
190         v8::Isolate* isolate = (v8::Isolate*)pJSRuntime;
191         v8::Isolate::Scope isolate_scope(isolate);
192         v8::HandleScope handle_scope(isolate);
193
194         CFX_WideString ws = CFX_WideString(sMethodName);
195         CFX_ByteString bsMethodName = ws.UTF8Encode();
196
197         v8::Local<v8::FunctionTemplate> funTempl = v8::FunctionTemplate::New(isolate, pMethodCall);
198         v8::Local<v8::ObjectTemplate> objTemp;
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, globalObjTemp);
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, globalObjTemp);
228         objTemp->Set(v8::String::NewFromUtf8(isolate, bsConst.c_str(), v8::NewStringType::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::Local<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_embedderDataSlot);
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_PrivateData;
267                                 pPrivateData->ObjDefID = i;
268
269                                 v8Context->Global()->GetPrototype()->ToObject(v8Context).ToLocalChecked()->SetAlignedPointerInInternalField(0, pPrivateData);
270
271                                 if(pObjDef->m_pConstructor)
272                                         pObjDef->m_pConstructor(context, v8Context->Global()->GetPrototype()->ToObject(v8Context).ToLocalChecked(), v8Context->Global()->GetPrototype()->ToObject(v8Context).ToLocalChecked());
273                         }
274                 }
275                 else
276                 {
277                         v8::Local<v8::Object> obj = JS_NewFxDynamicObj(pJSRuntime, context, i);
278                         v8Context->Global()->Set(v8Context, objName, obj).FromJust();
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>& v8PersistentContext)
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, v8PersistentContext);
291         v8::Context::Scope context_scope(context);
292
293         CFX_PtrArray* pArray = (CFX_PtrArray*)isolate->GetData(g_embedderDataSlot);
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, bsScript.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, bsScript.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_embedderDataSlot);
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>::New(isolate, pObjDef->m_objTemplate);
384         v8::Local<v8::Object> obj;
385         if (!objTemp->NewInstance(context).ToLocal(&obj)) return v8::Local<v8::Object>();
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()->GetPrototype()->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_embedderDataSlot);
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_embedderDataSlot);
422         if(!pArray) return v8::Local<v8::Object>();
423
424         v8::Local<v8::Context> context = isolate->GetCurrentContext();
425         return context->Global()->GetPrototype()->ToObject(context).ToLocalChecked();
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->GetAlignedPointerFromInternalField(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_embedderDataSlot);
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->GetAlignedPointerFromInternalField(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->GetAlignedPointerFromInternalField(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).ToLocalChecked()->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* PropertyName, 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;
571 }
572
573 v8::Local<v8::Array> JS_GetObjectElementNames(IJS_Runtime* pJSRuntime, v8::Local<v8::Object> pObj)
574 {
575         if(pObj.IsEmpty()) return v8::Local<v8::Array>();
576         v8::Local<v8::Array> val;
577         if (!pObj->GetPropertyNames(pJSRuntime->GetCurrentContext()).ToLocal(&val)) return v8::Local<v8::Array>();
578         return val;
579 }
580
581 void JS_PutObjectString(IJS_Runtime* pJSRuntime,v8::Local<v8::Object> pObj, const wchar_t* PropertyName, const wchar_t* sValue) //VT_string
582 {
583         if(pObj.IsEmpty()) return;
584         pObj->Set(pJSRuntime->GetCurrentContext(), WSToJSString(pJSRuntime, PropertyName), WSToJSString(pJSRuntime, sValue)).FromJust();
585 }
586
587 void JS_PutObjectNumber(IJS_Runtime* pJSRuntime,v8::Local<v8::Object> pObj, const wchar_t* PropertyName, int nValue)
588 {
589         if(pObj.IsEmpty()) return;
590         pObj->Set(pJSRuntime->GetCurrentContext(), WSToJSString(pJSRuntime,PropertyName),v8::Int32::New(pJSRuntime, nValue)).FromJust();
591 }
592
593 void JS_PutObjectNumber(IJS_Runtime* pJSRuntime,v8::Local<v8::Object> pObj, const wchar_t* PropertyName, float fValue)
594 {
595         if(pObj.IsEmpty()) return;
596         pObj->Set(pJSRuntime->GetCurrentContext(), WSToJSString(pJSRuntime,PropertyName),v8::Number::New(pJSRuntime, (double)fValue)).FromJust();
597 }
598
599 void JS_PutObjectNumber(IJS_Runtime* pJSRuntime,v8::Local<v8::Object> pObj, const wchar_t* PropertyName, double dValue)
600 {
601         if(pObj.IsEmpty()) return;
602         pObj->Set(pJSRuntime->GetCurrentContext(), WSToJSString(pJSRuntime,PropertyName),v8::Number::New(pJSRuntime, (double)dValue)).FromJust();
603 }
604
605 void JS_PutObjectBoolean(IJS_Runtime* pJSRuntime,v8::Local<v8::Object> pObj, const wchar_t* PropertyName, bool bValue)
606 {
607         if(pObj.IsEmpty()) return;
608         pObj->Set(pJSRuntime->GetCurrentContext(), WSToJSString(pJSRuntime,PropertyName),v8::Boolean::New(pJSRuntime, bValue)).FromJust();
609 }
610
611 void JS_PutObjectObject(IJS_Runtime* pJSRuntime,v8::Local<v8::Object> pObj, const wchar_t* PropertyName, v8::Local<v8::Object> pPut)
612 {
613         if(pObj.IsEmpty()) return;
614         pObj->Set(pJSRuntime->GetCurrentContext(), WSToJSString(pJSRuntime,PropertyName),pPut).FromJust();
615 }
616
617 void JS_PutObjectNull(IJS_Runtime* pJSRuntime,v8::Local<v8::Object> pObj, const wchar_t* PropertyName)
618 {
619         if(pObj.IsEmpty()) return;
620         pObj->Set(pJSRuntime->GetCurrentContext(), WSToJSString(pJSRuntime,PropertyName),v8::Local<v8::Object>()).FromJust();
621 }
622
623 v8::Local<v8::Array> JS_NewArray(IJS_Runtime* pJSRuntime)
624 {
625         return v8::Array::New(pJSRuntime);
626 }
627
628 unsigned JS_PutArrayElement(IJS_Runtime* pJSRuntime, v8::Local<v8::Array> pArray,unsigned index,v8::Local<v8::Value> pValue,FXJSVALUETYPE eType)
629 {
630         if(pArray.IsEmpty()) return 0;
631         if (pArray->Set(pJSRuntime->GetCurrentContext(), index, pValue).IsNothing()) return 0;
632         return 1;
633 }
634
635 v8::Local<v8::Value> JS_GetArrayElement(IJS_Runtime* pJSRuntime, v8::Local<v8::Array> 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)) return 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::Value> 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> pValue)
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).ToLocalChecked());
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
782   // 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
784   // is removed, with _get_timezone replacing it.
785   long timezone = 0;
786   _get_timezone(&timezone);
787 #endif
788         return (double)(-(timezone * 1000));
789 }
790
791 int _getDaylightSavingTA(double d)
792 {
793         if(!FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS))
794                 return 0;
795         time_t t = (time_t)(d/1000);
796         struct tm * tmp = localtime(&t);
797         if (tmp == NULL)
798                 return 0;
799         if (tmp->tm_isdst > 0)
800                 //One hour.
801                 return (int)60*60*1000;
802         return 0;
803 }
804
805 double _Mod(double x, double y)
806 {
807         double r = fmod(x, y);
808         if (r < 0) r += y;
809         return r;
810 }
811
812 int _isfinite(double v)
813 {
814 #if _MSC_VER
815         return ::_finite(v);
816 #else
817         return std::fabs(v) < std::numeric_limits<double>::max();
818 #endif
819 }
820
821 double _toInteger(double n)
822 {
823         return (n >= 0)? FXSYS_floor(n): -FXSYS_floor(-n);
824 }
825
826 bool _isLeapYear(int year)
827 {
828         return (year%4==0)&&((year%100!=0)||(year%400!=0));
829 }
830
831 int _DayFromYear(int y)
832 {
833         return (int)(365*(y - 1970.0) + FXSYS_floor((y - 1969.0)/4) - FXSYS_floor((y - 1901.0)/100)+FXSYS_floor((y - 1601.0)/400));
834 }
835
836 double _TimeFromYear(int y)
837 {
838         return  ((double)86400000) * _DayFromYear(y);
839 }
840
841 double _TimeFromYearMonth(int y, int m)
842 {
843         static int daysMonth[12] ={ 0,31,59,90,120,151,181,212,243,273,304,334};
844         static int leapDaysMonth[12] = { 0,31,60,91,121,152,182,213,244,274,305,335};
845         int* pMonth = daysMonth;
846         if(_isLeapYear(y))
847                 pMonth = leapDaysMonth;
848         return _TimeFromYear(y) + ((double)pMonth[m])*86400000;
849 }
850
851 int _Day(double t)
852 {
853         return (int)FXSYS_floor(t / 86400000);
854 }
855
856 int _YearFromTime(double t)
857 {
858         //estimate the time.
859         int y = 1970 +(int)(t/(365.0*86400000));
860         if (_TimeFromYear(y) <= t)
861         {
862                 while(_TimeFromYear(y+1) <= t) y++;
863         }
864         else
865                 while(_TimeFromYear(y-1) > t) y--;
866         return y;
867 }
868
869 int _DayWithinYear(double t)
870 {
871         int year = _YearFromTime(t);
872         int day = _Day(t);
873         return day-_DayFromYear(year);
874 }
875
876 int _MonthFromTime(double t)
877 {
878         int day = _DayWithinYear(t);
879         int year = _YearFromTime(t);
880         if(0<=day && day <31)
881                 return 0;
882         if(31<=day && day< 59+_isLeapYear(year))
883                 return 1;
884         if((59+_isLeapYear(year))<=day && day<(90+_isLeapYear(year)))
885                 return 2;
886         if((90+_isLeapYear(year))<=day && day<(120+_isLeapYear(year)))
887                 return 3;
888         if((120+_isLeapYear(year))<=day && day<(151+_isLeapYear(year)))
889                 return 4;
890         if((151+_isLeapYear(year))<=day && day<(181+_isLeapYear(year)))
891                 return 5;
892         if((181+_isLeapYear(year))<=day && day<(212+_isLeapYear(year)))
893                 return 6;
894         if((212+_isLeapYear(year))<=day && day<(243+_isLeapYear(year)))
895                 return 7;
896         if((243+_isLeapYear(year))<=day && day<(273+_isLeapYear(year)))
897                 return 8;
898         if((273+_isLeapYear(year))<=day && day<(304+_isLeapYear(year)))
899                 return 9;
900         if((304+_isLeapYear(year))<=day && day<(334+_isLeapYear(year)))
901                 return 10;
902         if((334+_isLeapYear(year))<=day && day<(365+_isLeapYear(year)))
903                 return 11;
904
905         return -1;
906 }
907
908 int _DateFromTime(double t)
909 {
910         int day = _DayWithinYear(t);
911         int year = _YearFromTime(t);
912         bool leap = _isLeapYear(year);
913         int month = _MonthFromTime(t);
914         switch (month)
915         {
916         case 0:
917                 return day+1;
918         case 1:
919                 return day-30;
920         case 2:
921                 return day-58-leap;
922         case 3:
923                 return day-89-leap;
924         case 4:
925                 return day-119-leap;
926         case 5:
927                 return day-150-leap;
928         case 6:
929                 return day-180-leap;
930         case 7:
931                 return day-211-leap;
932         case 8:
933                 return day-242-leap;
934         case 9:
935                 return day-272-leap;
936         case 10:
937                 return day-303-leap;
938         case 11:
939                 return day-333-leap;
940         default:
941                 return 0;
942         }
943 }
944
945 double JS_GetDateTime()
946 {
947         if(!FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS))
948                 return 0;
949         time_t t = time(NULL);
950         struct tm* pTm = localtime(&t);
951
952         int year = pTm->tm_year+1900;
953         double t1 = _TimeFromYear(year);
954
955         return t1 + pTm->tm_yday*86400000.0 + pTm->tm_hour*3600000.0+pTm->tm_min*60000.0+pTm->tm_sec*1000.0;
956 }
957
958 int JS_GetYearFromTime(double dt)
959 {
960         return _YearFromTime(dt);
961 }
962
963 int JS_GetMonthFromTime(double dt)
964 {
965         return _MonthFromTime(dt);
966 }
967
968 int JS_GetDayFromTime(double dt)
969 {
970         return _DateFromTime(dt);
971 }
972
973 int JS_GetHourFromTime(double dt)
974 {
975         return (int)_Mod(FXSYS_floor((double)(dt/(60*60*1000))), 24);
976 }
977
978 int JS_GetMinFromTime(double dt)
979 {
980         return (int)_Mod(FXSYS_floor((double)(dt/(60*1000))), 60);
981 }
982
983 int JS_GetSecFromTime(double dt)
984 {
985         return (int)_Mod(FXSYS_floor((double)(dt/1000)), 60);
986 }
987
988 double JS_DateParse(const wchar_t* string)
989 {
990         v8::Isolate* pIsolate = v8::Isolate::GetCurrent();
991         v8::Isolate::Scope isolate_scope(pIsolate);
992         v8::HandleScope scope(pIsolate);
993
994         v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
995
996         //Use the built-in object method.
997         v8::Local<v8::Value> v = context->Global()->Get(context, v8::String::NewFromUtf8(pIsolate, "Date", v8::NewStringType::kNormal).ToLocalChecked()).ToLocalChecked();
998         if(v->IsObject())
999         {
1000                 v8::Local<v8::Object> o = v->ToObject(context).ToLocalChecked();
1001                 v = o->Get(context,v8::String::NewFromUtf8(pIsolate, "parse", v8::NewStringType::kNormal).ToLocalChecked()).ToLocalChecked();
1002                 if(v->IsFunction())
1003                 {
1004                         v8::Local<v8::Function> funC = v8::Local<v8::Function>::Cast(v);
1005
1006                         const int argc = 1;
1007                         v8::Local<v8::String> timeStr = WSToJSString(pIsolate, string);
1008                         v8::Local<v8::Value> argv[argc] = {timeStr};
1009                         v = funC->Call(context, context->Global(), argc, argv).ToLocalChecked();
1010                         if(v->IsNumber())
1011                         {
1012                                 double date =  v->ToNumber(context).ToLocalChecked()->Value();
1013                                 if(!_isfinite(date)) return date;
1014                                 return date + _getLocalTZA() + _getDaylightSavingTA(date);
1015                         }
1016
1017                 }
1018         }
1019         return 0;
1020 }
1021
1022 double JS_MakeDay(int nYear, int nMonth, int nDate)
1023 {
1024         if (!_isfinite(nYear) || !_isfinite(nMonth) ||!_isfinite(nDate))
1025                 return GetNan();
1026         double y = _toInteger(nYear);
1027         double m = _toInteger(nMonth);
1028         double dt = _toInteger(nDate);
1029         double ym = y + FXSYS_floor((double)m/12);
1030         double mn = _Mod(m ,12);
1031
1032         double t = _TimeFromYearMonth((int)ym,(int)mn);
1033
1034         if (_YearFromTime(t) != ym || _MonthFromTime(t) != mn ||_DateFromTime(t) != 1)
1035                 return GetNan();
1036         return _Day(t)+dt-1;
1037 }
1038
1039 double JS_MakeTime(int nHour, int nMin, int nSec, int nMs)
1040 {
1041         if (!_isfinite(nHour) ||!_isfinite(nMin) ||!_isfinite(nSec) ||!_isfinite(nMs))
1042                 return GetNan();
1043
1044         double h = _toInteger(nHour);
1045         double m = _toInteger(nMin);
1046         double s = _toInteger(nSec);
1047         double milli = _toInteger(nMs);
1048
1049         return h * 3600000 + m * 60000 + s * 1000 + milli;
1050 }
1051
1052 double JS_MakeDate(double day, double time)
1053 {
1054         if (!_isfinite(day) ||!_isfinite(time))
1055                 return GetNan();
1056
1057         return day * 86400000 + time;
1058 }
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.