Skip to content

Commit

Permalink
More cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
lewing committed Sep 1, 2024
1 parent dcb69a8 commit ebccffa
Showing 1 changed file with 14 additions and 15 deletions.
29 changes: 14 additions & 15 deletions src/tasks/WasmAppBuilder/PInvokeTableGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ private string DelegateKey(PInvokeCallback export)
// it needs to match the key generated in get_native_to_interp
var method = export.Method;
string module_symbol = method.DeclaringType!.Module!.Assembly!.GetName()!.Name!;
return $"\"{_fixupSymbolName($"{module_symbol}_{method.DeclaringType.Namespace}_{method.DeclaringType.Name}_{method.Name}")}\"";
return $"\"{_fixupSymbolName($"{module_symbol}_{method.DeclaringType.Namespace}_{method.DeclaringType.Name}_{method.Name}_{method.GetParameters().Length}")}\"";
}

#pragma warning disable SYSLIB1045 // framework doesn't support GeneratedRegexAttribute
Expand Down Expand Up @@ -362,28 +362,27 @@ private void EmitNativeToInterp(StreamWriter w, List<PInvokeCallback> callbacks)
var interpArgs = new List<string>();
if (!is_void)
{
interpArgs.Add("(int*)&res");
interpArgs.Add("(int*)&result");
}
interpArgs.AddRange(unmanagedRange.Select(i => $"(int*)&arg{i}"));
interpArgs.Add($"wasm_native_to_interp_ftndescs [{cb_index}].arg");

w.Write(
$$"""
typedef void (*WasmInterpEntrySig_{{cb_index}}) ({{string.Join(", ", interpArgs.Select(_ => "int*"))}});
{{(attr ? $"__attribute__((export_name(\"{EscapeLiteral(cb.EntryPoint!)}\")))" : "// no export name defined")}}
{{MapType(method.ReturnType)}}
{{cb.EntryName}} ({{string.Join(", ", unmanagedRange.Select(i => $"{MapType(parameters[i].ParameterType)} arg{i}"))}}) {
typedef void (*InterpEntry_T{{cb_index}}) ({{string.Join(", ", interpArgs.Select(_ => "int*"))}});
{{(!is_void ? $"{MapType(method.ReturnType)} result;" : "// void result")}}
{{(attr ? $"__attribute__((export_name(\"{EscapeLiteral(cb.EntryPoint!)}\")" : "// no export name defined")}}
{{MapType(method.ReturnType)}} {{cb.EntryName}} ({{string.Join(", ", unmanagedRange.Select(i => $"{MapType(parameters[i].ParameterType)} arg{i}"))}}) {
{{(!is_void ? $"{MapType(method.ReturnType)} res;" : "")}}
// In case when null force interpreter to initialize the pointers
if (!(WasmInterpEntrySig_{{cb_index}})wasm_native_to_interp_ftndescs [{{cb_index}}].func) {
{{(attr ? "initialize_runtime();" : "// no defined entrypoint so no need to initialize runtime")}}
mono_wasm_marshal_get_managed_wrapper ("{{type!.Module!.Assembly!.GetName()!.Name!}}","{{type.Namespace}}", "{{type.Name}}", "{{cb.Method.Name}}", {{numParams}});
if (!(InterpEntry_T{{cb_index}})wasm_native_to_interp_ftndescs [{{cb_index}}].func) {
{{(attr ? "initialize_runtime();" : "")}} // ensure the ftndescs and runtime are initialized when required
mono_wasm_marshal_get_managed_wrapper ("{{type!.Module!.Assembly!.GetName()!.Name!}}", "{{type.Namespace}}", "{{type.Name}}", "{{cb.Method.Name}}", {{numParams}});
}
((WasmInterpEntrySig_{{cb_index}})wasm_native_to_interp_ftndescs [{{cb_index}}].func) ({{string.Join(", ", interpArgs)}});
{{(!is_void ? "return res;" : "")}}
((InterpEntry_T{{cb_index}})wasm_native_to_interp_ftndescs [{{cb_index}}].func) ({{string.Join(", ", interpArgs)}});
{{(!is_void ? "return result;" : "// void result")}}
}
""");
Expand All @@ -394,12 +393,12 @@ private void EmitNativeToInterp(StreamWriter w, List<PInvokeCallback> callbacks)
$$"""
static void *wasm_native_to_interp_funcs[] = {
{{string.Join(", ", callbacks.Select(cb => cb.EntryName))}}
{{string.Join($",{Environment.NewLine} ", callbacks.Select(cb => cb.EntryName))}}
};
// these strings need to match the keys generated in get_native_to_interp
static const char *wasm_native_to_interp_map[] = {
{{string.Join(", ", callbacks.Select(DelegateKey))}}
{{string.Join($",{Environment.NewLine} ", callbacks.Select(DelegateKey))}}
};
""");
Expand Down

0 comments on commit ebccffa

Please sign in to comment.