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: Created Oct. 24, 2014, 12:42 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, const v8::Handle<v8::Value> value, JsValue::PrivateCtrArg)
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 38 AdblockPlus::JsValue::~JsValue()
41 JsValue::~JsValue() 39 {
42 { 40 }
43 } 41
44 42 bool AdblockPlus::JsValue::IsUndefined() const
45 bool JsValue::IsUndefined() const 43 {
46 { 44 const JsContext context(jsEngine);
47 const JsContext context(jsEngine); 45 return UnwrapValue()->IsUndefined();
48 return unwrapValue()->IsUndefined(); 46 }
49 } 47
50 48 bool AdblockPlus::JsValue::IsNull() const
51 bool JsValue::IsNull() const 49 {
52 { 50 const JsContext context(jsEngine);
53 const JsContext context(jsEngine); 51 return UnwrapValue()->IsNull();
54 return unwrapValue()->IsNull(); 52 }
55 } 53
56 54 bool AdblockPlus::JsValue::IsString() const
57 bool JsValue::IsString() const 55 {
58 { 56 const JsContext context(jsEngine);
59 const JsContext context(jsEngine); 57 v8::Local<v8::Value> value = UnwrapValue();
60 auto value = unwrapValue();
61 return value->IsString() || value->IsStringObject(); 58 return value->IsString() || value->IsStringObject();
62 } 59 }
63 60
64 bool JsValue::IsNumber() const 61 bool AdblockPlus::JsValue::IsNumber() const
65 { 62 {
66 const JsContext context(jsEngine); 63 const JsContext context(jsEngine);
67 auto value = unwrapValue(); 64 v8::Local<v8::Value> value = UnwrapValue();
68 return value->IsNumber() || value->IsNumberObject(); 65 return value->IsNumber() || value->IsNumberObject();
69 } 66 }
70 67
71 bool JsValue::IsBool() const 68 bool AdblockPlus::JsValue::IsBool() const
72 { 69 {
73 const JsContext context(jsEngine); 70 const JsContext context(jsEngine);
74 auto value = unwrapValue(); 71 v8::Local<v8::Value> value = UnwrapValue();
75 return value->IsBoolean() || value->IsBooleanObject(); 72 return value->IsBoolean() || value->IsBooleanObject();
76 } 73 }
77 74
78 bool JsValue::IsObject() const 75 bool AdblockPlus::JsValue::IsObject() const
79 { 76 {
80 const JsContext context(jsEngine); 77 const JsContext context(jsEngine);
81 return unwrapValue()->IsObject(); 78 return UnwrapValue()->IsObject();
82 } 79 }
83 80
84 bool JsValue::IsArray() const 81 bool AdblockPlus::JsValue::IsArray() const
85 { 82 {
86 const JsContext context(jsEngine); 83 const JsContext context(jsEngine);
87 return unwrapValue()->IsArray(); 84 return UnwrapValue()->IsArray();
88 } 85 }
89 86
90 bool JsValue::IsFunction() const 87 bool AdblockPlus::JsValue::IsFunction() const
91 { 88 {
92 const JsContext context(jsEngine); 89 const JsContext context(jsEngine);
93 return unwrapValue()->IsFunction(); 90 return UnwrapValue()->IsFunction();
94 } 91 }
95 92
96 std::string JsValue::AsString() const 93 std::string AdblockPlus::JsValue::AsString() const
97 { 94 {
98 const JsContext context(jsEngine); 95 const JsContext context(jsEngine);
99 return Utils::FromV8String(unwrapValue()); 96 return Utils::FromV8String(UnwrapValue());
100 } 97 }
101 98
102 int64_t JsValue::AsInt() const 99 int64_t AdblockPlus::JsValue::AsInt() const
103 { 100 {
104 const JsContext context(jsEngine); 101 const JsContext context(jsEngine);
105 return unwrapValue()->IntegerValue(); 102 return UnwrapValue()->IntegerValue();
106 } 103 }
107 104
108 bool JsValue::AsBool() const 105 bool AdblockPlus::JsValue::AsBool() const
109 { 106 {
110 const JsContext context(jsEngine); 107 const JsContext context(jsEngine);
111 return unwrapValue()->BooleanValue(); 108 return UnwrapValue()->BooleanValue();
112 } 109 }
113 110
114 JsValueList JsValue::AsList() const 111 AdblockPlus::JsValueList AdblockPlus::JsValue::AsList() const
115 { 112 {
116 if (!IsArray()) 113 if (!IsArray())
117 throw std::runtime_error("Cannot convert a non-array to list"); 114 throw std::runtime_error("Cannot convert a non-array to list");
118 115
119 const JsContext context(jsEngine); 116 const JsContext context(jsEngine);
120 JsValueList result; 117 JsValueList result;
121 auto array = v8::Local<v8::Array>::Cast(unwrapValue()); 118 v8::Local<v8::Array> array = v8::Local<v8::Array>::Cast(UnwrapValue());
122 uint32_t length = array->Length(); 119 uint32_t length = array->Length();
123 for (uint32_t i = 0; i < length; i++) 120 for (uint32_t i = 0; i < length; i++)
124 { 121 {
125 v8::Local<v8::Value> item = array->Get(i); 122 v8::Local<v8::Value> item = array->Get(i);
126 result.push_back(std::make_shared<JsValue>(jsEngine, item, PrivateCtrArg())) ; 123 result.push_back(JsValuePtr(new JsValue(jsEngine, item)));
127 } 124 }
128 return result; 125 return result;
129 } 126 }
130 127
131 std::vector<std::string> JsValue::GetOwnPropertyNames() const 128 std::vector<std::string> AdblockPlus::JsValue::GetOwnPropertyNames() const
132 { 129 {
133 if (!IsObject()) 130 if (!IsObject())
134 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");
135 132
136 const JsContext context(jsEngine); 133 const JsContext context(jsEngine);
134 v8::Local<v8::Object> object = v8::Local<v8::Object>::Cast(UnwrapValue());
135 JsValueList properties = JsValuePtr(new JsValue(jsEngine, object->GetOwnProper tyNames()))->AsList();
137 std::vector<std::string> result; 136 std::vector<std::string> result;
138 auto object = v8::Local<v8::Object>::Cast(unwrapValue());
139 JsValueList properties = std::make_shared<JsValue>(jsEngine, object->GetOwnPro pertyNames(), PrivateCtrArg())->AsList();
140 for (JsValueList::iterator it = properties.begin(); it != properties.end(); ++ it) 137 for (JsValueList::iterator it = properties.begin(); it != properties.end(); ++ it)
141 result.push_back((*it)->AsString()); 138 result.push_back((*it)->AsString());
142 return result; 139 return result;
143 } 140 }
144 141
145 142
146 JsValuePtr JsValue::GetProperty(const std::string& name) const 143 AdblockPlus::JsValuePtr AdblockPlus::JsValue::GetProperty(const std::string& nam e) const
147 { 144 {
148 if (!IsObject()) 145 if (!IsObject())
149 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");
150 147
151 const JsContext context(jsEngine); 148 const JsContext context(jsEngine);
152 v8::Local<v8::String> property = Utils::ToV8String(jsEngine->isolate, name); 149 v8::Local<v8::String> property = Utils::ToV8String(name);
153 auto obj = v8::Local<v8::Object>::Cast(unwrapValue()); 150 v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue());
154 return std::make_shared<JsValue>(jsEngine, obj->Get(property), PrivateCtrArg() ); 151 return JsValuePtr(new JsValue(jsEngine, obj->Get(property)));
155 } 152 }
156 153
157 void JsValue::SetProperty(const std::string& name, const v8::Handle<v8::Value> v al) 154 void AdblockPlus::JsValue::SetProperty(const std::string& name, v8::Handle<v8::V alue> val)
158 { 155 {
159 if (!IsObject()) 156 if (!IsObject())
160 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");
161 158
162 v8::Local<v8::String> property = Utils::ToV8String(jsEngine->isolate, name); 159 v8::Local<v8::String> property = Utils::ToV8String(name);
163 auto obj = v8::Local<v8::Object>::Cast(unwrapValue()); 160 v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue());
164 obj->Set(property, val); 161 obj->Set(property, val);
165 } 162 }
166 163
167 v8::Local<v8::Value> JsValue::unwrapValue() const 164 v8::Local<v8::Value> AdblockPlus::JsValue::UnwrapValue() const
168 { 165 {
169 return v8::Local<v8::Value>::New(jsEngine->isolate, value); 166 return v8::Local<v8::Value>::New(jsEngine->isolate, value);
170 } 167 }
171 168
172 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)
173 { 170 {
174 const JsContext context(jsEngine); 171 const JsContext context(jsEngine);
175 SetProperty(name, Utils::ToV8String(jsEngine->isolate, val)); 172 SetProperty(name, Utils::ToV8String(val));
176 } 173 }
177 174
178 void JsValue::SetProperty(const std::string& name, int64_t val) 175 void AdblockPlus::JsValue::SetProperty(const std::string& name, int64_t val)
179 { 176 {
180 const JsContext context(jsEngine); 177 const JsContext context(jsEngine);
181 SetProperty(name, v8::Number::New(jsEngine->isolate, val)); 178 SetProperty(name, v8::Number::New(val));
182 } 179 }
183 180
184 void JsValue::SetProperty(const std::string& name, const JsValuePtr& val) 181 void AdblockPlus::JsValue::SetProperty(const std::string& name, const JsValuePtr & val)
185 { 182 {
186 const JsContext context(jsEngine); 183 const JsContext context(jsEngine);
187 SetProperty(name, val->unwrapValue()); 184 SetProperty(name, val->UnwrapValue());
188 } 185 }
189 186
190 void JsValue::SetProperty(const std::string& name, bool val) 187 void AdblockPlus::JsValue::SetProperty(const std::string& name, bool val)
191 { 188 {
192 const JsContext context(jsEngine); 189 const JsContext context(jsEngine);
193 SetProperty(name, v8::Boolean::New(val)); 190 SetProperty(name, v8::Boolean::New(val));
194 } 191 }
195 192
196 std::string JsValue::GetClass() const 193 std::string AdblockPlus::JsValue::GetClass() const
197 { 194 {
198 if (!IsObject()) 195 if (!IsObject())
199 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");
200 197
201 const JsContext context(jsEngine); 198 const JsContext context(jsEngine);
202 auto obj = v8::Local<v8::Object>::Cast(unwrapValue()); 199 v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(UnwrapValue());
203 return Utils::FromV8String(obj->GetConstructorName()); 200 return Utils::FromV8String(obj->GetConstructorName());
204 } 201 }
205 202
206 JsValuePtr JsValue::Call(const JsValueList& params, JsValuePtr thisPtr) const 203 AdblockPlus::JsValuePtr AdblockPlus::JsValue::Call(const JsValueList& params, Js ValuePtr thisPtr) const
207 { 204 {
208 const JsContext context(jsEngine);
209
210 if (!IsFunction()) 205 if (!IsFunction())
211 throw new std::runtime_error("Attempting to call a non-function"); 206 throw new std::runtime_error("Attempting to call a non-function");
212 207
208 const JsContext context(jsEngine);
213 if (!thisPtr) 209 if (!thisPtr)
214 { 210 {
215 auto 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);
216 thisPtr = std::make_shared<JsValue>(jsEngine, localContext->Global(), Privat eCtrArg()); 212 thisPtr = JsValuePtr(new JsValue(jsEngine, localContext->Global()));
217 } 213 }
218 if (!thisPtr->IsObject()) 214 if (!thisPtr->IsObject())
219 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");
220 auto thisObj = v8::Local<v8::Object>::Cast(thisPtr->unwrapValue()); 216 v8::Local<v8::Object> thisObj = v8::Local<v8::Object>::Cast(thisPtr->UnwrapVal ue());
221 217
222 std::vector<v8::Handle<v8::Value>> argv; 218 std::vector<v8::Handle<v8::Value>> argv;
223 for (JsValueList::const_iterator it = params.begin(); it != params.end(); ++it ) 219 for (JsValueList::const_iterator it = params.begin(); it != params.end(); ++it )
224 argv.push_back((*it)->unwrapValue()); 220 argv.push_back((*it)->UnwrapValue());
225 221
226 const v8::TryCatch tryCatch; 222 const v8::TryCatch tryCatch;
227 auto func = v8::Local<v8::Function>::Cast(unwrapValue()); 223 v8::Local<v8::Function> func = v8::Local<v8::Function>::Cast(UnwrapValue());
228 v8::Local<v8::Value> result = func->Call(thisObj, argv.size(), 224 v8::Local<v8::Value> result = func->Call(thisObj, argv.size(),
229 argv.size() ? &argv.front() : 0); 225 argv.size() ? &argv.front() : 0);
230 226
231 if (tryCatch.HasCaught()) 227 if (tryCatch.HasCaught())
232 throw JsError(tryCatch.Exception(), tryCatch.Message()); 228 throw JsError(tryCatch.Exception(), tryCatch.Message());
233 229
234 return std::make_shared<JsValue>(jsEngine, result, JsValue::Private::CtrArg()) ; 230 return JsValuePtr(new JsValue(jsEngine, result));
235 } 231 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld