xref: /aosp_15_r20/external/pdfium/fxjs/cfx_v8.cpp (revision 3ac0a46f773bac49fa9476ec2b1cf3f8da5ec3a4)
1*3ac0a46fSAndroid Build Coastguard Worker // Copyright 2017 The PDFium Authors
2*3ac0a46fSAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*3ac0a46fSAndroid Build Coastguard Worker // found in the LICENSE file.
4*3ac0a46fSAndroid Build Coastguard Worker 
5*3ac0a46fSAndroid Build Coastguard Worker // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6*3ac0a46fSAndroid Build Coastguard Worker 
7*3ac0a46fSAndroid Build Coastguard Worker #include "fxjs/cfx_v8.h"
8*3ac0a46fSAndroid Build Coastguard Worker 
9*3ac0a46fSAndroid Build Coastguard Worker #include "fxjs/fxv8.h"
10*3ac0a46fSAndroid Build Coastguard Worker #include "v8/include/v8-isolate.h"
11*3ac0a46fSAndroid Build Coastguard Worker 
CFX_V8(v8::Isolate * isolate)12*3ac0a46fSAndroid Build Coastguard Worker CFX_V8::CFX_V8(v8::Isolate* isolate) : m_pIsolate(isolate) {}
13*3ac0a46fSAndroid Build Coastguard Worker 
14*3ac0a46fSAndroid Build Coastguard Worker CFX_V8::~CFX_V8() = default;
15*3ac0a46fSAndroid Build Coastguard Worker 
GetObjectProperty(v8::Local<v8::Object> pObj,ByteStringView bsUTF8PropertyName)16*3ac0a46fSAndroid Build Coastguard Worker v8::Local<v8::Value> CFX_V8::GetObjectProperty(
17*3ac0a46fSAndroid Build Coastguard Worker     v8::Local<v8::Object> pObj,
18*3ac0a46fSAndroid Build Coastguard Worker     ByteStringView bsUTF8PropertyName) {
19*3ac0a46fSAndroid Build Coastguard Worker   return fxv8::ReentrantGetObjectPropertyHelper(GetIsolate(), pObj,
20*3ac0a46fSAndroid Build Coastguard Worker                                                 bsUTF8PropertyName);
21*3ac0a46fSAndroid Build Coastguard Worker }
22*3ac0a46fSAndroid Build Coastguard Worker 
GetObjectPropertyNames(v8::Local<v8::Object> pObj)23*3ac0a46fSAndroid Build Coastguard Worker std::vector<WideString> CFX_V8::GetObjectPropertyNames(
24*3ac0a46fSAndroid Build Coastguard Worker     v8::Local<v8::Object> pObj) {
25*3ac0a46fSAndroid Build Coastguard Worker   return fxv8::ReentrantGetObjectPropertyNamesHelper(GetIsolate(), pObj);
26*3ac0a46fSAndroid Build Coastguard Worker }
27*3ac0a46fSAndroid Build Coastguard Worker 
PutObjectProperty(v8::Local<v8::Object> pObj,ByteStringView bsUTF8PropertyName,v8::Local<v8::Value> pPut)28*3ac0a46fSAndroid Build Coastguard Worker void CFX_V8::PutObjectProperty(v8::Local<v8::Object> pObj,
29*3ac0a46fSAndroid Build Coastguard Worker                                ByteStringView bsUTF8PropertyName,
30*3ac0a46fSAndroid Build Coastguard Worker                                v8::Local<v8::Value> pPut) {
31*3ac0a46fSAndroid Build Coastguard Worker   fxv8::ReentrantPutObjectPropertyHelper(GetIsolate(), pObj, bsUTF8PropertyName,
32*3ac0a46fSAndroid Build Coastguard Worker                                          pPut);
33*3ac0a46fSAndroid Build Coastguard Worker }
34*3ac0a46fSAndroid Build Coastguard Worker 
DisposeIsolate()35*3ac0a46fSAndroid Build Coastguard Worker void CFX_V8::DisposeIsolate() {
36*3ac0a46fSAndroid Build Coastguard Worker   if (m_pIsolate)
37*3ac0a46fSAndroid Build Coastguard Worker     m_pIsolate.ExtractAsDangling()->Dispose();
38*3ac0a46fSAndroid Build Coastguard Worker }
39*3ac0a46fSAndroid Build Coastguard Worker 
NewArray()40*3ac0a46fSAndroid Build Coastguard Worker v8::Local<v8::Array> CFX_V8::NewArray() {
41*3ac0a46fSAndroid Build Coastguard Worker   return fxv8::NewArrayHelper(GetIsolate());
42*3ac0a46fSAndroid Build Coastguard Worker }
43*3ac0a46fSAndroid Build Coastguard Worker 
NewObject()44*3ac0a46fSAndroid Build Coastguard Worker v8::Local<v8::Object> CFX_V8::NewObject() {
45*3ac0a46fSAndroid Build Coastguard Worker   return fxv8::NewObjectHelper(GetIsolate());
46*3ac0a46fSAndroid Build Coastguard Worker }
47*3ac0a46fSAndroid Build Coastguard Worker 
PutArrayElement(v8::Local<v8::Array> pArray,size_t index,v8::Local<v8::Value> pValue)48*3ac0a46fSAndroid Build Coastguard Worker void CFX_V8::PutArrayElement(v8::Local<v8::Array> pArray,
49*3ac0a46fSAndroid Build Coastguard Worker                              size_t index,
50*3ac0a46fSAndroid Build Coastguard Worker                              v8::Local<v8::Value> pValue) {
51*3ac0a46fSAndroid Build Coastguard Worker   fxv8::ReentrantPutArrayElementHelper(GetIsolate(), pArray, index, pValue);
52*3ac0a46fSAndroid Build Coastguard Worker }
53*3ac0a46fSAndroid Build Coastguard Worker 
GetArrayElement(v8::Local<v8::Array> pArray,size_t index)54*3ac0a46fSAndroid Build Coastguard Worker v8::Local<v8::Value> CFX_V8::GetArrayElement(v8::Local<v8::Array> pArray,
55*3ac0a46fSAndroid Build Coastguard Worker                                              size_t index) {
56*3ac0a46fSAndroid Build Coastguard Worker   return fxv8::ReentrantGetArrayElementHelper(GetIsolate(), pArray, index);
57*3ac0a46fSAndroid Build Coastguard Worker }
58*3ac0a46fSAndroid Build Coastguard Worker 
GetArrayLength(v8::Local<v8::Array> pArray)59*3ac0a46fSAndroid Build Coastguard Worker size_t CFX_V8::GetArrayLength(v8::Local<v8::Array> pArray) {
60*3ac0a46fSAndroid Build Coastguard Worker   return fxv8::GetArrayLengthHelper(pArray);
61*3ac0a46fSAndroid Build Coastguard Worker }
62*3ac0a46fSAndroid Build Coastguard Worker 
NewNumber(int number)63*3ac0a46fSAndroid Build Coastguard Worker v8::Local<v8::Number> CFX_V8::NewNumber(int number) {
64*3ac0a46fSAndroid Build Coastguard Worker   return fxv8::NewNumberHelper(GetIsolate(), number);
65*3ac0a46fSAndroid Build Coastguard Worker }
66*3ac0a46fSAndroid Build Coastguard Worker 
NewNumber(double number)67*3ac0a46fSAndroid Build Coastguard Worker v8::Local<v8::Number> CFX_V8::NewNumber(double number) {
68*3ac0a46fSAndroid Build Coastguard Worker   return fxv8::NewNumberHelper(GetIsolate(), number);
69*3ac0a46fSAndroid Build Coastguard Worker }
70*3ac0a46fSAndroid Build Coastguard Worker 
NewNumber(float number)71*3ac0a46fSAndroid Build Coastguard Worker v8::Local<v8::Number> CFX_V8::NewNumber(float number) {
72*3ac0a46fSAndroid Build Coastguard Worker   return fxv8::NewNumberHelper(GetIsolate(), number);
73*3ac0a46fSAndroid Build Coastguard Worker }
74*3ac0a46fSAndroid Build Coastguard Worker 
NewBoolean(bool b)75*3ac0a46fSAndroid Build Coastguard Worker v8::Local<v8::Boolean> CFX_V8::NewBoolean(bool b) {
76*3ac0a46fSAndroid Build Coastguard Worker   return fxv8::NewBooleanHelper(GetIsolate(), b);
77*3ac0a46fSAndroid Build Coastguard Worker }
78*3ac0a46fSAndroid Build Coastguard Worker 
NewString(ByteStringView str)79*3ac0a46fSAndroid Build Coastguard Worker v8::Local<v8::String> CFX_V8::NewString(ByteStringView str) {
80*3ac0a46fSAndroid Build Coastguard Worker   return fxv8::NewStringHelper(GetIsolate(), str);
81*3ac0a46fSAndroid Build Coastguard Worker }
82*3ac0a46fSAndroid Build Coastguard Worker 
NewString(WideStringView str)83*3ac0a46fSAndroid Build Coastguard Worker v8::Local<v8::String> CFX_V8::NewString(WideStringView str) {
84*3ac0a46fSAndroid Build Coastguard Worker   // Conversion from pdfium's wchar_t wide-strings to v8's uint16_t
85*3ac0a46fSAndroid Build Coastguard Worker   // wide-strings isn't handled by v8, so use UTF8 as a common
86*3ac0a46fSAndroid Build Coastguard Worker   // intermediate format.
87*3ac0a46fSAndroid Build Coastguard Worker   return NewString(FX_UTF8Encode(str).AsStringView());
88*3ac0a46fSAndroid Build Coastguard Worker }
89*3ac0a46fSAndroid Build Coastguard Worker 
NewNull()90*3ac0a46fSAndroid Build Coastguard Worker v8::Local<v8::Value> CFX_V8::NewNull() {
91*3ac0a46fSAndroid Build Coastguard Worker   return fxv8::NewNullHelper(GetIsolate());
92*3ac0a46fSAndroid Build Coastguard Worker }
93*3ac0a46fSAndroid Build Coastguard Worker 
NewUndefined()94*3ac0a46fSAndroid Build Coastguard Worker v8::Local<v8::Value> CFX_V8::NewUndefined() {
95*3ac0a46fSAndroid Build Coastguard Worker   return fxv8::NewUndefinedHelper(GetIsolate());
96*3ac0a46fSAndroid Build Coastguard Worker }
97*3ac0a46fSAndroid Build Coastguard Worker 
NewDate(double d)98*3ac0a46fSAndroid Build Coastguard Worker v8::Local<v8::Date> CFX_V8::NewDate(double d) {
99*3ac0a46fSAndroid Build Coastguard Worker   return fxv8::NewDateHelper(GetIsolate(), d);
100*3ac0a46fSAndroid Build Coastguard Worker }
101*3ac0a46fSAndroid Build Coastguard Worker 
ToInt32(v8::Local<v8::Value> pValue)102*3ac0a46fSAndroid Build Coastguard Worker int CFX_V8::ToInt32(v8::Local<v8::Value> pValue) {
103*3ac0a46fSAndroid Build Coastguard Worker   return fxv8::ReentrantToInt32Helper(GetIsolate(), pValue);
104*3ac0a46fSAndroid Build Coastguard Worker }
105*3ac0a46fSAndroid Build Coastguard Worker 
ToBoolean(v8::Local<v8::Value> pValue)106*3ac0a46fSAndroid Build Coastguard Worker bool CFX_V8::ToBoolean(v8::Local<v8::Value> pValue) {
107*3ac0a46fSAndroid Build Coastguard Worker   return fxv8::ReentrantToBooleanHelper(GetIsolate(), pValue);
108*3ac0a46fSAndroid Build Coastguard Worker }
109*3ac0a46fSAndroid Build Coastguard Worker 
ToDouble(v8::Local<v8::Value> pValue)110*3ac0a46fSAndroid Build Coastguard Worker double CFX_V8::ToDouble(v8::Local<v8::Value> pValue) {
111*3ac0a46fSAndroid Build Coastguard Worker   return fxv8::ReentrantToDoubleHelper(GetIsolate(), pValue);
112*3ac0a46fSAndroid Build Coastguard Worker }
113*3ac0a46fSAndroid Build Coastguard Worker 
ToWideString(v8::Local<v8::Value> pValue)114*3ac0a46fSAndroid Build Coastguard Worker WideString CFX_V8::ToWideString(v8::Local<v8::Value> pValue) {
115*3ac0a46fSAndroid Build Coastguard Worker   return fxv8::ReentrantToWideStringHelper(GetIsolate(), pValue);
116*3ac0a46fSAndroid Build Coastguard Worker }
117*3ac0a46fSAndroid Build Coastguard Worker 
ToByteString(v8::Local<v8::Value> pValue)118*3ac0a46fSAndroid Build Coastguard Worker ByteString CFX_V8::ToByteString(v8::Local<v8::Value> pValue) {
119*3ac0a46fSAndroid Build Coastguard Worker   return fxv8::ReentrantToByteStringHelper(GetIsolate(), pValue);
120*3ac0a46fSAndroid Build Coastguard Worker }
121*3ac0a46fSAndroid Build Coastguard Worker 
ToObject(v8::Local<v8::Value> pValue)122*3ac0a46fSAndroid Build Coastguard Worker v8::Local<v8::Object> CFX_V8::ToObject(v8::Local<v8::Value> pValue) {
123*3ac0a46fSAndroid Build Coastguard Worker   return fxv8::ReentrantToObjectHelper(GetIsolate(), pValue);
124*3ac0a46fSAndroid Build Coastguard Worker }
125*3ac0a46fSAndroid Build Coastguard Worker 
ToArray(v8::Local<v8::Value> pValue)126*3ac0a46fSAndroid Build Coastguard Worker v8::Local<v8::Array> CFX_V8::ToArray(v8::Local<v8::Value> pValue) {
127*3ac0a46fSAndroid Build Coastguard Worker   return fxv8::ReentrantToArrayHelper(GetIsolate(), pValue);
128*3ac0a46fSAndroid Build Coastguard Worker }
129*3ac0a46fSAndroid Build Coastguard Worker 
operator ()(v8::Isolate * ptr)130*3ac0a46fSAndroid Build Coastguard Worker void CFX_V8IsolateDeleter::operator()(v8::Isolate* ptr) {
131*3ac0a46fSAndroid Build Coastguard Worker   ptr->Dispose();
132*3ac0a46fSAndroid Build Coastguard Worker }
133