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