未验证 提交 c533b605 编写于 作者: C campersau 提交者: GitHub

[wasm] Implement System.Console.Clear (#43002)

* [wasm] Implement System.Console.Clear

* Add dummy console.clear implementation if it does not exist

* Initialize console in Clear

* Apply suggestions from codereview
上级 c2e9b268
......@@ -236,4 +236,7 @@
<ItemGroup Condition="'$(TargetsUnix)' == 'true'">
<Reference Include="System.Threading.Thread" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetsBrowser)' == 'true'">
<ProjectReference Include="$(LibrariesProjectRoot)System.Private.Runtime.InteropServices.JavaScript\src\System.Private.Runtime.InteropServices.JavaScript.csproj" />
</ItemGroup>
</Project>
......@@ -6,6 +6,8 @@
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;
using JSObject = System.Runtime.InteropServices.JavaScript.JSObject;
namespace System
{
internal sealed class WasmConsoleStream : ConsoleStream
......@@ -74,6 +76,9 @@ public override void Flush()
internal static class ConsolePal
{
private static volatile bool s_consoleInitialized;
private static JSObject? s_console;
private static Encoding? s_outputEncoding;
internal static void EnsureConsoleInitialized() { }
......@@ -163,7 +168,16 @@ public static string Title
char sourceChar, ConsoleColor sourceForeColor,
ConsoleColor sourceBackColor) => throw new PlatformNotSupportedException();
public static void Clear() => throw new PlatformNotSupportedException();
public static void Clear()
{
if (!s_consoleInitialized)
{
s_console = (JSObject)System.Runtime.InteropServices.JavaScript.Runtime.GetGlobalObject("console");
s_consoleInitialized = true;
}
s_console?.Invoke("clear");
}
public static void SetCursorPosition(int left, int top) => throw new PlatformNotSupportedException();
......
......@@ -47,6 +47,7 @@ if (is_browser) {
if (typeof console === "undefined") {
var Console = function () {
this.log = function(msg){ print(msg) };
this.clear = function() { };
};
console = new Console();
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册