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 "../../include/javascript/JavaScript.h"
8 #include "../../include/javascript/JS_Define.h"
9 #include "../../include/javascript/JS_Object.h"
10 #include "../../include/javascript/JS_Value.h"
11 #include "../../include/javascript/Document.h"
12
13 /* ---------------------------- CJS_Value ---------------------------- */
14
CJS_Value(v8::Isolate * isolate)15 CJS_Value::CJS_Value(v8::Isolate* isolate) : m_eType(VT_unknown),m_isolate(isolate)
16 {
17 }
CJS_Value(v8::Isolate * isolate,v8::Local<v8::Value> pValue,FXJSVALUETYPE t)18 CJS_Value::CJS_Value(v8::Isolate* isolate, v8::Local<v8::Value> pValue,FXJSVALUETYPE t) :
19 m_pValue(pValue), m_eType(t), m_isolate(isolate)
20 {
21 }
22
CJS_Value(v8::Isolate * isolate,const int & iValue)23 CJS_Value::CJS_Value(v8::Isolate* isolate, const int &iValue):m_isolate(isolate)
24 {
25 operator =(iValue);
26 }
27
CJS_Value(v8::Isolate * isolate,const bool & bValue)28 CJS_Value::CJS_Value(v8::Isolate* isolate, const bool &bValue):m_isolate(isolate)
29 {
30 operator =(bValue);
31 }
32
CJS_Value(v8::Isolate * isolate,const float & fValue)33 CJS_Value::CJS_Value(v8::Isolate* isolate, const float &fValue):m_isolate(isolate)
34 {
35 operator =(fValue);
36 }
37
CJS_Value(v8::Isolate * isolate,const double & dValue)38 CJS_Value::CJS_Value(v8::Isolate* isolate, const double &dValue):m_isolate(isolate)
39 {
40 operator =(dValue);
41 }
42
CJS_Value(v8::Isolate * isolate,JSFXObject pJsObj)43 CJS_Value::CJS_Value(v8::Isolate* isolate, JSFXObject pJsObj):m_isolate(isolate)
44 {
45 operator =(pJsObj);
46 }
47
CJS_Value(v8::Isolate * isolate,CJS_Object * pJsObj)48 CJS_Value::CJS_Value(v8::Isolate* isolate, CJS_Object* pJsObj):m_isolate(isolate)
49 {
50 operator =(pJsObj);
51 }
52
CJS_Value(v8::Isolate * isolate,CJS_Document * pJsDoc)53 CJS_Value::CJS_Value(v8::Isolate* isolate, CJS_Document* pJsDoc):m_isolate(isolate)
54 {
55 m_eType = VT_object;
56 if (pJsDoc)
57 m_pValue = (JSFXObject)*pJsDoc;
58 }
59
CJS_Value(v8::Isolate * isolate,FX_LPCWSTR pWstr)60 CJS_Value::CJS_Value(v8::Isolate* isolate, FX_LPCWSTR pWstr):m_isolate(isolate)
61 {
62 operator =(pWstr);
63 }
64
CJS_Value(v8::Isolate * isolate,FX_LPCSTR pStr)65 CJS_Value::CJS_Value(v8::Isolate* isolate, FX_LPCSTR pStr):m_isolate(isolate)
66 {
67 operator = (pStr);
68 }
69
CJS_Value(v8::Isolate * isolate,CJS_Array & array)70 CJS_Value::CJS_Value(v8::Isolate* isolate, CJS_Array& array):m_isolate(isolate)
71 {
72 operator = (array);
73 }
74
~CJS_Value()75 CJS_Value::~CJS_Value()
76 {
77 }
78
Attach(v8::Local<v8::Value> pValue,FXJSVALUETYPE t)79 void CJS_Value::Attach(v8::Local<v8::Value> pValue,FXJSVALUETYPE t)
80 {
81 m_pValue = pValue;
82 m_eType = t;
83 }
84
Attach(CJS_Value * pValue)85 void CJS_Value::Attach(CJS_Value *pValue)
86 {
87 if (pValue)
88 Attach(pValue->ToV8Value(), pValue->GetType());
89 }
90
Detach()91 void CJS_Value::Detach()
92 {
93 m_pValue = v8::Local<v8::Value>();
94 m_eType = VT_unknown;
95 }
96
97 /* ---------------------------------------------------------------------------------------- */
98
ToInt() const99 int CJS_Value::ToInt() const
100 {
101 return JS_ToInt32(m_isolate, m_pValue);
102 }
103
ToBool() const104 bool CJS_Value::ToBool() const
105 {
106 return JS_ToBoolean(m_isolate, m_pValue);
107 }
108
ToDouble() const109 double CJS_Value::ToDouble() const
110 {
111 return JS_ToNumber(m_isolate, m_pValue);
112 }
113
ToFloat() const114 float CJS_Value::ToFloat() const
115 {
116 return (float)ToDouble();
117 }
118
ToCJSObject() const119 CJS_Object* CJS_Value::ToCJSObject() const
120 {
121 v8::Local<v8::Object> pObj = JS_ToObject(m_isolate, m_pValue);
122 return (CJS_Object*)JS_GetPrivate(m_isolate, pObj);
123 }
124
ToV8Object() const125 v8::Local<v8::Object> CJS_Value::ToV8Object() const
126 {
127 return JS_ToObject(m_isolate, m_pValue);
128 }
129
ToCFXWideString() const130 CFX_WideString CJS_Value::ToCFXWideString() const
131 {
132 return JS_ToString(m_isolate, m_pValue);
133 }
134
ToCFXByteString() const135 CFX_ByteString CJS_Value::ToCFXByteString() const
136 {
137 return CFX_ByteString::FromUnicode(ToCFXWideString());
138 }
139
ToV8Value() const140 v8::Local<v8::Value> CJS_Value::ToV8Value() const
141 {
142 return m_pValue;
143 }
144
ToV8Array() const145 v8::Local<v8::Array>CJS_Value::ToV8Array() const
146 {
147 if (IsArrayObject())
148 return v8::Local<v8::Array>::Cast(JS_ToObject(m_isolate, m_pValue));
149 return v8::Local<v8::Array>();
150 }
151
152 /* ---------------------------------------------------------------------------------------- */
153
operator =(int iValue)154 void CJS_Value::operator =(int iValue)
155 {
156 m_pValue = JS_NewNumber(m_isolate, iValue);
157
158 m_eType = VT_number;
159 }
160
operator =(bool bValue)161 void CJS_Value::operator =(bool bValue)
162 {
163 m_pValue = JS_NewBoolean(m_isolate, bValue);
164
165 m_eType = VT_boolean;
166 }
167
operator =(double dValue)168 void CJS_Value::operator =(double dValue)
169 {
170 m_pValue = JS_NewNumber(m_isolate,dValue);
171
172 m_eType = VT_number;
173 }
174
operator =(float fValue)175 void CJS_Value::operator = (float fValue)
176 {
177 m_pValue = JS_NewNumber(m_isolate,fValue);
178 m_eType = VT_number;
179 }
180
operator =(v8::Local<v8::Object> pObj)181 void CJS_Value::operator =(v8::Local<v8::Object> pObj)
182 {
183
184 m_pValue = JS_NewObject(m_isolate,pObj);
185
186 m_eType = VT_fxobject;
187 }
188
operator =(CJS_Object * pObj)189 void CJS_Value::operator =(CJS_Object * pObj)
190 {
191 if (pObj)
192 operator = ((JSFXObject)*pObj);
193 }
194
operator =(CJS_Document * pJsDoc)195 void CJS_Value::operator = (CJS_Document* pJsDoc)
196 {
197 m_eType = VT_object;
198 if (pJsDoc) {
199 m_pValue = static_cast<JSFXObject>(*pJsDoc);
200 }
201 }
202
operator =(FX_LPCWSTR pWstr)203 void CJS_Value::operator =(FX_LPCWSTR pWstr)
204 {
205 m_pValue = JS_NewString(m_isolate,(wchar_t *)pWstr);
206
207 m_eType = VT_string;
208 }
209
SetNull()210 void CJS_Value::SetNull()
211 {
212 m_pValue = JS_NewNull();
213
214 m_eType = VT_null;
215 }
216
operator =(FX_LPCSTR pStr)217 void CJS_Value::operator = (FX_LPCSTR pStr)
218 {
219 operator = (CFX_WideString::FromLocal(pStr).c_str());
220 }
221
operator =(CJS_Array & array)222 void CJS_Value::operator = (CJS_Array & array)
223 {
224 m_pValue = JS_NewObject2(m_isolate,(v8::Local<v8::Array>)array);
225
226 m_eType = VT_object;
227 }
228
operator =(CJS_Date & date)229 void CJS_Value::operator = (CJS_Date & date)
230 {
231 m_pValue = JS_NewDate(m_isolate, (double)date);
232
233 m_eType = VT_date;
234 }
235
operator =(CJS_Value value)236 void CJS_Value::operator = (CJS_Value value)
237 {
238 m_pValue = value.ToV8Value();
239
240 m_eType = value.m_eType;
241 m_isolate = value.m_isolate;
242 }
243
244 /* ---------------------------------------------------------------------------------------- */
245
GetType() const246 FXJSVALUETYPE CJS_Value::GetType() const
247 {
248 if(m_pValue.IsEmpty()) return VT_unknown;
249 if(m_pValue->IsString()) return VT_string;
250 if(m_pValue->IsNumber()) return VT_number;
251 if(m_pValue->IsBoolean()) return VT_boolean;
252 if(m_pValue->IsDate()) return VT_date;
253 if(m_pValue->IsObject()) return VT_object;
254 if(m_pValue->IsNull()) return VT_null;
255 if(m_pValue->IsUndefined()) return VT_undefined;
256 return VT_unknown;
257 }
258
IsArrayObject() const259 FX_BOOL CJS_Value::IsArrayObject() const
260 {
261 if(m_pValue.IsEmpty()) return FALSE;
262 return m_pValue->IsArray();
263 }
264
IsDateObject() const265 FX_BOOL CJS_Value::IsDateObject() const
266 {
267 if(m_pValue.IsEmpty()) return FALSE;
268 return m_pValue->IsDate();
269 }
270
271 //CJS_Value::operator CJS_Array()
ConvertToArray(CJS_Array & array) const272 FX_BOOL CJS_Value::ConvertToArray(CJS_Array &array) const
273 {
274 if (IsArrayObject())
275 {
276 array.Attach(JS_ToArray(m_isolate, m_pValue));
277 return TRUE;
278 }
279
280 return FALSE;
281 }
282
ConvertToDate(CJS_Date & date) const283 FX_BOOL CJS_Value::ConvertToDate(CJS_Date &date) const
284 {
285 // if (GetType() == VT_date)
286 // {
287 // date = (double)(*this);
288 // return TRUE;
289 // }
290
291 if (IsDateObject())
292 {
293 date.Attach(m_pValue);
294 return TRUE;
295 }
296
297 return FALSE;
298 }
299
300 /* ---------------------------- CJS_PropValue ---------------------------- */
301
CJS_PropValue(const CJS_Value & value)302 CJS_PropValue::CJS_PropValue(const CJS_Value &value) :
303 CJS_Value(value),
304 m_bIsSetting(0)
305 {
306 }
307
CJS_PropValue(v8::Isolate * isolate)308 CJS_PropValue::CJS_PropValue(v8::Isolate* isolate) : CJS_Value(isolate),
309 m_bIsSetting(0)
310 {
311 }
312
~CJS_PropValue()313 CJS_PropValue::~CJS_PropValue()
314 {
315 }
316
IsSetting()317 FX_BOOL CJS_PropValue::IsSetting()
318 {
319 return m_bIsSetting;
320 }
321
IsGetting()322 FX_BOOL CJS_PropValue::IsGetting()
323 {
324 return !m_bIsSetting;
325 }
326
operator <<(int iValue)327 void CJS_PropValue::operator <<(int iValue)
328 {
329 ASSERT(!m_bIsSetting);
330 CJS_Value::operator =(iValue);
331 }
332
operator >>(int & iValue) const333 void CJS_PropValue::operator >>(int & iValue) const
334 {
335 ASSERT(m_bIsSetting);
336 iValue = CJS_Value::ToInt();
337 }
338
339
operator <<(bool bValue)340 void CJS_PropValue::operator <<(bool bValue)
341 {
342 ASSERT(!m_bIsSetting);
343 CJS_Value::operator =(bValue);
344 }
345
operator >>(bool & bValue) const346 void CJS_PropValue::operator >>(bool& bValue) const
347 {
348 ASSERT(m_bIsSetting);
349 bValue = CJS_Value::ToBool();
350 }
351
operator <<(double dValue)352 void CJS_PropValue::operator <<(double dValue)
353 {
354 ASSERT(!m_bIsSetting);
355 CJS_Value::operator =(dValue);
356 }
357
operator >>(double & dValue) const358 void CJS_PropValue::operator >>(double& dValue) const
359 {
360 ASSERT(m_bIsSetting);
361 dValue = CJS_Value::ToDouble();
362 }
363
operator <<(CJS_Object * pObj)364 void CJS_PropValue::operator <<(CJS_Object* pObj)
365 {
366 ASSERT(!m_bIsSetting);
367 CJS_Value::operator = (pObj);
368 }
369
operator >>(CJS_Object * & ppObj) const370 void CJS_PropValue::operator >>(CJS_Object*& ppObj) const
371 {
372 ASSERT(m_bIsSetting);
373 ppObj = CJS_Value::ToCJSObject();
374 }
375
operator <<(CJS_Document * pJsDoc)376 void CJS_PropValue::operator <<(CJS_Document* pJsDoc)
377 {
378 ASSERT(!m_bIsSetting);
379 CJS_Value::operator = (pJsDoc);
380 }
381
operator >>(CJS_Document * & ppJsDoc) const382 void CJS_PropValue::operator >>(CJS_Document*& ppJsDoc) const
383 {
384 ASSERT(m_bIsSetting);
385 ppJsDoc = static_cast<CJS_Document*>(CJS_Value::ToCJSObject());
386 }
387
operator <<(JSFXObject pObj)388 void CJS_PropValue::operator<<(JSFXObject pObj)
389 {
390 ASSERT(!m_bIsSetting);
391 CJS_Value::operator = (pObj);
392 }
393
operator >>(JSFXObject & ppObj) const394 void CJS_PropValue::operator>>(JSFXObject &ppObj) const
395 {
396 ASSERT(m_bIsSetting);
397 ppObj = CJS_Value::ToV8Object();
398 }
399
400
StartSetting()401 void CJS_PropValue::StartSetting()
402 {
403 m_bIsSetting = 1;
404 }
405
StartGetting()406 void CJS_PropValue::StartGetting()
407 {
408 m_bIsSetting = 0;
409 }
operator <<(CFX_ByteString string)410 void CJS_PropValue::operator <<(CFX_ByteString string)
411 {
412 ASSERT(!m_bIsSetting);
413 CJS_Value::operator = (string.c_str());
414 }
415
operator >>(CFX_ByteString & string) const416 void CJS_PropValue::operator >>(CFX_ByteString &string) const
417 {
418 ASSERT(m_bIsSetting);
419 string = CJS_Value::ToCFXByteString();
420 }
421
operator <<(FX_LPCWSTR c_string)422 void CJS_PropValue::operator <<(FX_LPCWSTR c_string)
423 {
424 ASSERT(!m_bIsSetting);
425 CJS_Value::operator =(c_string);
426 }
427
operator >>(CFX_WideString & wide_string) const428 void CJS_PropValue::operator >>(CFX_WideString &wide_string) const
429 {
430 ASSERT(m_bIsSetting);
431 wide_string = CJS_Value::ToCFXWideString();
432 }
433
operator <<(CFX_WideString wide_string)434 void CJS_PropValue::operator <<(CFX_WideString wide_string)
435 {
436 ASSERT(!m_bIsSetting);
437 CJS_Value::operator = (wide_string.c_str());
438 }
439
operator >>(CJS_Array & array) const440 void CJS_PropValue::operator >>(CJS_Array &array) const
441 {
442 ASSERT(m_bIsSetting);
443 ConvertToArray(array);
444 }
445
operator <<(CJS_Array & array)446 void CJS_PropValue::operator <<(CJS_Array &array)
447 {
448 ASSERT(!m_bIsSetting);
449 CJS_Value::operator=(array);
450 }
451
operator >>(CJS_Date & date) const452 void CJS_PropValue::operator>>(CJS_Date &date) const
453 {
454 ASSERT(m_bIsSetting);
455 ConvertToDate(date);
456 }
457
operator <<(CJS_Date & date)458 void CJS_PropValue::operator<<(CJS_Date &date)
459 {
460 ASSERT(!m_bIsSetting);
461 CJS_Value::operator=(date);
462 }
463
operator v8::Local<v8::Value>() const464 CJS_PropValue::operator v8::Local<v8::Value>() const
465 {
466 return m_pValue;
467 }
468
469 /* ======================================== CJS_Array ========================================= */
CJS_Array(v8::Isolate * isolate)470 CJS_Array::CJS_Array(v8::Isolate* isolate):m_isolate(isolate)
471 {
472 }
473
~CJS_Array()474 CJS_Array::~CJS_Array()
475 {
476 }
477
Attach(v8::Local<v8::Array> pArray)478 void CJS_Array::Attach(v8::Local<v8::Array> pArray)
479 {
480 m_pArray = pArray;
481 }
482
IsAttached()483 FX_BOOL CJS_Array::IsAttached()
484 {
485 return FALSE;
486 }
487
GetElement(unsigned index,CJS_Value & value)488 void CJS_Array::GetElement(unsigned index,CJS_Value &value)
489 {
490 if (m_pArray.IsEmpty())
491 return;
492 v8::Local<v8::Value> p = JS_GetArrayElement(m_isolate, m_pArray,index);
493 value.Attach(p,VT_object);
494 }
495
SetElement(unsigned index,CJS_Value value)496 void CJS_Array::SetElement(unsigned index,CJS_Value value)
497 {
498 if (m_pArray.IsEmpty())
499 m_pArray = JS_NewArray(m_isolate);
500
501 JS_PutArrayElement(m_isolate, m_pArray, index, value.ToV8Value(), value.GetType());
502 }
503
GetLength()504 int CJS_Array::GetLength()
505 {
506 if (m_pArray.IsEmpty())
507 return 0;
508 return JS_GetArrayLength(m_pArray);
509 }
510
operator v8::Local<v8::Array>()511 CJS_Array:: operator v8::Local<v8::Array>()
512 {
513 if (m_pArray.IsEmpty())
514 m_pArray = JS_NewArray(m_isolate);
515
516 return m_pArray;
517 }
518
519 /* ======================================== CJS_Date ========================================= */
520
CJS_Date(v8::Isolate * isolate)521 CJS_Date::CJS_Date(v8::Isolate* isolate) :m_isolate(isolate)
522 {
523 }
524
CJS_Date(v8::Isolate * isolate,double dMsec_time)525 CJS_Date::CJS_Date(v8::Isolate* isolate,double dMsec_time)
526 {
527 m_isolate = isolate;
528 m_pDate = JS_NewDate(isolate,dMsec_time);
529 }
530
CJS_Date(v8::Isolate * isolate,int year,int mon,int day,int hour,int min,int sec)531 CJS_Date::CJS_Date(v8::Isolate* isolate,int year, int mon, int day,int hour, int min, int sec)
532 {
533 m_isolate = isolate;
534 m_pDate = JS_NewDate(isolate,MakeDate(year,mon,day,hour,min,sec,0));
535 }
536
MakeDate(int year,int mon,int day,int hour,int min,int sec,int ms)537 double CJS_Date::MakeDate(int year, int mon, int day,int hour, int min, int sec,int ms)
538 {
539 return JS_MakeDate(JS_MakeDay(year,mon,day), JS_MakeTime(hour,min,sec,ms));
540 }
541
~CJS_Date()542 CJS_Date::~CJS_Date()
543 {
544 }
545
IsValidDate()546 FX_BOOL CJS_Date::IsValidDate()
547 {
548 if(m_pDate.IsEmpty()) return FALSE;
549 return !JS_PortIsNan(JS_ToNumber(m_isolate, m_pDate));
550 }
551
Attach(v8::Local<v8::Value> pDate)552 void CJS_Date::Attach(v8::Local<v8::Value> pDate)
553 {
554 m_pDate = pDate;
555 }
556
GetYear()557 int CJS_Date::GetYear()
558 {
559 if (IsValidDate())
560 return JS_GetYearFromTime(JS_LocalTime(JS_ToNumber(m_isolate, m_pDate)));
561
562 return 0;
563 }
564
SetYear(int iYear)565 void CJS_Date::SetYear(int iYear)
566 {
567 double date = MakeDate(iYear,GetMonth(),GetDay(),GetHours(),GetMinutes(),GetSeconds(),0);
568 JS_ValueCopy(m_pDate, JS_NewDate(m_isolate,date));
569 }
570
GetMonth()571 int CJS_Date::GetMonth()
572 {
573 if (IsValidDate())
574 return JS_GetMonthFromTime(JS_LocalTime(JS_ToNumber(m_isolate, m_pDate)));
575
576 return 0;
577 }
578
SetMonth(int iMonth)579 void CJS_Date::SetMonth(int iMonth)
580 {
581
582 double date = MakeDate(GetYear(),iMonth,GetDay(),GetHours(),GetMinutes(),GetSeconds(),0);
583 JS_ValueCopy(m_pDate, JS_NewDate(m_isolate,date));
584
585 }
586
GetDay()587 int CJS_Date::GetDay()
588 {
589 if (IsValidDate())
590 return JS_GetDayFromTime(JS_LocalTime(JS_ToNumber(m_isolate, m_pDate)));
591
592 return 0;
593 }
594
SetDay(int iDay)595 void CJS_Date::SetDay(int iDay)
596 {
597
598 double date = MakeDate(GetYear(),GetMonth(),iDay,GetHours(),GetMinutes(),GetSeconds(),0);
599 JS_ValueCopy(m_pDate,JS_NewDate(m_isolate,date));
600
601 }
602
GetHours()603 int CJS_Date::GetHours()
604 {
605 if (IsValidDate())
606 return JS_GetHourFromTime(JS_LocalTime(JS_ToNumber(m_isolate, m_pDate)));
607
608 return 0;
609 }
610
SetHours(int iHours)611 void CJS_Date::SetHours(int iHours)
612 {
613 double date = MakeDate(GetYear(),GetMonth(),GetDay(),iHours,GetMinutes(),GetSeconds(),0);
614 JS_ValueCopy(m_pDate,JS_NewDate(m_isolate,date));
615 }
616
GetMinutes()617 int CJS_Date::GetMinutes()
618 {
619 if (IsValidDate())
620 return JS_GetMinFromTime(JS_LocalTime(JS_ToNumber(m_isolate, m_pDate)));
621
622 return 0;
623 }
624
SetMinutes(int minutes)625 void CJS_Date::SetMinutes(int minutes)
626 {
627 double date = MakeDate(GetYear(),GetMonth(),GetDay(),GetHours(),minutes,GetSeconds(),0);
628 JS_ValueCopy(m_pDate,JS_NewDate(m_isolate,date));
629 }
630
GetSeconds()631 int CJS_Date::GetSeconds()
632 {
633 if (IsValidDate())
634 return JS_GetSecFromTime(JS_LocalTime(JS_ToNumber(m_isolate, m_pDate)));
635
636 return 0;
637 }
638
SetSeconds(int seconds)639 void CJS_Date::SetSeconds(int seconds)
640 {
641 double date = MakeDate(GetYear(),GetMonth(),GetDay(),GetHours(),GetMinutes(),seconds,0);
642 JS_ValueCopy(m_pDate,JS_NewDate(m_isolate,date));
643 }
644
operator v8::Local<v8::Value>()645 CJS_Date::operator v8::Local<v8::Value>()
646 {
647 return m_pDate;
648 }
649
operator double() const650 CJS_Date::operator double() const
651 {
652 if(m_pDate.IsEmpty())
653 return 0.0;
654 return JS_ToNumber(m_isolate, m_pDate);
655 }
656
ToString() const657 CFX_WideString CJS_Date::ToString() const
658 {
659 if(m_pDate.IsEmpty())
660 return L"";
661 return JS_ToString(m_isolate, m_pDate);
662 }
663