OLD | NEW |
1 #ifndef ADBLOCKPLUS_JS_ENGINE_H | 1 #ifndef ADBLOCKPLUS_JS_ENGINE_H |
2 #define ADBLOCKPLUS_JS_ENGINE_H | 2 #define ADBLOCKPLUS_JS_ENGINE_H |
3 | 3 |
4 #include <stdexcept> | 4 #include <stdexcept> |
5 #include <string> | 5 #include <string> |
6 #include <v8.h> | 6 #include <v8.h> |
7 #include <AdblockPlus/AppInfo.h> | 7 #include <AdblockPlus/AppInfo.h> |
8 #include <AdblockPlus/ErrorCallback.h> | 8 #include <AdblockPlus/ErrorCallback.h> |
9 #include <AdblockPlus/FileSystem.h> | 9 #include <AdblockPlus/FileSystem.h> |
10 #include <AdblockPlus/JsValue.h> | 10 #include <AdblockPlus/JsValue.h> |
11 #include <AdblockPlus/WebRequest.h> | 11 #include <AdblockPlus/WebRequest.h> |
12 | 12 |
13 #include "tr1_memory.h" | 13 #include "tr1_memory.h" |
14 | 14 |
15 namespace AdblockPlus | 15 namespace AdblockPlus |
16 { | 16 { |
17 class JsError : public std::runtime_error | 17 class JsError : public std::runtime_error |
18 { | 18 { |
19 public: | 19 public: |
20 JsError(const v8::Handle<v8::Value> exception, | 20 JsError(const v8::Handle<v8::Value> exception, |
21 const v8::Handle<v8::Message> message); | 21 const v8::Handle<v8::Message> message); |
22 }; | 22 }; |
23 | 23 |
| 24 class JsEngine; |
24 typedef std::tr1::shared_ptr<JsEngine> JsEnginePtr; | 25 typedef std::tr1::shared_ptr<JsEngine> JsEnginePtr; |
25 | 26 |
26 class JsEngine | 27 class JsEngine : public std::tr1::enable_shared_from_this<JsEngine> |
27 { | 28 { |
28 friend class JsValue; | 29 friend class JsValue; |
29 | 30 |
30 public: | 31 public: |
31 JsEngine(const AppInfo& appInfo = AppInfo()); | 32 static JsEnginePtr New(const AppInfo& appInfo = AppInfo()); |
32 JsValuePtr Evaluate(const std::string& source, | 33 JsValuePtr Evaluate(const std::string& source, |
33 const std::string& filename = ""); | 34 const std::string& filename = ""); |
34 void Load(const std::string& scriptPath); | 35 void Load(const std::string& scriptPath); |
35 void Gc(); | 36 void Gc(); |
36 JsValuePtr NewValue(const std::string& val); | 37 JsValuePtr NewValue(const std::string& val); |
37 JsValuePtr NewValue(int64_t val); | 38 JsValuePtr NewValue(int64_t val); |
38 JsValuePtr NewValue(bool val); | 39 JsValuePtr NewValue(bool val); |
39 inline JsValuePtr NewValue(const char* val) | 40 inline JsValuePtr NewValue(const char* val) |
40 { | 41 { |
41 return NewValue(std::string(val)); | 42 return NewValue(std::string(val)); |
42 } | 43 } |
43 inline JsValuePtr NewValue(int val) | 44 inline JsValuePtr NewValue(int val) |
44 { | 45 { |
45 return NewValue(static_cast<int64_t>(val)); | 46 return NewValue(static_cast<int64_t>(val)); |
46 } | 47 } |
47 JsValuePtr NewObject(); | 48 JsValuePtr NewObject(); |
48 JsValuePtr NewCallback(v8::InvocationCallback callback); | 49 JsValuePtr NewCallback(v8::InvocationCallback callback); |
49 static JsEngine& FromArguments(const v8::Arguments& arguments); | 50 static JsEnginePtr FromArguments(const v8::Arguments& arguments); |
50 JsValueList ConvertArguments(const v8::Arguments& arguments); | 51 JsValueList ConvertArguments(const v8::Arguments& arguments); |
51 | 52 |
52 FileSystemPtr GetFileSystem(); | 53 FileSystemPtr GetFileSystem(); |
53 void SetFileSystem(FileSystemPtr val); | 54 void SetFileSystem(FileSystemPtr val); |
54 WebRequestPtr GetWebRequest(); | 55 WebRequestPtr GetWebRequest(); |
55 void SetWebRequest(WebRequestPtr val); | 56 void SetWebRequest(WebRequestPtr val); |
56 ErrorCallbackPtr GetErrorCallback(); | 57 ErrorCallbackPtr GetErrorCallback(); |
57 void SetErrorCallback(ErrorCallbackPtr val); | 58 void SetErrorCallback(ErrorCallbackPtr val); |
58 | 59 |
59 class Context | 60 class Context |
60 { | 61 { |
61 public: | 62 public: |
62 Context(const JsEngine& jsEngine); | 63 Context(const JsEnginePtr jsEngine); |
63 virtual inline ~Context() {}; | 64 virtual inline ~Context() {}; |
64 | 65 |
65 private: | 66 private: |
66 const v8::Locker locker; | 67 const v8::Locker locker; |
67 const v8::HandleScope handleScope; | 68 const v8::HandleScope handleScope; |
68 const v8::Context::Scope contextScope; | 69 const v8::Context::Scope contextScope; |
69 }; | 70 }; |
70 | 71 |
71 private: | 72 private: |
| 73 JsEngine(); |
| 74 |
72 FileSystemPtr fileSystem; | 75 FileSystemPtr fileSystem; |
73 WebRequestPtr webRequest; | 76 WebRequestPtr webRequest; |
74 ErrorCallbackPtr errorCallback; | 77 ErrorCallbackPtr errorCallback; |
75 v8::Isolate* isolate; | 78 v8::Isolate* isolate; |
76 v8::Persistent<v8::Context> context; | 79 v8::Persistent<v8::Context> context; |
77 }; | 80 }; |
78 } | 81 } |
79 | 82 |
80 #endif | 83 #endif |
OLD | NEW |