Skip to content

[wasm] Preparation for public API in System.Runtime.InteropServices.JavaScript #71332

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

Merged
merged 3 commits into from
Jun 30, 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
63 changes: 4 additions & 59 deletions src/libraries/Common/src/Interop/Browser/Interop.Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,14 @@
using System;
using System.Runtime.CompilerServices;

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

internal static partial class Interop
{
internal static partial class Runtime
internal static unsafe partial class Runtime
{
[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal static extern string InvokeJS(string str, out int exceptionalResult);
[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal static extern void InvokeJSWithArgsRef(IntPtr jsHandle, in string method, in object?[] parms, out int exceptionalResult, out object result);
// FIXME: All of these signatures need to be object? in various places and not object, but the nullability
// warnings will take me hours and hours to fix so I'm not doing that right now since they're already broken
[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal static extern void GetObjectPropertyRef(IntPtr jsHandle, in string propertyName, out int exceptionalResult, out object result);
[MethodImplAttribute(MethodImplOptions.InternalCall)]
Expand All @@ -42,7 +36,8 @@ internal static 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);

// FIXME: Why are these IntPtr?

#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)]
Expand All @@ -56,57 +51,7 @@ internal static partial class Runtime
[MethodImplAttribute(MethodImplOptions.InternalCall)]
internal static extern void CancelPromiseRef(IntPtr promiseJSHandle, out int exceptionalResult, out string result);

// / <summary>
// / Execute the provided string in the JavaScript context
// / </summary>
// / <returns>The js.</returns>
// / <param name="str">String.</param>
public static string InvokeJS(string str)
{
string res = InvokeJS(str, out int exception);
if (exception != 0)
throw new JSException(res);
return res;
}

public static object GetGlobalObject(string? str = null)
{
int exception;
GetGlobalObjectRef(str, out exception, out object jsObj);

if (exception != 0)
throw new JSException($"Error obtaining a handle to global {str}");

ReleaseInFlight(jsObj);
return jsObj;
}

[MethodImplAttribute(MethodImplOptions.NoInlining)]
public static void StopProfile()
{
}

// Called by the AOT profiler to save profile data into INTERNAL.aot_profile_data
[MethodImplAttribute(MethodImplOptions.NoInlining)]
public static unsafe void DumpAotProfileData(ref byte buf, int len, string extraArg)
{
if (len == 0)
throw new JSException("Profile data length is 0");

var arr = new byte[len];
fixed (void *p = &buf)
{
var span = new ReadOnlySpan<byte>(p, len);
// Send it to JS
var module = (JSObject)Runtime.GetGlobalObject("INTERNAL");
module.SetObjectProperty("aot_profile_data", Uint8Array.From(span));
}
}
#endregion

internal static void ReleaseInFlight(object? obj)
{
JSObject? jsObj = obj as JSObject;
jsObj?.ReleaseInFlight();
}
}
}
1 change: 1 addition & 0 deletions src/libraries/NetCoreAppLibrary.props
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@
System.Runtime.Extensions;
System.Runtime.Handles;
System.Runtime.InteropServices;
System.Runtime.InteropServices.JavaScript;
System.Runtime.InteropServices.RuntimeInformation;
System.Runtime.Intrinsics;
System.Runtime.Loader;
Expand Down
2 changes: 1 addition & 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,6 @@
<Reference Include="Microsoft.Win32.Primitives" />
</ItemGroup>
<ItemGroup Condition="'$(TargetPlatformIdentifier)' == 'Browser'">
<ProjectReference Include="$(LibrariesProjectRoot)System.Private.Runtime.InteropServices.JavaScript\src\System.Private.Runtime.InteropServices.JavaScript.csproj" />
<ProjectReference Include="$(LibrariesProjectRoot)System.Runtime.InteropServices.JavaScript\src\System.Runtime.InteropServices.JavaScript.csproj" SkipUseReferenceAssembly="true"/>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
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
{
internal sealed class WasmConsoleStream : ConsoleStream
Expand Down
2 changes: 1 addition & 1 deletion src/libraries/System.Net.Http/src/System.Net.Http.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@
<Reference Include="System.Security.Cryptography" />
</ItemGroup>
<ItemGroup Condition="'$(TargetPlatformIdentifier)' == 'Browser'">
<ProjectReference Include="$(LibrariesProjectRoot)System.Private.Runtime.InteropServices.JavaScript\src\System.Private.Runtime.InteropServices.JavaScript.csproj" />
<ProjectReference Include="$(LibrariesProjectRoot)System.Runtime.InteropServices.JavaScript\src\System.Runtime.InteropServices.JavaScript.csproj" SkipUseReferenceAssembly="true"/>
</ItemGroup>
<ItemGroup Condition="'$(UseManagedNtlm)' == 'true'">
<ProjectReference Include="$(LibrariesProjectRoot)System.Formats.Asn1\src\System.Formats.Asn1.csproj" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Net.Security;
using System.IO;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
using System.Runtime.InteropServices.JavaScript;

using JSObject = System.Runtime.InteropServices.JavaScript.JSObject;
using JSException = System.Runtime.InteropServices.JavaScript.JSException;
using Uint8Array = System.Runtime.InteropServices.JavaScript.Uint8Array;
using Function = System.Runtime.InteropServices.JavaScript.Function;
#pragma warning disable CS0612 // using obsolete members until we finish https://github.com/dotnet/runtime/pull/66304/

namespace System.Net.Http
{
Expand All @@ -28,8 +22,8 @@ namespace System.Net.Http
internal sealed class BrowserHttpHandler : HttpMessageHandler
{
// This partial implementation contains members common to Browser WebAssembly running on .NET Core.
private static readonly JSObject? s_fetch = (JSObject)System.Runtime.InteropServices.JavaScript.Runtime.GetGlobalObject("fetch");
private static readonly JSObject? s_window = (JSObject)System.Runtime.InteropServices.JavaScript.Runtime.GetGlobalObject("window");
private static readonly JSObject? s_fetch = (JSObject)Runtime.InteropServices.JavaScript.Runtime.GetGlobalObject("fetch");
private static readonly JSObject? s_window = (JSObject)Runtime.InteropServices.JavaScript.Runtime.GetGlobalObject("window");

private static readonly HttpRequestOptionsKey<bool> EnableStreamingResponse = new HttpRequestOptionsKey<bool>("WebAssemblyEnableStreamingResponse");
private static readonly HttpRequestOptionsKey<IDictionary<string, object>> FetchOptions = new HttpRequestOptionsKey<IDictionary<string, object>>("WebAssemblyFetchOptions");
Expand All @@ -47,6 +41,10 @@ private static bool GetIsStreamingSupported()
return (bool)streamingSupported.Call();
}

private static Function AbortControllerFactory = new Function("return new AbortController()");
private static Function HeadersFactory = new Function("return new Headers()");
private static Function ObjectFactory = new Function("return new Object()");

#pragma warning disable CA1822
public bool UseCookies
{
Expand Down Expand Up @@ -153,7 +151,7 @@ async Task<HttpResponseMessage> Impl(HttpRequestMessage request, CancellationTok
CancellationTokenRegistration? abortRegistration = null;
try
{
using var requestObject = new JSObject();
using var requestObject = (JSObject)ObjectFactory.Call();

if (request.Options.TryGetValue(FetchOptions, out IDictionary<string, object>? fetchOptions))
{
Expand Down Expand Up @@ -200,7 +198,8 @@ async Task<HttpResponseMessage> Impl(HttpRequestMessage request, CancellationTok
// Process headers
// Cors has its own restrictions on headers.
// https://developer.mozilla.org/en-US/docs/Web/API/Headers
using (JSObject jsHeaders = new JSObject("Headers"))

using (JSObject jsHeaders = (JSObject)HeadersFactory.Call())
{
foreach (KeyValuePair<string, IEnumerable<string>> header in request.Headers)
{
Expand All @@ -223,7 +222,7 @@ async Task<HttpResponseMessage> Impl(HttpRequestMessage request, CancellationTok
}


JSObject abortController = new JSObject("AbortController");
JSObject abortController = (JSObject)AbortControllerFactory.Call();
using JSObject signal = (JSObject)abortController.GetObjectProperty("signal");
requestObject.SetObjectProperty("signal", signal);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
<Reference Include="System.Threading.Channels" Condition="'$(TargetPlatformIdentifier)' == 'Browser'" />
<Reference Include="System.Memory" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="$(LibrariesProjectRoot)System.Private.Runtime.InteropServices.JavaScript\src\System.Private.Runtime.InteropServices.JavaScript.csproj" Condition="'$(TargetPlatformIdentifier)' == 'Browser'" />
<ItemGroup Condition="'$(TargetPlatformIdentifier)' == 'Browser'">
<ProjectReference Include="$(LibrariesProjectRoot)System.Runtime.InteropServices.JavaScript\src\System.Runtime.InteropServices.JavaScript.csproj" SkipUseReferenceAssembly="true"/>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
using JavaScript = 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.Net.WebSockets
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

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

[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Runtime.InteropServices.JavaScript.Array))]
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Runtime.InteropServices.JavaScript.ArrayBuffer))]
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Runtime.InteropServices.JavaScript.DataView))]
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Runtime.InteropServices.JavaScript.Function))]
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Runtime.InteropServices.JavaScript.Uint8Array))]

