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