Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion src/libraries/System.Console/src/System.Console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,22 @@
<Link>Common\Interop\Android\Interop.Libraries.cs</Link>
</Compile>
</ItemGroup>
<!-- WebAssembly -->
<ItemGroup Condition="'$(TargetsBrowser)' == 'true'">
<Compile Include="System\ConsolePal.WebAssembly.cs" />
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.Dup.cs"
Link="Common\Interop\Unix\Interop.Dup.cs" />
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.Write.cs"
Link="Common\Interop\Unix\Interop.Write.cs" />
<Compile Include="$(CommonPath)Interop\Unix\Interop.Libraries.cs"
Link="Common\Interop\Unix\Interop.Libraries.cs" />
<Compile Include="$(CommonPath)Interop\Unix\Interop.Errors.cs"
Link="Common\Interop\Unix\Interop.Errors.cs" />
<Compile Include="$(CommonPath)Interop\Unix\System.Native\Interop.FileDescriptors.cs"
Link="Common\Interop\Unix\Interop.FileDescriptors.cs" />
<Compile Include="$(CommonPath)Interop\Unix\Interop.IOErrors.cs"
Link="Common\Interop\Unix\Interop.IOErrors.cs" />
</ItemGroup>
<!-- Windows -->
<ItemGroup Condition="'$(TargetsWindows)' == 'true'">
<Compile Include="System\ConsolePal.Windows.cs" />
Expand Down Expand Up @@ -135,7 +151,7 @@
Link="Common\System\Text\ValueStringBuilder.cs" />
</ItemGroup>
<!-- Unix -->
<ItemGroup Condition=" '$(TargetsUnix)' == 'true' and '$(TargetsAndroid)' != 'true' and '$(TargetsiOS)' != 'true' and '$(TargetstvOS)' != 'true'">
<ItemGroup Condition=" '$(TargetsUnix)' == 'true' and '$(TargetsAndroid)' != 'true' and '$(TargetsiOS)' != 'true' and '$(TargetstvOS)' != 'true' and '$(TargetsBrowser)' != 'true'">
<Compile Include="System\ConsolePal.Unix.cs" />
<Compile Include="System\TermInfo.cs" />
<Compile Include="System\IO\StdInReader.cs" />
Expand Down
226 changes: 226 additions & 0 deletions src/libraries/System.Console/src/System/ConsolePal.WebAssembly.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

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
{
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 => Encoding.Unicode;

public static void SetConsoleOutputEncoding(Encoding enc) => throw new PlatformNotSupportedException();

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 => false;

public static bool CapsLock => false;

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 CursorLeft => throw new PlatformNotSupportedException();

public static int CursorTop => 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 => throw new PlatformNotSupportedException();
set => throw new PlatformNotSupportedException();
}

public static int WindowTop
{
get => throw new PlatformNotSupportedException();
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();
}
}
}