Removing unnecessary casts from wchar_t* to wchar_t*, by various names.
[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 static FX_DWORD g_nan[2] = {0,0x7FF80000 };
25 double g_NaN = (*(double *)g_nan);
26
27
28 class CJS_PrivateData: public CFX_Object
29 {
30 public:
31         CJS_PrivateData():ObjDefID(-1), pPrivate(NULL) {}
32         int ObjDefID;
33         FX_LPVOID       pPrivate;
34 };
35
36
37 class CJS_ObjDefintion: public CFX_Object
38 {
39 public:
40         CJS_ObjDefintion(v8::Isolate* isolate, const wchar_t* sObjName, FXJSOBJTYPE eObjType, LP_CONSTRUCTOR pConstructor, LP_DESTRUCTOR pDestructor, unsigned bApplyNew):
41           objName(sObjName), objType(eObjType), m_pConstructor(pConstructor), m_pDestructor(pDestructor),m_bApplyNew(bApplyNew),m_bSetAsGlobalObject(FALSE)
42           {
43                   v8::Isolate::Scope isolate_scope(isolate);
44                   v8::HandleScope handle_scope(isolate);
45
46                   v8::Handle<v8::ObjectTemplate> objTemplate = v8::ObjectTemplate::New(isolate);
47                   objTemplate->SetInternalFieldCount(1);
48                   m_objTemplate.Reset(isolate, objTemplate);
49
50                  //Document as the global object.
51                   if(FXSYS_wcscmp(sObjName, L"Document") == 0)
52                   {
53                          m_bSetAsGlobalObject = TRUE;
54                   }
55
56           }
57           ~CJS_ObjDefintion()
58           {
59                   m_objTemplate.Reset();
60                   m_StaticObj.Reset();
61           }
62 public:
63         const wchar_t* objName;
64         FXJSOBJTYPE objType;
65         LP_CONSTRUCTOR m_pConstructor;
66         LP_DESTRUCTOR m_pDestructor;
67         unsigned m_bApplyNew;
68         FX_BOOL m_bSetAsGlobalObject;
69
70         v8::Persistent<v8::ObjectTemplate> m_objTemplate;
71         v8::Persistent<v8::Object> m_StaticObj;
72 };
73
74 int JS_DefineObj(IJS_Runtime* pJSRuntime, const wchar_t* sObjName, FXJSOBJTYPE eObjType, LP_CONSTRUCTOR pConstructor, LP_DESTRUCTOR pDestructor, unsigned bApplyNew)
75 {
76         v8::Isolate* isolate = (v8::Isolate*)pJSRuntime;
77         v8::Isolate::Scope isolate_scope(isolate);
78         v8::HandleScope handle_scope(isolate);
79         CFX_PtrArray* pArray = (CFX_PtrArray*)isolate->GetData(0);
80         if(!pArray) 
81         {
82                 pArray = FX_NEW CFX_PtrArray();
83                 isolate->SetData(0, pArray);
84         }
85         CJS_ObjDefintion* pObjDef = FX_NEW CJS_ObjDefintion(isolate, sObjName, eObjType, pConstructor, pDestructor, bApplyNew);
86         pArray->Add(pObjDef);
87         return pArray->GetSize()-1;
88 }
89
90 int JS_DefineObjMethod(IJS_Runtime* pJSRuntime, int nObjDefnID, const wchar_t* sMethodName, v8::FunctionCallback pMethodCall, unsigned nParamNum)
91 {
92         v8::Isolate* isolate = (v8::Isolate*)pJSRuntime;
93         v8::Isolate::Scope isolate_scope(isolate);
94         v8::HandleScope handle_scope(isolate);
95
96         CFX_WideString ws = CFX_WideString(sMethodName);
97         CFX_ByteString bsMethodName = ws.UTF8Encode();
98
99         CFX_PtrArray* pArray = (CFX_PtrArray*)isolate->GetData(0);
100         if(!pArray) return 0;
101
102         if(nObjDefnID<0 || nObjDefnID>= pArray->GetSize()) return 0;
103         CJS_ObjDefintion* pObjDef = (CJS_ObjDefintion*)pArray->GetAt(nObjDefnID);
104         v8::Local<v8::ObjectTemplate> objTemp = v8::Local<v8::ObjectTemplate>::New(isolate, pObjDef->m_objTemplate);
105         objTemp->Set(v8::String::NewFromUtf8(isolate, FX_LPCSTR(bsMethodName)), v8::FunctionTemplate::New(isolate, pMethodCall), v8::ReadOnly);
106         pObjDef->m_objTemplate.Reset(isolate,objTemp);
107         return 0;
108 }
109
110 int JS_DefineObjProperty(IJS_Runtime* pJSRuntime, int nObjDefnID, const wchar_t* sPropName, v8::AccessorGetterCallback pPropGet, v8::AccessorSetterCallback pPropPut)
111 {
112         v8::Isolate* isolate = (v8::Isolate*)pJSRuntime;
113         v8::Isolate::Scope isolate_scope(isolate);
114         v8::HandleScope handle_scope(isolate);
115
116         CFX_WideString ws = CFX_WideString(sPropName);
117         CFX_ByteString bsPropertyName = ws.UTF8Encode();
118
119         CFX_PtrArray* pArray = (CFX_PtrArray*)isolate->GetData(0);
120         if(!pArray) return 0;
121
122         if(nObjDefnID<0 || nObjDefnID>= pArray->GetSize()) return 0;
123         CJS_ObjDefintion* pObjDef = (CJS_ObjDefintion*)pArray->GetAt(nObjDefnID);
124         v8::Local<v8::ObjectTemplate> objTemp = v8::Local<v8::ObjectTemplate>::New(isolate, pObjDef->m_objTemplate);
125         objTemp->SetAccessor(v8::String::NewFromUtf8(isolate, FX_LPCSTR(bsPropertyName)), pPropGet, pPropPut);
126         pObjDef->m_objTemplate.Reset(isolate,objTemp);
127         return 0;
128 }
129
130 int     JS_DefineObjAllProperties(IJS_Runtime* pJSRuntime, int nObjDefnID, v8::NamedPropertyQueryCallback pPropQurey, v8::NamedPropertyGetterCallback pPropGet, v8::NamedPropertySetterCallback pPropPut, v8::NamedPropertyDeleterCallback pPropDel)
131 {
132         v8::Isolate* isolate = (v8::Isolate*)pJSRuntime;
133         v8::Isolate::Scope isolate_scope(isolate);
134         v8::HandleScope handle_scope(isolate);
135
136         CFX_PtrArray* pArray = (CFX_PtrArray*)isolate->GetData(0);
137         if(!pArray) return 0;
138
139         if(nObjDefnID<0 || nObjDefnID>= pArray->GetSize()) return 0;
140         CJS_ObjDefintion* pObjDef = (CJS_ObjDefintion*)pArray->GetAt(nObjDefnID);
141         v8::Local<v8::ObjectTemplate> objTemp = v8::Local<v8::ObjectTemplate>::New(isolate, pObjDef->m_objTemplate);
142         objTemp->SetNamedPropertyHandler(pPropGet, pPropPut, pPropQurey, pPropDel);
143         pObjDef->m_objTemplate.Reset(isolate,objTemp);
144         return 0;
145 }
146
147 int JS_DefineObjConst(IJS_Runtime* pJSRuntime, int nObjDefnID, const wchar_t* sConstName, v8::Handle<v8::Value> pDefault)
148 {
149         v8::Isolate* isolate = (v8::Isolate*)pJSRuntime;
150         v8::Isolate::Scope isolate_scope(isolate);
151         v8::HandleScope handle_scope(isolate);
152
153         CFX_PtrArray* pArray = (CFX_PtrArray*)isolate->GetData(0);
154         if(!pArray) return 0;
155
156         CFX_WideString ws = CFX_WideString(sConstName);
157         CFX_ByteString bsConstName = ws.UTF8Encode();
158
159         if(nObjDefnID<0 || nObjDefnID>= pArray->GetSize()) return 0;
160         CJS_ObjDefintion* pObjDef = (CJS_ObjDefintion*)pArray->GetAt(nObjDefnID);
161         v8::Local<v8::ObjectTemplate> objTemp = v8::Local<v8::ObjectTemplate>::New(isolate, pObjDef->m_objTemplate);
162         objTemp->Set(isolate, FX_LPCSTR(bsConstName), pDefault);
163         pObjDef->m_objTemplate.Reset(isolate,objTemp);
164         return 0;
165 }
166
167 static v8::Persistent<v8::ObjectTemplate>& _getGlobalObjectTemplate(IJS_Runtime* pJSRuntime)
168 {
169         v8::Isolate* isolate = (v8::Isolate*)pJSRuntime;
170         v8::Isolate::Scope isolate_scope(isolate);
171         v8::HandleScope handle_scope(isolate);
172
173         CFX_PtrArray* pArray = (CFX_PtrArray*)isolate->GetData(0);
174         ASSERT(pArray != NULL);
175         for(int i=0; i<pArray->GetSize(); i++)
176         {
177                 CJS_ObjDefintion* pObjDef = (CJS_ObjDefintion*)pArray->GetAt(i);
178                 if(pObjDef->m_bSetAsGlobalObject)
179                         return pObjDef->m_objTemplate;
180         }
181         static v8::Persistent<v8::ObjectTemplate> gloabalObjectTemplate;
182         return gloabalObjectTemplate;
183 }
184
185 int JS_DefineGlobalMethod(IJS_Runtime* pJSRuntime, const wchar_t* sMethodName, v8::FunctionCallback pMethodCall, unsigned nParamNum)
186 {
187         v8::Isolate* isolate = (v8::Isolate*)pJSRuntime;
188         v8::Isolate::Scope isolate_scope(isolate);
189         v8::HandleScope handle_scope(isolate);
190
191         CFX_WideString ws = CFX_WideString(sMethodName);
192         CFX_ByteString bsMethodName = ws.UTF8Encode();
193
194         v8::Local<v8::FunctionTemplate> funTempl = v8::FunctionTemplate::New(isolate, pMethodCall);
195         v8::Local<v8::ObjectTemplate> objTemp;
196
197         v8::Persistent<v8::ObjectTemplate>& globalObjTemp = _getGlobalObjectTemplate(pJSRuntime);
198         if(globalObjTemp.IsEmpty())
199                 objTemp = v8::ObjectTemplate::New(isolate);
200         else
201                 objTemp = v8::Local<v8::ObjectTemplate>::New(isolate, globalObjTemp);
202         objTemp->Set(v8::String::NewFromUtf8(isolate, FX_LPCSTR(bsMethodName)), funTempl, v8::ReadOnly);
203
204         globalObjTemp.Reset(isolate,objTemp);
205
206         return 0;
207 }
208
209 int JS_DefineGlobalConst(IJS_Runtime* pJSRuntime, const wchar_t* sConstName, v8::Handle<v8::Value> pDefault)
210 {
211         v8::Isolate* isolate = (v8::Isolate*)pJSRuntime;
212         v8::Isolate::Scope isolate_scope(isolate);
213         v8::HandleScope handle_scope(isolate);
214
215         CFX_WideString ws = CFX_WideString(sConstName);
216         CFX_ByteString bsConst= ws.UTF8Encode();
217
218         v8::Local<v8::ObjectTemplate> objTemp;
219
220         v8::Persistent<v8::ObjectTemplate>& globalObjTemp = _getGlobalObjectTemplate(pJSRuntime);
221         if(globalObjTemp.IsEmpty())
222                 objTemp = v8::ObjectTemplate::New(isolate);
223         else
224                 objTemp = v8::Local<v8::ObjectTemplate>::New(isolate, globalObjTemp);
225         objTemp->Set(v8::String::NewFromUtf8(isolate, FX_LPCSTR(bsConst)), pDefault, v8::ReadOnly);
226
227         globalObjTemp.Reset(isolate,objTemp);
228
229         return 0;
230 }
231
232
233 void JS_InitialRuntime(IJS_Runtime* pJSRuntime,IFXJS_Runtime* pFXRuntime, IFXJS_Context* context, v8::Persistent<v8::Context>& v8PersistentContext)
234 {
235         v8::Isolate* isolate = (v8::Isolate*)pJSRuntime;
236         v8::Isolate::Scope isolate_scope(isolate);
237         v8::HandleScope handle_scope(isolate);
238
239         v8::Persistent<v8::ObjectTemplate>& globalObjTemp = _getGlobalObjectTemplate(pJSRuntime);
240         v8::Handle<v8::Context> v8Context = v8::Context::New(isolate, NULL, v8::Local<v8::ObjectTemplate>::New(isolate, globalObjTemp));
241         v8::Context::Scope context_scope(v8Context);
242
243         v8::Handle<v8::External> ptr = v8::External::New(isolate, pFXRuntime);
244         v8Context->SetEmbedderData(1, ptr);
245
246         CFX_PtrArray* pArray = (CFX_PtrArray*)isolate->GetData(0);
247         if(!pArray) return;
248
249         for(int i=0; i<pArray->GetSize(); i++)
250         {
251                 CJS_ObjDefintion* pObjDef = (CJS_ObjDefintion*)pArray->GetAt(i);
252                 CFX_WideString ws = CFX_WideString(pObjDef->objName);
253                 CFX_ByteString bs = ws.UTF8Encode();
254                 v8::Handle<v8::String> objName = v8::String::NewFromUtf8(isolate,(FX_LPCSTR)bs, v8::String::kNormalString, bs.GetLength());
255
256
257                 if(pObjDef->objType == JS_DYNAMIC)
258                 {
259                         //Document is set as global object, need to construct it first.
260                         if(ws.Equal(L"Document"))
261                         {
262
263                                 CJS_PrivateData* pPrivateData = FX_NEW CJS_PrivateData;
264                                 pPrivateData->ObjDefID = i;
265                                 v8::Handle<v8::External> ptr = v8::External::New(isolate, pPrivateData);
266
267                                 v8Context->Global()->GetPrototype()->ToObject()->SetInternalField(0, ptr); 
268
269                                 if(pObjDef->m_pConstructor)
270                                         pObjDef->m_pConstructor(context, v8Context->Global()->GetPrototype()->ToObject(), v8Context->Global()->GetPrototype()->ToObject());
271                         }
272                 }
273                 else
274                 {
275                         v8::Handle<v8::Object> obj = JS_NewFxDynamicObj(pJSRuntime, context, i);
276                         v8Context->Global()->Set(objName, obj);
277                         pObjDef->m_StaticObj.Reset(isolate, obj);
278                 }
279         }
280         v8PersistentContext.Reset(isolate, v8Context);
281 }
282
283 void JS_ReleaseRuntime(IJS_Runtime* pJSRuntime, v8::Persistent<v8::Context>& v8PersistentContext)
284 {
285         v8::Isolate* isolate = (v8::Isolate*)pJSRuntime;
286         v8::Isolate::Scope isolate_scope(isolate);
287         v8::HandleScope handle_scope(isolate);
288         v8::Local<v8::Context> context = v8::Local<v8::Context>::New(isolate, v8PersistentContext);
289         v8::Context::Scope context_scope(context);
290
291         CFX_PtrArray* pArray = (CFX_PtrArray*)isolate->GetData(0);
292         if(!pArray) return ;
293
294         for(int i=0; i<pArray->GetSize(); i++)
295         {
296                 CJS_ObjDefintion* pObjDef = (CJS_ObjDefintion*)pArray->GetAt(i);
297                 if(!pObjDef->m_StaticObj.IsEmpty())
298                 {
299                         v8::Local<v8::Object> pObj = v8::Local<v8::Object>::New(isolate, pObjDef->m_StaticObj);
300                         if(pObjDef->m_pDestructor)
301                                 pObjDef->m_pDestructor(pObj);
302                         JS_FreePrivate(pObj);
303                 }
304                 delete pObjDef;
305         }
306         delete pArray;
307         isolate->SetData(0,NULL);
308 }
309
310 void JS_Initial() 
311 {
312 #ifndef FOXIT_CHROME_BUILD
313         v8::V8::InitializeICU();
314 #endif
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,(FX_LPCSTR)bsScript,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,(FX_LPCSTR)bsScript,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::Value * pError,const wchar_t * main,const wchar_t * sub)
461 {
462
463 }
464
465 unsigned JS_CalcHash(const wchar_t* main, unsigned nLen)
466 {
467         return (unsigned)FX_HashCode_String_GetW(main, nLen);
468 }
469
470 unsigned JS_CalcHash(const wchar_t* main)
471 {
472         return (unsigned)FX_HashCode_String_GetW(main, FXSYS_wcslen(main));
473 }
474 const wchar_t*  JS_GetTypeof(v8::Handle<v8::Value> pObj)
475 {
476         if(pObj.IsEmpty()) return NULL;
477         if(pObj->IsString())
478                 return VALUE_NAME_STRING;
479         if(pObj->IsNumber())
480                 return VALUE_NAME_NUMBER;
481         if(pObj->IsBoolean())
482                 return VALUE_NAME_BOOLEAN;
483         if(pObj->IsDate())
484                 return VALUE_NAME_DATE;
485         if(pObj->IsObject())
486                 return VALUE_NAME_OBJECT;
487         if(pObj->IsNull())
488                 return VALUE_NAME_NULL;
489         if(pObj->IsUndefined())
490                 return VALUE_NAME_UNDEFINED;
491         return NULL;
492
493 }
494 const wchar_t*  JS_GetClassname(v8::Handle<v8::Object> pObj)
495 {
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, (FX_LPCSTR)bs);
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         return (double)(-(timezone * 1000));
762 }
763
764 int _getDaylightSavingTA(double d)
765 {
766         if(!FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS))
767                 return 0;
768         time_t t = (time_t)(d/1000);
769         struct tm * tmp = localtime(&t);
770         if (tmp == NULL)
771                 return 0;
772         if (tmp->tm_isdst > 0)
773                 //One hour.
774                 return (int)60*60*1000;
775         return 0;
776 }
777
778 double _Mod(double x, double y)
779 {   
780         double r = fmod(x, y);
781         if (r < 0) r += y;
782         return r;
783 }
784
785 int _isfinite(double v)
786 {
787 #if _MSC_VER
788         return ::_finite(v);
789 #else
790         return std::fabs(v) < std::numeric_limits<double>::max();
791 #endif
792 }
793
794 double _toInteger(double n)
795 {
796         return (n >= 0)? FXSYS_floor(n): -FXSYS_floor(-n);
797 }
798
799 bool _isLeapYear(int year)
800 {
801         return (year%4==0)&&((year%100!=0)||(year%400!=0));
802 }
803
804 int _DayFromYear(int y)
805 {
806         return (int)(365*(y - 1970.0) + FXSYS_floor((y - 1969.0)/4) - FXSYS_floor((y - 1901.0)/100)+FXSYS_floor((y - 1601.0)/400));
807 }
808
809 double _TimeFromYear(int y)
810 {
811         return  ((double)86400000) * _DayFromYear(y);
812 }
813
814 double _TimeFromYearMonth(int y, int m)
815 {
816         static int daysMonth[12] ={ 0,31,59,90,120,151,181,212,243,273,304,334};
817         static int leapDaysMonth[12] = { 0,31,60,91,121,152,182,213,244,274,305,335};
818         int* pMonth = daysMonth;
819         if(_isLeapYear(y))
820                 pMonth = leapDaysMonth;
821         return _TimeFromYear(y) + ((double)pMonth[m])*86400000;
822 }
823
824 int _Day(double t)
825 {
826         return (int)FXSYS_floor(t / 86400000);
827 }
828
829 int _YearFromTime(double t)
830 {
831         //estimate the time.
832         int y = 1970 +(int)(t/(365.0*86400000));
833         if (_TimeFromYear(y) <= t)
834         {
835                 while(_TimeFromYear(y+1) <= t) y++;
836         }
837         else
838                 while(_TimeFromYear(y-1) > t) y--;
839         return y;
840 }
841
842 int _DayWithinYear(double t)
843 {
844         int year = _YearFromTime(t);
845         int day = _Day(t);
846         return day-_DayFromYear(year);
847 }
848
849 int _MonthFromTime(double t)
850 {
851         int day = _DayWithinYear(t);
852         int year = _YearFromTime(t);
853         if(0<=day && day <31)
854                 return 0;
855         if(31<=day && day< 59+_isLeapYear(year))
856                 return 1;
857         if((59+_isLeapYear(year))<=day && day<(90+_isLeapYear(year)))
858                 return 2;
859         if((90+_isLeapYear(year))<=day && day<(120+_isLeapYear(year)))
860                 return 3;
861         if((120+_isLeapYear(year))<=day && day<(151+_isLeapYear(year)))
862                 return 4;
863         if((151+_isLeapYear(year))<=day && day<(181+_isLeapYear(year)))
864                 return 5;
865         if((181+_isLeapYear(year))<=day && day<(212+_isLeapYear(year)))
866                 return 6;
867         if((212+_isLeapYear(year))<=day && day<(243+_isLeapYear(year)))
868                 return 7;
869         if((243+_isLeapYear(year))<=day && day<(273+_isLeapYear(year)))
870                 return 8;
871         if((273+_isLeapYear(year))<=day && day<(304+_isLeapYear(year)))
872                 return 9;
873         if((304+_isLeapYear(year))<=day && day<(334+_isLeapYear(year)))
874                 return 10;
875         if((334+_isLeapYear(year))<=day && day<(365+_isLeapYear(year)))
876                 return 11;
877
878         return -1;
879 }
880
881 int _DateFromTime(double t)
882 {
883         int day = _DayWithinYear(t);
884         int year = _YearFromTime(t);
885         bool leap = _isLeapYear(year);
886         int month = _MonthFromTime(t);
887         switch (month)
888         {
889         case 0:  
890                 return day+1;
891         case 1:  
892                 return day-30;
893         case 2:  
894                 return day-58-leap;
895         case 3:  
896                 return day-89-leap;
897         case 4:  
898                 return day-119-leap;
899         case 5:  
900                 return day-150-leap;
901         case 6:  
902                 return day-180-leap;
903         case 7:  
904                 return day-211-leap;
905         case 8:  
906                 return day-242-leap;
907         case 9:  
908                 return day-272-leap;
909         case 10: 
910                 return day-303-leap;
911         case 11: 
912                 return day-333-leap;
913         default:
914                 return 0;
915         }
916 }
917
918 double JS_GetDateTime()
919 {
920         if(!FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS))
921                 return 0;
922         time_t t = time(NULL);
923         struct tm* pTm = localtime(&t);
924
925         int year = pTm->tm_year+1900;
926         double t1 = _TimeFromYear(year);
927
928         return t1 + pTm->tm_yday*86400000.0 + pTm->tm_hour*3600000.0+pTm->tm_min*60000.0+pTm->tm_sec*1000.0;
929 }
930
931 int JS_GetYearFromTime(double dt)
932 {
933         return _YearFromTime(dt);
934 }
935
936 int JS_GetMonthFromTime(double dt)
937 {
938         return _MonthFromTime(dt);
939 }
940
941 int JS_GetDayFromTime(double dt)
942 {
943         return _DateFromTime(dt);
944 }
945
946 int JS_GetHourFromTime(double dt)
947 {
948         return (int)_Mod(FXSYS_floor((double)(dt/(60*60*1000))), 24);
949 }
950
951 int JS_GetMinFromTime(double dt)
952 {
953         return (int)_Mod(FXSYS_floor((double)(dt/(60*1000))), 60);
954 }
955
956 int JS_GetSecFromTime(double dt)
957 {
958         return (int)_Mod(FXSYS_floor((double)(dt/1000)), 60);
959 }
960
961 double JS_DateParse(const wchar_t* string)
962 {
963         v8::Isolate* pIsolate = v8::Isolate::GetCurrent();
964         v8::Isolate::Scope isolate_scope(pIsolate);
965         v8::HandleScope scope(pIsolate);
966
967         v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
968         
969         //Use the built-in object method.
970         v8::Local<v8::Value> v = context->Global()->Get(v8::String::NewFromUtf8(pIsolate, "Date"));
971         if(v->IsObject())
972         {
973                 v8::Local<v8::Object> o = v->ToObject();
974                 v = o->Get(v8::String::NewFromUtf8(pIsolate, "parse"));
975                 if(v->IsFunction())
976                 {
977                         v8::Local<v8::Function> funC = v8::Handle<v8::Function>::Cast(v);
978
979                         const int argc = 1;
980                         v8::Local<v8::String> timeStr = WSToJSString(pIsolate, string);
981                         v8::Handle<v8::Value> argv[argc] = {timeStr};
982                         v = funC->Call(context->Global(), argc, argv);
983                         if(v->IsNumber())
984                         {
985                                 double date =  v->ToNumber()->Value();
986                                 if(!_isfinite(date)) return date;
987                                 return date + _getLocalTZA() + _getDaylightSavingTA(date);
988                         }
989
990                 }
991         }
992         return 0;
993 }
994
995 double JS_MakeDay(int nYear, int nMonth, int nDate)
996 {
997         if (!_isfinite(nYear) || !_isfinite(nMonth) ||!_isfinite(nDate))
998                 return g_NaN;
999         double y = _toInteger(nYear);
1000         double m = _toInteger(nMonth);
1001         double dt = _toInteger(nDate);
1002         double ym = y + FXSYS_floor((double)m/12);
1003         double mn = _Mod(m ,12);
1004
1005         double t = _TimeFromYearMonth((int)ym,(int)mn);
1006
1007         if (_YearFromTime(t) != ym || _MonthFromTime(t) != mn ||_DateFromTime(t) != 1)
1008                 return g_NaN;
1009         return _Day(t)+dt-1;
1010 }
1011
1012 double JS_MakeTime(int nHour, int nMin, int nSec, int nMs)
1013 {
1014         if (!_isfinite(nHour) ||!_isfinite(nMin) ||!_isfinite(nSec) ||!_isfinite(nMs))
1015                 return g_NaN;
1016
1017         double h = _toInteger(nHour);
1018         double m = _toInteger(nMin);
1019         double s = _toInteger(nSec);
1020         double milli = _toInteger(nMs);
1021
1022         return h * 3600000 + m * 60000 + s * 1000 + milli;
1023 }
1024
1025 double JS_MakeDate(double day, double time)
1026 {
1027         if (!_isfinite(day) ||!_isfinite(time))
1028                 return g_NaN;
1029
1030         return day * 86400000 + time;
1031 }
1032
1033 bool JS_PortIsNan(double d)
1034 {
1035         return d != d;
1036 }
1037
1038 double JS_LocalTime(double d)
1039 {
1040         return JS_GetDateTime() + _getDaylightSavingTA(d);
1041 }
1042
1043 //JavaScript time implement End.