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

Delta Between Two Patch Sets: src/JsValue.cpp

Issue 6584950149087232: Issue 1280 - Update v8 (Closed)
Left Patch Set: get rid of auto and fix friends of JsValue Created Oct. 31, 2014, 5:06 p.m.
Right Patch Set: revert not critical chanegs in JsValue ctr Created Nov. 3, 2014, 2:49 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « src/JsEngine.cpp ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 /* 1 /*
2 * This file is part of Adblock Plus <http://adblockplus.org/>, 2 * This file is part of Adblock Plus <http://adblockplus.org/>,
3 * Copyright (C) 2006-2014 Eyeo GmbH 3 * Copyright (C) 2006-2014 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
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 #include <vector> 18 #include <vector>
19 #include <v8.h>
20 #include <AdblockPlus.h> 19 #include <AdblockPlus.h>
21 20
22 #include "JsContext.h" 21 #include "JsContext.h"
23 #include "JsError.h" 22 #include "JsError.h"
24 #include "Utils.h" 23 #include "Utils.h"
25 24
26 using namespace AdblockPlus; 25 AdblockPlus::JsValue::JsValue(AdblockPlus::JsEnginePtr jsEngine,
27 26 v8::Handle<v8::Value> value)
28 JsValue::JsValue(const JsEnginePtr& jsEngine, v8::Handle<v8::Value> value)
29 : jsEngine(jsEngine), 27 : jsEngine(jsEngine),
30 value(jsEngine->isolate, value) 28 value(jsEngine->isolate, value)
31 { 29 {
32 } 30 }
33 31
34 JsValue::JsValue(const JsValuePtr& value) 32 AdblockPlus::JsValue::JsValue(AdblockPlus::JsValuePtr value)
35 : jsEngine(value->jsEngine), 33 : jsEngine(value->jsEngine),
36 value(value->value) 34 value(value->value)
37 { 35 {
38 } 36 }
39 37
40 JsValue::~JsValue() 38 AdblockPlus::JsValue::~JsValue()
41 { 39 {
42 } 40 }
43 41
44 bool JsValue::IsUndefined() const 42 bool AdblockPlus::JsValue::IsUndefined() const
45 { 43 {
46 const JsContext context(jsEngine); 44 const JsContext context(jsEngine);
47 return UnwrapValue()->IsUndefined(); 45 return UnwrapValue()->IsUndefined();
48 } 46 }
49 47
50 bool JsValue::IsNull() const 48 bool AdblockPlus::JsValue::IsNull() const
51 { 49 {
52 const JsContext context(jsEngine); 50 const JsContext context(jsEngine);
53 return UnwrapValue()->IsNull(); 51 return UnwrapValue()->IsNull();
54 } 52 }
55 53
56 bool JsValue::IsString() const 54 bool AdblockPlus::JsValue::IsString() const
57 { 55 {
58 const JsContext context(jsEngine); 56 const JsContext context(jsEngine);
59 v8::Local<v8::Value> value = UnwrapValue(); 57 v8::Local<v8::Value> value = UnwrapValue();
60 return value->IsString() || value->IsStringObject(); 58 return value->IsString() || value->IsStringObject();
61 } 59 }
62 60
63 bool JsValue::IsNumber() const 61 bool AdblockPlus::JsValue::IsNumber() const
64 { 62 {
65 const JsContext context(jsEngine); 63 const JsContext context(jsEngine);
66 v8::Local<v8::Value> value = UnwrapValue(); 64 v8::Local<v8::Value> value = UnwrapValue();
67 return value->IsNumber() || value->IsNumberObject(); 65 return value->IsNumber() || value->IsNumberObject();
68 } 66 }
69 67
70 bool JsValue::IsBool() const 68 bool AdblockPlus::JsValue::IsBool() const
71 { 69 {
72 const JsContext context(jsEngine); 70 const JsContext context(jsEngine);
73 v8::Local<v8::Value> value = UnwrapValue(); 71 v8::Local<v8::Value> value = UnwrapValue();
74 return value->IsBoolean() || value->IsBooleanObject(); 72 return value->IsBoolean() || value->IsBooleanObject();
75 } 73 }
76 74
77 bool JsValue::IsObject() const 75 bool AdblockPlus::JsValue::IsObject() const
78 { 76 {
79 const JsContext context(jsEngine); 77 const JsContext context(jsEngine);
80 return UnwrapValue()->IsObject(); 78 return UnwrapValue()->IsObject();
81 } 79 }
82 80
83 bool JsValue::IsArray() const 81 bool AdblockPlus::JsValue::IsArray() const
84 { 82 {
85 const JsContext context(jsEngine); 83 const JsContext context(jsEngine);
86 return UnwrapValue()->IsArray(); 84 return UnwrapValue()->IsArray();
87 } 85 }
88 86
89 bool JsValue::IsFunction() const 87 bool AdblockPlus::JsValue::IsFunction() const
90 { 88 {
91 const JsContext context(jsEngine); 89 const JsContext context(jsEngine);
92 return UnwrapValue()->IsFunction(); 90 return UnwrapValue()->IsFunction();
93 } 91 }
94 92
95 std::string JsValue::AsString() const 93 std::string AdblockPlus::JsValue::AsString() const
96 { 94 {
97 const JsContext context(jsEngine); 95 const JsContext context(jsEngine);
98 return Utils::FromV8String(UnwrapValue()); 96 return Utils::FromV8String(UnwrapValue());
99 } 97 }
100 98
101 int64_t JsValue::AsInt() const 99 int64_t AdblockPlus::JsValue::AsInt() const
102 { 100 {
103 const JsContext context(jsEngine); 101 const JsContext context(jsEngine);
104 return UnwrapValue()->IntegerValue(); 102 return UnwrapValue()->IntegerValue();
105 } 103 }
106 104
107 bool JsValue::AsBool() const 105 bool AdblockPlus::JsValue::AsBool() const
108 { 106 {
109 const JsContext context(jsEngine); 107 const JsContext context(jsEngine);
110 return UnwrapValue()->BooleanValue(); 108 return UnwrapValue()->BooleanValue();
111 } 109 }
112 110
113 JsValueList JsValue::AsList() const 111 AdblockPlus::JsValueList AdblockPlus::JsValue::AsList() const
114 { 112 {
115 if (!IsArray()) 113 if (!IsArray())
116 throw std::runtime_error("Cannot convert a non-array to list"); 114 throw std::runtime_error("Cannot convert a non-array to list");
117 115
118 const JsContext context(jsEngine); 116 const JsContext context(jsEngine);
119 JsValueList result; 117 JsValueList result;
120 v8::Local<v8::Array> array = v8::Local<v8::Array>::Cast(UnwrapValue()); 118 v8::Local<v8::Array> array = v8::Local<v8::Array>::Cast(UnwrapValue());
121 uint32_t length = array->Length(); 119 uint32_t length = array->Length();
122 for (uint32_t i = 0; i < length; i++) 120 for (uint32_t i = 0; i < length; i++)
123 { 121 {
124 v8::Local<v8::Value> item = array->Get(i); 122 v8::Local<v8::Value> item = array->Get(i);
125 result.push_back(JsValuePtr(new JsValue(jsEngine, item))); 123 result.push_back(JsValuePtr(new JsValue(jsEngine, item)));
126 } 124 }
127 return result; 125 return result;
128 } 126 }
129 127
130 std::vector<std::string> JsValue::GetOwnPropertyNames() const 128 std::vector<std::string> AdblockPlus::JsValue::GetOwnPropertyNames() const
131 { 129 {
132 if (!IsObject()) 130 if (!IsObject())
133 throw new std::runtime_error("Attempting to get propert list for a non-objec t"); 131 throw new std::runtime_error("Attempting to get propert list for a non-objec t");
134 132
135 const JsContext context(jsEngine); 133 const JsContext context(jsEngine);
136 std::vector<std::string> result;
137 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(UnwrapValue()); 134 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(UnwrapValue());
138 JsValueList properties = JsValuePtr(new JsValue(jsEngine, object->GetOwnProper tyNames()))->AsList(); 135 JsValueList properties = JsValuePtr(new JsValue(jsEngine, object->GetOwnProper tyNames()))->AsList();
136 std::vector<std::string> result;
139 for (JsValueList::iterator it = properties.begin(); it != properties.end(); ++ it) 137 for (JsValueList::iterator it = properties.begin(); it != properties.end(); ++ it)
140 result.push_back((*it)->AsString()); 138 result.push_back((*it)->AsString());
141 return result; 139 return result;
142 } 140 }
143 141
144 142
145 JsValuePtr JsValue::GetProperty(const std::string& name) const 143 AdblockPlus::JsValuePtr AdblockPlus::JsValue::GetProperty(const std::string& nam e) const
146 { 144 {
147 if (!IsObject()) 145 if (!IsObject())
148 throw new std::runtime_error("Attempting to get property of a non-object"); 146 throw new std::runtime_error("Attempting to get property of a non-object");
149 147
150 const JsContext context(jsEngine); 148 const JsContext context(jsEngine);
151 v8::Local<v8::String> property = Utils::ToV8String(jsEngine->isolate, name); 149 v8::Local<v8::String> property = Utils::ToV8String(name);
152 v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue()); 150 v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue());
153 return JsValuePtr(new JsValue(jsEngine, obj->Get(property))); 151 return JsValuePtr(new JsValue(jsEngine, obj->Get(property)));
154 } 152 }
155 153
156 void JsValue::SetProperty(const std::string& name, v8::Handle<v8::Value> val) 154 void AdblockPlus::JsValue::SetProperty(const std::string& name, v8::Handle<v8::V alue> val)
157 { 155 {
158 if (!IsObject()) 156 if (!IsObject())
159 throw new std::runtime_error("Attempting to set property on a non-object"); 157 throw new std::runtime_error("Attempting to set property on a non-object");
160 158
161 v8::Local<v8::String> property = Utils::ToV8String(jsEngine->isolate, name); 159 v8::Local<v8::String> property = Utils::ToV8String(name);
162 v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue()); 160 v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue());
163 obj->Set(property, val); 161 obj->Set(property, val);
164 } 162 }
165 163
166 v8::Local<v8::Value> JsValue::UnwrapValue() const 164 v8::Local<v8::Value> AdblockPlus::JsValue::UnwrapValue() const
167 { 165 {
168 return v8::Local<v8::Value>::New(jsEngine->isolate, value); 166 return v8::Local<v8::Value>::New(jsEngine->isolate, value);
169 } 167 }
170 168
171 void JsValue::SetProperty(const std::string& name, const std::string& val) 169 void AdblockPlus::JsValue::SetProperty(const std::string& name, const std::strin g& val)
172 { 170 {
173 const JsContext context(jsEngine); 171 const JsContext context(jsEngine);
174 SetProperty(name, Utils::ToV8String(jsEngine->isolate, val)); 172 SetProperty(name, Utils::ToV8String(val));
175 } 173 }
176 174
177 void JsValue::SetProperty(const std::string& name, int64_t val) 175 void AdblockPlus::JsValue::SetProperty(const std::string& name, int64_t val)
178 { 176 {
179 const JsContext context(jsEngine); 177 const JsContext context(jsEngine);
180 SetProperty(name, v8::Number::New(jsEngine->isolate, val)); 178 SetProperty(name, v8::Number::New(val));
181 } 179 }
182 180
183 void JsValue::SetProperty(const std::string& name, const JsValuePtr& val) 181 void AdblockPlus::JsValue::SetProperty(const std::string& name, const JsValuePtr & val)
184 { 182 {
185 const JsContext context(jsEngine); 183 const JsContext context(jsEngine);
186 SetProperty(name, val->UnwrapValue()); 184 SetProperty(name, val->UnwrapValue());
187 } 185 }
188 186
189 void JsValue::SetProperty(const std::string& name, bool val) 187 void AdblockPlus::JsValue::SetProperty(const std::string& name, bool val)
190 { 188 {
191 const JsContext context(jsEngine); 189 const JsContext context(jsEngine);
192 SetProperty(name, v8::Boolean::New(val)); 190 SetProperty(name, v8::Boolean::New(val));
193 } 191 }
194 192
195 std::string JsValue::GetClass() const 193 std::string AdblockPlus::JsValue::GetClass() const
196 { 194 {
197 if (!IsObject()) 195 if (!IsObject())
198 throw new std::runtime_error("Cannot get constructor of a non-object"); 196 throw new std::runtime_error("Cannot get constructor of a non-object");
199 197
200 const JsContext context(jsEngine); 198 const JsContext context(jsEngine);
201 v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue()); 199 v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue());
202 return Utils::FromV8String(obj->GetConstructorName()); 200 return Utils::FromV8String(obj->GetConstructorName());
203 } 201 }
204 202
205 JsValuePtr JsValue::Call(const JsValueList& params, JsValuePtr thisPtr) const 203 AdblockPlus::JsValuePtr AdblockPlus::JsValue::Call(const JsValueList& params, Js ValuePtr thisPtr) const
206 { 204 {
207 const JsContext context(jsEngine);
208
209 if (!IsFunction()) 205 if (!IsFunction())
210 throw new std::runtime_error("Attempting to call a non-function"); 206 throw new std::runtime_error("Attempting to call a non-function");
211 207
208 const JsContext context(jsEngine);
212 if (!thisPtr) 209 if (!thisPtr)
213 { 210 {
214 v8::Local<v8::Context> localContext = v8::Local<v8::Context>::New(jsEngine-> isolate, jsEngine->context); 211 v8::Local<v8::Context> localContext = v8::Local<v8::Context>::New(jsEngine-> isolate, jsEngine->context);
215 thisPtr = JsValuePtr(new JsValue(jsEngine, localContext->Global())); 212 thisPtr = JsValuePtr(new JsValue(jsEngine, localContext->Global()));
216 } 213 }
217 if (!thisPtr->IsObject()) 214 if (!thisPtr->IsObject())
218 throw new std::runtime_error("`this` pointer has to be an object"); 215 throw new std::runtime_error("`this` pointer has to be an object");
219 v8::Local<v8::Object> thisObj = v8::Local<v8::Object>::Cast(thisPtr->UnwrapVal ue()); 216 v8::Local<v8::Object> thisObj = v8::Local<v8::Object>::Cast(thisPtr->UnwrapVal ue());
220 217
221 std::vector<v8::Handle<v8::Value>> argv; 218 std::vector<v8::Handle<v8::Value>> argv;
222 for (JsValueList::const_iterator it = params.begin(); it != params.end(); ++it ) 219 for (JsValueList::const_iterator it = params.begin(); it != params.end(); ++it )
223 argv.push_back((*it)->UnwrapValue()); 220 argv.push_back((*it)->UnwrapValue());
224 221
225 const v8::TryCatch tryCatch; 222 const v8::TryCatch tryCatch;
226 v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(UnwrapValue()); 223 v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(UnwrapValue());
227 v8::Local<v8::Value> result = func->Call(thisObj, argv.size(), 224 v8::Local<v8::Value> result = func->Call(thisObj, argv.size(),
228 argv.size() ? &argv.front() : 0); 225 argv.size() ? &argv.front() : 0);
229 226
230 if (tryCatch.HasCaught()) 227 if (tryCatch.HasCaught())
231 throw JsError(tryCatch.Exception(), tryCatch.Message()); 228 throw JsError(tryCatch.Exception(), tryCatch.Message());
232 229
233 return JsValuePtr(new JsValue(jsEngine, result)); 230 return JsValuePtr(new JsValue(jsEngine, result));
234 } 231 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld