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