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