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

Side by Side Diff: src/objects.cc

Issue 598020: Refactor prototype setting code and expose SetPrototype to public V8 API. (Closed)
Patch Set: Next round of Mad's comments Created 10 years, 10 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
« no previous file with comments | « src/objects.h ('k') | test/cctest/test-api.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 2006-2009 the V8 project authors. All rights reserved. 1 // Copyright 2006-2009 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 5335 matching lines...) Expand 10 before | Expand all | Expand 10 after
5346 // set only element to len. 5346 // set only element to len.
5347 Object* obj = Heap::AllocateFixedArray(1); 5347 Object* obj = Heap::AllocateFixedArray(1);
5348 if (obj->IsFailure()) return obj; 5348 if (obj->IsFailure()) return obj;
5349 FixedArray::cast(obj)->set(0, len); 5349 FixedArray::cast(obj)->set(0, len);
5350 if (IsJSArray()) JSArray::cast(this)->set_length(Smi::FromInt(1)); 5350 if (IsJSArray()) JSArray::cast(this)->set_length(Smi::FromInt(1));
5351 set_elements(FixedArray::cast(obj)); 5351 set_elements(FixedArray::cast(obj));
5352 return this; 5352 return this;
5353 } 5353 }
5354 5354
5355 5355
5356 Object* JSObject::SetPrototype(Object* value,
5357 bool skip_hidden_prototypes) {
5358 // Silently ignore the change if value is not a JSObject or null.
5359 // SpiderMonkey behaves this way.
5360 if (!value->IsJSObject() && !value->IsNull()) return value;
5361
5362 // Before we can set the prototype we need to be sure
5363 // prototype cycles are prevented.
5364 // It is sufficient to validate that the receiver is not in the new prototype
5365 // chain.
5366 for (Object* pt = value; pt != Heap::null_value(); pt = pt->GetPrototype()) {
5367 if (JSObject::cast(pt) == this) {
5368 // Cycle detected.
5369 HandleScope scope;
5370 return Top::Throw(*Factory::NewError("cyclic_proto",
5371 HandleVector<Object>(NULL, 0)));
5372 }
5373 }
5374
5375 JSObject* real_receiver = this;
5376
5377 if (skip_hidden_prototypes) {
5378 // Find the first object in the chain whose prototype object is not
5379 // hidden and set the new prototype on that object.
5380 Object* current_proto = real_receiver->GetPrototype();
5381 while (current_proto->IsJSObject() &&
5382 JSObject::cast(current_proto)->map()->is_hidden_prototype()) {
5383 real_receiver = JSObject::cast(current_proto);
5384 current_proto = current_proto->GetPrototype();
5385 }
5386 }
5387
5388 // Set the new prototype of the object.
5389 Object* new_map = real_receiver->map()->CopyDropTransitions();
5390 if (new_map->IsFailure()) return new_map;
5391 Map::cast(new_map)->set_prototype(value);
5392 real_receiver->set_map(Map::cast(new_map));
5393
5394 return value;
5395 }
5396
5397
5356 bool JSObject::HasElementPostInterceptor(JSObject* receiver, uint32_t index) { 5398 bool JSObject::HasElementPostInterceptor(JSObject* receiver, uint32_t index) {
5357 switch (GetElementsKind()) { 5399 switch (GetElementsKind()) {
5358 case FAST_ELEMENTS: { 5400 case FAST_ELEMENTS: {
5359 uint32_t length = IsJSArray() ? 5401 uint32_t length = IsJSArray() ?
5360 static_cast<uint32_t> 5402 static_cast<uint32_t>
5361 (Smi::cast(JSArray::cast(this)->length())->value()) : 5403 (Smi::cast(JSArray::cast(this)->length())->value()) :
5362 static_cast<uint32_t>(FixedArray::cast(elements())->length()); 5404 static_cast<uint32_t>(FixedArray::cast(elements())->length());
5363 if ((index < length) && 5405 if ((index < length) &&
5364 !FixedArray::cast(elements())->get(index)->IsTheHole()) { 5406 !FixedArray::cast(elements())->get(index)->IsTheHole()) {
5365 return true; 5407 return true;
(...skipping 2934 matching lines...) Expand 10 before | Expand all | Expand 10 after
8300 if (break_point_objects()->IsUndefined()) return 0; 8342 if (break_point_objects()->IsUndefined()) return 0;
8301 // Single beak point. 8343 // Single beak point.
8302 if (!break_point_objects()->IsFixedArray()) return 1; 8344 if (!break_point_objects()->IsFixedArray()) return 1;
8303 // Multiple break points. 8345 // Multiple break points.
8304 return FixedArray::cast(break_point_objects())->length(); 8346 return FixedArray::cast(break_point_objects())->length();
8305 } 8347 }
8306 #endif 8348 #endif
8307 8349
8308 8350
8309 } } // namespace v8::internal 8351 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/objects.h ('k') | test/cctest/test-api.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698