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