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