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

Delta Between Two Patch Sets: src/JsEngine.cpp

Issue 9846017: Make JavaScript sources compile into the library; convert JavaScript files on the fly (Closed)
Left Patch Set: Created March 14, 2013, 10:02 p.m.
Right Patch Set: Review comments addressed Created March 15, 2013, 3:59 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 | « shell/src/Main.cpp ('k') | no next file » | 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 "ConsoleJsObject.h" 4 #include "ConsoleJsObject.h"
5 5
6 extern const char* jsSources[]; 6 extern const char* jsSources[];
7 7
8 namespace 8 namespace
9 { 9 {
10 v8::Handle<v8::Context> CreateContext( 10 v8::Handle<v8::Context> CreateContext(
11 AdblockPlus::ErrorCallback& errorCallback) 11 AdblockPlus::ErrorCallback& errorCallback)
12 { 12 {
13 const v8::HandleScope handleScope; 13 const v8::HandleScope handleScope;
14 const v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(); 14 const v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New();
15 global->Set(v8::String::New("console"), 15 global->Set(v8::String::New("console"),
16 AdblockPlus::ConsoleJsObject::Create(errorCallback)); 16 AdblockPlus::ConsoleJsObject::Create(errorCallback));
17 return v8::Context::New(0, global); 17 return v8::Context::New(0, global);
18 } 18 }
19
20 v8::Handle<v8::Script> CompileScript(const char* source, const char* filename)
21 {
22 const v8::Handle<v8::String> v8Source = v8::String::New(source);
23 if (filename && filename[0])
24 {
25 const v8::Handle<v8::String> v8Filename = v8::String::New(filename);
26 return v8::Script::Compile(v8Source, v8Filename);
27 }
28 else
29 return v8::Script::Compile(v8Source);
30 }
31
19 32
20 void CheckTryCatch(const v8::TryCatch& tryCatch) 33 void CheckTryCatch(const v8::TryCatch& tryCatch)
21 { 34 {
22 if (tryCatch.HasCaught()) 35 if (tryCatch.HasCaught())
23 throw AdblockPlus::JsError(tryCatch.Exception()); 36 throw AdblockPlus::JsError(tryCatch.Exception());
24 } 37 }
25 38
26 std::string Slurp(std::istream& stream) 39 std::string Slurp(std::istream& stream)
27 { 40 {
28 std::stringstream content; 41 std::stringstream content;
29 content << stream.rdbuf(); 42 content << stream.rdbuf();
30 return content.str(); 43 return content.str();
31 } 44 }
32 } 45 }
33 46
34 AdblockPlus::JsError::JsError(const v8::Handle<v8::Value> exception) 47 AdblockPlus::JsError::JsError(const v8::Handle<v8::Value> exception)
35 : std::runtime_error(*v8::String::AsciiValue(exception)) 48 : std::runtime_error(*v8::String::AsciiValue(exception))
36 { 49 {
37 } 50 }
38 51
39 AdblockPlus::JsEngine::JsEngine(const FileReader* const fileReader, 52 AdblockPlus::JsEngine::JsEngine(const FileReader* const fileReader,
40 ErrorCallback* const errorCallback) 53 ErrorCallback* const errorCallback)
41 : fileReader(fileReader), context(CreateContext(*errorCallback)) 54 : fileReader(fileReader), context(CreateContext(*errorCallback))
42 { 55 {
43 for (int i = 0; jsSources[i] && jsSources[i + 1]; i += 2) 56 for (int i = 0; jsSources[i] && jsSources[i + 1]; i += 2)
44 Evaluate(jsSources[i + 1], jsSources[i]); 57 Evaluate(jsSources[i + 1], jsSources[i]);
45 } 58 }
46 59
47 v8::Handle<v8::Script> AdblockPlus::JsEngine::CompileScript(const char* source, const char* filename)
48 {
49 const v8::Handle<v8::String> v8Source = v8::String::New(source);
50 if (filename)
51 {
52 const v8::Handle<v8::String> v8Filename = v8::String::New(filename);
53 return v8::Script::Compile(v8Source, v8Filename);
54 }
55 else
56 return v8::Script::Compile(v8Source);
57 }
58
59 void AdblockPlus::JsEngine::Evaluate(const char* source, const char* filename) 60 void AdblockPlus::JsEngine::Evaluate(const char* source, const char* filename)
60 { 61 {
61 const v8::HandleScope handleScope; 62 const v8::HandleScope handleScope;
62 const v8::Context::Scope contextScope(context); 63 const v8::Context::Scope contextScope(context);
63 const v8::TryCatch tryCatch; 64 const v8::TryCatch tryCatch;
64 const v8::Handle<v8::Script> script = CompileScript(source, filename); 65 const v8::Handle<v8::Script> script = CompileScript(source, filename);
65 CheckTryCatch(tryCatch); 66 CheckTryCatch(tryCatch);
66 script->Run(); 67 script->Run();
67 CheckTryCatch(tryCatch); 68 CheckTryCatch(tryCatch);
68 } 69 }
70
71 void AdblockPlus::JsEngine::Evaluate(const std::string& source,
72 const std::string& filename)
73 {
74 Evaluate(source.c_str(), filename.c_str());
75 }
76
69 77
70 void AdblockPlus::JsEngine::Load(const std::string& scriptPath) 78 void AdblockPlus::JsEngine::Load(const std::string& scriptPath)
71 { 79 {
72 const std::auto_ptr<std::istream> file = fileReader->Read(scriptPath); 80 const std::auto_ptr<std::istream> file = fileReader->Read(scriptPath);
73 if (!*file) 81 if (!*file)
74 throw std::runtime_error("Unable to load script " + scriptPath); 82 throw std::runtime_error("Unable to load script " + scriptPath);
75 Evaluate(Slurp(*file)); 83 Evaluate(Slurp(*file));
76 } 84 }
77 85
78 std::string AdblockPlus::JsEngine::Call(const std::string& functionName) 86 std::string AdblockPlus::JsEngine::Call(const std::string& functionName)
79 { 87 {
80 const v8::HandleScope handleScope; 88 const v8::HandleScope handleScope;
81 const v8::Context::Scope contextScope(context); 89 const v8::Context::Scope contextScope(context);
82 const v8::Local<v8::Object> global = context->Global(); 90 const v8::Local<v8::Object> global = context->Global();
83 const v8::Local<v8::Function> function = v8::Local<v8::Function>::Cast( 91 const v8::Local<v8::Function> function = v8::Local<v8::Function>::Cast(
84 global->Get(v8::String::New(functionName.c_str()))); 92 global->Get(v8::String::New(functionName.c_str())));
85 const v8::TryCatch tryCatch; 93 const v8::TryCatch tryCatch;
86 const v8::Local<v8::Value> result = function->Call(function, 0, 0); 94 const v8::Local<v8::Value> result = function->Call(function, 0, 0);
87 CheckTryCatch(tryCatch); 95 CheckTryCatch(tryCatch);
88 const v8::String::AsciiValue ascii(result); 96 const v8::String::AsciiValue ascii(result);
89 return *ascii; 97 return *ascii;
90 } 98 }
91 99
92 void AdblockPlus::JsEngine::Gc() 100 void AdblockPlus::JsEngine::Gc()
93 { 101 {
94 while (!v8::V8::IdleNotification()); 102 while (!v8::V8::IdleNotification());
95 } 103 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld