| OLD | NEW |
| 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 |
| 11 // with the distribution. | 11 // with the distribution. |
| 12 // * Neither the name of Google Inc. nor the names of its | 12 // * Neither the name of Google Inc. nor the names of its |
| 13 // contributors may be used to endorse or promote products derived | 13 // contributors may be used to endorse or promote products derived |
| 14 // from this software without specific prior written permission. | 14 // from this software without specific prior written permission. |
| 15 // | 15 // |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | 27 |
| 28 #include <stdlib.h> | 28 #include <stdlib.h> |
| 29 #include <limits> |
| 29 | 30 |
| 30 #include "v8.h" | 31 #include "v8.h" |
| 31 | 32 |
| 32 #include "accessors.h" | 33 #include "accessors.h" |
| 33 #include "api.h" | 34 #include "api.h" |
| 34 #include "arguments.h" | 35 #include "arguments.h" |
| 35 #include "bootstrapper.h" | 36 #include "bootstrapper.h" |
| 36 #include "codegen.h" | 37 #include "codegen.h" |
| 37 #include "compilation-cache.h" | 38 #include "compilation-cache.h" |
| 38 #include "compiler.h" | 39 #include "compiler.h" |
| (...skipping 732 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 771 | 772 |
| 772 RUNTIME_FUNCTION(MaybeObject*, Runtime_Fix) { | 773 RUNTIME_FUNCTION(MaybeObject*, Runtime_Fix) { |
| 773 NoHandleAllocation ha(isolate); | 774 NoHandleAllocation ha(isolate); |
| 774 ASSERT(args.length() == 1); | 775 ASSERT(args.length() == 1); |
| 775 CONVERT_ARG_CHECKED(JSProxy, proxy, 0); | 776 CONVERT_ARG_CHECKED(JSProxy, proxy, 0); |
| 776 proxy->Fix(); | 777 proxy->Fix(); |
| 777 return isolate->heap()->undefined_value(); | 778 return isolate->heap()->undefined_value(); |
| 778 } | 779 } |
| 779 | 780 |
| 780 | 781 |
| 782 static size_t ArrayBufferAllocatedLength(Isolate* isolate, |
| 783 JSArrayBuffer* buffer) { |
| 784 NoHandleAllocation hc(isolate); |
| 785 Object* byte_length = buffer->byte_length(); |
| 786 if (byte_length->IsSmi()) { |
| 787 return Smi::cast(byte_length)->value(); |
| 788 } else { |
| 789 double value = HeapNumber::cast(byte_length)->value(); |
| 790 return static_cast<size_t>(value); |
| 791 } |
| 792 } |
| 793 |
| 794 |
| 795 static void ArrayBufferWeakCallback(v8::Isolate* external_isolate, |
| 796 Persistent<Value> object, |
| 797 void* data) { |
| 798 Isolate* isolate = reinterpret_cast<Isolate*>(external_isolate); |
| 799 HandleScope scope(isolate); |
| 800 Handle<Object> internal_object = Utils::OpenHandle(*object); |
| 801 |
| 802 size_t allocated_length = ArrayBufferAllocatedLength( |
| 803 isolate, JSArrayBuffer::cast(*internal_object)); |
| 804 isolate->heap()->AdjustAmountOfExternalAllocatedMemory(-allocated_length); |
| 805 if (data != NULL) |
| 806 free(data); |
| 807 object.Dispose(external_isolate); |
| 808 } |
| 809 |
| 810 |
| 811 RUNTIME_FUNCTION(MaybeObject*, Runtime_ArrayBufferInitialize) { |
| 812 HandleScope scope(isolate); |
| 813 ASSERT(args.length() == 2); |
| 814 CONVERT_ARG_HANDLE_CHECKED(JSArrayBuffer, holder, 0); |
| 815 CONVERT_ARG_HANDLE_CHECKED(Object, byteLength, 1); |
| 816 size_t allocated_length; |
| 817 if (byteLength->IsSmi()) { |
| 818 allocated_length = Smi::cast(*byteLength)->value(); |
| 819 } else { |
| 820 ASSERT(byteLength->IsHeapNumber()); |
| 821 double value = HeapNumber::cast(*byteLength)->value(); |
| 822 |
| 823 ASSERT(value >= 0); |
| 824 |
| 825 if (value > std::numeric_limits<size_t>::max()) { |
| 826 return isolate->Throw( |
| 827 *isolate->factory()->NewRangeError("invalid_array_buffer_length", |
| 828 HandleVector<Object>(NULL, 0))); |
| 829 } |
| 830 |
| 831 allocated_length = static_cast<size_t>(value); |
| 832 } |
| 833 |
| 834 void* data; |
| 835 if (allocated_length != 0) { |
| 836 data = malloc(allocated_length); |
| 837 |
| 838 if (data == NULL) { |
| 839 return isolate->Throw(*isolate->factory()-> |
| 840 NewRangeError("invalid_array_buffer_length", |
| 841 HandleVector<Object>(NULL, 0))); |
| 842 } |
| 843 |
| 844 memset(data, 0, allocated_length); |
| 845 } else { |
| 846 data = NULL; |
| 847 } |
| 848 holder->set_backing_store(data); |
| 849 |
| 850 Object* byte_length; |
| 851 { |
| 852 MaybeObject* maybe_byte_length = |
| 853 isolate->heap()->NumberFromDouble(allocated_length); |
| 854 if (!maybe_byte_length->ToObject(&byte_length)) return maybe_byte_length; |
| 855 } |
| 856 CHECK(byte_length->IsSmi() || byte_length->IsHeapNumber()); |
| 857 holder->set_byte_length(byte_length); |
| 858 |
| 859 v8::Isolate* external_isolate = reinterpret_cast<v8::Isolate*>(isolate); |
| 860 v8::Handle<Object> external_holder(*holder); |
| 861 Persistent<Object> weak_handle = Persistent<Object>::New( |
| 862 external_isolate, external_holder); |
| 863 weak_handle.MakeWeak(external_isolate, data, ArrayBufferWeakCallback); |
| 864 weak_handle.MarkIndependent(external_isolate); |
| 865 isolate->heap()->AdjustAmountOfExternalAllocatedMemory(allocated_length); |
| 866 |
| 867 return *holder; |
| 868 } |
| 869 |
| 870 |
| 871 RUNTIME_FUNCTION(MaybeObject*, Runtime_ArrayBufferGetByteLength) { |
| 872 NoHandleAllocation ha(isolate); |
| 873 ASSERT(args.length() == 1); |
| 874 CONVERT_ARG_CHECKED(JSArrayBuffer, holder, 0); |
| 875 return holder->byte_length(); |
| 876 } |
| 877 |
| 878 |
| 879 RUNTIME_FUNCTION(MaybeObject*, Runtime_ArrayBufferSliceImpl) { |
| 880 HandleScope scope(isolate); |
| 881 ASSERT(args.length() == 3); |
| 882 CONVERT_ARG_HANDLE_CHECKED(JSArrayBuffer, source, 0); |
| 883 CONVERT_ARG_HANDLE_CHECKED(JSArrayBuffer, target, 1); |
| 884 CONVERT_DOUBLE_ARG_CHECKED(first, 2); |
| 885 size_t start = static_cast<size_t>(first); |
| 886 size_t source_length = ArrayBufferAllocatedLength(isolate, *source); |
| 887 size_t target_length = ArrayBufferAllocatedLength(isolate, *target); |
| 888 |
| 889 if (target_length == 0) |
| 890 return isolate->heap()->undefined_value(); |
| 891 |
| 892 ASSERT(source_length - target_length >= start); |
| 893 uint8_t* source_data = reinterpret_cast<uint8_t*>(source->backing_store()); |
| 894 uint8_t* target_data = reinterpret_cast<uint8_t*>(target->backing_store()); |
| 895 CopyBytes(target_data, source_data + start, target_length); |
| 896 return isolate->heap()->undefined_value(); |
| 897 } |
| 898 |
| 899 |
| 781 RUNTIME_FUNCTION(MaybeObject*, Runtime_SetInitialize) { | 900 RUNTIME_FUNCTION(MaybeObject*, Runtime_SetInitialize) { |
| 782 HandleScope scope(isolate); | 901 HandleScope scope(isolate); |
| 783 ASSERT(args.length() == 1); | 902 ASSERT(args.length() == 1); |
| 784 CONVERT_ARG_HANDLE_CHECKED(JSSet, holder, 0); | 903 CONVERT_ARG_HANDLE_CHECKED(JSSet, holder, 0); |
| 785 Handle<ObjectHashSet> table = isolate->factory()->NewObjectHashSet(0); | 904 Handle<ObjectHashSet> table = isolate->factory()->NewObjectHashSet(0); |
| 786 holder->set_table(*table); | 905 holder->set_table(*table); |
| 787 return *holder; | 906 return *holder; |
| 788 } | 907 } |
| 789 | 908 |
| 790 | 909 |
| (...skipping 5000 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 5791 CONVERT_DOUBLE_ARG_CHECKED(number, 0); | 5910 CONVERT_DOUBLE_ARG_CHECKED(number, 0); |
| 5792 | 5911 |
| 5793 // We do not include 0 so that we don't have to treat +0 / -0 cases. | 5912 // We do not include 0 so that we don't have to treat +0 / -0 cases. |
| 5794 if (number > 0 && number <= Smi::kMaxValue) { | 5913 if (number > 0 && number <= Smi::kMaxValue) { |
| 5795 return Smi::FromInt(static_cast<int>(number)); | 5914 return Smi::FromInt(static_cast<int>(number)); |
| 5796 } | 5915 } |
| 5797 return isolate->heap()->NumberFromDouble(DoubleToInteger(number)); | 5916 return isolate->heap()->NumberFromDouble(DoubleToInteger(number)); |
| 5798 } | 5917 } |
| 5799 | 5918 |
| 5800 | 5919 |
| 5920 // ES6 draft 9.1.11 |
| 5921 RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToPositiveInteger) { |
| 5922 NoHandleAllocation ha(isolate); |
| 5923 ASSERT(args.length() == 1); |
| 5924 |
| 5925 CONVERT_DOUBLE_ARG_CHECKED(number, 0); |
| 5926 |
| 5927 // We do not include 0 so that we don't have to treat +0 / -0 cases. |
| 5928 if (number > 0 && number <= Smi::kMaxValue) { |
| 5929 return Smi::FromInt(static_cast<int>(number)); |
| 5930 } |
| 5931 if (number <= 0) { |
| 5932 return Smi::FromInt(0); |
| 5933 } |
| 5934 return isolate->heap()->NumberFromDouble(DoubleToInteger(number)); |
| 5935 } |
| 5936 |
| 5937 |
| 5801 RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToIntegerMapMinusZero) { | 5938 RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToIntegerMapMinusZero) { |
| 5802 NoHandleAllocation ha(isolate); | 5939 NoHandleAllocation ha(isolate); |
| 5803 ASSERT(args.length() == 1); | 5940 ASSERT(args.length() == 1); |
| 5804 | 5941 |
| 5805 CONVERT_DOUBLE_ARG_CHECKED(number, 0); | 5942 CONVERT_DOUBLE_ARG_CHECKED(number, 0); |
| 5806 | 5943 |
| 5807 // We do not include 0 so that we don't have to treat +0 / -0 cases. | 5944 // We do not include 0 so that we don't have to treat +0 / -0 cases. |
| 5808 if (number > 0 && number <= Smi::kMaxValue) { | 5945 if (number > 0 && number <= Smi::kMaxValue) { |
| 5809 return Smi::FromInt(static_cast<int>(number)); | 5946 return Smi::FromInt(static_cast<int>(number)); |
| 5810 } | 5947 } |
| (...skipping 7191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 13002 // Handle last resort GC and make sure to allow future allocations | 13139 // Handle last resort GC and make sure to allow future allocations |
| 13003 // to grow the heap without causing GCs (if possible). | 13140 // to grow the heap without causing GCs (if possible). |
| 13004 isolate->counters()->gc_last_resort_from_js()->Increment(); | 13141 isolate->counters()->gc_last_resort_from_js()->Increment(); |
| 13005 isolate->heap()->CollectAllGarbage(Heap::kNoGCFlags, | 13142 isolate->heap()->CollectAllGarbage(Heap::kNoGCFlags, |
| 13006 "Runtime::PerformGC"); | 13143 "Runtime::PerformGC"); |
| 13007 } | 13144 } |
| 13008 } | 13145 } |
| 13009 | 13146 |
| 13010 | 13147 |
| 13011 } } // namespace v8::internal | 13148 } } // namespace v8::internal |
| OLD | NEW |