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

Delta Between Two Patch Sets: src/JsEngine.cpp

Issue 10184021: Some refactoring of global JavaScript objects (Closed)
Left Patch Set: Created April 15, 2013, 2:54 p.m.
Right 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:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « src/GlobalJsObject.cpp ('k') | src/JsValue.cpp » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 #include <AdblockPlus.h> 1 #include <AdblockPlus.h>
2 #include <sstream> 2 #include <sstream>
3 3
4 #include "GlobalJsObject.h" 4 #include "GlobalJsObject.h"
5 #include "Utils.h"
5 6
6 namespace 7 namespace
7 { 8 {
8 v8::Handle<v8::Context> CreateContext( 9 v8::Handle<v8::Context> CreateContext(
9 v8::Isolate* isolate, 10 v8::Isolate* isolate,
10 AdblockPlus::JsEngine& jsEngine) 11 AdblockPlus::JsEngine& jsEngine)
11 { 12 {
12 const v8::Locker locker(isolate); 13 const v8::Locker locker(isolate);
13 const v8::HandleScope handleScope; 14 const v8::HandleScope handleScope;
14 const v8::Handle<v8::ObjectTemplate> global = 15 const v8::Handle<v8::ObjectTemplate> global =
(...skipping 12 matching lines...) Expand all
27 else 28 else
28 return v8::Script::Compile(v8Source); 29 return v8::Script::Compile(v8Source);
29 } 30 }
30 31
31 void CheckTryCatch(const v8::TryCatch& tryCatch) 32 void CheckTryCatch(const v8::TryCatch& tryCatch)
32 { 33 {
33 if (tryCatch.HasCaught()) 34 if (tryCatch.HasCaught())
34 throw AdblockPlus::JsError(tryCatch.Exception(), tryCatch.Message()); 35 throw AdblockPlus::JsError(tryCatch.Exception(), tryCatch.Message());
35 } 36 }
36 37
37 std::string Slurp(std::istream& stream)
38 {
39 std::stringstream content;
40 content << stream.rdbuf();
41 return content.str();
42 }
43
44 std::string ExceptionToString(const v8::Handle<v8::Value> exception, 38 std::string ExceptionToString(const v8::Handle<v8::Value> exception,
45 const v8::Handle<v8::Message> message) 39 const v8::Handle<v8::Message> message)
46 { 40 {
47 std::stringstream error; 41 std::stringstream error;
48 error << *v8::String::Utf8Value(exception); 42 error << *v8::String::Utf8Value(exception);
49 if (!message.IsEmpty()) 43 if (!message.IsEmpty())
50 { 44 {
51 error << " at "; 45 error << " at ";
52 error << *v8::String::Utf8Value(message->GetScriptResourceName()); 46 error << *v8::String::Utf8Value(message->GetScriptResourceName());
53 error << ":"; 47 error << ":";
54 error << message->GetLineNumber(); 48 error << message->GetLineNumber();
55 } 49 }
56 return error.str(); 50 return error.str();
57 } 51 }
58 } 52 }
59 53
60 AdblockPlus::JsError::JsError(const v8::Handle<v8::Value> exception, 54 AdblockPlus::JsError::JsError(const v8::Handle<v8::Value> exception,
61 const v8::Handle<v8::Message> message) 55 const v8::Handle<v8::Message> message)
62 : std::runtime_error(ExceptionToString(exception, message)) 56 : std::runtime_error(ExceptionToString(exception, message))
63 { 57 {
64 } 58 }
65 59
66 AdblockPlus::JsEngine::JsEngine(FileReader* const fileReader, 60 AdblockPlus::JsEngine::JsEngine(FileSystem* const fileSystem,
67 WebRequest* const webRequest, 61 WebRequest* const webRequest,
68 ErrorCallback* const errorCallback) 62 ErrorCallback* const errorCallback)
69 : fileReader(*fileReader), webRequest(*webRequest), 63 : fileSystem(*fileSystem), webRequest(*webRequest),
70 errorCallback(*errorCallback), isolate(v8::Isolate::GetCurrent()), 64 errorCallback(*errorCallback), isolate(v8::Isolate::GetCurrent()),
71 context(CreateContext(isolate, *this)) 65 context(CreateContext(isolate, *this))
72 { 66 {
73 } 67 }
74 68
75 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::Evaluate(const std::string& sourc e, 69 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::Evaluate(const std::string& sourc e,
76 const std::string& filename) 70 const std::string& filename)
77 { 71 {
78 const Context context(*this); 72 const Context context(*this);
79 const v8::TryCatch tryCatch; 73 const v8::TryCatch tryCatch;
80 const v8::Handle<v8::Script> script = CompileScript(source, filename); 74 const v8::Handle<v8::Script> script = CompileScript(source, filename);
81 CheckTryCatch(tryCatch); 75 CheckTryCatch(tryCatch);
82 v8::Local<v8::Value> result = script->Run(); 76 v8::Local<v8::Value> result = script->Run();
83 CheckTryCatch(tryCatch); 77 CheckTryCatch(tryCatch);
84 return JsValuePtr(new JsValue(*this, result)); 78 return JsValuePtr(new JsValue(*this, result));
85 } 79 }
86 80
87 void AdblockPlus::JsEngine::Load(const std::string& scriptPath) 81 void AdblockPlus::JsEngine::Load(const std::string& scriptPath)
88 { 82 {
89 const std::auto_ptr<std::istream> file = fileReader.Read(scriptPath); 83 const std::tr1::shared_ptr<std::istream> file = fileSystem.Read(scriptPath);
90 if (!*file) 84 if (!file || !*file)
91 throw std::runtime_error("Unable to load script " + scriptPath); 85 throw std::runtime_error("Unable to load script " + scriptPath);
92 Evaluate(Slurp(*file)); 86 Evaluate(Utils::Slurp(*file));
93 } 87 }
94 88
95 void AdblockPlus::JsEngine::Gc() 89 void AdblockPlus::JsEngine::Gc()
96 { 90 {
97 while (!v8::V8::IdleNotification()); 91 while (!v8::V8::IdleNotification());
98 } 92 }
99 93
100 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(const std::string& val) 94 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(const std::string& val)
101 { 95 {
102 const Context context(*this); 96 const Context context(*this);
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
135 for (int i = 0; i < arguments.Length(); i++) 129 for (int i = 0; i < arguments.Length(); i++)
136 list.push_back(JsValuePtr(new JsValue(*this, arguments[i]))); 130 list.push_back(JsValuePtr(new JsValue(*this, arguments[i])));
137 return list; 131 return list;
138 } 132 }
139 133
140 AdblockPlus::JsEngine::Context::Context(const JsEngine& jsEngine) 134 AdblockPlus::JsEngine::Context::Context(const JsEngine& jsEngine)
141 : locker(jsEngine.isolate), handleScope(), 135 : locker(jsEngine.isolate), handleScope(),
142 contextScope(jsEngine.context) 136 contextScope(jsEngine.context)
143 { 137 {
144 } 138 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld