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, 2) \
42 V(File_CreateLink, 3) \
43 V(File_Delete, 2) \
44 V(File_DeleteLink, 2) \
45 V(File_Exists, 2) \
46 V(File_Flush, 1) \
47 V(File_GetPointer, 1) \
48 V(File_GetStdioHandleType, 1) \
49 V(File_GetType, 3) \
50 V(File_LastAccessed, 2) \
51 V(File_LastModified, 2) \
52 V(File_Length, 1) \
53 V(File_LengthFromPath, 2) \
54 V(File_LinkTarget, 2) \
55 V(File_Lock, 4) \
56 V(File_Open, 3) \
57 V(File_OpenStdio, 1) \
58 V(File_Position, 1) \
59 V(File_Read, 2) \
60 V(File_ReadByte, 1) \
61 V(File_ReadInto, 4) \
62 V(File_Rename, 3) \
63 V(File_RenameLink, 3) \
64 V(File_ResolveSymbolicLinks, 2) \
65 V(File_SetLastAccessed, 3) \
66 V(File_SetLastModified, 3) \
67 V(File_SetPointer, 2) \
68 V(File_SetPosition, 2) \
69 V(File_Stat, 2) \
70 V(File_Truncate, 2) \
71 V(File_WriteByte, 2) \
72 V(File_WriteFrom, 4) \
73 V(FileSystemWatcher_CloseWatcher, 1) \
74 V(FileSystemWatcher_GetSocketId, 2) \
75 V(FileSystemWatcher_InitWatcher, 0) \
76 V(FileSystemWatcher_IsSupported, 0) \
77 V(FileSystemWatcher_ReadEvents, 2) \
78 V(FileSystemWatcher_UnwatchPath, 2) \
79 V(FileSystemWatcher_WatchPath, 5) \
80 V(Filter_CreateZLibDeflate, 8) \
81 V(Filter_CreateZLibInflate, 4) \
82 V(Filter_Process, 4) \
83 V(Filter_Processed, 3) \
84 V(InternetAddress_Parse, 1) \
85 V(InternetAddress_ParseScopedLinkLocalAddress, 1) \
86 V(InternetAddress_RawAddrToString, 1) \
87 V(IOService_NewServicePort, 0) \
88 V(Namespace_Create, 2) \
89 V(Namespace_GetDefault, 0) \
90 V(Namespace_GetPointer, 1) \
91 V(NetworkInterface_ListSupported, 0) \
92 V(Platform_NumberOfProcessors, 0) \
93 V(Platform_OperatingSystem, 0) \
94 V(Platform_OperatingSystemVersion, 0) \
95 V(Platform_PathSeparator, 0) \
96 V(Platform_LocalHostname, 0) \
97 V(Platform_ExecutableName, 0) \
98 V(Platform_ResolvedExecutableName, 0) \
99 V(Platform_Environment, 0) \
100 V(Platform_ExecutableArguments, 0) \
101 V(Platform_GetVersion, 0) \
102 V(Platform_LocaleName, 0) \
103 V(Process_Start, 12) \
104 V(Process_Wait, 5) \
105 V(Process_KillPid, 2) \
106 V(Process_SetExitCode, 1) \
107 V(Process_GetExitCode, 0) \
108 V(Process_Exit, 1) \
109 V(Process_Sleep, 1) \
110 V(Process_Pid, 1) \
111 V(Process_SetSignalHandler, 1) \
112 V(Process_ClearSignalHandler, 1) \
113 V(ProcessInfo_CurrentRSS, 0) \
114 V(ProcessInfo_MaxRSS, 0) \
115 V(RawSocketOption_GetOptionValue, 1) \
116 V(SecureSocket_Connect, 7) \
117 V(SecureSocket_Destroy, 1) \
118 V(SecureSocket_FilterPointer, 1) \
119 V(SecureSocket_GetSelectedProtocol, 1) \
120 V(SecureSocket_Handshake, 1) \
121 V(SecureSocket_Init, 1) \
122 V(SecureSocket_PeerCertificate, 1) \
123 V(SecureSocket_RegisterBadCertificateCallback, 2) \
124 V(SecureSocket_RegisterHandshakeCompleteCallback, 2) \
125 V(SecureSocket_Renegotiate, 4) \
126 V(SecurityContext_Allocate, 1) \
127 V(SecurityContext_UsePrivateKeyBytes, 3) \
128 V(SecurityContext_SetAlpnProtocols, 3) \
129 V(SecurityContext_SetClientAuthoritiesBytes, 3) \
130 V(SecurityContext_SetTrustedCertificatesBytes, 3) \
131 V(SecurityContext_TrustBuiltinRoots, 1) \
132 V(SecurityContext_UseCertificateChainBytes, 3) \
133 V(ServerSocket_Accept, 2) \
134 V(ServerSocket_CreateBindListen, 7) \
135 V(ServerSocket_CreateUnixDomainBindListen, 5) \
136 V(SocketBase_IsBindError, 2) \
137 V(Socket_Available, 1) \
138 V(Socket_AvailableDatagram, 1) \
139 V(Socket_CreateBindConnect, 5) \
140 V(Socket_CreateUnixDomainBindConnect, 4) \
141 V(Socket_CreateBindDatagram, 6) \
142 V(Socket_CreateConnect, 4) \
143 V(Socket_CreateUnixDomainConnect, 3) \
144 V(Socket_GetPort, 1) \
145 V(Socket_GetRemotePeer, 1) \
146 V(Socket_GetError, 1) \
147 V(Socket_GetOption, 3) \
148 V(Socket_GetRawOption, 4) \
149 V(Socket_GetSocketId, 1) \
150 V(Socket_GetStdioHandle, 2) \
151 V(Socket_GetType, 1) \
152 V(Socket_JoinMulticast, 4) \
153 V(Socket_LeaveMulticast, 4) \
154 V(Socket_Read, 2) \
155 V(Socket_RecvFrom, 1) \
156 V(Socket_SendTo, 6) \
157 V(Socket_SetOption, 4) \
158 V(Socket_SetRawOption, 4) \
159 V(Socket_SetSocketId, 3) \
160 V(Socket_WriteList, 4) \
161 V(Stdin_ReadByte, 1) \
162 V(Stdin_GetEchoMode, 1) \
163 V(Stdin_SetEchoMode, 2) \
164 V(Stdin_GetLineMode, 1) \
165 V(Stdin_SetLineMode, 2) \
166 V(Stdin_AnsiSupported, 1) \
167 V(Stdout_GetTerminalSize, 1) \
168 V(Stdout_AnsiSupported, 1) \
169 V(StringToSystemEncoding, 1) \
170 V(SynchronousSocket_Available, 1) \
171 V(SynchronousSocket_CloseSync, 1) \
172 V(SynchronousSocket_CreateConnectSync, 3) \
173 V(SynchronousSocket_GetPort, 1) \
174 V(SynchronousSocket_GetRemotePeer, 1) \
175 V(SynchronousSocket_LookupRequest, 2) \
176 V(SynchronousSocket_ShutdownRead, 1) \
177 V(SynchronousSocket_ShutdownWrite, 1) \
178 V(SynchronousSocket_Read, 2) \
179 V(SynchronousSocket_ReadList, 4) \
180 V(SynchronousSocket_WriteList, 4) \
181 V(SystemEncodingToString, 1) \
182 V(X509_Der, 1) \
183 V(X509_Pem, 1) \
184 V(X509_Sha1, 1) \
185 V(X509_Subject, 1) \
186 V(X509_Issuer, 1) \
187 V(X509_StartValidity, 1) \
188 V(X509_EndValidity, 1)
189
190IO_NATIVE_LIST(DECLARE_FUNCTION);
191
192static struct NativeEntries {
193 const char* name_;
194 Dart_NativeFunction function_;
195 int argument_count_;
196} IOEntries[] = {IO_NATIVE_LIST(REGISTER_FUNCTION)};
197
198Dart_NativeFunction IONativeLookup(Dart_Handle name,
199 int argument_count,
200 bool* auto_setup_scope) {
201 const char* function_name = NULL;
202 Dart_Handle result = Dart_StringToCString(name, &function_name);
203 ASSERT(!Dart_IsError(result));
204 ASSERT(function_name != NULL);
205 ASSERT(auto_setup_scope != NULL);
206 *auto_setup_scope = true;
207 int num_entries = sizeof(IOEntries) / sizeof(struct NativeEntries);
208 for (int i = 0; i < num_entries; i++) {
209 struct NativeEntries* entry = &(IOEntries[i]);
210 if ((strcmp(function_name, entry->name_) == 0) &&
211 (entry->argument_count_ == argument_count)) {
212 return reinterpret_cast<Dart_NativeFunction>(entry->function_);
213 }
214 }
215 return NULL;
216}
217
218const uint8_t* IONativeSymbol(Dart_NativeFunction nf) {
219 int num_entries = sizeof(IOEntries) / sizeof(struct NativeEntries);
220 for (int i = 0; i < num_entries; i++) {
221 struct NativeEntries* entry = &(IOEntries[i]);
222 if (reinterpret_cast<Dart_NativeFunction>(entry->function_) == nf) {
223 return reinterpret_cast<const uint8_t*>(entry->name_);
224 }
225 }
226 return NULL;
227}
228
229} // namespace bin
230} // namespace dart
231