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

Side by Side Diff: src/JsValue.cpp

Issue 10173031: Don`t use references to JsEngine to avoid use-after-free errors,switch to shared_ptr instead (Closed)
Patch Set: Created April 18, 2013, 4:15 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 2
3 namespace 3 namespace
4 { 4 {
5 std::string fromV8String(v8::Handle<v8::Value> value) 5 std::string fromV8String(v8::Handle<v8::Value> value)
6 { 6 {
7 v8::String::Utf8Value stringValue(value); 7 v8::String::Utf8Value stringValue(value);
8 if (stringValue.length()) 8 if (stringValue.length())
9 return std::string(*stringValue, stringValue.length()); 9 return std::string(*stringValue, stringValue.length());
10 else 10 else
11 return std::string(); 11 return std::string();
12 } 12 }
13 13
14 v8::Local<v8::String> toV8String(const std::string& str) 14 v8::Local<v8::String> toV8String(const std::string& str)
15 { 15 {
16 return v8::String::New(str.c_str(), str.length()); 16 return v8::String::New(str.c_str(), str.length());
17 } 17 }
18 } 18 }
19 19
20 AdblockPlus::JsValue::JsValue(AdblockPlus::JsEngine& jsEngine, 20 AdblockPlus::JsValue::JsValue(AdblockPlus::JsEnginePtr jsEngine,
21 v8::Handle<v8::Value> value) 21 v8::Handle<v8::Value> value)
22 : jsEngine(jsEngine), 22 : jsEngine(jsEngine),
23 value(v8::Persistent<v8::Value>::New(jsEngine.isolate, value)) 23 value(v8::Persistent<v8::Value>::New(jsEngine->isolate, value))
24 { 24 {
25 } 25 }
26 26
27 AdblockPlus::JsValue::~JsValue() 27 AdblockPlus::JsValue::~JsValue()
28 { 28 {
29 value.Dispose(jsEngine.isolate); 29 value.Dispose(jsEngine->isolate);
30 } 30 }
31 31
32 bool AdblockPlus::JsValue::IsUndefined() const 32 bool AdblockPlus::JsValue::IsUndefined() const
33 { 33 {
34 const JsEngine::Context context(jsEngine); 34 const JsEngine::Context context(jsEngine);
35 return value->IsUndefined(); 35 return value->IsUndefined();
36 } 36 }
37 37
38 bool AdblockPlus::JsValue::IsNull() const 38 bool AdblockPlus::JsValue::IsNull() const
39 { 39 {
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after
185 AdblockPlus::JsValuePtr AdblockPlus::JsValue::Call( 185 AdblockPlus::JsValuePtr AdblockPlus::JsValue::Call(
186 const JsValueList& params, 186 const JsValueList& params,
187 AdblockPlus::JsValuePtr thisPtr) const 187 AdblockPlus::JsValuePtr thisPtr) const
188 { 188 {
189 if (!IsFunction()) 189 if (!IsFunction())
190 throw new std::runtime_error("Attempting to call a non-function"); 190 throw new std::runtime_error("Attempting to call a non-function");
191 191
192 const JsEngine::Context context(jsEngine); 192 const JsEngine::Context context(jsEngine);
193 193
194 if (!thisPtr) 194 if (!thisPtr)
195 thisPtr = JsValuePtr(new JsValue(jsEngine, jsEngine.context->Global())); 195 thisPtr = JsValuePtr(new JsValue(jsEngine, jsEngine->context->Global()));
196 if (!thisPtr->IsObject()) 196 if (!thisPtr->IsObject())
197 throw new std::runtime_error("`this` pointer has to be an object"); 197 throw new std::runtime_error("`this` pointer has to be an object");
198 v8::Persistent<v8::Object> thisObj = v8::Persistent<v8::Object>::Cast(thisPtr- >value); 198 v8::Persistent<v8::Object> thisObj = v8::Persistent<v8::Object>::Cast(thisPtr- >value);
199 199
200 size_t argc = params.size(); 200 size_t argc = params.size();
201 v8::Handle<v8::Value>* argv = new v8::Handle<v8::Value>[argc]; 201 v8::Handle<v8::Value>* argv = new v8::Handle<v8::Value>[argc];
202 for (size_t i = 0; i < argc; ++i) 202 for (size_t i = 0; i < argc; ++i)
203 argv[i] = params[i]->value; 203 argv[i] = params[i]->value;
204 204
205 const v8::TryCatch tryCatch; 205 const v8::TryCatch tryCatch;
206 v8::Persistent<v8::Function> func = v8::Persistent<v8::Function>::Cast(value); 206 v8::Persistent<v8::Function> func = v8::Persistent<v8::Function>::Cast(value);
207 v8::Local<v8::Value> result = func->Call(thisObj, argc, argv); 207 v8::Local<v8::Value> result = func->Call(thisObj, argc, argv);
208 delete argv; 208 delete argv;
209 209
210 if (tryCatch.HasCaught()) 210 if (tryCatch.HasCaught())
211 throw AdblockPlus::JsError(tryCatch.Exception(), tryCatch.Message()); 211 throw AdblockPlus::JsError(tryCatch.Exception(), tryCatch.Message());
212 212
213 return JsValuePtr(new JsValue(jsEngine, result)); 213 return JsValuePtr(new JsValue(jsEngine, result));
214 } 214 }
OLDNEW
« src/JsEngine.cpp ('K') | « src/JsEngine.cpp ('k') | src/WebRequestJsObject.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld