Skip to content

Commit

Permalink
[wasi][mono] Fix conflicting naming for wasm import/export (#103881)
Browse files Browse the repository at this point in the history
Add namespace for entrypoint
  • Loading branch information
mkhamoyan authored Jun 26, 2024
1 parent f88ba03 commit ce02b53
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/mono/browser/runtime/runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -370,13 +370,13 @@ mono_wasm_assembly_find_method (MonoClass *klass, const char *name, int argument
* This wrapper ensures that the interpreter initializes the pointers.
*/
void
mono_wasm_marshal_get_managed_wrapper (const char* assemblyName, const char* typeName, const char* methodName, int num_params)
mono_wasm_marshal_get_managed_wrapper (const char* assemblyName, const char* namespaceName, const char* typeName, const char* methodName, int num_params)
{
MonoError error;
mono_error_init (&error);
MonoAssembly* assembly = mono_wasm_assembly_load (assemblyName);
assert (assembly);
MonoClass* class = mono_wasm_assembly_find_class (assembly, "", typeName);
MonoClass* class = mono_wasm_assembly_find_class (assembly, namespaceName, typeName);
assert (class);
MonoMethod* method = mono_wasm_assembly_find_method (class, methodName, num_params);
assert (method);
Expand Down
2 changes: 1 addition & 1 deletion src/mono/browser/runtime/runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ MonoDomain *mono_wasm_load_runtime_common (int debug_level, MonoLogCallback log_
MonoAssembly *mono_wasm_assembly_load (const char *name);
MonoClass *mono_wasm_assembly_find_class (MonoAssembly *assembly, const char *namespace, const char *name);
MonoMethod *mono_wasm_assembly_find_method (MonoClass *klass, const char *name, int arguments);
void mono_wasm_marshal_get_managed_wrapper (const char* assemblyName, const char* typeName, const char* methodName, int num_params);
void mono_wasm_marshal_get_managed_wrapper (const char* assemblyName, const char* namespaceName, const char* typeName, const char* methodName, int num_params);
int initialize_runtime ();

#endif
17 changes: 12 additions & 5 deletions src/tasks/WasmAppBuilder/PInvokeTableGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ private string CEntryPoint(PInvoke pinvoke)
if (pinvoke.WasmLinkage)
{
// We mangle the name to avoid collisions with symbols in other modules
return _fixupSymbolName($"{pinvoke.Module}#{pinvoke.EntryPoint}");
string namespaceName = pinvoke.Method.DeclaringType?.Namespace ?? string.Empty;
return _fixupSymbolName($"{namespaceName}#{pinvoke.Module}#{pinvoke.EntryPoint}");
}
return _fixupSymbolName(pinvoke.EntryPoint);
}
Expand Down Expand Up @@ -295,8 +296,13 @@ private string CEntryPoint(PInvokeCallback export)
}

var method = export.Method;
// EntryPoint wasn't specified generate a name for the entry point
return _fixupSymbolName($"wasm_native_to_interp_{method.DeclaringType!.Module!.Assembly!.GetName()!.Name!}_{method.DeclaringType.Name}_{method.Name}");
string namespaceName = method.DeclaringType?.Namespace ?? string.Empty;
string assemblyName = method.DeclaringType?.Module?.Assembly?.GetName()?.Name ?? string.Empty;
string declaringTypeName = method.DeclaringType?.Name ?? string.Empty;

string entryPoint = $"wasm_native_to_interp_{namespaceName}_{assemblyName}_{declaringTypeName}_{method.Name}";

return _fixupSymbolName(entryPoint);
}

private string DelegateKey(PInvokeCallback export)
Expand Down Expand Up @@ -390,10 +396,11 @@ private void EmitNativeToInterp(StreamWriter w, List<PInvokeCallback> callbacks)
sb.Append($" if (!(WasmInterpEntrySig_{cb_index})wasm_native_to_interp_ftndescs [{cb_index}].func) {{\n");
var assemblyFullName = cb.Method.DeclaringType == null ? "" : cb.Method.DeclaringType.Assembly.FullName;
var assemblyName = assemblyFullName != null && assemblyFullName.Split(',').Length > 0 ? assemblyFullName.Split(',')[0].Trim() : "";
var typeName = cb.Method.DeclaringType == null || cb.Method.DeclaringType.FullName == null ? "" : cb.Method.DeclaringType.FullName;
var namespaceName = cb.Method.DeclaringType == null ? "" : cb.Method.DeclaringType.Namespace;
var typeName = cb.Method.DeclaringType == null || cb.Method.DeclaringType.Name == null ? "" : cb.Method.DeclaringType.Name;
var methodName = cb.Method.Name;
int numParams = method.GetParameters().Length;
sb.Append($" mono_wasm_marshal_get_managed_wrapper (\"{assemblyName}\", \"{typeName}\", \"{methodName}\", {numParams});\n");
sb.Append($" mono_wasm_marshal_get_managed_wrapper (\"{assemblyName}\",\"{namespaceName}\", \"{typeName}\", \"{methodName}\", {numParams});\n");
sb.Append($" }}\n");

sb.Append($" ((WasmInterpEntrySig_{cb_index})wasm_native_to_interp_ftndescs [{cb_index}].func) (");
Expand Down

0 comments on commit ce02b53

Please sign in to comment.