Skip to content
This repository has been archived by the owner on Nov 1, 2020. It is now read-only.

Commit

Permalink
Wasm: Add hard coded mappings for mono js interop compatibility (#8306)
Browse files Browse the repository at this point in the history
his PR provides hard coded mappings to a new assembly to enable CoreRT to be a drop in replacement regards the JS interop for mono based frameworks. It follow the discussion at #8303

closes: #8303
  • Loading branch information
yowl authored Sep 15, 2020
1 parent a892bba commit a7ff563
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/BuildIntegration/WebAssembly/dotnet_support.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@

var DotNetSupportLib = {
$DOTNET: {
_dotnet_get_global: function () {
function testGlobal(obj) {
obj['___dotnet_global___'] = obj;
var success = typeof ___dotnet_global___ === 'object' && obj['___dotnet_global___'] === obj;
if (!success) {
delete obj['___dotnet_global___'];
}
return success;
}
if (typeof ___dotnet_global___ === 'object') {
return ___dotnet_global___;
}
if (typeof global === 'object' && testGlobal(global)) {
___dotnet_global___ = global;
} else if (typeof window === 'object' && testGlobal(window)) {
___dotnet_global___ = window;
}
if (typeof ___dotnet_global___ === 'object') {
return ___dotnet_global___;
}
throw Error('unable to get DotNet global object.');
},

},

corert_wasm_invoke_js: function (js, length, exception) {
var jsFuncName = UTF8ToString(js, length);
var res = eval(jsFuncName);
exception = 0;
return "" + res;
},

corert_wasm_invoke_js_unmarshalled: function (js, length, arg0, arg1, arg2, exception) {

var jsFuncName = UTF8ToString(js, length);
var dotNetExports = DOTNET._dotnet_get_global().DotNet;
if (!dotNetExports) {
throw new Error('The Microsoft.JSInterop.js library is not loaded.');
}
var funcInstance = dotNetExports.jsCallDispatcher.findJSFunction(jsFuncName);

return funcInstance.call(null, arg0, arg1, arg2);
},

};

autoAddDeps(DotNetSupportLib, '$DOTNET');
mergeInto(LibraryManager.library, DotNetSupportLib);



15 changes: 15 additions & 0 deletions src/ILCompiler.WebAssembly/src/CodeGen/ILToWebAssemblyImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1869,6 +1869,21 @@ private void ImportCall(ILOpcode opcode, int token)
}
}

// Hard coded InternalCall mappings for mono interoperability
if (callee.IsInternalCall)
{
var metadataType = callee.OwningType as MetadataType;
// See https://github.com/dotnet/runtime/blob/9ba9a300a08170c8170ea52981810f41fad68cf0/src/mono/wasm/runtime/driver.c#L400-L407
// Mono have these InternalCall methods in different namespaces but just mapping them to System.Private.WebAssembly.
if (metadataType != null && (metadataType.Namespace == "WebAssembly.JSInterop" && metadataType.Name == "InternalCalls" || metadataType.Namespace == "WebAssembly" && metadataType.Name == "Runtime"))
{
var coreRtJsInternalCallsType = _compilation.TypeSystemContext
.GetModuleForSimpleName("System.Private.WebAssembly")
.GetKnownType("System.Private.WebAssembly", "InternalCalls");
callee = coreRtJsInternalCallsType.GetMethod(callee.Name, callee.Signature);
}
}

if (callee.IsRawPInvoke() || (callee.IsInternalCall && callee.HasCustomAttribute("System.Runtime", "RuntimeImportAttribute")))
{
ImportRawPInvoke(callee);
Expand Down
51 changes: 51 additions & 0 deletions src/System.Private.WebAssembly/InternalCalls.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// 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.Runtime.InteropServices;

namespace System.Private.WebAssembly
{
// Provides end points for the hard coded mappings in ILToWebAssemblyImporter.cs that provide a drop in replacement for mono InternalCall methods. There are 4 :
// https://github.com/dotnet/runtime/blob/9ba9a300a08170c8170ea52981810f41fad68cf0/src/mono/wasm/runtime/driver.c#L400-L407
// Have only done what has been hit so far in trying to bring up Uno Platform
internal static class InternalCalls
{
//Uno compatibility
[DllImport("*", EntryPoint = "corert_wasm_invoke_js")]
private static extern string InvokeJSInternal(string js, int length, out int exception);

public static string InvokeJS(string js, out int exception)
{
return InvokeJSInternal(js, js.Length, out exception);
}

[DllImport("*", EntryPoint = "corert_wasm_invoke_js_unmarshalled")]
internal static extern IntPtr InvokeJSUnmarshalledInternal(string js, int length, IntPtr p1, IntPtr p2, IntPtr p3, out string exception);

// Matches this signature:
// https://github.com/mono/mono/blob/f24d652d567c4611f9b4e3095be4e2a1a2ab23a4/sdks/wasm/driver.c#L21
public static IntPtr InvokeJSUnmarshalled(out string exception, string js, IntPtr p1, IntPtr p2, IntPtr p3)
{
// convention : if the methodId is known, then js is null and p1 is the method id
return System.Private.WebAssembly.InternalCalls.InvokeJSUnmarshalledInternal(js, js?.Length ?? 0, p1, p2, p3, out exception);
}

//TODO:
//mono_add_internal_call ("WebAssembly.JSInterop.InternalCalls::InvokeJS", mono_wasm_invoke_js_blazor); // blazor specific
//mono_add_internal_call("WebAssembly.JSInterop.InternalCalls::InvokeJSMarshalled", mono_wasm_invoke_js_marshalled);
}
}

namespace System.Runtime
{
[AttributeUsage(AttributeTargets.Method, Inherited = false)]
internal sealed class RuntimeExportAttribute : Attribute
{
public string EntryPoint;

public RuntimeExportAttribute(string entry)
{
EntryPoint = entry;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>8</LangVersion>
</PropertyGroup>

</Project>
25 changes: 25 additions & 0 deletions src/System.Private.WebAssembly/System.Private.WebAssembly.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30413.136
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "System.Private.WebAssembly", "System.Private.WebAssembly.csproj", "{A47FF380-20A6-4A59-8B92-E8C9E73B13D2}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{A47FF380-20A6-4A59-8B92-E8C9E73B13D2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A47FF380-20A6-4A59-8B92-E8C9E73B13D2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A47FF380-20A6-4A59-8B92-E8C9E73B13D2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A47FF380-20A6-4A59-8B92-E8C9E73B13D2}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {79A9162D-3CA7-402A-B9D8-3CF1FEC8A2AA}
EndGlobalSection
EndGlobal

0 comments on commit a7ff563

Please sign in to comment.