blob: 44cc58ca49bbb2f24edda912979afebf5bd337ae [file] [log] [blame]
dsinclair08fea802016-07-12 10:37:52 -07001// Copyright 2014 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
Tom Sepez70e52142018-12-13 20:07:50 +00007#ifndef FXJS_XFA_CFXJSE_VALUE_H_
8#define FXJS_XFA_CFXJSE_VALUE_H_
dsinclair08fea802016-07-12 10:37:52 -07009
Dan Sinclair76a44de2017-01-11 08:58:35 -050010#include <memory>
11#include <vector>
12
dsinclaira52ab742016-09-29 13:59:29 -070013#include "core/fxcrt/fx_string.h"
14#include "core/fxcrt/fx_system.h"
Tom Sepez98b356a2018-07-16 21:35:06 +000015#include "core/fxcrt/unowned_ptr.h"
dsinclair43554682016-09-29 17:29:48 -070016#include "v8/include/v8.h"
dsinclair08fea802016-07-12 10:37:52 -070017
18class CFXJSE_Class;
19class CFXJSE_HostObject;
20
21class CFXJSE_Value {
22 public:
23 explicit CFXJSE_Value(v8::Isolate* pIsolate);
24 ~CFXJSE_Value();
25
Tom Sepez47617702019-01-23 21:55:41 +000026 bool IsEmpty() const;
tsepez304bb912016-11-03 06:10:26 -070027 bool IsUndefined() const;
28 bool IsNull() const;
29 bool IsBoolean() const;
30 bool IsString() const;
31 bool IsNumber() const;
32 bool IsInteger() const;
33 bool IsObject() const;
34 bool IsArray() const;
35 bool IsFunction() const;
tsepez304bb912016-11-03 06:10:26 -070036 bool ToBoolean() const;
Dan Sinclair05df0752017-03-14 14:43:42 -040037 float ToFloat() const;
dsinclair08fea802016-07-12 10:37:52 -070038 double ToDouble() const;
39 int32_t ToInteger() const;
Ryan Harrison275e2602017-09-18 14:23:18 -040040 ByteString ToString() const;
41 WideString ToWideString() const {
42 return WideString::FromUTF8(ToString().AsStringView());
dsinclair08fea802016-07-12 10:37:52 -070043 }
Tom Sepez4c358482018-07-23 20:41:04 +000044 CFXJSE_HostObject* ToHostObject() const;
dsinclair08fea802016-07-12 10:37:52 -070045
46 void SetUndefined();
47 void SetNull();
tsepez304bb912016-11-03 06:10:26 -070048 void SetBoolean(bool bBoolean);
dsinclair08fea802016-07-12 10:37:52 -070049 void SetInteger(int32_t nInteger);
50 void SetDouble(double dDouble);
Tom Sepezc839ac72018-12-14 20:34:11 +000051 void SetString(ByteStringView szString);
Dan Sinclair05df0752017-03-14 14:43:42 -040052 void SetFloat(float fFloat);
dsinclair08fea802016-07-12 10:37:52 -070053
Tom Sepez7e4877f2019-04-05 21:32:36 +000054 void SetHostObject(CFXJSE_HostObject* lpObject, CFXJSE_Class* pClass);
55 void ClearHostObject();
56
Dan Sinclair76a44de2017-01-11 08:58:35 -050057 void SetArray(const std::vector<std::unique_ptr<CFXJSE_Value>>& values);
dsinclair08fea802016-07-12 10:37:52 -070058
Tom Sepezc839ac72018-12-14 20:34:11 +000059 bool GetObjectProperty(ByteStringView szPropName, CFXJSE_Value* lpPropValue);
60 bool SetObjectProperty(ByteStringView szPropName, CFXJSE_Value* lpPropValue);
tsepez304bb912016-11-03 06:10:26 -070061 bool GetObjectPropertyByIdx(uint32_t uPropIdx, CFXJSE_Value* lpPropValue);
Tom Sepezc839ac72018-12-14 20:34:11 +000062 bool DeleteObjectProperty(ByteStringView szPropName);
63 bool HasObjectOwnProperty(ByteStringView szPropName, bool bUseTypeGetter);
64 bool SetObjectOwnProperty(ByteStringView szPropName,
dsinclair08fea802016-07-12 10:37:52 -070065 CFXJSE_Value* lpPropValue);
tsepez304bb912016-11-03 06:10:26 -070066 bool SetFunctionBind(CFXJSE_Value* lpOldFunction, CFXJSE_Value* lpNewThis);
dsinclair08fea802016-07-12 10:37:52 -070067
Tom Sepez98b356a2018-07-16 21:35:06 +000068 v8::Isolate* GetIsolate() const { return m_pIsolate.Get(); }
dsinclair08fea802016-07-12 10:37:52 -070069 const v8::Global<v8::Value>& DirectGetValue() const { return m_hValue; }
70 void ForceSetValue(v8::Local<v8::Value> hValue) {
Tom Sepez98b356a2018-07-16 21:35:06 +000071 m_hValue.Reset(GetIsolate(), hValue);
dsinclair08fea802016-07-12 10:37:52 -070072 }
73 void Assign(const CFXJSE_Value* lpValue) {
74 ASSERT(lpValue);
75 if (lpValue) {
Tom Sepez98b356a2018-07-16 21:35:06 +000076 m_hValue.Reset(GetIsolate(), lpValue->m_hValue);
dsinclair08fea802016-07-12 10:37:52 -070077 } else {
78 m_hValue.Reset();
79 }
80 }
81
82 private:
Lei Zhangda379c72018-08-28 23:12:22 +000083 CFXJSE_Value() = delete;
84 CFXJSE_Value(const CFXJSE_Value&) = delete;
85 CFXJSE_Value& operator=(const CFXJSE_Value&) = delete;
dsinclair08fea802016-07-12 10:37:52 -070086
Lei Zhangda379c72018-08-28 23:12:22 +000087 UnownedPtr<v8::Isolate> const m_pIsolate;
dsinclair08fea802016-07-12 10:37:52 -070088 v8::Global<v8::Value> m_hValue;
89};
90
Tom Sepez70e52142018-12-13 20:07:50 +000091#endif // FXJS_XFA_CFXJSE_VALUE_H_