Use phantom handles instead of weak handles
[pdfium.git] / fpdfsdk / src / jsapi / fxjs_v8.cpp
1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4  
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7 #include "../../../core/include/fxcrt/fx_basic.h"
8 #include "../../../core/include/fxcrt/fx_ext.h" 
9 #include "../../include/jsapi/fxjs_v8.h"
10 #include "../../include/fsdk_define.h"
11 #include "time.h"
12 #include <cmath>
13 #include <limits>
14
15 #define VALUE_NAME_STRING               L"string"
16 #define VALUE_NAME_NUMBER               L"number"
17 #define VALUE_NAME_BOOLEAN              L"boolean"
18 #define VALUE_NAME_DATE                 L"date"
19 #define VALUE_NAME_OBJECT               L"object"
20 #define VALUE_NAME_FXOBJ                L"fxobj"
21 #define VALUE_NAME_NULL                 L"null"
22 #define VALUE_NAME_UNDEFINED    L"undefined"
23
24 const static FX_DWORD g_nan[2] = {0,0x7FF80000 };
25 static double GetNan()
26 {
27   return *(double*)g_nan;
28 }
29
30
31 class CJS_PrivateData
32 {
33 public:
34         CJS_PrivateData():ObjDefID(-1), pPrivate(NULL) {}
35         int ObjDefID;
36         FX_LPVOID       pPrivate;
37 };
38
39
40 class CJS_ObjDefintion
41 {
42 public:
43         CJS_ObjDefintion(v8::Isolate* isolate, const wchar_t* sObjName, FXJSOBJTYPE eObjType, LP_CONSTRUCTOR pConstructor, LP_DESTRUCTOR pDestructor, unsigned bApplyNew):
44           objName(sObjName), objType(eObjType), m_pConstructor(pConstructor), m_pDestructor(pDestructor),m_bApplyNew(bApplyNew),m_bSetAsGlobalObject(FALSE)
45           {
46                   v8::Isolate::Scope isolate_scope(isolate);
47                   v8::HandleScope handle_scope(isolate);
48
49                   v8::Handle<v8::ObjectTemplate> objTemplate = v8::ObjectTemplate::New(isolate);
50                   objTemplate->SetInternalFieldCount(2);
51                   m_objTemplate.Reset(isolate, objTemplate);
52
53                  //Document as the global object.
54                   if(FXSYS_wcscmp(sObjName, L"Document") == 0)
55                   {
56                          m_bSetAsGlobalObject = TRUE;
57                   }
58
59           }
60           ~CJS_ObjDefintion()
61           {
62                   m_objTemplate.Reset();
63                   m_StaticObj.Reset();
64           }
65 public:
66         const wchar_t* objName;
67         FXJSOBJTYPE objType;
68         LP_CONSTRUCTOR m_pConstructor;
69         LP_DESTRUCTOR m_pDestructor;
70         unsigned m_bApplyNew;
71         FX_BOOL m_bSetAsGlobalObject;
72
73         v8::Persistent<v8::ObjectTemplate> m_objTemplate;
74         v8::Persistent<v8::Object> m_StaticObj;
75 };
76
77 int JS_DefineObj(IJS_Runtime* pJSRuntime, const wchar_t* sObjName, FXJSOBJTYPE eObjType, LP_CONSTRUCTOR pConstructor, LP_DESTRUCTOR pDestructor, unsigned bApplyNew)
78 {
79         v8::Isolate* isolate = (v8::Isolate*)pJSRuntime;
80         v8::Isolate::Scope isolate_scope(isolate);
81         v8::HandleScope handle_scope(isolate);
82         CFX_PtrArray* pArray = (CFX_PtrArray*)isolate->GetData(0);
83         if(!pArray)
84         {
85                 pArray = new CFX_PtrArray();
86                 isolate->SetData(0, pArray);
87         }
88         CJS_ObjDefintion* pObjDef = new CJS_ObjDefintion(isolate, sObjName, eObjType, pConstructor, pDestructor, bApplyNew);
89         pArray->Add(pObjDef);
90         return pArray->GetSize()-1;
91 }
92
93 int JS_DefineObjMethod(IJS_Runtime* pJSRuntime, int nObjDefnID, const wchar_t* sMethodName, v8::FunctionCallback pMethodCall)
94 {
95         v8::Isolate* isolate = (v8::Isolate*)pJSRuntime;
96         v8::Isolate::Scope isolate_scope(isolate);
97         v8::HandleScope handle_scope(isolate);
98
99         CFX_WideString ws = CFX_WideString(sMethodName);
100         CFX_ByteString bsMethodName = ws.UTF8Encode();
101
102         CFX_PtrArray* pArray = (CFX_PtrArray*)isolate->GetData(0);
103         if(!pArray) return 0;
104
105         if(nObjDefnID<0 || nObjDefnID>= pArray->GetSize()) return 0;
106         CJS_ObjDefintion* pObjDef = (CJS_ObjDefintion*)pArray->GetAt(nObjDefnID);
107         v8::Local<v8::ObjectTemplate> objTemp = v8::Local<v8::ObjectTemplate>::New(isolate, pObjDef->m_objTemplate);
108         objTemp->Set(v8::String::NewFromUtf8(isolate, FX_LPCSTR(bsMethodName)), v8::FunctionTemplate::New(isolate, pMethodCall), v8::ReadOnly);
109         pObjDef->m_objTemplate.Reset(isolate,objTemp);
110         return 0;
111 }
112
113 int JS_DefineObjProperty(IJS_Runtime* pJSRuntime, int nObjDefnID, const wchar_t* sPropName, v8::AccessorGetterCallback pPropGet, v8::AccessorSetterCallback pPropPut)
114 {
115         v8::Isolate* isolate = (v8::Isolate*)pJSRuntime;
116         v8::Isolate::Scope isolate_scope(isolate);
117         v8::HandleScope handle_scope(isolate);
118
119         CFX_WideString ws = CFX_WideString(sPropName);
120         CFX_ByteString bsPropertyName = ws.UTF8Encode();
121
122         CFX_PtrArray* pArray = (CFX_PtrArray*)isolate->GetData(0);
123         if(!pArray) return 0;
124
125         if(nObjDefnID<0 || nObjDefnID>= pArray->GetSize()) return 0;
126         CJS_ObjDefintion* pObjDef = (CJS_ObjDefintion*)pArray->GetAt(nObjDefnID);
127         v8::Local<v8::ObjectTemplate> objTemp = v8::Local<v8::ObjectTemplate>::New(isolate, pObjDef->m_objTemplate);
128         objTemp->SetAccessor(v8::String::NewFromUtf8(isolate, FX_LPCSTR(bsPropertyName)), pPropGet, pPropPut);
129         pObjDef->m_objTemplate.Reset(isolate,objTemp);
130         return 0;
131 }
132
133 int     JS_DefineObjAllProperties(IJS_Runtime* pJSRuntime, int nObjDefnID, v8::NamedPropertyQueryCallback pPropQurey, v8::NamedPropertyGetterCallback pPropGet, v8::NamedPropertySetterCallback pPropPut, v8::NamedPropertyDeleterCallback pPropDel)
134 {
135         v8::Isolate* isolate = (v8::Isolate*)pJSRuntime;
136         v8::Isolate::Scope isolate_scope(isolate);
137         v8::HandleScope handle_scope(isolate);
138
139         CFX_PtrArray* pArray = (CFX_PtrArray*)isolate->GetData(0);
140         if(!pArray) return 0;
141
142         if(nObjDefnID<0 || nObjDefnID>= pArray->GetSize()) return 0;
143         CJS_ObjDefintion* pObjDef = (CJS_ObjDefintion*)pArray->GetAt(nObjDefnID);
144         v8::Local<v8::ObjectTemplate> objTemp = v8::Local<v8::ObjectTemplate>::New(isolate, pObjDef->m_objTemplate);
145         objTemp->SetNamedPropertyHandler(pPropGet, pPropPut, pPropQurey, pPropDel);
146         pObjDef->m_objTemplate.Reset(isolate,objTemp);
147         return 0;
148 }
149
150 int JS_DefineObjConst(IJS_Runtime* pJSRuntime, int nObjDefnID, const wchar_t* sConstName, v8::Handle<v8::Value> pDefault)
151 {
152         v8::Isolate* isolate = (v8::Isolate*)pJSRuntime;
153         v8::Isolate::Scope isolate_scope(isolate);
154         v8::HandleScope handle_scope(isolate);
155
156         CFX_PtrArray* pArray = (CFX_PtrArray*)isolate->GetData(0);
157         if(!pArray) return 0;
158
159         CFX_WideString ws = CFX_WideString(sConstName);
160         CFX_ByteString bsConstName = ws.UTF8Encode();
161
162         if(nObjDefnID<0 || nObjDefnID>= pArray->GetSize()) return 0;
163         CJS_ObjDefintion* pObjDef = (CJS_ObjDefintion*)pArray->GetAt(nObjDefnID);
164         v8::Local<v8::ObjectTemplate> objTemp = v8::Local<v8::ObjectTemplate>::New(isolate, pObjDef->m_objTemplate);
165         objTemp->Set(isolate, FX_LPCSTR(bsConstName), pDefault);
166         pObjDef->m_objTemplate.Reset(isolate,objTemp);
167         return 0;
168 }
169
170 static v8::Persistent<v8::ObjectTemplate>& _getGlobalObjectTemplate(IJS_Runtime* pJSRuntime)
171 {
172         v8::Isolate* isolate = (v8::Isolate*)pJSRuntime;
173         v8::Isolate::Scope isolate_scope(isolate);
174         v8::HandleScope handle_scope(isolate);
175
176         CFX_PtrArray* pArray = (CFX_PtrArray*)isolate->GetData(0);
177         ASSERT(pArray != NULL);
178         for(int i=0; i<pArray->GetSize(); i++)
179         {
180                 CJS_ObjDefintion* pObjDef = (CJS_ObjDefintion*)pArray->GetAt(i);
181                 if(pObjDef->m_bSetAsGlobalObject)
182                         return pObjDef->m_objTemplate;
183         }
184         static v8::Persistent<v8::ObjectTemplate> gloabalObjectTemplate;
185         return gloabalObjectTemplate;
186 }
187
188 int JS_DefineGlobalMethod(IJS_Runtime* pJSRuntime, const wchar_t* sMethodName, v8::FunctionCallback pMethodCall)
189 {
190         v8::Isolate* isolate = (v8::Isolate*)pJSRuntime;
191         v8::Isolate::Scope isolate_scope(isolate);
192         v8::HandleScope handle_scope(isolate);
193
194         CFX_WideString ws = CFX_WideString(sMethodName);
195         CFX_ByteString bsMethodName = ws.UTF8Encode();
196
197         v8::Local<v8::FunctionTemplate> funTempl = v8::FunctionTemplate::New(isolate, pMethodCall);
198         v8::Local<v8::ObjectTemplate> objTemp;
199
200         v8::Persistent<v8::ObjectTemplate>& globalObjTemp = _getGlobalObjectTemplate(pJSRuntime);
201         if(globalObjTemp.IsEmpty())
202                 objTemp = v8::ObjectTemplate::New(isolate);
203         else
204                 objTemp = v8::Local<v8::ObjectTemplate>::New(isolate, globalObjTemp);
205         objTemp->Set(v8::String::NewFromUtf8(isolate, FX_LPCSTR(bsMethodName)), funTempl, v8::ReadOnly);
206
207         globalObjTemp.Reset(isolate,objTemp);
208
209         return 0;
210 }
211
212 int JS_DefineGlobalConst(IJS_Runtime* pJSRuntime, const wchar_t* sConstName, v8::Handle<v8::Value> pDefault)
213 {
214         v8::Isolate* isolate = (v8::Isolate*)pJSRuntime;
215         v8::Isolate::Scope isolate_scope(isolate);
216         v8::HandleScope handle_scope(isolate);
217
218         CFX_WideString ws = CFX_WideString(sConstName);
219         CFX_ByteString bsConst= ws.UTF8Encode();
220
221         v8::Local<v8::ObjectTemplate> objTemp;
222
223         v8::Persistent<v8::ObjectTemplate>& globalObjTemp = _getGlobalObjectTemplate(pJSRuntime);
224         if(globalObjTemp.IsEmpty())
225                 objTemp = v8::ObjectTemplate::New(isolate);
226         else
227                 objTemp = v8::Local<v8::ObjectTemplate>::New(isolate, globalObjTemp);
228         objTemp->Set(v8::String::NewFromUtf8(isolate, FX_LPCSTR(bsConst)), pDefault, v8::ReadOnly);
229
230         globalObjTemp.Reset(isolate,objTemp);
231
232         return 0;
233 }
234
235
236 void JS_InitialRuntime(IJS_Runtime* pJSRuntime,IFXJS_Runtime* pFXRuntime, IFXJS_Context* context, v8::Persistent<v8::Context>& v8PersistentContext)
237 {
238         v8::Isolate* isolate = (v8::Isolate*)pJSRuntime;
239         v8::Isolate::Scope isolate_scope(isolate);
240         v8::HandleScope handle_scope(isolate);
241
242         v8::Persistent<v8::ObjectTemplate>& globalObjTemp = _getGlobalObjectTemplate(pJSRuntime);
243         v8::Handle<v8::Context> v8Context = v8::Context::New(isolate, NULL, v8::Local<v8::ObjectTemplate>::New(isolate, globalObjTemp));
244         v8::Context::Scope context_scope(v8Context);
245
246         v8::Handle<v8::External> ptr = v8::External::New(isolate, pFXRuntime);
247         v8Context->SetEmbedderData(1, ptr);
248
249         CFX_PtrArray* pArray = (CFX_PtrArray*)isolate->GetData(0);
250         if(!pArray) return;
251
252         for(int i=0; i<pArray->GetSize(); i++)
253         {
254                 CJS_ObjDefintion* pObjDef = (CJS_ObjDefintion*)pArray->GetAt(i);
255                 CFX_WideString ws = CFX_WideString(pObjDef->objName);
256                 CFX_ByteString bs = ws.UTF8Encode();
257                 v8::Handle<v8::String> objName = v8::String::NewFromUtf8(isolate, bs.c_str(), v8::String::kNormalString, bs.GetLength());
258
259
260                 if(pObjDef->objType == JS_DYNAMIC)
261                 {
262                         //Document is set as global object, need to construct it first.
263                         if(ws.Equal(L"Document"))
264                         {
265
266                                 CJS_PrivateData* pPrivateData = new CJS_PrivateData;
267                                 pPrivateData->ObjDefID = i;
268
269                                 v8Context->Global()->GetPrototype()->ToObject()->SetAlignedPointerInInternalField(0, pPrivateData);
270
271                                 if(pObjDef->m_pConstructor)
272                                         pObjDef->m_pConstructor(context, v8Context->Global()->GetPrototype()->ToObject(), v8Context->Global()->GetPrototype()->ToObject());
273                         }
274                 }
275                 else
276                 {
277                         v8::Handle<v8::Object> obj = JS_NewFxDynamicObj(pJSRuntime, context, i);
278                         v8Context->Global()->Set(objName, obj);
279                         pObjDef->m_StaticObj.Reset(isolate, obj);
280                 }
281         }
282         v8PersistentContext.Reset(isolate, v8Context);
283 }
284
285 void JS_ReleaseRuntime(IJS_Runtime* pJSRuntime, v8::Persistent<v8::Context>& v8PersistentContext)
286 {
287         v8::Isolate* isolate = (v8::Isolate*)pJSRuntime;
288         v8::Isolate::Scope isolate_scope(isolate);
289         v8::HandleScope handle_scope(isolate);
290         v8::Local<v8::Context> context = v8::Local<v8::Context>::New(isolate, v8PersistentContext);
291         v8::Context::Scope context_scope(context);
292
293         CFX_PtrArray* pArray = (CFX_PtrArray*)isolate->GetData(0);
294         if(!pArray) return ;
295
296         for(int i=0; i<pArray->GetSize(); i++)
297         {
298                 CJS_ObjDefintion* pObjDef = (CJS_ObjDefintion*)pArray->GetAt(i);
299                 if(!pObjDef->m_StaticObj.IsEmpty())
300                 {
301                         v8::Local<v8::Object> pObj = v8::Local<v8::Object>::New(isolate, pObjDef->m_StaticObj);
302                         if(pObjDef->m_pDestructor)
303                                 pObjDef->m_pDestructor(pObj);
304                         JS_FreePrivate(pObj);
305                 }
306                 delete pObjDef;
307         }
308         delete pArray;
309         isolate->SetData(0,NULL);
310 }
311
312 void JS_Initial() 
313 {
314 }
315 void JS_Release()
316 {
317
318 }
319 int JS_Parse(IJS_Runtime* pJSRuntime, IFXJS_Context* pJSContext, const wchar_t* script, long length, FXJSErr* perror)
320 {
321         v8::Isolate* isolate = (v8::Isolate*)pJSRuntime;
322         v8::Isolate::Scope isolate_scope(isolate);
323         v8::TryCatch try_catch;
324
325         CFX_WideString wsScript(script);
326         CFX_ByteString bsScript = wsScript.UTF8Encode();
327
328
329         v8::Handle<v8::Script> compiled_script = v8::Script::Compile(v8::String::NewFromUtf8(isolate, bsScript.c_str(), v8::String::kNormalString, bsScript.GetLength()));
330         if (compiled_script.IsEmpty()) {
331                 v8::String::Utf8Value error(try_catch.Exception());
332                 return -1;
333         }
334         return 0;
335 }
336
337 int JS_Execute(IJS_Runtime* pJSRuntime, IFXJS_Context* pJSContext, const wchar_t* script, long length, FXJSErr* perror)
338 {
339         v8::Isolate* isolate = (v8::Isolate*)pJSRuntime;
340         v8::Isolate::Scope isolate_scope(isolate);
341         v8::TryCatch try_catch;
342
343         CFX_WideString wsScript(script);
344         CFX_ByteString bsScript = wsScript.UTF8Encode();
345
346     v8::Handle<v8::Script> compiled_script = v8::Script::Compile(v8::String::NewFromUtf8(isolate, bsScript.c_str(), v8::String::kNormalString, bsScript.GetLength()));
347         if (compiled_script.IsEmpty()) {
348                 v8::String::Utf8Value error(try_catch.Exception());
349                 return -1;
350         }
351
352         v8::Handle<v8::Value> result = compiled_script->Run();
353         if (result.IsEmpty()) {
354                 v8::String::Utf8Value error(try_catch.Exception());
355                 return -1;
356         }
357         return 0;
358 }
359
360 v8::Handle<v8::Object> JS_NewFxDynamicObj(IJS_Runtime* pJSRuntime, IFXJS_Context* pJSContext, int nObjDefnID)
361 {
362         v8::Isolate* isolate = (v8::Isolate*)pJSRuntime;
363         v8::Isolate::Scope isolate_scope(isolate);
364         if(-1 == nObjDefnID)
365         {
366                 v8::Local<v8::ObjectTemplate> objTempl = v8::ObjectTemplate::New(isolate);
367                 return objTempl->NewInstance();
368         }
369
370         CFX_PtrArray* pArray = (CFX_PtrArray*)isolate->GetData(0);
371         if(!pArray) return v8::Handle<v8::Object>();
372
373
374         if(nObjDefnID<0 || nObjDefnID>= pArray->GetSize()) return v8::Handle<v8::Object>();
375         CJS_ObjDefintion* pObjDef = (CJS_ObjDefintion*)pArray->GetAt(nObjDefnID);
376
377         v8::Local<v8::Context> context = isolate->GetCurrentContext();
378         v8::Local<v8::ObjectTemplate> objTemp = v8::Local<v8::ObjectTemplate>::New(isolate, pObjDef->m_objTemplate);
379         v8::Local<v8::Object> obj = objTemp->NewInstance();
380
381         CJS_PrivateData* pPrivateData = new CJS_PrivateData;
382         pPrivateData->ObjDefID = nObjDefnID;
383
384         obj->SetAlignedPointerInInternalField(0, pPrivateData);
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         CJS_PrivateData* pPrivateData = (CJS_PrivateData*)pObj->GetAlignedPointerFromInternalField(0);
426         if(pPrivateData)
427                 return pPrivateData->ObjDefID;
428         return -1;
429 }
430
431 IJS_Runtime* JS_GetRuntime(v8::Handle<v8::Object> pObj)
432 {
433         if(pObj.IsEmpty()) return NULL;
434         v8::Local<v8::Context> context = pObj->CreationContext();
435         if(context.IsEmpty()) return NULL;
436         return context->GetIsolate();
437 }
438
439 int JS_GetObjDefnID(IJS_Runtime * pJSRuntime, const wchar_t* pObjName)
440 {
441         v8::Isolate* isolate = (v8::Isolate*)pJSRuntime;
442         v8::Isolate::Scope isolate_scope(isolate);
443
444         CFX_PtrArray* pArray = (CFX_PtrArray*)isolate->GetData(0);
445         if(!pArray) return -1;
446
447         for(int i=0; i<pArray->GetSize(); i++)
448         {
449                 CJS_ObjDefintion* pObjDef = (CJS_ObjDefintion*)pArray->GetAt(i);
450                 if(FXSYS_wcscmp(pObjDef->objName, pObjName) == 0)
451                         return i;
452         }
453         return -1;
454 }
455
456 void JS_Error(v8::Isolate* isolate, const CFX_WideString& message)
457 {
458     // Conversion from pdfium's wchar_t wide-strings to v8's uint16_t
459     // wide-strings isn't handled by v8, so use UTF8 as a common
460     // intermediate format.
461     CFX_ByteString utf8_message = message.UTF8Encode();
462     isolate->ThrowException(v8::String::NewFromUtf8(isolate,
463                                                     utf8_message.c_str()));
464 }
465
466 unsigned JS_CalcHash(const wchar_t* main, unsigned nLen)
467 {
468         return (unsigned)FX_HashCode_String_GetW(main, nLen);
469 }
470
471 unsigned JS_CalcHash(const wchar_t* main)
472 {
473         return (unsigned)FX_HashCode_String_GetW(main, FXSYS_wcslen(main));
474 }
475 const wchar_t*  JS_GetTypeof(v8::Handle<v8::Value> pObj)
476 {
477         if(pObj.IsEmpty()) return NULL;
478         if(pObj->IsString())
479                 return VALUE_NAME_STRING;
480         if(pObj->IsNumber())
481                 return VALUE_NAME_NUMBER;
482         if(pObj->IsBoolean())
483                 return VALUE_NAME_BOOLEAN;
484         if(pObj->IsDate())
485                 return VALUE_NAME_DATE;
486         if(pObj->IsObject())
487                 return VALUE_NAME_OBJECT;
488         if(pObj->IsNull())
489                 return VALUE_NAME_NULL;
490         if(pObj->IsUndefined())
491                 return VALUE_NAME_UNDEFINED;
492         return NULL;
493
494 }
495 void JS_SetPrivate(v8::Handle<v8::Object> pObj, void* p)
496 {
497         JS_SetPrivate(NULL, pObj, p);
498 }
499
500 void* JS_GetPrivate(v8::Handle<v8::Object> pObj)
501 {
502         return JS_GetPrivate(NULL,pObj);
503 }
504
505 void JS_SetPrivate(IJS_Runtime* pJSRuntime, v8::Handle<v8::Object> pObj, void* p)
506 {
507         if(pObj.IsEmpty() || !pObj->InternalFieldCount()) return;
508         CJS_PrivateData* pPrivateData  = (CJS_PrivateData*)pObj->GetAlignedPointerFromInternalField(0);
509         if(!pPrivateData) return;
510         pPrivateData->pPrivate = p;
511 }
512
513 void* JS_GetPrivate(IJS_Runtime* pJSRuntime, v8::Handle<v8::Object> pObj)
514 {
515         if(pObj.IsEmpty()) return NULL;
516         CJS_PrivateData* pPrivateData  = NULL;
517         if(pObj->InternalFieldCount())
518                 pPrivateData = (CJS_PrivateData*)pObj->GetAlignedPointerFromInternalField(0);
519         else
520         {
521                 //It could be a global proxy object.
522                 v8::Local<v8::Value> v = pObj->GetPrototype();
523                 if(v->IsObject())
524                         pPrivateData = (CJS_PrivateData*)v->ToObject()->GetAlignedPointerFromInternalField(0);
525         }
526         if(!pPrivateData) return NULL;
527         return pPrivateData->pPrivate;
528 }
529
530 void JS_FreePrivate(void* pPrivateData)
531 {
532         delete (CJS_PrivateData*)pPrivateData;
533 }
534
535 void JS_FreePrivate(v8::Handle<v8::Object> pObj)
536 {
537         if(pObj.IsEmpty() || !pObj->InternalFieldCount()) return;
538         JS_FreePrivate(pObj->GetAlignedPointerFromInternalField(0));
539         pObj->SetAlignedPointerInInternalField(0, NULL);
540 }
541
542
543 v8::Handle<v8::Value> JS_GetObjectValue(v8::Handle<v8::Object> pObj)
544 {
545         return pObj;
546 }
547
548 v8::Handle<v8::String> WSToJSString(IJS_Runtime* pJSRuntime, const wchar_t* PropertyName, int Len = -1)
549 {
550         CFX_WideString ws = CFX_WideString(PropertyName,Len);
551         CFX_ByteString bs = ws.UTF8Encode();
552         if(!pJSRuntime) pJSRuntime = v8::Isolate::GetCurrent();
553         return v8::String::NewFromUtf8(pJSRuntime, bs.c_str());
554 }
555
556 v8::Handle<v8::Value> JS_GetObjectElement(IJS_Runtime* pJSRuntime, v8::Handle<v8::Object> pObj,const wchar_t* PropertyName)
557 {
558         if(pObj.IsEmpty()) return v8::Handle<v8::Value>();
559         return pObj->Get(WSToJSString(pJSRuntime,PropertyName));
560 }
561
562 v8::Handle<v8::Array> JS_GetObjectElementNames(v8::Handle<v8::Object> pObj)
563 {
564         if(pObj.IsEmpty()) return v8::Handle<v8::Array>();
565         return pObj->GetPropertyNames();
566 }
567
568 void JS_PutObjectString(IJS_Runtime* pJSRuntime,v8::Handle<v8::Object> pObj, const wchar_t* PropertyName, const wchar_t* sValue) //VT_string
569 {
570         if(pObj.IsEmpty()) return;
571         pObj->Set(WSToJSString(pJSRuntime, PropertyName), WSToJSString(pJSRuntime, sValue));
572 }
573
574 void JS_PutObjectNumber(IJS_Runtime* pJSRuntime,v8::Handle<v8::Object> pObj, const wchar_t* PropertyName, int nValue)
575 {
576         if(pObj.IsEmpty()) return;
577         pObj->Set(WSToJSString(pJSRuntime,PropertyName),v8::Int32::New(pJSRuntime, nValue));
578 }
579
580 void JS_PutObjectNumber(IJS_Runtime* pJSRuntime,v8::Handle<v8::Object> pObj, const wchar_t* PropertyName, float fValue)
581 {
582         if(pObj.IsEmpty()) return;
583         pObj->Set(WSToJSString(pJSRuntime,PropertyName),v8::Number::New(pJSRuntime, (double)fValue));
584 }
585
586 void JS_PutObjectNumber(IJS_Runtime* pJSRuntime,v8::Handle<v8::Object> pObj, const wchar_t* PropertyName, double dValue)
587 {
588         if(pObj.IsEmpty()) return;
589         pObj->Set(WSToJSString(pJSRuntime,PropertyName),v8::Number::New(pJSRuntime, (double)dValue));
590 }
591
592 void JS_PutObjectBoolean(IJS_Runtime* pJSRuntime,v8::Handle<v8::Object> pObj, const wchar_t* PropertyName, bool bValue)
593 {
594         if(pObj.IsEmpty()) return;
595         pObj->Set(WSToJSString(pJSRuntime,PropertyName),v8::Boolean::New(pJSRuntime, bValue));
596 }
597
598 void JS_PutObjectObject(IJS_Runtime* pJSRuntime,v8::Handle<v8::Object> pObj, const wchar_t* PropertyName, v8::Handle<v8::Object> pPut)
599 {
600         if(pObj.IsEmpty()) return;
601         pObj->Set(WSToJSString(pJSRuntime,PropertyName),pPut);
602 }
603
604 void JS_PutObjectNull(IJS_Runtime* pJSRuntime,v8::Handle<v8::Object> pObj, const wchar_t* PropertyName)
605 {
606         if(pObj.IsEmpty()) return;
607         pObj->Set(WSToJSString(pJSRuntime,PropertyName),v8::Handle<v8::Object>());
608 }
609
610 v8::Handle<v8::Array> JS_NewArray(IJS_Runtime* pJSRuntime)
611 {
612         return v8::Array::New(pJSRuntime);
613 }
614
615 unsigned JS_PutArrayElement(v8::Handle<v8::Array> pArray,unsigned index,v8::Handle<v8::Value> pValue,FXJSVALUETYPE eType)
616 {       
617         if(pArray.IsEmpty()) return 0;
618         pArray->Set(index, pValue);
619         return 1;
620 }
621
622 v8::Handle<v8::Value> JS_GetArrayElemnet(v8::Handle<v8::Array> pArray,unsigned index)
623 {
624         if(pArray.IsEmpty()) return v8::Handle<v8::Value>();
625         return pArray->Get(index);
626 }
627
628 unsigned JS_GetArrayLength(v8::Handle<v8::Array> pArray)
629 {
630         if(pArray.IsEmpty()) return 0;
631         return pArray->Length();
632 }
633
634 v8::Handle<v8::Value> JS_NewNumber(IJS_Runtime* pJSRuntime,int number)
635 {
636         return v8::Int32::New(pJSRuntime, number);
637 }
638
639 v8::Handle<v8::Value> JS_NewNumber(IJS_Runtime* pJSRuntime,double number)
640 {
641         return v8::Number::New(pJSRuntime, number);
642 }
643
644 v8::Handle<v8::Value> JS_NewNumber(IJS_Runtime* pJSRuntime,float number)
645 {
646         return v8::Number::New(pJSRuntime, (float)number);
647 }
648
649 v8::Handle<v8::Value> JS_NewBoolean(IJS_Runtime* pJSRuntime,bool b)
650 {
651         return v8::Boolean::New(pJSRuntime, b);
652 }
653
654 v8::Handle<v8::Value> JS_NewObject(IJS_Runtime* pJSRuntime,v8::Handle<v8::Object> pObj)
655 {
656         if(pObj.IsEmpty()) return v8::Handle<v8::Value>();
657         return pObj->Clone();
658 }
659
660 v8::Handle<v8::Value> JS_NewObject2(IJS_Runtime* pJSRuntime,v8::Handle<v8::Array> pObj)
661 {
662         if(pObj.IsEmpty()) return v8::Handle<v8::Value>();
663         return pObj->Clone();
664 }
665
666
667 v8::Handle<v8::Value> JS_NewString(IJS_Runtime* pJSRuntime,const wchar_t* string)
668 {
669         return WSToJSString(pJSRuntime, string);
670 }
671
672 v8::Handle<v8::Value> JS_NewString(IJS_Runtime* pJSRuntime,const wchar_t* string, unsigned nLen)
673 {
674         return WSToJSString(pJSRuntime, string, nLen);
675 }
676
677 v8::Handle<v8::Value> JS_NewNull()
678 {
679         return v8::Handle<v8::Value>();
680 }
681
682 v8::Handle<v8::Value> JS_NewDate(IJS_Runtime* pJSRuntime,double d)
683 {
684         return v8::Date::New(pJSRuntime, d);
685 }
686
687 v8::Handle<v8::Value> JS_NewValue(IJS_Runtime* pJSRuntime)
688 {
689         return v8::Handle<v8::Value>();
690 }
691
692 v8::Handle<v8::Value> JS_GetListValue(v8::Handle<v8::Value> pList, int index)
693 {
694
695         if(!pList.IsEmpty() && pList->IsObject())
696         {
697                 v8::Local<v8::Object> obj = pList->ToObject();
698                 return obj->Get(index);
699         }
700         return v8::Handle<v8::Value>();
701 }
702
703 int     JS_ToInt32(v8::Handle<v8::Value> pValue)
704 {
705         if(pValue.IsEmpty()) return 0;
706         return pValue->ToInt32()->Value();
707 }
708
709 bool JS_ToBoolean(v8::Handle<v8::Value> pValue)
710 {
711         if(pValue.IsEmpty()) return false;
712         return pValue->ToBoolean()->Value();
713 }
714
715 double JS_ToNumber(v8::Handle<v8::Value> pValue)
716 {
717         if(pValue.IsEmpty()) return 0.0;
718         return pValue->ToNumber()->Value();
719 }
720
721 v8::Handle<v8::Object> JS_ToObject(v8::Handle<v8::Value> pValue)
722 {
723         if(pValue.IsEmpty()) return v8::Handle<v8::Object>();
724         return pValue->ToObject();
725 }
726
727 CFX_WideString  JS_ToString(v8::Handle<v8::Value> pValue)
728 {
729         if(pValue.IsEmpty()) return L"";
730         v8::String::Utf8Value s(pValue->ToString());
731         return CFX_WideString::FromUTF8(*s, s.length());
732 }
733
734 v8::Handle<v8::Array> JS_ToArray(v8::Handle<v8::Value> pValue)
735 {
736         if(pValue.IsEmpty()) return v8::Handle<v8::Array>();
737         return v8::Handle<v8::Array>::Cast(pValue->ToObject());
738 }
739
740 void JS_ValueCopy(v8::Handle<v8::Value>& pTo, v8::Handle<v8::Value> pFrom)
741 {
742         pTo = pFrom;
743 }
744
745
746 //JavaScript time implement begin.
747
748 double _getLocalTZA()
749 {
750         if(!FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS))
751                 return 0;
752         time_t t = 0;
753         time(&t);
754         localtime(&t);
755 #if _MSC_VER >= 1900
756   // In gcc and in Visual Studio prior to VS 2015 'timezone' is a global
757   // variable declared in time.h. That variable was deprecated and in VS 2015
758   // is removed, with _get_timezone replacing it.
759   long timezone = 0;
760   _get_timezone(&timezone);
761 #endif
762         return (double)(-(timezone * 1000));
763 }
764
765 int _getDaylightSavingTA(double d)
766 {
767         if(!FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS))
768                 return 0;
769         time_t t = (time_t)(d/1000);
770         struct tm * tmp = localtime(&t);
771         if (tmp == NULL)
772                 return 0;
773         if (tmp->tm_isdst > 0)
774                 //One hour.
775                 return (int)60*60*1000;
776         return 0;
777 }
778
779 double _Mod(double x, double y)
780 {   
781         double r = fmod(x, y);
782         if (r < 0) r += y;
783         return r;
784 }
785
786 int _isfinite(double v)
787 {
788 #if _MSC_VER
789         return ::_finite(v);
790 #else
791         return std::fabs(v) < std::numeric_limits<double>::max();
792 #endif
793 }
794
795 double _toInteger(double n)
796 {
797         return (n >= 0)? FXSYS_floor(n): -FXSYS_floor(-n);
798 }
799
800 bool _isLeapYear(int year)
801 {
802         return (year%4==0)&&((year%100!=0)||(year%400!=0));
803 }
804
805 int _DayFromYear(int y)
806 {
807         return (int)(365*(y - 1970.0) + FXSYS_floor((y - 1969.0)/4) - FXSYS_floor((y - 1901.0)/100)+FXSYS_floor((y - 1601.0)/400));
808 }
809
810 double _TimeFromYear(int y)
811 {
812         return  ((double)86400000) * _DayFromYear(y);
813 }
814
815 double _TimeFromYearMonth(int y, int m)
816 {
817         static int daysMonth[12] ={ 0,31,59,90,120,151,181,212,243,273,304,334};
818         static int leapDaysMonth[12] = { 0,31,60,91,121,152,182,213,244,274,305,335};
819         int* pMonth = daysMonth;
820         if(_isLeapYear(y))
821                 pMonth = leapDaysMonth;
822         return _TimeFromYear(y) + ((double)pMonth[m])*86400000;
823 }
824
825 int _Day(double t)
826 {
827         return (int)FXSYS_floor(t / 86400000);
828 }
829
830 int _YearFromTime(double t)
831 {
832         //estimate the time.
833         int y = 1970 +(int)(t/(365.0*86400000));
834         if (_TimeFromYear(y) <= t)
835         {
836                 while(_TimeFromYear(y+1) <= t) y++;
837         }
838         else
839                 while(_TimeFromYear(y-1) > t) y--;
840         return y;
841 }
842
843 int _DayWithinYear(double t)
844 {
845         int year = _YearFromTime(t);
846         int day = _Day(t);
847         return day-_DayFromYear(year);
848 }
849
850 int _MonthFromTime(double t)
851 {
852         int day = _DayWithinYear(t);
853         int year = _YearFromTime(t);
854         if(0<=day && day <31)
855                 return 0;
856         if(31<=day && day< 59+_isLeapYear(year))
857                 return 1;
858         if((59+_isLeapYear(year))<=day && day<(90+_isLeapYear(year)))
859                 return 2;
860         if((90+_isLeapYear(year))<=day && day<(120+_isLeapYear(year)))
861                 return 3;
862         if((120+_isLeapYear(year))<=day && day<(151+_isLeapYear(year)))
863                 return 4;
864         if((151+_isLeapYear(year))<=day && day<(181+_isLeapYear(year)))
865                 return 5;
866         if((181+_isLeapYear(year))<=day && day<(212+_isLeapYear(year)))
867                 return 6;
868         if((212+_isLeapYear(year))<=day && day<(243+_isLeapYear(year)))
869                 return 7;
870         if((243+_isLeapYear(year))<=day && day<(273+_isLeapYear(year)))
871                 return 8;
872         if((273+_isLeapYear(year))<=day && day<(304+_isLeapYear(year)))
873                 return 9;
874         if((304+_isLeapYear(year))<=day && day<(334+_isLeapYear(year)))
875                 return 10;
876         if((334+_isLeapYear(year))<=day && day<(365+_isLeapYear(year)))
877                 return 11;
878
879         return -1;
880 }
881
882 int _DateFromTime(double t)
883 {
884         int day = _DayWithinYear(t);
885         int year = _YearFromTime(t);
886         bool leap = _isLeapYear(year);
887         int month = _MonthFromTime(t);
888         switch (month)
889         {
890         case 0:  
891                 return day+1;
892         case 1:  
893                 return day-30;
894         case 2:  
895                 return day-58-leap;
896         case 3:  
897                 return day-89-leap;
898         case 4:  
899                 return day-119-leap;
900         case 5:  
901                 return day-150-leap;
902         case 6:  
903                 return day-180-leap;
904         case 7:  
905                 return day-211-leap;
906         case 8:  
907                 return day-242-leap;
908         case 9:  
909                 return day-272-leap;
910         case 10: 
911                 return day-303-leap;
912         case 11: 
913                 return day-333-leap;
914         default:
915                 return 0;
916         }
917 }
918
919 double JS_GetDateTime()
920 {
921         if(!FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS))
922                 return 0;
923         time_t t = time(NULL);
924         struct tm* pTm = localtime(&t);
925
926         int year = pTm->tm_year+1900;
927         double t1 = _TimeFromYear(year);
928
929         return t1 + pTm->tm_yday*86400000.0 + pTm->tm_hour*3600000.0+pTm->tm_min*60000.0+pTm->tm_sec*1000.0;
930 }
931
932 int JS_GetYearFromTime(double dt)
933 {
934         return _YearFromTime(dt);
935 }
936
937 int JS_GetMonthFromTime(double dt)
938 {
939         return _MonthFromTime(dt);
940 }
941
942 int JS_GetDayFromTime(double dt)
943 {
944         return _DateFromTime(dt);
945 }
946
947 int JS_GetHourFromTime(double dt)
948 {
949         return (int)_Mod(FXSYS_floor((double)(dt/(60*60*1000))), 24);
950 }
951
952 int JS_GetMinFromTime(double dt)
953 {
954         return (int)_Mod(FXSYS_floor((double)(dt/(60*1000))), 60);
955 }
956
957 int JS_GetSecFromTime(double dt)
958 {
959         return (int)_Mod(FXSYS_floor((double)(dt/1000)), 60);
960 }
961
962 double JS_DateParse(const wchar_t* string)
963 {
964         v8::Isolate* pIsolate = v8::Isolate::GetCurrent();
965         v8::Isolate::Scope isolate_scope(pIsolate);
966         v8::HandleScope scope(pIsolate);
967
968         v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
969         
970         //Use the built-in object method.
971         v8::Local<v8::Value> v = context->Global()->Get(v8::String::NewFromUtf8(pIsolate, "Date"));
972         if(v->IsObject())
973         {
974                 v8::Local<v8::Object> o = v->ToObject();
975                 v = o->Get(v8::String::NewFromUtf8(pIsolate, "parse"));
976                 if(v->IsFunction())
977                 {
978                         v8::Local<v8::Function> funC = v8::Handle<v8::Function>::Cast(v);
979
980                         const int argc = 1;
981                         v8::Local<v8::String> timeStr = WSToJSString(pIsolate, string);
982                         v8::Handle<v8::Value> argv[argc] = {timeStr};
983                         v = funC->Call(context->Global(), argc, argv);
984                         if(v->IsNumber())
985                         {
986                                 double date =  v->ToNumber()->Value();
987                                 if(!_isfinite(date)) return date;
988                                 return date + _getLocalTZA() + _getDaylightSavingTA(date);
989                         }
990
991                 }
992         }
993         return 0;
994 }
995
996 double JS_MakeDay(int nYear, int nMonth, int nDate)
997 {
998         if (!_isfinite(nYear) || !_isfinite(nMonth) ||!_isfinite(nDate))
999                 return GetNan();
1000         double y = _toInteger(nYear);
1001         double m = _toInteger(nMonth);
1002         double dt = _toInteger(nDate);
1003         double ym = y + FXSYS_floor((double)m/12);
1004         double mn = _Mod(m ,12);
1005
1006         double t = _TimeFromYearMonth((int)ym,(int)mn);
1007
1008         if (_YearFromTime(t) != ym || _MonthFromTime(t) != mn ||_DateFromTime(t) != 1)
1009                 return GetNan();
1010         return _Day(t)+dt-1;
1011 }
1012
1013 double JS_MakeTime(int nHour, int nMin, int nSec, int nMs)
1014 {
1015         if (!_isfinite(nHour) ||!_isfinite(nMin) ||!_isfinite(nSec) ||!_isfinite(nMs))
1016                 return GetNan();
1017
1018         double h = _toInteger(nHour);
1019         double m = _toInteger(nMin);
1020         double s = _toInteger(nSec);
1021         double milli = _toInteger(nMs);
1022
1023         return h * 3600000 + m * 60000 + s * 1000 + milli;
1024 }
1025
1026 double JS_MakeDate(double day, double time)
1027 {
1028         if (!_isfinite(day) ||!_isfinite(time))
1029                 return GetNan();
1030
1031         return day * 86400000 + time;
1032 }
1033
1034 bool JS_PortIsNan(double d)
1035 {
1036         return d != d;
1037 }
1038
1039 double JS_LocalTime(double d)
1040 {
1041         return JS_GetDateTime() + _getDaylightSavingTA(d);
1042 }
1043
1044 //JavaScript time implement End.