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 1079 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1090 chars[size] = '\0'; | 1090 chars[size] = '\0'; |
1091 for (int i = 0; i < size;) { | 1091 for (int i = 0; i < size;) { |
1092 int read = static_cast<int>(fread(&chars[i], 1, size - i, file)); | 1092 int read = static_cast<int>(fread(&chars[i], 1, size - i, file)); |
1093 i += read; | 1093 i += read; |
1094 } | 1094 } |
1095 fclose(file); | 1095 fclose(file); |
1096 *size_out = size; | 1096 *size_out = size; |
1097 return chars; | 1097 return chars; |
1098 } | 1098 } |
1099 | 1099 |
1100 static void ReadBufferWeakCallback(v8::Isolate* isolate, | 1100 |
1101 Persistent<ArrayBuffer>* array_buffer, | 1101 struct DataAndPersistent { |
1102 uint8_t* data) { | 1102 uint8_t* data; |
1103 size_t byte_length = | 1103 Persistent<ArrayBuffer> handle; |
1104 Local<ArrayBuffer>::New(isolate, *array_buffer)->ByteLength(); | 1104 }; |
1105 isolate->AdjustAmountOfExternalAllocatedMemory( | 1105 |
| 1106 |
| 1107 static void ReadBufferWeakCallback( |
| 1108 const v8::WeakCallbackData<ArrayBuffer, DataAndPersistent>& data) { |
| 1109 size_t byte_length = data.GetValue()->ByteLength(); |
| 1110 data.GetIsolate()->AdjustAmountOfExternalAllocatedMemory( |
1106 -static_cast<intptr_t>(byte_length)); | 1111 -static_cast<intptr_t>(byte_length)); |
1107 | 1112 |
1108 delete[] data; | 1113 delete[] data.GetParameter()->data; |
1109 array_buffer->Reset(); | 1114 data.GetParameter()->handle.Reset(); |
| 1115 delete data.GetParameter(); |
1110 } | 1116 } |
1111 | 1117 |
1112 | 1118 |
1113 void Shell::ReadBuffer(const v8::FunctionCallbackInfo<v8::Value>& args) { | 1119 void Shell::ReadBuffer(const v8::FunctionCallbackInfo<v8::Value>& args) { |
1114 ASSERT(sizeof(char) == sizeof(uint8_t)); // NOLINT | 1120 ASSERT(sizeof(char) == sizeof(uint8_t)); // NOLINT |
1115 String::Utf8Value filename(args[0]); | 1121 String::Utf8Value filename(args[0]); |
1116 int length; | 1122 int length; |
1117 if (*filename == NULL) { | 1123 if (*filename == NULL) { |
1118 Throw(args.GetIsolate(), "Error loading file"); | 1124 Throw(args.GetIsolate(), "Error loading file"); |
1119 return; | 1125 return; |
1120 } | 1126 } |
1121 | 1127 |
1122 Isolate* isolate = args.GetIsolate(); | 1128 Isolate* isolate = args.GetIsolate(); |
1123 uint8_t* data = reinterpret_cast<uint8_t*>( | 1129 DataAndPersistent* data = new DataAndPersistent; |
| 1130 data->data = reinterpret_cast<uint8_t*>( |
1124 ReadChars(args.GetIsolate(), *filename, &length)); | 1131 ReadChars(args.GetIsolate(), *filename, &length)); |
1125 if (data == NULL) { | 1132 if (data->data == NULL) { |
| 1133 delete data; |
1126 Throw(args.GetIsolate(), "Error reading file"); | 1134 Throw(args.GetIsolate(), "Error reading file"); |
1127 return; | 1135 return; |
1128 } | 1136 } |
1129 Handle<v8::ArrayBuffer> buffer = ArrayBuffer::New(isolate, data, length); | 1137 Handle<v8::ArrayBuffer> buffer = ArrayBuffer::New(isolate, data, length); |
1130 v8::Persistent<v8::ArrayBuffer> weak_handle(isolate, buffer); | 1138 data->handle.Reset(isolate, buffer); |
1131 weak_handle.MakeWeak(data, ReadBufferWeakCallback); | 1139 data->handle.SetWeak(data, ReadBufferWeakCallback); |
1132 weak_handle.MarkIndependent(); | 1140 data->handle.MarkIndependent(); |
1133 isolate->AdjustAmountOfExternalAllocatedMemory(length); | 1141 isolate->AdjustAmountOfExternalAllocatedMemory(length); |
1134 | 1142 |
1135 args.GetReturnValue().Set(buffer); | 1143 args.GetReturnValue().Set(buffer); |
1136 } | 1144 } |
1137 | 1145 |
1138 | 1146 |
1139 #ifndef V8_SHARED | 1147 #ifndef V8_SHARED |
1140 static char* ReadToken(char* data, char token) { | 1148 static char* ReadToken(char* data, char token) { |
1141 char* next = i::OS::StrChr(data, token); | 1149 char* next = i::OS::StrChr(data, token); |
1142 if (next != NULL) { | 1150 if (next != NULL) { |
(...skipping 612 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1755 } | 1763 } |
1756 | 1764 |
1757 } // namespace v8 | 1765 } // namespace v8 |
1758 | 1766 |
1759 | 1767 |
1760 #ifndef GOOGLE3 | 1768 #ifndef GOOGLE3 |
1761 int main(int argc, char* argv[]) { | 1769 int main(int argc, char* argv[]) { |
1762 return v8::Shell::Main(argc, argv); | 1770 return v8::Shell::Main(argc, argv); |
1763 } | 1771 } |
1764 #endif | 1772 #endif |
OLD | NEW |