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