Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: src/GlobalJsObject.cpp

Issue 10184021: Some refactoring of global JavaScript objects (Closed)
Patch Set: Unbitrotted patch Created April 16, 2013, 3:32 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/GlobalJsObject.h ('k') | src/JsEngine.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "ConsoleJsObject.h" 6 #include "ConsoleJsObject.h"
5 #include "FileSystemJsObject.h" 7 #include "FileSystemJsObject.h"
6 #include "GlobalJsObject.h" 8 #include "GlobalJsObject.h"
7 #include "ConsoleJsObject.h" 9 #include "ConsoleJsObject.h"
8 #include "WebRequestJsObject.h" 10 #include "WebRequestJsObject.h"
9 #include "Thread.h" 11 #include "Thread.h"
10 12
11 using namespace AdblockPlus; 13 using namespace AdblockPlus;
12 14
13 namespace 15 namespace
14 { 16 {
15 class TimeoutThread : public Thread 17 class TimeoutThread : public Thread
16 { 18 {
17 public: 19 public:
18 TimeoutThread(v8::Isolate* const isolate, const v8::Arguments& arguments) 20 TimeoutThread(JsValueList& arguments)
19 : isolate(isolate)
20 { 21 {
21 if (arguments.Length() < 2) 22 if (arguments.size() < 2)
22 throw std::runtime_error("setTimeout requires at least 2 parameters"); 23 throw std::runtime_error("setTimeout requires at least 2 parameters");
23 24
24 const v8::Local<v8::Value> functionValue = arguments[0]; 25 if (!arguments[0]->IsFunction())
25 if (!functionValue->IsFunction())
26 throw std::runtime_error( 26 throw std::runtime_error(
27 "First argument to setTimeout must be a function"); 27 "First argument to setTimeout must be a function");
28 28
29 const v8::Local<v8::Value> delayValue = arguments[1]; 29 function = arguments[0];
30 if (!delayValue->IsNumber()) 30 delay = arguments[1]->AsInt();
31 throw std::runtime_error( 31 for (size_t i = 2; i < arguments.size(); i++)
32 "Second argument to setTimeout must be a number"); 32 functionArguments.push_back(arguments[i]);
33
34 function = v8::Persistent<v8::Function>::New(
35 isolate, v8::Local<v8::Function>::Cast(functionValue));
36 delay = delayValue->ToNumber()->Value();
37 for (int i = 2; i < arguments.Length(); i++)
38 {
39 const Value argument = Value::New(isolate, arguments[i]);
40 functionArguments.push_back(argument);
41 }
42 }
43
44 ~TimeoutThread()
45 {
46 function.Dispose(isolate);
47 for (Values::iterator it = functionArguments.begin();
48 it != functionArguments.end(); it++)
49 it->Dispose(isolate);
50 } 33 }
51 34
52 void Run() 35 void Run()
53 { 36 {
54 Sleep(delay); 37 Sleep(delay);
55 const v8::Locker locker(isolate); 38
56 const v8::HandleScope handleScope; 39 function->Call(functionArguments);
57 v8::Handle<v8::Value>* argv = functionArguments.empty() ? 0 : &(functionAr guments.front());
58 function->Call(function, functionArguments.size(), argv);
59 delete this;
60 } 40 }
61 41
62 private: 42 private:
63 typedef v8::Persistent<v8::Value> Value; 43 JsValuePtr function;
64 typedef std::vector<Value> Values;
65
66 v8::Isolate* const isolate;
67 v8::Persistent<v8::Function> function;
68 int delay; 44 int delay;
69 Values functionArguments; 45 JsValueList functionArguments;
70 }; 46 };
71 47
72 v8::Handle<v8::Value> SetTimeoutCallback(const v8::Arguments& arguments) 48 v8::Handle<v8::Value> SetTimeoutCallback(const v8::Arguments& arguments)
73 { 49 {
74 TimeoutThread* timeoutThread; 50 TimeoutThread* timeoutThread;
75 try 51 try
76 { 52 {
77 timeoutThread = new TimeoutThread(v8::Isolate::GetCurrent(), arguments); 53 AdblockPlus::JsValueList converted =
54 AdblockPlus::JsEngine::FromArguments(arguments)
55 .ConvertArguments(arguments);
56 timeoutThread = new TimeoutThread(converted);
78 } 57 }
79 catch (const std::exception& e) 58 catch (const std::exception& e)
80 { 59 {
81 return v8::ThrowException(v8::String::New(e.what())); 60 return v8::ThrowException(v8::String::New(e.what()));
82 } 61 }
83 timeoutThread->Start(); 62 timeoutThread->Start();
84 63
85 // We should actually return the timer ID here, which could be 64 // We should actually return the timer ID here, which could be
86 // used via clearTimeout(). But since we don't seem to need 65 // used via clearTimeout(). But since we don't seem to need
87 // clearTimeout(), we can save that for later. 66 // clearTimeout(), we can save that for later.
88 return v8::Undefined(); 67 return v8::Undefined();
89 } 68 }
90 } 69 }
91 70
92 v8::Handle<v8::ObjectTemplate> GlobalJsObject::Create( 71 v8::Handle<v8::ObjectTemplate> GlobalJsObject::Create(
93 FileSystem& fileSystem, WebRequest& webRequest, ErrorCallback& errorCallback) 72 JsEngine& jsEngine)
94 { 73 {
95 const v8::Locker locker(v8::Isolate::GetCurrent()); 74 const v8::Locker locker(v8::Isolate::GetCurrent());
96 v8::HandleScope handleScope; 75 v8::HandleScope handleScope;
97 const v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(); 76 const v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New();
98 const v8::Handle<v8::FunctionTemplate> setTimeoutFunction = 77 const v8::Handle<v8::FunctionTemplate> setTimeoutFunction =
99 v8::FunctionTemplate::New(SetTimeoutCallback); 78 v8::FunctionTemplate::New(SetTimeoutCallback,
79 v8::External::New(&jsEngine));
100 global->Set(v8::String::New("setTimeout"), setTimeoutFunction); 80 global->Set(v8::String::New("setTimeout"), setTimeoutFunction);
101 const v8::Handle<v8::ObjectTemplate> fileSystemObject = 81 const v8::Handle<v8::ObjectTemplate> fileSystemObject =
102 FileSystemJsObject::Create(fileSystem); 82 FileSystemJsObject::Create(jsEngine);
103 global->Set(v8::String::New("_fileSystem"), fileSystemObject); 83 global->Set(v8::String::New("_fileSystem"), fileSystemObject);
104 const v8::Handle<v8::ObjectTemplate> webRequestObject = 84 const v8::Handle<v8::ObjectTemplate> webRequestObject =
105 WebRequestJsObject::Create(webRequest); 85 WebRequestJsObject::Create(jsEngine);
106 global->Set(v8::String::New("_webRequest"), webRequestObject); 86 global->Set(v8::String::New("_webRequest"), webRequestObject);
107 const v8::Handle<v8::ObjectTemplate> consoleObject = 87 const v8::Handle<v8::ObjectTemplate> consoleObject =
108 ConsoleJsObject::Create(errorCallback); 88 ConsoleJsObject::Create(jsEngine);
109 global->Set(v8::String::New("console"), consoleObject); 89 global->Set(v8::String::New("console"), consoleObject);
110 return handleScope.Close(global); 90 return handleScope.Close(global);
111 } 91 }
OLDNEW
« no previous file with comments | « src/GlobalJsObject.h ('k') | src/JsEngine.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld