Left: | ||
Right: |
OLD | NEW |
---|---|
1 #include <vector> | |
2 | |
1 #include "GlobalJsObject.h" | 3 #include "GlobalJsObject.h" |
4 #include "Thread.h" | |
2 | 5 |
3 using namespace AdblockPlus; | 6 using namespace AdblockPlus; |
4 | 7 |
8 namespace | |
9 { | |
10 class TimeoutThread : public Thread | |
11 { | |
12 public: | |
13 TimeoutThread(v8::Isolate* const isolate, const v8::Arguments& arguments) | |
14 : isolate(isolate) | |
15 { | |
16 function = v8::Persistent<v8::Function>::New( | |
17 isolate, v8::Local<v8::Function>::Cast(arguments[0])); | |
Felix Dahlke
2013/04/08 13:07:49
Phew, good find. V8 isn't exactly documented but I
| |
18 delay = arguments[1]->ToNumber()->Value(); | |
19 for (int i = 2; i < arguments.Length(); i++) | |
20 { | |
21 const Value argument = Value::New(isolate, arguments[i]); | |
22 functionArguments.push_back(argument); | |
23 } | |
24 } | |
25 | |
26 ~TimeoutThread() | |
27 { | |
28 function.Dispose(isolate); | |
29 for (Values::iterator it = functionArguments.begin(); | |
30 it != functionArguments.end(); it++) | |
31 it->Dispose(isolate); | |
32 } | |
33 | |
34 void Run() | |
35 { | |
36 Sleep(delay); | |
37 const v8::Locker locker(isolate); | |
Wladimir Palant
2013/04/08 08:36:00
Does this really lock when this line is reached or
| |
38 const v8::HandleScope handleScope; | |
39 function->Call(function, functionArguments.size(), &functionArguments[0]); | |
40 delete this; | |
Wladimir Palant
2013/04/08 08:36:00
Self-deleting object?
1) Is that a good practice?
Felix Dahlke
2013/04/08 13:07:49
1) Depends. It's not generally considered a bad pr
Wladimir Palant
2013/04/08 14:03:47
I checked, delete is actually thread-safe - so no
| |
41 } | |
42 | |
43 private: | |
44 typedef v8::Persistent<v8::Value> Value; | |
45 typedef std::vector<Value> Values; | |
46 | |
47 v8::Isolate* const isolate; | |
48 v8::Persistent<v8::Function> function; | |
49 int delay; | |
50 Values functionArguments; | |
51 }; | |
52 | |
53 v8::Handle<v8::Value> SetTimeoutCallback(const v8::Arguments& arguments) | |
54 { | |
55 TimeoutThread* const timeoutThread = | |
56 new TimeoutThread(v8::Isolate::GetCurrent(), arguments); | |
57 timeoutThread->Start(); | |
58 | |
59 // We should actually return the timer ID here, which could be | |
60 // used via clearTimeout(). But since we don't seem to need | |
61 // clearTimeout(), we can safe that for later. | |
Wladimir Palant
2013/04/08 08:36:00
safe => save?
Felix Dahlke
2013/04/08 13:07:49
Yes, of course.
| |
62 return v8::Undefined(); | |
63 } | |
64 } | |
65 | |
5 v8::Handle<v8::ObjectTemplate> GlobalJsObject::Create( | 66 v8::Handle<v8::ObjectTemplate> GlobalJsObject::Create( |
6 ErrorCallback& errorCallback) | 67 ErrorCallback& errorCallback) |
7 { | 68 { |
8 const v8::Locker locker(v8::Isolate::GetCurrent()); | 69 const v8::Locker locker(v8::Isolate::GetCurrent()); |
9 v8::HandleScope handleScope; | 70 v8::HandleScope handleScope; |
10 const v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(); | 71 const v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(); |
11 const v8::Handle<v8::ObjectTemplate> console = | 72 const v8::Handle<v8::ObjectTemplate> console = |
12 AdblockPlus::ConsoleJsObject::Create(errorCallback); | 73 AdblockPlus::ConsoleJsObject::Create(errorCallback); |
13 global->Set(v8::String::New("console"), console); | 74 global->Set(v8::String::New("console"), console); |
75 const v8::Handle<v8::FunctionTemplate> setTimeoutFunction = | |
76 v8::FunctionTemplate::New(SetTimeoutCallback); | |
77 global->Set(v8::String::New("setTimeout"), setTimeoutFunction); | |
14 return handleScope.Close(global); | 78 return handleScope.Close(global); |
15 } | 79 } |
OLD | NEW |