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

Side by Side Diff: src/JsEngine.cpp

Issue 10296001: Implement File API (Closed)
Patch Set: Don't pass a blog to _fileSystem.write Created April 12, 2013, 12:10 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
OLDNEW
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 AdblockPlus::ErrorCallback& errorCallback, 10 AdblockPlus::FileSystem& fileSystem,
10 AdblockPlus::WebRequest& webRequest) 11 AdblockPlus::WebRequest& webRequest,
12 AdblockPlus::ErrorCallback& errorCallback)
11 { 13 {
12 const v8::Locker locker(v8::Isolate::GetCurrent()); 14 const v8::Locker locker(v8::Isolate::GetCurrent());
13 const v8::HandleScope handleScope; 15 const v8::HandleScope handleScope;
14 const v8::Handle<v8::ObjectTemplate> global = 16 const v8::Handle<v8::ObjectTemplate> global =
15 AdblockPlus::GlobalJsObject::Create(errorCallback, webRequest); 17 AdblockPlus::GlobalJsObject::Create(fileSystem, webRequest,
18 errorCallback);
16 return v8::Context::New(0, global); 19 return v8::Context::New(0, global);
17 } 20 }
18 21
19 v8::Handle<v8::Script> CompileScript(const std::string& source, const std::str ing& filename) 22 v8::Handle<v8::Script> CompileScript(const std::string& source, const std::str ing& filename)
20 { 23 {
21 const v8::Handle<v8::String> v8Source = v8::String::New(source.c_str()); 24 const v8::Handle<v8::String> v8Source = v8::String::New(source.c_str());
22 if (filename.length()) 25 if (filename.length())
23 { 26 {
24 const v8::Handle<v8::String> v8Filename = v8::String::New(filename.c_str() ); 27 const v8::Handle<v8::String> v8Filename = v8::String::New(filename.c_str() );
25 return v8::Script::Compile(v8Source, v8Filename); 28 return v8::Script::Compile(v8Source, v8Filename);
26 } 29 }
27 else 30 else
28 return v8::Script::Compile(v8Source); 31 return v8::Script::Compile(v8Source);
29 } 32 }
30 33
31 void CheckTryCatch(const v8::TryCatch& tryCatch) 34 void CheckTryCatch(const v8::TryCatch& tryCatch)
32 { 35 {
33 if (tryCatch.HasCaught()) 36 if (tryCatch.HasCaught())
34 throw AdblockPlus::JsError(tryCatch.Exception(), tryCatch.Message()); 37 throw AdblockPlus::JsError(tryCatch.Exception(), tryCatch.Message());
35 } 38 }
36 39
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, 40 std::string ExceptionToString(const v8::Handle<v8::Value> exception,
45 const v8::Handle<v8::Message> message) 41 const v8::Handle<v8::Message> message)
46 { 42 {
47 std::stringstream error; 43 std::stringstream error;
48 error << *v8::String::Utf8Value(exception); 44 error << *v8::String::Utf8Value(exception);
49 if (!message.IsEmpty()) 45 if (!message.IsEmpty())
50 { 46 {
51 error << " at "; 47 error << " at ";
52 error << *v8::String::Utf8Value(message->GetScriptResourceName()); 48 error << *v8::String::Utf8Value(message->GetScriptResourceName());
53 error << ":"; 49 error << ":";
54 error << message->GetLineNumber(); 50 error << message->GetLineNumber();
55 } 51 }
56 return error.str(); 52 return error.str();
57 } 53 }
58 } 54 }
59 55
60 AdblockPlus::JsError::JsError(const v8::Handle<v8::Value> exception, 56 AdblockPlus::JsError::JsError(const v8::Handle<v8::Value> exception,
61 const v8::Handle<v8::Message> message) 57 const v8::Handle<v8::Message> message)
62 : std::runtime_error(ExceptionToString(exception, message)) 58 : std::runtime_error(ExceptionToString(exception, message))
63 { 59 {
64 } 60 }
65 61
66 AdblockPlus::JsEngine::JsEngine(const FileReader* const fileReader, 62 AdblockPlus::JsEngine::JsEngine(FileSystem* const fileSystem,
67 WebRequest* const webRequest, 63 WebRequest* const webRequest,
68 ErrorCallback* const errorCallback) 64 ErrorCallback* const errorCallback)
69 : fileReader(fileReader), context(CreateContext(*errorCallback, *webRequest)) 65 : fileSystem(fileSystem),
66 context(CreateContext(*fileSystem, *webRequest, *errorCallback))
70 { 67 {
71 } 68 }
72 69
73 std::string AdblockPlus::JsEngine::Evaluate(const std::string& source, 70 std::string AdblockPlus::JsEngine::Evaluate(const std::string& source,
74 const std::string& filename) 71 const std::string& filename)
75 { 72 {
76 const v8::Locker locker(v8::Isolate::GetCurrent()); 73 const v8::Locker locker(v8::Isolate::GetCurrent());
77 const v8::HandleScope handleScope; 74 const v8::HandleScope handleScope;
78 const v8::Context::Scope contextScope(context); 75 const v8::Context::Scope contextScope(context);
79 const v8::TryCatch tryCatch; 76 const v8::TryCatch tryCatch;
80 const v8::Handle<v8::Script> script = CompileScript(source, filename); 77 const v8::Handle<v8::Script> script = CompileScript(source, filename);
81 CheckTryCatch(tryCatch); 78 CheckTryCatch(tryCatch);
82 v8::Local<v8::Value> result = script->Run(); 79 v8::Local<v8::Value> result = script->Run();
83 CheckTryCatch(tryCatch); 80 CheckTryCatch(tryCatch);
84 v8::String::Utf8Value resultString(result); 81 v8::String::Utf8Value resultString(result);
85 return std::string(*resultString); 82 return std::string(*resultString);
86 } 83 }
87 84
88 void AdblockPlus::JsEngine::Load(const std::string& scriptPath) 85 void AdblockPlus::JsEngine::Load(const std::string& scriptPath)
89 { 86 {
90 const std::auto_ptr<std::istream> file = fileReader->Read(scriptPath); 87 const std::auto_ptr<std::istream> file = fileSystem->Read(scriptPath);
91 if (!*file) 88 if (!file.get() || !*file)
Wladimir Palant 2013/04/12 16:10:35 I would suspect that !file.get() is the same as !f
Felix Dahlke 2013/04/15 03:43:34 No, auto_ptr doesn't. Surprised me as well.
92 throw std::runtime_error("Unable to load script " + scriptPath); 89 throw std::runtime_error("Unable to load script " + scriptPath);
93 Evaluate(Slurp(*file)); 90 Evaluate(Utils::Slurp(*file));
94 } 91 }
95 92
96 void AdblockPlus::JsEngine::Gc() 93 void AdblockPlus::JsEngine::Gc()
97 { 94 {
98 while (!v8::V8::IdleNotification()); 95 while (!v8::V8::IdleNotification());
99 } 96 }
OLDNEW
« src/GlobalJsObject.cpp ('K') | « src/GlobalJsObject.cpp ('k') | src/Utils.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld