| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include "bin/file.h" | 5 #include "bin/file.h" |
| 6 | 6 |
| 7 #include "bin/builtin.h" | 7 #include "bin/builtin.h" |
| 8 #include "bin/dartutils.h" | 8 #include "bin/dartutils.h" |
| 9 | 9 |
| 10 #include "include/dart_api.h" | 10 #include "include/dart_api.h" |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 return false; | 37 return false; |
| 38 } | 38 } |
| 39 remaining -= bytes_read; // Reduce the number of remaining bytes. | 39 remaining -= bytes_read; // Reduce the number of remaining bytes. |
| 40 current_buffer += bytes_read; // Move the buffer forward. | 40 current_buffer += bytes_read; // Move the buffer forward. |
| 41 } | 41 } |
| 42 return true; | 42 return true; |
| 43 } | 43 } |
| 44 | 44 |
| 45 | 45 |
| 46 static File* GetFileHandle(Dart_Handle fileobj) { | 46 static File* GetFileHandle(Dart_Handle fileobj) { |
| 47 Dart_Result result = Dart_GetNativeInstanceField(fileobj, kFileFieldIndex); | 47 intptr_t value = 0; |
| 48 ASSERT(Dart_IsValidResult(result)); | 48 Dart_Handle result = Dart_GetNativeInstanceField(fileobj, kFileFieldIndex, |
| 49 File* file = reinterpret_cast<File*>(Dart_GetResultAsCIntptr(result)); | 49 &value); |
| 50 ASSERT(Dart_IsValid(result)); |
| 51 File* file = reinterpret_cast<File*>(value); |
| 50 return file; | 52 return file; |
| 51 } | 53 } |
| 52 | 54 |
| 53 | 55 |
| 54 void FUNCTION_NAME(File_OpenFile)(Dart_NativeArguments args) { | 56 void FUNCTION_NAME(File_OpenFile)(Dart_NativeArguments args) { |
| 55 Dart_EnterScope(); | 57 Dart_EnterScope(); |
| 56 Dart_Handle fileobj = Dart_GetNativeArgument(args, 0); | 58 Dart_Handle fileobj = Dart_GetNativeArgument(args, 0); |
| 57 const char* filename = | 59 const char* filename = |
| 58 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1)); | 60 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1)); |
| 59 bool writable = DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 2)); | 61 bool writable = DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 2)); |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 148 Dart_EnterScope(); | 150 Dart_EnterScope(); |
| 149 intptr_t return_value = -1; | 151 intptr_t return_value = -1; |
| 150 File* file = GetFileHandle(Dart_GetNativeArgument(args, 0)); | 152 File* file = GetFileHandle(Dart_GetNativeArgument(args, 0)); |
| 151 if (file != NULL) { | 153 if (file != NULL) { |
| 152 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1); | 154 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1); |
| 153 ASSERT(Dart_IsArray(buffer_obj)); | 155 ASSERT(Dart_IsArray(buffer_obj)); |
| 154 int64_t offset = | 156 int64_t offset = |
| 155 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 2)); | 157 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 2)); |
| 156 int64_t length = | 158 int64_t length = |
| 157 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3)); | 159 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3)); |
| 158 Dart_Result result = Dart_GetLength(buffer_obj); | 160 intptr_t array_len = 0; |
| 159 ASSERT(Dart_IsValidResult(result)); | 161 Dart_Handle result = Dart_GetLength(buffer_obj, &array_len); |
| 160 ASSERT((offset + length) <= Dart_GetResultAsCIntptr(result)); | 162 ASSERT(Dart_IsValid(result)); |
| 163 ASSERT((offset + length) <= array_len); |
| 161 uint8_t* buffer = new uint8_t[length]; | 164 uint8_t* buffer = new uint8_t[length]; |
| 162 int total_bytes_read = | 165 int total_bytes_read = |
| 163 file->Read(reinterpret_cast<void*>(buffer), length); | 166 file->Read(reinterpret_cast<void*>(buffer), length); |
| 164 /* | 167 /* |
| 165 * Reading 0 indicates end of file. | 168 * Reading 0 indicates end of file. |
| 166 */ | 169 */ |
| 167 if (total_bytes_read >= 0) { | 170 if (total_bytes_read >= 0) { |
| 168 result = | 171 result = |
| 169 Dart_ArraySet(buffer_obj, offset, buffer, total_bytes_read); | 172 Dart_ArraySet(buffer_obj, offset, buffer, total_bytes_read); |
| 170 ASSERT(Dart_IsValidResult(result)); | 173 ASSERT(Dart_IsValid(result)); |
| 171 return_value = total_bytes_read; | 174 return_value = total_bytes_read; |
| 172 } | 175 } |
| 173 delete[] buffer; | 176 delete[] buffer; |
| 174 } | 177 } |
| 175 Dart_SetReturnValue(args, Dart_NewInteger(return_value)); | 178 Dart_SetReturnValue(args, Dart_NewInteger(return_value)); |
| 176 Dart_ExitScope(); | 179 Dart_ExitScope(); |
| 177 } | 180 } |
| 178 | 181 |
| 179 | 182 |
| 180 void FUNCTION_NAME(File_WriteList)(Dart_NativeArguments args) { | 183 void FUNCTION_NAME(File_WriteList)(Dart_NativeArguments args) { |
| 181 Dart_EnterScope(); | 184 Dart_EnterScope(); |
| 182 intptr_t return_value = -1; | 185 intptr_t return_value = -1; |
| 183 File* file = GetFileHandle(Dart_GetNativeArgument(args, 0)); | 186 File* file = GetFileHandle(Dart_GetNativeArgument(args, 0)); |
| 184 if (file != NULL) { | 187 if (file != NULL) { |
| 185 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1); | 188 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1); |
| 186 ASSERT(Dart_IsArray(buffer_obj)); | 189 ASSERT(Dart_IsArray(buffer_obj)); |
| 187 int64_t offset = | 190 int64_t offset = |
| 188 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 2)); | 191 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 2)); |
| 189 int64_t length = | 192 int64_t length = |
| 190 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3)); | 193 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3)); |
| 191 Dart_Result result = Dart_GetLength(buffer_obj); | 194 intptr_t buffer_len = 0; |
| 192 ASSERT(Dart_IsValidResult(result)); | 195 Dart_Handle result = Dart_GetLength(buffer_obj, &buffer_len); |
| 193 ASSERT((offset + length) <= Dart_GetResultAsCIntptr(result)); | 196 ASSERT(Dart_IsValid(result)); |
| 197 ASSERT((offset + length) <= buffer_len); |
| 194 uint8_t* buffer = new uint8_t[length]; | 198 uint8_t* buffer = new uint8_t[length]; |
| 195 result = Dart_ArrayGet(buffer_obj, offset, buffer, length); | 199 result = Dart_ArrayGet(buffer_obj, offset, buffer, length); |
| 196 ASSERT(Dart_IsValidResult(result)); | 200 ASSERT(Dart_IsValid(result)); |
| 197 int total_bytes_written = | 201 int total_bytes_written = |
| 198 file->Write(reinterpret_cast<void*>(buffer), length); | 202 file->Write(reinterpret_cast<void*>(buffer), length); |
| 199 if (total_bytes_written >= 0) { | 203 if (total_bytes_written >= 0) { |
| 200 return_value = total_bytes_written; | 204 return_value = total_bytes_written; |
| 201 } | 205 } |
| 202 delete[] buffer; | 206 delete[] buffer; |
| 203 } | 207 } |
| 204 Dart_SetReturnValue(args, Dart_NewInteger(return_value)); | 208 Dart_SetReturnValue(args, Dart_NewInteger(return_value)); |
| 205 Dart_ExitScope(); | 209 Dart_ExitScope(); |
| 206 } | 210 } |
| (...skipping 27 matching lines...) Expand all Loading... |
| 234 Dart_EnterScope(); | 238 Dart_EnterScope(); |
| 235 intptr_t return_value = -1; | 239 intptr_t return_value = -1; |
| 236 File* file = GetFileHandle(Dart_GetNativeArgument(args, 0)); | 240 File* file = GetFileHandle(Dart_GetNativeArgument(args, 0)); |
| 237 if (file != NULL) { | 241 if (file != NULL) { |
| 238 file->Flush(); | 242 file->Flush(); |
| 239 return_value = 0; | 243 return_value = 0; |
| 240 } | 244 } |
| 241 Dart_SetReturnValue(args, Dart_NewInteger(return_value)); | 245 Dart_SetReturnValue(args, Dart_NewInteger(return_value)); |
| 242 Dart_ExitScope(); | 246 Dart_ExitScope(); |
| 243 } | 247 } |
| OLD | NEW |