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

Side by Side Diff: src/JsEngine.cpp

Issue 10305024: Simplify context setup, set properties on the global object directly instead of using templates (Closed)
Patch Set: Unbitrotted patch Created April 18, 2013, 11:44 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 #include "Utils.h" 5 #include "Utils.h"
6 6
7 namespace 7 namespace
8 { 8 {
9 v8::Handle<v8::Context> CreateContext(
10 v8::Isolate* isolate,
11 const AdblockPlus::AppInfo& appInfo,
12 AdblockPlus::JsEngine& jsEngine)
13 {
14 const v8::Locker locker(isolate);
15 const v8::HandleScope handleScope;
16 const v8::Handle<v8::ObjectTemplate> global =
17 AdblockPlus::GlobalJsObject::Create(appInfo, jsEngine);
18 return v8::Context::New(0, global);
19 }
20
21 v8::Handle<v8::Script> CompileScript(const std::string& source, const std::str ing& filename) 9 v8::Handle<v8::Script> CompileScript(const std::string& source, const std::str ing& filename)
22 { 10 {
23 const v8::Handle<v8::String> v8Source = v8::String::New(source.c_str()); 11 const v8::Handle<v8::String> v8Source = v8::String::New(source.c_str());
24 if (filename.length()) 12 if (filename.length())
25 { 13 {
26 const v8::Handle<v8::String> v8Filename = v8::String::New(filename.c_str() ); 14 const v8::Handle<v8::String> v8Filename = v8::String::New(filename.c_str() );
27 return v8::Script::Compile(v8Source, v8Filename); 15 return v8::Script::Compile(v8Source, v8Filename);
28 } 16 }
29 else 17 else
30 return v8::Script::Compile(v8Source); 18 return v8::Script::Compile(v8Source);
(...skipping 25 matching lines...) Expand all
56 const v8::Handle<v8::Message> message) 44 const v8::Handle<v8::Message> message)
57 : std::runtime_error(ExceptionToString(exception, message)) 45 : std::runtime_error(ExceptionToString(exception, message))
58 { 46 {
59 } 47 }
60 48
61 AdblockPlus::JsEngine::JsEngine(const AppInfo& appInfo, 49 AdblockPlus::JsEngine::JsEngine(const AppInfo& appInfo,
62 FileSystem* const fileSystem, 50 FileSystem* const fileSystem,
63 WebRequest* const webRequest, 51 WebRequest* const webRequest,
64 ErrorCallback* const errorCallback) 52 ErrorCallback* const errorCallback)
65 : fileSystem(*fileSystem), webRequest(*webRequest), 53 : fileSystem(*fileSystem), webRequest(*webRequest),
66 errorCallback(*errorCallback), isolate(v8::Isolate::GetCurrent()), 54 errorCallback(*errorCallback), isolate(v8::Isolate::GetCurrent())
67 context(CreateContext(isolate, appInfo, *this))
68 { 55 {
56 const v8::Locker locker(isolate);
57 const v8::HandleScope handleScope;
58
59 context = v8::Context::New();
60 AdblockPlus::GlobalJsObject::Setup(*this, appInfo,
61 JsValuePtr(new JsValue(*this, context->Global())));
69 } 62 }
70 63
71 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::Evaluate(const std::string& sourc e, 64 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::Evaluate(const std::string& sourc e,
72 const std::string& filename) 65 const std::string& filename)
73 { 66 {
74 const Context context(*this); 67 const Context context(*this);
75 const v8::TryCatch tryCatch; 68 const v8::TryCatch tryCatch;
76 const v8::Handle<v8::Script> script = CompileScript(source, filename); 69 const v8::Handle<v8::Script> script = CompileScript(source, filename);
77 CheckTryCatch(tryCatch); 70 CheckTryCatch(tryCatch);
78 v8::Local<v8::Value> result = script->Run(); 71 v8::Local<v8::Value> result = script->Run();
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
110 const Context context(*this); 103 const Context context(*this);
111 return JsValuePtr(new JsValue(*this, v8::Boolean::New(val))); 104 return JsValuePtr(new JsValue(*this, v8::Boolean::New(val)));
112 } 105 }
113 106
114 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewObject() 107 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewObject()
115 { 108 {
116 const Context context(*this); 109 const Context context(*this);
117 return JsValuePtr(new JsValue(*this, v8::Object::New())); 110 return JsValuePtr(new JsValue(*this, v8::Object::New()));
118 } 111 }
119 112
113 AdblockPlus::JsValuePtr AdblockPlus::JsEngine::NewCallback(
114 v8::InvocationCallback callback)
115 {
116 const Context context(*this);
117
118 v8::Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(callback,
119 v8::External::New(this));
120 return JsValuePtr(new JsValue(*this, templ->GetFunction()));
121 }
122
120 AdblockPlus::JsEngine& AdblockPlus::JsEngine::FromArguments(const v8::Arguments& arguments) 123 AdblockPlus::JsEngine& AdblockPlus::JsEngine::FromArguments(const v8::Arguments& arguments)
121 { 124 {
122 const v8::Local<const v8::External> external = 125 const v8::Local<const v8::External> external =
123 v8::Local<const v8::External>::Cast(arguments.Data()); 126 v8::Local<const v8::External>::Cast(arguments.Data());
124 return *static_cast<JsEngine* const>(external->Value()); 127 return *static_cast<JsEngine* const>(external->Value());
125 } 128 }
126 129
127 AdblockPlus::JsValueList AdblockPlus::JsEngine::ConvertArguments(const v8::Argum ents& arguments) 130 AdblockPlus::JsValueList AdblockPlus::JsEngine::ConvertArguments(const v8::Argum ents& arguments)
128 { 131 {
129 const Context context(*this); 132 const Context context(*this);
130 JsValueList list; 133 JsValueList list;
131 for (int i = 0; i < arguments.Length(); i++) 134 for (int i = 0; i < arguments.Length(); i++)
132 list.push_back(JsValuePtr(new JsValue(*this, arguments[i]))); 135 list.push_back(JsValuePtr(new JsValue(*this, arguments[i])));
133 return list; 136 return list;
134 } 137 }
135 138
136 AdblockPlus::JsEngine::Context::Context(const JsEngine& jsEngine) 139 AdblockPlus::JsEngine::Context::Context(const JsEngine& jsEngine)
137 : locker(jsEngine.isolate), handleScope(), 140 : locker(jsEngine.isolate), handleScope(),
138 contextScope(jsEngine.context) 141 contextScope(jsEngine.context)
139 { 142 {
140 } 143 }
OLDNEW
« src/GlobalJsObject.cpp ('K') | « src/GlobalJsObject.cpp ('k') | src/WebRequestJsObject.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld