blob: 93e568c0ba059dc4e74d955241af3459a1f4fbda [file] [log] [blame]
Dan Sinclair1770c022016-03-14 14:14:16 -04001// 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.
4
5// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
dsinclair43554682016-09-29 17:29:48 -07007#include "fxjs/cfxjse_value.h"
Dan Sinclair1770c022016-03-14 14:14:16 -04008
9#include <math.h>
10
dsinclair43554682016-09-29 17:29:48 -070011#include "fxjs/cfxjse_class.h"
12#include "fxjs/cfxjse_context.h"
Dan Sinclair1770c022016-03-14 14:14:16 -040013
dsinclair4044d2d2016-06-13 12:38:29 -070014namespace {
15
Dan Sinclair05df0752017-03-14 14:43:42 -040016double ftod(float fNumber) {
17 static_assert(sizeof(float) == 4, "float of incorrect size");
dsinclair4044d2d2016-06-13 12:38:29 -070018
19 uint32_t nFloatBits = (uint32_t&)fNumber;
20 uint8_t nExponent = (uint8_t)(nFloatBits >> 23);
21 if (nExponent == 0 || nExponent == 255)
22 return fNumber;
23
24 int8_t nErrExp = nExponent - 150;
25 if (nErrExp >= 0)
26 return fNumber;
27
28 double dwError = pow(2.0, nErrExp), dwErrorHalf = dwError / 2;
29 double dNumber = fNumber, dNumberAbs = fabs(fNumber);
30 double dNumberAbsMin = dNumberAbs - dwErrorHalf,
31 dNumberAbsMax = dNumberAbs + dwErrorHalf;
32 int32_t iErrPos = 0;
33 if (floor(dNumberAbsMin) == floor(dNumberAbsMax)) {
34 dNumberAbsMin = fmod(dNumberAbsMin, 1.0);
35 dNumberAbsMax = fmod(dNumberAbsMax, 1.0);
36 int32_t iErrPosMin = 1, iErrPosMax = 38;
37 do {
38 int32_t iMid = (iErrPosMin + iErrPosMax) / 2;
39 double dPow = pow(10.0, iMid);
40 if (floor(dNumberAbsMin * dPow) == floor(dNumberAbsMax * dPow)) {
41 iErrPosMin = iMid + 1;
42 } else {
43 iErrPosMax = iMid;
44 }
45 } while (iErrPosMin < iErrPosMax);
46 iErrPos = iErrPosMax;
47 }
48 double dPow = pow(10.0, iErrPos);
49 return fNumber < 0 ? ceil(dNumber * dPow - 0.5) / dPow
50 : floor(dNumber * dPow + 0.5) / dPow;
51}
52
53} // namespace
54
Ryan Harrison275e2602017-09-18 14:23:18 -040055void FXJSE_ThrowMessage(const ByteStringView& utf8Message) {
Dan Sinclair1770c022016-03-14 14:14:16 -040056 v8::Isolate* pIsolate = v8::Isolate::GetCurrent();
57 ASSERT(pIsolate);
58
59 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(pIsolate);
60 v8::Local<v8::String> hMessage = v8::String::NewFromUtf8(
Tom Sepez33b42e42017-07-19 13:19:12 -070061 pIsolate, utf8Message.unterminated_c_str(), v8::String::kNormalString,
Dan Sinclair1770c022016-03-14 14:14:16 -040062 utf8Message.GetLength());
dsinclair769b1372016-06-08 13:12:41 -070063 v8::Local<v8::Value> hError = v8::Exception::Error(hMessage);
Dan Sinclair1770c022016-03-14 14:14:16 -040064 pIsolate->ThrowException(hError);
65}
66
weilieec3a362016-06-18 06:25:37 -070067CFXJSE_Value::CFXJSE_Value(v8::Isolate* pIsolate) : m_pIsolate(pIsolate) {}
68
69CFXJSE_Value::~CFXJSE_Value() {}
70
dsinclair8f3074b2016-06-02 17:45:25 -070071CFXJSE_HostObject* CFXJSE_Value::ToHostObject(CFXJSE_Class* lpClass) const {
Dan Sinclair1770c022016-03-14 14:14:16 -040072 ASSERT(!m_hValue.IsEmpty());
73
74 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);
dsinclair12a6b0c2016-05-26 11:14:08 -070075 v8::Local<v8::Value> pValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
76 ASSERT(!pValue.IsEmpty());
Dan Sinclair1770c022016-03-14 14:14:16 -040077
dsinclair12a6b0c2016-05-26 11:14:08 -070078 if (!pValue->IsObject())
Dan Sinclair1770c022016-03-14 14:14:16 -040079 return nullptr;
80
dsinclair12a6b0c2016-05-26 11:14:08 -070081 return FXJSE_RetrieveObjectBinding(pValue.As<v8::Object>(), lpClass);
Dan Sinclair1770c022016-03-14 14:14:16 -040082}
83
dsinclairf27aeec2016-06-07 19:36:18 -070084void CFXJSE_Value::SetObject(CFXJSE_HostObject* lpObject,
85 CFXJSE_Class* pClass) {
86 if (!pClass) {
87 ASSERT(!lpObject);
88 SetJSObject();
89 return;
Dan Sinclair1770c022016-03-14 14:14:16 -040090 }
dsinclairf27aeec2016-06-07 19:36:18 -070091 SetHostObject(lpObject, pClass);
Dan Sinclair1770c022016-03-14 14:14:16 -040092}
93
tsepez29adee72016-05-31 14:22:09 -070094void CFXJSE_Value::SetHostObject(CFXJSE_HostObject* lpObject,
95 CFXJSE_Class* lpClass) {
Dan Sinclair1770c022016-03-14 14:14:16 -040096 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);
97 ASSERT(lpClass);
98 v8::Local<v8::FunctionTemplate> hClass =
99 v8::Local<v8::FunctionTemplate>::New(m_pIsolate, lpClass->m_hTemplate);
100 v8::Local<v8::Object> hObject = hClass->InstanceTemplate()->NewInstance();
101 FXJSE_UpdateObjectBinding(hObject, lpObject);
102 m_hValue.Reset(m_pIsolate, hObject);
103}
104
Dan Sinclair76a44de2017-01-11 08:58:35 -0500105void CFXJSE_Value::SetArray(
106 const std::vector<std::unique_ptr<CFXJSE_Value>>& values) {
Dan Sinclair1770c022016-03-14 14:14:16 -0400107 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);
Dan Sinclair76a44de2017-01-11 08:58:35 -0500108 v8::Local<v8::Array> hArrayObject = v8::Array::New(m_pIsolate, values.size());
109 uint32_t count = 0;
110 for (auto& v : values) {
111 hArrayObject->Set(count++, v8::Local<v8::Value>::New(
112 m_pIsolate, v.get()->DirectGetValue()));
Dan Sinclair1770c022016-03-14 14:14:16 -0400113 }
114 m_hValue.Reset(m_pIsolate, hArrayObject);
115}
116
dsinclair04fd27b2016-03-30 12:59:04 -0700117void CFXJSE_Value::SetDate(double dDouble) {
Dan Sinclair1770c022016-03-14 14:14:16 -0400118 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);
119 v8::Local<v8::Value> hDate = v8::Date::New(m_pIsolate, dDouble);
120 m_hValue.Reset(m_pIsolate, hDate);
121}
122
Dan Sinclair05df0752017-03-14 14:43:42 -0400123void CFXJSE_Value::SetFloat(float fFloat) {
dsinclair4044d2d2016-06-13 12:38:29 -0700124 CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
dsinclair08fea802016-07-12 10:37:52 -0700125 v8::Local<v8::Value> pValue = v8::Number::New(m_pIsolate, ftod(fFloat));
dsinclair4044d2d2016-06-13 12:38:29 -0700126 m_hValue.Reset(m_pIsolate, pValue);
127}
128
Ryan Harrison275e2602017-09-18 14:23:18 -0400129bool CFXJSE_Value::SetObjectProperty(const ByteStringView& szPropName,
tsepez304bb912016-11-03 06:10:26 -0700130 CFXJSE_Value* lpPropValue) {
dsinclairf27aeec2016-06-07 19:36:18 -0700131 ASSERT(lpPropValue);
Dan Sinclair1770c022016-03-14 14:14:16 -0400132 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);
133 v8::Local<v8::Value> hObject =
134 v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
135 if (!hObject->IsObject())
tsepez304bb912016-11-03 06:10:26 -0700136 return false;
Dan Sinclair1770c022016-03-14 14:14:16 -0400137
138 v8::Local<v8::Value> hPropValue =
139 v8::Local<v8::Value>::New(m_pIsolate, lpPropValue->DirectGetValue());
tsepez304bb912016-11-03 06:10:26 -0700140 return (bool)hObject.As<v8::Object>()->Set(
Tom Sepez33b42e42017-07-19 13:19:12 -0700141 v8::String::NewFromUtf8(m_pIsolate, szPropName.unterminated_c_str(),
Dan Sinclair1770c022016-03-14 14:14:16 -0400142 v8::String::kNormalString,
143 szPropName.GetLength()),
144 hPropValue);
145}
146
Ryan Harrison275e2602017-09-18 14:23:18 -0400147bool CFXJSE_Value::GetObjectProperty(const ByteStringView& szPropName,
tsepez304bb912016-11-03 06:10:26 -0700148 CFXJSE_Value* lpPropValue) {
dsinclairf27aeec2016-06-07 19:36:18 -0700149 ASSERT(lpPropValue);
Dan Sinclair1770c022016-03-14 14:14:16 -0400150 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);
151 v8::Local<v8::Value> hObject =
152 v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
153 if (!hObject->IsObject())
tsepez304bb912016-11-03 06:10:26 -0700154 return false;
Dan Sinclair1770c022016-03-14 14:14:16 -0400155
156 v8::Local<v8::Value> hPropValue =
157 hObject.As<v8::Object>()->Get(v8::String::NewFromUtf8(
Tom Sepez33b42e42017-07-19 13:19:12 -0700158 m_pIsolate, szPropName.unterminated_c_str(),
159 v8::String::kNormalString, szPropName.GetLength()));
Dan Sinclair1770c022016-03-14 14:14:16 -0400160 lpPropValue->ForceSetValue(hPropValue);
tsepez304bb912016-11-03 06:10:26 -0700161 return true;
Dan Sinclair1770c022016-03-14 14:14:16 -0400162}
163
tsepez304bb912016-11-03 06:10:26 -0700164bool CFXJSE_Value::SetObjectProperty(uint32_t uPropIdx,
165 CFXJSE_Value* lpPropValue) {
Dan Sinclair1770c022016-03-14 14:14:16 -0400166 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);
167 v8::Local<v8::Value> hObject =
168 v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
169 if (!hObject->IsObject())
tsepez304bb912016-11-03 06:10:26 -0700170 return false;
Dan Sinclair1770c022016-03-14 14:14:16 -0400171
172 v8::Local<v8::Value> hPropValue =
173 v8::Local<v8::Value>::New(m_pIsolate, lpPropValue->DirectGetValue());
tsepez304bb912016-11-03 06:10:26 -0700174 return (bool)hObject.As<v8::Object>()->Set(uPropIdx, hPropValue);
Dan Sinclair1770c022016-03-14 14:14:16 -0400175}
176
tsepez304bb912016-11-03 06:10:26 -0700177bool CFXJSE_Value::GetObjectPropertyByIdx(uint32_t uPropIdx,
178 CFXJSE_Value* lpPropValue) {
Dan Sinclair1770c022016-03-14 14:14:16 -0400179 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);
180 v8::Local<v8::Value> hObject =
181 v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
182 if (!hObject->IsObject())
tsepez304bb912016-11-03 06:10:26 -0700183 return false;
Dan Sinclair1770c022016-03-14 14:14:16 -0400184
185 v8::Local<v8::Value> hPropValue = hObject.As<v8::Object>()->Get(uPropIdx);
186 lpPropValue->ForceSetValue(hPropValue);
tsepez304bb912016-11-03 06:10:26 -0700187 return true;
Dan Sinclair1770c022016-03-14 14:14:16 -0400188}
189
Ryan Harrison275e2602017-09-18 14:23:18 -0400190bool CFXJSE_Value::DeleteObjectProperty(const ByteStringView& szPropName) {
Dan Sinclair1770c022016-03-14 14:14:16 -0400191 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);
192 v8::Local<v8::Value> hObject =
193 v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
194 if (!hObject->IsObject())
tsepez304bb912016-11-03 06:10:26 -0700195 return false;
Dan Sinclair1770c022016-03-14 14:14:16 -0400196
197 hObject.As<v8::Object>()->Delete(v8::String::NewFromUtf8(
Tom Sepez33b42e42017-07-19 13:19:12 -0700198 m_pIsolate, szPropName.unterminated_c_str(), v8::String::kNormalString,
Dan Sinclair1770c022016-03-14 14:14:16 -0400199 szPropName.GetLength()));
tsepez304bb912016-11-03 06:10:26 -0700200 return true;
Dan Sinclair1770c022016-03-14 14:14:16 -0400201}
202
Ryan Harrison275e2602017-09-18 14:23:18 -0400203bool CFXJSE_Value::HasObjectOwnProperty(const ByteStringView& szPropName,
tsepez304bb912016-11-03 06:10:26 -0700204 bool bUseTypeGetter) {
Dan Sinclair1770c022016-03-14 14:14:16 -0400205 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);
206 v8::Local<v8::Value> hObject =
207 v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
208 if (!hObject->IsObject())
tsepez304bb912016-11-03 06:10:26 -0700209 return false;
Dan Sinclair1770c022016-03-14 14:14:16 -0400210
211 v8::Local<v8::String> hKey = v8::String::NewFromUtf8(
Tom Sepez33b42e42017-07-19 13:19:12 -0700212 m_pIsolate, szPropName.unterminated_c_str(), v8::String::kNormalString,
Dan Sinclair1770c022016-03-14 14:14:16 -0400213 szPropName.GetLength());
214 return hObject.As<v8::Object>()->HasRealNamedProperty(hKey) ||
215 (bUseTypeGetter &&
216 hObject.As<v8::Object>()
217 ->HasOwnProperty(m_pIsolate->GetCurrentContext(), hKey)
218 .FromMaybe(false));
219}
220
Ryan Harrison275e2602017-09-18 14:23:18 -0400221bool CFXJSE_Value::SetObjectOwnProperty(const ByteStringView& szPropName,
tsepez304bb912016-11-03 06:10:26 -0700222 CFXJSE_Value* lpPropValue) {
dsinclairf27aeec2016-06-07 19:36:18 -0700223 ASSERT(lpPropValue);
Dan Sinclair1770c022016-03-14 14:14:16 -0400224 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);
225 v8::Local<v8::Value> hObject =
226 v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
227 if (!hObject->IsObject())
tsepez304bb912016-11-03 06:10:26 -0700228 return false;
Dan Sinclair1770c022016-03-14 14:14:16 -0400229
dsinclair12a6b0c2016-05-26 11:14:08 -0700230 v8::Local<v8::Value> pValue =
Dan Sinclair1770c022016-03-14 14:14:16 -0400231 v8::Local<v8::Value>::New(m_pIsolate, lpPropValue->m_hValue);
232 return hObject.As<v8::Object>()
233 ->DefineOwnProperty(
234 m_pIsolate->GetCurrentContext(),
Tom Sepez33b42e42017-07-19 13:19:12 -0700235 v8::String::NewFromUtf8(m_pIsolate, szPropName.unterminated_c_str(),
Dan Sinclair1770c022016-03-14 14:14:16 -0400236 v8::String::kNormalString,
237 szPropName.GetLength()),
dsinclair12a6b0c2016-05-26 11:14:08 -0700238 pValue)
Dan Sinclair1770c022016-03-14 14:14:16 -0400239 .FromMaybe(false);
240}
241
tsepez304bb912016-11-03 06:10:26 -0700242bool CFXJSE_Value::SetFunctionBind(CFXJSE_Value* lpOldFunction,
243 CFXJSE_Value* lpNewThis) {
dsinclairf27aeec2016-06-07 19:36:18 -0700244 ASSERT(lpOldFunction && lpNewThis);
245
Dan Sinclair1770c022016-03-14 14:14:16 -0400246 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);
247 v8::Local<v8::Value> rgArgs[2];
248 v8::Local<v8::Value> hOldFunction =
249 v8::Local<v8::Value>::New(m_pIsolate, lpOldFunction->DirectGetValue());
250 if (hOldFunction.IsEmpty() || !hOldFunction->IsFunction())
tsepez304bb912016-11-03 06:10:26 -0700251 return false;
Dan Sinclair1770c022016-03-14 14:14:16 -0400252
253 rgArgs[0] = hOldFunction;
254 v8::Local<v8::Value> hNewThis =
255 v8::Local<v8::Value>::New(m_pIsolate, lpNewThis->DirectGetValue());
256 if (hNewThis.IsEmpty())
tsepez304bb912016-11-03 06:10:26 -0700257 return false;
Dan Sinclair1770c022016-03-14 14:14:16 -0400258
259 rgArgs[1] = hNewThis;
260 v8::Local<v8::String> hBinderFuncSource =
261 v8::String::NewFromUtf8(m_pIsolate,
262 "(function (oldfunction, newthis) { return "
263 "oldfunction.bind(newthis); })");
264 v8::Local<v8::Function> hBinderFunc =
265 v8::Script::Compile(hBinderFuncSource)->Run().As<v8::Function>();
266 v8::Local<v8::Value> hBoundFunction =
267 hBinderFunc->Call(m_pIsolate->GetCurrentContext()->Global(), 2, rgArgs);
268 if (hBoundFunction.IsEmpty() || !hBoundFunction->IsFunction())
tsepez304bb912016-11-03 06:10:26 -0700269 return false;
Dan Sinclair1770c022016-03-14 14:14:16 -0400270
271 m_hValue.Reset(m_pIsolate, hBoundFunction);
tsepez304bb912016-11-03 06:10:26 -0700272 return true;
Dan Sinclair1770c022016-03-14 14:14:16 -0400273}
274
275#define FXJSE_INVALID_PTR ((void*)(intptr_t)-1)
tsepez304bb912016-11-03 06:10:26 -0700276bool CFXJSE_Value::Call(CFXJSE_Value* lpReceiver,
277 CFXJSE_Value* lpRetValue,
278 uint32_t nArgCount,
279 CFXJSE_Value** lpArgs) {
Dan Sinclair1770c022016-03-14 14:14:16 -0400280 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);
281 v8::Local<v8::Value> hFunctionValue =
282 v8::Local<v8::Value>::New(m_pIsolate, DirectGetValue());
283 v8::Local<v8::Object> hFunctionObject =
284 !hFunctionValue.IsEmpty() && hFunctionValue->IsObject()
285 ? hFunctionValue.As<v8::Object>()
286 : v8::Local<v8::Object>();
287
288 v8::TryCatch trycatch(m_pIsolate);
289 if (hFunctionObject.IsEmpty() || !hFunctionObject->IsCallable()) {
290 if (lpRetValue)
291 lpRetValue->ForceSetValue(FXJSE_CreateReturnValue(m_pIsolate, trycatch));
tsepez304bb912016-11-03 06:10:26 -0700292 return false;
Dan Sinclair1770c022016-03-14 14:14:16 -0400293 }
294
295 v8::Local<v8::Value> hReturnValue;
296 v8::Local<v8::Value>* lpLocalArgs = NULL;
297 if (nArgCount) {
298 lpLocalArgs = FX_Alloc(v8::Local<v8::Value>, nArgCount);
299 for (uint32_t i = 0; i < nArgCount; i++) {
300 new (lpLocalArgs + i) v8::Local<v8::Value>;
dsinclair12a6b0c2016-05-26 11:14:08 -0700301 CFXJSE_Value* lpArg = lpArgs[i];
Dan Sinclair1770c022016-03-14 14:14:16 -0400302 if (lpArg) {
303 lpLocalArgs[i] =
304 v8::Local<v8::Value>::New(m_pIsolate, lpArg->DirectGetValue());
305 }
306 if (lpLocalArgs[i].IsEmpty()) {
307 lpLocalArgs[i] = v8::Undefined(m_pIsolate);
308 }
309 }
310 }
311
tsepez304bb912016-11-03 06:10:26 -0700312 bool bRetValue = true;
Dan Sinclair1770c022016-03-14 14:14:16 -0400313 if (lpReceiver == FXJSE_INVALID_PTR) {
314 v8::MaybeLocal<v8::Value> maybe_retvalue =
315 hFunctionObject->CallAsConstructor(m_pIsolate->GetCurrentContext(),
316 nArgCount, lpLocalArgs);
317 hReturnValue = maybe_retvalue.FromMaybe(v8::Local<v8::Value>());
318 } else {
319 v8::Local<v8::Value> hReceiver;
320 if (lpReceiver) {
321 hReceiver =
322 v8::Local<v8::Value>::New(m_pIsolate, lpReceiver->DirectGetValue());
323 }
324 if (hReceiver.IsEmpty() || !hReceiver->IsObject())
325 hReceiver = v8::Object::New(m_pIsolate);
326
327 v8::MaybeLocal<v8::Value> maybe_retvalue = hFunctionObject->CallAsFunction(
328 m_pIsolate->GetCurrentContext(), hReceiver, nArgCount, lpLocalArgs);
329 hReturnValue = maybe_retvalue.FromMaybe(v8::Local<v8::Value>());
330 }
331
332 if (trycatch.HasCaught()) {
333 hReturnValue = FXJSE_CreateReturnValue(m_pIsolate, trycatch);
tsepez304bb912016-11-03 06:10:26 -0700334 bRetValue = false;
Dan Sinclair1770c022016-03-14 14:14:16 -0400335 }
336
337 if (lpRetValue)
338 lpRetValue->ForceSetValue(hReturnValue);
339
340 if (lpLocalArgs) {
341 for (uint32_t i = 0; i < nArgCount; i++)
342 lpLocalArgs[i].~Local();
343 FX_Free(lpLocalArgs);
344 }
345 return bRetValue;
346}
dsinclair08fea802016-07-12 10:37:52 -0700347
tsepez304bb912016-11-03 06:10:26 -0700348bool CFXJSE_Value::IsUndefined() const {
dsinclair08fea802016-07-12 10:37:52 -0700349 if (m_hValue.IsEmpty())
tsepez304bb912016-11-03 06:10:26 -0700350 return false;
dsinclair08fea802016-07-12 10:37:52 -0700351
352 CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
353 v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
354 return hValue->IsUndefined();
355}
356
tsepez304bb912016-11-03 06:10:26 -0700357bool CFXJSE_Value::IsNull() const {
dsinclair08fea802016-07-12 10:37:52 -0700358 if (m_hValue.IsEmpty())
tsepez304bb912016-11-03 06:10:26 -0700359 return false;
dsinclair08fea802016-07-12 10:37:52 -0700360
361 CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
362 v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
363 return hValue->IsNull();
364}
365
tsepez304bb912016-11-03 06:10:26 -0700366bool CFXJSE_Value::IsBoolean() const {
dsinclair08fea802016-07-12 10:37:52 -0700367 if (m_hValue.IsEmpty())
tsepez304bb912016-11-03 06:10:26 -0700368 return false;
dsinclair08fea802016-07-12 10:37:52 -0700369
370 CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
371 v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
372 return hValue->IsBoolean();
373}
374
tsepez304bb912016-11-03 06:10:26 -0700375bool CFXJSE_Value::IsString() const {
dsinclair08fea802016-07-12 10:37:52 -0700376 if (m_hValue.IsEmpty())
tsepez304bb912016-11-03 06:10:26 -0700377 return false;
dsinclair08fea802016-07-12 10:37:52 -0700378
379 CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
380 v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
381 return hValue->IsString();
382}
383
tsepez304bb912016-11-03 06:10:26 -0700384bool CFXJSE_Value::IsNumber() const {
dsinclair08fea802016-07-12 10:37:52 -0700385 if (m_hValue.IsEmpty())
tsepez304bb912016-11-03 06:10:26 -0700386 return false;
dsinclair08fea802016-07-12 10:37:52 -0700387
388 CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
389 v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
390 return hValue->IsNumber();
391}
392
tsepez304bb912016-11-03 06:10:26 -0700393bool CFXJSE_Value::IsInteger() const {
dsinclair08fea802016-07-12 10:37:52 -0700394 if (m_hValue.IsEmpty())
tsepez304bb912016-11-03 06:10:26 -0700395 return false;
dsinclair08fea802016-07-12 10:37:52 -0700396
397 CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
398 v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
399 return hValue->IsInt32();
400}
401
tsepez304bb912016-11-03 06:10:26 -0700402bool CFXJSE_Value::IsObject() const {
dsinclair08fea802016-07-12 10:37:52 -0700403 if (m_hValue.IsEmpty())
tsepez304bb912016-11-03 06:10:26 -0700404 return false;
dsinclair08fea802016-07-12 10:37:52 -0700405
406 CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
407 v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
408 return hValue->IsObject();
409}
410
tsepez304bb912016-11-03 06:10:26 -0700411bool CFXJSE_Value::IsArray() const {
dsinclair08fea802016-07-12 10:37:52 -0700412 if (m_hValue.IsEmpty())
tsepez304bb912016-11-03 06:10:26 -0700413 return false;
dsinclair08fea802016-07-12 10:37:52 -0700414
415 CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
416 v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
417 return hValue->IsArray();
418}
419
tsepez304bb912016-11-03 06:10:26 -0700420bool CFXJSE_Value::IsFunction() const {
dsinclair08fea802016-07-12 10:37:52 -0700421 if (m_hValue.IsEmpty())
tsepez304bb912016-11-03 06:10:26 -0700422 return false;
dsinclair08fea802016-07-12 10:37:52 -0700423
424 CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
425 v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
426 return hValue->IsFunction();
427}
428
tsepez304bb912016-11-03 06:10:26 -0700429bool CFXJSE_Value::IsDate() const {
dsinclair08fea802016-07-12 10:37:52 -0700430 if (m_hValue.IsEmpty())
tsepez304bb912016-11-03 06:10:26 -0700431 return false;
dsinclair08fea802016-07-12 10:37:52 -0700432
433 CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
434 v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
435 return hValue->IsDate();
436}
437
tsepez304bb912016-11-03 06:10:26 -0700438bool CFXJSE_Value::ToBoolean() const {
dsinclair08fea802016-07-12 10:37:52 -0700439 ASSERT(!m_hValue.IsEmpty());
440 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);
441 v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
tsepez304bb912016-11-03 06:10:26 -0700442 return static_cast<bool>(hValue->BooleanValue());
dsinclair08fea802016-07-12 10:37:52 -0700443}
444
Dan Sinclair05df0752017-03-14 14:43:42 -0400445float CFXJSE_Value::ToFloat() const {
dsinclair08fea802016-07-12 10:37:52 -0700446 ASSERT(!m_hValue.IsEmpty());
447 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);
448 v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
Dan Sinclair05df0752017-03-14 14:43:42 -0400449 return static_cast<float>(hValue->NumberValue());
dsinclair08fea802016-07-12 10:37:52 -0700450}
451
452double CFXJSE_Value::ToDouble() const {
453 ASSERT(!m_hValue.IsEmpty());
454 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);
455 v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
456 return static_cast<double>(hValue->NumberValue());
457}
458
459int32_t CFXJSE_Value::ToInteger() const {
460 ASSERT(!m_hValue.IsEmpty());
461 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);
462 v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
463 return static_cast<int32_t>(hValue->NumberValue());
464}
465
Ryan Harrison275e2602017-09-18 14:23:18 -0400466ByteString CFXJSE_Value::ToString() const {
dsinclair08fea802016-07-12 10:37:52 -0700467 ASSERT(!m_hValue.IsEmpty());
468 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);
469 v8::Local<v8::Value> hValue = v8::Local<v8::Value>::New(m_pIsolate, m_hValue);
470 v8::Local<v8::String> hString = hValue->ToString();
471 v8::String::Utf8Value hStringVal(hString);
Ryan Harrison275e2602017-09-18 14:23:18 -0400472 return ByteString(*hStringVal);
dsinclair08fea802016-07-12 10:37:52 -0700473}
474
475void CFXJSE_Value::SetUndefined() {
476 CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
477 v8::Local<v8::Value> hValue = v8::Undefined(m_pIsolate);
478 m_hValue.Reset(m_pIsolate, hValue);
479}
480
481void CFXJSE_Value::SetNull() {
482 CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
483 v8::Local<v8::Value> hValue = v8::Null(m_pIsolate);
484 m_hValue.Reset(m_pIsolate, hValue);
485}
486
tsepez304bb912016-11-03 06:10:26 -0700487void CFXJSE_Value::SetBoolean(bool bBoolean) {
dsinclair08fea802016-07-12 10:37:52 -0700488 CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
tsepez304bb912016-11-03 06:10:26 -0700489 v8::Local<v8::Value> hValue = v8::Boolean::New(m_pIsolate, bBoolean != false);
dsinclair08fea802016-07-12 10:37:52 -0700490 m_hValue.Reset(m_pIsolate, hValue);
491}
492
493void CFXJSE_Value::SetInteger(int32_t nInteger) {
494 CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
495 v8::Local<v8::Value> hValue = v8::Integer::New(m_pIsolate, nInteger);
496 m_hValue.Reset(m_pIsolate, hValue);
497}
498
499void CFXJSE_Value::SetDouble(double dDouble) {
500 CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
501 v8::Local<v8::Value> hValue = v8::Number::New(m_pIsolate, dDouble);
502 m_hValue.Reset(m_pIsolate, hValue);
503}
504
Ryan Harrison275e2602017-09-18 14:23:18 -0400505void CFXJSE_Value::SetString(const ByteStringView& szString) {
dsinclair08fea802016-07-12 10:37:52 -0700506 CFXJSE_ScopeUtil_IsolateHandle scope(m_pIsolate);
507 v8::Local<v8::Value> hValue = v8::String::NewFromUtf8(
508 m_pIsolate, reinterpret_cast<const char*>(szString.raw_str()),
509 v8::String::kNormalString, szString.GetLength());
510 m_hValue.Reset(m_pIsolate, hValue);
511}
512
513void CFXJSE_Value::SetJSObject() {
514 CFXJSE_ScopeUtil_IsolateHandleRootContext scope(m_pIsolate);
515 v8::Local<v8::Value> hValue = v8::Object::New(m_pIsolate);
516 m_hValue.Reset(m_pIsolate, hValue);
517}