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