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

Side by Side Diff: src/JsEngine.cpp

Issue 10213003: Make JsEngine::Evaluate() return a wrapper for v8::Value to accessdifferent variable types easily (Closed)
Patch Set: Added tests, resolved type conversion ambiguities, implemented some missing API calls Created April 15, 2013, 6:23 a.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 5
6 namespace 6 namespace
7 { 7 {
8 v8::Handle<v8::Context> CreateContext( 8 v8::Handle<v8::Context> CreateContext(
9 v8::Isolate* isolate,
9 AdblockPlus::ErrorCallback& errorCallback, 10 AdblockPlus::ErrorCallback& errorCallback,
10 AdblockPlus::WebRequest& webRequest) 11 AdblockPlus::WebRequest& webRequest)
11 { 12 {
12 const v8::Locker locker(v8::Isolate::GetCurrent()); 13 const v8::Locker locker(isolate);
13 const v8::HandleScope handleScope; 14 const v8::HandleScope handleScope;
14 const v8::Handle<v8::ObjectTemplate> global = 15 const v8::Handle<v8::ObjectTemplate> global =
15 AdblockPlus::GlobalJsObject::Create(errorCallback, webRequest); 16 AdblockPlus::GlobalJsObject::Create(errorCallback, webRequest);
16 return v8::Context::New(0, global); 17 return v8::Context::New(0, global);
17 } 18 }
18 19
19 v8::Handle<v8::Script> CompileScript(const std::string& source, const std::str ing& filename) 20 v8::Handle<v8::Script> CompileScript(const std::string& source, const std::str ing& filename)
20 { 21 {
21 const v8::Handle<v8::String> v8Source = v8::String::New(source.c_str()); 22 const v8::Handle<v8::String> v8Source = v8::String::New(source.c_str());
22 if (filename.length()) 23 if (filename.length())
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
59 60
60 AdblockPlus::JsError::JsError(const v8::Handle<v8::Value> exception, 61 AdblockPlus::JsError::JsError(const v8::Handle<v8::Value> exception,
61 const v8::Handle<v8::Message> message) 62 const v8::Handle<v8::Message> message)
62 : std::runtime_error(ExceptionToString(exception, message)) 63 : std::runtime_error(ExceptionToString(exception, message))
63 { 64 {
64 } 65 }
65 66
66 AdblockPlus::JsEngine::JsEngine(const FileReader* const fileReader, 67 AdblockPlus::JsEngine::JsEngine(const FileReader* const fileReader,
67 WebRequest* const webRequest, 68 WebRequest* const webRequest,
68 ErrorCallback* const errorCallback) 69 ErrorCallback* const errorCallback)
69 : fileReader(fileReader), context(CreateContext(*errorCallback, *webRequest)) 70 : fileReader(fileReader), isolate(v8::Isolate::GetCurrent()),
71 context(CreateContext(isolate, *errorCallback, *webRequest))
70 { 72 {
71 } 73 }
72 74
73 std::string AdblockPlus::JsEngine::Evaluate(const std::string& source, 75 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::Evaluate(const std::string& sourc e,
74 const std::string& filename) 76 const std::string& filename)
75 { 77 {
76 const v8::Locker locker(v8::Isolate::GetCurrent()); 78 const Context context(*this);
77 const v8::HandleScope handleScope;
78 const v8::Context::Scope contextScope(context);
79 const v8::TryCatch tryCatch; 79 const v8::TryCatch tryCatch;
80 const v8::Handle<v8::Script> script = CompileScript(source, filename); 80 const v8::Handle<v8::Script> script = CompileScript(source, filename);
81 CheckTryCatch(tryCatch); 81 CheckTryCatch(tryCatch);
82 v8::Local<v8::Value> result = script->Run(); 82 v8::Local<v8::Value> result = script->Run();
83 CheckTryCatch(tryCatch); 83 CheckTryCatch(tryCatch);
84 v8::String::Utf8Value resultString(result); 84 return JsValuePtr(new JsValue(*this, result));
85 return std::string(*resultString);
86 } 85 }
87 86
88 void AdblockPlus::JsEngine::Load(const std::string& scriptPath) 87 void AdblockPlus::JsEngine::Load(const std::string& scriptPath)
89 { 88 {
90 const std::auto_ptr<std::istream> file = fileReader->Read(scriptPath); 89 const std::auto_ptr<std::istream> file = fileReader->Read(scriptPath);
91 if (!*file) 90 if (!*file)
92 throw std::runtime_error("Unable to load script " + scriptPath); 91 throw std::runtime_error("Unable to load script " + scriptPath);
93 Evaluate(Slurp(*file)); 92 Evaluate(Slurp(*file));
94 } 93 }
95 94
96 void AdblockPlus::JsEngine::Gc() 95 void AdblockPlus::JsEngine::Gc()
97 { 96 {
98 while (!v8::V8::IdleNotification()); 97 while (!v8::V8::IdleNotification());
99 } 98 }
99
100 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(const std::string& val)
101 {
102 const Context context(*this);
103 return JsValuePtr(new JsValue(*this, v8::String::New(val.c_str(), val.length() )));
104 }
105
106 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(int64_t val)
107 {
108 const Context context(*this);
109 return JsValuePtr(new JsValue(*this, v8::Integer::New(val)));
110 }
111
112 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewValue(bool val)
113 {
114 const Context context(*this);
115 return JsValuePtr(new JsValue(*this, v8::Boolean::New(val)));
116 }
117
118 AdblockPlus::JsEngine::Context::Context(const JsEngine& jsEngine)
119 : locker(jsEngine.isolate), handleScope(),
120 contextScope(jsEngine.context)
121 {
122 }
OLDNEW
« src/FilterEngine.cpp ('K') | « src/FilterEngine.cpp ('k') | src/JsValue.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld