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

Side by Side Diff: src/JsValue.cpp

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

Powered by Google App Engine
This is Rietveld