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

Side by Side Diff: src/runtime.cc

Issue 13064003: First steps towards implementing ArrayBuffer &co in V8 (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: ArrayBuffer.slice + more tests Created 7 years, 9 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
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
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
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 Failure::OutOfMemoryException(0x19);
827 }
828
829 allocated_length = static_cast<size_t>(value);
830 }
rossberg 2013/03/27 17:33:14 Nit: indentation is off
Dmitry Lomov (no reviews) 2013/03/27 18:45:00 Done.
831
832 void* data;
833 if (allocated_length != 0) {
834 data = malloc(allocated_length);
835
836 if (data == NULL) {
837 return Failure::OutOfMemoryException(0x1A);
838 }
839
840 memset(data, 0, allocated_length);
841 } else {
842 data = NULL;
843 }
844 holder->set_backing_store(data);
845
846 if (Smi::IsValid(allocated_length)) {
rossberg 2013/03/27 17:33:14 You can use isolate->heap()->NumberFromDouble here
Dmitry Lomov (no reviews) 2013/03/27 18:45:00 Done.
847 holder->set_byte_length(Smi::FromInt(static_cast<int>(allocated_length)));
848 } else {
849 holder->set_byte_length(*isolate->factory()->NewNumber(allocated_length));
850 }
851
852 v8::Isolate* external_isolate = reinterpret_cast<v8::Isolate*>(isolate);
853 v8::Handle<Object> external_holder(*holder);
854 Persistent<Object> weak_handle = Persistent<Object>::New(
855 external_isolate, external_holder);
856 weak_handle.MakeWeak(external_isolate, data, ArrayBufferWeakCallback);
857 weak_handle.MarkIndependent(external_isolate);
858 isolate->heap()->AdjustAmountOfExternalAllocatedMemory(allocated_length);
859
860 return *holder;
861 }
862
863
864 RUNTIME_FUNCTION(MaybeObject*, Runtime_ArrayBufferGetByteLength) {
865 NoHandleAllocation ha(isolate);
866 ASSERT(args.length() == 1);
867 CONVERT_ARG_CHECKED(JSArrayBuffer, holder, 0);
868 return holder->byte_length();
869 }
870
871
872 RUNTIME_FUNCTION(MaybeObject*, Runtime_ArrayBufferSliceImpl) {
873 HandleScope scope(isolate);
874 ASSERT(args.length() == 3);
875 CONVERT_ARG_HANDLE_CHECKED(JSArrayBuffer, holder, 0);
876 CONVERT_ARG_HANDLE_CHECKED(JSArrayBuffer, result, 1);
rossberg 2013/03/27 17:33:14 Nit: I wouldn't call this 'result'. How about 'tar
Dmitry Lomov (no reviews) 2013/03/27 18:45:00 Done.
877 CONVERT_DOUBLE_ARG_CHECKED(first, 2);
878 size_t start = static_cast<size_t>(first);
879 size_t holder_length = ArrayBufferAllocatedLength(isolate, *holder);
880 size_t result_length = ArrayBufferAllocatedLength(isolate, *result);
881
882 if (result_length == 0)
883 return isolate->heap()->undefined_value();
884
885 ASSERT(holder_length - result_length >= start);
886 uint8_t* holder_data = reinterpret_cast<uint8_t*>(holder->backing_store());
887 uint8_t* target_data = reinterpret_cast<uint8_t*>(result->backing_store());
888 CopyBytes(target_data, holder_data + start, result_length);
889 return isolate->heap()->undefined_value();
890 }
891
892
781 RUNTIME_FUNCTION(MaybeObject*, Runtime_SetInitialize) { 893 RUNTIME_FUNCTION(MaybeObject*, Runtime_SetInitialize) {
782 HandleScope scope(isolate); 894 HandleScope scope(isolate);
783 ASSERT(args.length() == 1); 895 ASSERT(args.length() == 1);
784 CONVERT_ARG_HANDLE_CHECKED(JSSet, holder, 0); 896 CONVERT_ARG_HANDLE_CHECKED(JSSet, holder, 0);
785 Handle<ObjectHashSet> table = isolate->factory()->NewObjectHashSet(0); 897 Handle<ObjectHashSet> table = isolate->factory()->NewObjectHashSet(0);
786 holder->set_table(*table); 898 holder->set_table(*table);
787 return *holder; 899 return *holder;
788 } 900 }
789 901
790 902
(...skipping 5000 matching lines...) Expand 10 before | Expand all | Expand 10 after
5791 CONVERT_DOUBLE_ARG_CHECKED(number, 0); 5903 CONVERT_DOUBLE_ARG_CHECKED(number, 0);
5792 5904
5793 // We do not include 0 so that we don't have to treat +0 / -0 cases. 5905 // We do not include 0 so that we don't have to treat +0 / -0 cases.
5794 if (number > 0 && number <= Smi::kMaxValue) { 5906 if (number > 0 && number <= Smi::kMaxValue) {
5795 return Smi::FromInt(static_cast<int>(number)); 5907 return Smi::FromInt(static_cast<int>(number));
5796 } 5908 }
5797 return isolate->heap()->NumberFromDouble(DoubleToInteger(number)); 5909 return isolate->heap()->NumberFromDouble(DoubleToInteger(number));
5798 } 5910 }
5799 5911
5800 5912
5913 // ES6 draft 9.1.11
5914 RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToPositiveInteger) {
5915 NoHandleAllocation ha(isolate);
5916 ASSERT(args.length() == 1);
5917
5918 CONVERT_DOUBLE_ARG_CHECKED(number, 0);
5919
5920 // We do not include 0 so that we don't have to treat +0 / -0 cases.
5921 if (number > 0 && number <= Smi::kMaxValue) {
5922 return Smi::FromInt(static_cast<int>(number));
5923 }
5924 if (number <= 0) {
5925 return Smi::FromInt(0);
5926 }
5927 return isolate->heap()->NumberFromDouble(DoubleToInteger(number));
5928 }
5929
5930
5801 RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToIntegerMapMinusZero) { 5931 RUNTIME_FUNCTION(MaybeObject*, Runtime_NumberToIntegerMapMinusZero) {
5802 NoHandleAllocation ha(isolate); 5932 NoHandleAllocation ha(isolate);
5803 ASSERT(args.length() == 1); 5933 ASSERT(args.length() == 1);
5804 5934
5805 CONVERT_DOUBLE_ARG_CHECKED(number, 0); 5935 CONVERT_DOUBLE_ARG_CHECKED(number, 0);
5806 5936
5807 // We do not include 0 so that we don't have to treat +0 / -0 cases. 5937 // We do not include 0 so that we don't have to treat +0 / -0 cases.
5808 if (number > 0 && number <= Smi::kMaxValue) { 5938 if (number > 0 && number <= Smi::kMaxValue) {
5809 return Smi::FromInt(static_cast<int>(number)); 5939 return Smi::FromInt(static_cast<int>(number));
5810 } 5940 }
(...skipping 7191 matching lines...) Expand 10 before | Expand all | Expand 10 after
13002 // Handle last resort GC and make sure to allow future allocations 13132 // Handle last resort GC and make sure to allow future allocations
13003 // to grow the heap without causing GCs (if possible). 13133 // to grow the heap without causing GCs (if possible).
13004 isolate->counters()->gc_last_resort_from_js()->Increment(); 13134 isolate->counters()->gc_last_resort_from_js()->Increment();
13005 isolate->heap()->CollectAllGarbage(Heap::kNoGCFlags, 13135 isolate->heap()->CollectAllGarbage(Heap::kNoGCFlags,
13006 "Runtime::PerformGC"); 13136 "Runtime::PerformGC");
13007 } 13137 }
13008 } 13138 }
13009 13139
13010 13140
13011 } } // namespace v8::internal 13141 } } // namespace v8::internal
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698