| Left: | ||
| Right: |
| OLD | NEW |
|---|---|
| 1 /* | 1 /* |
| 2 * This file is part of Adblock Plus <http://adblockplus.org/>, | 2 * This file is part of Adblock Plus <http://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2014 Eyeo GmbH | 3 * Copyright (C) 2006-2014 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 17 matching lines...) Expand all Loading... | |
| 28 { | 28 { |
| 29 class Value; | 29 class Value; |
| 30 template<class T> class Handle; | 30 template<class T> class Handle; |
| 31 } | 31 } |
| 32 | 32 |
| 33 namespace AdblockPlus | 33 namespace AdblockPlus |
| 34 { | 34 { |
| 35 class JsValue; | 35 class JsValue; |
| 36 class JsEngine; | 36 class JsEngine; |
| 37 | 37 |
| 38 typedef std::tr1::shared_ptr<JsValue> JsValuePtr; | |
| 39 typedef std::vector<AdblockPlus::JsValuePtr> JsValueList; | |
| 40 typedef std::tr1::shared_ptr<JsEngine> JsEnginePtr; | 38 typedef std::tr1::shared_ptr<JsEngine> JsEnginePtr; |
| 41 | 39 |
| 40 /** | |
| 41 * Shared smart pointer to a `JsValue` instance. | |
| 42 */ | |
| 43 typedef std::tr1::shared_ptr<JsValue> JsValuePtr; | |
| 44 | |
| 45 /** | |
| 46 * List of JavaScript values. | |
| 47 */ | |
| 48 typedef std::vector<AdblockPlus::JsValuePtr> JsValueList; | |
| 49 | |
| 50 /** | |
| 51 * Wrapper for JavaScript objects. | |
|
Wladimir Palant
2014/08/29 18:18:50
"JavaScript values"? This can wrap primitive value
Wladimir Palant
2014/09/01 17:36:56
This comment hasn't been addressed.
Felix Dahlke
2014/09/02 05:24:51
Oops, done.
| |
| 52 * See `JsEngine` for creating `JsValue` objects. | |
| 53 */ | |
| 42 class JsValue | 54 class JsValue |
| 43 { | 55 { |
| 44 friend class JsEngine; | 56 friend class JsEngine; |
| 45 public: | 57 public: |
| 46 JsValue(JsValuePtr value); | 58 JsValue(JsValuePtr value); |
| 47 virtual ~JsValue(); | 59 virtual ~JsValue(); |
| 48 | 60 |
| 49 bool IsUndefined() const; | 61 bool IsUndefined() const; |
| 50 bool IsNull() const; | 62 bool IsNull() const; |
| 51 bool IsString() const; | 63 bool IsString() const; |
| 52 bool IsNumber() const; | 64 bool IsNumber() const; |
| 53 bool IsBool() const; | 65 bool IsBool() const; |
| 54 bool IsObject() const; | 66 bool IsObject() const; |
| 55 bool IsArray() const; | 67 bool IsArray() const; |
| 56 bool IsFunction() const; | 68 bool IsFunction() const; |
| 57 std::string AsString() const; | 69 std::string AsString() const; |
| 58 int64_t AsInt() const; | 70 int64_t AsInt() const; |
| 59 bool AsBool() const; | 71 bool AsBool() const; |
| 60 JsValueList AsList() const; | 72 JsValueList AsList() const; |
| 73 | |
| 74 /** | |
| 75 * Returns a list of property names if this is an object (see `IsObject()`). | |
| 76 * @return List of property names. | |
| 77 */ | |
| 61 std::vector<std::string> GetOwnPropertyNames() const; | 78 std::vector<std::string> GetOwnPropertyNames() const; |
| 79 | |
| 80 /** | |
| 81 * Returns a property value if this is an object (see `IsObject()`). | |
| 82 * @param name Property name. | |
| 83 * @return Property value, undefined (see `IsUndefined()`) if the property | |
| 84 * does not exist. | |
| 85 */ | |
| 62 JsValuePtr GetProperty(const std::string& name) const; | 86 JsValuePtr GetProperty(const std::string& name) const; |
| 87 | |
| 88 //@{ | |
| 89 /** | |
| 90 * Sets a property value if this is an object (see `IsObject()`). | |
| 91 * @param name Property name. | |
| 92 * @param val Property value. | |
| 93 */ | |
| 63 void SetProperty(const std::string& name, const std::string& val); | 94 void SetProperty(const std::string& name, const std::string& val); |
| 64 void SetProperty(const std::string& name, int64_t val); | 95 void SetProperty(const std::string& name, int64_t val); |
| 65 void SetProperty(const std::string& name, bool val); | 96 void SetProperty(const std::string& name, bool val); |
| 66 void SetProperty(const std::string& name, JsValuePtr value); | 97 void SetProperty(const std::string& name, JsValuePtr value); |
| 67 inline void SetProperty(const std::string& name, const char* val) | 98 inline void SetProperty(const std::string& name, const char* val) |
| 68 { | 99 { |
| 69 SetProperty(name, std::string(val)); | 100 SetProperty(name, std::string(val)); |
| 70 } | 101 } |
| 71 inline void SetProperty(const std::string& name, int val) | 102 inline void SetProperty(const std::string& name, int val) |
| 72 { | 103 { |
| 73 SetProperty(name, static_cast<int64_t>(val)); | 104 SetProperty(name, static_cast<int64_t>(val)); |
| 74 } | 105 } |
| 106 //@} | |
| 107 | |
| 108 /** | |
| 109 * Returns the value's class name, e.g.\ _Array_ for arrays | |
| 110 * (see `IsArray()`). | |
| 111 * Technically, this is the name of the function that was used as a | |
| 112 * constructor. | |
| 113 * @return Class name of the value. | |
| 114 */ | |
| 75 std::string GetClass() const; | 115 std::string GetClass() const; |
| 116 | |
| 117 /** | |
| 118 * Invokes the value as a function (see `IsFunction()`). | |
| 119 * @param params Optional list of parameters. | |
| 120 * @param thisPtr Optional `this` value. | |
| 121 * @return Value returned by the function. | |
| 122 */ | |
| 76 JsValuePtr Call(const JsValueList& params = JsValueList(), | 123 JsValuePtr Call(const JsValueList& params = JsValueList(), |
| 77 AdblockPlus::JsValuePtr thisPtr = AdblockPlus::JsValuePtr()) const; | 124 AdblockPlus::JsValuePtr thisPtr = AdblockPlus::JsValuePtr()) const; |
| 125 | |
| 78 protected: | 126 protected: |
| 79 JsValue(JsEnginePtr jsEngine, v8::Handle<v8::Value> value); | 127 JsValue(JsEnginePtr jsEngine, v8::Handle<v8::Value> value); |
| 80 void SetProperty(const std::string& name, v8::Handle<v8::Value> val); | 128 void SetProperty(const std::string& name, v8::Handle<v8::Value> val); |
| 81 | 129 |
| 82 JsEnginePtr jsEngine; | 130 JsEnginePtr jsEngine; |
| 83 V8ValueHolder<v8::Value> value; | 131 V8ValueHolder<v8::Value> value; |
| 84 }; | 132 }; |
| 85 } | 133 } |
| 86 | 134 |
| 87 #endif | 135 #endif |
| OLD | NEW |