Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/mono/wasm/templates/templates/console/Program.cs
Original file line number Diff line number Diff line change
@@ -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();
}
5 changes: 2 additions & 3 deletions src/mono/wasm/templates/templates/console/app-support.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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) {
Expand All @@ -190,4 +190,3 @@ try {
catch (err) {
set_exit_code(2, err);
}

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<OutputType>Exe</OutputType>
<PublishTrimmed>true</PublishTrimmed>
<WasmEmitSymbolMap Condition="'$(RunAOTCompilation)' != 'true'">true</WasmEmitSymbolMap>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup>
Expand Down
11 changes: 11 additions & 0 deletions src/mono/wasm/templates/templates/console/main.mjs
Original file line number Diff line number Diff line change
@@ -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);
}
33 changes: 19 additions & 14 deletions src/tests/BuildWasmApps/Wasm.Build.Tests/WasmTemplateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")]
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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, "<RunAOTCompilation>true</RunAOTCompilation>");

Expand Down