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: Created April 12, 2013, 2:55 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
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 AdblockPlus::ErrorCallback& errorCallback, 9 AdblockPlus::ErrorCallback& errorCallback,
10 AdblockPlus::WebRequest& webRequest) 10 AdblockPlus::WebRequest& webRequest)
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
63 { 63 {
64 } 64 }
65 65
66 AdblockPlus::JsEngine::JsEngine(const FileReader* const fileReader, 66 AdblockPlus::JsEngine::JsEngine(const FileReader* const fileReader,
67 WebRequest* const webRequest, 67 WebRequest* const webRequest,
68 ErrorCallback* const errorCallback) 68 ErrorCallback* const errorCallback)
69 : fileReader(fileReader), context(CreateContext(*errorCallback, *webRequest)) 69 : fileReader(fileReader), context(CreateContext(*errorCallback, *webRequest))
70 { 70 {
71 } 71 }
72 72
73 std::string AdblockPlus::JsEngine::Evaluate(const std::string& source, 73 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::Evaluate(const std::string& sourc e,
74 const std::string& filename) 74 const std::string& filename)
75 { 75 {
76 const v8::Locker locker(v8::Isolate::GetCurrent()); 76 const v8::Locker locker(v8::Isolate::GetCurrent());
77 const v8::HandleScope handleScope; 77 const v8::HandleScope handleScope;
78 const v8::Context::Scope contextScope(context); 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(v8::Isolate::GetCurrent(), context, 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 }
OLDNEW
« include/AdblockPlus/JsValue.h ('K') | « libadblockplus.gyp ('k') | src/JsValue.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld