From c4f9d6fa06ee6fc8db5e6e1760936be6ba8a8e6e Mon Sep 17 00:00:00 2001 From: Ankit Jain Date: Wed, 29 Jun 2022 20:26:28 +0000 Subject: [PATCH 1/2] [wasm] InterpToNativeGenerator: use invariant culture when generating C .. functions. Fixes https://github.com/dotnet/runtime/issues/71149 --- .../WasmAppBuilder/InterpToNativeGenerator.cs | 2 +- .../Wasm.Build.Tests/BuildTestBase.cs | 12 ++++- .../PInvokeTableGeneratorTests.cs | 53 ++++++++++++++++++- 3 files changed, 63 insertions(+), 4 deletions(-) diff --git a/src/tasks/WasmAppBuilder/InterpToNativeGenerator.cs b/src/tasks/WasmAppBuilder/InterpToNativeGenerator.cs index e3859e451d0d4f..61d35321ffca48 100644 --- a/src/tasks/WasmAppBuilder/InterpToNativeGenerator.cs +++ b/src/tasks/WasmAppBuilder/InterpToNativeGenerator.cs @@ -174,7 +174,7 @@ public string Emit(char c) throw new InvalidSignatureCharException(c); } - return $"mono_wasm_interp_method_args_get_{char.ToLower(c)}arg (margs, {argIndex})"; + return $"mono_wasm_interp_method_args_get_{char.ToLower(c, CultureInfo.InvariantCulture)}arg (margs, {argIndex})"; } } } diff --git a/src/tests/BuildWasmApps/Wasm.Build.Tests/BuildTestBase.cs b/src/tests/BuildWasmApps/Wasm.Build.Tests/BuildTestBase.cs index 6c11e076aba594..aaadd0a654fee0 100644 --- a/src/tests/BuildWasmApps/Wasm.Build.Tests/BuildTestBase.cs +++ b/src/tests/BuildWasmApps/Wasm.Build.Tests/BuildTestBase.cs @@ -345,7 +345,14 @@ protected static BuildArgs ExpandBuildArgs(BuildArgs buildArgs, string extraProp (int exitCode, string buildOutput) result; try { - result = AssertBuild(sb.ToString(), id, expectSuccess: options.ExpectSuccess, envVars: s_buildEnv.EnvVars); + var envVars = s_buildEnv.EnvVars; + if (options.ExtraBuildEnvironmentVariables is not null) + { + envVars = new Dictionary(s_buildEnv.EnvVars); + foreach (var kvp in options.ExtraBuildEnvironmentVariables!) + envVars[kvp.Key] = kvp.Value; + } + result = AssertBuild(sb.ToString(), id, expectSuccess: options.ExpectSuccess, envVars: envVars); //AssertRuntimePackPath(result.buildOutput); @@ -898,7 +905,8 @@ public record BuildProjectOptions string? Verbosity = null, string? Label = null, string? TargetFramework = null, - string? MainJS = null + string? MainJS = null, + IDictionary? ExtraBuildEnvironmentVariables = null ); public record BlazorBuildOptions diff --git a/src/tests/BuildWasmApps/Wasm.Build.Tests/PInvokeTableGeneratorTests.cs b/src/tests/BuildWasmApps/Wasm.Build.Tests/PInvokeTableGeneratorTests.cs index 133ec47c710554..0250963d26b285 100644 --- a/src/tests/BuildWasmApps/Wasm.Build.Tests/PInvokeTableGeneratorTests.cs +++ b/src/tests/BuildWasmApps/Wasm.Build.Tests/PInvokeTableGeneratorTests.cs @@ -1,7 +1,7 @@ // 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.Collections.Generic; using System.IO; using Xunit; using Xunit.Abstractions; @@ -115,6 +115,57 @@ public static int Main() Assert.Contains("Main running", output); } + [Theory] + [BuildAndRun(host: RunHost.V8, parameters: new object[] { "tr_TR.UTF-8" })] + public void BuildNativeInNonEnglishCulture(BuildArgs buildArgs, string culture, RunHost host, string id) + { + // Check that we can generate interp tables in non-english cultures + // Prompted by https://github.com/dotnet/runtime/issues/71149 + + string code = @" + using System; + using System.Runtime.InteropServices; + + Console.WriteLine($""square: {square(5)}""); + return 42; + + [DllImport(""simple"")] static extern int square(int x); + "; + + buildArgs = ExpandBuildArgs(buildArgs, + extraItems: @$"", + extraProperties: buildArgs.AOT + ? string.Empty + : "true"); + + var extraEnvVars = new Dictionary { + { "LANG", culture }, + { "LC_ALL", culture }, + }; + + (_, string output) = BuildProject(buildArgs, + id: id, + new BuildProjectOptions( + InitProject: () => + { + File.WriteAllText(Path.Combine(_projectDir!, "Program.cs"), code); + File.Copy(Path.Combine(BuildEnvironment.TestAssetsPath, "native-libs", "simple.c"), + Path.Combine(_projectDir!, "simple.c")); + }, + Publish: buildArgs.AOT, + DotnetWasmFromRuntimePack: false, + ExtraBuildEnvironmentVariables: extraEnvVars + )); + + output = RunAndTestWasmApp(buildArgs, + buildDir: _projectDir, + expectedExitCode: 42, + host: host, + id: id, + envVars: extraEnvVars); + Assert.Contains("square: 25", output); + } + private (BuildArgs, string) BuildForVariadicFunctionTests(string programText, BuildArgs buildArgs, string id) { string filename = "variadic.o"; From 4d2ce5f8718605289c33402110ba695825c7ffed Mon Sep 17 00:00:00 2001 From: Ankit Jain Date: Wed, 29 Jun 2022 23:28:46 +0000 Subject: [PATCH 2/2] Add missing file --- src/tests/BuildWasmApps/testassets/native-libs/simple.c | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 src/tests/BuildWasmApps/testassets/native-libs/simple.c diff --git a/src/tests/BuildWasmApps/testassets/native-libs/simple.c b/src/tests/BuildWasmApps/testassets/native-libs/simple.c new file mode 100644 index 00000000000000..bc91595b3cda2e --- /dev/null +++ b/src/tests/BuildWasmApps/testassets/native-libs/simple.c @@ -0,0 +1,6 @@ +#include + +int square(int x) +{ + return x * x; +}