Skip to content

Commit

Permalink
[webcil] Minor cleanups (dotnet#81009)
Browse files Browse the repository at this point in the history
* replace magic constants by strlen(LITERAL)

   Let the C compiler do the math

* Use CopyIfDifferent for webcil converter in WasmAppBuilder

* WasmAppHost: serve .webcil as application/octet-stream

* Add mime types for webcil in two more places

* Always add to _fileWrites

   But log whether we actually copied anything or not

Co-authored-by: Ankit Jain <[email protected]>
  • Loading branch information
2 people authored and mdh1418 committed Jan 24, 2023
1 parent f5882dd commit 5dea376
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/mono/mono/metadata/assembly.c
Original file line number Diff line number Diff line change
Expand Up @@ -1460,8 +1460,8 @@ bundled_assembly_match (const char *bundled_name, const char *name)
return TRUE;
/* if they want a .dll and we have the matching .webcil, return it */
if (g_str_has_suffix (bundled_name, ".webcil") && g_str_has_suffix (name, ".dll")) {
size_t bprefix = strlen (bundled_name) - 7;
size_t nprefix = strlen (name) - 4;
size_t bprefix = strlen (bundled_name) - strlen (".webcil");
size_t nprefix = strlen (name) - strlen (".dll");
if (bprefix == nprefix && strncmp (bundled_name, name, bprefix) == 0)
return TRUE;
}
Expand Down
2 changes: 1 addition & 1 deletion src/mono/mono/metadata/mono-debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -1105,7 +1105,7 @@ bsymfile_match (BundledSymfile *bsymfile, const char *assembly_name)
#ifdef ENABLE_WEBCIL
const char *p = strstr (assembly_name, ".webcil");
/* if assembly_name ends with .webcil, check if aname matches, with a .dll extension instead */
if (p && *(p + 7) == 0) {
if (p && *(p + strlen(".webcil")) == 0) {
size_t n = p - assembly_name;
if (!strncmp (bsymfile->aname, assembly_name, n)
&& !strcmp (bsymfile->aname + n, ".dll"))
Expand Down
7 changes: 5 additions & 2 deletions src/mono/mono/mini/monovm.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,11 @@ mono_core_preload_hook (MonoAssemblyLoadContext *alc, MonoAssemblyName *aname, c
#ifdef ENABLE_WEBCIL
else {
/* /path/foo.dll -> /path/foo.webcil */
size_t n = strlen (fullpath) - 4;
char *fullpath2 = g_malloc (n + 8);
size_t n = strlen (fullpath);
if (n < strlen(".dll"))
continue;
n -= strlen(".dll");
char *fullpath2 = g_malloc (n + strlen(".webcil") + 1);
g_strlcpy (fullpath2, fullpath, n + 1);
g_strlcpy (fullpath2 + n, ".webcil", 8);
if (g_file_test (fullpath2, G_FILE_TEST_IS_REGULAR)) {
Expand Down
1 change: 1 addition & 0 deletions src/mono/sample/wasm/Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<_ServeMimeTypes>$(_ServeMimeTypes) --mime .mjs=text/javascript</_ServeMimeTypes>
<_ServeMimeTypes>$(_ServeMimeTypes) --mime .cjs=text/javascript</_ServeMimeTypes>
<_ServeMimeTypes>$(_ServeMimeTypes) --mime .js=text/javascript</_ServeMimeTypes>
<_ServeMimeTypes>$(_ServeMimeTypes) --mime .webcil=application/octet-stream</_ServeMimeTypes>
</PropertyGroup>

<Target Name="BuildSampleInTree"
Expand Down
2 changes: 2 additions & 0 deletions src/mono/sample/wasm/simple-server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ private async void ServeAsync(HttpListenerContext context)
string? contentType = null;
if (path.EndsWith(".wasm"))
contentType = "application/wasm";
if (path.EndsWith(".webcil"))
contentType = "application/octet-stream";
if (path.EndsWith(".json"))
contentType = "application/json";
if (path.EndsWith(".js") || path.EndsWith(".mjs") || path.EndsWith(".cjs"))
Expand Down
2 changes: 1 addition & 1 deletion src/mono/wasm/host/WebServerStartup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public void Configure(IApplicationBuilder app,
provider.Mappings[".cjs"] = "text/javascript";
provider.Mappings[".mjs"] = "text/javascript";

foreach (string extn in new string[] { ".dll", ".pdb", ".dat", ".blat" })
foreach (string extn in new string[] { ".dll", ".pdb", ".dat", ".blat", ".webcil" })
{
provider.Mappings[extn] = "application/octet-stream";
}
Expand Down
18 changes: 13 additions & 5 deletions src/tasks/WasmAppBuilder/WasmAppBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,12 @@ private bool ExecuteInternal ()
var tmpWebcil = Path.GetTempFileName();
var webcilWriter = Microsoft.WebAssembly.Build.Tasks.WebcilConverter.FromPortableExecutable(inputPath: assembly, outputPath: tmpWebcil, logger: Log);
webcilWriter.ConvertToWebcil();
var finalWebcil = Path.ChangeExtension(assembly, ".webcil");
FileCopyChecked(tmpWebcil, Path.Combine(asmRootPath, Path.GetFileName(finalWebcil)), "Assemblies");
var finalWebcil = Path.Combine(asmRootPath, Path.ChangeExtension(Path.GetFileName(assembly), ".webcil"));
if (Utils.CopyIfDifferent(tmpWebcil, finalWebcil, useHash: true))
Log.LogMessage(MessageImportance.Low, $"Generated {finalWebcil} .");
else
Log.LogMessage(MessageImportance.Low, $"Skipped generating {finalWebcil} as the contents are unchanged.");
_fileWrites.Add(finalWebcil);
}
else
{
Expand Down Expand Up @@ -283,9 +287,13 @@ private bool ExecuteInternal ()
var tmpWebcil = Path.GetTempFileName();
var webcilWriter = Microsoft.WebAssembly.Build.Tasks.WebcilConverter.FromPortableExecutable(inputPath: fullPath, outputPath: tmpWebcil, logger: Log);
webcilWriter.ConvertToWebcil();
var finalWebcil = Path.ChangeExtension(name, ".webcil");
FileCopyChecked(tmpWebcil, Path.Combine(directory, finalWebcil), "SatelliteAssemblies");
config.Assets.Add(new SatelliteAssemblyEntry(finalWebcil, culture));
var finalWebcil = Path.Combine(directory, Path.ChangeExtension(name, ".webcil"));
if (Utils.CopyIfDifferent(tmpWebcil, finalWebcil, useHash: true))
Log.LogMessage(MessageImportance.Low, $"Generated {finalWebcil} .");
else
Log.LogMessage(MessageImportance.Low, $"Skipped generating {finalWebcil} as the contents are unchanged.");
_fileWrites.Add(finalWebcil);
config.Assets.Add(new SatelliteAssemblyEntry(Path.GetFileName(finalWebcil), culture));
}
else
{
Expand Down

0 comments on commit 5dea376

Please sign in to comment.