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

Delta Between Two Patch Sets: src/JsEngine.cpp

Issue 10198022: Pass application data into libadblockplus (Closed)
Left Patch Set: Created April 12, 2013, 1:23 p.m.
Right Patch Set: Merged upstream, addressed issues, renamed AppInfo fields Created April 17, 2013, 2:47 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
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(
10 v8::Isolate* isolate,
9 const AdblockPlus::AppInfo& appInfo, 11 const AdblockPlus::AppInfo& appInfo,
10 AdblockPlus::ErrorCallback& errorCallback, 12 AdblockPlus::JsEngine& jsEngine)
11 AdblockPlus::WebRequest& webRequest)
12 { 13 {
13 const v8::Locker locker(v8::Isolate::GetCurrent()); 14 const v8::Locker locker(isolate);
14 const v8::HandleScope handleScope; 15 const v8::HandleScope handleScope;
15 const v8::Handle<v8::ObjectTemplate> global = 16 const v8::Handle<v8::ObjectTemplate> global =
16 AdblockPlus::GlobalJsObject::Create(appInfo, errorCallback, webRequest); 17 AdblockPlus::GlobalJsObject::Create(appInfo, jsEngine);
17 return v8::Context::New(0, global); 18 return v8::Context::New(0, global);
18 } 19 }
19 20
20 v8::Handle<v8::Script> CompileScript(const std::string& source, const std::str ing& filename) 21 v8::Handle<v8::Script> CompileScript(const std::string& source, const std::str ing& filename)
21 { 22 {
22 const v8::Handle<v8::String> v8Source = v8::String::New(source.c_str()); 23 const v8::Handle<v8::String> v8Source = v8::String::New(source.c_str());
23 if (filename.length()) 24 if (filename.length())
24 { 25 {
25 const v8::Handle<v8::String> v8Filename = v8::String::New(filename.c_str() ); 26 const v8::Handle<v8::String> v8Filename = v8::String::New(filename.c_str() );
26 return v8::Script::Compile(v8Source, v8Filename); 27 return v8::Script::Compile(v8Source, v8Filename);
27 } 28 }
28 else 29 else
29 return v8::Script::Compile(v8Source); 30 return v8::Script::Compile(v8Source);
30 } 31 }
31 32
32 void CheckTryCatch(const v8::TryCatch& tryCatch) 33 void CheckTryCatch(const v8::TryCatch& tryCatch)
33 { 34 {
34 if (tryCatch.HasCaught()) 35 if (tryCatch.HasCaught())
35 throw AdblockPlus::JsError(tryCatch.Exception(), tryCatch.Message()); 36 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 } 37 }
44 38
45 std::string ExceptionToString(const v8::Handle<v8::Value> exception, 39 std::string ExceptionToString(const v8::Handle<v8::Value> exception,
46 const v8::Handle<v8::Message> message) 40 const v8::Handle<v8::Message> message)
47 { 41 {
48 std::stringstream error; 42 std::stringstream error;
49 error << *v8::String::Utf8Value(exception); 43 error << *v8::String::Utf8Value(exception);
50 if (!message.IsEmpty()) 44 if (!message.IsEmpty())
51 { 45 {
52 error << " at "; 46 error << " at ";
53 error << *v8::String::Utf8Value(message->GetScriptResourceName()); 47 error << *v8::String::Utf8Value(message->GetScriptResourceName());
54 error << ":"; 48 error << ":";
55 error << message->GetLineNumber(); 49 error << message->GetLineNumber();
56 } 50 }
57 return error.str(); 51 return error.str();
58 } 52 }
59 } 53 }
60 54
61 AdblockPlus::JsError::JsError(const v8::Handle<v8::Value> exception, 55 AdblockPlus::JsError::JsError(const v8::Handle<v8::Value> exception,
62 const v8::Handle<v8::Message> message) 56 const v8::Handle<v8::Message> message)
63 : std::runtime_error(ExceptionToString(exception, message)) 57 : std::runtime_error(ExceptionToString(exception, message))
64 { 58 {
65 } 59 }
66 60
67 AdblockPlus::JsEngine::JsEngine(const AppInfo& appInfo, 61 AdblockPlus::JsEngine::JsEngine(const AppInfo& appInfo,
68 const FileReader* const fileReader, 62 FileSystem* const fileSystem,
69 WebRequest* const webRequest, 63 WebRequest* const webRequest,
70 ErrorCallback* const errorCallback) 64 ErrorCallback* const errorCallback)
71 : fileReader(fileReader), 65 : fileSystem(*fileSystem), webRequest(*webRequest),
72 context(CreateContext(appInfo, *errorCallback, *webRequest)) 66 errorCallback(*errorCallback), isolate(v8::Isolate::GetCurrent()),
67 context(CreateContext(isolate, appInfo, *this))
73 { 68 {
74 } 69 }
75 70
76 std::string AdblockPlus::JsEngine::Evaluate(const std::string& source, 71 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::Evaluate(const std::string& sourc e,
77 const std::string& filename) 72 const std::string& filename)
78 { 73 {
79 const v8::Locker locker(v8::Isolate::GetCurrent()); 74 const Context context(*this);
80 const v8::HandleScope handleScope;
81 const v8::Context::Scope contextScope(context);
82 const v8::TryCatch tryCatch; 75 const v8::TryCatch tryCatch;
83 const v8::Handle<v8::Script> script = CompileScript(source, filename); 76 const v8::Handle<v8::Script> script = CompileScript(source, filename);
84 CheckTryCatch(tryCatch); 77 CheckTryCatch(tryCatch);
85 v8::Local<v8::Value> result = script->Run(); 78 v8::Local<v8::Value> result = script->Run();
86 CheckTryCatch(tryCatch); 79 CheckTryCatch(tryCatch);
87 v8::String::Utf8Value resultString(result); 80 return JsValuePtr(new JsValue(*this, result));
88 return std::string(*resultString);
89 } 81 }
90 82
91 void AdblockPlus::JsEngine::Load(const std::string& scriptPath) 83 void AdblockPlus::JsEngine::Load(const std::string& scriptPath)
92 { 84 {
93 const std::auto_ptr<std::istream> file = fileReader->Read(scriptPath); 85 const std::tr1::shared_ptr<std::istream> file = fileSystem.Read(scriptPath);
94 if (!*file) 86 if (!file || !*file)
95 throw std::runtime_error("Unable to load script " + scriptPath); 87 throw std::runtime_error("Unable to load script " + scriptPath);
96 Evaluate(Slurp(*file)); 88 Evaluate(Utils::Slurp(*file));
97 } 89 }
98 90
99 void AdblockPlus::JsEngine::Gc() 91 void AdblockPlus::JsEngine::Gc()
100 { 92 {
101 while (!v8::V8::IdleNotification()); 93 while (!v8::V8::IdleNotification());
102 } 94 }
95
96 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(const std::string& val)
97 {
98 const Context context(*this);
99 return JsValuePtr(new JsValue(*this, v8::String::New(val.c_str(), val.length() )));
100 }
101
102 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(int64_t val)
103 {
104 const Context context(*this);
105 return JsValuePtr(new JsValue(*this, v8::Integer::New(val)));
106 }
107
108 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(bool val)
109 {
110 const Context context(*this);
111 return JsValuePtr(new JsValue(*this, v8::Boolean::New(val)));
112 }
113
114 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewObject()
115 {
116 const Context context(*this);
117 return JsValuePtr(new JsValue(*this, v8::Object::New()));
118 }
119
120 AdblockPlus::JsEngine& AdblockPlus::JsEngine::FromArguments(const v8::Arguments& arguments)
121 {
122 const v8::Local<const v8::External> external =
123 v8::Local<const v8::External>::Cast(arguments.Data());
124 return *static_cast<JsEngine* const>(external->Value());
125 }
126
127 AdblockPlus::JsValueList AdblockPlus::JsEngine::ConvertArguments(const v8::Argum ents& arguments)
128 {
129 const Context context(*this);
130 JsValueList list;
131 for (int i = 0; i < arguments.Length(); i++)
132 list.push_back(JsValuePtr(new JsValue(*this, arguments[i])));
133 return list;
134 }
135
136 AdblockPlus::JsEngine::Context::Context(const JsEngine& jsEngine)
137 : locker(jsEngine.isolate), handleScope(),
138 contextScope(jsEngine.context)
139 {
140 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld