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 #ifndef FXJS_CFX_V8_H_ 8 #define FXJS_CFX_V8_H_ 9 10 #include <stddef.h> 11 12 #include <vector> 13 14 #include "core/fxcrt/fx_string.h" 15 #include "core/fxcrt/unowned_ptr.h" 16 #include "v8/include/v8-forward.h" 17 18 class CFX_V8 { 19 public: 20 explicit CFX_V8(v8::Isolate* pIsolate); 21 virtual ~CFX_V8(); 22 GetIsolate()23 v8::Isolate* GetIsolate() const { return m_pIsolate; } 24 25 v8::Local<v8::Value> NewNull(); 26 v8::Local<v8::Value> NewUndefined(); 27 v8::Local<v8::Array> NewArray(); 28 v8::Local<v8::Object> NewObject(); 29 v8::Local<v8::Number> NewNumber(int number); 30 v8::Local<v8::Number> NewNumber(double number); 31 v8::Local<v8::Number> NewNumber(float number); 32 v8::Local<v8::Boolean> NewBoolean(bool b); 33 v8::Local<v8::String> NewString(ByteStringView str); 34 v8::Local<v8::String> NewString(WideStringView str); 35 v8::Local<v8::Date> NewDate(double d); 36 37 int ToInt32(v8::Local<v8::Value> pValue); 38 bool ToBoolean(v8::Local<v8::Value> pValue); 39 double ToDouble(v8::Local<v8::Value> pValue); 40 WideString ToWideString(v8::Local<v8::Value> pValue); 41 ByteString ToByteString(v8::Local<v8::Value> pValue); 42 v8::Local<v8::Object> ToObject(v8::Local<v8::Value> pValue); 43 v8::Local<v8::Array> ToArray(v8::Local<v8::Value> pValue); 44 45 // Arrays. 46 size_t GetArrayLength(v8::Local<v8::Array> pArray); 47 v8::Local<v8::Value> GetArrayElement(v8::Local<v8::Array> pArray, 48 size_t index); 49 void PutArrayElement(v8::Local<v8::Array> pArray, 50 size_t index, 51 v8::Local<v8::Value> pValue); 52 53 // Objects. 54 std::vector<WideString> GetObjectPropertyNames(v8::Local<v8::Object> pObj); 55 v8::Local<v8::Value> GetObjectProperty(v8::Local<v8::Object> pObj, 56 ByteStringView bsUTF8PropertyName); 57 void PutObjectProperty(v8::Local<v8::Object> pObj, 58 ByteStringView bsUTF8PropertyName, 59 v8::Local<v8::Value> pValue); 60 61 protected: SetIsolate(v8::Isolate * pIsolate)62 void SetIsolate(v8::Isolate* pIsolate) { m_pIsolate = pIsolate; } 63 void DisposeIsolate(); 64 65 private: 66 UnownedPtr<v8::Isolate> m_pIsolate; 67 }; 68 69 // Use with std::unique_ptr<v8::Isolate> to dispose of isolates correctly. 70 struct CFX_V8IsolateDeleter { 71 void operator()(v8::Isolate* ptr); 72 }; 73 74 #endif // FXJS_CFX_V8_H_ 75