| 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 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 33 } | 33 } |
| 34 | 34 |
| 35 namespace AdblockPlus | 35 namespace AdblockPlus |
| 36 { | 36 { |
| 37 class JsValue; | 37 class JsValue; |
| 38 class JsEngine; | 38 class JsEngine; |
| 39 | 39 |
| 40 typedef std::shared_ptr<JsEngine> JsEnginePtr; | 40 typedef std::shared_ptr<JsEngine> JsEnginePtr; |
| 41 | 41 |
| 42 /** | 42 /** |
| 43 * Shared smart pointer to a `JsValue` instance. | |
| 44 */ | |
| 45 typedef std::shared_ptr<JsValue> JsValuePtr; | |
| 46 typedef std::shared_ptr<const JsValue> JsConstValuePtr; | |
| 47 | |
| 48 /** | |
| 49 * List of JavaScript values. | 43 * List of JavaScript values. |
| 50 */ | 44 */ |
| 51 typedef std::vector<AdblockPlus::JsValuePtr> JsValueList; | 45 typedef std::vector<AdblockPlus::JsValue> JsValueList; |
| 52 typedef std::vector<AdblockPlus::JsConstValuePtr> JsConstValueList; | |
| 53 | 46 |
| 54 /** | 47 /** |
| 55 * Wrapper for JavaScript values. | 48 * Wrapper for JavaScript values. |
| 56 * See `JsEngine` for creating `JsValue` objects. | 49 * See `JsEngine` for creating `JsValue` objects. |
| 57 */ | 50 */ |
| 58 class JsValue | 51 class JsValue |
| 59 { | 52 { |
| 60 friend class JsEngine; | 53 friend class JsEngine; |
| 61 public: | 54 public: |
| 62 JsValue(JsValue&& src); | 55 JsValue(JsValue&& src); |
| 56 JsValue(const JsValue& src); |
| 63 virtual ~JsValue(); | 57 virtual ~JsValue(); |
| 64 | 58 |
| 59 JsValue& operator=(const JsValue& src); |
| 60 |
| 65 bool IsUndefined() const; | 61 bool IsUndefined() const; |
| 66 bool IsNull() const; | 62 bool IsNull() const; |
| 67 bool IsString() const; | 63 bool IsString() const; |
| 68 bool IsNumber() const; | 64 bool IsNumber() const; |
| 69 bool IsBool() const; | 65 bool IsBool() const; |
| 70 bool IsObject() const; | 66 bool IsObject() const; |
| 71 bool IsArray() const; | 67 bool IsArray() const; |
| 72 bool IsFunction() const; | 68 bool IsFunction() const; |
| 73 std::string AsString() const; | 69 std::string AsString() const; |
| 74 int64_t AsInt() const; | 70 int64_t AsInt() const; |
| 75 bool AsBool() const; | 71 bool AsBool() const; |
| 76 JsValueList AsList() const; | 72 JsValueList AsList() const; |
| 77 | 73 |
| 78 /** | 74 /** |
| 79 * Returns a list of property names if this is an object (see `IsObject()`). | 75 * Returns a list of property names if this is an object (see `IsObject()`). |
| 80 * @return List of property names. | 76 * @return List of property names. |
| 81 */ | 77 */ |
| 82 std::vector<std::string> GetOwnPropertyNames() const; | 78 std::vector<std::string> GetOwnPropertyNames() const; |
| 83 | 79 |
| 84 /** | 80 /** |
| 85 * Returns a property value if this is an object (see `IsObject()`). | 81 * Returns a property value if this is an object (see `IsObject()`). |
| 86 * @param name Property name. | 82 * @param name Property name. |
| 87 * @return Property value, undefined (see `IsUndefined()`) if the property | 83 * @return Property value, undefined (see `IsUndefined()`) if the property |
| 88 * does not exist. | 84 * does not exist. |
| 89 */ | 85 */ |
| 90 JsValuePtr GetProperty(const std::string& name) const; | 86 JsValue GetProperty(const std::string& name) const; |
| 91 | 87 |
| 92 //@{ | 88 //@{ |
| 93 /** | 89 /** |
| 94 * Sets a property value if this is an object (see `IsObject()`). | 90 * Sets a property value if this is an object (see `IsObject()`). |
| 95 * @param name Property name. | 91 * @param name Property name. |
| 96 * @param val Property value. | 92 * @param val Property value. |
| 97 */ | 93 */ |
| 98 void SetProperty(const std::string& name, const std::string& val); | 94 void SetProperty(const std::string& name, const std::string& val); |
| 99 void SetProperty(const std::string& name, int64_t val); | 95 void SetProperty(const std::string& name, int64_t val); |
| 100 void SetProperty(const std::string& name, bool val); | 96 void SetProperty(const std::string& name, bool val); |
| 101 void SetProperty(const std::string& name, const JsValuePtr& value); | 97 void SetProperty(const std::string& name, const JsValue& value); |
| 102 inline void SetProperty(const std::string& name, const char* val) | 98 inline void SetProperty(const std::string& name, const char* val) |
| 103 { | 99 { |
| 104 SetProperty(name, std::string(val)); | 100 SetProperty(name, std::string(val)); |
| 105 } | 101 } |
| 106 inline void SetProperty(const std::string& name, int val) | 102 inline void SetProperty(const std::string& name, int val) |
| 107 { | 103 { |
| 108 SetProperty(name, static_cast<int64_t>(val)); | 104 SetProperty(name, static_cast<int64_t>(val)); |
| 109 } | 105 } |
| 110 //@} | 106 //@} |
| 111 | 107 |
| 112 /** | 108 /** |
| 113 * Returns the value's class name, e.g.\ _Array_ for arrays | 109 * Returns the value's class name, e.g.\ _Array_ for arrays |
| 114 * (see `IsArray()`). | 110 * (see `IsArray()`). |
| 115 * Technically, this is the name of the function that was used as a | 111 * Technically, this is the name of the function that was used as a |
| 116 * constructor. | 112 * constructor. |
| 117 * @return Class name of the value. | 113 * @return Class name of the value. |
| 118 */ | 114 */ |
| 119 std::string GetClass() const; | 115 std::string GetClass() const; |
| 120 | 116 |
| 121 /** | 117 /** |
| 122 * Invokes the value as a function (see `IsFunction()`). | 118 * Invokes the value as a function (see `IsFunction()`). |
| 123 * @param params Optional list of parameters. | 119 * @param params Optional list of parameters. |
| 124 * @param thisPtr Optional `this` value. | |
| 125 * @return Value returned by the function. | 120 * @return Value returned by the function. |
| 126 */ | 121 */ |
| 127 JsValue Call(const JsConstValueList& params = JsConstValueList(), | 122 JsValue Call(const JsValueList& params = JsValueList()) const; |
| 128 AdblockPlus::JsValuePtr thisPtr = AdblockPlus::JsValuePtr()) const; | 123 |
| 124 /** |
| 125 * Invokes the value as a method (see `IsFunction()`). |
| 126 * @param params list of parameters. |
| 127 * @param thisPtr `this` value. |
| 128 * @return Value returned by the function. |
| 129 */ |
| 130 JsValue Call(const JsValueList& params, const AdblockPlus::JsValue& thisValu
e) const; |
| 129 | 131 |
| 130 /** | 132 /** |
| 131 * Invokes the value as a function (see `IsFunction()`) with single | 133 * Invokes the value as a function (see `IsFunction()`) with single |
| 132 * parameter. | 134 * parameter. |
| 133 * @param arg A single required parameter. | 135 * @param arg A single required parameter. |
| 134 * @return Value returned by the function. | 136 * @return Value returned by the function. |
| 135 */ | 137 */ |
| 136 JsValue Call(const JsValue& arg) const; | 138 JsValue Call(const JsValue& arg) const; |
| 137 | 139 |
| 138 v8::Local<v8::Value> UnwrapValue() const; | 140 v8::Local<v8::Value> UnwrapValue() const; |
| 141 |
| 139 protected: | 142 protected: |
| 140 JsEnginePtr jsEngine; | 143 JsEnginePtr jsEngine; |
| 141 private: | 144 private: |
| 142 JsValue(JsEnginePtr jsEngine, v8::Handle<v8::Value> value); | 145 JsValue(JsEnginePtr jsEngine, v8::Handle<v8::Value> value); |
| 143 void SetProperty(const std::string& name, v8::Handle<v8::Value> val); | 146 void SetProperty(const std::string& name, v8::Handle<v8::Value> val); |
| 144 // Parameter args is not const because a pointer to its internal arrays is | 147 // Parameter args is not const because a pointer to its internal arrays is |
| 145 // passed to v8::Function::Call but the latter does not expect a const point
er. | 148 // passed to v8::Function::Call but the latter does not expect a const point
er. |
| 146 JsValue Call(std::vector<v8::Handle<v8::Value>>& args, v8::Local<v8::Object>
thisObj) const; | 149 JsValue Call(std::vector<v8::Handle<v8::Value>>& args, v8::Local<v8::Object>
thisObj) const; |
| 147 | 150 |
| 148 std::unique_ptr<v8::Persistent<v8::Value>> value; | 151 std::unique_ptr<v8::Persistent<v8::Value>> value; |
| 149 }; | 152 }; |
| 150 } | 153 } |
| 151 | 154 |
| 152 #endif | 155 #endif |
| OLD | NEW |