1 // Copyright 2014 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_XFA_CFXJSE_VALUE_H_ 8 #define FXJS_XFA_CFXJSE_VALUE_H_ 9 10 #include <stdint.h> 11 12 #include <memory> 13 #include <vector> 14 15 #include "core/fxcrt/fx_string.h" 16 #include "core/fxcrt/unowned_ptr.h" 17 #include "third_party/base/check.h" 18 #include "v8/include/v8-forward.h" 19 #include "v8/include/v8-persistent-handle.h" 20 21 class CFXJSE_Class; 22 class CFXJSE_HostObject; 23 24 class CFXJSE_Value { 25 public: 26 CFXJSE_Value(); 27 CFXJSE_Value(v8::Isolate* pIsolate, v8::Local<v8::Value> value); 28 ~CFXJSE_Value(); 29 30 bool IsEmpty() const; 31 bool IsUndefined(v8::Isolate* pIsolate) const; 32 bool IsNull(v8::Isolate* pIsolate) const; 33 bool IsBoolean(v8::Isolate* pIsolate) const; 34 bool IsString(v8::Isolate* pIsolate) const; 35 bool IsNumber(v8::Isolate* pIsolate) const; 36 bool IsInteger(v8::Isolate* pIsolate) const; 37 bool IsObject(v8::Isolate* pIsolate) const; 38 bool IsArray(v8::Isolate* pIsolate) const; 39 bool IsFunction(v8::Isolate* pIsolate) const; 40 bool ToBoolean(v8::Isolate* pIsolate) const; 41 float ToFloat(v8::Isolate* pIsolate) const; 42 double ToDouble(v8::Isolate* pIsolate) const; 43 int32_t ToInteger(v8::Isolate* pIsolate) const; 44 ByteString ToString(v8::Isolate* pIsolate) const; ToWideString(v8::Isolate * pIsolate)45 WideString ToWideString(v8::Isolate* pIsolate) const { 46 return WideString::FromUTF8(ToString(pIsolate).AsStringView()); 47 } 48 CFXJSE_HostObject* ToHostObject(v8::Isolate* pIsolate) const; 49 50 void SetUndefined(v8::Isolate* pIsolate); 51 void SetNull(v8::Isolate* pIsolate); 52 void SetBoolean(v8::Isolate* pIsolate, bool bBoolean); 53 void SetInteger(v8::Isolate* pIsolate, int32_t nInteger); 54 void SetDouble(v8::Isolate* pIsolate, double dDouble); 55 void SetString(v8::Isolate* pIsolate, ByteStringView szString); 56 void SetFloat(v8::Isolate* pIsolate, float fFloat); 57 58 void SetHostObject(v8::Isolate* pIsolate, 59 CFXJSE_HostObject* pObject, 60 CFXJSE_Class* pClass); 61 62 void SetArray(v8::Isolate* pIsolate, 63 const std::vector<std::unique_ptr<CFXJSE_Value>>& values); 64 65 bool GetObjectProperty(v8::Isolate* pIsolate, 66 ByteStringView szPropName, 67 CFXJSE_Value* pPropValue); 68 bool SetObjectProperty(v8::Isolate* pIsolate, 69 ByteStringView szPropName, 70 CFXJSE_Value* pPropValue); 71 bool GetObjectPropertyByIdx(v8::Isolate* pIsolate, 72 uint32_t uPropIdx, 73 CFXJSE_Value* pPropValue); 74 void DeleteObjectProperty(v8::Isolate* pIsolate, ByteStringView szPropName); 75 bool SetObjectOwnProperty(v8::Isolate* pIsolate, 76 ByteStringView szPropName, 77 CFXJSE_Value* pPropValue); 78 79 // Return empty local on error. 80 static v8::Local<v8::Function> NewBoundFunction( 81 v8::Isolate* pIsolate, 82 v8::Local<v8::Function> hOldFunction, 83 v8::Local<v8::Object> lpNewThis); 84 85 v8::Local<v8::Value> GetValue(v8::Isolate* pIsolate) const; DirectGetValue()86 const v8::Global<v8::Value>& DirectGetValue() const { return m_hValue; } ForceSetValue(v8::Isolate * pIsolate,v8::Local<v8::Value> hValue)87 void ForceSetValue(v8::Isolate* pIsolate, v8::Local<v8::Value> hValue) { 88 m_hValue.Reset(pIsolate, hValue); 89 } 90 91 private: 92 CFXJSE_Value(const CFXJSE_Value&) = delete; 93 CFXJSE_Value& operator=(const CFXJSE_Value&) = delete; 94 95 v8::Global<v8::Value> m_hValue; 96 }; 97 98 #endif // FXJS_XFA_CFXJSE_VALUE_H_ 99