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

Side by Side Diff: src/JsValue.cpp

Issue 29417605: Issue 5034 - Part 3: Create plain JsValue instead of JsValuePtr (Closed) Base URL: https://hg.adblockplus.org/libadblockplus/
Patch Set: Pass JsEngine by ref Created April 20, 2017, 1:01 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
« no previous file with comments | « src/JsEngine.cpp ('k') | src/Notification.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2017 eyeo GmbH 3 * Copyright (C) 2006-2017 eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 19 matching lines...) Expand all
30 value(new v8::Persistent<v8::Value>(jsEngine->GetIsolate(), value)) 30 value(new v8::Persistent<v8::Value>(jsEngine->GetIsolate(), value))
31 { 31 {
32 } 32 }
33 33
34 AdblockPlus::JsValue::JsValue(AdblockPlus::JsValue&& src) 34 AdblockPlus::JsValue::JsValue(AdblockPlus::JsValue&& src)
35 : jsEngine(src.jsEngine), 35 : jsEngine(src.jsEngine),
36 value(std::move(src.value)) 36 value(std::move(src.value))
37 { 37 {
38 } 38 }
39 39
40 AdblockPlus::JsValue::JsValue(const JsValue& src)
41 : jsEngine(src.jsEngine)
42 {
43 const JsContext context(src.jsEngine);
44 value.reset(new v8::Persistent<v8::Value>(src.jsEngine->GetIsolate(), *src.val ue));
45 }
46
40 AdblockPlus::JsValue::~JsValue() 47 AdblockPlus::JsValue::~JsValue()
41 { 48 {
42 if (value) 49 if (value)
43 { 50 {
44 value->Dispose(); 51 value->Dispose();
45 value.reset(); 52 value.reset();
46 } 53 }
47 } 54 }
48 55
56 JsValue& AdblockPlus::JsValue::operator=(const JsValue& src)
57 {
58 const JsContext context(src.jsEngine);
59 if (value)
60 value->Dispose();
61 jsEngine = src.jsEngine;
62 value.reset(new v8::Persistent<v8::Value>(src.jsEngine->GetIsolate(), *src.val ue));
63
64 return *this;
65 }
66
49 bool AdblockPlus::JsValue::IsUndefined() const 67 bool AdblockPlus::JsValue::IsUndefined() const
50 { 68 {
51 const JsContext context(jsEngine); 69 const JsContext context(jsEngine);
52 return UnwrapValue()->IsUndefined(); 70 return UnwrapValue()->IsUndefined();
53 } 71 }
54 72
55 bool AdblockPlus::JsValue::IsNull() const 73 bool AdblockPlus::JsValue::IsNull() const
56 { 74 {
57 const JsContext context(jsEngine); 75 const JsContext context(jsEngine);
58 return UnwrapValue()->IsNull(); 76 return UnwrapValue()->IsNull();
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
120 if (!IsArray()) 138 if (!IsArray())
121 throw std::runtime_error("Cannot convert a non-array to list"); 139 throw std::runtime_error("Cannot convert a non-array to list");
122 140
123 const JsContext context(jsEngine); 141 const JsContext context(jsEngine);
124 JsValueList result; 142 JsValueList result;
125 v8::Local<v8::Array> array = v8::Local<v8::Array>::Cast(UnwrapValue()); 143 v8::Local<v8::Array> array = v8::Local<v8::Array>::Cast(UnwrapValue());
126 uint32_t length = array->Length(); 144 uint32_t length = array->Length();
127 for (uint32_t i = 0; i < length; i++) 145 for (uint32_t i = 0; i < length; i++)
128 { 146 {
129 v8::Local<v8::Value> item = array->Get(i); 147 v8::Local<v8::Value> item = array->Get(i);
130 result.push_back(JsValuePtr(new JsValue(jsEngine, item))); 148 result.push_back(JsValue(jsEngine, item));
131 } 149 }
132 return result; 150 return result;
133 } 151 }
134 152
135 std::vector<std::string> AdblockPlus::JsValue::GetOwnPropertyNames() const 153 std::vector<std::string> AdblockPlus::JsValue::GetOwnPropertyNames() const
136 { 154 {
137 if (!IsObject()) 155 if (!IsObject())
138 throw new std::runtime_error("Attempting to get propert list for a non-objec t"); 156 throw new std::runtime_error("Attempting to get propert list for a non-objec t");
139 157
140 const JsContext context(jsEngine); 158 const JsContext context(jsEngine);
141 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(UnwrapValue()); 159 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(UnwrapValue());
142 JsValueList properties = JsValuePtr(new JsValue(jsEngine, object->GetOwnProper tyNames()))->AsList(); 160 JsValueList properties = JsValue(jsEngine, object->GetOwnPropertyNames()).AsLi st();
143 std::vector<std::string> result; 161 std::vector<std::string> result;
144 for (const auto& property : properties) 162 for (const auto& property : properties)
145 result.push_back(property->AsString()); 163 result.push_back(property.AsString());
146 return result; 164 return result;
147 } 165 }
148 166
149 167
150 AdblockPlus::JsValue AdblockPlus::JsValue::GetProperty(const std::string& name) const 168 AdblockPlus::JsValue AdblockPlus::JsValue::GetProperty(const std::string& name) const
151 { 169 {
152 if (!IsObject()) 170 if (!IsObject())
153 throw new std::runtime_error("Attempting to get property of a non-object"); 171 throw new std::runtime_error("Attempting to get property of a non-object");
154 172
155 const JsContext context(jsEngine); 173 const JsContext context(jsEngine);
(...skipping 10 matching lines...) Expand all
166 v8::Local<v8::String> property = Utils::ToV8String(jsEngine->GetIsolate(), nam e); 184 v8::Local<v8::String> property = Utils::ToV8String(jsEngine->GetIsolate(), nam e);
167 v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue()); 185 v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue());
168 obj->Set(property, val); 186 obj->Set(property, val);
169 } 187 }
170 188
171 v8::Local<v8::Value> AdblockPlus::JsValue::UnwrapValue() const 189 v8::Local<v8::Value> AdblockPlus::JsValue::UnwrapValue() const
172 { 190 {
173 return v8::Local<v8::Value>::New(jsEngine->GetIsolate(), *value); 191 return v8::Local<v8::Value>::New(jsEngine->GetIsolate(), *value);
174 } 192 }
175 193
176 JsValue AdblockPlus::JsValue::Clone() const
177 {
178 return JsValue(jsEngine, UnwrapValue());
179 }
180
181 void AdblockPlus::JsValue::SetProperty(const std::string& name, const std::strin g& val) 194 void AdblockPlus::JsValue::SetProperty(const std::string& name, const std::strin g& val)
182 { 195 {
183 const JsContext context(jsEngine); 196 const JsContext context(jsEngine);
184 SetProperty(name, Utils::ToV8String(jsEngine->GetIsolate(), val)); 197 SetProperty(name, Utils::ToV8String(jsEngine->GetIsolate(), val));
185 } 198 }
186 199
187 void AdblockPlus::JsValue::SetProperty(const std::string& name, int64_t val) 200 void AdblockPlus::JsValue::SetProperty(const std::string& name, int64_t val)
188 { 201 {
189 const JsContext context(jsEngine); 202 const JsContext context(jsEngine);
190 SetProperty(name, v8::Number::New(jsEngine->GetIsolate(), val)); 203 SetProperty(name, v8::Number::New(jsEngine->GetIsolate(), val));
(...skipping 14 matching lines...) Expand all
205 std::string AdblockPlus::JsValue::GetClass() const 218 std::string AdblockPlus::JsValue::GetClass() const
206 { 219 {
207 if (!IsObject()) 220 if (!IsObject())
208 throw new std::runtime_error("Cannot get constructor of a non-object"); 221 throw new std::runtime_error("Cannot get constructor of a non-object");
209 222
210 const JsContext context(jsEngine); 223 const JsContext context(jsEngine);
211 v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue()); 224 v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue());
212 return Utils::FromV8String(obj->GetConstructorName()); 225 return Utils::FromV8String(obj->GetConstructorName());
213 } 226 }
214 227
215 JsValue JsValue::Call(const JsConstValueList& params, const JsValuePtr& thisPtr) const 228 JsValue JsValue::Call(const JsValueList& params, const JsValuePtr& thisPtr) cons t
216 { 229 {
217 const JsContext context(jsEngine); 230 const JsContext context(jsEngine);
218 v8::Local<v8::Object> thisObj = thisPtr ? 231 v8::Local<v8::Object> thisObj = thisPtr ?
219 v8::Local<v8::Object>::Cast(thisPtr->UnwrapValue()) : 232 v8::Local<v8::Object>::Cast(thisPtr->UnwrapValue()) :
220 context.GetV8Context()->Global(); 233 context.GetV8Context()->Global();
221 234
222 std::vector<v8::Handle<v8::Value>> argv; 235 std::vector<v8::Handle<v8::Value>> argv;
223 for (const auto& param : params) 236 for (const auto& param : params)
224 argv.push_back(param->UnwrapValue()); 237 argv.push_back(param.UnwrapValue());
225 238
226 return Call(argv, thisObj); 239 return Call(argv, thisObj);
227 } 240 }
228 241
229 JsValue JsValue::Call(const JsValue& arg) const 242 JsValue JsValue::Call(const JsValue& arg) const
230 { 243 {
231 const JsContext context(jsEngine); 244 const JsContext context(jsEngine);
232 245
233 std::vector<v8::Handle<v8::Value>> argv; 246 std::vector<v8::Handle<v8::Value>> argv;
234 argv.push_back(arg.UnwrapValue()); 247 argv.push_back(arg.UnwrapValue());
(...skipping 13 matching lines...) Expand all
248 const v8::TryCatch tryCatch; 261 const v8::TryCatch tryCatch;
249 v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(UnwrapValue()); 262 v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(UnwrapValue());
250 v8::Local<v8::Value> result = func->Call(thisObj, args.size(), 263 v8::Local<v8::Value> result = func->Call(thisObj, args.size(),
251 args.size() ? &args[0] : nullptr); 264 args.size() ? &args[0] : nullptr);
252 265
253 if (tryCatch.HasCaught()) 266 if (tryCatch.HasCaught())
254 throw JsError(tryCatch.Exception(), tryCatch.Message()); 267 throw JsError(tryCatch.Exception(), tryCatch.Message());
255 268
256 return JsValue(jsEngine, result); 269 return JsValue(jsEngine, result);
257 } 270 }
OLDNEW
« no previous file with comments | « src/JsEngine.cpp ('k') | src/Notification.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld