| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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/dartutils.h" | 5 #include "bin/dartutils.h" |
| 6 #include "bin/io_buffer.h" | 6 #include "bin/io_buffer.h" |
| 7 #include "bin/platform.h" | 7 #include "bin/platform.h" |
| 8 #include "bin/process.h" | 8 #include "bin/process.h" |
| 9 #include "bin/socket.h" | 9 #include "bin/socket.h" |
| 10 #include "bin/utils.h" | 10 #include "bin/utils.h" |
| (...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 284 intptr_t pid) { | 284 intptr_t pid) { |
| 285 return Dart_SetNativeInstanceField(process, kProcessIdNativeField, pid); | 285 return Dart_SetNativeInstanceField(process, kProcessIdNativeField, pid); |
| 286 } | 286 } |
| 287 | 287 |
| 288 | 288 |
| 289 void FUNCTION_NAME(SystemEncodingToString)(Dart_NativeArguments args) { | 289 void FUNCTION_NAME(SystemEncodingToString)(Dart_NativeArguments args) { |
| 290 Dart_Handle bytes = Dart_GetNativeArgument(args, 0); | 290 Dart_Handle bytes = Dart_GetNativeArgument(args, 0); |
| 291 intptr_t bytes_length = 0; | 291 intptr_t bytes_length = 0; |
| 292 Dart_Handle result = Dart_ListLength(bytes, &bytes_length); | 292 Dart_Handle result = Dart_ListLength(bytes, &bytes_length); |
| 293 if (Dart_IsError(result)) Dart_PropagateError(result); | 293 if (Dart_IsError(result)) Dart_PropagateError(result); |
| 294 uint8_t* buffer = new uint8_t[bytes_length + 1]; | 294 uint8_t* buffer = |
| 295 reinterpret_cast<uint8_t*>(Dart_ScopeAllocate(bytes_length + 1)); |
| 295 result = Dart_ListGetAsBytes(bytes, 0, buffer, bytes_length); | 296 result = Dart_ListGetAsBytes(bytes, 0, buffer, bytes_length); |
| 296 buffer[bytes_length] = '\0'; | 297 buffer[bytes_length] = '\0'; |
| 297 if (Dart_IsError(result)) { | 298 if (Dart_IsError(result)) Dart_PropagateError(result); |
| 298 delete[] buffer; | 299 intptr_t len; |
| 299 Dart_PropagateError(result); | 300 char* str = |
| 301 StringUtils::ConsoleStringToUtf8( |
| 302 reinterpret_cast<char*>(buffer), |
| 303 bytes_length, |
| 304 &len); |
| 305 if (str == NULL) { |
| 306 Dart_ThrowException( |
| 307 DartUtils::NewInternalError("SystemEncodingToString failed")); |
| 300 } | 308 } |
| 301 char* str = | 309 result = |
| 302 StringUtils::ConsoleStringToUtf8(reinterpret_cast<char*>(buffer)); | 310 Dart_NewStringFromUTF8(reinterpret_cast<const uint8_t*>(str), len); |
| 303 Dart_SetReturnValue(args, DartUtils::NewString(str)); | 311 free(str); |
| 304 if (str != reinterpret_cast<char*>(buffer)) free(str); | 312 if (Dart_IsError(result)) Dart_PropagateError(result); |
| 313 Dart_SetReturnValue(args, result); |
| 305 } | 314 } |
| 306 | 315 |
| 307 | 316 |
| 308 void FUNCTION_NAME(StringToSystemEncoding)(Dart_NativeArguments args) { | 317 void FUNCTION_NAME(StringToSystemEncoding)(Dart_NativeArguments args) { |
| 309 Dart_Handle str = Dart_GetNativeArgument(args, 0); | 318 Dart_Handle str = Dart_GetNativeArgument(args, 0); |
| 310 const char* utf8 = DartUtils::GetStringValue(str); | 319 char* utf8; |
| 311 const char* system_string = StringUtils::Utf8ToConsoleString(utf8); | 320 intptr_t utf8_len; |
| 312 int external_length = strlen(system_string); | 321 Dart_Handle result = Dart_StringToUTF8( |
| 322 str, reinterpret_cast<uint8_t **>(&utf8), &utf8_len); |
| 323 if (Dart_IsError(result)) { |
| 324 Dart_PropagateError(result); |
| 325 } |
| 326 intptr_t system_len; |
| 327 const char* system_string = |
| 328 StringUtils::Utf8ToConsoleString(utf8, utf8_len, &system_len); |
| 329 if (system_string == NULL) { |
| 330 Dart_ThrowException( |
| 331 DartUtils::NewInternalError("StringToSystemEncoding failed")); |
| 332 } |
| 313 uint8_t* buffer = NULL; | 333 uint8_t* buffer = NULL; |
| 314 Dart_Handle external_array = IOBuffer::Allocate(external_length, &buffer); | 334 Dart_Handle external_array = IOBuffer::Allocate(system_len, &buffer); |
| 315 memmove(buffer, system_string, external_length); | 335 if (Dart_IsError(external_array)) { |
| 316 if (utf8 != system_string) free(const_cast<char*>(system_string)); | 336 free(const_cast<char*>(system_string)); |
| 337 Dart_PropagateError(result); |
| 338 } |
| 339 memmove(buffer, system_string, system_len); |
| 340 free(const_cast<char*>(system_string)); |
| 317 Dart_SetReturnValue(args, external_array); | 341 Dart_SetReturnValue(args, external_array); |
| 318 } | 342 } |
| 319 | 343 |
| 320 } // namespace bin | 344 } // namespace bin |
| 321 } // namespace dart | 345 } // namespace dart |
| OLD | NEW |