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::Local<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, const FX_WCHAR* pWstr):m_isolate(isolate)
65 CJS_Value::CJS_Value(v8::Isolate* isolate, const FX_CHAR* 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::Local<v8::Value> pValue,FXJSVALUETYPE t)
85 void CJS_Value::Attach(CJS_Value *pValue)
88 Attach(pValue->ToV8Value(), pValue->GetType());
91 void CJS_Value::Detach()
93 m_pValue = v8::Local<v8::Value>();
97 /* ---------------------------------------------------------------------------------------- */
99 int CJS_Value::ToInt() const
101 return JS_ToInt32(m_isolate, m_pValue);
104 bool CJS_Value::ToBool() const
106 return JS_ToBoolean(m_isolate, m_pValue);
109 double CJS_Value::ToDouble() const
111 return JS_ToNumber(m_isolate, m_pValue);
114 float CJS_Value::ToFloat() const
116 return (float)ToDouble();
119 CJS_Object* CJS_Value::ToCJSObject() const
121 v8::Local<v8::Object> pObj = JS_ToObject(m_isolate, m_pValue);
122 return (CJS_Object*)JS_GetPrivate(m_isolate, pObj);
125 v8::Local<v8::Object> CJS_Value::ToV8Object() const
127 return JS_ToObject(m_isolate, m_pValue);
130 CFX_WideString CJS_Value::ToCFXWideString() const
132 return JS_ToString(m_isolate, m_pValue);
135 CFX_ByteString CJS_Value::ToCFXByteString() const
137 return CFX_ByteString::FromUnicode(ToCFXWideString());
140 v8::Local<v8::Value> CJS_Value::ToV8Value() const
145 v8::Local<v8::Array>CJS_Value::ToV8Array() const
148 return v8::Local<v8::Array>::Cast(JS_ToObject(m_isolate, m_pValue));
149 return v8::Local<v8::Array>();
152 /* ---------------------------------------------------------------------------------------- */
154 void CJS_Value::operator =(int iValue)
156 m_pValue = JS_NewNumber(m_isolate, iValue);
161 void CJS_Value::operator =(bool bValue)
163 m_pValue = JS_NewBoolean(m_isolate, bValue);
165 m_eType = VT_boolean;
168 void CJS_Value::operator =(double dValue)
170 m_pValue = JS_NewNumber(m_isolate,dValue);
175 void CJS_Value::operator = (float fValue)
177 m_pValue = JS_NewNumber(m_isolate,fValue);
181 void CJS_Value::operator =(v8::Local<v8::Object> pObj)
184 m_pValue = JS_NewObject(m_isolate,pObj);
186 m_eType = VT_fxobject;
189 void CJS_Value::operator =(CJS_Object * pObj)
192 operator = ((JSFXObject)*pObj);
195 void CJS_Value::operator = (CJS_Document* pJsDoc)
199 m_pValue = static_cast<JSFXObject>(*pJsDoc);
203 void CJS_Value::operator =(const FX_WCHAR* pWstr)
205 m_pValue = JS_NewString(m_isolate,(wchar_t *)pWstr);
210 void CJS_Value::SetNull()
212 m_pValue = JS_NewNull();
217 void CJS_Value::operator = (const FX_CHAR* pStr)
219 operator = (CFX_WideString::FromLocal(pStr).c_str());
222 void CJS_Value::operator = (CJS_Array & array)
224 m_pValue = JS_NewObject2(m_isolate,(v8::Local<v8::Array>)array);
229 void CJS_Value::operator = (CJS_Date & date)
231 m_pValue = JS_NewDate(m_isolate, (double)date);
236 void CJS_Value::operator = (CJS_Value value)
238 m_pValue = value.ToV8Value();
240 m_eType = value.m_eType;
241 m_isolate = value.m_isolate;
244 /* ---------------------------------------------------------------------------------------- */
246 FXJSVALUETYPE CJS_Value::GetType() const
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;
259 bool CJS_Value::IsArrayObject() const
261 if(m_pValue.IsEmpty()) return false;
262 return m_pValue->IsArray();
265 bool CJS_Value::IsDateObject() const
267 if(m_pValue.IsEmpty()) return false;
268 return m_pValue->IsDate();
271 //CJS_Value::operator CJS_Array()
272 bool CJS_Value::ConvertToArray(CJS_Array &array) const
276 array.Attach(JS_ToArray(m_isolate, m_pValue));
283 bool CJS_Value::ConvertToDate(CJS_Date &date) const
285 // if (GetType() == VT_date)
287 // date = (double)(*this);
293 date.Attach(m_pValue);
300 /* ---------------------------- CJS_PropValue ---------------------------- */
302 CJS_PropValue::CJS_PropValue(const CJS_Value &value) :
308 CJS_PropValue::CJS_PropValue(v8::Isolate* isolate) : CJS_Value(isolate),
313 CJS_PropValue::~CJS_PropValue()
317 bool CJS_PropValue::IsSetting()
322 bool CJS_PropValue::IsGetting()
324 return !m_bIsSetting;
327 void CJS_PropValue::operator <<(int iValue)
329 ASSERT(!m_bIsSetting);
330 CJS_Value::operator =(iValue);
333 void CJS_PropValue::operator >>(int & iValue) const
335 ASSERT(m_bIsSetting);
336 iValue = CJS_Value::ToInt();
340 void CJS_PropValue::operator <<(bool bValue)
342 ASSERT(!m_bIsSetting);
343 CJS_Value::operator =(bValue);
346 void CJS_PropValue::operator >>(bool& bValue) const
348 ASSERT(m_bIsSetting);
349 bValue = CJS_Value::ToBool();
352 void CJS_PropValue::operator <<(double dValue)
354 ASSERT(!m_bIsSetting);
355 CJS_Value::operator =(dValue);
358 void CJS_PropValue::operator >>(double& dValue) const
360 ASSERT(m_bIsSetting);
361 dValue = CJS_Value::ToDouble();
364 void CJS_PropValue::operator <<(CJS_Object* pObj)
366 ASSERT(!m_bIsSetting);
367 CJS_Value::operator = (pObj);
370 void CJS_PropValue::operator >>(CJS_Object*& ppObj) const
372 ASSERT(m_bIsSetting);
373 ppObj = CJS_Value::ToCJSObject();
376 void CJS_PropValue::operator <<(CJS_Document* pJsDoc)
378 ASSERT(!m_bIsSetting);
379 CJS_Value::operator = (pJsDoc);
382 void CJS_PropValue::operator >>(CJS_Document*& ppJsDoc) const
384 ASSERT(m_bIsSetting);
385 ppJsDoc = static_cast<CJS_Document*>(CJS_Value::ToCJSObject());
388 void CJS_PropValue::operator<<(JSFXObject pObj)
390 ASSERT(!m_bIsSetting);
391 CJS_Value::operator = (pObj);
394 void CJS_PropValue::operator>>(JSFXObject &ppObj) const
396 ASSERT(m_bIsSetting);
397 ppObj = CJS_Value::ToV8Object();
401 void CJS_PropValue::StartSetting()
406 void CJS_PropValue::StartGetting()
410 void CJS_PropValue::operator <<(CFX_ByteString string)
412 ASSERT(!m_bIsSetting);
413 CJS_Value::operator = (string.c_str());
416 void CJS_PropValue::operator >>(CFX_ByteString &string) const
418 ASSERT(m_bIsSetting);
419 string = CJS_Value::ToCFXByteString();
422 void CJS_PropValue::operator <<(const FX_WCHAR* c_string)
424 ASSERT(!m_bIsSetting);
425 CJS_Value::operator =(c_string);
428 void CJS_PropValue::operator >>(CFX_WideString &wide_string) const
430 ASSERT(m_bIsSetting);
431 wide_string = CJS_Value::ToCFXWideString();
434 void CJS_PropValue::operator <<(CFX_WideString wide_string)
436 ASSERT(!m_bIsSetting);
437 CJS_Value::operator = (wide_string.c_str());
440 void CJS_PropValue::operator >>(CJS_Array &array) const
442 ASSERT(m_bIsSetting);
443 ConvertToArray(array);
446 void CJS_PropValue::operator <<(CJS_Array &array)
448 ASSERT(!m_bIsSetting);
449 CJS_Value::operator=(array);
452 void CJS_PropValue::operator>>(CJS_Date &date) const
454 ASSERT(m_bIsSetting);
458 void CJS_PropValue::operator<<(CJS_Date &date)
460 ASSERT(!m_bIsSetting);
461 CJS_Value::operator=(date);
464 CJS_PropValue::operator v8::Local<v8::Value>() const
469 /* ======================================== CJS_Array ========================================= */
470 CJS_Array::CJS_Array(v8::Isolate* isolate):m_isolate(isolate)
474 CJS_Array::~CJS_Array()
478 void CJS_Array::Attach(v8::Local<v8::Array> pArray)
483 bool CJS_Array::IsAttached()
488 void CJS_Array::GetElement(unsigned index,CJS_Value &value)
490 if (m_pArray.IsEmpty())
492 v8::Local<v8::Value> p = JS_GetArrayElement(m_isolate, m_pArray,index);
493 value.Attach(p,VT_object);
496 void CJS_Array::SetElement(unsigned index,CJS_Value value)
498 if (m_pArray.IsEmpty())
499 m_pArray = JS_NewArray(m_isolate);
501 JS_PutArrayElement(m_isolate, m_pArray, index, value.ToV8Value(), value.GetType());
504 int CJS_Array::GetLength()
506 if (m_pArray.IsEmpty())
508 return JS_GetArrayLength(m_pArray);
511 CJS_Array:: operator v8::Local<v8::Array>()
513 if (m_pArray.IsEmpty())
514 m_pArray = JS_NewArray(m_isolate);
519 /* ======================================== CJS_Date ========================================= */
521 CJS_Date::CJS_Date(v8::Isolate* isolate) :m_isolate(isolate)
525 CJS_Date::CJS_Date(v8::Isolate* isolate,double dMsec_time)
528 m_pDate = JS_NewDate(isolate,dMsec_time);
531 CJS_Date::CJS_Date(v8::Isolate* isolate,int year, int mon, int day,int hour, int min, int sec)
534 m_pDate = JS_NewDate(isolate,MakeDate(year,mon,day,hour,min,sec,0));
537 double CJS_Date::MakeDate(int year, int mon, int day,int hour, int min, int sec,int ms)
539 return JS_MakeDate(JS_MakeDay(year,mon,day), JS_MakeTime(hour,min,sec,ms));
542 CJS_Date::~CJS_Date()
546 bool CJS_Date::IsValidDate()
548 if(m_pDate.IsEmpty()) return false;
549 return !JS_PortIsNan(JS_ToNumber(m_isolate, m_pDate));
552 void CJS_Date::Attach(v8::Local<v8::Value> pDate)
557 int CJS_Date::GetYear()
560 return JS_GetYearFromTime(JS_LocalTime(JS_ToNumber(m_isolate, m_pDate)));
565 void CJS_Date::SetYear(int iYear)
567 double date = MakeDate(iYear,GetMonth(),GetDay(),GetHours(),GetMinutes(),GetSeconds(),0);
568 JS_ValueCopy(m_pDate, JS_NewDate(m_isolate,date));
571 int CJS_Date::GetMonth()
574 return JS_GetMonthFromTime(JS_LocalTime(JS_ToNumber(m_isolate, m_pDate)));
579 void CJS_Date::SetMonth(int iMonth)
582 double date = MakeDate(GetYear(),iMonth,GetDay(),GetHours(),GetMinutes(),GetSeconds(),0);
583 JS_ValueCopy(m_pDate, JS_NewDate(m_isolate,date));
587 int CJS_Date::GetDay()
590 return JS_GetDayFromTime(JS_LocalTime(JS_ToNumber(m_isolate, m_pDate)));
595 void CJS_Date::SetDay(int iDay)
598 double date = MakeDate(GetYear(),GetMonth(),iDay,GetHours(),GetMinutes(),GetSeconds(),0);
599 JS_ValueCopy(m_pDate,JS_NewDate(m_isolate,date));
603 int CJS_Date::GetHours()
606 return JS_GetHourFromTime(JS_LocalTime(JS_ToNumber(m_isolate, m_pDate)));
611 void CJS_Date::SetHours(int iHours)
613 double date = MakeDate(GetYear(),GetMonth(),GetDay(),iHours,GetMinutes(),GetSeconds(),0);
614 JS_ValueCopy(m_pDate,JS_NewDate(m_isolate,date));
617 int CJS_Date::GetMinutes()
620 return JS_GetMinFromTime(JS_LocalTime(JS_ToNumber(m_isolate, m_pDate)));
625 void CJS_Date::SetMinutes(int minutes)
627 double date = MakeDate(GetYear(),GetMonth(),GetDay(),GetHours(),minutes,GetSeconds(),0);
628 JS_ValueCopy(m_pDate,JS_NewDate(m_isolate,date));
631 int CJS_Date::GetSeconds()
634 return JS_GetSecFromTime(JS_LocalTime(JS_ToNumber(m_isolate, m_pDate)));
639 void CJS_Date::SetSeconds(int seconds)
641 double date = MakeDate(GetYear(),GetMonth(),GetDay(),GetHours(),GetMinutes(),seconds,0);
642 JS_ValueCopy(m_pDate,JS_NewDate(m_isolate,date));
645 CJS_Date::operator v8::Local<v8::Value>()
650 CJS_Date::operator double() const
652 if(m_pDate.IsEmpty())
654 return JS_ToNumber(m_isolate, m_pDate);
657 CFX_WideString CJS_Date::ToString() const
659 if(m_pDate.IsEmpty())
661 return JS_ToString(m_isolate, m_pDate);