From 259936d8d1835c47d3817a0de5db760b36863f40 Mon Sep 17 00:00:00 2001 From: Jason Ginchereau Date: Mon, 19 Aug 2024 07:02:58 -1000 Subject: [PATCH] Fix generator bugs with un-namespaced types (#360) --- src/NodeApi.DotNetHost/JSMarshaller.cs | 15 +++++++++------ src/NodeApi.Generator/ExpressionExtensions.cs | 3 ++- src/NodeApi.Generator/ModuleGenerator.cs | 7 ++++++- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/NodeApi.DotNetHost/JSMarshaller.cs b/src/NodeApi.DotNetHost/JSMarshaller.cs index babe1059..00edec22 100644 --- a/src/NodeApi.DotNetHost/JSMarshaller.cs +++ b/src/NodeApi.DotNetHost/JSMarshaller.cs @@ -577,8 +577,9 @@ public LambdaExpression BuildToJSMethodExpression(MethodInfo method) string name = method.Name; if (method.DeclaringType!.IsInterface) { - name = method.DeclaringType.Namespace + '.' + - method.DeclaringType.Name + '.' + name; + string nsPrefix = method.DeclaringType.Namespace != null ? + method.DeclaringType.Namespace + '.' : string.Empty; + name = nsPrefix + method.DeclaringType.Name + '.' + name; } ParameterInfo[] allMethodParameters = method.GetParameters(); @@ -1097,8 +1098,9 @@ public LambdaExpression BuildToJSPropertyGetExpression(PropertyInfo property) string name = "get_" + property.Name; if (property.DeclaringType!.IsInterface) { - name = property.DeclaringType.Namespace + '.' + - property.DeclaringType.Name + '.' + name; + string nsPrefix = property.DeclaringType.Namespace != null ? + property.DeclaringType.Namespace + '.' : string.Empty; + name = nsPrefix + property.DeclaringType.Name + '.' + name; } ParameterExpression thisParameter = Expression.Parameter(typeof(JSValue), "__this"); @@ -1167,8 +1169,9 @@ public LambdaExpression BuildToJSPropertySetExpression(PropertyInfo property) string name = "set_" + property.Name; if (property.DeclaringType!.IsInterface) { - name = property.DeclaringType.Namespace + '.' + - property.DeclaringType.Name + '.' + name; + string nsPrefix = property.DeclaringType.Namespace != null ? + property.DeclaringType.Namespace + '.' : string.Empty; + name = nsPrefix + property.DeclaringType.Name + '.' + name; } ParameterExpression thisParameter = diff --git a/src/NodeApi.Generator/ExpressionExtensions.cs b/src/NodeApi.Generator/ExpressionExtensions.cs index 52cfc8c6..2e0b6556 100644 --- a/src/NodeApi.Generator/ExpressionExtensions.cs +++ b/src/NodeApi.Generator/ExpressionExtensions.cs @@ -339,7 +339,8 @@ internal static string FormatType(Type type) } else { - return $"{type.Namespace}.{type.Name.Substring(0, type.Name.IndexOf('`'))}<{typeArgs}>"; + string nsPrefix = type.Namespace != null ? type.Namespace + "." : string.Empty; + return $"{nsPrefix}{type.Name.Substring(0, type.Name.IndexOf('`'))}<{typeArgs}>"; } } else if (type.IsNested) diff --git a/src/NodeApi.Generator/ModuleGenerator.cs b/src/NodeApi.Generator/ModuleGenerator.cs index 41e6713e..6562651e 100644 --- a/src/NodeApi.Generator/ModuleGenerator.cs +++ b/src/NodeApi.Generator/ModuleGenerator.cs @@ -837,7 +837,12 @@ private static void GenerateInterfaceAdapter( JSMarshaller _marshaller) { string ns = GetNamespace(interfaceType); - string adapterName = $"proxy_{ns.Replace('.', '_')}_{interfaceType.Name}"; + if (ns.Length > 0) + { + ns += '_'; + } + + string adapterName = $"proxy_{ns.Replace('.', '_')}{interfaceType.Name}"; static string ReplaceMethodVariables(string cs) => cs.Replace(typeof(JSValue).Namespace + ".", "")