Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wasm]: JavaScript interop with [JSImport] and [JSExport] attributes and Roslyn #66304

Merged
merged 5 commits into from
Jul 10, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,6 @@
<PlatformManifestFileEntry Include="libmono-profiler-aot.a" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-wasm-eh-js.a" IsNative="true" />
<PlatformManifestFileEntry Include="libmono-wasm-eh-wasm.a" IsNative="true" />
<PlatformManifestFileEntry Include="System.Private.Runtime.InteropServices.Javascript.dll" />
<PlatformManifestFileEntry Include="dotnet-crypto-worker.js" IsNative="true" />
<PlatformManifestFileEntry Include="dotnet.js" IsNative="true" />
<PlatformManifestFileEntry Include="dotnet.worker.js" IsNative="true" />
Expand Down
34 changes: 17 additions & 17 deletions src/libraries/Common/src/Interop/Browser/Interop.Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,23 @@ internal static partial class Interop
{
internal static unsafe partial class Runtime
{
[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal static extern void ReleaseCSOwnedObject(IntPtr jsHandle);
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern unsafe void BindJSFunction(in string function_name, in string module_name, void* signature, out IntPtr bound_function_js_handle, out int is_exception, out object result);
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern void InvokeJSFunction(IntPtr bound_function_js_handle, void* data);
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern unsafe void BindCSFunction(in string fully_qualified_name, int signature_hash, void* signature, out int is_exception, out object result);
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern void MarshalPromise(void* data);
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern IntPtr RegisterGCRoot(IntPtr start, int bytesSize, IntPtr name);
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern void DeregisterGCRoot(IntPtr handle);

#region Legacy

[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal static extern string InvokeJS(string str, out int exceptionalResult);
[MethodImplAttribute(MethodImplOptions.InternalCall)]
Expand All @@ -26,8 +43,6 @@ internal static unsafe partial class Runtime
[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal static extern void TypedArrayToArrayRef(IntPtr jsHandle, out int exceptionalResult, out object result);
[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal static extern void ReleaseCSOwnedObject(IntPtr jsHandle);
[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal static extern void CreateCSOwnedObjectRef(in string className, in object[] parms, out int exceptionalResult, out object result);
[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal static extern void TypedArrayCopyToRef(IntPtr jsHandle, int arrayPtr, int begin, int end, int bytesPerElement, out int exceptionalResult, out object result);
Expand All @@ -36,21 +51,6 @@ internal static unsafe partial class Runtime
[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal static extern void TypedArrayCopyFromRef(IntPtr jsHandle, int arrayPtr, int begin, int end, int bytesPerElement, out int exceptionalResult, out object result);


#region Legacy
[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal static extern void WebSocketSend(IntPtr webSocketJSHandle, IntPtr messagePtr, int offset, int length, int messageType, bool endOfMessage, out IntPtr promiseJSHandle, out int exceptionalResult, out object result);
[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal static extern void WebSocketReceive(IntPtr webSocketJSHandle, IntPtr bufferPtr, int offset, int length, IntPtr responsePtr, out IntPtr promiseJSHandle, out int exceptionalResult, out object result);
[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal static extern void WebSocketOpenRef(in string uri, in object[]? subProtocols, in Delegate onClosed, out IntPtr webSocketJSHandle, out IntPtr promiseJSHandle, out int exceptionalResult, out object result);
[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal static extern void WebSocketAbort(IntPtr webSocketJSHandle, out int exceptionalResult, out string result);
[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal static extern void WebSocketCloseRef(IntPtr webSocketJSHandle, int code, in string? reason, bool waitForCloseReceived, out IntPtr promiseJSHandle, out int exceptionalResult, out object result);
[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal static extern void CancelPromiseRef(IntPtr promiseJSHandle, out int exceptionalResult, out string result);

#endregion

}
Expand Down
2 changes: 0 additions & 2 deletions src/libraries/NetCoreAppLibrary.props
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@
System.ObjectModel;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see that you are only removing code from this section. Should the newly added library not be exposed in the shared framework?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new ones are System.Runtime.InteropServices.JavaScript and JSImportGenerator.
Both of them should be visible and compile on any platform.
I don't fully understand the question nor the necessary action. Could you please elaborate ?

Copy link
Member

@ViktorHofer ViktorHofer Jul 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new assembly System.Runtime.InteropServices.JavaScript is currently not exposed in the shared framework because the entry is missing from here:

<NetCoreAppLibrary>
$(NetFxReference)
netstandard;
Microsoft.CSharp;
Microsoft.VisualBasic.Core;
Microsoft.Win32.Primitives;
Microsoft.Win32.Registry;
System.AppContext;
System.Buffers;
System.Collections;
System.Collections.Concurrent;
System.Collections.Immutable;
System.Collections.NonGeneric;
System.Collections.Specialized;
System.ComponentModel;
System.ComponentModel.Annotations;
System.ComponentModel.EventBasedAsync;
System.ComponentModel.Primitives;
System.ComponentModel.TypeConverter;
System.Console;
System.Data.Common;
System.Data.DataSetExtensions;
System.Diagnostics.Contracts;
System.Diagnostics.Debug;
System.Diagnostics.DiagnosticSource;
System.Diagnostics.FileVersionInfo;
System.Diagnostics.Process;
System.Diagnostics.StackTrace;
System.Diagnostics.TextWriterTraceListener;
System.Diagnostics.Tools;
System.Diagnostics.TraceSource;
System.Diagnostics.Tracing;
System.Drawing.Primitives;
System.Dynamic.Runtime;
System.Formats.Asn1;
System.Formats.Tar;
System.Globalization;
System.Globalization.Calendars;
System.Globalization.Extensions;
System.IO;
System.IO.Compression;
System.IO.Compression.Brotli;
System.IO.Compression.ZipFile;
System.IO.FileSystem;
System.IO.FileSystem.AccessControl;
System.IO.FileSystem.DriveInfo;
System.IO.FileSystem.Primitives;
System.IO.FileSystem.Watcher;
System.IO.IsolatedStorage;
System.IO.MemoryMappedFiles;
System.IO.Pipes;
System.IO.Pipes.AccessControl;
System.IO.UnmanagedMemoryStream;
System.Linq;
System.Linq.Expressions;
System.Linq.Parallel;
System.Linq.Queryable;
System.Memory;
System.Net.Http;
System.Net.Http.Json;
System.Net.HttpListener;
System.Net.Mail;
System.Net.NameResolution;
System.Net.NetworkInformation;
System.Net.Ping;
System.Net.Primitives;
System.Net.Quic;
System.Net.Requests;
System.Net.Security;
System.Net.ServicePoint;
System.Net.Sockets;
System.Net.WebClient;
System.Net.WebHeaderCollection;
System.Net.WebProxy;
System.Net.WebSockets;
System.Net.WebSockets.Client;
System.Numerics.Vectors;
System.ObjectModel;
System.Private.CoreLib;
System.Private.DataContractSerialization;
System.Private.Uri;
System.Private.Xml;
System.Private.Xml.Linq;
System.Reflection;
System.Reflection.DispatchProxy;
System.Reflection.Emit;
System.Reflection.Emit.ILGeneration;
System.Reflection.Emit.Lightweight;
System.Reflection.Extensions;
System.Reflection.Metadata;
System.Reflection.Primitives;
System.Reflection.TypeExtensions;
System.Resources.Reader;
System.Resources.ResourceManager;
System.Resources.Writer;
System.Runtime;
System.Runtime.CompilerServices.Unsafe;
System.Runtime.CompilerServices.VisualC;
System.Runtime.Extensions;
System.Runtime.Handles;
System.Runtime.InteropServices;
System.Runtime.InteropServices.JavaScript;
System.Runtime.InteropServices.RuntimeInformation;
System.Runtime.Intrinsics;
System.Runtime.Loader;
System.Runtime.Numerics;
System.Runtime.Serialization.Formatters;
System.Runtime.Serialization.Json;
System.Runtime.Serialization.Primitives;
System.Runtime.Serialization.Xml;
System.Security.AccessControl;
System.Security.Claims;
System.Security.Cryptography;
System.Security.Cryptography.Algorithms;
System.Security.Cryptography.Cng;
System.Security.Cryptography.Csp;
System.Security.Cryptography.Encoding;
System.Security.Cryptography.OpenSsl;
System.Security.Cryptography.Primitives;
System.Security.Cryptography.X509Certificates;
System.Security.Principal;
System.Security.Principal.Windows;
System.Security.SecureString;
System.Text.Encoding;
System.Text.Encoding.CodePages;
System.Text.Encoding.Extensions;
System.Text.Encodings.Web;
System.Text.Json;
System.Text.RegularExpressions;
System.Threading;
System.Threading.Channels;
System.Threading.Overlapped;
System.Threading.Tasks;
System.Threading.Tasks.Dataflow;
System.Threading.Tasks.Extensions;
System.Threading.Tasks.Parallel;
System.Threading.Thread;
System.Threading.ThreadPool;
System.Threading.Timer;
System.Transactions.Local;
System.ValueTuple;
System.Web.HttpUtility;
System.Xml.ReaderWriter;
System.Xml.XDocument;
System.Xml.XmlDocument;
System.Xml.XmlSerializer;
System.Xml.XPath;
System.Xml.XPath.XDocument;
</NetCoreAppLibrary>

The source generator is already exposed, so no action necessary for that one.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's already there from previous PR

System.Runtime.InteropServices.JavaScript;

is that OK ?

System.Private.CoreLib;
System.Private.DataContractSerialization;
System.Private.Runtime.InteropServices.JavaScript;
System.Private.Uri;
System.Private.Xml;
System.Private.Xml.Linq;
Expand Down Expand Up @@ -179,7 +178,6 @@
System.Net.Quic;
System.Private.CoreLib;
System.Private.DataContractSerialization;
System.Private.Runtime.InteropServices.JavaScript;
System.Private.Uri;
System.Private.Xml;
System.Private.Xml.Linq;
Expand Down
3 changes: 2 additions & 1 deletion src/libraries/System.Console/src/System.Console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@
<Reference Include="Microsoft.Win32.Primitives" />
</ItemGroup>
<ItemGroup Condition="'$(TargetPlatformIdentifier)' == 'Browser'">
<ProjectReference Include="$(LibrariesProjectRoot)System.Runtime.InteropServices.JavaScript\src\System.Runtime.InteropServices.JavaScript.csproj" SkipUseReferenceAssembly="true"/>
<ProjectReference Include="$(LibrariesProjectRoot)System.Runtime.InteropServices.JavaScript\gen\JSImportGenerator\JSImportGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
ViktorHofer marked this conversation as resolved.
Show resolved Hide resolved
<Reference Include="System.Runtime.InteropServices.JavaScript" />
</ItemGroup>
</Project>
20 changes: 3 additions & 17 deletions src/libraries/System.Console/src/System/ConsolePal.WebAssembly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@

using System.IO;
using System.Text;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;
using System.Runtime.InteropServices.JavaScript;

using JSObject = System.Runtime.InteropServices.JavaScript.JSObject;

#pragma warning disable CS0612 // using obsolete members until we finish https://github.com/dotnet/runtime/pull/66304/

namespace System
Expand Down Expand Up @@ -75,10 +72,10 @@ public override void Flush()
}
}

internal static class ConsolePal
internal static partial class ConsolePal
{
private static volatile bool s_consoleInitialized;
private static JSObject? s_console;
[JSImport("globalThis.console.clear")]
public static partial void Clear();

private static Encoding? s_outputEncoding;

Expand Down Expand Up @@ -169,17 +166,6 @@ public static void MoveBufferArea(int sourceLeft, int sourceTop,
char sourceChar, ConsoleColor sourceForeColor,
ConsoleColor sourceBackColor) => 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();

public static int BufferWidth
Expand Down
8 changes: 6 additions & 2 deletions src/libraries/System.Net.Http/src/System.Net.Http.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -486,8 +486,11 @@
<Compile Include="System\Net\Http\BrowserHttpHandler\SocketsHttpHandler.cs" />
<Compile Include="System\Net\Http\BrowserHttpHandler\BrowserHttpHandler.cs" />
<Compile Include="System\Net\Http\BrowserHttpHandler\HttpTelemetry.Browser.cs" />
<Compile Include="$(CommonPath)System\Net\Http\HttpHandlerDefaults.cs"
<Compile Include="System\Net\Http\BrowserHttpHandler\BrowserHttpInterop.cs" />
<Compile Include="$(CommonPath)System\Net\Http\HttpHandlerDefaults.cs"
Link="Common\System\Net\Http\HttpHandlerDefaults.cs" />
<Compile Include="$(LibrariesProjectRoot)System.Runtime.InteropServices.JavaScript\src\System\Runtime\InteropServices\JavaScript\CancelablePromise.cs"
Link="InteropServices\JavaScript\CancelablePromise.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="$(LibrariesProjectRoot)System.Net.Quic\src\System.Net.Quic.csproj" />
Expand Down Expand Up @@ -520,7 +523,8 @@
<Reference Include="System.Security.Cryptography" />
</ItemGroup>
<ItemGroup Condition="'$(TargetPlatformIdentifier)' == 'Browser'">
<ProjectReference Include="$(LibrariesProjectRoot)System.Runtime.InteropServices.JavaScript\src\System.Runtime.InteropServices.JavaScript.csproj" SkipUseReferenceAssembly="true"/>
<ProjectReference Include="$(LibrariesProjectRoot)System.Runtime.InteropServices.JavaScript\gen\JSImportGenerator\JSImportGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<Reference Include="System.Runtime.InteropServices.JavaScript" />
</ItemGroup>
<ItemGroup Condition="'$(UseManagedNtlm)' == 'true'">
<ProjectReference Include="$(LibrariesProjectRoot)System.Formats.Asn1\src\System.Formats.Asn1.csproj" />
Expand Down
Loading