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

Side by Side Diff: src/runtime.cc

Issue 17153011: DataView implementation. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Fixes Created 7 years, 6 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
« no previous file with comments | « src/runtime.h ('k') | src/typedarray.js » ('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 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
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
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
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(JSDataView, data_view, 0);
1035 return data_view->buffer();
1036 }
1037
1038
1039 RUNTIME_FUNCTION(MaybeObject*, Runtime_DataViewGetByteOffset) {
1040 HandleScope scope(isolate);
1041 ASSERT(args.length() == 1);
1042 CONVERT_ARG_HANDLE_CHECKED(JSDataView, data_view, 0);
1043 return data_view->byte_offset();
1044 }
1045
1046
1047 RUNTIME_FUNCTION(MaybeObject*, Runtime_DataViewGetByteLength) {
1048 HandleScope scope(isolate);
1049 ASSERT(args.length() == 1);
1050 CONVERT_ARG_HANDLE_CHECKED(JSDataView, data_view, 0);
1051 return data_view->byte_length();
1052 }
1053
1054
1055 inline static bool NeedToFlipBytes(bool is_little_endian) {
1056 #ifdef V8_TARGET_LITTLE_ENDIAN
1057 return !is_little_endian;
1058 #else
1059 return is_little_endian;
1060 #endif
1061 }
1062
1063
1064 template<int n>
1065 inline void CopyBytes(uint8_t* target, uint8_t* source) {
1066 for (int i = 0; i < n; i++) {
1067 *(target++) = *(source++);
1068 }
1069 }
1070
1071
1072 template<int n>
1073 inline void FlipBytes(uint8_t* target, uint8_t* source) {
1074 source = source + (n-1);
1075 for (int i = 0; i < n; i++) {
1076 *(target++) = *(source--);
1077 }
1078 }
1079
1080
1081 template<typename T>
1082 inline static bool DataViewGetValue(
1083 Isolate* isolate,
1084 Handle<JSDataView> data_view,
1085 Handle<Object> byte_offset_obj,
1086 bool is_little_endian,
1087 T* result) {
1088 size_t byte_offset = NumberToSize(isolate, *byte_offset_obj);
1089 Handle<JSArrayBuffer> buffer(JSArrayBuffer::cast(data_view->buffer()));
1090
1091 size_t data_view_byte_offset =
1092 NumberToSize(isolate, data_view->byte_offset());
1093 size_t data_view_byte_length =
1094 NumberToSize(isolate, data_view->byte_length());
1095 if (byte_offset + sizeof(T) > data_view_byte_length ||
1096 byte_offset + sizeof(T) < byte_offset) { // overflow
1097 return false;
1098 }
1099
1100 union Value {
1101 T data;
1102 uint8_t bytes[sizeof(T)];
1103 };
1104
1105 Value value;
1106 size_t buffer_offset = data_view_byte_offset + byte_offset;
1107 ASSERT(
1108 NumberToSize(isolate, buffer->byte_length())
1109 >= buffer_offset + sizeof(T));
1110 uint8_t* source =
1111 static_cast<uint8_t*>(buffer->backing_store()) + buffer_offset;
1112 if (NeedToFlipBytes(is_little_endian)) {
1113 FlipBytes<sizeof(T)>(value.bytes, source);
1114 } else {
1115 CopyBytes<sizeof(T)>(value.bytes, source);
1116 }
1117 *result = value.data;
1118 return true;
1119 }
1120
1121
1122 template<typename T>
1123 static bool DataViewSetValue(
1124 Isolate* isolate,
1125 Handle<JSDataView> data_view,
1126 Handle<Object> byte_offset_obj,
1127 bool is_little_endian,
1128 T data) {
1129 size_t byte_offset = NumberToSize(isolate, *byte_offset_obj);
1130 Handle<JSArrayBuffer> buffer(JSArrayBuffer::cast(data_view->buffer()));
1131
1132 size_t data_view_byte_offset =
1133 NumberToSize(isolate, data_view->byte_offset());
1134 size_t data_view_byte_length =
1135 NumberToSize(isolate, data_view->byte_length());
1136 if (byte_offset + sizeof(T) > data_view_byte_length ||
1137 byte_offset + sizeof(T) < byte_offset) { // overflow
1138 return false;
1139 }
1140
1141 union Value {
1142 T data;
1143 uint8_t bytes[sizeof(T)];
1144 };
1145
1146 Value value;
1147 value.data = data;
1148 size_t buffer_offset = data_view_byte_offset + byte_offset;
1149 ASSERT(
1150 NumberToSize(isolate, buffer->byte_length())
1151 >= buffer_offset + sizeof(T));
1152 uint8_t* target =
1153 static_cast<uint8_t*>(buffer->backing_store()) + buffer_offset;
1154 if (NeedToFlipBytes(is_little_endian)) {
1155 FlipBytes<sizeof(T)>(target, value.bytes);
1156 } else {
1157 CopyBytes<sizeof(T)>(target, value.bytes);
1158 }
1159 return true;
1160 }
1161
1162
1163 #define DATA_VIEW_GETTER(TypeName, Type, Converter) \
1164 RUNTIME_FUNCTION(MaybeObject*, Runtime_DataViewGet##TypeName) { \
1165 HandleScope scope(isolate); \
1166 ASSERT(args.length() == 3); \
1167 CONVERT_ARG_HANDLE_CHECKED(JSDataView, holder, 0); \
1168 CONVERT_ARG_HANDLE_CHECKED(Object, offset, 1); \
1169 CONVERT_BOOLEAN_ARG_CHECKED(is_little_endian, 2); \
1170 Type result; \
1171 if (DataViewGetValue( \
1172 isolate, holder, offset, is_little_endian, &result)) { \
1173 return isolate->heap()->Converter(result); \
1174 } else { \
1175 return isolate->Throw(*isolate->factory()->NewRangeError( \
1176 "invalid_data_view_accessor_offset", \
1177 HandleVector<Object>(NULL, 0))); \
1178 } \
1179 }
1180
1181 DATA_VIEW_GETTER(Uint8, uint8_t, NumberFromUint32)
1182 DATA_VIEW_GETTER(Int8, int8_t, NumberFromInt32)
1183 DATA_VIEW_GETTER(Uint16, uint16_t, NumberFromUint32)
1184 DATA_VIEW_GETTER(Int16, int16_t, NumberFromInt32)
1185 DATA_VIEW_GETTER(Uint32, uint32_t, NumberFromUint32)
1186 DATA_VIEW_GETTER(Int32, int32_t, NumberFromInt32)
1187 DATA_VIEW_GETTER(Float32, float, NumberFromDouble)
1188 DATA_VIEW_GETTER(Float64, double, NumberFromDouble)
1189
1190 #undef DATA_VIEW_GETTER
1191
1192 #define DATA_VIEW_SETTER(TypeName, Type) \
1193 RUNTIME_FUNCTION(MaybeObject*, Runtime_DataViewSet##TypeName) { \
1194 HandleScope scope(isolate); \
1195 ASSERT(args.length() == 4); \
1196 CONVERT_ARG_HANDLE_CHECKED(JSDataView, holder, 0); \
1197 CONVERT_ARG_HANDLE_CHECKED(Object, offset, 1); \
1198 CONVERT_ARG_HANDLE_CHECKED(Object, value, 2); \
1199 CONVERT_BOOLEAN_ARG_CHECKED(is_little_endian, 3); \
1200 Type v = static_cast<Type>(value->Number()); \
1201 if (DataViewSetValue( \
1202 isolate, holder, offset, is_little_endian, v)) { \
1203 return isolate->heap()->undefined_value(); \
1204 } else { \
1205 return isolate->Throw(*isolate->factory()->NewRangeError( \
1206 "invalid_data_view_accessor_offset", \
1207 HandleVector<Object>(NULL, 0))); \
1208 } \
1209 }
1210
1211 DATA_VIEW_SETTER(Uint8, uint8_t)
1212 DATA_VIEW_SETTER(Int8, int8_t)
1213 DATA_VIEW_SETTER(Uint16, uint16_t)
1214 DATA_VIEW_SETTER(Int16, int16_t)
1215 DATA_VIEW_SETTER(Uint32, uint32_t)
1216 DATA_VIEW_SETTER(Int32, int32_t)
1217 DATA_VIEW_SETTER(Float32, float)
1218 DATA_VIEW_SETTER(Float64, double)
1219
1220 #undef DATA_VIEW_SETTER
1221
1222
1006 RUNTIME_FUNCTION(MaybeObject*, Runtime_SetInitialize) { 1223 RUNTIME_FUNCTION(MaybeObject*, Runtime_SetInitialize) {
1007 HandleScope scope(isolate); 1224 HandleScope scope(isolate);
1008 ASSERT(args.length() == 1); 1225 ASSERT(args.length() == 1);
1009 CONVERT_ARG_HANDLE_CHECKED(JSSet, holder, 0); 1226 CONVERT_ARG_HANDLE_CHECKED(JSSet, holder, 0);
1010 Handle<ObjectHashSet> table = isolate->factory()->NewObjectHashSet(0); 1227 Handle<ObjectHashSet> table = isolate->factory()->NewObjectHashSet(0);
1011 holder->set_table(*table); 1228 holder->set_table(*table);
1012 return *holder; 1229 return *holder;
1013 } 1230 }
1014 1231
1015 1232
(...skipping 12619 matching lines...) Expand 10 before | Expand all | Expand 10 after
13635 // Handle last resort GC and make sure to allow future allocations 13852 // Handle last resort GC and make sure to allow future allocations
13636 // to grow the heap without causing GCs (if possible). 13853 // to grow the heap without causing GCs (if possible).
13637 isolate->counters()->gc_last_resort_from_js()->Increment(); 13854 isolate->counters()->gc_last_resort_from_js()->Increment();
13638 isolate->heap()->CollectAllGarbage(Heap::kNoGCFlags, 13855 isolate->heap()->CollectAllGarbage(Heap::kNoGCFlags,
13639 "Runtime::PerformGC"); 13856 "Runtime::PerformGC");
13640 } 13857 }
13641 } 13858 }
13642 13859
13643 13860
13644 } } // namespace v8::internal 13861 } } // namespace v8::internal
OLDNEW
« no previous file with comments | « src/runtime.h ('k') | src/typedarray.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698