blob: 8517b76dac29422b3833ef1adc3717ae5c7e9408 [file] [log] [blame]
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07001// 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.
Lei Zhanga6d9f0e2015-06-13 00:48:38 -07004
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07005// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
Tom Sepez37458412015-10-06 11:33:46 -07007#ifndef FPDFSDK_SRC_JAVASCRIPT_JS_VALUE_H_
8#define FPDFSDK_SRC_JAVASCRIPT_JS_VALUE_H_
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -07009
Lei Zhanga688a042015-11-09 13:57:49 -080010#include "core/include/fxcrt/fx_basic.h"
Lei Zhangbde53d22015-11-12 22:21:30 -080011#include "fpdfsdk/include/jsapi/fxjs_v8.h"
Tom Sepez9a3f8122015-04-07 15:35:48 -070012
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070013class CJS_Array;
14class CJS_Date;
Tom Sepezf79a69c2014-10-30 13:23:42 -070015class CJS_Document;
16class CJS_Object;
Tom Sepez67fd5df2015-10-08 12:24:19 -070017class CJS_Runtime;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070018
Nico Weber9d8ec5a2015-08-04 13:00:21 -070019class CJS_Value {
20 public:
Tom Sepez39bfe122015-09-17 15:25:23 -070021 enum Type {
22 VT_unknown,
23 VT_string,
24 VT_number,
25 VT_boolean,
26 VT_date,
27 VT_object,
28 VT_fxobject,
29 VT_null,
30 VT_undefined
31 };
32
Tom Sepez67fd5df2015-10-08 12:24:19 -070033 CJS_Value(CJS_Runtime* pRuntime);
34 CJS_Value(CJS_Runtime* pRuntime, v8::Local<v8::Value> pValue, Type t);
35 CJS_Value(CJS_Runtime* pRuntime, const int& iValue);
36 CJS_Value(CJS_Runtime* pRuntime, const double& dValue);
37 CJS_Value(CJS_Runtime* pRuntime, const float& fValue);
38 CJS_Value(CJS_Runtime* pRuntime, const bool& bValue);
39 CJS_Value(CJS_Runtime* pRuntime, v8::Local<v8::Object>);
40 CJS_Value(CJS_Runtime* pRuntime, CJS_Object*);
41 CJS_Value(CJS_Runtime* pRuntime, CJS_Document*);
42 CJS_Value(CJS_Runtime* pRuntime, const FX_CHAR* pStr);
43 CJS_Value(CJS_Runtime* pRuntime, const FX_WCHAR* pWstr);
44 CJS_Value(CJS_Runtime* pRuntime, CJS_Array& array);
Tom Sepezf79a69c2014-10-30 13:23:42 -070045
Nico Weber9d8ec5a2015-08-04 13:00:21 -070046 ~CJS_Value();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070047
Nico Weber9d8ec5a2015-08-04 13:00:21 -070048 void SetNull();
Tom Sepez39bfe122015-09-17 15:25:23 -070049 void Attach(v8::Local<v8::Value> pValue, Type t);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070050 void Attach(CJS_Value* pValue);
51 void Detach();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070052
Tom Sepez39bfe122015-09-17 15:25:23 -070053 Type GetType() const;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070054 int ToInt() const;
55 bool ToBool() const;
56 double ToDouble() const;
57 float ToFloat() const;
58 CJS_Object* ToCJSObject() const;
59 CFX_WideString ToCFXWideString() const;
60 CFX_ByteString ToCFXByteString() const;
61 v8::Local<v8::Object> ToV8Object() const;
62 v8::Local<v8::Array> ToV8Array() const;
63 v8::Local<v8::Value> ToV8Value() const;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070064
Nico Weber9d8ec5a2015-08-04 13:00:21 -070065 void operator=(int iValue);
66 void operator=(bool bValue);
67 void operator=(double);
68 void operator=(float);
69 void operator=(CJS_Object*);
70 void operator=(CJS_Document*);
71 void operator=(v8::Local<v8::Object>);
72 void operator=(CJS_Array&);
73 void operator=(CJS_Date&);
74 void operator=(const FX_WCHAR* pWstr);
75 void operator=(const FX_CHAR* pStr);
76 void operator=(CJS_Value value);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070077
Nico Weber9d8ec5a2015-08-04 13:00:21 -070078 FX_BOOL IsArrayObject() const;
79 FX_BOOL IsDateObject() const;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070080 FX_BOOL ConvertToArray(CJS_Array&) const;
81 FX_BOOL ConvertToDate(CJS_Date&) const;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070082
Tom Sepez67fd5df2015-10-08 12:24:19 -070083 CJS_Runtime* GetJSRuntime() const { return m_pJSRuntime; }
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070084
Nico Weber9d8ec5a2015-08-04 13:00:21 -070085 protected:
Tom Sepez39bfe122015-09-17 15:25:23 -070086 Type m_eType;
Nico Weber9d8ec5a2015-08-04 13:00:21 -070087 v8::Local<v8::Value> m_pValue;
Tom Sepez67fd5df2015-10-08 12:24:19 -070088 CJS_Runtime* m_pJSRuntime;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -070089};
90
Nico Weber9d8ec5a2015-08-04 13:00:21 -070091class CJS_PropValue : public CJS_Value {
92 public:
93 CJS_PropValue(const CJS_Value&);
Tom Sepez67fd5df2015-10-08 12:24:19 -070094 CJS_PropValue(CJS_Runtime* pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -070095 ~CJS_PropValue();
96
Tom Sepez67fd5df2015-10-08 12:24:19 -070097 FX_BOOL IsSetting() const { return m_bIsSetting; }
98 FX_BOOL IsGetting() const { return !m_bIsSetting; }
99
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700100 void operator<<(int);
101 void operator>>(int&) const;
102 void operator<<(bool);
103 void operator>>(bool&) const;
104 void operator<<(double);
105 void operator>>(double&) const;
106 void operator<<(CJS_Object* pObj);
107 void operator>>(CJS_Object*& ppObj) const;
108 void operator<<(CJS_Document* pJsDoc);
109 void operator>>(CJS_Document*& ppJsDoc) const;
110 void operator<<(CFX_ByteString);
111 void operator>>(CFX_ByteString&) const;
112 void operator<<(CFX_WideString);
113 void operator>>(CFX_WideString&) const;
114 void operator<<(const FX_WCHAR* c_string);
Tom Sepez808a99e2015-09-10 12:28:37 -0700115 void operator<<(v8::Local<v8::Object>);
116 void operator>>(v8::Local<v8::Object>&) const;
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700117 void operator>>(CJS_Array& array) const;
118 void operator<<(CJS_Array& array);
119 void operator<<(CJS_Date& date);
120 void operator>>(CJS_Date& date) const;
121 operator v8::Local<v8::Value>() const;
122 void StartSetting();
123 void StartGetting();
124
125 private:
126 FX_BOOL m_bIsSetting;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700127};
128
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700129class CJS_Array {
130 public:
Tom Sepez67fd5df2015-10-08 12:24:19 -0700131 CJS_Array(CJS_Runtime* pRuntime);
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700132 virtual ~CJS_Array();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700133
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700134 void Attach(v8::Local<v8::Array> pArray);
135 void GetElement(unsigned index, CJS_Value& value);
136 void SetElement(unsigned index, CJS_Value value);
137 int GetLength();
138 FX_BOOL IsAttached();
139 operator v8::Local<v8::Array>();
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700140
Tom Sepez67fd5df2015-10-08 12:24:19 -0700141 CJS_Runtime* GetJSRuntime() const { return m_pJSRuntime; }
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700142
143 private:
144 v8::Local<v8::Array> m_pArray;
Tom Sepez67fd5df2015-10-08 12:24:19 -0700145 CJS_Runtime* m_pJSRuntime;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700146};
147
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700148class CJS_Date {
149 friend class CJS_Value;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700150
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700151 public:
Tom Sepez67fd5df2015-10-08 12:24:19 -0700152 CJS_Date(CJS_Runtime* pRuntime);
153 CJS_Date(CJS_Runtime* pRuntime, double dMsec_time);
154 CJS_Date(CJS_Runtime* pRuntime,
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700155 int year,
156 int mon,
157 int day,
158 int hour,
159 int min,
160 int sec);
161 virtual ~CJS_Date();
162 void Attach(v8::Local<v8::Value> pDate);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700163
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700164 int GetYear();
165 void SetYear(int iYear);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700166
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700167 int GetMonth();
168 void SetMonth(int iMonth);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700169
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700170 int GetDay();
171 void SetDay(int iDay);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700172
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700173 int GetHours();
174 void SetHours(int iHours);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700175
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700176 int GetMinutes();
177 void SetMinutes(int minutes);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700178
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700179 int GetSeconds();
180 void SetSeconds(int seconds);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700181
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700182 operator v8::Local<v8::Value>();
183 operator double() const;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700184
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700185 CFX_WideString ToString() const;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700186
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700187 static double
188 MakeDate(int year, int mon, int mday, int hour, int min, int sec, int ms);
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700189
Nico Weber9d8ec5a2015-08-04 13:00:21 -0700190 FX_BOOL IsValidDate();
191
192 protected:
193 v8::Local<v8::Value> m_pDate;
Tom Sepez67fd5df2015-10-08 12:24:19 -0700194 CJS_Runtime* m_pJSRuntime;
John Abd-El-Malek3f3b45c2014-05-23 17:28:10 -0700195};
196
Tom Sepez39bfe122015-09-17 15:25:23 -0700197double JS_GetDateTime();
198int JS_GetYearFromTime(double dt);
199int JS_GetMonthFromTime(double dt);
200int JS_GetDayFromTime(double dt);
201int JS_GetHourFromTime(double dt);
202int JS_GetMinFromTime(double dt);
203int JS_GetSecFromTime(double dt);
204double JS_DateParse(const wchar_t* string);
205double JS_MakeDay(int nYear, int nMonth, int nDay);
206double JS_MakeTime(int nHour, int nMin, int nSec, int nMs);
207double JS_MakeDate(double day, double time);
208bool JS_PortIsNan(double d);
209double JS_LocalTime(double d);
210
Tom Sepez37458412015-10-06 11:33:46 -0700211#endif // FPDFSDK_SRC_JAVASCRIPT_JS_VALUE_H_