blob: 4d5804cf40f412f4cb1ac6e65075381752dd8d33 [file] [log] [blame]
Tom Sepez3466e272020-03-18 23:33:35 +00001// Copyright 2020 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.
4
5// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7#include "fxjs/fxv8.h"
8
9namespace fxv8 {
10
Tom Sepez1512c362020-03-19 22:13:52 +000011v8::Local<v8::Value> NewNullHelper(v8::Isolate* pIsolate) {
12 return v8::Null(pIsolate);
13}
14
15v8::Local<v8::Value> NewUndefinedHelper(v8::Isolate* pIsolate) {
16 return v8::Undefined(pIsolate);
17}
18
19v8::Local<v8::Number> NewNumberHelper(v8::Isolate* pIsolate, int number) {
20 return v8::Int32::New(pIsolate, number);
21}
22
23v8::Local<v8::Number> NewNumberHelper(v8::Isolate* pIsolate, double number) {
24 return v8::Number::New(pIsolate, number);
25}
26
27v8::Local<v8::Number> NewNumberHelper(v8::Isolate* pIsolate, float number) {
28 return v8::Number::New(pIsolate, number);
29}
30
31v8::Local<v8::Boolean> NewBooleanHelper(v8::Isolate* pIsolate, bool b) {
32 return v8::Boolean::New(pIsolate, b);
33}
34
Tom Sepez3466e272020-03-18 23:33:35 +000035v8::Local<v8::String> NewStringHelper(v8::Isolate* pIsolate,
36 ByteStringView str) {
37 return v8::String::NewFromUtf8(pIsolate, str.unterminated_c_str(),
38 v8::NewStringType::kNormal, str.GetLength())
39 .ToLocalChecked();
40}
41
42v8::Local<v8::String> NewStringHelper(v8::Isolate* pIsolate,
43 WideStringView str) {
44 return NewStringHelper(pIsolate, FX_UTF8Encode(str).AsStringView());
45}
46
Tom Sepez1512c362020-03-19 22:13:52 +000047v8::Local<v8::Array> NewArrayHelper(v8::Isolate* pIsolate) {
48 return v8::Array::New(pIsolate);
49}
50
51v8::Local<v8::Object> NewObjectHelper(v8::Isolate* pIsolate) {
52 return v8::Object::New(pIsolate);
53}
54
55v8::Local<v8::Date> NewDateHelper(v8::Isolate* pIsolate, double d) {
56 return v8::Date::New(pIsolate->GetCurrentContext(), d)
57 .ToLocalChecked()
58 .As<v8::Date>();
59}
60
Tom Sepez3466e272020-03-18 23:33:35 +000061int ReentrantToInt32Helper(v8::Isolate* pIsolate, v8::Local<v8::Value> pValue) {
62 if (pValue.IsEmpty())
63 return 0;
Tom Sepez5f3e5812020-08-11 22:46:10 +000064 v8::TryCatch squash_exceptions(pIsolate);
Tom Sepez3466e272020-03-18 23:33:35 +000065 return pValue->Int32Value(pIsolate->GetCurrentContext()).FromMaybe(0);
66}
67
68bool ReentrantToBooleanHelper(v8::Isolate* pIsolate,
69 v8::Local<v8::Value> pValue) {
70 if (pValue.IsEmpty())
71 return false;
Tom Sepez5f3e5812020-08-11 22:46:10 +000072 v8::TryCatch squash_exceptions(pIsolate);
Tom Sepez3466e272020-03-18 23:33:35 +000073 return pValue->BooleanValue(pIsolate);
74}
75
76double ReentrantToDoubleHelper(v8::Isolate* pIsolate,
77 v8::Local<v8::Value> pValue) {
78 if (pValue.IsEmpty())
79 return 0.0;
Tom Sepez5f3e5812020-08-11 22:46:10 +000080 v8::TryCatch squash_exceptions(pIsolate);
Tom Sepez3466e272020-03-18 23:33:35 +000081 return pValue->NumberValue(pIsolate->GetCurrentContext()).FromMaybe(0.0);
82}
83
84WideString ReentrantToWideStringHelper(v8::Isolate* pIsolate,
85 v8::Local<v8::Value> pValue) {
86 if (pValue.IsEmpty())
87 return WideString();
Tom Sepezc72e23f2020-03-19 18:58:17 +000088
Tom Sepez5f3e5812020-08-11 22:46:10 +000089 v8::TryCatch squash_exceptions(pIsolate);
Tom Sepez3466e272020-03-18 23:33:35 +000090 v8::MaybeLocal<v8::String> maybe_string =
91 pValue->ToString(pIsolate->GetCurrentContext());
92 if (maybe_string.IsEmpty())
93 return WideString();
Tom Sepezc72e23f2020-03-19 18:58:17 +000094
Tom Sepez3466e272020-03-18 23:33:35 +000095 v8::String::Utf8Value s(pIsolate, maybe_string.ToLocalChecked());
96 return WideString::FromUTF8(ByteStringView(*s, s.length()));
97}
98
99ByteString ReentrantToByteStringHelper(v8::Isolate* pIsolate,
100 v8::Local<v8::Value> pValue) {
101 if (pValue.IsEmpty())
102 return ByteString();
Tom Sepezc72e23f2020-03-19 18:58:17 +0000103
Tom Sepez5f3e5812020-08-11 22:46:10 +0000104 v8::TryCatch squash_exceptions(pIsolate);
Tom Sepez3466e272020-03-18 23:33:35 +0000105 v8::MaybeLocal<v8::String> maybe_string =
106 pValue->ToString(pIsolate->GetCurrentContext());
107 if (maybe_string.IsEmpty())
108 return ByteString();
Tom Sepezc72e23f2020-03-19 18:58:17 +0000109
Tom Sepez3466e272020-03-18 23:33:35 +0000110 v8::String::Utf8Value s(pIsolate, maybe_string.ToLocalChecked());
111 return ByteString(*s);
112}
113
Tom Sepez1512c362020-03-19 22:13:52 +0000114v8::Local<v8::Object> ReentrantToObjectHelper(v8::Isolate* pIsolate,
115 v8::Local<v8::Value> pValue) {
116 if (pValue.IsEmpty() || !pValue->IsObject())
117 return v8::Local<v8::Object>();
Tom Sepez5f3e5812020-08-11 22:46:10 +0000118
119 v8::TryCatch squash_exceptions(pIsolate);
Tom Sepez1512c362020-03-19 22:13:52 +0000120 v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
121 return pValue->ToObject(context).ToLocalChecked();
122}
123
124v8::Local<v8::Array> ReentrantToArrayHelper(v8::Isolate* pIsolate,
125 v8::Local<v8::Value> pValue) {
126 if (pValue.IsEmpty() || !pValue->IsArray())
127 return v8::Local<v8::Array>();
Tom Sepez5f3e5812020-08-11 22:46:10 +0000128
129 v8::TryCatch squash_exceptions(pIsolate);
Tom Sepez1512c362020-03-19 22:13:52 +0000130 v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
131 return v8::Local<v8::Array>::Cast(pValue->ToObject(context).ToLocalChecked());
132}
133
Tom Sepezc72e23f2020-03-19 18:58:17 +0000134v8::Local<v8::Value> ReentrantGetObjectPropertyHelper(
135 v8::Isolate* pIsolate,
136 v8::Local<v8::Object> pObj,
137 ByteStringView bsUTF8PropertyName) {
138 if (pObj.IsEmpty())
139 return v8::Local<v8::Value>();
140
Tom Sepez5f3e5812020-08-11 22:46:10 +0000141 v8::TryCatch squash_exceptions(pIsolate);
Tom Sepezc72e23f2020-03-19 18:58:17 +0000142 v8::Local<v8::Value> val;
143 if (!pObj->Get(pIsolate->GetCurrentContext(),
144 NewStringHelper(pIsolate, bsUTF8PropertyName))
145 .ToLocal(&val)) {
146 return v8::Local<v8::Value>();
147 }
148 return val;
149}
150
151std::vector<WideString> ReentrantGetObjectPropertyNamesHelper(
152 v8::Isolate* pIsolate,
153 v8::Local<v8::Object> pObj) {
154 if (pObj.IsEmpty())
155 return std::vector<WideString>();
156
Tom Sepez5f3e5812020-08-11 22:46:10 +0000157 v8::TryCatch squash_exceptions(pIsolate);
Tom Sepezc72e23f2020-03-19 18:58:17 +0000158 v8::Local<v8::Array> val;
159 v8::Local<v8::Context> context = pIsolate->GetCurrentContext();
160 if (!pObj->GetPropertyNames(context).ToLocal(&val))
161 return std::vector<WideString>();
162
163 std::vector<WideString> result;
164 for (uint32_t i = 0; i < val->Length(); ++i) {
165 result.push_back(ReentrantToWideStringHelper(
166 pIsolate, val->Get(context, i).ToLocalChecked()));
167 }
168 return result;
169}
170
171bool ReentrantPutObjectPropertyHelper(v8::Isolate* pIsolate,
172 v8::Local<v8::Object> pObj,
173 ByteStringView bsUTF8PropertyName,
174 v8::Local<v8::Value> pPut) {
175 ASSERT(!pPut.IsEmpty());
176 if (pObj.IsEmpty())
177 return false;
178
Tom Sepez5f3e5812020-08-11 22:46:10 +0000179 v8::TryCatch squash_exceptions(pIsolate);
Tom Sepezc72e23f2020-03-19 18:58:17 +0000180 v8::Local<v8::String> name = NewStringHelper(pIsolate, bsUTF8PropertyName);
Tom Sepez2fb992d2020-03-20 19:45:59 +0000181 v8::Maybe<bool> result = pObj->Set(pIsolate->GetCurrentContext(), name, pPut);
182 return result.IsJust() && result.FromJust();
Tom Sepezc72e23f2020-03-19 18:58:17 +0000183}
184
Tom Sepez1512c362020-03-19 22:13:52 +0000185bool ReentrantPutArrayElementHelper(v8::Isolate* pIsolate,
186 v8::Local<v8::Array> pArray,
187 unsigned index,
188 v8::Local<v8::Value> pValue) {
189 if (pArray.IsEmpty())
190 return false;
Tom Sepez2fb992d2020-03-20 19:45:59 +0000191
Tom Sepez5f3e5812020-08-11 22:46:10 +0000192 v8::TryCatch squash_exceptions(pIsolate);
Tom Sepez2fb992d2020-03-20 19:45:59 +0000193 v8::Maybe<bool> result =
194 pArray->Set(pIsolate->GetCurrentContext(), index, pValue);
195 return result.IsJust() && result.FromJust();
Tom Sepez1512c362020-03-19 22:13:52 +0000196}
197
198v8::Local<v8::Value> ReentrantGetArrayElementHelper(v8::Isolate* pIsolate,
199 v8::Local<v8::Array> pArray,
200 unsigned index) {
201 if (pArray.IsEmpty())
202 return v8::Local<v8::Value>();
Tom Sepez5f3e5812020-08-11 22:46:10 +0000203
204 v8::TryCatch squash_exceptions(pIsolate);
Tom Sepez1512c362020-03-19 22:13:52 +0000205 v8::Local<v8::Value> val;
206 if (!pArray->Get(pIsolate->GetCurrentContext(), index).ToLocal(&val))
207 return v8::Local<v8::Value>();
208 return val;
209}
210
211unsigned GetArrayLengthHelper(v8::Local<v8::Array> pArray) {
212 if (pArray.IsEmpty())
213 return 0;
214 return pArray->Length();
215}
216
Tom Sepezacc07782020-03-19 20:02:00 +0000217void ThrowExceptionHelper(v8::Isolate* pIsolate, ByteStringView str) {
218 pIsolate->ThrowException(NewStringHelper(pIsolate, str));
219}
220
221void ThrowExceptionHelper(v8::Isolate* pIsolate, WideStringView str) {
222 pIsolate->ThrowException(NewStringHelper(pIsolate, str));
223}
224
Tom Sepez3466e272020-03-18 23:33:35 +0000225} // namespace fxv8