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

Delta Between Two Patch Sets: src/JsEngine.cpp

Issue 10274013: Simplified JsEngine API, Call() and GetVariable() aren`t reallynecessary if Evaluate() returns a v… (Closed)
Left Patch Set: Created April 11, 2013, 4:13 p.m.
Right Patch Set: Slight improvement Created April 11, 2013, 5:44 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 | « include/AdblockPlus/JsEngine.h ('k') | test/GlobalJsObject.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 5
6 namespace 6 namespace
7 { 7 {
8 v8::Handle<v8::Context> CreateContext( 8 v8::Handle<v8::Context> CreateContext(
9 AdblockPlus::ErrorCallback& errorCallback, 9 AdblockPlus::ErrorCallback& errorCallback,
10 AdblockPlus::WebRequest& webRequest) 10 AdblockPlus::WebRequest& webRequest)
11 { 11 {
12 const v8::Locker locker(v8::Isolate::GetCurrent()); 12 const v8::Locker locker(v8::Isolate::GetCurrent());
13 const v8::HandleScope handleScope; 13 const v8::HandleScope handleScope;
14 const v8::Handle<v8::ObjectTemplate> global = 14 const v8::Handle<v8::ObjectTemplate> global =
15 AdblockPlus::GlobalJsObject::Create(errorCallback, webRequest); 15 AdblockPlus::GlobalJsObject::Create(errorCallback, webRequest);
16 return v8::Context::New(0, global); 16 return v8::Context::New(0, global);
17 } 17 }
18 18
19 v8::Handle<v8::Script> CompileScript(const char* source, const char* filename) 19 v8::Handle<v8::Script> CompileScript(const std::string& source, const std::str ing& filename)
20 { 20 {
21 const v8::Handle<v8::String> v8Source = v8::String::New(source); 21 const v8::Handle<v8::String> v8Source = v8::String::New(source.c_str());
22 if (filename && filename[0]) 22 if (filename.length())
23 { 23 {
24 const v8::Handle<v8::String> v8Filename = v8::String::New(filename); 24 const v8::Handle<v8::String> v8Filename = v8::String::New(filename.c_str() );
25 return v8::Script::Compile(v8Source, v8Filename); 25 return v8::Script::Compile(v8Source, v8Filename);
26 } 26 }
27 else 27 else
28 return v8::Script::Compile(v8Source); 28 return v8::Script::Compile(v8Source);
29 } 29 }
30 30
31 void CheckTryCatch(const v8::TryCatch& tryCatch) 31 void CheckTryCatch(const v8::TryCatch& tryCatch)
32 { 32 {
33 if (tryCatch.HasCaught()) 33 if (tryCatch.HasCaught())
34 throw AdblockPlus::JsError(tryCatch.Exception(), tryCatch.Message()); 34 throw AdblockPlus::JsError(tryCatch.Exception(), tryCatch.Message());
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
70 { 70 {
71 } 71 }
72 72
73 std::string AdblockPlus::JsEngine::Evaluate(const std::string& source, 73 std::string AdblockPlus::JsEngine::Evaluate(const std::string& source,
74 const std::string& filename) 74 const std::string& filename)
75 { 75 {
76 const v8::Locker locker(v8::Isolate::GetCurrent()); 76 const v8::Locker locker(v8::Isolate::GetCurrent());
77 const v8::HandleScope handleScope; 77 const v8::HandleScope handleScope;
78 const v8::Context::Scope contextScope(context); 78 const v8::Context::Scope contextScope(context);
79 const v8::TryCatch tryCatch; 79 const v8::TryCatch tryCatch;
80 const v8::Handle<v8::Script> script = CompileScript(source.c_str(), filename.c _str()); 80 const v8::Handle<v8::Script> script = CompileScript(source, filename);
81 CheckTryCatch(tryCatch); 81 CheckTryCatch(tryCatch);
82 v8::Local<v8::Value> result = script->Run(); 82 v8::Local<v8::Value> result = script->Run();
83 CheckTryCatch(tryCatch); 83 CheckTryCatch(tryCatch);
84 v8::String::Utf8Value resultString(result); 84 v8::String::Utf8Value resultString(result);
85 return std::string(*resultString); 85 return std::string(*resultString);
86 } 86 }
87 87
88 void AdblockPlus::JsEngine::Load(const std::string& scriptPath) 88 void AdblockPlus::JsEngine::Load(const std::string& scriptPath)
89 { 89 {
90 const std::auto_ptr<std::istream> file = fileReader->Read(scriptPath); 90 const std::auto_ptr<std::istream> file = fileReader->Read(scriptPath);
91 if (!*file) 91 if (!*file)
92 throw std::runtime_error("Unable to load script " + scriptPath); 92 throw std::runtime_error("Unable to load script " + scriptPath);
93 Evaluate(Slurp(*file)); 93 Evaluate(Slurp(*file));
94 } 94 }
95 95
96 void AdblockPlus::JsEngine::Gc() 96 void AdblockPlus::JsEngine::Gc()
97 { 97 {
98 while (!v8::V8::IdleNotification()); 98 while (!v8::V8::IdleNotification());
99 } 99 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld