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

Powered by Google App Engine
This is Rietveld