OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-2017 eyeo GmbH | 3 * Copyright (C) 2006-2017 eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 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 | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 * GNU General Public License for more details. | 12 * GNU General Public License for more details. |
13 * | 13 * |
14 * You should have received a copy of the GNU General Public License | 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/>. | 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
16 */ | 16 */ |
17 | 17 |
18 #ifndef ADBLOCK_PLUS_JS_VALUE_H | 18 #ifndef ADBLOCK_PLUS_JS_VALUE_H |
19 #define ADBLOCK_PLUS_JS_VALUE_H | 19 #define ADBLOCK_PLUS_JS_VALUE_H |
20 | 20 |
21 #include <stdint.h> | 21 #include <stdint.h> |
22 #include <string> | 22 #include <string> |
23 #include <vector> | 23 #include <vector> |
24 #include <memory> | 24 #include <memory> |
25 | 25 |
| 26 #include <AdblockPlus/IFileSystem.h> |
| 27 |
26 namespace v8 | 28 namespace v8 |
27 { | 29 { |
28 class Value; | 30 class Value; |
29 class Object; | 31 class Object; |
30 template<class T> class Handle; | |
31 template<class T> class Local; | 32 template<class T> class Local; |
32 template<class T> class Persistent; | 33 template<class T> class Global; |
33 } | 34 } |
34 | 35 |
35 namespace AdblockPlus | 36 namespace AdblockPlus |
36 { | 37 { |
37 class JsValue; | 38 class JsValue; |
38 class JsEngine; | 39 class JsEngine; |
39 | 40 |
40 typedef std::shared_ptr<JsEngine> JsEnginePtr; | 41 typedef std::shared_ptr<JsEngine> JsEnginePtr; |
41 | 42 |
| 43 typedef IFileSystem::IOBuffer StringBuffer; |
| 44 |
42 /** | 45 /** |
43 * List of JavaScript values. | 46 * List of JavaScript values. |
44 */ | 47 */ |
45 typedef std::vector<AdblockPlus::JsValue> JsValueList; | 48 typedef std::vector<AdblockPlus::JsValue> JsValueList; |
46 | 49 |
47 /** | 50 /** |
48 * Wrapper for JavaScript values. | 51 * Wrapper for JavaScript values. |
49 * See `JsEngine` for creating `JsValue` objects. | 52 * See `JsEngine` for creating `JsValue` objects. |
50 */ | 53 */ |
51 class JsValue | 54 class JsValue |
52 { | 55 { |
53 friend class JsEngine; | 56 friend class JsEngine; |
54 public: | 57 public: |
55 JsValue(JsValue&& src); | 58 JsValue(JsValue&& src); |
56 JsValue(const JsValue& src); | 59 JsValue(const JsValue& src); |
57 virtual ~JsValue(); | 60 virtual ~JsValue(); |
58 | 61 |
59 JsValue& operator=(const JsValue& src); | 62 JsValue& operator=(const JsValue& src); |
| 63 JsValue& operator=(JsValue&& src); |
60 | 64 |
61 bool IsUndefined() const; | 65 bool IsUndefined() const; |
62 bool IsNull() const; | 66 bool IsNull() const; |
63 bool IsString() const; | 67 bool IsString() const; |
64 bool IsNumber() const; | 68 bool IsNumber() const; |
65 bool IsBool() const; | 69 bool IsBool() const; |
66 bool IsObject() const; | 70 bool IsObject() const; |
67 bool IsArray() const; | 71 bool IsArray() const; |
68 bool IsFunction() const; | 72 bool IsFunction() const; |
69 std::string AsString() const; | 73 std::string AsString() const; |
| 74 StringBuffer AsStringBuffer() const; |
70 int64_t AsInt() const; | 75 int64_t AsInt() const; |
71 bool AsBool() const; | 76 bool AsBool() const; |
72 JsValueList AsList() const; | 77 JsValueList AsList() const; |
73 | 78 |
74 /** | 79 /** |
75 * Returns a list of property names if this is an object (see `IsObject()`). | 80 * Returns a list of property names if this is an object (see `IsObject()`). |
76 * @return List of property names. | 81 * @return List of property names. |
77 */ | 82 */ |
78 std::vector<std::string> GetOwnPropertyNames() const; | 83 std::vector<std::string> GetOwnPropertyNames() const; |
79 | 84 |
80 /** | 85 /** |
81 * Returns a property value if this is an object (see `IsObject()`). | 86 * Returns a property value if this is an object (see `IsObject()`). |
82 * @param name Property name. | 87 * @param name Property name. |
83 * @return Property value, undefined (see `IsUndefined()`) if the property | 88 * @return Property value, undefined (see `IsUndefined()`) if the property |
84 * does not exist. | 89 * does not exist. |
85 */ | 90 */ |
86 JsValue GetProperty(const std::string& name) const; | 91 JsValue GetProperty(const std::string& name) const; |
87 | 92 |
88 //@{ | 93 //@{ |
89 /** | 94 /** |
90 * Sets a property value if this is an object (see `IsObject()`). | 95 * Sets a property value if this is an object (see `IsObject()`). |
91 * @param name Property name. | 96 * @param name Property name. |
92 * @param val Property value. | 97 * @param val Property value. |
93 */ | 98 */ |
94 void SetProperty(const std::string& name, const std::string& val); | 99 void SetProperty(const std::string& name, const std::string& val); |
| 100 void SetProperty(const std::string& name, const StringBuffer& val); |
95 void SetProperty(const std::string& name, int64_t val); | 101 void SetProperty(const std::string& name, int64_t val); |
96 void SetProperty(const std::string& name, bool val); | 102 void SetProperty(const std::string& name, bool val); |
97 void SetProperty(const std::string& name, const JsValue& value); | 103 void SetProperty(const std::string& name, const JsValue& value); |
98 inline void SetProperty(const std::string& name, const char* val) | 104 inline void SetProperty(const std::string& name, const char* val) |
99 { | 105 { |
100 SetProperty(name, std::string(val)); | 106 SetProperty(name, std::string(val)); |
101 } | 107 } |
102 inline void SetProperty(const std::string& name, int val) | 108 inline void SetProperty(const std::string& name, int val) |
103 { | 109 { |
104 SetProperty(name, static_cast<int64_t>(val)); | 110 SetProperty(name, static_cast<int64_t>(val)); |
105 } | 111 } |
106 //@} | 112 //@} |
| 113 /** |
| 114 * Sets a property value string if this is an object (see `IsObject()`). |
| 115 * @param name Property name. |
| 116 * @param val Property value as a StringBuffer. |
| 117 */ |
| 118 void SetStringBufferProperty(const std::string& name, const StringBuffer& va
l); |
107 | 119 |
108 /** | 120 /** |
109 * Returns the value's class name, e.g.\ _Array_ for arrays | 121 * Returns the value's class name, e.g.\ _Array_ for arrays |
110 * (see `IsArray()`). | 122 * (see `IsArray()`). |
111 * Technically, this is the name of the function that was used as a | 123 * Technically, this is the name of the function that was used as a |
112 * constructor. | 124 * constructor. |
113 * @return Class name of the value. | 125 * @return Class name of the value. |
114 */ | 126 */ |
115 std::string GetClass() const; | 127 std::string GetClass() const; |
116 | 128 |
(...skipping 18 matching lines...) Expand all Loading... |
135 * @param arg A single required parameter. | 147 * @param arg A single required parameter. |
136 * @return Value returned by the function. | 148 * @return Value returned by the function. |
137 */ | 149 */ |
138 JsValue Call(const JsValue& arg) const; | 150 JsValue Call(const JsValue& arg) const; |
139 | 151 |
140 v8::Local<v8::Value> UnwrapValue() const; | 152 v8::Local<v8::Value> UnwrapValue() const; |
141 | 153 |
142 protected: | 154 protected: |
143 JsEnginePtr jsEngine; | 155 JsEnginePtr jsEngine; |
144 private: | 156 private: |
145 JsValue(JsEnginePtr jsEngine, v8::Handle<v8::Value> value); | 157 JsValue(JsEnginePtr jsEngine, v8::Local<v8::Value> value); |
146 void SetProperty(const std::string& name, v8::Handle<v8::Value> val); | 158 void SetProperty(const std::string& name, v8::Local<v8::Value> val); |
| 159 |
147 // Parameter args is not const because a pointer to its internal arrays is | 160 // Parameter args is not const because a pointer to its internal arrays is |
148 // passed to v8::Function::Call but the latter does not expect a const point
er. | 161 // passed to v8::Function::Call but the latter does not expect a const point
er. |
149 JsValue Call(std::vector<v8::Handle<v8::Value>>& args, v8::Local<v8::Object>
thisObj) const; | 162 JsValue Call(std::vector<v8::Local<v8::Value>>& args, v8::Local<v8::Object>
thisObj) const; |
150 | 163 |
151 std::unique_ptr<v8::Persistent<v8::Value>> value; | 164 std::unique_ptr<v8::Global<v8::Value>> value; |
152 }; | 165 }; |
153 } | 166 } |
154 | 167 |
155 #endif | 168 #endif |
OLD | NEW |