diff --git a/src/mono/wasm/templates/templates/console/Program.cs b/src/mono/wasm/templates/templates/console/Program.cs index 87607cda6afc59..edd4616471ece2 100644 --- a/src/mono/wasm/templates/templates/console/Program.cs +++ b/src/mono/wasm/templates/templates/console/Program.cs @@ -1,3 +1,17 @@ using System; +using System.Runtime.InteropServices.JavaScript; Console.WriteLine("Hello, Console!"); + +public partial class MyClass +{ + [JSExport] + internal static string Greeting() + { + var text = $"Hello, World! Greetings from node version: {GetNodeVersion()}"; + return text; + } + + [JSImport("node.process.version")] + internal static partial string GetNodeVersion(); +} diff --git a/src/mono/wasm/templates/templates/console/app-support.mjs b/src/mono/wasm/templates/templates/console/app-support.mjs index e072bda4122e55..6affa5b5448afa 100644 --- a/src/mono/wasm/templates/templates/console/app-support.mjs +++ b/src/mono/wasm/templates/templates/console/app-support.mjs @@ -131,7 +131,7 @@ try { initRunArgs(); mergeArguments(); - createDotnetRuntime(({ MONO, INTERNAL, BINDING, Module }) => ({ + createDotnetRuntime(({ MONO, INTERNAL, BINDING, IMPORTS, Module }) => ({ disableDotnet6Compatibility: true, config: null, configSrc: "./mono-config.json", @@ -166,7 +166,7 @@ try { if (runArgs.runtimeArgs.length > 0) INTERNAL.mono_wasm_set_runtime_options(runArgs.runtimeArgs); - Object.assign(App, { MONO, BINDING, Module, runArgs }); + Object.assign(App, { MONO, BINDING, IMPORTS, Module, runArgs }); try { if (App.main) { @@ -190,4 +190,3 @@ try { catch (err) { set_exit_code(2, err); } - diff --git a/src/mono/wasm/templates/templates/console/console.0.csproj b/src/mono/wasm/templates/templates/console/console.0.csproj index bff281b3d7e5b6..b2ebf81c77a68c 100644 --- a/src/mono/wasm/templates/templates/console/console.0.csproj +++ b/src/mono/wasm/templates/templates/console/console.0.csproj @@ -9,6 +9,7 @@ Exe true true + true diff --git a/src/mono/wasm/templates/templates/console/main.mjs b/src/mono/wasm/templates/templates/console/main.mjs index f6c65b9a8082b5..cd90d5b0fc79fe 100644 --- a/src/mono/wasm/templates/templates/console/main.mjs +++ b/src/mono/wasm/templates/templates/console/main.mjs @@ -1,5 +1,16 @@ import { App } from './app-support.mjs' App.main = async function (applicationArguments) { + + App.IMPORTS.node = { + process : { + version: () => globalThis.process.version + } + }; + + const exports = await App.MONO.mono_wasm_get_assembly_exports("console.0.dll"); + const text = exports.MyClass.Greeting(); + console.log(text); + await App.MONO.mono_run_main("console.0.dll", applicationArguments); } diff --git a/src/tests/BuildWasmApps/Wasm.Build.Tests/WasmTemplateTests.cs b/src/tests/BuildWasmApps/Wasm.Build.Tests/WasmTemplateTests.cs index 2ca8daca7c449b..e3edd4d60cd82c 100644 --- a/src/tests/BuildWasmApps/Wasm.Build.Tests/WasmTemplateTests.cs +++ b/src/tests/BuildWasmApps/Wasm.Build.Tests/WasmTemplateTests.cs @@ -3,11 +3,14 @@ using System; using System.IO; +using System.Text; using System.Threading.Tasks; +using System.Text.RegularExpressions; using Xunit; using Xunit.Abstractions; using Xunit.Sdk; + #nullable enable namespace Wasm.Build.Tests @@ -19,6 +22,19 @@ public WasmTemplateTests(ITestOutputHelper output, SharedBuildPerTestClassFixtur { } + private void updateProgramCS() { + string programText = """ + Console.WriteLine("Hello, Console!"); + + for (int i = 0; i < args.Length; i ++) + Console.WriteLine ($"args[{i}] = {args[i]}"); + """; + var path = Path.Combine(_projectDir!, "Program.cs"); + string text = File.ReadAllText(path); + text = text.Replace(@"Console.WriteLine(""Hello, Console!"");", programText); + File.WriteAllText(path, text); + } + [Theory] [InlineData("Debug")] [InlineData("Release")] @@ -127,13 +143,7 @@ public void ConsoleBuildAndRun(string config) string projectFile = CreateWasmTemplateProject(id, "wasmconsole"); string projectName = Path.GetFileNameWithoutExtension(projectFile); - string programText = """ - using System; - - for (int i = 0; i < args.Length; i ++) - Console.WriteLine ($"args[{i}] = {args[i]}"); - """; - File.WriteAllText(Path.Combine(_projectDir!, "Program.cs"), programText); + updateProgramCS(); var buildArgs = new BuildArgs(projectName, config, false, id, null); buildArgs = ExpandBuildArgs(buildArgs); @@ -169,13 +179,8 @@ public void ConsolePublishAndRun(string config, bool aot) string projectFile = CreateWasmTemplateProject(id, "wasmconsole"); string projectName = Path.GetFileNameWithoutExtension(projectFile); - string programText = """ - using System; - - for (int i = 0; i < args.Length; i ++) - Console.WriteLine ($"args[{i}] = {args[i]}"); - """; - File.WriteAllText(Path.Combine(_projectDir!, "Program.cs"), programText); + updateProgramCS(); + if (aot) AddItemsPropertiesToProject(projectFile, "true");