[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Runtime.InteropServices.JavaScript.Runtime))]
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Runtime.InteropServices.JavaScript.JSObject))]
[assembly: System.Runtime.CompilerServices.TypeForwardedTo(typeof(System.Runtime.InteropServices.JavaScript.JSException))]
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,12 @@
<PropertyGroup>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<TargetFramework>$(NetCoreAppCurrent)-Browser</TargetFramework>
<NoWarn>$(NoWarn);CA1419</NoWarn> <!-- // TODO https://github.com/dotnet/roslyn-analyzers/issues/5232: not intended for use with P/Invoke -->
</PropertyGroup>
<ItemGroup>
<Compile Include="$(CommonPath)Interop\Browser\Interop.Runtime.cs" Link="Common\Interop\Browser\Interop.Runtime.cs" />
<Compile Include="System\Runtime\InteropServices\JavaScript\Runtime.cs" />
<Compile Include="System\Runtime\InteropServices\JavaScript\Runtime.CS.Owned.cs" />
<Compile Include="System\Runtime\InteropServices\JavaScript\Runtime.JS.Owned.cs" />
<Compile Include="System\Runtime\InteropServices\JavaScript\JSException.cs" />
<Compile Include="System\Runtime\InteropServices\JavaScript\JSObject.cs" />
<Compile Include="System\Runtime\InteropServices\JavaScript\DataView.cs" />
<Compile Include="System\Runtime\InteropServices\JavaScript\Uint8Array.cs" />
<Compile Include="System\Runtime\InteropServices\JavaScript\Array.cs" />
<Compile Include="System\Runtime\InteropServices\JavaScript\ArrayBuffer.cs" />
<Compile Include="System\Runtime\InteropServices\JavaScript\Function.cs" />
<Compile Include="System\Runtime\InteropServices\JavaScript\JSObject.References.cs" />
<Reference Include="System.Runtime" />
<ProjectReference Include="$(LibrariesProjectRoot)System.Runtime.InteropServices.JavaScript\src\System.Runtime.InteropServices.JavaScript.csproj" SkipUseReferenceAssembly="true"/>
</ItemGroup>
<ItemGroup>
<Reference Include="System.Collections" />
<Reference Include="System.Memory" />
<Reference Include="System.Net.Primitives" />
<Reference Include="System.Runtime" />
<Reference Include="System.Runtime.InteropServices" />
<Reference Include="System.Threading" />
<Compile Include="System.Private.Runtime.InteropServices.JavaScript.TypeForwards.cs" />
</ItemGroup>
</Project>

This file was deleted.

Loading