This repository has been archived by the owner on Nov 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 509
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Wasm: Add hard coded mappings for mono js interop compatibility (#8306)
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
Showing
5 changed files
with
153 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/System.Private.WebAssembly/System.Private.WebAssembly.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
25
src/System.Private.WebAssembly/System.Private.WebAssembly.sln
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |