| 1 | // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 | |
| 5 | #include "bin/platform.h" |
| 6 | |
| 7 | #include "bin/dartutils.h" |
| 8 | #include "bin/file.h" |
| 9 | #include "bin/utils.h" |
| 10 | #include "include/dart_api.h" |
| 11 | |
| 12 | namespace dart { |
| 13 | namespace bin { |
| 14 | |
| 15 | void FUNCTION_NAME(Platform_NumberOfProcessors)(Dart_NativeArguments args) { |
| 16 | Dart_SetReturnValue(args, Dart_NewInteger(Platform::NumberOfProcessors())); |
| 17 | } |
| 18 | |
| 19 | void FUNCTION_NAME(Platform_OperatingSystem)(Dart_NativeArguments args) { |
| 20 | Dart_Handle str = DartUtils::NewString(Platform::OperatingSystem()); |
| 21 | ThrowIfError(str); |
| 22 | Dart_SetReturnValue(args, str); |
| 23 | } |
| 24 | |
| 25 | void FUNCTION_NAME(Platform_OperatingSystemVersion)(Dart_NativeArguments args) { |
| 26 | const char* version = Platform::OperatingSystemVersion(); |
| 27 | if (version == NULL) { |
| 28 | Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
| 29 | } else { |
| 30 | Dart_Handle str = DartUtils::NewString(version); |
| 31 | ThrowIfError(str); |
| 32 | Dart_SetReturnValue(args, str); |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | void FUNCTION_NAME(Platform_PathSeparator)(Dart_NativeArguments args) { |
| 37 | Dart_Handle str = DartUtils::NewString(File::PathSeparator()); |
| 38 | ThrowIfError(str); |
| 39 | Dart_SetReturnValue(args, str); |
| 40 | } |
| 41 | |
| 42 | void FUNCTION_NAME(Platform_LocalHostname)(Dart_NativeArguments args) { |
| 43 | const intptr_t HOSTNAME_LENGTH = 256; |
| 44 | char hostname[HOSTNAME_LENGTH]; |
| 45 | if (Platform::LocalHostname(hostname, HOSTNAME_LENGTH)) { |
| 46 | Dart_Handle str = DartUtils::NewString(hostname); |
| 47 | ThrowIfError(str); |
| 48 | Dart_SetReturnValue(args, str); |
| 49 | } else { |
| 50 | Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | void FUNCTION_NAME(Platform_ExecutableName)(Dart_NativeArguments args) { |
| 55 | if (Platform::GetExecutableName() != NULL) { |
| 56 | Dart_SetReturnValue( |
| 57 | args, Dart_NewStringFromCString(Platform::GetExecutableName())); |
| 58 | } else { |
| 59 | Dart_SetReturnValue(args, Dart_Null()); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | void FUNCTION_NAME(Platform_ResolvedExecutableName)(Dart_NativeArguments args) { |
| 64 | if (Platform::GetResolvedExecutableName() != NULL) { |
| 65 | Dart_SetReturnValue( |
| 66 | args, Dart_NewStringFromCString(Platform::GetResolvedExecutableName())); |
| 67 | } else { |
| 68 | Dart_SetReturnValue(args, Dart_Null()); |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | void FUNCTION_NAME(Platform_ExecutableArguments)(Dart_NativeArguments args) { |
| 73 | int end = Platform::GetScriptIndex(); |
| 74 | char** argv = Platform::GetArgv(); |
| 75 | Dart_Handle string_type = DartUtils::GetDartType("dart:core" , "String" ); |
| 76 | ThrowIfError(string_type); |
| 77 | Dart_Handle result = |
| 78 | Dart_NewListOfTypeFilled(string_type, Dart_EmptyString(), end - 1); |
| 79 | for (intptr_t i = 1; i < end; i++) { |
| 80 | Dart_Handle str = DartUtils::NewString(argv[i]); |
| 81 | ThrowIfError(str); |
| 82 | ThrowIfError(Dart_ListSetAt(result, i - 1, str)); |
| 83 | } |
| 84 | Dart_SetReturnValue(args, result); |
| 85 | } |
| 86 | |
| 87 | void FUNCTION_NAME(Platform_Environment)(Dart_NativeArguments args) { |
| 88 | intptr_t count = 0; |
| 89 | char** env = Platform::Environment(&count); |
| 90 | if (env == NULL) { |
| 91 | OSError error(-1, "Failed to retrieve environment variables." , |
| 92 | OSError::kUnknown); |
| 93 | Dart_SetReturnValue(args, DartUtils::NewDartOSError(&error)); |
| 94 | } else { |
| 95 | Dart_Handle result = Dart_NewList(count); |
| 96 | ThrowIfError(result); |
| 97 | intptr_t result_idx = 0; |
| 98 | for (intptr_t env_idx = 0; env_idx < count; env_idx++) { |
| 99 | Dart_Handle str = DartUtils::NewString(env[env_idx]); |
| 100 | if (Dart_IsError(str)) { |
| 101 | // Silently skip over environment entries that are not valid UTF8 |
| 102 | // strings. |
| 103 | continue; |
| 104 | } |
| 105 | Dart_Handle error = Dart_ListSetAt(result, result_idx, str); |
| 106 | ThrowIfError(error); |
| 107 | result_idx++; |
| 108 | } |
| 109 | Dart_SetReturnValue(args, result); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | void FUNCTION_NAME(Platform_GetVersion)(Dart_NativeArguments args) { |
| 114 | Dart_SetReturnValue(args, Dart_NewStringFromCString(Dart_VersionString())); |
| 115 | } |
| 116 | |
| 117 | void FUNCTION_NAME(Platform_LocaleName)(Dart_NativeArguments args) { |
| 118 | const char* locale = Platform::LocaleName(); |
| 119 | if (locale == NULL) { |
| 120 | Dart_SetReturnValue(args, DartUtils::NewDartOSError()); |
| 121 | } else { |
| 122 | Dart_SetReturnValue(args, Dart_NewStringFromCString(locale)); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | } // namespace bin |
| 127 | } // namespace dart |
| 128 | |