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.
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
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"
13 /* ---------------------------- CJS_Value ---------------------------- */
15 CJS_Value::CJS_Value(v8::Isolate* isolate) : m_eType(VT_unknown),m_isolate(isolate)
18 CJS_Value::CJS_Value(v8::Isolate* isolate, v8::Handle<v8::Value> pValue,FXJSVALUETYPE t) :
19 m_pValue(pValue), m_eType(t), m_isolate(isolate)
23 CJS_Value::CJS_Value(v8::Isolate* isolate, const int &iValue):m_isolate(isolate)
28 CJS_Value::CJS_Value(v8::Isolate* isolate, const bool &bValue):m_isolate(isolate)
33 CJS_Value::CJS_Value(v8::Isolate* isolate, const float &fValue):m_isolate(isolate)
38 CJS_Value::CJS_Value(v8::Isolate* isolate, const double &dValue):m_isolate(isolate)
43 CJS_Value::CJS_Value(v8::Isolate* isolate, JSFXObject pJsObj):m_isolate(isolate)
48 CJS_Value::CJS_Value(v8::Isolate* isolate, CJS_Object* pJsObj):m_isolate(isolate)
53 CJS_Value::CJS_Value(v8::Isolate* isolate, CJS_Document* pJsDoc):m_isolate(isolate)
57 m_pValue = (JSFXObject)*pJsDoc;
60 CJS_Value::CJS_Value(v8::Isolate* isolate, FX_LPCWSTR pWstr):m_isolate(isolate)
65 CJS_Value::CJS_Value(v8::Isolate* isolate, FX_LPCSTR pStr):m_isolate(isolate)
70 CJS_Value::CJS_Value(v8::Isolate* isolate, CJS_Array& array):m_isolate(isolate)
75 CJS_Value::~CJS_Value()
79 void CJS_Value::Attach(v8::Handle<v8::Value> pValue,FXJSVALUETYPE t)
85 void CJS_Value::Attach(CJS_Value *pValue)
88 Attach(pValue->ToJSValue(),pValue->GetType());
91 void CJS_Value::Detach()
93 m_pValue = v8::Handle<v8::Value>();
97 /* ---------------------------------------------------------------------------------------- */
99 CJS_Value::operator int() const
102 return JS_ToInt32(m_pValue);
106 CJS_Value::operator bool() const
109 return JS_ToBoolean(m_pValue);
113 CJS_Value::operator double() const
116 return JS_ToNumber(m_pValue);
120 CJS_Value::operator float() const
123 return (float)JS_ToNumber(m_pValue);
127 CJS_Value::operator CJS_Object *() const
130 v8::Handle<v8::Object> pObj = JS_ToObject(m_pValue);
131 return (CJS_Object*)JS_GetPrivate(m_isolate, pObj);
134 CJS_Value::operator v8::Handle<v8::Object>() const
136 return JS_ToObject(m_pValue);
139 CJS_Value::operator CFX_WideString() const
141 return JS_ToString(m_pValue);
144 CJS_Value::operator CFX_ByteString() const
146 return CFX_ByteString::FromUnicode(operator CFX_WideString());
149 v8::Handle<v8::Value> CJS_Value::ToJSValue()
155 CJS_Value::operator v8::Handle<v8::Array>() const
158 return v8::Handle<v8::Array>::Cast(JS_ToObject(m_pValue));
159 return v8::Handle<v8::Array>();
162 /* ---------------------------------------------------------------------------------------- */
164 void CJS_Value::operator =(int iValue)
166 m_pValue = JS_NewNumber(m_isolate, iValue);
171 void CJS_Value::operator =(bool bValue)
173 m_pValue = JS_NewBoolean(m_isolate, bValue);
175 m_eType = VT_boolean;
178 void CJS_Value::operator =(double dValue)
180 m_pValue = JS_NewNumber(m_isolate,dValue);
185 void CJS_Value::operator = (float fValue)
187 m_pValue = JS_NewNumber(m_isolate,fValue);
191 void CJS_Value::operator =(v8::Handle<v8::Object> pObj)
194 m_pValue = JS_NewObject(m_isolate,pObj);
196 m_eType = VT_fxobject;
199 void CJS_Value::operator =(CJS_Object * pObj)
202 operator = ((JSFXObject)*pObj);
205 void CJS_Value::operator = (CJS_Document* pJsDoc)
209 m_pValue = static_cast<JSFXObject>(*pJsDoc);
213 void CJS_Value::operator =(FX_LPCWSTR pWstr)
215 m_pValue = JS_NewString(m_isolate,(wchar_t *)pWstr);
220 void CJS_Value::SetNull()
222 m_pValue = JS_NewNull();
227 void CJS_Value::operator = (FX_LPCSTR pStr)
229 operator = (CFX_WideString::FromLocal(pStr));
232 void CJS_Value::operator = (CJS_Array & array)
234 m_pValue = JS_NewObject2(m_isolate,(v8::Handle<v8::Array>)array);
239 void CJS_Value::operator = (CJS_Date & date)
241 m_pValue = JS_NewDate(m_isolate, (double)date);
246 void CJS_Value::operator = (CJS_Value value)
248 m_pValue = value.ToJSValue();
250 m_eType = value.m_eType;
253 /* ---------------------------------------------------------------------------------------- */
255 FXJSVALUETYPE CJS_Value::GetType() const
257 if(m_pValue.IsEmpty()) return VT_unknown;
258 if(m_pValue->IsString()) return VT_string;
259 if(m_pValue->IsNumber()) return VT_number;
260 if(m_pValue->IsBoolean()) return VT_boolean;
261 if(m_pValue->IsDate()) return VT_date;
262 if(m_pValue->IsObject()) return VT_object;
263 if(m_pValue->IsNull()) return VT_null;
264 if(m_pValue->IsUndefined()) return VT_undefined;
268 FX_BOOL CJS_Value::IsArrayObject() const
270 if(m_pValue.IsEmpty()) return FALSE;
271 return m_pValue->IsArray();
274 FX_BOOL CJS_Value::IsDateObject() const
276 if(m_pValue.IsEmpty()) return FALSE;
277 return m_pValue->IsDate();
280 //CJS_Value::operator CJS_Array()
281 FX_BOOL CJS_Value::ConvertToArray(CJS_Array &array) const
285 array.Attach(JS_ToArray(m_pValue));
292 FX_BOOL CJS_Value::ConvertToDate(CJS_Date &date) const
294 // if (GetType() == VT_date)
296 // date = (double)(*this);
302 date.Attach(m_pValue);
309 /* ---------------------------- CJS_PropValue ---------------------------- */
311 CJS_PropValue::CJS_PropValue(const CJS_Value &value) :
317 CJS_PropValue::CJS_PropValue(v8::Isolate* isolate) : CJS_Value(isolate),
322 CJS_PropValue::~CJS_PropValue()
326 FX_BOOL CJS_PropValue::IsSetting()
331 FX_BOOL CJS_PropValue::IsGetting()
333 return !m_bIsSetting;
336 void CJS_PropValue::operator <<(int iValue)
338 ASSERT(!m_bIsSetting);
339 CJS_Value::operator =(iValue);
342 void CJS_PropValue::operator >>(int & iValue) const
344 ASSERT(m_bIsSetting);
345 iValue = CJS_Value::operator int();
349 void CJS_PropValue::operator <<(bool bValue)
351 ASSERT(!m_bIsSetting);
352 CJS_Value::operator =(bValue);
355 void CJS_PropValue::operator >>(bool& bValue) const
357 ASSERT(m_bIsSetting);
358 bValue = CJS_Value::operator bool();
362 void CJS_PropValue::operator <<(double dValue)
364 ASSERT(!m_bIsSetting);
365 CJS_Value::operator =(dValue);
368 void CJS_PropValue::operator >>(double& dValue) const
370 ASSERT(m_bIsSetting);
371 dValue = CJS_Value::operator double();
374 void CJS_PropValue::operator <<(CJS_Object* pObj)
376 ASSERT(!m_bIsSetting);
377 CJS_Value::operator = (pObj);
380 void CJS_PropValue::operator >>(CJS_Object*& ppObj) const
382 ASSERT(m_bIsSetting);
383 ppObj = CJS_Value::operator CJS_Object *();
386 void CJS_PropValue::operator <<(CJS_Document* pJsDoc)
388 ASSERT(!m_bIsSetting);
389 CJS_Value::operator = (pJsDoc);
392 void CJS_PropValue::operator >>(CJS_Document*& ppJsDoc) const
394 ASSERT(m_bIsSetting);
395 ppJsDoc = static_cast<CJS_Document*>(CJS_Value::operator CJS_Object *());
398 void CJS_PropValue::operator<<(JSFXObject pObj)
400 ASSERT(!m_bIsSetting);
401 CJS_Value::operator = (pObj);
404 void CJS_PropValue::operator>>(JSFXObject &ppObj) const
406 ASSERT(m_bIsSetting);
407 ppObj = CJS_Value::operator JSFXObject ();
411 void CJS_PropValue::StartSetting()
416 void CJS_PropValue::StartGetting()
420 void CJS_PropValue::operator <<(CFX_ByteString string)
422 ASSERT(!m_bIsSetting);
423 CJS_Value::operator = (string.c_str());
426 void CJS_PropValue::operator >>(CFX_ByteString &string) const
428 ASSERT(m_bIsSetting);
429 string = CJS_Value::operator CFX_ByteString();
432 void CJS_PropValue::operator <<(FX_LPCWSTR c_string)
434 ASSERT(!m_bIsSetting);
435 CJS_Value::operator =(c_string);
438 void CJS_PropValue::operator >>(CFX_WideString &wide_string) const
440 ASSERT(m_bIsSetting);
441 wide_string = CJS_Value::operator CFX_WideString();
444 void CJS_PropValue::operator <<(CFX_WideString wide_string)
446 ASSERT(!m_bIsSetting);
447 CJS_Value::operator = (wide_string);
450 void CJS_PropValue::operator >>(CJS_Array &array) const
452 ASSERT(m_bIsSetting);
453 ConvertToArray(array);
456 void CJS_PropValue::operator <<(CJS_Array &array)
458 ASSERT(!m_bIsSetting);
459 CJS_Value::operator=(array);
462 void CJS_PropValue::operator>>(CJS_Date &date) const
464 ASSERT(m_bIsSetting);
468 void CJS_PropValue::operator<<(CJS_Date &date)
470 ASSERT(!m_bIsSetting);
471 CJS_Value::operator=(date);
474 CJS_PropValue::operator v8::Handle<v8::Value>() const
479 /* ======================================== CJS_Array ========================================= */
480 CJS_Array::CJS_Array(v8::Isolate* isolate):m_isolate(isolate)
484 CJS_Array::~CJS_Array()
488 void CJS_Array::Attach(v8::Handle<v8::Array> pArray)
493 FX_BOOL CJS_Array::IsAttached()
498 void CJS_Array::GetElement(unsigned index,CJS_Value &value)
500 if (m_pArray.IsEmpty())
502 v8::Handle<v8::Value> p = JS_GetArrayElemnet(m_pArray,index);
503 value.Attach(p,VT_object);
506 void CJS_Array::SetElement(unsigned index,CJS_Value value)
508 if (m_pArray.IsEmpty())
509 m_pArray = JS_NewArray(m_isolate);
511 JS_PutArrayElement(m_pArray,index,value.ToJSValue(),value.GetType());
514 int CJS_Array::GetLength()
516 if (m_pArray.IsEmpty())
518 return JS_GetArrayLength(m_pArray);
521 CJS_Array:: operator v8::Handle<v8::Array>()
523 if (m_pArray.IsEmpty())
524 m_pArray = JS_NewArray(m_isolate);
529 /* ======================================== CJS_Date ========================================= */
531 CJS_Date::CJS_Date(v8::Isolate* isolate) :m_isolate(isolate)
535 CJS_Date::CJS_Date(v8::Isolate* isolate,double dMsec_time)
538 m_pDate = JS_NewDate(isolate,dMsec_time);
541 CJS_Date::CJS_Date(v8::Isolate* isolate,int year, int mon, int day,int hour, int min, int sec)
544 m_pDate = JS_NewDate(isolate,MakeDate(year,mon,day,hour,min,sec,0));
547 double CJS_Date::MakeDate(int year, int mon, int day,int hour, int min, int sec,int ms)
549 return JS_MakeDate(JS_MakeDay(year,mon,day), JS_MakeTime(hour,min,sec,ms));
552 CJS_Date::~CJS_Date()
556 FX_BOOL CJS_Date::IsValidDate()
558 if(m_pDate.IsEmpty()) return FALSE;
559 return !JS_PortIsNan(JS_ToNumber(m_pDate));
562 void CJS_Date::Attach(v8::Handle<v8::Value> pDate)
567 int CJS_Date::GetYear()
570 return JS_GetYearFromTime(JS_LocalTime(JS_ToNumber(m_pDate)));
575 void CJS_Date::SetYear(int iYear)
577 double date = MakeDate(iYear,GetMonth(),GetDay(),GetHours(),GetMinutes(),GetSeconds(),0);
578 JS_ValueCopy(m_pDate, JS_NewDate(m_isolate,date));
581 int CJS_Date::GetMonth()
584 return JS_GetMonthFromTime(JS_LocalTime(JS_ToNumber(m_pDate)));
589 void CJS_Date::SetMonth(int iMonth)
592 double date = MakeDate(GetYear(),iMonth,GetDay(),GetHours(),GetMinutes(),GetSeconds(),0);
593 JS_ValueCopy(m_pDate, JS_NewDate(m_isolate,date));
597 int CJS_Date::GetDay()
600 return JS_GetDayFromTime(JS_LocalTime(JS_ToNumber(m_pDate)));
605 void CJS_Date::SetDay(int iDay)
608 double date = MakeDate(GetYear(),GetMonth(),iDay,GetHours(),GetMinutes(),GetSeconds(),0);
609 JS_ValueCopy(m_pDate,JS_NewDate(m_isolate,date));
613 int CJS_Date::GetHours()
616 return JS_GetHourFromTime(JS_LocalTime(JS_ToNumber(m_pDate)));
621 void CJS_Date::SetHours(int iHours)
623 double date = MakeDate(GetYear(),GetMonth(),GetDay(),iHours,GetMinutes(),GetSeconds(),0);
624 JS_ValueCopy(m_pDate,JS_NewDate(m_isolate,date));
627 int CJS_Date::GetMinutes()
630 return JS_GetMinFromTime(JS_LocalTime(JS_ToNumber(m_pDate)));
635 void CJS_Date::SetMinutes(int minutes)
637 double date = MakeDate(GetYear(),GetMonth(),GetDay(),GetHours(),minutes,GetSeconds(),0);
638 JS_ValueCopy(m_pDate,JS_NewDate(m_isolate,date));
641 int CJS_Date::GetSeconds()
644 return JS_GetSecFromTime(JS_LocalTime(JS_ToNumber(m_pDate)));
649 void CJS_Date::SetSeconds(int seconds)
651 double date = MakeDate(GetYear(),GetMonth(),GetDay(),GetHours(),GetMinutes(),seconds,0);
652 JS_ValueCopy(m_pDate,JS_NewDate(m_isolate,date));
655 CJS_Date::operator v8::Handle<v8::Value>()
660 CJS_Date::operator double() const
662 if(m_pDate.IsEmpty())
664 return JS_ToNumber(m_pDate);
667 CFX_WideString CJS_Date::ToString() const
669 if(m_pDate.IsEmpty())
671 return JS_ToString(m_pDate);