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