Skip to content

Commit 70999d8

Browse files
alinpahontu2912Stefan Pahontu
andauthored
[wasm] add new interop to the console template (#72375)
* first try for wasm console template * removed unnecessary references * modified initial .csproj file * Delete console.csproj * solved formatting and fixed failing test * solved failing tests * fixed styling * simplified updating of Program.cs * removed unnecessary files * used string.replace instead of regex Co-authored-by: Stefan Pahontu <[email protected]>
1 parent 40f62b6 commit 70999d8

File tree

5 files changed

+47
-17
lines changed

5 files changed

+47
-17
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,17 @@
11
using System;
2+
using System.Runtime.InteropServices.JavaScript;
23

34
Console.WriteLine("Hello, Console!");
5+
6+
public partial class MyClass
7+
{
8+
[JSExport]
9+
internal static string Greeting()
10+
{
11+
var text = $"Hello, World! Greetings from node version: {GetNodeVersion()}";
12+
return text;
13+
}
14+
15+
[JSImport("node.process.version")]
16+
internal static partial string GetNodeVersion();
17+
}

src/mono/wasm/templates/templates/console/app-support.mjs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ try {
131131
initRunArgs();
132132
mergeArguments();
133133

134-
createDotnetRuntime(({ MONO, INTERNAL, BINDING, Module }) => ({
134+
createDotnetRuntime(({ MONO, INTERNAL, BINDING, IMPORTS, Module }) => ({
135135
disableDotnet6Compatibility: true,
136136
config: null,
137137
configSrc: "./mono-config.json",
@@ -166,7 +166,7 @@ try {
166166
if (runArgs.runtimeArgs.length > 0)
167167
INTERNAL.mono_wasm_set_runtime_options(runArgs.runtimeArgs);
168168

169-
Object.assign(App, { MONO, BINDING, Module, runArgs });
169+
Object.assign(App, { MONO, BINDING, IMPORTS, Module, runArgs });
170170

171171
try {
172172
if (App.main) {
@@ -190,4 +190,3 @@ try {
190190
catch (err) {
191191
set_exit_code(2, err);
192192
}
193-

src/mono/wasm/templates/templates/console/console.0.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<OutputType>Exe</OutputType>
1010
<PublishTrimmed>true</PublishTrimmed>
1111
<WasmEmitSymbolMap Condition="'$(RunAOTCompilation)' != 'true'">true</WasmEmitSymbolMap>
12+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
1213
</PropertyGroup>
1314

1415
<ItemGroup>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
import { App } from './app-support.mjs'
22

33
App.main = async function (applicationArguments) {
4+
5+
App.IMPORTS.node = {
6+
process : {
7+
version: () => globalThis.process.version
8+
}
9+
};
10+
11+
const exports = await App.MONO.mono_wasm_get_assembly_exports("console.0.dll");
12+
const text = exports.MyClass.Greeting();
13+
console.log(text);
14+
415
await App.MONO.mono_run_main("console.0.dll", applicationArguments);
516
}

src/tests/BuildWasmApps/Wasm.Build.Tests/WasmTemplateTests.cs

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33

44
using System;
55
using System.IO;
6+
using System.Text;
67
using System.Threading.Tasks;
8+
using System.Text.RegularExpressions;
79
using Xunit;
810
using Xunit.Abstractions;
911
using Xunit.Sdk;
1012

13+
1114
#nullable enable
1215

1316
namespace Wasm.Build.Tests
@@ -19,6 +22,19 @@ public WasmTemplateTests(ITestOutputHelper output, SharedBuildPerTestClassFixtur
1922
{
2023
}
2124

25+
private void updateProgramCS() {
26+
string programText = """
27+
Console.WriteLine("Hello, Console!");
28+
29+
for (int i = 0; i < args.Length; i ++)
30+
Console.WriteLine ($"args[{i}] = {args[i]}");
31+
""";
32+
var path = Path.Combine(_projectDir!, "Program.cs");
33+
string text = File.ReadAllText(path);
34+
text = text.Replace(@"Console.WriteLine(""Hello, Console!"");", programText);
35+
File.WriteAllText(path, text);
36+
}
37+
2238
[Theory]
2339
[InlineData("Debug")]
2440
[InlineData("Release")]
@@ -127,13 +143,7 @@ public void ConsoleBuildAndRun(string config)
127143
string projectFile = CreateWasmTemplateProject(id, "wasmconsole");
128144
string projectName = Path.GetFileNameWithoutExtension(projectFile);
129145

130-
string programText = """
131-
using System;
132-
133-
for (int i = 0; i < args.Length; i ++)
134-
Console.WriteLine ($"args[{i}] = {args[i]}");
135-
""";
136-
File.WriteAllText(Path.Combine(_projectDir!, "Program.cs"), programText);
146+
updateProgramCS();
137147

138148
var buildArgs = new BuildArgs(projectName, config, false, id, null);
139149
buildArgs = ExpandBuildArgs(buildArgs);
@@ -169,13 +179,8 @@ public void ConsolePublishAndRun(string config, bool aot)
169179
string projectFile = CreateWasmTemplateProject(id, "wasmconsole");
170180
string projectName = Path.GetFileNameWithoutExtension(projectFile);
171181

172-
string programText = """
173-
using System;
174-
175-
for (int i = 0; i < args.Length; i ++)
176-
Console.WriteLine ($"args[{i}] = {args[i]}");
177-
""";
178-
File.WriteAllText(Path.Combine(_projectDir!, "Program.cs"), programText);
182+
updateProgramCS();
183+
179184
if (aot)
180185
AddItemsPropertiesToProject(projectFile, "<RunAOTCompilation>true</RunAOTCompilation>");
181186

0 commit comments

Comments
 (0)