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

Side by Side Diff: src/JsValue.cpp

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

Powered by Google App Engine
This is Rietveld