1*3ac0a46fSAndroid Build Coastguard Worker // Copyright 2020 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/fxv8.h"
8*3ac0a46fSAndroid Build Coastguard Worker
9*3ac0a46fSAndroid Build Coastguard Worker #include "third_party/base/numerics/safe_conversions.h"
10*3ac0a46fSAndroid Build Coastguard Worker #include "v8/include/v8-container.h"
11*3ac0a46fSAndroid Build Coastguard Worker #include "v8/include/v8-date.h"
12*3ac0a46fSAndroid Build Coastguard Worker #include "v8/include/v8-exception.h"
13*3ac0a46fSAndroid Build Coastguard Worker #include "v8/include/v8-isolate.h"
14*3ac0a46fSAndroid Build Coastguard Worker #include "v8/include/v8-primitive.h"
15*3ac0a46fSAndroid Build Coastguard Worker #include "v8/include/v8-value.h"
16*3ac0a46fSAndroid Build Coastguard Worker
17*3ac0a46fSAndroid Build Coastguard Worker namespace fxv8 {
18*3ac0a46fSAndroid Build Coastguard Worker
IsUndefined(v8::Local<v8::Value> value)19*3ac0a46fSAndroid Build Coastguard Worker bool IsUndefined(v8::Local<v8::Value> value) {
20*3ac0a46fSAndroid Build Coastguard Worker return !value.IsEmpty() && value->IsUndefined();
21*3ac0a46fSAndroid Build Coastguard Worker }
22*3ac0a46fSAndroid Build Coastguard Worker
IsNull(v8::Local<v8::Value> value)23*3ac0a46fSAndroid Build Coastguard Worker bool IsNull(v8::Local<v8::Value> value) {
24*3ac0a46fSAndroid Build Coastguard Worker return !value.IsEmpty() && value->IsNull();
25*3ac0a46fSAndroid Build Coastguard Worker }
26*3ac0a46fSAndroid Build Coastguard Worker
IsBoolean(v8::Local<v8::Value> value)27*3ac0a46fSAndroid Build Coastguard Worker bool IsBoolean(v8::Local<v8::Value> value) {
28*3ac0a46fSAndroid Build Coastguard Worker return !value.IsEmpty() && value->IsBoolean();
29*3ac0a46fSAndroid Build Coastguard Worker }
30*3ac0a46fSAndroid Build Coastguard Worker
IsString(v8::Local<v8::Value> value)31*3ac0a46fSAndroid Build Coastguard Worker bool IsString(v8::Local<v8::Value> value) {
32*3ac0a46fSAndroid Build Coastguard Worker return !value.IsEmpty() && value->IsString();
33*3ac0a46fSAndroid Build Coastguard Worker }
34*3ac0a46fSAndroid Build Coastguard Worker
IsNumber(v8::Local<v8::Value> value)35*3ac0a46fSAndroid Build Coastguard Worker bool IsNumber(v8::Local<v8::Value> value) {
36*3ac0a46fSAndroid Build Coastguard Worker return !value.IsEmpty() && value->IsNumber();
37*3ac0a46fSAndroid Build Coastguard Worker }
38*3ac0a46fSAndroid Build Coastguard Worker
IsInteger(v8::Local<v8::Value> value)39*3ac0a46fSAndroid Build Coastguard Worker bool IsInteger(v8::Local<v8::Value> value) {
40*3ac0a46fSAndroid Build Coastguard Worker return !value.IsEmpty() && value->IsInt32();
41*3ac0a46fSAndroid Build Coastguard Worker }
42*3ac0a46fSAndroid Build Coastguard Worker
IsObject(v8::Local<v8::Value> value)43*3ac0a46fSAndroid Build Coastguard Worker bool IsObject(v8::Local<v8::Value> value) {
44*3ac0a46fSAndroid Build Coastguard Worker return !value.IsEmpty() && value->IsObject();
45*3ac0a46fSAndroid Build Coastguard Worker }
46*3ac0a46fSAndroid Build Coastguard Worker
IsArray(v8::Local<v8::Value> value)47*3ac0a46fSAndroid Build Coastguard Worker bool IsArray(v8::Local<v8::Value> value) {
48*3ac0a46fSAndroid Build Coastguard Worker return !value.IsEmpty() && value->IsArray();
49*3ac0a46fSAndroid Build Coastguard Worker }
50*3ac0a46fSAndroid Build Coastguard Worker
IsDate(v8::Local<v8::Value> value)51*3ac0a46fSAndroid Build Coastguard Worker bool IsDate(v8::Local<v8::Value> value) {
52*3ac0a46fSAndroid Build Coastguard Worker return !value.IsEmpty() && value->IsDate();
53*3ac0a46fSAndroid Build Coastguard Worker }
54*3ac0a46fSAndroid Build Coastguard Worker
IsFunction(v8::Local<v8::Value> value)55*3ac0a46fSAndroid Build Coastguard Worker bool IsFunction(v8::Local<v8::Value> value) {
56*3ac0a46fSAndroid Build Coastguard Worker return !value.IsEmpty() && value->IsFunction();
57*3ac0a46fSAndroid Build Coastguard Worker }
58*3ac0a46fSAndroid Build Coastguard Worker
NewNullHelper(v8::Isolate * pIsolate)59*3ac0a46fSAndroid Build Coastguard Worker v8::Local<v8::Value> NewNullHelper(v8::Isolate* pIsolate) {
60*3ac0a46fSAndroid Build Coastguard Worker return v8::Null(pIsolate);
61*3ac0a46fSAndroid Build Coastguard Worker }
62*3ac0a46fSAndroid Build Coastguard Worker
NewUndefinedHelper(v8::Isolate * pIsolate)63*3ac0a46fSAndroid Build Coastguard Worker v8::Local<v8::Value> NewUndefinedHelper(v8::Isolate* pIsolate) {
64*3ac0a46fSAndroid Build Coastguard Worker return v8::Undefined(pIsolate);
65*3ac0a46fSAndroid Build Coastguard Worker }
66*3ac0a46fSAndroid Build Coastguard Worker
NewNumberHelper(v8::Isolate * pIsolate,int number)67*3ac0a46fSAndroid Build Coastguard Worker v8::Local<v8::Number> NewNumberHelper(v8::Isolate* pIsolate, int number) {
68*3ac0a46fSAndroid Build Coastguard Worker return v8::Int32::New(pIsolate, number);
69*3ac0a46fSAndroid Build Coastguard Worker }
70*3ac0a46fSAndroid Build Coastguard Worker
NewNumberHelper(v8::Isolate * pIsolate,double number)71*3ac0a46fSAndroid Build Coastguard Worker v8::Local<v8::Number> NewNumberHelper(v8::Isolate* pIsolate, double number) {
72*3ac0a46fSAndroid Build Coastguard Worker return v8::Number::New(pIsolate, number);
73*3ac0a46fSAndroid Build Coastguard Worker }
74*3ac0a46fSAndroid Build Coastguard Worker
NewNumberHelper(v8::Isolate * pIsolate,float number)75*3ac0a46fSAndroid Build Coastguard Worker v8::Local<v8::Number> NewNumberHelper(v8::Isolate* pIsolate, float number) {
76*3ac0a46fSAndroid Build Coastguard Worker return v8::Number::New(pIsolate, number);
77*3ac0a46fSAndroid Build Coastguard Worker }
78*3ac0a46fSAndroid Build Coastguard Worker
NewBooleanHelper(v8::Isolate * pIsolate,bool b)79*3ac0a46fSAndroid Build Coastguard Worker v8::Local<v8::Boolean> NewBooleanHelper(v8::Isolate* pIsolate, bool b) {
80*3ac0a46fSAndroid Build Coastguard Worker return v8::Boolean::New(pIsolate, b);
81*3ac0a46fSAndroid Build Coastguard Worker }
82*3ac0a46fSAndroid Build Coastguard Worker
NewStringHelper(v8::Isolate * pIsolate,ByteStringView str)83*3ac0a46fSAndroid Build Coastguard Worker v8::Local<v8::String> NewStringHelper(v8::Isolate* pIsolate,
84*3ac0a46fSAndroid Build Coastguard Worker ByteStringView str) {
85*3ac0a46fSAndroid Build Coastguard Worker return v8::String::NewFromUtf8(
86*3ac0a46fSAndroid Build Coastguard Worker pIsolate, str.unterminated_c_str(), v8::NewStringType::kNormal,
87*3ac0a46fSAndroid Build Coastguard Worker pdfium::base::checked_cast<int>(str.GetLength()))
88*3ac0a46fSAndroid Build Coastguard Worker .ToLocalChecked();
89*3ac0a46fSAndroid Build Coastguard Worker }
90*3ac0a46fSAndroid Build Coastguard Worker
NewStringHelper(v8::Isolate * pIsolate,WideStringView str)91*3ac0a46fSAndroid Build Coastguard Worker v8::Local<v8::String> NewStringHelper(v8::Isolate* pIsolate,
92*3ac0a46fSAndroid Build Coastguard Worker WideStringView str) {
93*3ac0a46fSAndroid Build Coastguard Worker return NewStringHelper(pIsolate, FX_UTF8Encode(str).AsStringView());
94*3ac0a46fSAndroid Build Coastguard Worker }
95*3ac0a46fSAndroid Build Coastguard Worker
NewArrayHelper(v8::Isolate * pIsolate)96*3ac0a46fSAndroid Build Coastguard Worker v8::Local<v8::Array> NewArrayHelper(v8::Isolate* pIsolate) {
97*3ac0a46fSAndroid Build Coastguard Worker return v8::Array::New(pIsolate);
98*3ac0a46fSAndroid Build Coastguard Worker }
99*3ac0a46fSAndroid Build Coastguard Worker
NewArrayHelper(v8::Isolate * pIsolate,pdfium::span<v8::Local<v8::Value>> values)100*3ac0a46fSAndroid Build Coastguard Worker v8::Local<v8::Array> NewArrayHelper(v8::Isolate* pIsolate,
101*3ac0a46fSAndroid Build Coastguard Worker pdfium::span<v8::Local<v8::Value>> values) {
102*3ac0a46fSAndroid Build Coastguard Worker v8::Local<v8::Array> result = NewArrayHelper(pIsolate);
103*3ac0a46fSAndroid Build Coastguard Worker for (size_t i = 0; i < values.size(); ++i) {
104*3ac0a46fSAndroid Build Coastguard Worker fxv8::ReentrantPutArrayElementHelper(
105*3ac0a46fSAndroid Build Coastguard Worker pIsolate, result, i,
106*3ac0a46fSAndroid Build Coastguard Worker values[i].IsEmpty() ? fxv8::NewUndefinedHelper(pIsolate) : values[i]);
107*3ac0a46fSAndroid Build Coastguard Worker }
108*3ac0a46fSAndroid Build Coastguard Worker return result;
109*3ac0a46fSAndroid Build Coastguard Worker }
110*3ac0a46fSAndroid Build Coastguard Worker
NewObjectHelper(v8::Isolate * pIsolate)111*3ac0a46fSAndroid Build Coastguard Worker v8::Local<v8::Object> NewObjectHelper(v8::Isolate* pIsolate) {
112*3ac0a46fSAndroid Build Coastguard Worker return v8::Object::New(pIsolate);
113*3ac0a46fSAndroid Build Coastguard Worker }
114*3ac0a46fSAndroid Build Coastguard Worker
NewDateHelper(v8::Isolate * pIsolate,double d)115*3ac0a46fSAndroid Build Coastguard Worker v8::Local<v8::Date> NewDateHelper(v8::Isolate* pIsolate, double d) {
116*3ac0a46fSAndroid Build Coastguard Worker return v8::Date::New(pIsolate->GetCurrentContext(), d)
117*3ac0a46fSAndroid Build Coastguard Worker .ToLocalChecked()
118*3ac0a46fSAndroid Build Coastguard Worker .As<v8::Date>();
119*3ac0a46fSAndroid Build Coastguard Worker }
120*3ac0a46fSAndroid Build Coastguard Worker
ToWideString(v8::Isolate * pIsolate,v8::Local<v8::String> pValue)121*3ac0a46fSAndroid Build Coastguard Worker WideString ToWideString(v8::Isolate* pIsolate, v8::Local<v8::String> pValue) {
122*3ac0a46fSAndroid Build Coastguard Worker v8::String::Utf8Value s(pIsolate, pValue);
123*3ac0a46fSAndroid Build Coastguard Worker return WideString::FromUTF8(ByteStringView(*s, s.length()));
124*3ac0a46fSAndroid Build Coastguard Worker }
125*3ac0a46fSAndroid Build Coastguard Worker
ToByteString(v8::Isolate * pIsolate,v8::Local<v8::String> pValue)126*3ac0a46fSAndroid Build Coastguard Worker ByteString ToByteString(v8::Isolate* pIsolate, v8::Local<v8::String> pValue) {
127*3ac0a46fSAndroid Build Coastguard Worker v8::String::Utf8Value s(pIsolate, pValue);
128*3ac0a46fSAndroid Build Coastguard Worker return ByteString(*s, s.length());
129*3ac0a46fSAndroid Build Coastguard Worker }
130*3ac0a46fSAndroid Build Coastguard Worker
ReentrantToInt32Helper(v8::Isolate * pIsolate,v8::Local<v8::Value> pValue)131*3ac0a46fSAndroid Build Coastguard Worker int ReentrantToInt32Helper(v8::Isolate* pIsolate, v8::Local<v8::Value> pValue) {
132*3ac0a46fSAndroid Build Coastguard Worker if (pValue.IsEmpty())
133*3ac0a46fSAndroid Build Coastguard Worker return 0;
134*3ac0a46fSAndroid Build Coastguard Worker v8::TryCatch squash_exceptions(pIsolate);
135*3ac0a46fSAndroid Build Coastguard Worker return pValue->Int32Value(pIsolate->GetCurrentContext()).FromMaybe(0);
136*3ac0a46fSAndroid Build Coastguard Worker }
137*3ac0a46fSAndroid Build Coastguard Worker
ReentrantToBooleanHelper(v8::Isolate * pIsolate,v8::Local<v8::Value> pValue)138*3ac0a46fSAndroid Build Coastguard Worker bool ReentrantToBooleanHelper(v8::Isolate* pIsolate,
139*3ac0a46fSAndroid Build Coastguard Worker v8::Local<v8::Value> pValue) {
140*3ac0a46fSAndroid Build Coastguard Worker if (pValue.IsEmpty())
141*3ac0a46fSAndroid Build Coastguard Worker return false;
142*3ac0a46fSAndroid Build Coastguard Worker v8::TryCatch squash_exceptions(pIsolate);
143*3ac0a46fSAndroid Build Coastguard Worker return pValue->BooleanValue(pIsolate);
144*3ac0a46fSAndroid Build Coastguard Worker }
145*3ac0a46fSAndroid Build Coastguard Worker
ReentrantToFloatHelper(v8::Isolate * pIsolate,v8::Local<v8::Value> pValue)146*3ac0a46fSAndroid Build Coastguard Worker float ReentrantToFloatHelper(v8::Isolate* pIsolate,
147*3ac0a46fSAndroid Build Coastguard Worker v8::Local<v8::Value> pValue) {
148*3ac0a46fSAndroid Build Coastguard Worker return static_cast<float>(ReentrantToDoubleHelper(pIsolate, pValue));
149*3ac0a46fSAndroid Build Coastguard Worker }
150*3ac0a46fSAndroid Build Coastguard Worker
ReentrantToDoubleHelper(v8::Isolate * pIsolate,v8::Local<v8::Value> pValue)151*3ac0a46fSAndroid Build Coastguard Worker double ReentrantToDoubleHelper(v8::Isolate* pIsolate,
152*3ac0a46fSAndroid Build Coastguard Worker v8::Local<v8::Value> pValue) {
153*3ac0a46fSAndroid Build Coastguard Worker if (pValue.IsEmpty())
154*3ac0a46fSAndroid Build Coastguard Worker return 0.0;
155*3ac0a46fSAndroid Build Coastguard Worker v8::TryCatch squash_exceptions(pIsolate);
156*3ac0a46fSAndroid Build Coastguard Worker return pValue->NumberValue(pIsolate->GetCurrentContext()).FromMaybe(0.0);
157*3ac0a46fSAndroid Build Coastguard Worker }
158*3ac0a46fSAndroid Build Coastguard Worker
ReentrantToWideStringHelper(v8::Isolate * pIsolate,v8::Local<v8::Value> pValue)159*3ac0a46fSAndroid Build Coastguard Worker WideString ReentrantToWideStringHelper(v8::Isolate* pIsolate,
160*3ac0a46fSAndroid Build Coastguard Worker v8::Local<v8::Value> pValue) {
161*3ac0a46fSAndroid Build Coastguard Worker if (pValue.IsEmpty())
162*3ac0a46fSAndroid Build Coastguard Worker return WideString();
163*3ac0a46fSAndroid Build Coastguard Worker
164*3ac0a46fSAndroid Build Coastguard Worker v8::TryCatch squash_exceptions(pIsolate);
165*3ac0a46fSAndroid Build Coastguard Worker v8::MaybeLocal<v8::String> maybe_string =
166*3ac0a46fSAndroid Build Coastguard Worker pValue->ToString(pIsolate->GetCurrentContext());
167*3ac0a46fSAndroid Build Coastguard Worker if (maybe_string.IsEmpty())
168*3ac0a46fSAndroid Build Coastguard Worker return WideString();
169*3ac0a46fSAndroid Build Coastguard Worker
170*3ac0a46fSAndroid Build Coastguard Worker return ToWideString(pIsolate, maybe_string.ToLocalChecked());
171*3ac0a46fSAndroid Build Coastguard Worker }
172*3ac0a46fSAndroid Build Coastguard Worker
ReentrantToByteStringHelper(v8::Isolate * pIsolate,v8::Local<v8::Value> pValue)173*3ac0a46fSAndroid Build Coastguard Worker ByteString ReentrantToByteStringHelper(v8::Isolate* pIsolate,
174*3ac0a46fSAndroid Build Coastguard Worker v8::Local<v8::Value> pValue) {
175*3ac0a46fSAndroid Build Coastguard Worker if (pValue.IsEmpty())
176*3ac0a46fSAndroid Build Coastguard Worker return ByteString();
177*3ac0a46fSAndroid Build Coastguard Worker
178*3ac0a46fSAndroid Build Coastguard Worker v8::TryCatch squash_exceptions(pIsolate);
179*3ac0a46fSAndroid Build Coastguard Worker v8::MaybeLocal<v8::String> maybe_string =
180*3ac0a46fSAndroid Build Coastguard Worker pValue->ToString(pIsolate->GetCurrentContext());
181*3ac0a46fSAndroid Build Coastguard Worker if (maybe_string.IsEmpty())
182*3ac0a46fSAndroid Build Coastguard Worker return ByteString();
183*3ac0a46fSAndroid Build Coastguard Worker
184*3ac0a46fSAndroid Build Coastguard Worker return ToByteString(pIsolate, maybe_string.ToLocalChecked());
185*3ac0a46fSAndroid Build Coastguard Worker }
186*3ac0a46fSAndroid Build Coastguard Worker
ReentrantToObjectHelper(v8::Isolate * pIsolate,v8::Local<v8::Value> pValue)187*3ac0a46fSAndroid Build Coastguard Worker v8::Local<v8::Object> ReentrantToObjectHelper(v8::Isolate* pIsolate,
188*3ac0a46fSAndroid Build Coastguard Worker v8::Local<v8::Value> pValue) {
189*3ac0a46fSAndroid Build Coastguard Worker if (!fxv8::IsObject(pValue))
190*3ac0a46fSAndroid Build Coastguard Worker return v8::Local<v8::Object>();
191*3ac0a46fSAndroid Build Coastguard Worker
192*3ac0a46fSAndroid Build Coastguard Worker v8::TryCatch squash_exceptions(pIsolate);
193*3ac0a46fSAndroid Build Coastguard Worker v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
194*3ac0a46fSAndroid Build Coastguard Worker return pValue->ToObject(context).ToLocalChecked();
195*3ac0a46fSAndroid Build Coastguard Worker }
196*3ac0a46fSAndroid Build Coastguard Worker
ReentrantToArrayHelper(v8::Isolate * pIsolate,v8::Local<v8::Value> pValue)197*3ac0a46fSAndroid Build Coastguard Worker v8::Local<v8::Array> ReentrantToArrayHelper(v8::Isolate* pIsolate,
198*3ac0a46fSAndroid Build Coastguard Worker v8::Local<v8::Value> pValue) {
199*3ac0a46fSAndroid Build Coastguard Worker if (!fxv8::IsArray(pValue))
200*3ac0a46fSAndroid Build Coastguard Worker return v8::Local<v8::Array>();
201*3ac0a46fSAndroid Build Coastguard Worker
202*3ac0a46fSAndroid Build Coastguard Worker v8::TryCatch squash_exceptions(pIsolate);
203*3ac0a46fSAndroid Build Coastguard Worker v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
204*3ac0a46fSAndroid Build Coastguard Worker return v8::Local<v8::Array>::Cast(pValue->ToObject(context).ToLocalChecked());
205*3ac0a46fSAndroid Build Coastguard Worker }
206*3ac0a46fSAndroid Build Coastguard Worker
ReentrantGetObjectPropertyHelper(v8::Isolate * pIsolate,v8::Local<v8::Object> pObj,ByteStringView bsUTF8PropertyName)207*3ac0a46fSAndroid Build Coastguard Worker v8::Local<v8::Value> ReentrantGetObjectPropertyHelper(
208*3ac0a46fSAndroid Build Coastguard Worker v8::Isolate* pIsolate,
209*3ac0a46fSAndroid Build Coastguard Worker v8::Local<v8::Object> pObj,
210*3ac0a46fSAndroid Build Coastguard Worker ByteStringView bsUTF8PropertyName) {
211*3ac0a46fSAndroid Build Coastguard Worker if (pObj.IsEmpty())
212*3ac0a46fSAndroid Build Coastguard Worker return v8::Local<v8::Value>();
213*3ac0a46fSAndroid Build Coastguard Worker
214*3ac0a46fSAndroid Build Coastguard Worker v8::TryCatch squash_exceptions(pIsolate);
215*3ac0a46fSAndroid Build Coastguard Worker v8::Local<v8::Value> val;
216*3ac0a46fSAndroid Build Coastguard Worker if (!pObj->Get(pIsolate->GetCurrentContext(),
217*3ac0a46fSAndroid Build Coastguard Worker NewStringHelper(pIsolate, bsUTF8PropertyName))
218*3ac0a46fSAndroid Build Coastguard Worker .ToLocal(&val)) {
219*3ac0a46fSAndroid Build Coastguard Worker return v8::Local<v8::Value>();
220*3ac0a46fSAndroid Build Coastguard Worker }
221*3ac0a46fSAndroid Build Coastguard Worker return val;
222*3ac0a46fSAndroid Build Coastguard Worker }
223*3ac0a46fSAndroid Build Coastguard Worker
ReentrantGetObjectPropertyNamesHelper(v8::Isolate * pIsolate,v8::Local<v8::Object> pObj)224*3ac0a46fSAndroid Build Coastguard Worker std::vector<WideString> ReentrantGetObjectPropertyNamesHelper(
225*3ac0a46fSAndroid Build Coastguard Worker v8::Isolate* pIsolate,
226*3ac0a46fSAndroid Build Coastguard Worker v8::Local<v8::Object> pObj) {
227*3ac0a46fSAndroid Build Coastguard Worker if (pObj.IsEmpty())
228*3ac0a46fSAndroid Build Coastguard Worker return std::vector<WideString>();
229*3ac0a46fSAndroid Build Coastguard Worker
230*3ac0a46fSAndroid Build Coastguard Worker v8::TryCatch squash_exceptions(pIsolate);
231*3ac0a46fSAndroid Build Coastguard Worker v8::Local<v8::Array> val;
232*3ac0a46fSAndroid Build Coastguard Worker v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
233*3ac0a46fSAndroid Build Coastguard Worker if (!pObj->GetPropertyNames(context).ToLocal(&val))
234*3ac0a46fSAndroid Build Coastguard Worker return std::vector<WideString>();
235*3ac0a46fSAndroid Build Coastguard Worker
236*3ac0a46fSAndroid Build Coastguard Worker std::vector<WideString> result;
237*3ac0a46fSAndroid Build Coastguard Worker for (uint32_t i = 0; i < val->Length(); ++i) {
238*3ac0a46fSAndroid Build Coastguard Worker result.push_back(ReentrantToWideStringHelper(
239*3ac0a46fSAndroid Build Coastguard Worker pIsolate, val->Get(context, i).ToLocalChecked()));
240*3ac0a46fSAndroid Build Coastguard Worker }
241*3ac0a46fSAndroid Build Coastguard Worker return result;
242*3ac0a46fSAndroid Build Coastguard Worker }
243*3ac0a46fSAndroid Build Coastguard Worker
ReentrantHasObjectOwnPropertyHelper(v8::Isolate * pIsolate,v8::Local<v8::Object> pObj,ByteStringView bsUTF8PropertyName)244*3ac0a46fSAndroid Build Coastguard Worker bool ReentrantHasObjectOwnPropertyHelper(v8::Isolate* pIsolate,
245*3ac0a46fSAndroid Build Coastguard Worker v8::Local<v8::Object> pObj,
246*3ac0a46fSAndroid Build Coastguard Worker ByteStringView bsUTF8PropertyName) {
247*3ac0a46fSAndroid Build Coastguard Worker if (pObj.IsEmpty())
248*3ac0a46fSAndroid Build Coastguard Worker return false;
249*3ac0a46fSAndroid Build Coastguard Worker
250*3ac0a46fSAndroid Build Coastguard Worker v8::TryCatch squash_exceptions(pIsolate);
251*3ac0a46fSAndroid Build Coastguard Worker v8::Local<v8::Context> pContext = pIsolate->GetCurrentContext();
252*3ac0a46fSAndroid Build Coastguard Worker v8::Local<v8::String> hKey =
253*3ac0a46fSAndroid Build Coastguard Worker fxv8::NewStringHelper(pIsolate, bsUTF8PropertyName);
254*3ac0a46fSAndroid Build Coastguard Worker return pObj->HasRealNamedProperty(pContext, hKey).FromJust();
255*3ac0a46fSAndroid Build Coastguard Worker }
256*3ac0a46fSAndroid Build Coastguard Worker
ReentrantSetObjectOwnPropertyHelper(v8::Isolate * pIsolate,v8::Local<v8::Object> pObj,ByteStringView bsUTF8PropertyName,v8::Local<v8::Value> pValue)257*3ac0a46fSAndroid Build Coastguard Worker bool ReentrantSetObjectOwnPropertyHelper(v8::Isolate* pIsolate,
258*3ac0a46fSAndroid Build Coastguard Worker v8::Local<v8::Object> pObj,
259*3ac0a46fSAndroid Build Coastguard Worker ByteStringView bsUTF8PropertyName,
260*3ac0a46fSAndroid Build Coastguard Worker v8::Local<v8::Value> pValue) {
261*3ac0a46fSAndroid Build Coastguard Worker if (pObj.IsEmpty() || pValue.IsEmpty())
262*3ac0a46fSAndroid Build Coastguard Worker return false;
263*3ac0a46fSAndroid Build Coastguard Worker
264*3ac0a46fSAndroid Build Coastguard Worker v8::TryCatch squash_exceptions(pIsolate);
265*3ac0a46fSAndroid Build Coastguard Worker v8::Local<v8::String> name = NewStringHelper(pIsolate, bsUTF8PropertyName);
266*3ac0a46fSAndroid Build Coastguard Worker return pObj->DefineOwnProperty(pIsolate->GetCurrentContext(), name, pValue)
267*3ac0a46fSAndroid Build Coastguard Worker .FromMaybe(false);
268*3ac0a46fSAndroid Build Coastguard Worker }
269*3ac0a46fSAndroid Build Coastguard Worker
ReentrantPutObjectPropertyHelper(v8::Isolate * pIsolate,v8::Local<v8::Object> pObj,ByteStringView bsUTF8PropertyName,v8::Local<v8::Value> pPut)270*3ac0a46fSAndroid Build Coastguard Worker bool ReentrantPutObjectPropertyHelper(v8::Isolate* pIsolate,
271*3ac0a46fSAndroid Build Coastguard Worker v8::Local<v8::Object> pObj,
272*3ac0a46fSAndroid Build Coastguard Worker ByteStringView bsUTF8PropertyName,
273*3ac0a46fSAndroid Build Coastguard Worker v8::Local<v8::Value> pPut) {
274*3ac0a46fSAndroid Build Coastguard Worker if (pObj.IsEmpty() || pPut.IsEmpty())
275*3ac0a46fSAndroid Build Coastguard Worker return false;
276*3ac0a46fSAndroid Build Coastguard Worker
277*3ac0a46fSAndroid Build Coastguard Worker v8::TryCatch squash_exceptions(pIsolate);
278*3ac0a46fSAndroid Build Coastguard Worker v8::Local<v8::String> name = NewStringHelper(pIsolate, bsUTF8PropertyName);
279*3ac0a46fSAndroid Build Coastguard Worker v8::Maybe<bool> result = pObj->Set(pIsolate->GetCurrentContext(), name, pPut);
280*3ac0a46fSAndroid Build Coastguard Worker return result.IsJust() && result.FromJust();
281*3ac0a46fSAndroid Build Coastguard Worker }
282*3ac0a46fSAndroid Build Coastguard Worker
ReentrantDeleteObjectPropertyHelper(v8::Isolate * pIsolate,v8::Local<v8::Object> pObj,ByteStringView bsUTF8PropertyName)283*3ac0a46fSAndroid Build Coastguard Worker void ReentrantDeleteObjectPropertyHelper(v8::Isolate* pIsolate,
284*3ac0a46fSAndroid Build Coastguard Worker v8::Local<v8::Object> pObj,
285*3ac0a46fSAndroid Build Coastguard Worker ByteStringView bsUTF8PropertyName) {
286*3ac0a46fSAndroid Build Coastguard Worker v8::TryCatch squash_exceptions(pIsolate);
287*3ac0a46fSAndroid Build Coastguard Worker pObj->Delete(pIsolate->GetCurrentContext(),
288*3ac0a46fSAndroid Build Coastguard Worker fxv8::NewStringHelper(pIsolate, bsUTF8PropertyName))
289*3ac0a46fSAndroid Build Coastguard Worker .FromJust();
290*3ac0a46fSAndroid Build Coastguard Worker }
291*3ac0a46fSAndroid Build Coastguard Worker
ReentrantPutArrayElementHelper(v8::Isolate * pIsolate,v8::Local<v8::Array> pArray,size_t index,v8::Local<v8::Value> pValue)292*3ac0a46fSAndroid Build Coastguard Worker bool ReentrantPutArrayElementHelper(v8::Isolate* pIsolate,
293*3ac0a46fSAndroid Build Coastguard Worker v8::Local<v8::Array> pArray,
294*3ac0a46fSAndroid Build Coastguard Worker size_t index,
295*3ac0a46fSAndroid Build Coastguard Worker v8::Local<v8::Value> pValue) {
296*3ac0a46fSAndroid Build Coastguard Worker if (pArray.IsEmpty())
297*3ac0a46fSAndroid Build Coastguard Worker return false;
298*3ac0a46fSAndroid Build Coastguard Worker
299*3ac0a46fSAndroid Build Coastguard Worker v8::TryCatch squash_exceptions(pIsolate);
300*3ac0a46fSAndroid Build Coastguard Worker v8::Maybe<bool> result =
301*3ac0a46fSAndroid Build Coastguard Worker pArray->Set(pIsolate->GetCurrentContext(),
302*3ac0a46fSAndroid Build Coastguard Worker pdfium::base::checked_cast<uint32_t>(index), pValue);
303*3ac0a46fSAndroid Build Coastguard Worker return result.IsJust() && result.FromJust();
304*3ac0a46fSAndroid Build Coastguard Worker }
305*3ac0a46fSAndroid Build Coastguard Worker
ReentrantGetArrayElementHelper(v8::Isolate * pIsolate,v8::Local<v8::Array> pArray,size_t index)306*3ac0a46fSAndroid Build Coastguard Worker v8::Local<v8::Value> ReentrantGetArrayElementHelper(v8::Isolate* pIsolate,
307*3ac0a46fSAndroid Build Coastguard Worker v8::Local<v8::Array> pArray,
308*3ac0a46fSAndroid Build Coastguard Worker size_t index) {
309*3ac0a46fSAndroid Build Coastguard Worker if (pArray.IsEmpty())
310*3ac0a46fSAndroid Build Coastguard Worker return v8::Local<v8::Value>();
311*3ac0a46fSAndroid Build Coastguard Worker
312*3ac0a46fSAndroid Build Coastguard Worker v8::TryCatch squash_exceptions(pIsolate);
313*3ac0a46fSAndroid Build Coastguard Worker v8::Local<v8::Value> val;
314*3ac0a46fSAndroid Build Coastguard Worker if (!pArray
315*3ac0a46fSAndroid Build Coastguard Worker ->Get(pIsolate->GetCurrentContext(),
316*3ac0a46fSAndroid Build Coastguard Worker pdfium::base::checked_cast<uint32_t>(index))
317*3ac0a46fSAndroid Build Coastguard Worker .ToLocal(&val)) {
318*3ac0a46fSAndroid Build Coastguard Worker return v8::Local<v8::Value>();
319*3ac0a46fSAndroid Build Coastguard Worker }
320*3ac0a46fSAndroid Build Coastguard Worker return val;
321*3ac0a46fSAndroid Build Coastguard Worker }
322*3ac0a46fSAndroid Build Coastguard Worker
GetArrayLengthHelper(v8::Local<v8::Array> pArray)323*3ac0a46fSAndroid Build Coastguard Worker size_t GetArrayLengthHelper(v8::Local<v8::Array> pArray) {
324*3ac0a46fSAndroid Build Coastguard Worker if (pArray.IsEmpty())
325*3ac0a46fSAndroid Build Coastguard Worker return 0;
326*3ac0a46fSAndroid Build Coastguard Worker return pArray->Length();
327*3ac0a46fSAndroid Build Coastguard Worker }
328*3ac0a46fSAndroid Build Coastguard Worker
ThrowExceptionHelper(v8::Isolate * pIsolate,ByteStringView str)329*3ac0a46fSAndroid Build Coastguard Worker void ThrowExceptionHelper(v8::Isolate* pIsolate, ByteStringView str) {
330*3ac0a46fSAndroid Build Coastguard Worker pIsolate->ThrowException(NewStringHelper(pIsolate, str));
331*3ac0a46fSAndroid Build Coastguard Worker }
332*3ac0a46fSAndroid Build Coastguard Worker
ThrowExceptionHelper(v8::Isolate * pIsolate,WideStringView str)333*3ac0a46fSAndroid Build Coastguard Worker void ThrowExceptionHelper(v8::Isolate* pIsolate, WideStringView str) {
334*3ac0a46fSAndroid Build Coastguard Worker pIsolate->ThrowException(NewStringHelper(pIsolate, str));
335*3ac0a46fSAndroid Build Coastguard Worker }
336*3ac0a46fSAndroid Build Coastguard Worker
337*3ac0a46fSAndroid Build Coastguard Worker } // namespace fxv8
338