OLD | NEW |
1 // Copyright 2013 the V8 project authors. All rights reserved. | 1 // Copyright 2013 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 4671 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4682 MaybeObject* maybe = map()->Copy(); | 4682 MaybeObject* maybe = map()->Copy(); |
4683 if (!maybe->To(&new_map)) return maybe; | 4683 if (!maybe->To(&new_map)) return maybe; |
4684 | 4684 |
4685 new_map->set_is_extensible(false); | 4685 new_map->set_is_extensible(false); |
4686 set_map(new_map); | 4686 set_map(new_map); |
4687 ASSERT(!map()->is_extensible()); | 4687 ASSERT(!map()->is_extensible()); |
4688 return new_map; | 4688 return new_map; |
4689 } | 4689 } |
4690 | 4690 |
4691 | 4691 |
| 4692 MUST_USE_RESULT MaybeObject* JSObject::DeepCopy(Isolate* isolate) { |
| 4693 StackLimitCheck check(isolate); |
| 4694 if (check.HasOverflowed()) return isolate->StackOverflow(); |
| 4695 |
| 4696 Heap* heap = isolate->heap(); |
| 4697 Object* result; |
| 4698 { MaybeObject* maybe_result = heap->CopyJSObject(this); |
| 4699 if (!maybe_result->ToObject(&result)) return maybe_result; |
| 4700 } |
| 4701 JSObject* copy = JSObject::cast(result); |
| 4702 |
| 4703 // Deep copy local properties. |
| 4704 if (copy->HasFastProperties()) { |
| 4705 FixedArray* properties = copy->properties(); |
| 4706 for (int i = 0; i < properties->length(); i++) { |
| 4707 Object* value = properties->get(i); |
| 4708 if (value->IsJSObject()) { |
| 4709 JSObject* js_object = JSObject::cast(value); |
| 4710 { MaybeObject* maybe_result = js_object->DeepCopy(isolate); |
| 4711 if (!maybe_result->ToObject(&result)) return maybe_result; |
| 4712 } |
| 4713 properties->set(i, result); |
| 4714 } |
| 4715 } |
| 4716 int nof = copy->map()->inobject_properties(); |
| 4717 for (int i = 0; i < nof; i++) { |
| 4718 Object* value = copy->InObjectPropertyAt(i); |
| 4719 if (value->IsJSObject()) { |
| 4720 JSObject* js_object = JSObject::cast(value); |
| 4721 { MaybeObject* maybe_result = js_object->DeepCopy(isolate); |
| 4722 if (!maybe_result->ToObject(&result)) return maybe_result; |
| 4723 } |
| 4724 copy->InObjectPropertyAtPut(i, result); |
| 4725 } |
| 4726 } |
| 4727 } else { |
| 4728 { MaybeObject* maybe_result = |
| 4729 heap->AllocateFixedArray(copy->NumberOfLocalProperties()); |
| 4730 if (!maybe_result->ToObject(&result)) return maybe_result; |
| 4731 } |
| 4732 FixedArray* names = FixedArray::cast(result); |
| 4733 copy->GetLocalPropertyNames(names, 0); |
| 4734 for (int i = 0; i < names->length(); i++) { |
| 4735 ASSERT(names->get(i)->IsString()); |
| 4736 String* key_string = String::cast(names->get(i)); |
| 4737 PropertyAttributes attributes = |
| 4738 copy->GetLocalPropertyAttribute(key_string); |
| 4739 // Only deep copy fields from the object literal expression. |
| 4740 // In particular, don't try to copy the length attribute of |
| 4741 // an array. |
| 4742 if (attributes != NONE) continue; |
| 4743 Object* value = |
| 4744 copy->GetProperty(key_string, &attributes)->ToObjectUnchecked(); |
| 4745 if (value->IsJSObject()) { |
| 4746 JSObject* js_object = JSObject::cast(value); |
| 4747 { MaybeObject* maybe_result = js_object->DeepCopy(isolate); |
| 4748 if (!maybe_result->ToObject(&result)) return maybe_result; |
| 4749 } |
| 4750 { MaybeObject* maybe_result = |
| 4751 // Creating object copy for literals. No strict mode needed. |
| 4752 copy->SetProperty(key_string, result, NONE, kNonStrictMode); |
| 4753 if (!maybe_result->ToObject(&result)) return maybe_result; |
| 4754 } |
| 4755 } |
| 4756 } |
| 4757 } |
| 4758 |
| 4759 // Deep copy local elements. |
| 4760 // Pixel elements cannot be created using an object literal. |
| 4761 ASSERT(!copy->HasExternalArrayElements()); |
| 4762 switch (copy->GetElementsKind()) { |
| 4763 case FAST_SMI_ELEMENTS: |
| 4764 case FAST_ELEMENTS: |
| 4765 case FAST_HOLEY_SMI_ELEMENTS: |
| 4766 case FAST_HOLEY_ELEMENTS: { |
| 4767 FixedArray* elements = FixedArray::cast(copy->elements()); |
| 4768 if (elements->map() == heap->fixed_cow_array_map()) { |
| 4769 isolate->counters()->cow_arrays_created_runtime()->Increment(); |
| 4770 #ifdef DEBUG |
| 4771 for (int i = 0; i < elements->length(); i++) { |
| 4772 ASSERT(!elements->get(i)->IsJSObject()); |
| 4773 } |
| 4774 #endif |
| 4775 } else { |
| 4776 for (int i = 0; i < elements->length(); i++) { |
| 4777 Object* value = elements->get(i); |
| 4778 ASSERT(value->IsSmi() || |
| 4779 value->IsTheHole() || |
| 4780 (IsFastObjectElementsKind(copy->GetElementsKind()))); |
| 4781 if (value->IsJSObject()) { |
| 4782 JSObject* js_object = JSObject::cast(value); |
| 4783 { MaybeObject* maybe_result = js_object->DeepCopy(isolate); |
| 4784 if (!maybe_result->ToObject(&result)) return maybe_result; |
| 4785 } |
| 4786 elements->set(i, result); |
| 4787 } |
| 4788 } |
| 4789 } |
| 4790 break; |
| 4791 } |
| 4792 case DICTIONARY_ELEMENTS: { |
| 4793 SeededNumberDictionary* element_dictionary = copy->element_dictionary(); |
| 4794 int capacity = element_dictionary->Capacity(); |
| 4795 for (int i = 0; i < capacity; i++) { |
| 4796 Object* k = element_dictionary->KeyAt(i); |
| 4797 if (element_dictionary->IsKey(k)) { |
| 4798 Object* value = element_dictionary->ValueAt(i); |
| 4799 if (value->IsJSObject()) { |
| 4800 JSObject* js_object = JSObject::cast(value); |
| 4801 { MaybeObject* maybe_result = js_object->DeepCopy(isolate); |
| 4802 if (!maybe_result->ToObject(&result)) return maybe_result; |
| 4803 } |
| 4804 element_dictionary->ValueAtPut(i, result); |
| 4805 } |
| 4806 } |
| 4807 } |
| 4808 break; |
| 4809 } |
| 4810 case NON_STRICT_ARGUMENTS_ELEMENTS: |
| 4811 UNIMPLEMENTED(); |
| 4812 break; |
| 4813 case EXTERNAL_PIXEL_ELEMENTS: |
| 4814 case EXTERNAL_BYTE_ELEMENTS: |
| 4815 case EXTERNAL_UNSIGNED_BYTE_ELEMENTS: |
| 4816 case EXTERNAL_SHORT_ELEMENTS: |
| 4817 case EXTERNAL_UNSIGNED_SHORT_ELEMENTS: |
| 4818 case EXTERNAL_INT_ELEMENTS: |
| 4819 case EXTERNAL_UNSIGNED_INT_ELEMENTS: |
| 4820 case EXTERNAL_FLOAT_ELEMENTS: |
| 4821 case EXTERNAL_DOUBLE_ELEMENTS: |
| 4822 case FAST_DOUBLE_ELEMENTS: |
| 4823 case FAST_HOLEY_DOUBLE_ELEMENTS: |
| 4824 // No contained objects, nothing to do. |
| 4825 break; |
| 4826 } |
| 4827 return copy; |
| 4828 } |
| 4829 |
| 4830 |
4692 // Tests for the fast common case for property enumeration: | 4831 // Tests for the fast common case for property enumeration: |
4693 // - This object and all prototypes has an enum cache (which means that | 4832 // - This object and all prototypes has an enum cache (which means that |
4694 // it is no proxy, has no interceptors and needs no access checks). | 4833 // it is no proxy, has no interceptors and needs no access checks). |
4695 // - This object has no elements. | 4834 // - This object has no elements. |
4696 // - No prototype has enumerable properties/elements. | 4835 // - No prototype has enumerable properties/elements. |
4697 bool JSReceiver::IsSimpleEnum() { | 4836 bool JSReceiver::IsSimpleEnum() { |
4698 Heap* heap = GetHeap(); | 4837 Heap* heap = GetHeap(); |
4699 for (Object* o = this; | 4838 for (Object* o = this; |
4700 o != heap->null_value(); | 4839 o != heap->null_value(); |
4701 o = JSObject::cast(o)->GetPrototype()) { | 4840 o = JSObject::cast(o)->GetPrototype()) { |
(...skipping 9668 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
14370 set_year(Smi::FromInt(year), SKIP_WRITE_BARRIER); | 14509 set_year(Smi::FromInt(year), SKIP_WRITE_BARRIER); |
14371 set_month(Smi::FromInt(month), SKIP_WRITE_BARRIER); | 14510 set_month(Smi::FromInt(month), SKIP_WRITE_BARRIER); |
14372 set_day(Smi::FromInt(day), SKIP_WRITE_BARRIER); | 14511 set_day(Smi::FromInt(day), SKIP_WRITE_BARRIER); |
14373 set_weekday(Smi::FromInt(weekday), SKIP_WRITE_BARRIER); | 14512 set_weekday(Smi::FromInt(weekday), SKIP_WRITE_BARRIER); |
14374 set_hour(Smi::FromInt(hour), SKIP_WRITE_BARRIER); | 14513 set_hour(Smi::FromInt(hour), SKIP_WRITE_BARRIER); |
14375 set_min(Smi::FromInt(min), SKIP_WRITE_BARRIER); | 14514 set_min(Smi::FromInt(min), SKIP_WRITE_BARRIER); |
14376 set_sec(Smi::FromInt(sec), SKIP_WRITE_BARRIER); | 14515 set_sec(Smi::FromInt(sec), SKIP_WRITE_BARRIER); |
14377 } | 14516 } |
14378 | 14517 |
14379 } } // namespace v8::internal | 14518 } } // namespace v8::internal |
OLD | NEW |