libadblockplus
 All Classes Namespaces Functions Variables Typedefs Enumerations Pages
JsValue.h
1 /*
2  * This file is part of Adblock Plus <https://adblockplus.org/>,
3  * Copyright (C) 2006-present eyeo GmbH
4  *
5  * Adblock Plus is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 3 as
7  * published by the Free Software Foundation.
8  *
9  * Adblock Plus is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef ADBLOCK_PLUS_JS_VALUE_H
19 #define ADBLOCK_PLUS_JS_VALUE_H
20 
21 #include <stdint.h>
22 #include <string>
23 #include <vector>
24 #include <memory>
25 
26 #include <AdblockPlus/IFileSystem.h>
27 
28 namespace v8
29 {
30  class Value;
31  class Object;
32  template<class T> class Local;
33  template<class T> class Global;
34 }
35 
36 namespace AdblockPlus
37 {
38  class JsValue;
39  class JsEngine;
40 
41  typedef std::shared_ptr<JsEngine> JsEnginePtr;
42 
43  typedef IFileSystem::IOBuffer StringBuffer;
44 
48  typedef std::vector<AdblockPlus::JsValue> JsValueList;
49 
54  class JsValue
55  {
56  friend class JsEngine;
57  public:
58  JsValue(JsValue&& src);
59  JsValue(const JsValue& src);
60  virtual ~JsValue();
61 
62  JsValue& operator=(const JsValue& src);
63  JsValue& operator=(JsValue&& src);
64 
65  bool IsUndefined() const;
66  bool IsNull() const;
67  bool IsString() const;
68  bool IsNumber() const;
69  bool IsBool() const;
70  bool IsObject() const;
71  bool IsArray() const;
72  bool IsFunction() const;
73  std::string AsString() const;
74  StringBuffer AsStringBuffer() const;
75  int64_t AsInt() const;
76  bool AsBool() const;
77  JsValueList AsList() const;
78 
83  std::vector<std::string> GetOwnPropertyNames() const;
84 
91  JsValue GetProperty(const std::string& name) const;
92 
94 
99  void SetProperty(const std::string& name, const std::string& val);
100  void SetProperty(const std::string& name, const StringBuffer& val);
101  void SetProperty(const std::string& name, int64_t val);
102  void SetProperty(const std::string& name, bool val);
103  void SetProperty(const std::string& name, const JsValue& value);
104  inline void SetProperty(const std::string& name, const char* val)
105  {
106  SetProperty(name, std::string(val));
107  }
108  inline void SetProperty(const std::string& name, int val)
109  {
110  SetProperty(name, static_cast<int64_t>(val));
111  }
113 
118  void SetStringBufferProperty(const std::string& name, const StringBuffer& val);
119 
127  std::string GetClass() const;
128 
134  JsValue Call(const JsValueList& params = JsValueList()) const;
135 
142  JsValue Call(const JsValueList& params, const AdblockPlus::JsValue& thisValue) const;
143 
150  JsValue Call(const JsValue& arg) const;
151 
152  v8::Local<v8::Value> UnwrapValue() const;
153 
154  protected:
155  JsEnginePtr jsEngine;
156  private:
157  JsValue(JsEnginePtr jsEngine, v8::Local<v8::Value> value);
158  void SetProperty(const std::string& name, v8::Local<v8::Value> val);
159 
160  // Parameter args is not const because a pointer to its internal arrays is
161  // passed to v8::Function::Call but the latter does not expect a const pointer.
162  JsValue Call(std::vector<v8::Local<v8::Value>>& args, v8::Local<v8::Object> thisObj) const;
163 
164  std::unique_ptr<v8::Global<v8::Value>> value;
165  };
166 }
167 
168 #endif
void SetProperty(const std::string &name, const char *val)
Sets a property value if this is an object (see IsObject()).
Definition: JsValue.h:104
void SetStringBufferProperty(const std::string &name, const StringBuffer &val)
Sets a property value string if this is an object (see IsObject()).
std::vector< std::string > GetOwnPropertyNames() const
Returns a list of property names if this is an object (see IsObject()).
JavaScript engine used by FilterEngine, wraps v8.
Definition: JsEngine.h:73
std::vector< AdblockPlus::JsValue > JsValueList
List of JavaScript values.
Definition: JsValue.h:48
void SetProperty(const std::string &name, int val)
Sets a property value if this is an object (see IsObject()).
Definition: JsValue.h:108
Wrapper for JavaScript values.
Definition: JsValue.h:54
JsValue GetProperty(const std::string &name) const
Returns a property value if this is an object (see IsObject()).
void SetProperty(const std::string &name, const std::string &val)
Sets a property value if this is an object (see IsObject()).
Main namespace of libadblockplus.
Definition: ActiveObject.h:23
Definition: JsEngine.h:36
Definition: JsValue.h:33
std::string GetClass() const
Returns the value's class name, e.g. Array for arrays (see IsArray()).
Definition: JsValue.h:32
std::vector< uint8_t > IOBuffer
Type for the buffer used for IO.
Definition: IFileSystem.h:61
JsValue Call(const JsValueList &params=JsValueList()) const
Invokes the value as a function (see IsFunction()).
std::shared_ptr< JsEngine > JsEnginePtr
Shared smart pointer to a JsEngine instance.
Definition: JsEngine.h:48