| 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-present eyeo GmbH | 3 * Copyright (C) 2006-present 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 11 matching lines...) Expand all Loading... |
| 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> | 26 #include <AdblockPlus/IFileSystem.h> |
| 27 | 27 |
| 28 namespace v8 | 28 namespace v8 |
| 29 { | 29 { |
| 30 class Value; | 30 class Value; |
| 31 class Object; | 31 class Object; |
| 32 class Isolate; |
| 33 class Context; |
| 34 class Locker; |
| 35 class HandleScope; |
| 32 template<class T> class Local; | 36 template<class T> class Local; |
| 33 template<class T> class Global; | 37 template<class T> class Global; |
| 38 template<typename T> class FunctionCallbackInfo; |
| 39 typedef void(*FunctionCallback)(const FunctionCallbackInfo<v8::Value>& info); |
| 34 } | 40 } |
| 35 | 41 |
| 42 namespace duktape |
| 43 { |
| 44 class Value; |
| 45 class Object; |
| 46 class Isolate; |
| 47 class Context; |
| 48 class Locker; |
| 49 class HandleScope; |
| 50 template<class T> class Local; |
| 51 template<class T> class Global; |
| 52 template<typename T> class FunctionCallbackInfo; |
| 53 typedef void(*FunctionCallback)(const FunctionCallbackInfo<v8::Value>& info); |
| 54 } |
| 55 |
| 56 template <class TValue, class TObject, class TLocalValue, class TGlobalValue, cl
ass TLocalObject, class TCallbackFunc, class TCallbackFuncInfo, class TContext,
class TLocalContext, class TGlobalContext, class THandleScope, class TLocker, cl
ass TIsolate> |
| 57 struct JsEngineNamespace |
| 58 { |
| 59 typedef TValue Value; |
| 60 typedef TObject Object; |
| 61 typedef TLocalValue LocalValue; |
| 62 typedef TGlobalValue GlobalValue; |
| 63 typedef TLocalObject LocalObject; |
| 64 typedef TCallbackFunc CallbackFunc; |
| 65 typedef TCallbackFuncInfo CallbackFuncInfo; |
| 66 typedef TContext Context; |
| 67 typedef TLocalContext LocalContext; |
| 68 typedef TGlobalContext GlobalContext; |
| 69 typedef THandleScope HandleScope; |
| 70 typedef TLocker Locker; |
| 71 typedef TIsolate Isolate; |
| 72 }; |
| 73 #ifdef ABP_DUKTAPE |
| 74 typedef JsEngineNamespace<duktape::Value, duktape::Object, duktape::Local<duktap
e::Value>, duktape::Global<duktape::Value>, duktape::Local<duktape::Object>, duk
tape::FunctionCallback, duktape::FunctionCallbackInfo<duktape::Value>, duktape::
Context, duktape::Local<duktape::Context>, duktape::Global<duktape::Context>, du
ktape::HandleScope, duktape::Locker, duktape::Isolate> JsEngineNamespaceImpl; |
| 75 #define JSVALUEIMPL JsValueTemplate<JsEngineNamespaceImpl> |
| 76 #define JSENGINEIMPL JsEngineTemplate<JsEngineNamespaceImpl> |
| 77 #define JSVALUEDUKTAPE JSVALUEIMPL |
| 78 #define JSENGINEDUKTAPE JSENGINEIMPL |
| 79 #else // default to V8 |
| 80 typedef JsEngineNamespace<v8::Value, v8::Object, v8::Local<v8::Value>, v8::Globa
l<v8::Value>, v8::Local<v8::Object>, v8::FunctionCallback, v8::FunctionCallbackI
nfo<v8::Value>, v8::Context, v8::Local<v8::Context>, v8::Global<v8::Context>, v8
::HandleScope, v8::Locker, v8::Isolate> JsEngineNamespaceImpl; |
| 81 #define JSVALUEIMPL JsValueTemplate<JsEngineNamespaceImpl> |
| 82 #define JSENGINEIMPL JsEngineTemplate<JsEngineNamespaceImpl> |
| 83 #define JSVALUEV8 JSVALUEIMPL |
| 84 #define JSENGINEV8 JSENGINEIMPL |
| 85 #endif |
| 86 |
| 36 namespace AdblockPlus | 87 namespace AdblockPlus |
| 37 { | 88 { |
| 38 class JsValue; | 89 template <class T> class JsValueTemplate; |
| 39 class JsEngine; | 90 typedef JSVALUEIMPL JsValue; |
| 91 |
| 92 template <class T> class JsEngineTemplate; |
| 93 typedef JSENGINEIMPL JsEngine; |
| 40 | 94 |
| 41 typedef std::shared_ptr<JsEngine> JsEnginePtr; | 95 typedef std::shared_ptr<JsEngine> JsEnginePtr; |
| 42 | 96 |
| 43 typedef IFileSystem::IOBuffer StringBuffer; | 97 typedef IFileSystem::IOBuffer StringBuffer; |
| 44 | 98 |
| 45 /** | 99 /** |
| 46 * List of JavaScript values. | 100 * List of JavaScript values. |
| 47 */ | 101 */ |
| 48 typedef std::vector<AdblockPlus::JsValue> JsValueList; | 102 typedef std::vector<AdblockPlus::JsValue> JsValueList; |
| 49 | 103 |
| 50 /** | 104 /** |
| 51 * Wrapper for JavaScript values. | 105 * Wrapper for JavaScript values. |
| 52 * See `JsEngine` for creating `JsValue` objects. | 106 * See `JsEngine` for creating `JsValue` objects. |
| 53 */ | 107 */ |
| 54 class JsValue | 108 template <class T> class JsValueTemplate |
| 55 { | 109 { |
| 56 friend class JsEngine; | 110 template <typename T> friend class JsEngineTemplate; |
| 57 public: | 111 public: |
| 58 JsValue(JsValue&& src); | 112 JsValueTemplate(JsValueTemplate<T>&& src); |
| 59 JsValue(const JsValue& src); | 113 JsValueTemplate(const JsValueTemplate<T>& src); |
| 60 virtual ~JsValue(); | 114 virtual ~JsValueTemplate(); |
| 61 | 115 |
| 62 JsValue& operator=(const JsValue& src); | 116 JsValueTemplate<T>& operator=(const JsValueTemplate<T>& src); |
| 63 JsValue& operator=(JsValue&& src); | 117 JsValueTemplate<T>& operator=(JsValueTemplate<T>&& src); |
| 64 | 118 |
| 65 bool IsUndefined() const; | 119 bool IsUndefined() const; |
| 66 bool IsNull() const; | 120 bool IsNull() const; |
| 67 bool IsString() const; | 121 bool IsString() const; |
| 68 bool IsNumber() const; | 122 bool IsNumber() const; |
| 69 bool IsBool() const; | 123 bool IsBool() const; |
| 70 bool IsObject() const; | 124 bool IsObject() const; |
| 71 bool IsArray() const; | 125 bool IsArray() const; |
| 72 bool IsFunction() const; | 126 bool IsFunction() const; |
| 73 std::string AsString() const; | 127 std::string AsString() const; |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 JsValue Call(const JsValueList& params, const AdblockPlus::JsValue& thisValu
e) const; | 196 JsValue Call(const JsValueList& params, const AdblockPlus::JsValue& thisValu
e) const; |
| 143 | 197 |
| 144 /** | 198 /** |
| 145 * Invokes the value as a function (see `IsFunction()`) with single | 199 * Invokes the value as a function (see `IsFunction()`) with single |
| 146 * parameter. | 200 * parameter. |
| 147 * @param arg A single required parameter. | 201 * @param arg A single required parameter. |
| 148 * @return Value returned by the function. | 202 * @return Value returned by the function. |
| 149 */ | 203 */ |
| 150 JsValue Call(const JsValue& arg) const; | 204 JsValue Call(const JsValue& arg) const; |
| 151 | 205 |
| 152 v8::Local<v8::Value> UnwrapValue() const; | 206 typename T::LocalValue UnwrapValue() const; |
| 153 | 207 |
| 154 protected: | 208 protected: |
| 155 JsEnginePtr jsEngine; | 209 JsEnginePtr jsEngine; |
| 156 private: | 210 private: |
| 157 JsValue(JsEnginePtr jsEngine, v8::Local<v8::Value> value); | 211 JsValueTemplate<T>(JsEnginePtr jsEngine, typename T::LocalValue); |
| 158 void SetProperty(const std::string& name, v8::Local<v8::Value> val); | 212 void SetProperty(const std::string& name, typename T::LocalValue val); |
| 159 | 213 |
| 160 // Parameter args is not const because a pointer to its internal arrays is | 214 // 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 point
er. | 215 // passed to v8::Function::Call but the latter does not expect a const point
er. |
| 162 JsValue Call(std::vector<v8::Local<v8::Value>>& args, v8::Local<v8::Object>
thisObj) const; | 216 JsValue Call(std::vector<typename T::LocalValue>& args, typename T::LocalObj
ect thisObj) const; |
| 163 | 217 |
| 164 std::unique_ptr<v8::Global<v8::Value>> value; | 218 std::unique_ptr<typename T::GlobalValue> value; |
| 165 }; | 219 }; |
| 166 } | 220 } |
| 167 | 221 |
| 168 #endif | 222 #endif |
| OLD | NEW |