OLD | NEW |
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 | 5 |
6 // Defined when linking against shared lib on Windows. | 6 // Defined when linking against shared lib on Windows. |
7 #if defined(USING_V8_SHARED) && !defined(V8_SHARED) | 7 #if defined(USING_V8_SHARED) && !defined(V8_SHARED) |
8 #define V8_SHARED | 8 #define V8_SHARED |
9 #endif | 9 #endif |
10 | 10 |
(...skipping 1112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1123 i += read; | 1123 i += read; |
1124 } | 1124 } |
1125 fclose(file); | 1125 fclose(file); |
1126 *size_out = size; | 1126 *size_out = size; |
1127 return chars; | 1127 return chars; |
1128 } | 1128 } |
1129 | 1129 |
1130 | 1130 |
1131 struct DataAndPersistent { | 1131 struct DataAndPersistent { |
1132 uint8_t* data; | 1132 uint8_t* data; |
1133 Persistent<ArrayBuffer> handle; | 1133 int byte_length; |
| 1134 Global<ArrayBuffer> handle; |
1134 }; | 1135 }; |
1135 | 1136 |
1136 | 1137 |
1137 static void ReadBufferWeakCallback( | 1138 static void ReadBufferWeakCallback( |
1138 const v8::WeakCallbackData<ArrayBuffer, DataAndPersistent>& data) { | 1139 const v8::WeakCallbackInfo<DataAndPersistent>& data) { |
1139 size_t byte_length = data.GetValue()->ByteLength(); | 1140 int byte_length = data.GetParameter()->byte_length; |
1140 data.GetIsolate()->AdjustAmountOfExternalAllocatedMemory( | 1141 data.GetIsolate()->AdjustAmountOfExternalAllocatedMemory( |
1141 -static_cast<intptr_t>(byte_length)); | 1142 -static_cast<intptr_t>(byte_length)); |
1142 | 1143 |
1143 delete[] data.GetParameter()->data; | 1144 delete[] data.GetParameter()->data; |
1144 data.GetParameter()->handle.Reset(); | 1145 data.GetParameter()->handle.Reset(); |
1145 delete data.GetParameter(); | 1146 delete data.GetParameter(); |
1146 } | 1147 } |
1147 | 1148 |
1148 | 1149 |
1149 void Shell::ReadBuffer(const v8::FunctionCallbackInfo<v8::Value>& args) { | 1150 void Shell::ReadBuffer(const v8::FunctionCallbackInfo<v8::Value>& args) { |
1150 DCHECK(sizeof(char) == sizeof(uint8_t)); // NOLINT | 1151 DCHECK(sizeof(char) == sizeof(uint8_t)); // NOLINT |
1151 String::Utf8Value filename(args[0]); | 1152 String::Utf8Value filename(args[0]); |
1152 int length; | 1153 int length; |
1153 if (*filename == NULL) { | 1154 if (*filename == NULL) { |
1154 Throw(args.GetIsolate(), "Error loading file"); | 1155 Throw(args.GetIsolate(), "Error loading file"); |
1155 return; | 1156 return; |
1156 } | 1157 } |
1157 | 1158 |
1158 Isolate* isolate = args.GetIsolate(); | 1159 Isolate* isolate = args.GetIsolate(); |
1159 DataAndPersistent* data = new DataAndPersistent; | 1160 DataAndPersistent* data = new DataAndPersistent; |
1160 data->data = reinterpret_cast<uint8_t*>( | 1161 data->data = reinterpret_cast<uint8_t*>( |
1161 ReadChars(args.GetIsolate(), *filename, &length)); | 1162 ReadChars(args.GetIsolate(), *filename, &length)); |
1162 if (data->data == NULL) { | 1163 if (data->data == NULL) { |
1163 delete data; | 1164 delete data; |
1164 Throw(args.GetIsolate(), "Error reading file"); | 1165 Throw(args.GetIsolate(), "Error reading file"); |
1165 return; | 1166 return; |
1166 } | 1167 } |
| 1168 data->byte_length = length; |
1167 Handle<v8::ArrayBuffer> buffer = | 1169 Handle<v8::ArrayBuffer> buffer = |
1168 ArrayBuffer::New(isolate, data->data, length); | 1170 ArrayBuffer::New(isolate, data->data, length); |
1169 data->handle.Reset(isolate, buffer); | 1171 data->handle.Reset(isolate, buffer); |
1170 data->handle.SetWeak(data, ReadBufferWeakCallback); | 1172 data->handle.SetWeak(data, ReadBufferWeakCallback, |
| 1173 v8::WeakCallbackType::kParameter); |
1171 data->handle.MarkIndependent(); | 1174 data->handle.MarkIndependent(); |
1172 isolate->AdjustAmountOfExternalAllocatedMemory(length); | 1175 isolate->AdjustAmountOfExternalAllocatedMemory(length); |
1173 | 1176 |
1174 args.GetReturnValue().Set(buffer); | 1177 args.GetReturnValue().Set(buffer); |
1175 } | 1178 } |
1176 | 1179 |
1177 | 1180 |
1178 // Reads a file into a v8 string. | 1181 // Reads a file into a v8 string. |
1179 Handle<String> Shell::ReadFile(Isolate* isolate, const char* name) { | 1182 Handle<String> Shell::ReadFile(Isolate* isolate, const char* name) { |
1180 int size = 0; | 1183 int size = 0; |
(...skipping 544 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1725 } | 1728 } |
1726 | 1729 |
1727 } // namespace v8 | 1730 } // namespace v8 |
1728 | 1731 |
1729 | 1732 |
1730 #ifndef GOOGLE3 | 1733 #ifndef GOOGLE3 |
1731 int main(int argc, char* argv[]) { | 1734 int main(int argc, char* argv[]) { |
1732 return v8::Shell::Main(argc, argv); | 1735 return v8::Shell::Main(argc, argv); |
1733 } | 1736 } |
1734 #endif | 1737 #endif |
OLD | NEW |