| 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 22 matching lines...) Expand all Loading... |
| 33 if (bytes_read < 0) { | 33 if (bytes_read < 0) { |
| 34 return false; | 34 return false; |
| 35 } | 35 } |
| 36 remaining -= bytes_read; // Reduce the number of remaining bytes. | 36 remaining -= bytes_read; // Reduce the number of remaining bytes. |
| 37 current_buffer += bytes_read; // Move the buffer forward. | 37 current_buffer += bytes_read; // Move the buffer forward. |
| 38 } | 38 } |
| 39 return true; | 39 return true; |
| 40 } | 40 } |
| 41 | 41 |
| 42 | 42 |
| 43 static File* GetFileHandle(Dart_Handle fileobj) { | 43 void FUNCTION_NAME(File_Open)(Dart_NativeArguments args) { |
| 44 intptr_t value = | |
| 45 DartUtils::GetIntegerInstanceField(fileobj, DartUtils::kIdFieldName); | |
| 46 File* file = reinterpret_cast<File*>(value); | |
| 47 return file; | |
| 48 } | |
| 49 | |
| 50 | |
| 51 void FUNCTION_NAME(File_OpenFile)(Dart_NativeArguments args) { | |
| 52 Dart_EnterScope(); | 44 Dart_EnterScope(); |
| 53 Dart_Handle fileobj = Dart_GetNativeArgument(args, 0); | |
| 54 const char* filename = | 45 const char* filename = |
| 55 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1)); | 46 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); |
| 56 bool writable = DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 2)); | 47 bool writable = DartUtils::GetBooleanValue(Dart_GetNativeArgument(args, 1)); |
| 57 File* file = File::OpenFile(filename, writable); | 48 File* file = File::OpenFile(filename, writable); |
| 58 DartUtils::SetIntegerInstanceField(fileobj, | 49 Dart_SetReturnValue(args, Dart_NewInteger(reinterpret_cast<intptr_t>(file))); |
| 59 DartUtils::kIdFieldName, | |
| 60 reinterpret_cast<intptr_t>(file)); | |
| 61 Dart_SetReturnValue(args, Dart_NewBoolean(file != NULL)); | |
| 62 Dart_ExitScope(); | 50 Dart_ExitScope(); |
| 63 } | 51 } |
| 64 | 52 |
| 65 | 53 |
| 66 void FUNCTION_NAME(File_Exists)(Dart_NativeArguments args) { | 54 void FUNCTION_NAME(File_Exists)(Dart_NativeArguments args) { |
| 67 Dart_EnterScope(); | 55 Dart_EnterScope(); |
| 68 const char* filename = | 56 const char* filename = |
| 69 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1)); | 57 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 0)); |
| 70 bool exists = File::FileExists(filename); | 58 bool exists = File::FileExists(filename); |
| 71 Dart_SetReturnValue(args, Dart_NewBoolean(exists)); | 59 Dart_SetReturnValue(args, Dart_NewBoolean(exists)); |
| 72 Dart_ExitScope(); | 60 Dart_ExitScope(); |
| 73 } | 61 } |
| 74 | 62 |
| 75 | 63 |
| 76 void FUNCTION_NAME(File_Close)(Dart_NativeArguments args) { | 64 void FUNCTION_NAME(File_Close)(Dart_NativeArguments args) { |
| 77 Dart_EnterScope(); | 65 Dart_EnterScope(); |
| 78 intptr_t return_value = -1; | 66 intptr_t return_value = -1; |
| 79 Dart_Handle fileobj = Dart_GetNativeArgument(args, 0); | 67 intptr_t value = |
| 80 File* file = GetFileHandle(fileobj); | 68 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 0)); |
| 69 File* file = reinterpret_cast<File*>(value); |
| 81 if (file != NULL) { | 70 if (file != NULL) { |
| 82 DartUtils::SetIntegerInstanceField(fileobj, DartUtils::kIdFieldName, 0); | |
| 83 delete file; | 71 delete file; |
| 84 return_value = 0; | 72 return_value = 0; |
| 85 } | 73 } |
| 86 Dart_SetReturnValue(args, Dart_NewInteger(return_value)); | 74 Dart_SetReturnValue(args, Dart_NewInteger(return_value)); |
| 87 Dart_ExitScope(); | 75 Dart_ExitScope(); |
| 88 } | 76 } |
| 89 | 77 |
| 90 | 78 |
| 91 void FUNCTION_NAME(File_ReadByte)(Dart_NativeArguments args) { | 79 void FUNCTION_NAME(File_ReadByte)(Dart_NativeArguments args) { |
| 92 Dart_EnterScope(); | 80 Dart_EnterScope(); |
| 93 intptr_t return_value = -1; | 81 intptr_t return_value = -1; |
| 94 File* file = GetFileHandle(Dart_GetNativeArgument(args, 0)); | 82 intptr_t value = |
| 83 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 0)); |
| 84 File* file = reinterpret_cast<File*>(value); |
| 95 if (file != NULL) { | 85 if (file != NULL) { |
| 96 uint8_t buffer; | 86 uint8_t buffer; |
| 97 int bytes_read = file->Read(reinterpret_cast<void*>(&buffer), 1); | 87 int bytes_read = file->Read(reinterpret_cast<void*>(&buffer), 1); |
| 98 if (bytes_read >= 0) { | 88 if (bytes_read >= 0) { |
| 99 return_value = static_cast<intptr_t>(buffer); | 89 return_value = static_cast<intptr_t>(buffer); |
| 100 } | 90 } |
| 101 } | 91 } |
| 102 Dart_SetReturnValue(args, Dart_NewInteger(return_value)); | 92 Dart_SetReturnValue(args, Dart_NewInteger(return_value)); |
| 103 Dart_ExitScope(); | 93 Dart_ExitScope(); |
| 104 } | 94 } |
| 105 | 95 |
| 106 | 96 |
| 107 void FUNCTION_NAME(File_WriteByte)(Dart_NativeArguments args) { | 97 void FUNCTION_NAME(File_WriteByte)(Dart_NativeArguments args) { |
| 108 Dart_EnterScope(); | 98 Dart_EnterScope(); |
| 109 intptr_t return_value = -1; | 99 intptr_t return_value = -1; |
| 110 File* file = GetFileHandle(Dart_GetNativeArgument(args, 0)); | 100 intptr_t value = |
| 101 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 0)); |
| 102 File* file = reinterpret_cast<File*>(value); |
| 111 if (file != NULL) { | 103 if (file != NULL) { |
| 112 int64_t value = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 1)); | 104 int64_t value = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 1)); |
| 113 uint8_t buffer = static_cast<uint8_t>(value & 0xff); | 105 uint8_t buffer = static_cast<uint8_t>(value & 0xff); |
| 114 int bytes_written = file->Write(reinterpret_cast<void*>(&buffer), 1); | 106 int bytes_written = file->Write(reinterpret_cast<void*>(&buffer), 1); |
| 115 if (bytes_written >= 0) { | 107 if (bytes_written >= 0) { |
| 116 return_value = bytes_written; | 108 return_value = bytes_written; |
| 117 } | 109 } |
| 118 } | 110 } |
| 119 Dart_SetReturnValue(args, Dart_NewInteger(return_value)); | 111 Dart_SetReturnValue(args, Dart_NewInteger(return_value)); |
| 120 Dart_ExitScope(); | 112 Dart_ExitScope(); |
| 121 } | 113 } |
| 122 | 114 |
| 123 | 115 |
| 124 void FUNCTION_NAME(File_WriteString)(Dart_NativeArguments args) { | 116 void FUNCTION_NAME(File_WriteString)(Dart_NativeArguments args) { |
| 125 Dart_EnterScope(); | 117 Dart_EnterScope(); |
| 126 intptr_t return_value = -1; | 118 intptr_t return_value = -1; |
| 127 File* file = GetFileHandle(Dart_GetNativeArgument(args, 0)); | 119 intptr_t value = |
| 120 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 0)); |
| 121 File* file = reinterpret_cast<File*>(value); |
| 128 if (file != NULL) { | 122 if (file != NULL) { |
| 129 const char* str = | 123 const char* str = |
| 130 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1)); | 124 DartUtils::GetStringValue(Dart_GetNativeArgument(args, 1)); |
| 131 int bytes_written = file->Write(reinterpret_cast<const void*>(str), | 125 int bytes_written = file->Write(reinterpret_cast<const void*>(str), |
| 132 strlen(str)); | 126 strlen(str)); |
| 133 if (bytes_written >= 0) { | 127 if (bytes_written >= 0) { |
| 134 return_value = bytes_written; | 128 return_value = bytes_written; |
| 135 } | 129 } |
| 136 } | 130 } |
| 137 Dart_SetReturnValue(args, Dart_NewInteger(return_value)); | 131 Dart_SetReturnValue(args, Dart_NewInteger(return_value)); |
| 138 Dart_ExitScope(); | 132 Dart_ExitScope(); |
| 139 } | 133 } |
| 140 | 134 |
| 141 | 135 |
| 142 void FUNCTION_NAME(File_ReadList)(Dart_NativeArguments args) { | 136 void FUNCTION_NAME(File_ReadList)(Dart_NativeArguments args) { |
| 143 Dart_EnterScope(); | 137 Dart_EnterScope(); |
| 144 intptr_t return_value = -1; | 138 intptr_t return_value = -1; |
| 145 File* file = GetFileHandle(Dart_GetNativeArgument(args, 0)); | 139 intptr_t value = |
| 140 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 0)); |
| 141 File* file = reinterpret_cast<File*>(value); |
| 146 if (file != NULL) { | 142 if (file != NULL) { |
| 147 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1); | 143 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1); |
| 148 ASSERT(Dart_IsArray(buffer_obj)); | 144 ASSERT(Dart_IsArray(buffer_obj)); |
| 149 int64_t offset = | 145 int64_t offset = |
| 150 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 2)); | 146 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 2)); |
| 151 int64_t length = | 147 int64_t length = |
| 152 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3)); | 148 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3)); |
| 153 intptr_t array_len = 0; | 149 intptr_t array_len = 0; |
| 154 Dart_Handle result = Dart_GetLength(buffer_obj, &array_len); | 150 Dart_Handle result = Dart_GetLength(buffer_obj, &array_len); |
| 155 ASSERT(Dart_IsValid(result)); | 151 ASSERT(Dart_IsValid(result)); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 169 delete[] buffer; | 165 delete[] buffer; |
| 170 } | 166 } |
| 171 Dart_SetReturnValue(args, Dart_NewInteger(return_value)); | 167 Dart_SetReturnValue(args, Dart_NewInteger(return_value)); |
| 172 Dart_ExitScope(); | 168 Dart_ExitScope(); |
| 173 } | 169 } |
| 174 | 170 |
| 175 | 171 |
| 176 void FUNCTION_NAME(File_WriteList)(Dart_NativeArguments args) { | 172 void FUNCTION_NAME(File_WriteList)(Dart_NativeArguments args) { |
| 177 Dart_EnterScope(); | 173 Dart_EnterScope(); |
| 178 intptr_t return_value = -1; | 174 intptr_t return_value = -1; |
| 179 File* file = GetFileHandle(Dart_GetNativeArgument(args, 0)); | 175 intptr_t value = |
| 176 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 0)); |
| 177 File* file = reinterpret_cast<File*>(value); |
| 180 if (file != NULL) { | 178 if (file != NULL) { |
| 181 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1); | 179 Dart_Handle buffer_obj = Dart_GetNativeArgument(args, 1); |
| 182 ASSERT(Dart_IsArray(buffer_obj)); | 180 ASSERT(Dart_IsArray(buffer_obj)); |
| 183 int64_t offset = | 181 int64_t offset = |
| 184 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 2)); | 182 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 2)); |
| 185 int64_t length = | 183 int64_t length = |
| 186 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3)); | 184 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 3)); |
| 187 intptr_t buffer_len = 0; | 185 intptr_t buffer_len = 0; |
| 188 Dart_Handle result = Dart_GetLength(buffer_obj, &buffer_len); | 186 Dart_Handle result = Dart_GetLength(buffer_obj, &buffer_len); |
| 189 ASSERT(Dart_IsValid(result)); | 187 ASSERT(Dart_IsValid(result)); |
| 190 ASSERT((offset + length) <= buffer_len); | 188 ASSERT((offset + length) <= buffer_len); |
| 191 uint8_t* buffer = new uint8_t[length]; | 189 uint8_t* buffer = new uint8_t[length]; |
| 192 result = Dart_ArrayGet(buffer_obj, offset, buffer, length); | 190 result = Dart_ArrayGet(buffer_obj, offset, buffer, length); |
| 193 ASSERT(Dart_IsValid(result)); | 191 ASSERT(Dart_IsValid(result)); |
| 194 int total_bytes_written = | 192 int total_bytes_written = |
| 195 file->Write(reinterpret_cast<void*>(buffer), length); | 193 file->Write(reinterpret_cast<void*>(buffer), length); |
| 196 if (total_bytes_written >= 0) { | 194 if (total_bytes_written >= 0) { |
| 197 return_value = total_bytes_written; | 195 return_value = total_bytes_written; |
| 198 } | 196 } |
| 199 delete[] buffer; | 197 delete[] buffer; |
| 200 } | 198 } |
| 201 Dart_SetReturnValue(args, Dart_NewInteger(return_value)); | 199 Dart_SetReturnValue(args, Dart_NewInteger(return_value)); |
| 202 Dart_ExitScope(); | 200 Dart_ExitScope(); |
| 203 } | 201 } |
| 204 | 202 |
| 205 | 203 |
| 206 void FUNCTION_NAME(File_Position)(Dart_NativeArguments args) { | 204 void FUNCTION_NAME(File_Position)(Dart_NativeArguments args) { |
| 207 Dart_EnterScope(); | 205 Dart_EnterScope(); |
| 208 intptr_t return_value = -1; | 206 intptr_t return_value = -1; |
| 209 File* file = GetFileHandle(Dart_GetNativeArgument(args, 0)); | 207 intptr_t value = |
| 208 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 0)); |
| 209 File* file = reinterpret_cast<File*>(value); |
| 210 if (file != NULL) { | 210 if (file != NULL) { |
| 211 return_value = file->Position(); | 211 return_value = file->Position(); |
| 212 } | 212 } |
| 213 Dart_SetReturnValue(args, Dart_NewInteger(return_value)); | 213 Dart_SetReturnValue(args, Dart_NewInteger(return_value)); |
| 214 Dart_ExitScope(); | 214 Dart_ExitScope(); |
| 215 } | 215 } |
| 216 | 216 |
| 217 | 217 |
| 218 void FUNCTION_NAME(File_Length)(Dart_NativeArguments args) { | 218 void FUNCTION_NAME(File_Length)(Dart_NativeArguments args) { |
| 219 Dart_EnterScope(); | 219 Dart_EnterScope(); |
| 220 intptr_t return_value = -1; | 220 intptr_t return_value = -1; |
| 221 File* file = GetFileHandle(Dart_GetNativeArgument(args, 0)); | 221 intptr_t value = |
| 222 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 0)); |
| 223 File* file = reinterpret_cast<File*>(value); |
| 222 if (file != NULL) { | 224 if (file != NULL) { |
| 223 return_value = file->Length(); | 225 return_value = file->Length(); |
| 224 } | 226 } |
| 225 Dart_SetReturnValue(args, Dart_NewInteger(return_value)); | 227 Dart_SetReturnValue(args, Dart_NewInteger(return_value)); |
| 226 Dart_ExitScope(); | 228 Dart_ExitScope(); |
| 227 } | 229 } |
| 228 | 230 |
| 229 | 231 |
| 230 void FUNCTION_NAME(File_Flush)(Dart_NativeArguments args) { | 232 void FUNCTION_NAME(File_Flush)(Dart_NativeArguments args) { |
| 231 Dart_EnterScope(); | 233 Dart_EnterScope(); |
| 232 intptr_t return_value = -1; | 234 intptr_t return_value = -1; |
| 233 File* file = GetFileHandle(Dart_GetNativeArgument(args, 0)); | 235 intptr_t value = |
| 236 DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 0)); |
| 237 File* file = reinterpret_cast<File*>(value); |
| 234 if (file != NULL) { | 238 if (file != NULL) { |
| 235 file->Flush(); | 239 file->Flush(); |
| 236 return_value = 0; | 240 return_value = 0; |
| 237 } | 241 } |
| 238 Dart_SetReturnValue(args, Dart_NewInteger(return_value)); | 242 Dart_SetReturnValue(args, Dart_NewInteger(return_value)); |
| 239 Dart_ExitScope(); | 243 Dart_ExitScope(); |
| 240 } | 244 } |
| OLD | NEW |