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
3// BSD-style license that can be found in the LICENSE file.
4
5#include "bin/io_natives.h"
6
7#include <stdlib.h>
8#include <string.h>
9
10#include "bin/builtin.h"
11#include "bin/dartutils.h"
12#include "bin/socket_base.h"
13#include "include/dart_api.h"
14#include "platform/assert.h"
15
16namespace dart {
17namespace bin {
18
19// Lists the native functions implementing advanced dart:io classes.
20// Some classes, like File and Directory, list their implementations in
21// builtin_natives.cc instead.
22#define IO_NATIVE_LIST(V) \
23 V(CLI_WaitForEvent, 1) \
24 V(Crypto_GetRandomBytes, 1) \
25 V(Directory_Create, 2) \
26 V(Directory_CreateTemp, 2) \
27 V(Directory_Current, 1) \
28 V(Directory_Delete, 3) \
29 V(Directory_Exists, 2) \
30 V(Directory_FillWithDirectoryListing, 5) \
31 V(Directory_GetAsyncDirectoryListerPointer, 1) \
32 V(Directory_Rename, 3) \
33 V(Directory_SetAsyncDirectoryListerPointer, 2) \
34 V(Directory_SetCurrent, 2) \
35 V(Directory_SystemTemp, 1) \
36 V(EventHandler_SendData, 3) \
37 V(EventHandler_TimerMillisecondClock, 0) \
38 V(File_AreIdentical, 3) \
39 V(File_Close, 1) \
40 V(File_Copy, 3) \
41 V(File_Create, 3) \
42 V(File_CreateLink, 3) \
43 V(File_CreatePipe, 1) \
44 V(File_Delete, 2) \
45 V(File_DeleteLink, 2) \
46 V(File_Exists, 2) \
47 V(File_Flush, 1) \
48 V(File_GetPointer, 1) \
49 V(File_GetFD, 1) \
50 V(File_GetStdioHandleType, 1) \
51 V(File_GetType, 3) \
52 V(File_LastAccessed, 2) \
53 V(File_LastModified, 2) \
54 V(File_Length, 1) \
55 V(File_LengthFromPath, 2) \
56 V(File_LinkTarget, 2) \
57 V(File_Lock, 4) \
58 V(File_Open, 3) \
59 V(File_OpenStdio, 1) \
60 V(File_Position, 1) \
61 V(File_Read, 2) \
62 V(File_ReadByte, 1) \
63 V(File_ReadInto, 4) \
64 V(File_Rename, 3) \
65 V(File_RenameLink, 3) \
66 V(File_ResolveSymbolicLinks, 2) \
67 V(File_SetLastAccessed, 3) \
68 V(File_SetLastModified, 3) \
69 V(File_SetPointer, 2) \
70 V(File_SetPosition, 2) \
71 V(File_Stat, 2) \
72 V(File_Truncate, 2) \
73 V(File_WriteByte, 2) \
74 V(File_WriteFrom, 4) \
75 V(FileSystemWatcher_CloseWatcher, 1) \
76 V(FileSystemWatcher_GetSocketId, 2) \
77 V(FileSystemWatcher_InitWatcher, 0) \
78 V(FileSystemWatcher_IsSupported, 0) \
79 V(FileSystemWatcher_ReadEvents, 2) \
80 V(FileSystemWatcher_UnwatchPath, 2) \
81 V(FileSystemWatcher_WatchPath, 5) \
82 V(Filter_CreateZLibDeflate, 8) \
83 V(Filter_CreateZLibInflate, 4) \
84 V(Filter_Process, 4) \
85 V(Filter_Processed, 3) \
86 V(ResourceHandleImpl_toFile, 1) \
87 V(ResourceHandleImpl_toSocket, 1) \
88 V(ResourceHandleImpl_toRawSocket, 1) \
89 V(ResourceHandleImpl_toRawDatagramSocket, 1) \
90 V(InternetAddress_Parse, 1) \
91 V(InternetAddress_ParseScopedLinkLocalAddress, 1) \
92 V(InternetAddress_RawAddrToString, 1) \
93 V(IOService_NewServicePort, 0) \
94 V(Namespace_Create, 2) \
95 V(Namespace_GetDefault, 0) \
96 V(Namespace_GetPointer, 1) \
97 V(OSError_inProgressErrorCode, 0) \
98 V(Platform_NumberOfProcessors, 0) \
99 V(Platform_OperatingSystem, 0) \
100 V(Platform_OperatingSystemVersion, 0) \
101 V(Platform_PathSeparator, 0) \
102 V(Platform_LocalHostname, 0) \
103 V(Platform_ExecutableName, 0) \
104 V(Platform_ResolvedExecutableName, 0) \
105 V(Platform_Environment, 0) \
106 V(Platform_ExecutableArguments, 0) \
107 V(Platform_GetVersion, 0) \
108 V(Platform_LocaleName, 0) \
109 V(Process_Start, 12) \
110 V(Process_Wait, 5) \
111 V(Process_KillPid, 2) \
112 V(Process_SetExitCode, 1) \
113 V(Process_GetExitCode, 0) \
114 V(Process_Exit, 1) \
115 V(Process_Sleep, 1) \
116 V(Process_Pid, 1) \
117 V(Process_SetSignalHandler, 1) \
118 V(Process_ClearSignalHandler, 1) \
119 V(ProcessInfo_CurrentRSS, 0) \
120 V(ProcessInfo_MaxRSS, 0) \
121 V(RawSocketOption_GetOptionValue, 1) \
122 V(SecureSocket_Connect, 7) \
123 V(SecureSocket_Destroy, 1) \
124 V(SecureSocket_FilterPointer, 1) \
125 V(SecureSocket_GetSelectedProtocol, 1) \
126 V(SecureSocket_Handshake, 2) \
127 V(SecureSocket_MarkAsTrusted, 3) \
128 V(SecureSocket_NewX509CertificateWrapper, 1) \
129 V(SecureSocket_Init, 1) \
130 V(SecureSocket_PeerCertificate, 1) \
131 V(SecureSocket_RegisterBadCertificateCallback, 2) \
132 V(SecureSocket_RegisterKeyLogPort, 2) \
133 V(SecureSocket_RegisterHandshakeCompleteCallback, 2) \
134 V(SecurityContext_Allocate, 1) \
135 V(SecurityContext_UsePrivateKeyBytes, 3) \
136 V(SecurityContext_SetAlpnProtocols, 3) \
137 V(SecurityContext_SetClientAuthoritiesBytes, 3) \
138 V(SecurityContext_SetTrustedCertificatesBytes, 3) \
139 V(SecurityContext_TrustBuiltinRoots, 1) \
140 V(SecurityContext_SetAllowTlsRenegotiation, 2) \
141 V(SecurityContext_UseCertificateChainBytes, 3) \
142 V(ServerSocket_Accept, 2) \
143 V(ServerSocket_CreateBindListen, 7) \
144 V(ServerSocket_CreateUnixDomainBindListen, 5) \
145 V(SocketBase_IsBindError, 2) \
146 V(Socket_Available, 1) \
147 V(Socket_AvailableDatagram, 1) \
148 V(Socket_CreateBindConnect, 6) \
149 V(Socket_CreateUnixDomainBindConnect, 4) \
150 V(Socket_CreateBindDatagram, 6) \
151 V(Socket_CreateConnect, 4) \
152 V(Socket_CreateUnixDomainConnect, 3) \
153 V(Socket_GetPort, 1) \
154 V(Socket_GetRemotePeer, 1) \
155 V(Socket_GetError, 1) \
156 V(Socket_Fatal, 1) \
157 V(Socket_GetFD, 1) \
158 V(Socket_GetOption, 3) \
159 V(Socket_GetRawOption, 4) \
160 V(Socket_GetSocketId, 1) \
161 V(Socket_GetStdioHandle, 2) \
162 V(Socket_GetType, 1) \
163 V(Socket_JoinMulticast, 4) \
164 V(Socket_LeaveMulticast, 4) \
165 V(Socket_Read, 2) \
166 V(Socket_RecvFrom, 1) \
167 V(Socket_ReceiveMessage, 2) \
168 V(Socket_SendMessage, 5) \
169 V(Socket_SendTo, 6) \
170 V(Socket_SetOption, 4) \
171 V(Socket_SetRawOption, 4) \
172 V(Socket_SetSocketId, 3) \
173 V(Socket_WriteList, 4) \
174 V(Socket_HasPendingWrite, 1) \
175 V(SocketControlMessage_fromHandles, 2) \
176 V(SocketControlMessageImpl_extractHandles, 1) \
177 V(Stdin_ReadByte, 1) \
178 V(Stdin_GetEchoMode, 1) \
179 V(Stdin_SetEchoMode, 2) \
180 V(Stdin_GetEchoNewlineMode, 1) \
181 V(Stdin_SetEchoNewlineMode, 2) \
182 V(Stdin_GetLineMode, 1) \
183 V(Stdin_SetLineMode, 2) \
184 V(Stdin_AnsiSupported, 1) \
185 V(Stdout_GetTerminalSize, 1) \
186 V(Stdout_AnsiSupported, 1) \
187 V(StringToSystemEncoding, 1) \
188 V(SynchronousSocket_Available, 1) \
189 V(SynchronousSocket_CloseSync, 1) \
190 V(SynchronousSocket_CreateConnectSync, 3) \
191 V(SynchronousSocket_GetPort, 1) \
192 V(SynchronousSocket_GetRemotePeer, 1) \
193 V(SynchronousSocket_LookupRequest, 2) \
194 V(SynchronousSocket_ShutdownRead, 1) \
195 V(SynchronousSocket_ShutdownWrite, 1) \
196 V(SynchronousSocket_Read, 2) \
197 V(SynchronousSocket_ReadList, 4) \
198 V(SynchronousSocket_WriteList, 4) \
199 V(SystemEncodingToString, 1) \
200 V(X509_Der, 1) \
201 V(X509_Pem, 1) \
202 V(X509_Sha1, 1) \
203 V(X509_Subject, 1) \
204 V(X509_Issuer, 1) \
205 V(X509_StartValidity, 1) \
206 V(X509_EndValidity, 1)
207
208IO_NATIVE_LIST(DECLARE_FUNCTION);
209
210static const struct NativeEntries {
211 const char* name_;
212 Dart_NativeFunction function_;
213 int argument_count_;
214} IOEntries[] = {IO_NATIVE_LIST(REGISTER_FUNCTION)};
215
216Dart_NativeFunction IONativeLookup(Dart_Handle name,
217 int argument_count,
218 bool* auto_setup_scope) {
219 const char* function_name = nullptr;
220 Dart_Handle result = Dart_StringToCString(str: name, cstr: &function_name);
221 ASSERT(!Dart_IsError(result));
222 ASSERT(function_name != nullptr);
223 ASSERT(auto_setup_scope != nullptr);
224 *auto_setup_scope = true;
225 int num_entries = sizeof(IOEntries) / sizeof(struct NativeEntries);
226 for (int i = 0; i < num_entries; i++) {
227 const struct NativeEntries* entry = &(IOEntries[i]);
228 if ((strcmp(function_name, entry->name_) == 0) &&
229 (entry->argument_count_ == argument_count)) {
230 return reinterpret_cast<Dart_NativeFunction>(entry->function_);
231 }
232 }
233 return nullptr;
234}
235
236const uint8_t* IONativeSymbol(Dart_NativeFunction nf) {
237 int num_entries = sizeof(IOEntries) / sizeof(struct NativeEntries);
238 for (int i = 0; i < num_entries; i++) {
239 const struct NativeEntries* entry = &(IOEntries[i]);
240 if (reinterpret_cast<Dart_NativeFunction>(entry->function_) == nf) {
241 return reinterpret_cast<const uint8_t*>(entry->name_);
242 }
243 }
244 return nullptr;
245}
246
247} // namespace bin
248} // namespace dart
249

source code of dart_sdk/runtime/bin/io_natives.cc