OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/accessors.h" | 5 #include "src/accessors.h" |
6 | 6 |
7 #include "src/api.h" | 7 #include "src/api.h" |
8 #include "src/contexts.h" | 8 #include "src/contexts.h" |
9 #include "src/deoptimizer.h" | 9 #include "src/deoptimizer.h" |
10 #include "src/execution.h" | 10 #include "src/execution.h" |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
191 const v8::PropertyCallbackInfo<v8::Value>& info) { | 191 const v8::PropertyCallbackInfo<v8::Value>& info) { |
192 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(info.GetIsolate()); | 192 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(info.GetIsolate()); |
193 DisallowHeapAllocation no_allocation; | 193 DisallowHeapAllocation no_allocation; |
194 HandleScope scope(isolate); | 194 HandleScope scope(isolate); |
195 JSArray* holder = JSArray::cast(*Utils::OpenHandle(*info.Holder())); | 195 JSArray* holder = JSArray::cast(*Utils::OpenHandle(*info.Holder())); |
196 Object* result = holder->length(); | 196 Object* result = holder->length(); |
197 info.GetReturnValue().Set(Utils::ToLocal(Handle<Object>(result, isolate))); | 197 info.GetReturnValue().Set(Utils::ToLocal(Handle<Object>(result, isolate))); |
198 } | 198 } |
199 | 199 |
200 | 200 |
201 // Tries to non-observably convert |value| to a valid array length. | |
202 // Returns false if it fails. | |
203 static bool FastAsArrayLength(Isolate* isolate, Handle<Object> value, | |
204 uint32_t* length) { | |
205 if (value->ToArrayLength(length)) return true; | |
206 // We don't support AsArrayLength, so use AsArrayIndex for now. This just | |
207 // misses out on kMaxUInt32. | |
208 if (value->IsString()) return String::cast(*value)->AsArrayIndex(length); | |
209 return false; | |
210 } | |
211 | |
212 | |
213 void Accessors::ArrayLengthSetter( | 201 void Accessors::ArrayLengthSetter( |
214 v8::Local<v8::Name> name, | 202 v8::Local<v8::Name> name, |
215 v8::Local<v8::Value> val, | 203 v8::Local<v8::Value> val, |
216 const v8::PropertyCallbackInfo<void>& info) { | 204 const v8::PropertyCallbackInfo<void>& info) { |
217 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(info.GetIsolate()); | 205 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(info.GetIsolate()); |
218 HandleScope scope(isolate); | 206 HandleScope scope(isolate); |
219 | 207 |
220 Handle<JSObject> object = Utils::OpenHandle(*info.This()); | 208 Handle<JSObject> object = Utils::OpenHandle(*info.This()); |
221 Handle<JSArray> array = Handle<JSArray>::cast(object); | 209 Handle<JSArray> array = Handle<JSArray>::cast(object); |
222 Handle<Object> length_obj = Utils::OpenHandle(*val); | 210 Handle<Object> length_obj = Utils::OpenHandle(*val); |
223 | 211 |
224 uint32_t length = 0; | 212 uint32_t length = 0; |
225 if (!FastAsArrayLength(isolate, length_obj, &length)) { | 213 if (!JSArray::AnythingToArrayLength(isolate, length_obj, &length)) { |
226 Handle<Object> uint32_v; | 214 isolate->OptionalRescheduleException(false); |
227 if (!Object::ToUint32(isolate, length_obj).ToHandle(&uint32_v)) { | 215 return; |
228 isolate->OptionalRescheduleException(false); | |
229 return; | |
230 } | |
231 | |
232 Handle<Object> number_v; | |
233 if (!Object::ToNumber(length_obj).ToHandle(&number_v)) { | |
234 isolate->OptionalRescheduleException(false); | |
235 return; | |
236 } | |
237 | |
238 if (uint32_v->Number() != number_v->Number()) { | |
239 Handle<Object> exception = isolate->factory()->NewRangeError( | |
240 MessageTemplate::kInvalidArrayLength); | |
241 return isolate->ScheduleThrow(*exception); | |
242 } | |
243 | |
244 CHECK(uint32_v->ToArrayLength(&length)); | |
245 } | 216 } |
246 | 217 |
247 if (JSArray::ObservableSetLength(array, length).is_null()) { | 218 if (JSArray::ObservableSetLength(array, length).is_null()) { |
248 isolate->OptionalRescheduleException(false); | 219 isolate->OptionalRescheduleException(false); |
249 } | 220 } |
250 } | 221 } |
251 | 222 |
252 | 223 |
253 Handle<AccessorInfo> Accessors::ArrayLengthInfo( | 224 Handle<AccessorInfo> Accessors::ArrayLengthInfo( |
254 Isolate* isolate, PropertyAttributes attributes) { | 225 Isolate* isolate, PropertyAttributes attributes) { |
255 return MakeAccessor(isolate, | 226 return MakeAccessor(isolate, |
256 isolate->factory()->length_string(), | 227 isolate->factory()->length_string(), |
257 &ArrayLengthGetter, | 228 &ArrayLengthGetter, |
258 &ArrayLengthSetter, | 229 &ArrayLengthSetter, |
259 attributes); | 230 attributes); |
260 } | 231 } |
261 | 232 |
262 | 233 |
263 | |
264 // | 234 // |
265 // Accessors::StringLength | 235 // Accessors::StringLength |
266 // | 236 // |
267 | 237 |
268 void Accessors::StringLengthGetter( | 238 void Accessors::StringLengthGetter( |
269 v8::Local<v8::Name> name, | 239 v8::Local<v8::Name> name, |
270 const v8::PropertyCallbackInfo<v8::Value>& info) { | 240 const v8::PropertyCallbackInfo<v8::Value>& info) { |
271 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(info.GetIsolate()); | 241 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(info.GetIsolate()); |
272 DisallowHeapAllocation no_allocation; | 242 DisallowHeapAllocation no_allocation; |
273 HandleScope scope(isolate); | 243 HandleScope scope(isolate); |
(...skipping 1236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1510 Handle<Object> getter = v8::FromCData(isolate, &ModuleGetExport); | 1480 Handle<Object> getter = v8::FromCData(isolate, &ModuleGetExport); |
1511 Handle<Object> setter = v8::FromCData(isolate, &ModuleSetExport); | 1481 Handle<Object> setter = v8::FromCData(isolate, &ModuleSetExport); |
1512 info->set_getter(*getter); | 1482 info->set_getter(*getter); |
1513 if (!(attributes & ReadOnly)) info->set_setter(*setter); | 1483 if (!(attributes & ReadOnly)) info->set_setter(*setter); |
1514 return info; | 1484 return info; |
1515 } | 1485 } |
1516 | 1486 |
1517 | 1487 |
1518 } // namespace internal | 1488 } // namespace internal |
1519 } // namespace v8 | 1489 } // namespace v8 |
OLD | NEW |