Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(634)

Side by Side Diff: src/accessors.cc

Issue 259773009: Convert array.length to API-style accessor. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 6 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « src/accessors.h ('k') | src/bootstrapper.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after
167 bool Accessors::IsJSObjectFieldAccessor<HeapType>(Handle<HeapType> type, 167 bool Accessors::IsJSObjectFieldAccessor<HeapType>(Handle<HeapType> type,
168 Handle<String> name, 168 Handle<String> name,
169 int* object_offset); 169 int* object_offset);
170 170
171 171
172 // 172 //
173 // Accessors::ArrayLength 173 // Accessors::ArrayLength
174 // 174 //
175 175
176 176
177 Object* Accessors::ArrayGetLength(Isolate* isolate,
178 Object* object,
179 void*) {
180 // Traverse the prototype chain until we reach an array.
181 JSArray* holder = FindInstanceOf<JSArray>(isolate, object);
182 return holder == NULL ? Smi::FromInt(0) : holder->length();
183 }
184
185
186 // The helper function will 'flatten' Number objects. 177 // The helper function will 'flatten' Number objects.
187 Handle<Object> Accessors::FlattenNumber(Isolate* isolate, 178 Handle<Object> Accessors::FlattenNumber(Isolate* isolate,
188 Handle<Object> value) { 179 Handle<Object> value) {
189 if (value->IsNumber() || !value->IsJSValue()) return value; 180 if (value->IsNumber() || !value->IsJSValue()) return value;
190 Handle<JSValue> wrapper = Handle<JSValue>::cast(value); 181 Handle<JSValue> wrapper = Handle<JSValue>::cast(value);
191 ASSERT(wrapper->GetIsolate()->context()->native_context()->number_function()-> 182 ASSERT(wrapper->GetIsolate()->context()->native_context()->number_function()->
192 has_initial_map()); 183 has_initial_map());
193 if (wrapper->map() == 184 if (wrapper->map() ==
194 isolate->context()->native_context()->number_function()->initial_map()) { 185 isolate->context()->native_context()->number_function()->initial_map()) {
195 return handle(wrapper->value(), isolate); 186 return handle(wrapper->value(), isolate);
196 } 187 }
197 188
198 return value; 189 return value;
199 } 190 }
200 191
201 192
202 Object* Accessors::ArraySetLength(Isolate* isolate, 193 void Accessors::ArrayLengthGetter(
203 JSObject* object_raw, 194 v8::Local<v8::String> name,
204 Object* value_raw, 195 const v8::PropertyCallbackInfo<v8::Value>& info) {
205 void*) { 196 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(info.GetIsolate());
197 DisallowHeapAllocation no_allocation;
206 HandleScope scope(isolate); 198 HandleScope scope(isolate);
207 Handle<JSObject> object(object_raw, isolate); 199 Object* object = *Utils::OpenHandle(*info.This());
208 Handle<Object> value(value_raw, isolate); 200 // Traverse the prototype chain until we reach an array.
201 JSArray* holder = FindInstanceOf<JSArray>(isolate, object);
202 Object* result;
203 if (holder != NULL) {
204 result = holder->length();
205 } else {
206 result = Smi::FromInt(0);
207 }
208 info.GetReturnValue().Set(Utils::ToLocal(Handle<Object>(result, isolate)));
209 }
209 210
211
212 void Accessors::ArrayLengthSetter(
213 v8::Local<v8::String> name,
214 v8::Local<v8::Value> val,
215 const v8::PropertyCallbackInfo<void>& info) {
216 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(info.GetIsolate());
217 HandleScope scope(isolate);
218 Handle<JSObject> object = Utils::OpenHandle(*info.This());
219 Handle<Object> value = Utils::OpenHandle(*val);
210 // This means one of the object's prototypes is a JSArray and the 220 // This means one of the object's prototypes is a JSArray and the
211 // object does not have a 'length' property. Calling SetProperty 221 // object does not have a 'length' property. Calling SetProperty
212 // causes an infinite loop. 222 // causes an infinite loop.
213 if (!object->IsJSArray()) { 223 if (!object->IsJSArray()) {
214 Handle<Object> result; 224 MaybeHandle<Object> maybe_result =
215 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
216 isolate, result,
217 JSObject::SetLocalPropertyIgnoreAttributes( 225 JSObject::SetLocalPropertyIgnoreAttributes(
218 object, isolate->factory()->length_string(), value, NONE)); 226 object, isolate->factory()->length_string(), value, NONE);
219 return *result; 227 maybe_result.ToHandleChecked();
228 return;
220 } 229 }
221 230
222 value = FlattenNumber(isolate, value); 231 value = FlattenNumber(isolate, value);
223 232
224 Handle<JSArray> array_handle = Handle<JSArray>::cast(object); 233 Handle<JSArray> array_handle = Handle<JSArray>::cast(object);
225 234 MaybeHandle<Object> maybe;
226 Handle<Object> uint32_v; 235 Handle<Object> uint32_v;
227 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( 236 maybe = Execution::ToUint32(isolate, value);
228 isolate, uint32_v, Execution::ToUint32(isolate, value)); 237 if (!maybe.ToHandle(&uint32_v)) {
238 isolate->OptionalRescheduleException(false);
239 return;
240 }
229 Handle<Object> number_v; 241 Handle<Object> number_v;
230 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( 242 maybe = Execution::ToNumber(isolate, value);
231 isolate, number_v, Execution::ToNumber(isolate, value)); 243 if (!maybe.ToHandle(&number_v)) {
244 isolate->OptionalRescheduleException(false);
245 return;
246 }
232 247
233 if (uint32_v->Number() == number_v->Number()) { 248 if (uint32_v->Number() == number_v->Number()) {
234 Handle<Object> result; 249 MaybeHandle<Object> result;
235 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( 250 result = JSArray::SetElementsLength(array_handle, uint32_v);
236 isolate, result, 251 USE(result);
237 JSArray::SetElementsLength(array_handle, uint32_v)); 252 return;
238 return *result;
239 } 253 }
240 return isolate->Throw( 254
255 isolate->ScheduleThrow(
241 *isolate->factory()->NewRangeError("invalid_array_length", 256 *isolate->factory()->NewRangeError("invalid_array_length",
242 HandleVector<Object>(NULL, 0))); 257 HandleVector<Object>(NULL, 0)));
243 } 258 }
244 259
245 260
246 const AccessorDescriptor Accessors::ArrayLength = { 261 Handle<AccessorInfo> Accessors::ArrayLengthInfo(
247 ArrayGetLength, 262 Isolate* isolate, PropertyAttributes attributes) {
248 ArraySetLength, 263 return MakeAccessor(isolate,
249 0 264 isolate->factory()->length_string(),
250 }; 265 &ArrayLengthGetter,
266 &ArrayLengthSetter,
267 attributes);
268 }
269
251 270
252 271
253 // 272 //
254 // Accessors::StringLength 273 // Accessors::StringLength
255 // 274 //
256 275
257 void Accessors::StringLengthGetter( 276 void Accessors::StringLengthGetter(
258 v8::Local<v8::String> name, 277 v8::Local<v8::String> name,
259 const v8::PropertyCallbackInfo<v8::Value>& info) { 278 const v8::PropertyCallbackInfo<v8::Value>& info) {
260 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(info.GetIsolate()); 279 i::Isolate* isolate = reinterpret_cast<i::Isolate*>(info.GetIsolate());
(...skipping 1081 matching lines...) Expand 10 before | Expand all | Expand 10 after
1342 info->set_data(Smi::FromInt(index)); 1361 info->set_data(Smi::FromInt(index));
1343 Handle<Object> getter = v8::FromCData(isolate, &ModuleGetExport); 1362 Handle<Object> getter = v8::FromCData(isolate, &ModuleGetExport);
1344 Handle<Object> setter = v8::FromCData(isolate, &ModuleSetExport); 1363 Handle<Object> setter = v8::FromCData(isolate, &ModuleSetExport);
1345 info->set_getter(*getter); 1364 info->set_getter(*getter);
1346 if (!(attributes & ReadOnly)) info->set_setter(*setter); 1365 if (!(attributes & ReadOnly)) info->set_setter(*setter);
1347 return info; 1366 return info;
1348 } 1367 }
1349 1368
1350 1369
1351 } } // namespace v8::internal 1370 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/accessors.h ('k') | src/bootstrapper.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698