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

Delta Between Two Patch Sets: src/JsEngine.cpp

Issue 10213003: Make JsEngine::Evaluate() return a wrapper for v8::Value to accessdifferent variable types easily (Closed)
Left Patch Set: Extended JsValue API to support all FilterEngine functions Created April 13, 2013, 8:42 p.m.
Right Patch Set: Addressed review comments Created April 17, 2013, 7:56 a.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/FilterEngine.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::ErrorCallback& errorCallback, 11 AdblockPlus::FileSystem& fileSystem,
11 AdblockPlus::WebRequest& webRequest) 12 AdblockPlus::WebRequest& webRequest,
13 AdblockPlus::ErrorCallback& errorCallback)
12 { 14 {
13 const v8::Locker locker(isolate); 15 const v8::Locker locker(isolate);
14 const v8::HandleScope handleScope; 16 const v8::HandleScope handleScope;
15 const v8::Handle<v8::ObjectTemplate> global = 17 const v8::Handle<v8::ObjectTemplate> global =
16 AdblockPlus::GlobalJsObject::Create(errorCallback, webRequest); 18 AdblockPlus::GlobalJsObject::Create(fileSystem, webRequest,
19 errorCallback);
17 return v8::Context::New(0, global); 20 return v8::Context::New(0, global);
18 } 21 }
19 22
20 v8::Handle<v8::Script> CompileScript(const std::string& source, const std::str ing& filename) 23 v8::Handle<v8::Script> CompileScript(const std::string& source, const std::str ing& filename)
21 { 24 {
22 const v8::Handle<v8::String> v8Source = v8::String::New(source.c_str()); 25 const v8::Handle<v8::String> v8Source = v8::String::New(source.c_str());
23 if (filename.length()) 26 if (filename.length())
24 { 27 {
25 const v8::Handle<v8::String> v8Filename = v8::String::New(filename.c_str() ); 28 const v8::Handle<v8::String> v8Filename = v8::String::New(filename.c_str() );
26 return v8::Script::Compile(v8Source, v8Filename); 29 return v8::Script::Compile(v8Source, v8Filename);
27 } 30 }
28 else 31 else
29 return v8::Script::Compile(v8Source); 32 return v8::Script::Compile(v8Source);
30 } 33 }
31 34
32 void CheckTryCatch(const v8::TryCatch& tryCatch) 35 void CheckTryCatch(const v8::TryCatch& tryCatch)
33 { 36 {
34 if (tryCatch.HasCaught()) 37 if (tryCatch.HasCaught())
35 throw AdblockPlus::JsError(tryCatch.Exception(), tryCatch.Message()); 38 throw AdblockPlus::JsError(tryCatch.Exception(), tryCatch.Message());
36 }
37
38 std::string Slurp(std::istream& stream)
39 {
40 std::stringstream content;
41 content << stream.rdbuf();
42 return content.str();
43 } 39 }
44 40
45 std::string ExceptionToString(const v8::Handle<v8::Value> exception, 41 std::string ExceptionToString(const v8::Handle<v8::Value> exception,
46 const v8::Handle<v8::Message> message) 42 const v8::Handle<v8::Message> message)
47 { 43 {
48 std::stringstream error; 44 std::stringstream error;
49 error << *v8::String::Utf8Value(exception); 45 error << *v8::String::Utf8Value(exception);
50 if (!message.IsEmpty()) 46 if (!message.IsEmpty())
51 { 47 {
52 error << " at "; 48 error << " at ";
53 error << *v8::String::Utf8Value(message->GetScriptResourceName()); 49 error << *v8::String::Utf8Value(message->GetScriptResourceName());
54 error << ":"; 50 error << ":";
55 error << message->GetLineNumber(); 51 error << message->GetLineNumber();
56 } 52 }
57 return error.str(); 53 return error.str();
58 } 54 }
59 } 55 }
60 56
61 AdblockPlus::JsError::JsError(const v8::Handle<v8::Value> exception, 57 AdblockPlus::JsError::JsError(const v8::Handle<v8::Value> exception,
62 const v8::Handle<v8::Message> message) 58 const v8::Handle<v8::Message> message)
63 : std::runtime_error(ExceptionToString(exception, message)) 59 : std::runtime_error(ExceptionToString(exception, message))
64 { 60 {
65 } 61 }
66 62
67 AdblockPlus::JsEngine::JsEngine(const FileReader* const fileReader, 63 AdblockPlus::JsEngine::JsEngine(FileSystem* const fileSystem,
68 WebRequest* const webRequest, 64 WebRequest* const webRequest,
69 ErrorCallback* const errorCallback) 65 ErrorCallback* const errorCallback)
70 : fileReader(fileReader), isolate(v8::Isolate::GetCurrent()), 66 : fileSystem(fileSystem), isolate(v8::Isolate::GetCurrent()),
71 context(CreateContext(isolate, *errorCallback, *webRequest)) 67 context(CreateContext(isolate, *fileSystem, *webRequest, *errorCallback))
72 { 68 {
73 } 69 }
74 70
75 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::Evaluate(const std::string& sourc e, 71 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::Evaluate(const std::string& sourc e,
76 const std::string& filename) 72 const std::string& filename)
77 { 73 {
78 const Context context(*this); 74 const Context context(*this);
79 const v8::TryCatch tryCatch; 75 const v8::TryCatch tryCatch;
80 const v8::Handle<v8::Script> script = CompileScript(source, filename); 76 const v8::Handle<v8::Script> script = CompileScript(source, filename);
81 CheckTryCatch(tryCatch); 77 CheckTryCatch(tryCatch);
82 v8::Local<v8::Value> result = script->Run(); 78 v8::Local<v8::Value> result = script->Run();
83 CheckTryCatch(tryCatch); 79 CheckTryCatch(tryCatch);
84 return JsValuePtr(new JsValue(*this, result)); 80 return JsValuePtr(new JsValue(*this, result));
85 } 81 }
86 82
87 void AdblockPlus::JsEngine::Load(const std::string& scriptPath) 83 void AdblockPlus::JsEngine::Load(const std::string& scriptPath)
88 { 84 {
89 const std::auto_ptr<std::istream> file = fileReader->Read(scriptPath); 85 const std::tr1::shared_ptr<std::istream> file = fileSystem->Read(scriptPath);
90 if (!*file) 86 if (!file || !*file)
91 throw std::runtime_error("Unable to load script " + scriptPath); 87 throw std::runtime_error("Unable to load script " + scriptPath);
92 Evaluate(Slurp(*file)); 88 Evaluate(Utils::Slurp(*file));
93 } 89 }
94 90
95 void AdblockPlus::JsEngine::Gc() 91 void AdblockPlus::JsEngine::Gc()
96 { 92 {
97 while (!v8::V8::IdleNotification()); 93 while (!v8::V8::IdleNotification());
98 } 94 }
99 95
100 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(const std::string& val) 96 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(const std::string& val)
101 { 97 {
102 const Context context(*this); 98 const Context context(*this);
(...skipping 10 matching lines...) Expand all
113 { 109 {
114 const Context context(*this); 110 const Context context(*this);
115 return JsValuePtr(new JsValue(*this, v8::Boolean::New(val))); 111 return JsValuePtr(new JsValue(*this, v8::Boolean::New(val)));
116 } 112 }
117 113
118 AdblockPlus::JsEngine::Context::Context(const JsEngine& jsEngine) 114 AdblockPlus::JsEngine::Context::Context(const JsEngine& jsEngine)
119 : locker(jsEngine.isolate), handleScope(), 115 : locker(jsEngine.isolate), handleScope(),
120 contextScope(jsEngine.context) 116 contextScope(jsEngine.context)
121 { 117 {
122 } 118 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld