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

Delta Between Two Patch Sets: src/JsValue.cpp

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

Powered by Google App Engine
This is Rietveld