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

Side by Side Diff: src/JsValue.cpp

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