Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
fxv8.cpp
Go to the documentation of this file.
1// Copyright 2020 The PDFium Authors
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6
7#include "fxjs/fxv8.h"
8
9#include "core/fxcrt/compiler_specific.h"
10#include "core/fxcrt/numerics/safe_conversions.h"
11#include "v8/include/v8-container.h"
12#include "v8/include/v8-date.h"
13#include "v8/include/v8-exception.h"
14#include "v8/include/v8-isolate.h"
15#include "v8/include/v8-primitive.h"
16#include "v8/include/v8-value.h"
17
18namespace fxv8 {
19
21 return !value.IsEmpty() && value->IsUndefined();
22}
23
25 return !value.IsEmpty() && value->IsNull();
26}
27
29 return !value.IsEmpty() && value->IsBoolean();
30}
31
33 return !value.IsEmpty() && value->IsString();
34}
35
37 return !value.IsEmpty() && value->IsNumber();
38}
39
41 return !value.IsEmpty() && value->IsInt32();
42}
43
45 return !value.IsEmpty() && value->IsObject();
46}
47
49 return !value.IsEmpty() && value->IsArray();
50}
51
53 return !value.IsEmpty() && value->IsDate();
54}
55
57 return !value.IsEmpty() && value->IsFunction();
58}
59
60v8::Local<v8::Value> NewNullHelper(v8::Isolate* pIsolate) {
61 return v8::Null(pIsolate);
62}
63
64v8::Local<v8::Value> NewUndefinedHelper(v8::Isolate* pIsolate) {
65 return v8::Undefined(pIsolate);
66}
67
68v8::Local<v8::Number> NewNumberHelper(v8::Isolate* pIsolate, int number) {
69 return v8::Int32::New(pIsolate, number);
70}
71
72v8::Local<v8::Number> NewNumberHelper(v8::Isolate* pIsolate, double number) {
73 return v8::Number::New(pIsolate, number);
74}
75
76v8::Local<v8::Number> NewNumberHelper(v8::Isolate* pIsolate, float number) {
77 return v8::Number::New(pIsolate, number);
78}
79
80v8::Local<v8::Boolean> NewBooleanHelper(v8::Isolate* pIsolate, bool b) {
81 return v8::Boolean::New(pIsolate, b);
82}
83
84v8::Local<v8::String> NewStringHelper(v8::Isolate* pIsolate,
85 ByteStringView str) {
86 return v8::String::NewFromUtf8(pIsolate, str.unterminated_c_str(),
87 v8::NewStringType::kNormal,
88 pdfium::checked_cast<int>(str.GetLength()))
89 .ToLocalChecked();
90}
91
92v8::Local<v8::String> NewStringHelper(v8::Isolate* pIsolate,
93 WideStringView str) {
94 return NewStringHelper(pIsolate, FX_UTF8Encode(str).AsStringView());
95}
96
97v8::Local<v8::Array> NewArrayHelper(v8::Isolate* pIsolate) {
98 return v8::Array::New(pIsolate);
99}
100
101v8::Local<v8::Array> NewArrayHelper(v8::Isolate* pIsolate,
103 v8::Local<v8::Array> result = NewArrayHelper(pIsolate);
104 for (size_t i = 0; i < values.size(); ++i) {
105 fxv8::ReentrantPutArrayElementHelper(
106 pIsolate, result, i,
107 values[i].IsEmpty() ? fxv8::NewUndefinedHelper(pIsolate) : values[i]);
108 }
109 return result;
110}
111
112v8::Local<v8::Object> NewObjectHelper(v8::Isolate* pIsolate) {
113 return v8::Object::New(pIsolate);
114}
115
116v8::Local<v8::Date> NewDateHelper(v8::Isolate* pIsolate, double d) {
117 return v8::Date::New(pIsolate->GetCurrentContext(), d)
118 .ToLocalChecked()
119 .As<v8::Date>();
120}
121
127
130 // SAFETY: required from V8.
131 return UNSAFE_BUFFERS(ByteString(*s, s.length()));
132}
133
140
148
150 v8::Local<v8::Value> pValue) {
151 return static_cast<float>(ReentrantToDoubleHelper(pIsolate, pValue));
152}
153
161
175
189
199
209
226
246
259
272
285
294
308
311 size_t index) {
312 if (pArray.IsEmpty())
313 return v8::Local<v8::Value>();
314
316 v8::Local<v8::Value> val;
317 if (!pArray
320 .ToLocal(&val)) {
321 return v8::Local<v8::Value>();
322 }
323 return val;
324}
325
327 if (pArray.IsEmpty())
328 return 0;
329 return pArray->Length();
330}
331
332void ThrowExceptionHelper(v8::Isolate* pIsolate, ByteStringView str) {
333 pIsolate->ThrowException(NewStringHelper(pIsolate, str));
334}
335
336void ThrowExceptionHelper(v8::Isolate* pIsolate, WideStringView str) {
337 pIsolate->ThrowException(NewStringHelper(pIsolate, str));
338}
339
340} // namespace fxv8
fxcrt::ByteString ByteString
Definition bytestring.h:180
#define UNSAFE_BUFFERS(...)
Definition fxv8.h:22
v8::Local< v8::Number > NewNumberHelper(v8::Isolate *pIsolate, int number)
Definition fxv8.cpp:68
v8::Local< v8::Value > ReentrantGetArrayElementHelper(v8::Isolate *pIsolate, v8::Local< v8::Array > pArray, size_t index)
Definition fxv8.cpp:309
v8::Local< v8::Date > NewDateHelper(v8::Isolate *pIsolate, double d)
Definition fxv8.cpp:116
bool IsBoolean(v8::Local< v8::Value > value)
Definition fxv8.cpp:28
v8::Local< v8::Object > ReentrantToObjectHelper(v8::Isolate *pIsolate, v8::Local< v8::Value > pValue)
Definition fxv8.cpp:190
void ThrowExceptionHelper(v8::Isolate *pIsolate, WideStringView str)
Definition fxv8.cpp:336
v8::Local< v8::Value > NewUndefinedHelper(v8::Isolate *pIsolate)
Definition fxv8.cpp:64
bool IsArray(v8::Local< v8::Value > value)
Definition fxv8.cpp:48
bool IsUndefined(v8::Local< v8::Value > value)
Definition fxv8.cpp:20
double ReentrantToDoubleHelper(v8::Isolate *pIsolate, v8::Local< v8::Value > pValue)
Definition fxv8.cpp:154
v8::Local< v8::String > NewStringHelper(v8::Isolate *pIsolate, WideStringView str)
Definition fxv8.cpp:92
bool ReentrantSetObjectOwnPropertyHelper(v8::Isolate *pIsolate, v8::Local< v8::Object > pObj, ByteStringView bsUTF8PropertyName, v8::Local< v8::Value > pValue)
Definition fxv8.cpp:260
bool ReentrantPutArrayElementHelper(v8::Isolate *pIsolate, v8::Local< v8::Array > pArray, size_t index, v8::Local< v8::Value > pValue)
Definition fxv8.cpp:295
v8::Local< v8::String > NewStringHelper(v8::Isolate *pIsolate, ByteStringView str)
Definition fxv8.cpp:84
bool IsInteger(v8::Local< v8::Value > value)
Definition fxv8.cpp:40
void ThrowExceptionHelper(v8::Isolate *pIsolate, ByteStringView str)
Definition fxv8.cpp:332
bool ReentrantHasObjectOwnPropertyHelper(v8::Isolate *pIsolate, v8::Local< v8::Object > pObj, ByteStringView bsUTF8PropertyName)
Definition fxv8.cpp:247
v8::Local< v8::Boolean > NewBooleanHelper(v8::Isolate *pIsolate, bool b)
Definition fxv8.cpp:80
v8::Local< v8::Array > NewArrayHelper(v8::Isolate *pIsolate, pdfium::span< v8::Local< v8::Value > > values)
Definition fxv8.cpp:101
v8::Local< v8::Value > ReentrantGetObjectPropertyHelper(v8::Isolate *pIsolate, v8::Local< v8::Object > pObj, ByteStringView bsUTF8PropertyName)
Definition fxv8.cpp:210
WideString ReentrantToWideStringHelper(v8::Isolate *pIsolate, v8::Local< v8::Value > pValue)
Definition fxv8.cpp:162
void ReentrantDeleteObjectPropertyHelper(v8::Isolate *pIsolate, v8::Local< v8::Object > pObj, ByteStringView bsUTF8PropertyName)
Definition fxv8.cpp:286
bool IsNumber(v8::Local< v8::Value > value)
Definition fxv8.cpp:36
bool ReentrantPutObjectPropertyHelper(v8::Isolate *pIsolate, v8::Local< v8::Object > pObj, ByteStringView bsUTF8PropertyName, v8::Local< v8::Value > pPut)
Definition fxv8.cpp:273
bool IsObject(v8::Local< v8::Value > value)
Definition fxv8.cpp:44
bool IsString(v8::Local< v8::Value > value)
Definition fxv8.cpp:32
ByteString ReentrantToByteStringHelper(v8::Isolate *pIsolate, v8::Local< v8::Value > pValue)
Definition fxv8.cpp:176
ByteString ToByteString(v8::Isolate *pIsolate, v8::Local< v8::String > pValue)
Definition fxv8.cpp:128
v8::Local< v8::Object > NewObjectHelper(v8::Isolate *pIsolate)
Definition fxv8.cpp:112
bool IsDate(v8::Local< v8::Value > value)
Definition fxv8.cpp:52
v8::Local< v8::Array > NewArrayHelper(v8::Isolate *pIsolate)
Definition fxv8.cpp:97
bool ReentrantToBooleanHelper(v8::Isolate *pIsolate, v8::Local< v8::Value > pValue)
Definition fxv8.cpp:141
size_t GetArrayLengthHelper(v8::Local< v8::Array > pArray)
Definition fxv8.cpp:326
v8::Local< v8::Number > NewNumberHelper(v8::Isolate *pIsolate, double number)
Definition fxv8.cpp:72
WideString ToWideString(v8::Isolate *pIsolate, v8::Local< v8::String > pValue)
Definition fxv8.cpp:122
v8::Local< v8::Value > NewNullHelper(v8::Isolate *pIsolate)
Definition fxv8.cpp:60
bool IsNull(v8::Local< v8::Value > value)
Definition fxv8.cpp:24
v8::Local< v8::Number > NewNumberHelper(v8::Isolate *pIsolate, float number)
Definition fxv8.cpp:76
float ReentrantToFloatHelper(v8::Isolate *pIsolate, v8::Local< v8::Value > pValue)
Definition fxv8.cpp:149
int32_t ReentrantToInt32Helper(v8::Isolate *pIsolate, v8::Local< v8::Value > pValue)
Definition fxv8.cpp:134
v8::Local< v8::Array > ReentrantToArrayHelper(v8::Isolate *pIsolate, v8::Local< v8::Value > pValue)
Definition fxv8.cpp:200
std::vector< WideString > ReentrantGetObjectPropertyNamesHelper(v8::Isolate *pIsolate, v8::Local< v8::Object > pObj)
Definition fxv8.cpp:227
bool IsFunction(v8::Local< v8::Value > value)
Definition fxv8.cpp:56
fxcrt::ByteStringView ByteStringView
fxcrt::WideStringView WideStringView
fxcrt::WideString WideString
Definition widestring.h:207