1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
// Copyright 2017 PDFium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
#include "fxjs/cfx_v8.h"
#include "core/fxcrt/fx_memory.h"
#include "third_party/base/allocator/partition_allocator/partition_alloc.h"
CFX_V8::CFX_V8(v8::Isolate* isolate) : m_pIsolate(isolate) {}
CFX_V8::~CFX_V8() = default;
v8::Local<v8::Value> CFX_V8::GetObjectProperty(
v8::Local<v8::Object> pObj,
const WideString& wsPropertyName) {
if (pObj.IsEmpty())
return v8::Local<v8::Value>();
v8::Local<v8::Value> val;
if (!pObj->Get(m_pIsolate->GetCurrentContext(),
NewString(wsPropertyName.AsStringView()))
.ToLocal(&val))
return v8::Local<v8::Value>();
return val;
}
std::vector<WideString> CFX_V8::GetObjectPropertyNames(
v8::Local<v8::Object> pObj) {
if (pObj.IsEmpty())
return std::vector<WideString>();
v8::Local<v8::Array> val;
v8::Local<v8::Context> context = m_pIsolate->GetCurrentContext();
if (!pObj->GetPropertyNames(context).ToLocal(&val))
return std::vector<WideString>();
std::vector<WideString> result;
for (uint32_t i = 0; i < val->Length(); ++i) {
result.push_back(ToWideString(val->Get(context, i).ToLocalChecked()));
}
return result;
}
void CFX_V8::PutObjectProperty(v8::Local<v8::Object> pObj,
const WideString& wsPropertyName,
v8::Local<v8::Value> pPut) {
if (pObj.IsEmpty())
return;
pObj->Set(m_pIsolate->GetCurrentContext(),
NewString(wsPropertyName.AsStringView()), pPut)
.FromJust();
}
void CFX_V8::DisposeIsolate() {
if (m_pIsolate)
m_pIsolate.Release()->Dispose();
}
v8::Local<v8::Array> CFX_V8::NewArray() {
return v8::Array::New(GetIsolate());
}
v8::Local<v8::Object> CFX_V8::NewObject() {
return v8::Object::New(GetIsolate());
}
unsigned CFX_V8::PutArrayElement(v8::Local<v8::Array> pArray,
unsigned index,
v8::Local<v8::Value> pValue) {
if (pArray.IsEmpty())
return 0;
if (pArray->Set(m_pIsolate->GetCurrentContext(), index, pValue).IsNothing())
return 0;
return 1;
}
v8::Local<v8::Value> CFX_V8::GetArrayElement(v8::Local<v8::Array> pArray,
unsigned index) {
if (pArray.IsEmpty())
return v8::Local<v8::Value>();
v8::Local<v8::Value> val;
if (!pArray->Get(m_pIsolate->GetCurrentContext(), index).ToLocal(&val))
return v8::Local<v8::Value>();
return val;
}
unsigned CFX_V8::GetArrayLength(v8::Local<v8::Array> pArray) {
if (pArray.IsEmpty())
return 0;
return pArray->Length();
}
v8::Local<v8::Number> CFX_V8::NewNumber(int number) {
return v8::Int32::New(GetIsolate(), number);
}
v8::Local<v8::Number> CFX_V8::NewNumber(double number) {
return v8::Number::New(GetIsolate(), number);
}
v8::Local<v8::Number> CFX_V8::NewNumber(float number) {
return v8::Number::New(GetIsolate(), (float)number);
}
v8::Local<v8::Boolean> CFX_V8::NewBoolean(bool b) {
return v8::Boolean::New(GetIsolate(), b);
}
v8::Local<v8::String> CFX_V8::NewString(const ByteStringView& str) {
v8::Isolate* pIsolate = m_pIsolate ? GetIsolate() : v8::Isolate::GetCurrent();
return v8::String::NewFromUtf8(pIsolate, str.unterminated_c_str(),
v8::NewStringType::kNormal, str.GetLength())
.ToLocalChecked();
}
v8::Local<v8::String> CFX_V8::NewString(const WideStringView& str) {
// Conversion from pdfium's wchar_t wide-strings to v8's uint16_t
// wide-strings isn't handled by v8, so use UTF8 as a common
// intermediate format.
return NewString(FX_UTF8Encode(str).AsStringView());
}
v8::Local<v8::Value> CFX_V8::NewNull() {
return v8::Null(GetIsolate());
}
v8::Local<v8::Value> CFX_V8::NewUndefined() {
return v8::Undefined(GetIsolate());
}
v8::Local<v8::Date> CFX_V8::NewDate(double d) {
return v8::Date::New(m_pIsolate->GetCurrentContext(), d)
.ToLocalChecked()
.As<v8::Date>();
}
int CFX_V8::ToInt32(v8::Local<v8::Value> pValue) {
if (pValue.IsEmpty())
return 0;
v8::Local<v8::Context> context = m_pIsolate->GetCurrentContext();
v8::MaybeLocal<v8::Int32> maybe_int32 = pValue->ToInt32(context);
if (maybe_int32.IsEmpty())
return 0;
return maybe_int32.ToLocalChecked()->Value();
}
bool CFX_V8::ToBoolean(v8::Local<v8::Value> pValue) {
if (pValue.IsEmpty())
return false;
return pValue->BooleanValue(m_pIsolate.Get());
}
double CFX_V8::ToDouble(v8::Local<v8::Value> pValue) {
if (pValue.IsEmpty())
return 0.0;
v8::Local<v8::Context> context = m_pIsolate->GetCurrentContext();
v8::MaybeLocal<v8::Number> maybe_number = pValue->ToNumber(context);
if (maybe_number.IsEmpty())
return 0.0;
return maybe_number.ToLocalChecked()->Value();
}
WideString CFX_V8::ToWideString(v8::Local<v8::Value> pValue) {
if (pValue.IsEmpty())
return WideString();
v8::Local<v8::Context> context = m_pIsolate->GetCurrentContext();
v8::MaybeLocal<v8::String> maybe_string = pValue->ToString(context);
if (maybe_string.IsEmpty())
return WideString();
v8::String::Utf8Value s(GetIsolate(), maybe_string.ToLocalChecked());
return WideString::FromUTF8(ByteStringView(*s, s.length()));
}
ByteString CFX_V8::ToByteString(v8::Local<v8::Value> pValue) {
if (pValue.IsEmpty())
return ByteString();
v8::Local<v8::Context> context = m_pIsolate->GetCurrentContext();
v8::MaybeLocal<v8::String> maybe_string = pValue->ToString(context);
if (maybe_string.IsEmpty())
return ByteString();
v8::String::Utf8Value s(GetIsolate(), maybe_string.ToLocalChecked());
return ByteString(*s);
}
v8::Local<v8::Object> CFX_V8::ToObject(v8::Local<v8::Value> pValue) {
if (pValue.IsEmpty() || !pValue->IsObject())
return v8::Local<v8::Object>();
v8::Local<v8::Context> context = m_pIsolate->GetCurrentContext();
return pValue->ToObject(context).ToLocalChecked();
}
v8::Local<v8::Array> CFX_V8::ToArray(v8::Local<v8::Value> pValue) {
if (pValue.IsEmpty() || !pValue->IsArray())
return v8::Local<v8::Array>();
v8::Local<v8::Context> context = m_pIsolate->GetCurrentContext();
return v8::Local<v8::Array>::Cast(pValue->ToObject(context).ToLocalChecked());
}
void* CFX_V8ArrayBufferAllocator::Allocate(size_t length) {
if (length > kMaxAllowedBytes)
return nullptr;
return GetArrayBufferPartitionAllocator().root()->AllocFlags(
pdfium::base::PartitionAllocZeroFill, length, "CFX_V8ArrayBuffer");
}
void* CFX_V8ArrayBufferAllocator::AllocateUninitialized(size_t length) {
if (length > kMaxAllowedBytes)
return nullptr;
return GetArrayBufferPartitionAllocator().root()->Alloc(length,
"CFX_V8ArrayBuffer");
}
void CFX_V8ArrayBufferAllocator::Free(void* data, size_t length) {
GetArrayBufferPartitionAllocator().root()->Free(data);
}
|