• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 The PDFium Authors
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 "fxjs/xfa/cfxjse_value.h"
8 
9 #include <math.h>
10 
11 #include "fxjs/fxv8.h"
12 #include "fxjs/xfa/cfxjse_class.h"
13 #include "fxjs/xfa/cfxjse_context.h"
14 #include "fxjs/xfa/cfxjse_isolatetracker.h"
15 #include "third_party/base/check.h"
16 #include "v8/include/v8-container.h"
17 #include "v8/include/v8-exception.h"
18 #include "v8/include/v8-function.h"
19 #include "v8/include/v8-primitive.h"
20 #include "v8/include/v8-script.h"
21 
22 namespace {
23 
ftod(float fNumber)24 double ftod(float fNumber) {
25   static_assert(sizeof(float) == 4, "float of incorrect size");
26 
27   uint32_t nFloatBits = (uint32_t&)fNumber;
28   uint8_t nExponent = (uint8_t)(nFloatBits >> 23);
29   if (nExponent == 0 || nExponent == 255)
30     return fNumber;
31 
32   int8_t nErrExp = nExponent - 150;
33   if (nErrExp >= 0)
34     return fNumber;
35 
36   double dwError = pow(2.0, nErrExp);
37   double dwErrorHalf = dwError / 2;
38   double dNumber = fNumber;
39   double dNumberAbs = fabs(fNumber);
40   double dNumberAbsMin = dNumberAbs - dwErrorHalf;
41   double dNumberAbsMax = dNumberAbs + dwErrorHalf;
42   int32_t iErrPos = 0;
43   if (floor(dNumberAbsMin) == floor(dNumberAbsMax)) {
44     dNumberAbsMin = fmod(dNumberAbsMin, 1.0);
45     dNumberAbsMax = fmod(dNumberAbsMax, 1.0);
46     int32_t iErrPosMin = 1;
47     int32_t iErrPosMax = 38;
48     do {
49       int32_t iMid = (iErrPosMin + iErrPosMax) / 2;
50       double dPow = pow(10.0, iMid);
51       if (floor(dNumberAbsMin * dPow) == floor(dNumberAbsMax * dPow)) {
52         iErrPosMin = iMid + 1;
53       } else {
54         iErrPosMax = iMid;
55       }
56     } while (iErrPosMin < iErrPosMax);
57     iErrPos = iErrPosMax;
58   }
59   double dPow = pow(10.0, iErrPos);
60   return fNumber < 0 ? ceil(dNumber * dPow - 0.5) / dPow
61                      : floor(dNumber * dPow + 0.5) / dPow;
62 }
63 
64 }  // namespace
65 
FXJSE_ThrowMessage(v8::Isolate * pIsolate,ByteStringView utf8Message)66 void FXJSE_ThrowMessage(v8::Isolate* pIsolate, ByteStringView utf8Message) {
67   DCHECK(pIsolate);
68   CFXJSE_ScopeUtil_IsolateHandleRootContext scope(pIsolate);
69   v8::Local<v8::String> hMessage = fxv8::NewStringHelper(pIsolate, utf8Message);
70   v8::Local<v8::Value> hError = v8::Exception::Error(hMessage);
71   pIsolate->ThrowException(hError);
72 }
73 
74 CFXJSE_Value::CFXJSE_Value() = default;
75 
CFXJSE_Value(v8::Isolate * pIsolate,v8::Local<v8::Value> value)76 CFXJSE_Value::CFXJSE_Value(v8::Isolate* pIsolate, v8::Local<v8::Value> value) {
77   ForceSetValue(pIsolate, value);
78 }
79 
80 CFXJSE_Value::~CFXJSE_Value() = default;
81 
ToHostObject(v8::Isolate * pIsolate) const82 CFXJSE_HostObject* CFXJSE_Value::ToHostObject(v8::Isolate* pIsolate) const {
83   CFXJSE_ScopeUtil_IsolateHandleRootContext scope(pIsolate);
84   return CFXJSE_HostObject::FromV8(
85       v8::Local<v8::Value>::New(pIsolate, m_hValue));
86 }
87 
SetHostObject(v8::Isolate * pIsolate,CFXJSE_HostObject * pObject,CFXJSE_Class * pClass)88 void CFXJSE_Value::SetHostObject(v8::Isolate* pIsolate,
89                                  CFXJSE_HostObject* pObject,
90                                  CFXJSE_Class* pClass) {
91   CFXJSE_ScopeUtil_IsolateHandleRootContext scope(pIsolate);
92   m_hValue.Reset(pIsolate, pObject->NewBoundV8Object(
93                                pIsolate, pClass->GetTemplate(pIsolate)));
94 }
95 
SetArray(v8::Isolate * pIsolate,const std::vector<std::unique_ptr<CFXJSE_Value>> & values)96 void CFXJSE_Value::SetArray(
97     v8::Isolate* pIsolate,
98     const std::vector<std::unique_ptr<CFXJSE_Value>>& values) {
99   CFXJSE_ScopeUtil_IsolateHandleRootContext scope(pIsolate);
100   std::vector<v8::Local<v8::Value>> local_values;
101   local_values.reserve(values.size());
102   for (auto& v : values) {
103     if (v->IsEmpty())
104       local_values.push_back(fxv8::NewUndefinedHelper(pIsolate));
105     else
106       local_values.push_back(v->GetValue(pIsolate));
107   }
108   v8::Local<v8::Array> hArrayObject =
109       v8::Array::New(pIsolate, local_values.data(), local_values.size());
110   m_hValue.Reset(pIsolate, hArrayObject);
111 }
112 
SetFloat(v8::Isolate * pIsolate,float fFloat)113 void CFXJSE_Value::SetFloat(v8::Isolate* pIsolate, float fFloat) {
114   CFXJSE_ScopeUtil_IsolateHandle scope(pIsolate);
115   m_hValue.Reset(pIsolate, fxv8::NewNumberHelper(pIsolate, ftod(fFloat)));
116 }
117 
SetObjectProperty(v8::Isolate * pIsolate,ByteStringView szPropName,CFXJSE_Value * pPropValue)118 bool CFXJSE_Value::SetObjectProperty(v8::Isolate* pIsolate,
119                                      ByteStringView szPropName,
120                                      CFXJSE_Value* pPropValue) {
121   if (pPropValue->IsEmpty())
122     return false;
123 
124   CFXJSE_ScopeUtil_IsolateHandleRootContext scope(pIsolate);
125   v8::Local<v8::Value> hObject = GetValue(pIsolate);
126   if (!hObject->IsObject())
127     return false;
128 
129   return fxv8::ReentrantPutObjectPropertyHelper(
130       pIsolate, hObject.As<v8::Object>(), szPropName,
131       pPropValue->GetValue(pIsolate));
132 }
133 
GetObjectProperty(v8::Isolate * pIsolate,ByteStringView szPropName,CFXJSE_Value * pPropValue)134 bool CFXJSE_Value::GetObjectProperty(v8::Isolate* pIsolate,
135                                      ByteStringView szPropName,
136                                      CFXJSE_Value* pPropValue) {
137   CFXJSE_ScopeUtil_IsolateHandleRootContext scope(pIsolate);
138   v8::Local<v8::Value> hObject = GetValue(pIsolate);
139   if (!hObject->IsObject())
140     return false;
141 
142   pPropValue->ForceSetValue(
143       pIsolate, fxv8::ReentrantGetObjectPropertyHelper(
144                     pIsolate, hObject.As<v8::Object>(), szPropName));
145   return true;
146 }
147 
GetObjectPropertyByIdx(v8::Isolate * pIsolate,uint32_t uPropIdx,CFXJSE_Value * pPropValue)148 bool CFXJSE_Value::GetObjectPropertyByIdx(v8::Isolate* pIsolate,
149                                           uint32_t uPropIdx,
150                                           CFXJSE_Value* pPropValue) {
151   CFXJSE_ScopeUtil_IsolateHandleRootContext scope(pIsolate);
152   v8::Local<v8::Value> hObject = GetValue(pIsolate);
153   if (!hObject->IsArray())
154     return false;
155 
156   pPropValue->ForceSetValue(pIsolate,
157                             fxv8::ReentrantGetArrayElementHelper(
158                                 pIsolate, hObject.As<v8::Array>(), uPropIdx));
159   return true;
160 }
161 
DeleteObjectProperty(v8::Isolate * pIsolate,ByteStringView szPropName)162 void CFXJSE_Value::DeleteObjectProperty(v8::Isolate* pIsolate,
163                                         ByteStringView szPropName) {
164   CFXJSE_ScopeUtil_IsolateHandleRootContext scope(pIsolate);
165   v8::Local<v8::Value> hObject = v8::Local<v8::Value>::New(pIsolate, m_hValue);
166   if (hObject->IsObject()) {
167     fxv8::ReentrantDeleteObjectPropertyHelper(
168         pIsolate, hObject.As<v8::Object>(), szPropName);
169   }
170 }
171 
SetObjectOwnProperty(v8::Isolate * pIsolate,ByteStringView szPropName,CFXJSE_Value * pPropValue)172 bool CFXJSE_Value::SetObjectOwnProperty(v8::Isolate* pIsolate,
173                                         ByteStringView szPropName,
174                                         CFXJSE_Value* pPropValue) {
175   CFXJSE_ScopeUtil_IsolateHandleRootContext scope(pIsolate);
176   v8::Local<v8::Value> hObject = v8::Local<v8::Value>::New(pIsolate, m_hValue);
177   if (!hObject->IsObject())
178     return false;
179 
180   v8::Local<v8::Value> pValue =
181       v8::Local<v8::Value>::New(pIsolate, pPropValue->m_hValue);
182   return fxv8::ReentrantSetObjectOwnPropertyHelper(
183       pIsolate, hObject.As<v8::Object>(), szPropName, pValue);
184 }
185 
NewBoundFunction(v8::Isolate * pIsolate,v8::Local<v8::Function> hOldFunction,v8::Local<v8::Object> hNewThis)186 v8::Local<v8::Function> CFXJSE_Value::NewBoundFunction(
187     v8::Isolate* pIsolate,
188     v8::Local<v8::Function> hOldFunction,
189     v8::Local<v8::Object> hNewThis) {
190   DCHECK(!hOldFunction.IsEmpty());
191   DCHECK(!hNewThis.IsEmpty());
192 
193   CFXJSE_ScopeUtil_RootContext scope(pIsolate);
194   v8::Local<v8::Value> rgArgs[2];
195   rgArgs[0] = hOldFunction;
196   rgArgs[1] = hNewThis;
197   v8::Local<v8::String> hBinderFuncSource = fxv8::NewStringHelper(
198       pIsolate, "(function (fn, obj) { return fn.bind(obj); })");
199   v8::Local<v8::Context> hContext = pIsolate->GetCurrentContext();
200   v8::Local<v8::Function> hBinderFunc =
201       v8::Script::Compile(hContext, hBinderFuncSource)
202           .ToLocalChecked()
203           ->Run(hContext)
204           .ToLocalChecked()
205           .As<v8::Function>();
206   v8::Local<v8::Value> hBoundFunction =
207       hBinderFunc->Call(hContext, hContext->Global(), 2, rgArgs)
208           .ToLocalChecked();
209   if (!fxv8::IsFunction(hBoundFunction))
210     return v8::Local<v8::Function>();
211 
212   return hBoundFunction.As<v8::Function>();
213 }
214 
GetValue(v8::Isolate * pIsolate) const215 v8::Local<v8::Value> CFXJSE_Value::GetValue(v8::Isolate* pIsolate) const {
216   return v8::Local<v8::Value>::New(pIsolate, m_hValue);
217 }
218 
IsEmpty() const219 bool CFXJSE_Value::IsEmpty() const {
220   return m_hValue.IsEmpty();
221 }
222 
IsUndefined(v8::Isolate * pIsolate) const223 bool CFXJSE_Value::IsUndefined(v8::Isolate* pIsolate) const {
224   if (IsEmpty())
225     return false;
226 
227   CFXJSE_ScopeUtil_IsolateHandle scope(pIsolate);
228   v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(pIsolate, m_hValue);
229   return hValue->IsUndefined();
230 }
231 
IsNull(v8::Isolate * pIsolate) const232 bool CFXJSE_Value::IsNull(v8::Isolate* pIsolate) const {
233   if (IsEmpty())
234     return false;
235 
236   CFXJSE_ScopeUtil_IsolateHandle scope(pIsolate);
237   v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(pIsolate, m_hValue);
238   return hValue->IsNull();
239 }
240 
IsBoolean(v8::Isolate * pIsolate) const241 bool CFXJSE_Value::IsBoolean(v8::Isolate* pIsolate) const {
242   if (IsEmpty())
243     return false;
244 
245   CFXJSE_ScopeUtil_IsolateHandle scope(pIsolate);
246   v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(pIsolate, m_hValue);
247   return hValue->IsBoolean();
248 }
249 
IsString(v8::Isolate * pIsolate) const250 bool CFXJSE_Value::IsString(v8::Isolate* pIsolate) const {
251   if (IsEmpty())
252     return false;
253 
254   CFXJSE_ScopeUtil_IsolateHandle scope(pIsolate);
255   v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(pIsolate, m_hValue);
256   return hValue->IsString();
257 }
258 
IsNumber(v8::Isolate * pIsolate) const259 bool CFXJSE_Value::IsNumber(v8::Isolate* pIsolate) const {
260   if (IsEmpty())
261     return false;
262 
263   CFXJSE_ScopeUtil_IsolateHandle scope(pIsolate);
264   v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(pIsolate, m_hValue);
265   return hValue->IsNumber();
266 }
267 
IsInteger(v8::Isolate * pIsolate) const268 bool CFXJSE_Value::IsInteger(v8::Isolate* pIsolate) const {
269   if (IsEmpty())
270     return false;
271 
272   CFXJSE_ScopeUtil_IsolateHandle scope(pIsolate);
273   v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(pIsolate, m_hValue);
274   return hValue->IsInt32();
275 }
276 
IsObject(v8::Isolate * pIsolate) const277 bool CFXJSE_Value::IsObject(v8::Isolate* pIsolate) const {
278   if (IsEmpty())
279     return false;
280 
281   CFXJSE_ScopeUtil_IsolateHandle scope(pIsolate);
282   v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(pIsolate, m_hValue);
283   return hValue->IsObject();
284 }
285 
IsArray(v8::Isolate * pIsolate) const286 bool CFXJSE_Value::IsArray(v8::Isolate* pIsolate) const {
287   if (IsEmpty())
288     return false;
289 
290   CFXJSE_ScopeUtil_IsolateHandle scope(pIsolate);
291   v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(pIsolate, m_hValue);
292   return hValue->IsArray();
293 }
294 
IsFunction(v8::Isolate * pIsolate) const295 bool CFXJSE_Value::IsFunction(v8::Isolate* pIsolate) const {
296   if (IsEmpty())
297     return false;
298 
299   CFXJSE_ScopeUtil_IsolateHandle scope(pIsolate);
300   v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(pIsolate, m_hValue);
301   return hValue->IsFunction();
302 }
303 
ToBoolean(v8::Isolate * pIsolate) const304 bool CFXJSE_Value::ToBoolean(v8::Isolate* pIsolate) const {
305   DCHECK(!IsEmpty());
306   CFXJSE_ScopeUtil_IsolateHandleRootContext scope(pIsolate);
307   return fxv8::ReentrantToBooleanHelper(
308       pIsolate, v8::Local<v8::Value>::New(pIsolate, m_hValue));
309 }
310 
ToFloat(v8::Isolate * pIsolate) const311 float CFXJSE_Value::ToFloat(v8::Isolate* pIsolate) const {
312   return static_cast<float>(ToDouble(pIsolate));
313 }
314 
ToDouble(v8::Isolate * pIsolate) const315 double CFXJSE_Value::ToDouble(v8::Isolate* pIsolate) const {
316   DCHECK(!IsEmpty());
317   CFXJSE_ScopeUtil_IsolateHandleRootContext scope(pIsolate);
318   return fxv8::ReentrantToDoubleHelper(
319       pIsolate, v8::Local<v8::Value>::New(pIsolate, m_hValue));
320 }
321 
ToInteger(v8::Isolate * pIsolate) const322 int32_t CFXJSE_Value::ToInteger(v8::Isolate* pIsolate) const {
323   DCHECK(!IsEmpty());
324   CFXJSE_ScopeUtil_IsolateHandleRootContext scope(pIsolate);
325   return fxv8::ReentrantToInt32Helper(
326       pIsolate, v8::Local<v8::Value>::New(pIsolate, m_hValue));
327 }
328 
ToString(v8::Isolate * pIsolate) const329 ByteString CFXJSE_Value::ToString(v8::Isolate* pIsolate) const {
330   DCHECK(!IsEmpty());
331   CFXJSE_ScopeUtil_IsolateHandleRootContext scope(pIsolate);
332   return fxv8::ReentrantToByteStringHelper(
333       pIsolate, v8::Local<v8::Value>::New(pIsolate, m_hValue));
334 }
335 
SetUndefined(v8::Isolate * pIsolate)336 void CFXJSE_Value::SetUndefined(v8::Isolate* pIsolate) {
337   CFXJSE_ScopeUtil_IsolateHandle scope(pIsolate);
338   m_hValue.Reset(pIsolate, fxv8::NewUndefinedHelper(pIsolate));
339 }
340 
SetNull(v8::Isolate * pIsolate)341 void CFXJSE_Value::SetNull(v8::Isolate* pIsolate) {
342   CFXJSE_ScopeUtil_IsolateHandle scope(pIsolate);
343   m_hValue.Reset(pIsolate, fxv8::NewNullHelper(pIsolate));
344 }
345 
SetBoolean(v8::Isolate * pIsolate,bool bBoolean)346 void CFXJSE_Value::SetBoolean(v8::Isolate* pIsolate, bool bBoolean) {
347   CFXJSE_ScopeUtil_IsolateHandle scope(pIsolate);
348   m_hValue.Reset(pIsolate, fxv8::NewBooleanHelper(pIsolate, bBoolean));
349 }
350 
SetInteger(v8::Isolate * pIsolate,int32_t nInteger)351 void CFXJSE_Value::SetInteger(v8::Isolate* pIsolate, int32_t nInteger) {
352   CFXJSE_ScopeUtil_IsolateHandle scope(pIsolate);
353   m_hValue.Reset(pIsolate, fxv8::NewNumberHelper(pIsolate, nInteger));
354 }
355 
SetDouble(v8::Isolate * pIsolate,double dDouble)356 void CFXJSE_Value::SetDouble(v8::Isolate* pIsolate, double dDouble) {
357   CFXJSE_ScopeUtil_IsolateHandle scope(pIsolate);
358   m_hValue.Reset(pIsolate, fxv8::NewNumberHelper(pIsolate, dDouble));
359 }
360 
SetString(v8::Isolate * pIsolate,ByteStringView szString)361 void CFXJSE_Value::SetString(v8::Isolate* pIsolate, ByteStringView szString) {
362   CFXJSE_ScopeUtil_IsolateHandle scope(pIsolate);
363   m_hValue.Reset(pIsolate, fxv8::NewStringHelper(pIsolate, szString));
364 }
365