1 // Copyright 2017 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/cfx_v8.h"
8
9 #include "core/fxcrt/fx_memory.h"
10 #include "fxjs/fxv8.h"
11 #include "third_party/base/allocator/partition_allocator/partition_alloc.h"
12
CFX_V8(v8::Isolate * isolate)13 CFX_V8::CFX_V8(v8::Isolate* isolate) : m_pIsolate(isolate) {}
14
15 CFX_V8::~CFX_V8() = default;
16
GetObjectProperty(v8::Local<v8::Object> pObj,ByteStringView bsUTF8PropertyName)17 v8::Local<v8::Value> CFX_V8::GetObjectProperty(
18 v8::Local<v8::Object> pObj,
19 ByteStringView bsUTF8PropertyName) {
20 return fxv8::ReentrantGetObjectPropertyHelper(GetIsolate(), pObj,
21 bsUTF8PropertyName);
22 }
23
GetObjectPropertyNames(v8::Local<v8::Object> pObj)24 std::vector<WideString> CFX_V8::GetObjectPropertyNames(
25 v8::Local<v8::Object> pObj) {
26 return fxv8::ReentrantGetObjectPropertyNamesHelper(GetIsolate(), pObj);
27 }
28
PutObjectProperty(v8::Local<v8::Object> pObj,ByteStringView bsUTF8PropertyName,v8::Local<v8::Value> pPut)29 void CFX_V8::PutObjectProperty(v8::Local<v8::Object> pObj,
30 ByteStringView bsUTF8PropertyName,
31 v8::Local<v8::Value> pPut) {
32 fxv8::ReentrantPutObjectPropertyHelper(GetIsolate(), pObj, bsUTF8PropertyName,
33 pPut);
34 }
35
DisposeIsolate()36 void CFX_V8::DisposeIsolate() {
37 if (m_pIsolate)
38 m_pIsolate.Release()->Dispose();
39 }
40
NewArray()41 v8::Local<v8::Array> CFX_V8::NewArray() {
42 return fxv8::NewArrayHelper(GetIsolate());
43 }
44
NewObject()45 v8::Local<v8::Object> CFX_V8::NewObject() {
46 return fxv8::NewObjectHelper(GetIsolate());
47 }
48
PutArrayElement(v8::Local<v8::Array> pArray,unsigned index,v8::Local<v8::Value> pValue)49 void CFX_V8::PutArrayElement(v8::Local<v8::Array> pArray,
50 unsigned index,
51 v8::Local<v8::Value> pValue) {
52 fxv8::ReentrantPutArrayElementHelper(GetIsolate(), pArray, index, pValue);
53 }
54
GetArrayElement(v8::Local<v8::Array> pArray,unsigned index)55 v8::Local<v8::Value> CFX_V8::GetArrayElement(v8::Local<v8::Array> pArray,
56 unsigned index) {
57 return fxv8::ReentrantGetArrayElementHelper(GetIsolate(), pArray, index);
58 }
59
GetArrayLength(v8::Local<v8::Array> pArray)60 unsigned CFX_V8::GetArrayLength(v8::Local<v8::Array> pArray) {
61 return fxv8::GetArrayLengthHelper(pArray);
62 }
63
NewNumber(int number)64 v8::Local<v8::Number> CFX_V8::NewNumber(int number) {
65 return fxv8::NewNumberHelper(GetIsolate(), number);
66 }
67
NewNumber(double number)68 v8::Local<v8::Number> CFX_V8::NewNumber(double number) {
69 return fxv8::NewNumberHelper(GetIsolate(), number);
70 }
71
NewNumber(float number)72 v8::Local<v8::Number> CFX_V8::NewNumber(float number) {
73 return fxv8::NewNumberHelper(GetIsolate(), number);
74 }
75
NewBoolean(bool b)76 v8::Local<v8::Boolean> CFX_V8::NewBoolean(bool b) {
77 return fxv8::NewBooleanHelper(GetIsolate(), b);
78 }
79
NewString(ByteStringView str)80 v8::Local<v8::String> CFX_V8::NewString(ByteStringView str) {
81 v8::Isolate* pIsolate = m_pIsolate ? GetIsolate() : v8::Isolate::GetCurrent();
82 return fxv8::NewStringHelper(pIsolate, str);
83 }
84
NewString(WideStringView str)85 v8::Local<v8::String> CFX_V8::NewString(WideStringView str) {
86 // Conversion from pdfium's wchar_t wide-strings to v8's uint16_t
87 // wide-strings isn't handled by v8, so use UTF8 as a common
88 // intermediate format.
89 return NewString(FX_UTF8Encode(str).AsStringView());
90 }
91
NewNull()92 v8::Local<v8::Value> CFX_V8::NewNull() {
93 return fxv8::NewNullHelper(GetIsolate());
94 }
95
NewUndefined()96 v8::Local<v8::Value> CFX_V8::NewUndefined() {
97 return fxv8::NewUndefinedHelper(GetIsolate());
98 }
99
NewDate(double d)100 v8::Local<v8::Date> CFX_V8::NewDate(double d) {
101 return fxv8::NewDateHelper(GetIsolate(), d);
102 }
103
ToInt32(v8::Local<v8::Value> pValue)104 int CFX_V8::ToInt32(v8::Local<v8::Value> pValue) {
105 return fxv8::ReentrantToInt32Helper(GetIsolate(), pValue);
106 }
107
ToBoolean(v8::Local<v8::Value> pValue)108 bool CFX_V8::ToBoolean(v8::Local<v8::Value> pValue) {
109 return fxv8::ReentrantToBooleanHelper(GetIsolate(), pValue);
110 }
111
ToDouble(v8::Local<v8::Value> pValue)112 double CFX_V8::ToDouble(v8::Local<v8::Value> pValue) {
113 return fxv8::ReentrantToDoubleHelper(GetIsolate(), pValue);
114 }
115
ToWideString(v8::Local<v8::Value> pValue)116 WideString CFX_V8::ToWideString(v8::Local<v8::Value> pValue) {
117 return fxv8::ReentrantToWideStringHelper(GetIsolate(), pValue);
118 }
119
ToByteString(v8::Local<v8::Value> pValue)120 ByteString CFX_V8::ToByteString(v8::Local<v8::Value> pValue) {
121 return fxv8::ReentrantToByteStringHelper(GetIsolate(), pValue);
122 }
123
ToObject(v8::Local<v8::Value> pValue)124 v8::Local<v8::Object> CFX_V8::ToObject(v8::Local<v8::Value> pValue) {
125 return fxv8::ReentrantToObjectHelper(GetIsolate(), pValue);
126 }
127
ToArray(v8::Local<v8::Value> pValue)128 v8::Local<v8::Array> CFX_V8::ToArray(v8::Local<v8::Value> pValue) {
129 return fxv8::ReentrantToArrayHelper(GetIsolate(), pValue);
130 }
131
Allocate(size_t length)132 void* CFX_V8ArrayBufferAllocator::Allocate(size_t length) {
133 if (length > kMaxAllowedBytes)
134 return nullptr;
135 return GetArrayBufferPartitionAllocator().root()->AllocFlags(
136 pdfium::base::PartitionAllocZeroFill, length, "CFX_V8ArrayBuffer");
137 }
138
AllocateUninitialized(size_t length)139 void* CFX_V8ArrayBufferAllocator::AllocateUninitialized(size_t length) {
140 if (length > kMaxAllowedBytes)
141 return nullptr;
142 return GetArrayBufferPartitionAllocator().root()->Alloc(length,
143 "CFX_V8ArrayBuffer");
144 }
145
Free(void * data,size_t length)146 void CFX_V8ArrayBufferAllocator::Free(void* data, size_t length) {
147 GetArrayBufferPartitionAllocator().root()->Free(data);
148 }
149