Chromium Code Reviews| 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 |
| (...skipping 667 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 678 array_buffer->set_flag(Smi::FromInt(0)); | 678 array_buffer->set_flag(Smi::FromInt(0)); |
| 679 array_buffer->set_is_external(is_external); | 679 array_buffer->set_is_external(is_external); |
| 680 | 680 |
| 681 Handle<Object> byte_length = | 681 Handle<Object> byte_length = |
| 682 isolate->factory()->NewNumberFromSize(allocated_length); | 682 isolate->factory()->NewNumberFromSize(allocated_length); |
| 683 CHECK(byte_length->IsSmi() || byte_length->IsHeapNumber()); | 683 CHECK(byte_length->IsSmi() || byte_length->IsHeapNumber()); |
| 684 array_buffer->set_byte_length(*byte_length); | 684 array_buffer->set_byte_length(*byte_length); |
| 685 | 685 |
| 686 array_buffer->set_weak_next(isolate->heap()->array_buffers_list()); | 686 array_buffer->set_weak_next(isolate->heap()->array_buffers_list()); |
| 687 isolate->heap()->set_array_buffers_list(*array_buffer); | 687 isolate->heap()->set_array_buffers_list(*array_buffer); |
| 688 array_buffer->set_weak_first_array(isolate->heap()->undefined_value()); | 688 array_buffer->set_weak_first_view(isolate->heap()->undefined_value()); |
| 689 } | 689 } |
| 690 | 690 |
| 691 | 691 |
| 692 bool Runtime::SetupArrayBufferAllocatingData( | 692 bool Runtime::SetupArrayBufferAllocatingData( |
| 693 Isolate* isolate, | 693 Isolate* isolate, |
| 694 Handle<JSArrayBuffer> array_buffer, | 694 Handle<JSArrayBuffer> array_buffer, |
| 695 size_t allocated_length) { | 695 size_t allocated_length) { |
| 696 void* data; | 696 void* data; |
| 697 CHECK(V8::ArrayBufferAllocator() != NULL); | 697 CHECK(V8::ArrayBufferAllocator() != NULL); |
| 698 if (allocated_length != 0) { | 698 if (allocated_length != 0) { |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 843 holder->set_byte_offset(*byte_offset_object); | 843 holder->set_byte_offset(*byte_offset_object); |
| 844 holder->set_byte_length(*byte_length_object); | 844 holder->set_byte_length(*byte_length_object); |
| 845 | 845 |
| 846 size_t byte_offset = NumberToSize(isolate, *byte_offset_object); | 846 size_t byte_offset = NumberToSize(isolate, *byte_offset_object); |
| 847 size_t byte_length = NumberToSize(isolate, *byte_length_object); | 847 size_t byte_length = NumberToSize(isolate, *byte_length_object); |
| 848 ASSERT(byte_length % elementSize == 0); | 848 ASSERT(byte_length % elementSize == 0); |
| 849 size_t length = byte_length / elementSize; | 849 size_t length = byte_length / elementSize; |
| 850 | 850 |
| 851 Handle<Object> length_obj = isolate->factory()->NewNumberFromSize(length); | 851 Handle<Object> length_obj = isolate->factory()->NewNumberFromSize(length); |
| 852 holder->set_length(*length_obj); | 852 holder->set_length(*length_obj); |
| 853 holder->set_weak_next(buffer->weak_first_array()); | 853 holder->set_weak_next(buffer->weak_first_view()); |
| 854 buffer->set_weak_first_array(*holder); | 854 buffer->set_weak_first_view(*holder); |
| 855 | 855 |
| 856 Handle<ExternalArray> elements = | 856 Handle<ExternalArray> elements = |
| 857 isolate->factory()->NewExternalArray( | 857 isolate->factory()->NewExternalArray( |
| 858 static_cast<int>(length), arrayType, | 858 static_cast<int>(length), arrayType, |
| 859 static_cast<uint8_t*>(buffer->backing_store()) + byte_offset); | 859 static_cast<uint8_t*>(buffer->backing_store()) + byte_offset); |
| 860 holder->set_elements(*elements); | 860 holder->set_elements(*elements); |
| 861 return isolate->heap()->undefined_value(); | 861 return isolate->heap()->undefined_value(); |
| 862 } | 862 } |
| 863 | 863 |
| 864 | 864 |
| (...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 996 JSObject::SetElement( | 996 JSObject::SetElement( |
| 997 target, static_cast<uint32_t>(offset + idx), value, | 997 target, static_cast<uint32_t>(offset + idx), value, |
| 998 NONE, kNonStrictMode); | 998 NONE, kNonStrictMode); |
| 999 } | 999 } |
| 1000 } | 1000 } |
| 1001 | 1001 |
| 1002 return isolate->heap()->true_value(); | 1002 return isolate->heap()->true_value(); |
| 1003 } | 1003 } |
| 1004 | 1004 |
| 1005 | 1005 |
| 1006 RUNTIME_FUNCTION(MaybeObject*, Runtime_DataViewInitialize) { | |
| 1007 HandleScope scope(isolate); | |
| 1008 ASSERT(args.length() == 4); | |
| 1009 CONVERT_ARG_HANDLE_CHECKED(JSDataView, holder, 0); | |
| 1010 CONVERT_ARG_HANDLE_CHECKED(JSArrayBuffer, buffer, 1); | |
| 1011 CONVERT_ARG_HANDLE_CHECKED(Object, byte_offset, 2); | |
| 1012 CONVERT_ARG_HANDLE_CHECKED(Object, byte_length, 3); | |
| 1013 | |
| 1014 holder->set_buffer(*buffer); | |
| 1015 ASSERT(byte_offset->IsNumber()); | |
| 1016 ASSERT( | |
| 1017 NumberToSize(isolate, buffer->byte_length()) >= | |
| 1018 NumberToSize(isolate, *byte_offset) | |
| 1019 + NumberToSize(isolate, *byte_length)); | |
| 1020 holder->set_byte_offset(*byte_offset); | |
| 1021 ASSERT(byte_length->IsNumber()); | |
| 1022 holder->set_byte_length(*byte_length); | |
| 1023 | |
| 1024 holder->set_weak_next(buffer->weak_first_view()); | |
| 1025 buffer->set_weak_first_view(*holder); | |
| 1026 | |
| 1027 return isolate->heap()->undefined_value(); | |
| 1028 } | |
| 1029 | |
| 1030 | |
| 1031 RUNTIME_FUNCTION(MaybeObject*, Runtime_DataViewGetBuffer) { | |
| 1032 HandleScope scope(isolate); | |
| 1033 ASSERT(args.length() == 1); | |
| 1034 CONVERT_ARG_HANDLE_CHECKED(Object, holder, 0); | |
| 1035 Handle<JSDataView> data_view(JSDataView::cast(*holder)); | |
|
rossberg
2013/06/21 12:25:53
You can just declare the type in the line above no
Dmitry Lomov (no reviews)
2013/06/21 12:39:20
Done.
| |
| 1036 return data_view->buffer(); | |
| 1037 } | |
| 1038 | |
| 1039 | |
| 1040 RUNTIME_FUNCTION(MaybeObject*, Runtime_DataViewGetByteOffset) { | |
| 1041 HandleScope scope(isolate); | |
| 1042 ASSERT(args.length() == 1); | |
| 1043 CONVERT_ARG_HANDLE_CHECKED(Object, holder, 0); | |
| 1044 Handle<JSDataView> data_view(JSDataView::cast(*holder)); | |
| 1045 return data_view->byte_offset(); | |
| 1046 } | |
| 1047 | |
| 1048 | |
| 1049 RUNTIME_FUNCTION(MaybeObject*, Runtime_DataViewGetByteLength) { | |
| 1050 HandleScope scope(isolate); | |
| 1051 ASSERT(args.length() == 1); | |
| 1052 CONVERT_ARG_HANDLE_CHECKED(Object, holder, 0); | |
| 1053 Handle<JSDataView> data_view(JSDataView::cast(*holder)); | |
| 1054 return data_view->byte_length(); | |
| 1055 } | |
| 1056 | |
| 1057 | |
| 1058 inline static bool NeedToFlipBytes(bool is_little_endian) { | |
| 1059 #ifdef V8_TARGET_LITTLE_ENDIAN | |
| 1060 return !is_little_endian; | |
| 1061 #else | |
| 1062 return is_little_endian; | |
| 1063 #endif | |
| 1064 } | |
| 1065 | |
| 1066 | |
| 1067 template<int n> | |
| 1068 inline void CopyBytes(uint8_t* target, uint8_t* source) { | |
| 1069 for (int i = 0; i < n; i++) { | |
| 1070 *(target++) = *(source++); | |
| 1071 } | |
| 1072 } | |
| 1073 | |
| 1074 | |
| 1075 template<int n> | |
| 1076 inline void FlipBytes(uint8_t* target, uint8_t* source) { | |
| 1077 source = source + (n-1); | |
| 1078 for (int i = 0; i < n; i++) { | |
| 1079 *(target++) = *(source--); | |
| 1080 } | |
| 1081 } | |
| 1082 | |
| 1083 | |
| 1084 template<typename T> | |
| 1085 inline static bool DataViewGetValue( | |
| 1086 Isolate* isolate, | |
| 1087 Handle<JSDataView> data_view, | |
| 1088 Handle<Object> byte_offset_obj, | |
| 1089 bool is_little_endian, | |
| 1090 T* result) { | |
| 1091 size_t byte_offset = NumberToSize(isolate, *byte_offset_obj); | |
| 1092 Handle<JSArrayBuffer> buffer(JSArrayBuffer::cast(data_view->buffer())); | |
| 1093 | |
| 1094 size_t data_view_byte_offset = | |
| 1095 NumberToSize(isolate, data_view->byte_offset()); | |
| 1096 size_t data_view_byte_length = | |
| 1097 NumberToSize(isolate, data_view->byte_length()); | |
| 1098 if (byte_offset + sizeof(T) > data_view_byte_length || | |
| 1099 byte_offset + sizeof(T) < byte_offset) { // overflow | |
| 1100 return false; | |
| 1101 } | |
| 1102 | |
| 1103 union Value { | |
| 1104 T data; | |
| 1105 uint8_t bytes[sizeof(T)]; | |
| 1106 }; | |
| 1107 | |
| 1108 Value value; | |
| 1109 size_t buffer_offset = data_view_byte_offset + byte_offset; | |
| 1110 ASSERT( | |
| 1111 NumberToSize(isolate, buffer->byte_length()) | |
| 1112 >= buffer_offset + sizeof(T)); | |
| 1113 uint8_t* source = | |
| 1114 static_cast<uint8_t*>(buffer->backing_store()) + buffer_offset; | |
| 1115 if (NeedToFlipBytes(is_little_endian)) { | |
| 1116 FlipBytes<sizeof(T)>(value.bytes, source); | |
| 1117 } else { | |
| 1118 CopyBytes<sizeof(T)>(value.bytes, source); | |
| 1119 } | |
| 1120 *result = value.data; | |
| 1121 return true; | |
| 1122 } | |
| 1123 | |
| 1124 | |
| 1125 template<typename T> | |
| 1126 static bool DataViewSetValue( | |
| 1127 Isolate* isolate, | |
| 1128 Handle<JSDataView> data_view, | |
| 1129 Handle<Object> byte_offset_obj, | |
| 1130 bool is_little_endian, | |
| 1131 T data) { | |
| 1132 size_t byte_offset = NumberToSize(isolate, *byte_offset_obj); | |
| 1133 Handle<JSArrayBuffer> buffer(JSArrayBuffer::cast(data_view->buffer())); | |
| 1134 | |
| 1135 size_t data_view_byte_offset = | |
| 1136 NumberToSize(isolate, data_view->byte_offset()); | |
| 1137 size_t data_view_byte_length = | |
| 1138 NumberToSize(isolate, data_view->byte_length()); | |
| 1139 if (byte_offset + sizeof(T) > data_view_byte_length || | |
| 1140 byte_offset + sizeof(T) < byte_offset) { // overflow | |
| 1141 return false; | |
| 1142 } | |
| 1143 | |
| 1144 union Value { | |
| 1145 T data; | |
| 1146 uint8_t bytes[sizeof(T)]; | |
| 1147 }; | |
| 1148 | |
| 1149 Value value; | |
| 1150 value.data = data; | |
| 1151 size_t buffer_offset = data_view_byte_offset + byte_offset; | |
| 1152 ASSERT( | |
| 1153 NumberToSize(isolate, buffer->byte_length()) | |
| 1154 >= buffer_offset + sizeof(T)); | |
| 1155 uint8_t* target = | |
| 1156 static_cast<uint8_t*>(buffer->backing_store()) + buffer_offset; | |
| 1157 if (NeedToFlipBytes(is_little_endian)) { | |
| 1158 FlipBytes<sizeof(T)>(target, value.bytes); | |
| 1159 } else { | |
| 1160 CopyBytes<sizeof(T)>(target, value.bytes); | |
| 1161 } | |
| 1162 return true; | |
| 1163 } | |
| 1164 | |
| 1165 | |
| 1166 #define DATA_VIEW_GETTER(TypeName, Type, Converter) \ | |
| 1167 RUNTIME_FUNCTION(MaybeObject*, Runtime_DataViewGet##TypeName) { \ | |
| 1168 HandleScope scope(isolate); \ | |
| 1169 ASSERT(args.length() == 3); \ | |
| 1170 CONVERT_ARG_HANDLE_CHECKED(JSDataView, holder, 0); \ | |
| 1171 CONVERT_ARG_HANDLE_CHECKED(Object, offset, 1); \ | |
| 1172 CONVERT_BOOLEAN_ARG_CHECKED(is_little_endian, 2); \ | |
| 1173 Type result; \ | |
| 1174 if (DataViewGetValue( \ | |
| 1175 isolate, holder, offset, is_little_endian, &result)) { \ | |
| 1176 return isolate->heap()->Converter(result); \ | |
| 1177 } else { \ | |
| 1178 return isolate->Throw(*isolate->factory()->NewRangeError( \ | |
| 1179 "invalid_data_view_accessor_offset", \ | |
| 1180 HandleVector<Object>(NULL, 0))); \ | |
| 1181 } \ | |
| 1182 } | |
| 1183 | |
| 1184 DATA_VIEW_GETTER(Uint8, uint8_t, NumberFromUint32) | |
| 1185 DATA_VIEW_GETTER(Int8, int8_t, NumberFromInt32) | |
| 1186 DATA_VIEW_GETTER(Uint16, uint16_t, NumberFromUint32) | |
| 1187 DATA_VIEW_GETTER(Int16, int16_t, NumberFromInt32) | |
| 1188 DATA_VIEW_GETTER(Uint32, uint32_t, NumberFromUint32) | |
| 1189 DATA_VIEW_GETTER(Int32, int32_t, NumberFromInt32) | |
| 1190 DATA_VIEW_GETTER(Float32, float, NumberFromDouble) | |
| 1191 DATA_VIEW_GETTER(Float64, double, NumberFromDouble) | |
| 1192 | |
| 1193 #undef DATA_VIEW_GETTER | |
| 1194 | |
| 1195 #define DATA_VIEW_SETTER(TypeName, Type) \ | |
| 1196 RUNTIME_FUNCTION(MaybeObject*, Runtime_DataViewSet##TypeName) { \ | |
| 1197 HandleScope scope(isolate); \ | |
| 1198 ASSERT(args.length() == 4); \ | |
| 1199 CONVERT_ARG_HANDLE_CHECKED(JSDataView, holder, 0); \ | |
| 1200 CONVERT_ARG_HANDLE_CHECKED(Object, offset, 1); \ | |
| 1201 CONVERT_ARG_HANDLE_CHECKED(Object, value, 2); \ | |
| 1202 CONVERT_BOOLEAN_ARG_CHECKED(is_little_endian, 3); \ | |
| 1203 Type v = static_cast<Type>(value->Number()); \ | |
| 1204 if (DataViewSetValue( \ | |
| 1205 isolate, holder, offset, is_little_endian, v)) { \ | |
| 1206 return isolate->heap()->undefined_value(); \ | |
| 1207 } else { \ | |
| 1208 return isolate->Throw(*isolate->factory()->NewRangeError( \ | |
| 1209 "invalid_data_view_accessor_offset", \ | |
| 1210 HandleVector<Object>(NULL, 0))); \ | |
| 1211 } \ | |
| 1212 } | |
| 1213 | |
| 1214 DATA_VIEW_SETTER(Uint8, uint8_t) | |
| 1215 DATA_VIEW_SETTER(Int8, int8_t) | |
| 1216 DATA_VIEW_SETTER(Uint16, uint16_t) | |
| 1217 DATA_VIEW_SETTER(Int16, int16_t) | |
| 1218 DATA_VIEW_SETTER(Uint32, uint32_t) | |
| 1219 DATA_VIEW_SETTER(Int32, int32_t) | |
| 1220 DATA_VIEW_SETTER(Float32, float) | |
| 1221 DATA_VIEW_SETTER(Float64, double) | |
| 1222 | |
| 1223 #undef DATA_VIEW_SETTER | |
| 1224 | |
| 1225 | |
| 1006 RUNTIME_FUNCTION(MaybeObject*, Runtime_SetInitialize) { | 1226 RUNTIME_FUNCTION(MaybeObject*, Runtime_SetInitialize) { |
| 1007 HandleScope scope(isolate); | 1227 HandleScope scope(isolate); |
| 1008 ASSERT(args.length() == 1); | 1228 ASSERT(args.length() == 1); |
| 1009 CONVERT_ARG_HANDLE_CHECKED(JSSet, holder, 0); | 1229 CONVERT_ARG_HANDLE_CHECKED(JSSet, holder, 0); |
| 1010 Handle<ObjectHashSet> table = isolate->factory()->NewObjectHashSet(0); | 1230 Handle<ObjectHashSet> table = isolate->factory()->NewObjectHashSet(0); |
| 1011 holder->set_table(*table); | 1231 holder->set_table(*table); |
| 1012 return *holder; | 1232 return *holder; |
| 1013 } | 1233 } |
| 1014 | 1234 |
| 1015 | 1235 |
| (...skipping 12619 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 13635 // Handle last resort GC and make sure to allow future allocations | 13855 // Handle last resort GC and make sure to allow future allocations |
| 13636 // to grow the heap without causing GCs (if possible). | 13856 // to grow the heap without causing GCs (if possible). |
| 13637 isolate->counters()->gc_last_resort_from_js()->Increment(); | 13857 isolate->counters()->gc_last_resort_from_js()->Increment(); |
| 13638 isolate->heap()->CollectAllGarbage(Heap::kNoGCFlags, | 13858 isolate->heap()->CollectAllGarbage(Heap::kNoGCFlags, |
| 13639 "Runtime::PerformGC"); | 13859 "Runtime::PerformGC"); |
| 13640 } | 13860 } |
| 13641 } | 13861 } |
| 13642 | 13862 |
| 13643 | 13863 |
| 13644 } } // namespace v8::internal | 13864 } } // namespace v8::internal |
| OLD | NEW |