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: Created March 14, 2013, 10:02 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
« include/AdblockPlus/JsEngine.h ('K') | « 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);
(...skipping 15 matching lines...) Expand all
31 33
32 AdblockPlus::JsError::JsError(const v8::Handle<v8::Value> exception) 34 AdblockPlus::JsError::JsError(const v8::Handle<v8::Value> exception)
33 : std::runtime_error(*v8::String::AsciiValue(exception)) 35 : std::runtime_error(*v8::String::AsciiValue(exception))
34 { 36 {
35 } 37 }
36 38
37 AdblockPlus::JsEngine::JsEngine(const FileReader* const fileReader, 39 AdblockPlus::JsEngine::JsEngine(const FileReader* const fileReader,
38 ErrorCallback* const errorCallback) 40 ErrorCallback* const errorCallback)
39 : fileReader(fileReader), context(CreateContext(*errorCallback)) 41 : fileReader(fileReader), context(CreateContext(*errorCallback))
40 { 42 {
43 for (int i = 0; jsSources[i] && jsSources[i + 1]; i += 2)
44 Evaluate(jsSources[i + 1], jsSources[i]);
41 } 45 }
42 46
43 void AdblockPlus::JsEngine::Evaluate(const std::string& source) 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)
44 { 60 {
45 const v8::HandleScope handleScope; 61 const v8::HandleScope handleScope;
46 const v8::Context::Scope contextScope(context); 62 const v8::Context::Scope contextScope(context);
47 const v8::Handle<v8::String> v8Source = v8::String::New(source.c_str());
48 const v8::TryCatch tryCatch; 63 const v8::TryCatch tryCatch;
49 const v8::Handle<v8::Script> script = v8::Script::Compile(v8Source); 64 const v8::Handle<v8::Script> script = CompileScript(source, filename);
50 CheckTryCatch(tryCatch); 65 CheckTryCatch(tryCatch);
51 script->Run(); 66 script->Run();
52 CheckTryCatch(tryCatch); 67 CheckTryCatch(tryCatch);
53 } 68 }
54 69
55 void AdblockPlus::JsEngine::Load(const std::string& scriptPath) 70 void AdblockPlus::JsEngine::Load(const std::string& scriptPath)
56 { 71 {
57 const std::auto_ptr<std::istream> file = fileReader->Read(scriptPath); 72 const std::auto_ptr<std::istream> file = fileReader->Read(scriptPath);
58 if (!*file) 73 if (!*file)
59 throw std::runtime_error("Unable to load script " + scriptPath); 74 throw std::runtime_error("Unable to load script " + scriptPath);
(...skipping 11 matching lines...) Expand all
71 const v8::Local<v8::Value> result = function->Call(function, 0, 0); 86 const v8::Local<v8::Value> result = function->Call(function, 0, 0);
72 CheckTryCatch(tryCatch); 87 CheckTryCatch(tryCatch);
73 const v8::String::AsciiValue ascii(result); 88 const v8::String::AsciiValue ascii(result);
74 return *ascii; 89 return *ascii;
75 } 90 }
76 91
77 void AdblockPlus::JsEngine::Gc() 92 void AdblockPlus::JsEngine::Gc()
78 { 93 {
79 while (!v8::V8::IdleNotification()); 94 while (!v8::V8::IdleNotification());
80 } 95 }
OLDNEW
« include/AdblockPlus/JsEngine.h ('K') | « shell/src/Main.cpp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld