Skip to content

Commit 0076d37

Browse files
committed
Compiler: Use exported names for functions, or at least better names
This change makes the compiler use a function's first exported name (if any), or a name like `WasmFunction_N` where N is the function's index. This helps in diagnosing issues in compilation, compared to the prior code where function names were always new, random UUIDs.
1 parent 57a6bf1 commit 0076d37

File tree

4 files changed

+17
-11
lines changed

4 files changed

+17
-11
lines changed

Diff for: WasmNet.Core/ILGeneration/MonoCecilCompilationAssembly.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public MonoCecilCompilationAssembly()
3333
{
3434
var typeDefinition = new TypeDefinition(
3535
@namespace: assemblyName,
36-
name: $"Wasm{compilationType}Holder_{Id:N}",
36+
name: compilationType == CompilationType.Data ? "Data" : $"{compilationType}s",
3737
attributes: StaticClass,
3838
baseType: objectType);
3939

Diff for: WasmNet.Core/ILGeneration/ReflectionEmitCompilationAssembly.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ public ReflectionEmitCompilationAssembly()
2828
$"WasmModule_{Id:N}"
2929
);
3030

31-
_compilations[CompilationType.Function] = new(Module.DefineType($"WasmFunctionHolder_{Id:N}", StaticClass));
32-
_compilations[CompilationType.Global] = new(Module.DefineType($"WasmGlobalHolder_{Id:N}", StaticClass));
33-
_compilations[CompilationType.Data] = new(Module.DefineType($"WasmDataHolder_{Id:N}", StaticClass));
34-
_compilations[CompilationType.Element] = new(Module.DefineType($"WasmElementHolder_{Id:N}", StaticClass));
31+
foreach (var compilationType in Enum.GetValues<CompilationType>())
32+
{
33+
_compilations[compilationType] = new(Module.DefineType(compilationType == CompilationType.Data ? "Data" : $"{compilationType}s", StaticClass));
34+
}
3535
}
3636

3737
public Guid Id { get; } = Guid.NewGuid();

Diff for: WasmNet.Core/WasmFunctionInstance.cs

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace WasmNet.Core;
22

3-
public class WasmFunctionInstance(WasmType type, ModuleInstance module, WasmCode code)
3+
public class WasmFunctionInstance(int index, WasmType type, ModuleInstance module, WasmCode code)
44
: IFunctionInstance
55
{
66
public WasmType Type { get; } = type;
@@ -9,7 +9,11 @@ public class WasmFunctionInstance(WasmType type, ModuleInstance module, WasmCode
99

1010
public WasmCode Code { get; } = code;
1111

12-
public string EmitName { get; } = $"WasmFunction_{Guid.NewGuid():N}";
12+
public string EmitName { get; } = $"WasmFunction_{index}";
13+
14+
public string Name => module.Module.ExportSection?.Exports
15+
.FirstOrDefault(x => x.Index == index)
16+
?.Name ?? EmitName;
1317

1418
public Type ReturnType =>
1519
Type.Results.Count == 0
@@ -20,7 +24,7 @@ public class WasmFunctionInstance(WasmType type, ModuleInstance module, WasmCode
2024

2125
public object? Invoke(params object?[]? args)
2226
{
23-
var method = Module.CompilationAssembly.GetCompiledMethod(CompilationType.Function, EmitName);
27+
var method = Module.CompilationAssembly.GetCompiledMethod(CompilationType.Function, Name);
2428

2529
var argsWithModule = new[] { Module }
2630
.Concat(args ?? Array.Empty<object?>())

Diff for: WasmNet.Core/WasmRuntime.cs

+5-3
Original file line numberDiff line numberDiff line change
@@ -465,14 +465,16 @@ private void DeclareModuleFunctions(WasmModule module, ModuleInstance moduleInst
465465
var type = typeSection.Types[(int)func.FunctionSignatureIndex];
466466
var code = codeSection.Codes[i];
467467

468-
var funcInstance = new WasmFunctionInstance(type, moduleInstance, code);
468+
var funcInstance = new WasmFunctionInstance(i, type, moduleInstance, code);
469469

470470
var funcAddr = Store.AddFunction(funcInstance);
471471

472472
moduleInstance.AddFunctionAddress(funcAddr);
473+
474+
var funcExport = module.ExportSection?.Exports.FirstOrDefault(f => f.Index == i);
473475

474476
moduleInstance.CompilationAssembly.DeclareMethod(CompilationType.Function,
475-
funcInstance.EmitName,
477+
funcExport?.Name ?? funcInstance.EmitName,
476478
funcInstance.ReturnType,
477479
funcInstance.ParameterTypes);
478480
}
@@ -487,7 +489,7 @@ private void CompileFunctionBodies(ModuleInstance moduleInstance)
487489
if (func is WasmFunctionInstance wasmFunc)
488490
{
489491
moduleInstance.CompilationAssembly.CompileDeclaredMethod(moduleInstance,
490-
wasmFunc.EmitName,
492+
wasmFunc.Name,
491493
wasmFunc.Type,
492494
wasmFunc.Code);
493495
}

0 commit comments

Comments
 (0)