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

Side by Side Diff: src/JsEngine.cpp

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

Powered by Google App Engine
This is Rietveld