Set pointers in internal fields directly instead of wrapping them
[pdfium.git] / fpdfsdk / src / jsapi / fxjs_v8.cpp
1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4  
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7 #include "../../../core/include/fxcrt/fx_basic.h"
8 #include "../../../core/include/fxcrt/fx_ext.h" 
9 #include "../../include/jsapi/fxjs_v8.h"
10 #include "../../include/fsdk_define.h"
11 #include "time.h"
12 #include <cmath>
13 #include <limits>
14
15 #define VALUE_NAME_STRING               L"string"
16 #define VALUE_NAME_NUMBER               L"number"
17 #define VALUE_NAME_BOOLEAN              L"boolean"
18 #define VALUE_NAME_DATE                 L"date"
19 #define VALUE_NAME_OBJECT               L"object"
20 #define VALUE_NAME_FXOBJ                L"fxobj"
21 #define VALUE_NAME_NULL                 L"null"
22 #define VALUE_NAME_UNDEFINED    L"undefined"
23
24 const static FX_DWORD g_nan[2] = {0,0x7FF80000 };
25 static double GetNan()
26 {
27   return *(double*)g_nan;
28 }
29
30
31 class CJS_PrivateData
32 {
33 public:
34         CJS_PrivateData():ObjDefID(-1), pPrivate(NULL) {}
35         int ObjDefID;
36         FX_LPVOID       pPrivate;
37 };
38
39
40 class CJS_ObjDefintion
41 {
42 public:
43         CJS_ObjDefintion(v8::Isolate* isolate, const wchar_t* sObjName, FXJSOBJTYPE eObjType, LP_CONSTRUCTOR pConstructor, LP_DESTRUCTOR pDestructor, unsigned bApplyNew):
44           objName(sObjName), objType(eObjType), m_pConstructor(pConstructor), m_pDestructor(pDestructor),m_bApplyNew(bApplyNew),m_bSetAsGlobalObject(FALSE)
45           {
46                   v8::Isolate::Scope isolate_scope(isolate);
47                   v8::HandleScope handle_scope(isolate);
48
49                   v8::Handle<v8::ObjectTemplate> objTemplate = v8::ObjectTemplate::New(isolate);
50                   objTemplate->SetInternalFieldCount(1);
51                   m_objTemplate.Reset(isolate, objTemplate);
52
53                  //Document as the global object.
54                   if(FXSYS_wcscmp(sObjName, L"Document") == 0)
55                   {
56                          m_bSetAsGlobalObject = TRUE;
57                   }
58
59           }
60           ~CJS_ObjDefintion()
61           {
62                   m_objTemplate.Reset();
63                   m_StaticObj.Reset();
64           }
65 public:
66         const wchar_t* objName;
67         FXJSOBJTYPE objType;
68         LP_CONSTRUCTOR m_pConstructor;
69         LP_DESTRUCTOR m_pDestructor;
70         unsigned m_bApplyNew;
71         FX_BOOL m_bSetAsGlobalObject;
72
73         v8::Persistent<v8::ObjectTemplate> m_objTemplate;
74         v8::Persistent<v8::Object> m_StaticObj;
75 };
76
77 int JS_DefineObj(IJS_Runtime* pJSRuntime, const wchar_t* sObjName, FXJSOBJTYPE eObjType, LP_CONSTRUCTOR pConstructor, LP_DESTRUCTOR pDestructor, unsigned bApplyNew)
78 {
79         v8::Isolate* isolate = (v8::Isolate*)pJSRuntime;
80         v8::Isolate::Scope isolate_scope(isolate);
81         v8::HandleScope handle_scope(isolate);
82         CFX_PtrArray* pArray = (CFX_PtrArray*)isolate->GetData(0);
83         if(!pArray)
84         {
85                 pArray = new CFX_PtrArray();
86                 isolate->SetData(0, pArray);
87         }
88         CJS_ObjDefintion* pObjDef = new CJS_ObjDefintion(isolate, sObjName, eObjType, pConstructor, pDestructor, bApplyNew);
89         pArray->Add(pObjDef);
90         return pArray->GetSize()-1;
91 }
92
93 int JS_DefineObjMethod(IJS_Runtime* pJSRuntime, int nObjDefnID, const wchar_t* sMethodName, v8::FunctionCallback pMethodCall)
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(v8::Handle<v8::Object> pObj)
531 {
532         if(pObj.IsEmpty() || !pObj->InternalFieldCount()) return;
533         delete (CJS_PrivateData*)pObj->GetAlignedPointerFromInternalField(0);
534         pObj->SetAlignedPointerInInternalField(0, NULL);
535 }
536
537
538 v8::Handle<v8::Value> JS_GetObjectValue(v8::Handle<v8::Object> pObj)
539 {
540         return pObj;
541 }
542
543 v8::Handle<v8::String> WSToJSString(IJS_Runtime* pJSRuntime, const wchar_t* PropertyName, int Len = -1)
544 {
545         CFX_WideString ws = CFX_WideString(PropertyName,Len);
546         CFX_ByteString bs = ws.UTF8Encode();
547         if(!pJSRuntime) pJSRuntime = v8::Isolate::GetCurrent();
548         return v8::String::NewFromUtf8(pJSRuntime, bs.c_str());
549 }
550
551 v8::Handle<v8::Value> JS_GetObjectElement(IJS_Runtime* pJSRuntime, v8::Handle<v8::Object> pObj,const wchar_t* PropertyName)
552 {
553         if(pObj.IsEmpty()) return v8::Handle<v8::Value>();
554         return pObj->Get(WSToJSString(pJSRuntime,PropertyName));
555 }
556
557 v8::Handle<v8::Array> JS_GetObjectElementNames(v8::Handle<v8::Object> pObj)
558 {
559         if(pObj.IsEmpty()) return v8::Handle<v8::Array>();
560         return pObj->GetPropertyNames();
561 }
562
563 void JS_PutObjectString(IJS_Runtime* pJSRuntime,v8::Handle<v8::Object> pObj, const wchar_t* PropertyName, const wchar_t* sValue) //VT_string
564 {
565         if(pObj.IsEmpty()) return;
566         pObj->Set(WSToJSString(pJSRuntime, PropertyName), WSToJSString(pJSRuntime, sValue));
567 }
568
569 void JS_PutObjectNumber(IJS_Runtime* pJSRuntime,v8::Handle<v8::Object> pObj, const wchar_t* PropertyName, int nValue)
570 {
571         if(pObj.IsEmpty()) return;
572         pObj->Set(WSToJSString(pJSRuntime,PropertyName),v8::Int32::New(pJSRuntime, nValue));
573 }
574
575 void JS_PutObjectNumber(IJS_Runtime* pJSRuntime,v8::Handle<v8::Object> pObj, const wchar_t* PropertyName, float fValue)
576 {
577         if(pObj.IsEmpty()) return;
578         pObj->Set(WSToJSString(pJSRuntime,PropertyName),v8::Number::New(pJSRuntime, (double)fValue));
579 }
580
581 void JS_PutObjectNumber(IJS_Runtime* pJSRuntime,v8::Handle<v8::Object> pObj, const wchar_t* PropertyName, double dValue)
582 {
583         if(pObj.IsEmpty()) return;
584         pObj->Set(WSToJSString(pJSRuntime,PropertyName),v8::Number::New(pJSRuntime, (double)dValue));
585 }
586
587 void JS_PutObjectBoolean(IJS_Runtime* pJSRuntime,v8::Handle<v8::Object> pObj, const wchar_t* PropertyName, bool bValue)
588 {
589         if(pObj.IsEmpty()) return;
590         pObj->Set(WSToJSString(pJSRuntime,PropertyName),v8::Boolean::New(pJSRuntime, bValue));
591 }
592
593 void JS_PutObjectObject(IJS_Runtime* pJSRuntime,v8::Handle<v8::Object> pObj, const wchar_t* PropertyName, v8::Handle<v8::Object> pPut)
594 {
595         if(pObj.IsEmpty()) return;
596         pObj->Set(WSToJSString(pJSRuntime,PropertyName),pPut);
597 }
598
599 void JS_PutObjectNull(IJS_Runtime* pJSRuntime,v8::Handle<v8::Object> pObj, const wchar_t* PropertyName)
600 {
601         if(pObj.IsEmpty()) return;
602         pObj->Set(WSToJSString(pJSRuntime,PropertyName),v8::Handle<v8::Object>());
603 }
604
605 v8::Handle<v8::Array> JS_NewArray(IJS_Runtime* pJSRuntime)
606 {
607         return v8::Array::New(pJSRuntime);
608 }
609
610 unsigned JS_PutArrayElement(v8::Handle<v8::Array> pArray,unsigned index,v8::Handle<v8::Value> pValue,FXJSVALUETYPE eType)
611 {       
612         if(pArray.IsEmpty()) return 0;
613         pArray->Set(index, pValue);
614         return 1;
615 }
616
617 v8::Handle<v8::Value> JS_GetArrayElemnet(v8::Handle<v8::Array> pArray,unsigned index)
618 {
619         if(pArray.IsEmpty()) return v8::Handle<v8::Value>();
620         return pArray->Get(index);
621 }
622
623 unsigned JS_GetArrayLength(v8::Handle<v8::Array> pArray)
624 {
625         if(pArray.IsEmpty()) return 0;
626         return pArray->Length();
627 }
628
629 v8::Handle<v8::Value> JS_NewNumber(IJS_Runtime* pJSRuntime,int number)
630 {
631         return v8::Int32::New(pJSRuntime, number);
632 }
633
634 v8::Handle<v8::Value> JS_NewNumber(IJS_Runtime* pJSRuntime,double number)
635 {
636         return v8::Number::New(pJSRuntime, number);
637 }
638
639 v8::Handle<v8::Value> JS_NewNumber(IJS_Runtime* pJSRuntime,float number)
640 {
641         return v8::Number::New(pJSRuntime, (float)number);
642 }
643
644 v8::Handle<v8::Value> JS_NewBoolean(IJS_Runtime* pJSRuntime,bool b)
645 {
646         return v8::Boolean::New(pJSRuntime, b);
647 }
648
649 v8::Handle<v8::Value> JS_NewObject(IJS_Runtime* pJSRuntime,v8::Handle<v8::Object> pObj)
650 {
651         if(pObj.IsEmpty()) return v8::Handle<v8::Value>();
652         return pObj->Clone();
653 }
654
655 v8::Handle<v8::Value> JS_NewObject2(IJS_Runtime* pJSRuntime,v8::Handle<v8::Array> pObj)
656 {
657         if(pObj.IsEmpty()) return v8::Handle<v8::Value>();
658         return pObj->Clone();
659 }
660
661
662 v8::Handle<v8::Value> JS_NewString(IJS_Runtime* pJSRuntime,const wchar_t* string)
663 {
664         return WSToJSString(pJSRuntime, string);
665 }
666
667 v8::Handle<v8::Value> JS_NewString(IJS_Runtime* pJSRuntime,const wchar_t* string, unsigned nLen)
668 {
669         return WSToJSString(pJSRuntime, string, nLen);
670 }
671
672 v8::Handle<v8::Value> JS_NewNull()
673 {
674         return v8::Handle<v8::Value>();
675 }
676
677 v8::Handle<v8::Value> JS_NewDate(IJS_Runtime* pJSRuntime,double d)
678 {
679         return v8::Date::New(pJSRuntime, d);
680 }
681
682 v8::Handle<v8::Value> JS_NewValue(IJS_Runtime* pJSRuntime)
683 {
684         return v8::Handle<v8::Value>();
685 }
686
687 v8::Handle<v8::Value> JS_GetListValue(v8::Handle<v8::Value> pList, int index)
688 {
689
690         if(!pList.IsEmpty() && pList->IsObject())
691         {
692                 v8::Local<v8::Object> obj = pList->ToObject();
693                 return obj->Get(index);
694         }
695         return v8::Handle<v8::Value>();
696 }
697
698 int     JS_ToInt32(v8::Handle<v8::Value> pValue)
699 {
700         if(pValue.IsEmpty()) return 0;
701         return pValue->ToInt32()->Value();
702 }
703
704 bool JS_ToBoolean(v8::Handle<v8::Value> pValue)
705 {
706         if(pValue.IsEmpty()) return false;
707         return pValue->ToBoolean()->Value();
708 }
709
710 double JS_ToNumber(v8::Handle<v8::Value> pValue)
711 {
712         if(pValue.IsEmpty()) return 0.0;
713         return pValue->ToNumber()->Value();
714 }
715
716 v8::Handle<v8::Object> JS_ToObject(v8::Handle<v8::Value> pValue)
717 {
718         if(pValue.IsEmpty()) return v8::Handle<v8::Object>();
719         return pValue->ToObject();
720 }
721
722 CFX_WideString  JS_ToString(v8::Handle<v8::Value> pValue)
723 {
724         if(pValue.IsEmpty()) return L"";
725         v8::String::Utf8Value s(pValue->ToString());
726         return CFX_WideString::FromUTF8(*s, s.length());
727 }
728
729 v8::Handle<v8::Array> JS_ToArray(v8::Handle<v8::Value> pValue)
730 {
731         if(pValue.IsEmpty()) return v8::Handle<v8::Array>();
732         return v8::Handle<v8::Array>::Cast(pValue->ToObject());
733 }
734
735 void JS_ValueCopy(v8::Handle<v8::Value>& pTo, v8::Handle<v8::Value> pFrom)
736 {
737         pTo = pFrom;
738 }
739
740
741 //JavaScript time implement begin.
742
743 double _getLocalTZA()
744 {
745         if(!FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS))
746                 return 0;
747         time_t t = 0;
748         time(&t);
749         localtime(&t);
750 #if _MSC_VER >= 1900
751   // In gcc and in Visual Studio prior to VS 2015 'timezone' is a global
752   // variable declared in time.h. That variable was deprecated and in VS 2015
753   // is removed, with _get_timezone replacing it.
754   long timezone = 0;
755   _get_timezone(&timezone);
756 #endif
757         return (double)(-(timezone * 1000));
758 }
759
760 int _getDaylightSavingTA(double d)
761 {
762         if(!FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS))
763                 return 0;
764         time_t t = (time_t)(d/1000);
765         struct tm * tmp = localtime(&t);
766         if (tmp == NULL)
767                 return 0;
768         if (tmp->tm_isdst > 0)
769                 //One hour.
770                 return (int)60*60*1000;
771         return 0;
772 }
773
774 double _Mod(double x, double y)
775 {   
776         double r = fmod(x, y);
777         if (r < 0) r += y;
778         return r;
779 }
780
781 int _isfinite(double v)
782 {
783 #if _MSC_VER
784         return ::_finite(v);
785 #else
786         return std::fabs(v) < std::numeric_limits<double>::max();
787 #endif
788 }
789
790 double _toInteger(double n)
791 {
792         return (n >= 0)? FXSYS_floor(n): -FXSYS_floor(-n);
793 }
794
795 bool _isLeapYear(int year)
796 {
797         return (year%4==0)&&((year%100!=0)||(year%400!=0));
798 }
799
800 int _DayFromYear(int y)
801 {
802         return (int)(365*(y - 1970.0) + FXSYS_floor((y - 1969.0)/4) - FXSYS_floor((y - 1901.0)/100)+FXSYS_floor((y - 1601.0)/400));
803 }
804
805 double _TimeFromYear(int y)
806 {
807         return  ((double)86400000) * _DayFromYear(y);
808 }
809
810 double _TimeFromYearMonth(int y, int m)
811 {
812         static int daysMonth[12] ={ 0,31,59,90,120,151,181,212,243,273,304,334};
813         static int leapDaysMonth[12] = { 0,31,60,91,121,152,182,213,244,274,305,335};
814         int* pMonth = daysMonth;
815         if(_isLeapYear(y))
816                 pMonth = leapDaysMonth;
817         return _TimeFromYear(y) + ((double)pMonth[m])*86400000;
818 }
819
820 int _Day(double t)
821 {
822         return (int)FXSYS_floor(t / 86400000);
823 }
824
825 int _YearFromTime(double t)
826 {
827         //estimate the time.
828         int y = 1970 +(int)(t/(365.0*86400000));
829         if (_TimeFromYear(y) <= t)
830         {
831                 while(_TimeFromYear(y+1) <= t) y++;
832         }
833         else
834                 while(_TimeFromYear(y-1) > t) y--;
835         return y;
836 }
837
838 int _DayWithinYear(double t)
839 {
840         int year = _YearFromTime(t);
841         int day = _Day(t);
842         return day-_DayFromYear(year);
843 }
844
845 int _MonthFromTime(double t)
846 {
847         int day = _DayWithinYear(t);
848         int year = _YearFromTime(t);
849         if(0<=day && day <31)
850                 return 0;
851         if(31<=day && day< 59+_isLeapYear(year))
852                 return 1;
853         if((59+_isLeapYear(year))<=day && day<(90+_isLeapYear(year)))
854                 return 2;
855         if((90+_isLeapYear(year))<=day && day<(120+_isLeapYear(year)))
856                 return 3;
857         if((120+_isLeapYear(year))<=day && day<(151+_isLeapYear(year)))
858                 return 4;
859         if((151+_isLeapYear(year))<=day && day<(181+_isLeapYear(year)))
860                 return 5;
861         if((181+_isLeapYear(year))<=day && day<(212+_isLeapYear(year)))
862                 return 6;
863         if((212+_isLeapYear(year))<=day && day<(243+_isLeapYear(year)))
864                 return 7;
865         if((243+_isLeapYear(year))<=day && day<(273+_isLeapYear(year)))
866                 return 8;
867         if((273+_isLeapYear(year))<=day && day<(304+_isLeapYear(year)))
868                 return 9;
869         if((304+_isLeapYear(year))<=day && day<(334+_isLeapYear(year)))
870                 return 10;
871         if((334+_isLeapYear(year))<=day && day<(365+_isLeapYear(year)))
872                 return 11;
873
874         return -1;
875 }
876
877 int _DateFromTime(double t)
878 {
879         int day = _DayWithinYear(t);
880         int year = _YearFromTime(t);
881         bool leap = _isLeapYear(year);
882         int month = _MonthFromTime(t);
883         switch (month)
884         {
885         case 0:  
886                 return day+1;
887         case 1:  
888                 return day-30;
889         case 2:  
890                 return day-58-leap;
891         case 3:  
892                 return day-89-leap;
893         case 4:  
894                 return day-119-leap;
895         case 5:  
896                 return day-150-leap;
897         case 6:  
898                 return day-180-leap;
899         case 7:  
900                 return day-211-leap;
901         case 8:  
902                 return day-242-leap;
903         case 9:  
904                 return day-272-leap;
905         case 10: 
906                 return day-303-leap;
907         case 11: 
908                 return day-333-leap;
909         default:
910                 return 0;
911         }
912 }
913
914 double JS_GetDateTime()
915 {
916         if(!FSDK_IsSandBoxPolicyEnabled(FPDF_POLICY_MACHINETIME_ACCESS))
917                 return 0;
918         time_t t = time(NULL);
919         struct tm* pTm = localtime(&t);
920
921         int year = pTm->tm_year+1900;
922         double t1 = _TimeFromYear(year);
923
924         return t1 + pTm->tm_yday*86400000.0 + pTm->tm_hour*3600000.0+pTm->tm_min*60000.0+pTm->tm_sec*1000.0;
925 }
926
927 int JS_GetYearFromTime(double dt)
928 {
929         return _YearFromTime(dt);
930 }
931
932 int JS_GetMonthFromTime(double dt)
933 {
934         return _MonthFromTime(dt);
935 }
936
937 int JS_GetDayFromTime(double dt)
938 {
939         return _DateFromTime(dt);
940 }
941
942 int JS_GetHourFromTime(double dt)
943 {
944         return (int)_Mod(FXSYS_floor((double)(dt/(60*60*1000))), 24);
945 }
946
947 int JS_GetMinFromTime(double dt)
948 {
949         return (int)_Mod(FXSYS_floor((double)(dt/(60*1000))), 60);
950 }
951
952 int JS_GetSecFromTime(double dt)
953 {
954         return (int)_Mod(FXSYS_floor((double)(dt/1000)), 60);
955 }
956
957 double JS_DateParse(const wchar_t* string)
958 {
959         v8::Isolate* pIsolate = v8::Isolate::GetCurrent();
960         v8::Isolate::Scope isolate_scope(pIsolate);
961         v8::HandleScope scope(pIsolate);
962
963         v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
964         
965         //Use the built-in object method.
966         v8::Local<v8::Value> v = context->Global()->Get(v8::String::NewFromUtf8(pIsolate, "Date"));
967         if(v->IsObject())
968         {
969                 v8::Local<v8::Object> o = v->ToObject();
970                 v = o->Get(v8::String::NewFromUtf8(pIsolate, "parse"));
971                 if(v->IsFunction())
972                 {
973                         v8::Local<v8::Function> funC = v8::Handle<v8::Function>::Cast(v);
974
975                         const int argc = 1;
976                         v8::Local<v8::String> timeStr = WSToJSString(pIsolate, string);
977                         v8::Handle<v8::Value> argv[argc] = {timeStr};
978                         v = funC->Call(context->Global(), argc, argv);
979                         if(v->IsNumber())
980                         {
981                                 double date =  v->ToNumber()->Value();
982                                 if(!_isfinite(date)) return date;
983                                 return date + _getLocalTZA() + _getDaylightSavingTA(date);
984                         }
985
986                 }
987         }
988         return 0;
989 }
990
991 double JS_MakeDay(int nYear, int nMonth, int nDate)
992 {
993         if (!_isfinite(nYear) || !_isfinite(nMonth) ||!_isfinite(nDate))
994                 return GetNan();
995         double y = _toInteger(nYear);
996         double m = _toInteger(nMonth);
997         double dt = _toInteger(nDate);
998         double ym = y + FXSYS_floor((double)m/12);
999         double mn = _Mod(m ,12);
1000
1001         double t = _TimeFromYearMonth((int)ym,(int)mn);
1002
1003         if (_YearFromTime(t) != ym || _MonthFromTime(t) != mn ||_DateFromTime(t) != 1)
1004                 return GetNan();
1005         return _Day(t)+dt-1;
1006 }
1007
1008 double JS_MakeTime(int nHour, int nMin, int nSec, int nMs)
1009 {
1010         if (!_isfinite(nHour) ||!_isfinite(nMin) ||!_isfinite(nSec) ||!_isfinite(nMs))
1011                 return GetNan();
1012
1013         double h = _toInteger(nHour);
1014         double m = _toInteger(nMin);
1015         double s = _toInteger(nSec);
1016         double milli = _toInteger(nMs);
1017
1018         return h * 3600000 + m * 60000 + s * 1000 + milli;
1019 }
1020
1021 double JS_MakeDate(double day, double time)
1022 {
1023         if (!_isfinite(day) ||!_isfinite(time))
1024                 return GetNan();
1025
1026         return day * 86400000 + time;
1027 }
1028
1029 bool JS_PortIsNan(double d)
1030 {
1031         return d != d;
1032 }
1033
1034 double JS_LocalTime(double d)
1035 {
1036         return JS_GetDateTime() + _getDaylightSavingTA(d);
1037 }
1038
1039 //JavaScript time implement End.