Skip to content

Commit ade21e5

Browse files
committed
wip
1 parent 2a47838 commit ade21e5

File tree

5 files changed

+88
-0
lines changed

5 files changed

+88
-0
lines changed

src/mono/browser/runtime/runtime.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,8 @@ init_icall_table (void)
197197
static void*
198198
get_native_to_interp (MonoMethod *method, void *extra_arg)
199199
{
200+
assert (method);
201+
200202
void *addr = NULL;
201203
MONO_ENTER_GC_UNSAFE;
202204
MonoClass *klass = mono_method_get_class (method);

src/mono/mono/mini/interp/interp.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3469,6 +3469,14 @@ interp_create_method_pointer (MonoMethod *method, gboolean compile, MonoError *e
34693469
if (method->wrapper_type == MONO_WRAPPER_NATIVE_TO_MANAGED) {
34703470
WrapperInfo *info = mono_marshal_get_wrapper_info (method);
34713471
MonoMethod *orig_method = info->d.native_to_managed.method;
3472+
if (!orig_method) {
3473+
char *s = mono_method_get_full_name (method);
3474+
char *msg = g_strdup_printf ("No native to managed transition for method '%s', missing [UnmanagedCallersOnly] attribute.", s);
3475+
mono_error_set_platform_not_supported (error, msg);
3476+
g_free (s);
3477+
g_free (msg);
3478+
return NULL;
3479+
}
34723480

34733481
/*
34743482
* These are called from native code. Ask the host app for a trampoline.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Threading.Tasks;
8+
using System.Text.RegularExpressions;
9+
using Xunit.Abstractions;
10+
using Xunit;
11+
12+
#nullable enable
13+
14+
namespace Wasm.Build.Tests;
15+
16+
public class MiscTests : WasmTemplateTestsBase
17+
{
18+
public MiscTests(ITestOutputHelper output, SharedBuildPerTestClassFixture buildContext)
19+
: base(output, buildContext)
20+
{
21+
}
22+
23+
[Fact]
24+
public async Task TestGetFunctionPointerForDelegate()
25+
{
26+
Configuration config = Configuration.Release;
27+
ProjectInfo info = CopyTestAsset(config, false, TestAsset.WasmBasicTestApp, "GetFunctionPointerForDelegate");
28+
29+
BuildProject(info, config, new BuildOptions(AssertAppBundle: false), isNativeBuild: false);
30+
31+
var result = await RunForBuildWithDotnetRun(new BrowserRunOptions(Configuration: config, TestScenario: "GetFunctionPointerForDelegate"));
32+
33+
Assert.Equal(2, result.TestOutput.Count);
34+
Assert.Contains(result.TestOutput, m => m.Contains("System.PlatformNotSupportedException"));
35+
Assert.Contains(result.TestOutput, m => m.Contains("No native to managed transition for method '(wrapper native-to-managed) void MiscTests/<>c:<TestGetFunctionPointerForDelegate>b__0_0 ()', missing [UnmanagedCallersOnly] attribute."));
36+
}
37+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
using System.Runtime.InteropServices;
6+
using System.Runtime.InteropServices.JavaScript;
7+
8+
public partial class MiscTests
9+
{
10+
[JSExport]
11+
public static unsafe int TestGetFunctionPointerForDelegate()
12+
{
13+
try
14+
{
15+
nint fptr = Marshal.GetFunctionPointerForDelegate(new Action(() => Console.WriteLine("Managed method callee")));
16+
((delegate* unmanaged<void>)fptr)();
17+
Console.WriteLine("No Exception");
18+
}
19+
catch (Exception ex)
20+
{
21+
Console.WriteLine("TestOutput -> " + ex.Message);
22+
Console.WriteLine("TestOutput -> " + ex.GetType().FullName);
23+
}
24+
25+
return 42;
26+
}
27+
}

src/mono/wasm/testassets/WasmBasicTestApp/App/wwwroot/main.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,8 @@ switch (testCase) {
166166
case "EnvVariablesTest":
167167
dotnet.withEnvironmentVariable("foo", "bar");
168168
break;
169+
case "GetFunctionPointerForDelegate":
170+
break;
169171
case "HttpNoStreamingTest":
170172
break;
171173
case "BrowserProfilerTest":
@@ -281,6 +283,18 @@ try {
281283

282284
exit(retHttp == 42 ? 0 : 1);
283285
break;
286+
case "GetFunctionPointerForDelegate":
287+
console.log("not ready yet")
288+
const miscExports = await getAssemblyExports(config.mainAssemblyName);
289+
const testGetFunctionPointerForDelegate = miscExports.MiscTests.TestGetFunctionPointerForDelegate;
290+
console.log("ready");
291+
292+
const retMics = testGetFunctionPointerForDelegate();
293+
document.getElementById("out").innerHTML = retMics;
294+
console.debug(`ret: ${retMics}`);
295+
296+
exit(retMics == 42 ? 0 : 1);
297+
break;
284298
case "EnvVariablesTest":
285299
console.log("not ready yet")
286300
const myExportsEnv = await getAssemblyExports(config.mainAssemblyName);

0 commit comments

Comments
 (0)