Merge to XFA: Remove typdefs for pointer types in fx_system.h.
[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
30
31 class CJS_PrivateData
32 {
33 public:
34         CJS_PrivateData():ObjDefID(-1), pPrivate(NULL) {}
35         int ObjDefID;
36         void*   pPrivate;
37 };
38
39
40 class CJS_ObjDefintion
41 {
42 public:
43         CJS_ObjDefintion(v8::Isolate* isolate, const wchar_t* sObjName, FXJSOBJTYPE eObjType, LP_CONSTRUCTOR pConstructor, LP_DESTRUCTOR pDestructor, unsigned bApplyNew):
44           objName(sObjName), objType(eObjType), m_pConstructor(pConstructor), m_pDestructor(pDestructor),m_bApplyNew(bApplyNew),m_bSetAsGlobalObject(FALSE)
45           {
46                   v8::Isolate::Scope isolate_scope(isolate);
47                   v8::HandleScope handle_scope(isolate);
48
49                   v8::Local<v8::ObjectTemplate> objTemplate = v8::ObjectTemplate::New(isolate);
50                   objTemplate->SetInternalFieldCount(2);
51                   m_objTemplate.Reset(isolate, objTemplate);
52
53                  //Document as the global object.
54                   if(FXSYS_wcscmp(sObjName, L"Document") == 0)
55                   {
56                          m_bSetAsGlobalObject = TRUE;
57                   }
58
59           }
60           ~CJS_ObjDefintion()
61           {
62                   m_objTemplate.Reset();
63                   m_StaticObj.Reset();
64           }
65 public:
66         const wchar_t* objName;
67         FXJSOBJTYPE objType;
68         LP_CONSTRUCTOR m_pConstructor;
69         LP_DESTRUCTOR m_pDestructor;
70         unsigned m_bApplyNew;
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, unsigned bApplyNew)
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(1);
83         if(!pArray) 
84         {
85                 pArray = FX_NEW CFX_PtrArray();
86                 isolate->SetData(1, pArray);
87         }
88         CJS_ObjDefintion* pObjDef = FX_NEW CJS_ObjDefintion(isolate, sObjName, eObjType, pConstructor, pDestructor, bApplyNew);
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(1);
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(1);
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(1);
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(1);
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(1);
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::Locker locker(isolate);
241         v8::HandleScope handle_scope(isolate);
242
243         v8::Global<v8::ObjectTemplate>& globalObjTemp = _getGlobalObjectTemplate(pJSRuntime);
244         v8::Local<v8::Context> v8Context = v8::Context::New(isolate, NULL, v8::Local<v8::ObjectTemplate>::New(isolate, globalObjTemp));
245         v8::Context::Scope context_scope(v8Context);
246
247         //v8::Local<External> ptr = External::New(isolate, pFXRuntime);
248         //v8Context->SetEmbedderData(1, ptr);
249         isolate->SetData(2, pFXRuntime);
250
251         CFX_PtrArray* pArray = (CFX_PtrArray*)isolate->GetData(1);
252         if(!pArray) return;
253
254         for(int i=0; i<pArray->GetSize(); i++)
255         {
256                 CJS_ObjDefintion* pObjDef = (CJS_ObjDefintion*)pArray->GetAt(i);
257                 CFX_WideString ws = CFX_WideString(pObjDef->objName);
258                 CFX_ByteString bs = ws.UTF8Encode();
259                 v8::Local<v8::String> objName = v8::String::NewFromUtf8(isolate, bs.c_str(), v8::NewStringType::kNormal, bs.GetLength()).ToLocalChecked();
260
261
262                 if(pObjDef->objType == JS_DYNAMIC)
263                 {
264                         //Document is set as global object, need to construct it first.
265                         CFX_WideString wsString(L"Document");
266                         if(ws.Equal(wsString))
267                         {
268
269                                 CJS_PrivateData* pPrivateData = FX_NEW CJS_PrivateData;
270                                 pPrivateData->ObjDefID = i;
271
272                                 v8Context->Global()->GetPrototype()->ToObject(v8Context).ToLocalChecked()->SetAlignedPointerInInternalField(0, pPrivateData);
273
274                                 if(pObjDef->m_pConstructor)
275                                         pObjDef->m_pConstructor(context, v8Context->Global()->GetPrototype()->ToObject(v8Context).ToLocalChecked(), v8Context->Global()->GetPrototype()->ToObject(v8Context).ToLocalChecked());
276                         }
277                 }
278                 else
279                 {
280                         v8::Local<v8::Object> obj = JS_NewFxDynamicObj(pJSRuntime, context, i);
281                         v8Context->Global()->Set(v8Context, objName, obj).FromJust();
282                         pObjDef->m_StaticObj.Reset(isolate, obj);
283                 }
284         }
285         v8PersistentContext.Reset(isolate, v8Context);
286 }
287
288 void JS_ReleaseRuntime(IJS_Runtime* pJSRuntime, v8::Global<v8::Context>& v8PersistentContext)
289 {
290         v8::Isolate* isolate = (v8::Isolate*)pJSRuntime;
291         v8::Isolate::Scope isolate_scope(isolate);
292         v8::Locker locker(isolate);
293         v8::HandleScope handle_scope(isolate);
294
295         CFX_PtrArray* pArray = (CFX_PtrArray*)isolate->GetData(1);
296         if(!pArray) return ;
297
298         for(int i=0; i<pArray->GetSize(); i++)
299         {
300                 CJS_ObjDefintion* pObjDef = (CJS_ObjDefintion*)pArray->GetAt(i);
301                 if(!pObjDef->m_StaticObj.IsEmpty())
302                 {
303                         v8::Local<v8::Object> pObj = v8::Local<v8::Object>::New(isolate, pObjDef->m_StaticObj);
304                         if(pObjDef->m_pDestructor)
305                                 pObjDef->m_pDestructor(pObj);
306                         JS_FreePrivate(pObj);
307                 }
308                 delete pObjDef;
309         }
310         delete pArray;
311         isolate->SetData(1,NULL);
312         isolate->SetData(2,NULL);
313 }
314
315 void JS_Initial() 
316 {
317 }
318 void JS_Release()
319 {
320
321 }
322 int JS_Parse(IJS_Runtime* pJSRuntime, IFXJS_Context* pJSContext, const wchar_t* script, long length, FXJSErr* perror)
323 {
324         v8::Isolate* isolate = (v8::Isolate*)pJSRuntime;
325         v8::Isolate::Scope isolate_scope(isolate);
326         v8::TryCatch try_catch(isolate);
327
328         CFX_WideString wsScript(script);
329         CFX_ByteString bsScript = wsScript.UTF8Encode();
330
331
332         v8::Local<v8::Context> context = isolate->GetCurrentContext();
333         v8::Local<v8::Script> compiled_script;
334         if (!v8::Script::Compile(context, v8::String::NewFromUtf8(isolate, bsScript.c_str(), v8::NewStringType::kNormal, bsScript.GetLength()).ToLocalChecked()).ToLocal(&compiled_script)) {
335                 v8::String::Utf8Value error(try_catch.Exception());
336                 return -1;
337         }
338         return 0;
339 }
340
341 int JS_Execute(IJS_Runtime* pJSRuntime, IFXJS_Context* pJSContext, const wchar_t* script, long length, FXJSErr* perror)
342 {
343         v8::Isolate* isolate = (v8::Isolate*)pJSRuntime;
344         v8::Isolate::Scope isolate_scope(isolate);
345         v8::TryCatch try_catch(isolate);
346
347         CFX_WideString wsScript(script);
348         CFX_ByteString bsScript = wsScript.UTF8Encode();
349
350         v8::Local<v8::Context> context = isolate->GetCurrentContext();
351         v8::Local<v8::Script> compiled_script;
352         if (!v8::Script::Compile(context, v8::String::NewFromUtf8(isolate, bsScript.c_str(), v8::NewStringType::kNormal, bsScript.GetLength()).ToLocalChecked()).ToLocal(&compiled_script)) {
353                 v8::String::Utf8Value error(try_catch.Exception());
354                 return -1;
355         }
356
357         v8::Local<v8::Value> result;
358         if (!compiled_script->Run(context).ToLocal(&result)) {
359                 v8::String::Utf8Value error(try_catch.Exception());
360                 return -1;
361         }
362         return 0;
363 }
364
365 v8::Local<v8::Object> JS_NewFxDynamicObj(IJS_Runtime* pJSRuntime, IFXJS_Context* pJSContext, int nObjDefnID)
366 {
367         v8::Isolate* isolate = (v8::Isolate*)pJSRuntime;
368         v8::Isolate::Scope isolate_scope(isolate);
369         v8::Local<v8::Context> context = isolate->GetCurrentContext();
370         if(-1 == nObjDefnID)
371         {
372                 v8::Local<v8::ObjectTemplate> objTempl = v8::ObjectTemplate::New(isolate);
373                 v8::Local<v8::Object> obj;
374                 if (objTempl->NewInstance(context).ToLocal(&obj)) return obj;
375                 return v8::Local<v8::Object>();
376         }
377
378         CFX_PtrArray* pArray = (CFX_PtrArray*)isolate->GetData(1);
379         if(!pArray) return v8::Local<v8::Object>();
380
381
382         if(nObjDefnID<0 || nObjDefnID>= pArray->GetSize()) return v8::Local<v8::Object>();
383         CJS_ObjDefintion* pObjDef = (CJS_ObjDefintion*)pArray->GetAt(nObjDefnID);
384
385         v8::Local<v8::ObjectTemplate> objTemp = v8::Local<v8::ObjectTemplate>::New(isolate, pObjDef->m_objTemplate);
386         v8::Local<v8::Object> obj;
387         if (!objTemp->NewInstance(context).ToLocal(&obj)) return v8::Local<v8::Object>();
388         CJS_PrivateData* pPrivateData = FX_NEW CJS_PrivateData;
389         pPrivateData->ObjDefID = nObjDefnID;
390
391         obj->SetAlignedPointerInInternalField(0, pPrivateData);
392         if(pObjDef->m_pConstructor)
393                 pObjDef->m_pConstructor(pJSContext, obj, context->Global()->GetPrototype()->ToObject(context).ToLocalChecked());
394
395         return obj;
396 }
397
398 v8::Local<v8::Object> JS_GetStaticObj(IJS_Runtime* pJSRuntime, int nObjDefnID)
399 {
400         v8::Isolate* isolate = (v8::Isolate*)pJSRuntime;
401         v8::Isolate::Scope isolate_scope(isolate);
402
403         CFX_PtrArray* pArray = (CFX_PtrArray*)isolate->GetData(1);
404         if(!pArray) return v8::Local<v8::Object>();
405
406         if(nObjDefnID<0 || nObjDefnID>= pArray->GetSize()) return v8::Local<v8::Object>();
407         CJS_ObjDefintion* pObjDef = (CJS_ObjDefintion*)pArray->GetAt(nObjDefnID);
408         v8::Local<v8::Object> obj = v8::Local<v8::Object>::New(isolate,pObjDef->m_StaticObj);
409         return obj;
410 }
411
412 void JS_SetThisObj(IJS_Runtime* pJSRuntime, int nThisObjID)
413 {
414         //Do nothing.
415 }
416 v8::Local<v8::Object>   JS_GetThisObj(IJS_Runtime * pJSRuntime)
417 {
418         //Return the global object.
419         v8::Isolate* isolate = (v8::Isolate*)pJSRuntime;
420         v8::Isolate::Scope isolate_scope(isolate);
421
422         CFX_PtrArray* pArray = (CFX_PtrArray*)isolate->GetData(1);
423         if(!pArray) return v8::Local<v8::Object>();
424
425         v8::Local<v8::Context> context = isolate->GetCurrentContext();
426         return context->Global()->GetPrototype()->ToObject(context).ToLocalChecked();
427 }
428
429 int     JS_GetObjDefnID(v8::Local<v8::Object> pObj)
430 {
431         if(pObj.IsEmpty() || !pObj->InternalFieldCount()) return -1;
432         CJS_PrivateData* pPrivateData = (CJS_PrivateData*)pObj->GetAlignedPointerFromInternalField(0);
433         if(pPrivateData)
434                 return pPrivateData->ObjDefID;
435         return -1;
436 }
437
438 IJS_Runtime* JS_GetRuntime(v8::Local<v8::Object> pObj)
439 {
440         if(pObj.IsEmpty()) return NULL;
441         v8::Local<v8::Context> context = pObj->CreationContext();
442         if(context.IsEmpty()) return NULL;
443         return context->GetIsolate();
444 }
445
446 int JS_GetObjDefnID(IJS_Runtime * pJSRuntime, const wchar_t* pObjName)
447 {
448         v8::Isolate* isolate = (v8::Isolate*)pJSRuntime;
449         v8::Isolate::Scope isolate_scope(isolate);
450
451         CFX_PtrArray* pArray = (CFX_PtrArray*)isolate->GetData(1);
452         if(!pArray) return -1;
453
454         for(int i=0; i<pArray->GetSize(); i++)
455         {
456                 CJS_ObjDefintion* pObjDef = (CJS_ObjDefintion*)pArray->GetAt(i);
457                 if(FXSYS_wcscmp(pObjDef->objName, pObjName) == 0)
458                         return i;
459         }
460         return -1;
461 }
462
463 void JS_Error(v8::Isolate* isolate, const CFX_WideString& message)
464 {
465     // Conversion from pdfium's wchar_t wide-strings to v8's uint16_t
466     // wide-strings isn't handled by v8, so use UTF8 as a common
467     // intermediate format.
468     CFX_ByteString utf8_message = message.UTF8Encode();
469     isolate->ThrowException(v8::String::NewFromUtf8(isolate,
470                                                     utf8_message.c_str(),
471                                                     v8::NewStringType::kNormal).ToLocalChecked());
472 }
473
474 unsigned JS_CalcHash(const wchar_t* main, unsigned nLen)
475 {
476         return (unsigned)FX_HashCode_String_GetW((const FX_WCHAR*)main, nLen);
477 }
478
479 unsigned JS_CalcHash(const wchar_t* main)
480 {
481         return (unsigned)FX_HashCode_String_GetW((const FX_WCHAR*)main, FXSYS_wcslen(main));
482 }
483 const wchar_t*  JS_GetTypeof(v8::Local<v8::Value> pObj)
484 {
485         if(pObj.IsEmpty()) return NULL;
486         if(pObj->IsString())
487                 return VALUE_NAME_STRING;
488         if(pObj->IsNumber())
489                 return VALUE_NAME_NUMBER;
490         if(pObj->IsBoolean())
491                 return VALUE_NAME_BOOLEAN;
492         if(pObj->IsDate())
493                 return VALUE_NAME_DATE;
494         if(pObj->IsObject())
495                 return VALUE_NAME_OBJECT;
496         if(pObj->IsNull())
497                 return VALUE_NAME_NULL;
498         if(pObj->IsUndefined())
499                 return VALUE_NAME_UNDEFINED;
500         return NULL;
501
502 }
503 void JS_SetPrivate(v8::Local<v8::Object> pObj, void* p)
504 {
505         JS_SetPrivate(NULL, pObj, p);
506 }
507
508 void* JS_GetPrivate(v8::Local<v8::Object> pObj)
509 {
510         return JS_GetPrivate(NULL,pObj);
511 }
512
513 void JS_SetPrivate(IJS_Runtime* pJSRuntime, v8::Local<v8::Object> pObj, void* p)
514 {
515         if(pObj.IsEmpty() || !pObj->InternalFieldCount()) return;
516         CJS_PrivateData* pPrivateData  = (CJS_PrivateData*)pObj->GetAlignedPointerFromInternalField(0);
517         if(!pPrivateData) return;
518         pPrivateData->pPrivate = p;
519 }
520
521 void* JS_GetPrivate(IJS_Runtime* pJSRuntime, v8::Local<v8::Object> pObj)
522 {
523         if(pObj.IsEmpty()) return NULL;
524         CJS_PrivateData* pPrivateData  = NULL;
525         if(pObj->InternalFieldCount())
526                 pPrivateData = (CJS_PrivateData*)pObj->GetAlignedPointerFromInternalField(0);
527         else
528         {
529                 //It could be a global proxy object.
530                 v8::Local<v8::Value> v = pObj->GetPrototype();
531                 v8::Isolate* isolate = (v8::Isolate*)pJSRuntime;
532                 v8::Local<v8::Context> context = isolate->GetCurrentContext();
533                 if(v->IsObject())
534                         pPrivateData = (CJS_PrivateData*)v->ToObject(context).ToLocalChecked()->GetAlignedPointerFromInternalField(0);
535         }
536         if(!pPrivateData) return NULL;
537         return pPrivateData->pPrivate;
538 }
539
540 void JS_FreePrivate(void* pPrivateData)
541 {
542         delete (CJS_PrivateData*)pPrivateData;
543 }
544
545 void JS_FreePrivate(v8::Local<v8::Object> pObj)
546 {
547         if(pObj.IsEmpty() || !pObj->InternalFieldCount()) return;
548         JS_FreePrivate(pObj->GetAlignedPointerFromInternalField(0));
549         pObj->SetAlignedPointerInInternalField(0, NULL);
550 }
551
552
553 v8::Local<v8::Value> JS_GetObjectValue(v8::Local<v8::Object> pObj)
554 {
555         return pObj;
556 }
557
558 v8::Local<v8::String> WSToJSString(IJS_Runtime* pJSRuntime, const wchar_t* PropertyName, int Len = -1)
559 {
560         CFX_WideString ws = CFX_WideString(PropertyName,Len);
561         CFX_ByteString bs = ws.UTF8Encode();
562         if(!pJSRuntime) pJSRuntime = v8::Isolate::GetCurrent();
563         return v8::String::NewFromUtf8(pJSRuntime, bs.c_str(), v8::NewStringType::kNormal).ToLocalChecked();
564 }
565
566 v8::Local<v8::Value> JS_GetObjectElement(IJS_Runtime* pJSRuntime, v8::Local<v8::Object> pObj,const wchar_t* PropertyName)
567 {
568         if(pObj.IsEmpty()) return v8::Local<v8::Value>();
569         v8::Local<v8::Value> val;
570         if (!pObj->Get(pJSRuntime->GetCurrentContext(), WSToJSString(pJSRuntime,PropertyName)).ToLocal(&val)) return v8::Local<v8::Value>();
571         return val;
572 }
573
574 v8::Local<v8::Array> JS_GetObjectElementNames(IJS_Runtime* pJSRuntime, v8::Local<v8::Object> pObj)
575 {
576         if(pObj.IsEmpty()) return v8::Local<v8::Array>();
577         v8::Local<v8::Array> val;
578         if (!pObj->GetPropertyNames(pJSRuntime->GetCurrentContext()).ToLocal(&val)) return v8::Local<v8::Array>();
579         return val;
580 }
581
582 void JS_PutObjectString(IJS_Runtime* pJSRuntime,v8::Local<v8::Object> pObj, const wchar_t* PropertyName, const wchar_t* sValue) //VT_string
583 {
584         if(pObj.IsEmpty()) return;
585         pObj->Set(pJSRuntime->GetCurrentContext(), WSToJSString(pJSRuntime, PropertyName), WSToJSString(pJSRuntime, sValue)).FromJust();
586 }
587
588 void JS_PutObjectNumber(IJS_Runtime* pJSRuntime,v8::Local<v8::Object> pObj, const wchar_t* PropertyName, int nValue)
589 {
590         if(pObj.IsEmpty()) return;
591         pObj->Set(pJSRuntime->GetCurrentContext(), WSToJSString(pJSRuntime,PropertyName),v8::Int32::New(pJSRuntime, nValue)).FromJust();
592 }
593
594 void JS_PutObjectNumber(IJS_Runtime* pJSRuntime,v8::Local<v8::Object> pObj, const wchar_t* PropertyName, float fValue)
595 {
596         if(pObj.IsEmpty()) return;
597         pObj->Set(pJSRuntime->GetCurrentContext(), WSToJSString(pJSRuntime,PropertyName),v8::Number::New(pJSRuntime, (double)fValue)).FromJust();
598 }
599
600 void JS_PutObjectNumber(IJS_Runtime* pJSRuntime,v8::Local<v8::Object> pObj, const wchar_t* PropertyName, double dValue)
601 {
602         if(pObj.IsEmpty()) return;
603         pObj->Set(pJSRuntime->GetCurrentContext(), WSToJSString(pJSRuntime,PropertyName),v8::Number::New(pJSRuntime, (double)dValue)).FromJust();
604 }
605
606 void JS_PutObjectBoolean(IJS_Runtime* pJSRuntime,v8::Local<v8::Object> pObj, const wchar_t* PropertyName, bool bValue)
607 {
608         if(pObj.IsEmpty()) return;
609         pObj->Set(pJSRuntime->GetCurrentContext(), WSToJSString(pJSRuntime,PropertyName),v8::Boolean::New(pJSRuntime, bValue)).FromJust();
610 }
611
612 void JS_PutObjectObject(IJS_Runtime* pJSRuntime,v8::Local<v8::Object> pObj, const wchar_t* PropertyName, v8::Local<v8::Object> pPut)
613 {
614         if(pObj.IsEmpty()) return;
615         pObj->Set(pJSRuntime->GetCurrentContext(), WSToJSString(pJSRuntime,PropertyName),pPut).FromJust();
616 }
617
618 void JS_PutObjectNull(IJS_Runtime* pJSRuntime,v8::Local<v8::Object> pObj, const wchar_t* PropertyName)
619 {
620         if(pObj.IsEmpty()) return;
621         pObj->Set(pJSRuntime->GetCurrentContext(), WSToJSString(pJSRuntime,PropertyName),v8::Local<v8::Object>()).FromJust();
622 }
623
624 v8::Local<v8::Array> JS_NewArray(IJS_Runtime* pJSRuntime)
625 {
626         return v8::Array::New(pJSRuntime);
627 }
628
629 unsigned JS_PutArrayElement(IJS_Runtime* pJSRuntime, v8::Local<v8::Array> pArray,unsigned index,v8::Local<v8::Value> pValue,FXJSVALUETYPE eType)
630 {       
631         if(pArray.IsEmpty()) return 0;
632         if (pArray->Set(pJSRuntime->GetCurrentContext(), index, pValue).IsNothing()) return 0;
633         return 1;
634 }
635
636 v8::Local<v8::Value> JS_GetArrayElement(IJS_Runtime* pJSRuntime, v8::Local<v8::Array> pArray,unsigned index)
637 {
638         if(pArray.IsEmpty()) return v8::Local<v8::Value>();
639         v8::Local<v8::Value> val;
640         if (pArray->Get(pJSRuntime->GetCurrentContext(), index).ToLocal(&val)) return v8::Local<v8::Value>();
641         return val;
642 }
643
644 unsigned JS_GetArrayLength(v8::Local<v8::Array> pArray)
645 {
646         if(pArray.IsEmpty()) return 0;
647         return pArray->Length();
648 }
649
650 v8::Local<v8::Value> JS_NewNumber(IJS_Runtime* pJSRuntime,int number)
651 {
652         return v8::Int32::New(pJSRuntime, number);
653 }
654
655 v8::Local<v8::Value> JS_NewNumber(IJS_Runtime* pJSRuntime,double number)
656 {
657         return v8::Number::New(pJSRuntime, number);
658 }
659
660 v8::Local<v8::Value> JS_NewNumber(IJS_Runtime* pJSRuntime,float number)
661 {
662         return v8::Number::New(pJSRuntime, (float)number);
663 }
664
665 v8::Local<v8::Value> JS_NewBoolean(IJS_Runtime* pJSRuntime,bool b)
666 {
667         return v8::Boolean::New(pJSRuntime, b);
668 }
669
670 v8::Local<v8::Value> JS_NewObject(IJS_Runtime* pJSRuntime,v8::Local<v8::Object> pObj)
671 {
672         if(pObj.IsEmpty()) return v8::Local<v8::Value>();
673         return pObj->Clone();
674 }
675
676 v8::Local<v8::Value> JS_NewObject2(IJS_Runtime* pJSRuntime,v8::Local<v8::Array> pObj)
677 {
678         if(pObj.IsEmpty()) return v8::Local<v8::Value>();
679         return pObj->Clone();
680 }
681
682
683 v8::Local<v8::Value> JS_NewString(IJS_Runtime* pJSRuntime,const wchar_t* string)
684 {
685         return WSToJSString(pJSRuntime, string);
686 }
687
688 v8::Local<v8::Value> JS_NewString(IJS_Runtime* pJSRuntime,const wchar_t* string, unsigned nLen)
689 {
690         return WSToJSString(pJSRuntime, string, nLen);
691 }
692
693 v8::Local<v8::Value> JS_NewNull()
694 {
695         return v8::Local<v8::Value>();
696 }
697
698 v8::Local<v8::Value> JS_NewDate(IJS_Runtime* pJSRuntime,double d)
699 {
700         return v8::Date::New(pJSRuntime->GetCurrentContext(), d).ToLocalChecked();
701 }
702
703 v8::Local<v8::Value> JS_NewValue(IJS_Runtime* pJSRuntime)
704 {
705         return v8::Local<v8::Value>();
706 }
707
708 v8::Local<v8::Value> JS_GetListValue(IJS_Runtime* pJSRuntime, v8::Local<v8::Value> pList, int index)
709 {
710
711         v8::Local<v8::Context> context = pJSRuntime->GetCurrentContext();
712         if(!pList.IsEmpty() && pList->IsObject())
713         {
714                 v8::Local<v8::Object> obj;
715                 if (pList->ToObject(context).ToLocal(&obj))
716                 {
717                         v8::Local<v8::Value> val;
718                         if (obj->Get(context, index).ToLocal(&val)) return val;
719                 }
720         }
721         return v8::Local<v8::Value>();
722 }
723
724 int     JS_ToInt32(IJS_Runtime* pJSRuntime, v8::Local<v8::Value> pValue)
725 {
726         if(pValue.IsEmpty()) return 0;
727         v8::Local<v8::Context> context = pJSRuntime->GetCurrentContext();
728         return pValue->ToInt32(context).ToLocalChecked()->Value();
729 }
730
731 bool JS_ToBoolean(IJS_Runtime* pJSRuntime, v8::Local<v8::Value> pValue)
732 {
733         if(pValue.IsEmpty()) return false;
734         v8::Local<v8::Context> context = pJSRuntime->GetCurrentContext();
735         return pValue->ToBoolean(context).ToLocalChecked()->Value();
736 }
737
738 double JS_ToNumber(IJS_Runtime* pJSRuntime, v8::Local<v8::Value> pValue)
739 {
740         if(pValue.IsEmpty()) return 0.0;
741         v8::Local<v8::Context> context = pJSRuntime->GetCurrentContext();
742         return pValue->ToNumber(context).ToLocalChecked()->Value();
743 }
744
745 v8::Local<v8::Object> JS_ToObject(IJS_Runtime* pJSRuntime, v8::Local<v8::Value> pValue)
746 {
747         if(pValue.IsEmpty()) return v8::Local<v8::Object>();
748         v8::Local<v8::Context> context = pJSRuntime->GetCurrentContext();
749         return pValue->ToObject(context).ToLocalChecked();
750 }
751
752 CFX_WideString  JS_ToString(IJS_Runtime* pJSRuntime, v8::Local<v8::Value> pValue)
753 {
754         if(pValue.IsEmpty()) return L"";
755         v8::Local<v8::Context> context = pJSRuntime->GetCurrentContext();
756         v8::String::Utf8Value s(pValue->ToString(context).ToLocalChecked());
757         return CFX_WideString::FromUTF8(*s, s.length());
758 }
759
760 v8::Local<v8::Array> JS_ToArray(IJS_Runtime* pJSRuntime, v8::Local<v8::Value> pValue)
761 {
762         if(pValue.IsEmpty()) return v8::Local<v8::Array>();
763         v8::Local<v8::Context> context = pJSRuntime->GetCurrentContext();
764         return v8::Local<v8::Array>::Cast(pValue->ToObject(context).ToLocalChecked());
765 }
766
767 void JS_ValueCopy(v8::Local<v8::Value>& pTo, v8::Local<v8::Value> pFrom)
768 {
769         pTo = pFrom;
770 }
771
772
773 //JavaScript time implement begin.
774
775 double _getLocalTZA()
776 {
777         if(!FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS))
778                 return 0;
779         time_t t = 0;
780         time(&t);
781         localtime(&t);
782 #if _MSC_VER >= 1900
783   // In gcc and in Visual Studio prior to VS 2015 'timezone' is a global
784   // variable declared in time.h. That variable was deprecated and in VS 2015
785   // is removed, with _get_timezone replacing it.
786   long timezone = 0;
787   _get_timezone(&timezone);
788 #endif
789         return (double)(-(timezone * 1000));
790 }
791
792 int _getDaylightSavingTA(double d)
793 {
794         if(!FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS))
795                 return 0;
796         time_t t = (time_t)(d/1000);
797         struct tm * tmp = localtime(&t);
798         if (tmp == NULL)
799                 return 0;
800         if (tmp->tm_isdst > 0)
801                 //One hour.
802                 return (int)60*60*1000;
803         return 0;
804 }
805
806 double _Mod(double x, double y)
807 {   
808         double r = fmod(x, y);
809         if (r < 0) r += y;
810         return r;
811 }
812
813 int _isfinite(double v)
814 {
815 #if _MSC_VER
816         return ::_finite(v);
817 #else
818         return std::fabs(v) < std::numeric_limits<double>::max();
819 #endif
820 }
821
822 double _toInteger(double n)
823 {
824         return (n >= 0)? FXSYS_floor(n): -FXSYS_floor(-n);
825 }
826
827 bool _isLeapYear(int year)
828 {
829         return (year%4==0)&&((year%100!=0)||(year%400!=0));
830 }
831
832 int _DayFromYear(int y)
833 {
834         return (int)(365*(y - 1970.0) + FXSYS_floor((y - 1969.0)/4) - FXSYS_floor((y - 1901.0)/100)+FXSYS_floor((y - 1601.0)/400));
835 }
836
837 double _TimeFromYear(int y)
838 {
839         return  ((double)86400000) * _DayFromYear(y);
840 }
841
842 double _TimeFromYearMonth(int y, int m)
843 {
844         static int daysMonth[12] ={ 0,31,59,90,120,151,181,212,243,273,304,334};
845         static int leapDaysMonth[12] = { 0,31,60,91,121,152,182,213,244,274,305,335};
846         int* pMonth = daysMonth;
847         if(_isLeapYear(y))
848                 pMonth = leapDaysMonth;
849         return _TimeFromYear(y) + ((double)pMonth[m])*86400000;
850 }
851
852 int _Day(double t)
853 {
854         return (int)FXSYS_floor(t / 86400000);
855 }
856
857 int _YearFromTime(double t)
858 {
859         //estimate the time.
860         int y = 1970 +(int)(t/(365.0*86400000));
861         if (_TimeFromYear(y) <= t)
862         {
863                 while(_TimeFromYear(y+1) <= t) y++;
864         }
865         else
866                 while(_TimeFromYear(y-1) > t) y--;
867         return y;
868 }
869
870 int _DayWithinYear(double t)
871 {
872         int year = _YearFromTime(t);
873         int day = _Day(t);
874         return day-_DayFromYear(year);
875 }
876
877 int _MonthFromTime(double t)
878 {
879         int day = _DayWithinYear(t);
880         int year = _YearFromTime(t);
881         if(0<=day && day <31)
882                 return 0;
883         if(31<=day && day< 59+_isLeapYear(year))
884                 return 1;
885         if((59+_isLeapYear(year))<=day && day<(90+_isLeapYear(year)))
886                 return 2;
887         if((90+_isLeapYear(year))<=day && day<(120+_isLeapYear(year)))
888                 return 3;
889         if((120+_isLeapYear(year))<=day && day<(151+_isLeapYear(year)))
890                 return 4;
891         if((151+_isLeapYear(year))<=day && day<(181+_isLeapYear(year)))
892                 return 5;
893         if((181+_isLeapYear(year))<=day && day<(212+_isLeapYear(year)))
894                 return 6;
895         if((212+_isLeapYear(year))<=day && day<(243+_isLeapYear(year)))
896                 return 7;
897         if((243+_isLeapYear(year))<=day && day<(273+_isLeapYear(year)))
898                 return 8;
899         if((273+_isLeapYear(year))<=day && day<(304+_isLeapYear(year)))
900                 return 9;
901         if((304+_isLeapYear(year))<=day && day<(334+_isLeapYear(year)))
902                 return 10;
903         if((334+_isLeapYear(year))<=day && day<(365+_isLeapYear(year)))
904                 return 11;
905
906         return -1;
907 }
908
909 int _DateFromTime(double t)
910 {
911         int day = _DayWithinYear(t);
912         int year = _YearFromTime(t);
913         bool leap = _isLeapYear(year);
914         int month = _MonthFromTime(t);
915         switch (month)
916         {
917         case 0:  
918                 return day+1;
919         case 1:  
920                 return day-30;
921         case 2:  
922                 return day-58-leap;
923         case 3:  
924                 return day-89-leap;
925         case 4:  
926                 return day-119-leap;
927         case 5:  
928                 return day-150-leap;
929         case 6:  
930                 return day-180-leap;
931         case 7:  
932                 return day-211-leap;
933         case 8:  
934                 return day-242-leap;
935         case 9:  
936                 return day-272-leap;
937         case 10: 
938                 return day-303-leap;
939         case 11: 
940                 return day-333-leap;
941         default:
942                 return 0;
943         }
944 }
945
946 double JS_GetDateTime()
947 {
948         if(!FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS))
949                 return 0;
950         time_t t = time(NULL);
951         struct tm* pTm = localtime(&t);
952
953         int year = pTm->tm_year+1900;
954         double t1 = _TimeFromYear(year);
955
956         return t1 + pTm->tm_yday*86400000.0 + pTm->tm_hour*3600000.0+pTm->tm_min*60000.0+pTm->tm_sec*1000.0;
957 }
958
959 int JS_GetYearFromTime(double dt)
960 {
961         return _YearFromTime(dt);
962 }
963
964 int JS_GetMonthFromTime(double dt)
965 {
966         return _MonthFromTime(dt);
967 }
968
969 int JS_GetDayFromTime(double dt)
970 {
971         return _DateFromTime(dt);
972 }
973
974 int JS_GetHourFromTime(double dt)
975 {
976         return (int)_Mod(FXSYS_floor((double)(dt/(60*60*1000))), 24);
977 }
978
979 int JS_GetMinFromTime(double dt)
980 {
981         return (int)_Mod(FXSYS_floor((double)(dt/(60*1000))), 60);
982 }
983
984 int JS_GetSecFromTime(double dt)
985 {
986         return (int)_Mod(FXSYS_floor((double)(dt/1000)), 60);
987 }
988
989 double JS_DateParse(const wchar_t* string)
990 {
991         v8::Isolate* pIsolate = v8::Isolate::GetCurrent();
992         v8::Isolate::Scope isolate_scope(pIsolate);
993         v8::HandleScope scope(pIsolate);
994
995         v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
996         
997         //Use the built-in object method.
998         v8::Local<v8::Value> v = context->Global()->Get(context, v8::String::NewFromUtf8(pIsolate, "Date", v8::NewStringType::kNormal).ToLocalChecked()).ToLocalChecked();
999         if(v->IsObject())
1000         {
1001                 v8::Local<v8::Object> o = v->ToObject(context).ToLocalChecked();
1002                 v = o->Get(context,v8::String::NewFromUtf8(pIsolate, "parse", v8::NewStringType::kNormal).ToLocalChecked()).ToLocalChecked();
1003                 if(v->IsFunction())
1004                 {
1005                         v8::Local<v8::Function> funC = v8::Local<v8::Function>::Cast(v);
1006
1007                         const int argc = 1;
1008                         v8::Local<v8::String> timeStr = WSToJSString(pIsolate, string);
1009                         v8::Local<v8::Value> argv[argc] = {timeStr};
1010                         v = funC->Call(context, context->Global(), argc, argv).ToLocalChecked();
1011                         if(v->IsNumber())
1012                         {
1013                                 double date =  v->ToNumber(context).ToLocalChecked()->Value();
1014                                 if(!_isfinite(date)) return date;
1015                                 return date + _getLocalTZA() + _getDaylightSavingTA(date);
1016                         }
1017
1018                 }
1019         }
1020         return 0;
1021 }
1022
1023 double JS_MakeDay(int nYear, int nMonth, int nDate)
1024 {
1025         if (!_isfinite(nYear) || !_isfinite(nMonth) ||!_isfinite(nDate))
1026                 return GetNan();
1027         double y = _toInteger(nYear);
1028         double m = _toInteger(nMonth);
1029         double dt = _toInteger(nDate);
1030         double ym = y + FXSYS_floor((double)m/12);
1031         double mn = _Mod(m ,12);
1032
1033         double t = _TimeFromYearMonth((int)ym,(int)mn);
1034
1035         if (_YearFromTime(t) != ym || _MonthFromTime(t) != mn ||_DateFromTime(t) != 1)
1036                 return GetNan();
1037         return _Day(t)+dt-1;
1038 }
1039
1040 double JS_MakeTime(int nHour, int nMin, int nSec, int nMs)
1041 {
1042         if (!_isfinite(nHour) ||!_isfinite(nMin) ||!_isfinite(nSec) ||!_isfinite(nMs))
1043                 return GetNan();
1044
1045         double h = _toInteger(nHour);
1046         double m = _toInteger(nMin);
1047         double s = _toInteger(nSec);
1048         double milli = _toInteger(nMs);
1049
1050         return h * 3600000 + m * 60000 + s * 1000 + milli;
1051 }
1052
1053 double JS_MakeDate(double day, double time)
1054 {
1055         if (!_isfinite(day) ||!_isfinite(time))
1056                 return GetNan();
1057
1058         return day * 86400000 + time;
1059 }
1060
1061 bool JS_PortIsNan(double d)
1062 {
1063         return d != d;
1064 }
1065
1066 double JS_LocalTime(double d)
1067 {
1068         return JS_GetDateTime() + _getDaylightSavingTA(d);
1069 }
1070
1071 //JavaScript time implement End.