LEFT | RIGHT |
1 #include <vector> | 1 #include <vector> |
2 #include <stdexcept> | 2 #include <stdexcept> |
3 | 3 |
| 4 #include <AdblockPlus/JsEngine.h> |
| 5 #include <AdblockPlus/JsValue.h> |
| 6 |
4 #include "AppInfoJsObject.h" | 7 #include "AppInfoJsObject.h" |
| 8 #include "ConsoleJsObject.h" |
| 9 #include "FileSystemJsObject.h" |
5 #include "GlobalJsObject.h" | 10 #include "GlobalJsObject.h" |
6 #include "ConsoleJsObject.h" | 11 #include "ConsoleJsObject.h" |
7 #include "WebRequestJsObject.h" | 12 #include "WebRequestJsObject.h" |
8 #include "Thread.h" | 13 #include "Thread.h" |
9 | 14 |
10 using namespace AdblockPlus; | 15 using namespace AdblockPlus; |
11 | 16 |
12 namespace | 17 namespace |
13 { | 18 { |
14 class TimeoutThread : public Thread | 19 class TimeoutThread : public Thread |
15 { | 20 { |
16 public: | 21 public: |
17 TimeoutThread(v8::Isolate* const isolate, const v8::Arguments& arguments) | 22 TimeoutThread(JsValueList& arguments) |
18 : isolate(isolate) | |
19 { | 23 { |
20 if (arguments.Length() < 2) | 24 if (arguments.size() < 2) |
21 throw std::runtime_error("setTimeout requires at least 2 parameters"); | 25 throw std::runtime_error("setTimeout requires at least 2 parameters"); |
22 | 26 |
23 const v8::Local<v8::Value> functionValue = arguments[0]; | 27 if (!arguments[0]->IsFunction()) |
24 if (!functionValue->IsFunction()) | |
25 throw std::runtime_error( | 28 throw std::runtime_error( |
26 "First argument to setTimeout must be a function"); | 29 "First argument to setTimeout must be a function"); |
27 | 30 |
28 const v8::Local<v8::Value> delayValue = arguments[1]; | 31 function = arguments[0]; |
29 if (!delayValue->IsNumber()) | 32 delay = arguments[1]->AsInt(); |
30 throw std::runtime_error( | 33 for (size_t i = 2; i < arguments.size(); i++) |
31 "Second argument to setTimeout must be a number"); | 34 functionArguments.push_back(arguments[i]); |
32 | |
33 function = v8::Persistent<v8::Function>::New( | |
34 isolate, v8::Local<v8::Function>::Cast(functionValue)); | |
35 delay = delayValue->ToNumber()->Value(); | |
36 for (int i = 2; i < arguments.Length(); i++) | |
37 { | |
38 const Value argument = Value::New(isolate, arguments[i]); | |
39 functionArguments.push_back(argument); | |
40 } | |
41 } | |
42 | |
43 ~TimeoutThread() | |
44 { | |
45 function.Dispose(isolate); | |
46 for (Values::iterator it = functionArguments.begin(); | |
47 it != functionArguments.end(); it++) | |
48 it->Dispose(isolate); | |
49 } | 35 } |
50 | 36 |
51 void Run() | 37 void Run() |
52 { | 38 { |
53 Sleep(delay); | 39 Sleep(delay); |
54 const v8::Locker locker(isolate); | 40 |
55 const v8::HandleScope handleScope; | 41 function->Call(functionArguments); |
56 v8::Handle<v8::Value>* argv = functionArguments.empty() ? 0 : &(functionAr
guments.front()); | |
57 function->Call(function, functionArguments.size(), argv); | |
58 delete this; | |
59 } | 42 } |
60 | 43 |
61 private: | 44 private: |
62 typedef v8::Persistent<v8::Value> Value; | 45 JsValuePtr function; |
63 typedef std::vector<Value> Values; | |
64 | |
65 v8::Isolate* const isolate; | |
66 v8::Persistent<v8::Function> function; | |
67 int delay; | 46 int delay; |
68 Values functionArguments; | 47 JsValueList functionArguments; |
69 }; | 48 }; |
70 | 49 |
71 v8::Handle<v8::Value> SetTimeoutCallback(const v8::Arguments& arguments) | 50 v8::Handle<v8::Value> SetTimeoutCallback(const v8::Arguments& arguments) |
72 { | 51 { |
73 TimeoutThread* timeoutThread; | 52 TimeoutThread* timeoutThread; |
74 try | 53 try |
75 { | 54 { |
76 timeoutThread = new TimeoutThread(v8::Isolate::GetCurrent(), arguments); | 55 AdblockPlus::JsValueList converted = |
| 56 AdblockPlus::JsEngine::FromArguments(arguments) |
| 57 .ConvertArguments(arguments); |
| 58 timeoutThread = new TimeoutThread(converted); |
77 } | 59 } |
78 catch (const std::exception& e) | 60 catch (const std::exception& e) |
79 { | 61 { |
80 return v8::ThrowException(v8::String::New(e.what())); | 62 return v8::ThrowException(v8::String::New(e.what())); |
81 } | 63 } |
82 timeoutThread->Start(); | 64 timeoutThread->Start(); |
83 | 65 |
84 // We should actually return the timer ID here, which could be | 66 // We should actually return the timer ID here, which could be |
85 // used via clearTimeout(). But since we don't seem to need | 67 // used via clearTimeout(). But since we don't seem to need |
86 // clearTimeout(), we can save that for later. | 68 // clearTimeout(), we can save that for later. |
87 return v8::Undefined(); | 69 return v8::Undefined(); |
88 } | 70 } |
89 } | 71 } |
90 | 72 |
91 v8::Handle<v8::ObjectTemplate> GlobalJsObject::Create( | 73 v8::Handle<v8::ObjectTemplate> GlobalJsObject::Create( |
92 const AppInfo& appInfo, ErrorCallback& errorCallback, WebRequest& webRequest) | 74 const AppInfo& appInfo, JsEngine& jsEngine) |
93 { | 75 { |
94 const v8::Locker locker(v8::Isolate::GetCurrent()); | 76 const v8::Locker locker(v8::Isolate::GetCurrent()); |
95 v8::HandleScope handleScope; | 77 v8::HandleScope handleScope; |
96 const v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(); | 78 const v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(); |
97 const v8::Handle<v8::ObjectTemplate> console = | |
98 AdblockPlus::ConsoleJsObject::Create(errorCallback); | |
99 global->Set(v8::String::New("console"), console); | |
100 const v8::Handle<v8::FunctionTemplate> setTimeoutFunction = | 79 const v8::Handle<v8::FunctionTemplate> setTimeoutFunction = |
101 v8::FunctionTemplate::New(SetTimeoutCallback); | 80 v8::FunctionTemplate::New(SetTimeoutCallback, |
| 81 v8::External::New(&jsEngine)); |
102 global->Set(v8::String::New("setTimeout"), setTimeoutFunction); | 82 global->Set(v8::String::New("setTimeout"), setTimeoutFunction); |
103 const v8::Handle<v8::ObjectTemplate> info = AppInfoJsObject::Create(appInfo); | 83 const v8::Handle<v8::ObjectTemplate> fileSystemObject = |
104 global->Set(v8::String::New("_appInfo"), info); | 84 FileSystemJsObject::Create(jsEngine); |
105 const v8::Handle<v8::ObjectTemplate> request = | 85 global->Set(v8::String::New("_fileSystem"), fileSystemObject); |
106 AdblockPlus::WebRequestJsObject::Create(webRequest); | 86 const v8::Handle<v8::ObjectTemplate> webRequestObject = |
107 global->Set(v8::String::New("_webRequest"), request); | 87 WebRequestJsObject::Create(jsEngine); |
| 88 global->Set(v8::String::New("_webRequest"), webRequestObject); |
| 89 const v8::Handle<v8::ObjectTemplate> consoleObject = |
| 90 ConsoleJsObject::Create(jsEngine); |
| 91 global->Set(v8::String::New("console"), consoleObject); |
| 92 const v8::Handle<v8::ObjectTemplate> appInfoObject = |
| 93 AppInfoJsObject::Create(appInfo); |
| 94 global->Set(v8::String::New("_appInfo"), appInfoObject); |
108 return handleScope.Close(global); | 95 return handleScope.Close(global); |
109 } | 96 } |
LEFT | RIGHT |