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