-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
ConsolePal.WebAssembly.cs
225 lines (171 loc) · 7.42 KB
/
ConsolePal.WebAssembly.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.IO;
using System.Text;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;
namespace System
{
internal sealed class WasmConsoleStream : ConsoleStream
{
private readonly SafeFileHandle _handle;
internal WasmConsoleStream(SafeFileHandle handle, FileAccess access)
: base(access)
{
_handle = handle;
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
_handle.Dispose();
}
base.Dispose(disposing);
}
public override int Read(byte[] buffer, int offset, int count) => throw Error.GetReadNotSupported();
public override unsafe void Write(byte[] buffer, int offset, int count)
{
ValidateWrite(buffer, offset, count);
fixed (byte* bufPtr = buffer)
{
Write(_handle, bufPtr + offset, count);
}
}
private static unsafe void Write(SafeFileHandle fd, byte* bufPtr, int count)
{
while (count > 0)
{
int bytesWritten = Interop.Sys.Write(fd, bufPtr, count);
if (bytesWritten < 0)
{
Interop.ErrorInfo errorInfo = Interop.Sys.GetLastErrorInfo();
if (errorInfo.Error == Interop.Error.EPIPE)
{
return;
}
else
{
throw Interop.GetIOException(errorInfo);
}
}
count -= bytesWritten;
bufPtr += bytesWritten;
}
}
public override void Flush()
{
if (_handle.IsClosed)
{
throw Error.GetFileNotOpen();
}
base.Flush();
}
}
internal static class ConsolePal
{
private static Encoding? s_outputEncoding;
internal static void EnsureConsoleInitialized() { }
public static Stream OpenStandardInput() => throw new PlatformNotSupportedException();
public static Stream OpenStandardOutput()
{
return new WasmConsoleStream(Interop.Sys.Dup(Interop.Sys.FileDescriptors.STDOUT_FILENO), FileAccess.Write);
}
public static Stream OpenStandardError()
{
return new WasmConsoleStream(Interop.Sys.Dup(Interop.Sys.FileDescriptors.STDERR_FILENO), FileAccess.Write);
}
public static Encoding InputEncoding => throw new PlatformNotSupportedException();
public static void SetConsoleInputEncoding(Encoding enc) => throw new PlatformNotSupportedException();
public static Encoding OutputEncoding => s_outputEncoding ?? Encoding.UTF8;
public static void SetConsoleOutputEncoding(Encoding enc) => s_outputEncoding = enc;
public static bool IsInputRedirectedCore() => false;
public static bool IsOutputRedirectedCore() => false;
public static bool IsErrorRedirectedCore() => false;
internal static TextReader GetOrCreateReader() => throw new PlatformNotSupportedException();
public static bool NumberLock => throw new PlatformNotSupportedException();
public static bool CapsLock => throw new PlatformNotSupportedException();
public static bool KeyAvailable => false;
public static ConsoleKeyInfo ReadKey(bool intercept) => throw new PlatformNotSupportedException();
public static bool TreatControlCAsInput
{
get => throw new PlatformNotSupportedException();
set => throw new PlatformNotSupportedException();
}
public static ConsoleColor BackgroundColor
{
get => throw new PlatformNotSupportedException();
set => throw new PlatformNotSupportedException();
}
public static ConsoleColor ForegroundColor
{
get => throw new PlatformNotSupportedException();
set => throw new PlatformNotSupportedException();
}
public static void ResetColor() => throw new PlatformNotSupportedException();
public static int CursorSize
{
get => throw new PlatformNotSupportedException();
set => throw new PlatformNotSupportedException();
}
public static bool CursorVisible
{
get => throw new PlatformNotSupportedException();
set => throw new PlatformNotSupportedException();
}
public static (int Left, int Top) GetCursorPosition() => throw new PlatformNotSupportedException();
public static string Title
{
get => throw new PlatformNotSupportedException();
set => throw new PlatformNotSupportedException();
}
public static void Beep() => throw new PlatformNotSupportedException();
public static void Beep(int frequency, int duration) => throw new PlatformNotSupportedException();
public static void MoveBufferArea(int sourceLeft, int sourceTop,
int sourceWidth, int sourceHeight, int targetLeft, int targetTop,
char sourceChar, ConsoleColor sourceForeColor,
ConsoleColor sourceBackColor) => throw new PlatformNotSupportedException();
public static void Clear() => throw new PlatformNotSupportedException();
public static void SetCursorPosition(int left, int top) => throw new PlatformNotSupportedException();
public static int BufferWidth
{
get => throw new PlatformNotSupportedException();
set => throw new PlatformNotSupportedException();
}
public static int BufferHeight
{
get => throw new PlatformNotSupportedException();
set => throw new PlatformNotSupportedException();
}
public static void SetBufferSize(int width, int height) => throw new PlatformNotSupportedException();
public static int LargestWindowWidth => throw new PlatformNotSupportedException();
public static int LargestWindowHeight => throw new PlatformNotSupportedException();
public static int WindowLeft
{
get => 0;
set => throw new PlatformNotSupportedException();
}
public static int WindowTop
{
get => 0;
set => throw new PlatformNotSupportedException();
}
public static int WindowWidth
{
get => throw new PlatformNotSupportedException();
set => throw new PlatformNotSupportedException();
}
public static int WindowHeight
{
get => throw new PlatformNotSupportedException();
set => throw new PlatformNotSupportedException();
}
public static void SetWindowPosition(int left, int top) => throw new PlatformNotSupportedException();
public static void SetWindowSize(int width, int height) => throw new PlatformNotSupportedException();
internal sealed class ControlCHandlerRegistrar
{
internal ControlCHandlerRegistrar() => throw new PlatformNotSupportedException();
internal void Register() => throw new PlatformNotSupportedException();
internal void Unregister() => throw new PlatformNotSupportedException();
}
}
}