Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
JSExport on struct and records.
  • Loading branch information
maraf authored and github-actions committed Aug 31, 2022
commit 3431dc39eeda5fa1153382621940b43daefc8320
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using System.Threading;

Expand All @@ -16,6 +17,14 @@ namespace Microsoft.Interop.JavaScript
{
internal sealed class JSSignatureContext : IEquatable<JSSignatureContext>
{
private static SymbolDisplayPartKind[] nameKinds = new[]
{
SymbolDisplayPartKind.ClassName,
SymbolDisplayPartKind.StructName,
SymbolDisplayPartKind.RecordClassName,
SymbolDisplayPartKind.RecordStructName
};

internal static readonly string GeneratorName = typeof(JSImportGenerator).Assembly.GetName().Name;

internal static readonly string GeneratorVersion = typeof(JSImportGenerator).Assembly.GetName().Version.ToString();
Expand Down Expand Up @@ -118,12 +127,11 @@ public static JSSignatureContext Create(
};
int typesHash = Math.Abs((int)hash);



var fullName = $"{method.ContainingType.ToDisplayString()}.{method.Name}";
string qualifiedName;

var ns = string.Join(".", method.ContainingType.ToDisplayParts().Where(p => p.Kind == SymbolDisplayPartKind.NamespaceName).Select(x => x.ToString()).ToArray());
var cn = string.Join("/", method.ContainingType.ToDisplayParts().Where(p => p.Kind == SymbolDisplayPartKind.ClassName).Select(x => x.ToString()).ToArray());
var cn = string.Join("/", method.ContainingType.ToDisplayParts().Where(p => Array.IndexOf(nameKinds, p.Kind) >= 0).Select(x => x.ToString()).ToArray());
var qclasses = method.ContainingType.ContainingNamespace == null ? ns : ns + "." + cn;
qualifiedName = $"[{env.Compilation.AssemblyName}]{qclasses}:{method.Name}";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1202,6 +1202,13 @@ public void JsExportStringNoNs()
Assert.Equal("test!", actual);
}

[Fact]
public void JsExportStructClassRecords()
{
var actual = JavaScriptTestHelper.invokeStructClassRecords("test", nameof(JavaScriptTestHelperNoNamespace.EchoString));
Assert.Equal("test!", actual);
}

[Fact]
public void JsImportNative()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,9 @@ public static int EchoInt32([JSMarshalAs<JSType.Number>] int arg1)
[JSImport("invoke2", "JavaScriptTestHelper")]
[return: JSMarshalAs<JSType.String>]
internal static partial string invoke2_String([JSMarshalAs<JSType.String>] string value, [JSMarshalAs<JSType.String>] string name);
[JSImport("invokeStructClassRecords", "JavaScriptTestHelper")]
[return: JSMarshalAs<JSType.String>]
internal static partial string invokeStructClassRecords([JSMarshalAs<JSType.String>] string value, [JSMarshalAs<JSType.String>] string name);
[JSExport]
[return: JSMarshalAs<JSType.String>]
public static string EchoString([JSMarshalAs<JSType.String>] string arg1)
Expand Down Expand Up @@ -943,3 +946,30 @@ public static string EchoString(string message)
return message + "!";
}
}

public partial class JavaScriptTestHelperStruct
{
[System.Runtime.InteropServices.JavaScript.JSExport]
public static string EchoString(string message)
{
return message + "!";
}
}

public partial record class JavaScriptTestHelperRecordClass
{
[System.Runtime.InteropServices.JavaScript.JSExport]
public static string EchoString(string message)
{
return message + "!";
}
}

public partial record struct JavaScriptTestHelperRecordStruct
{
[System.Runtime.InteropServices.JavaScript.JSExport]
public static string EchoString(string message)
{
return message + "!";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,24 @@ export function invoke2(arg1, name) {
return res;
}

export function invokeStructClassRecords(arg1, name) {
let result = null;

["JavaScriptTestHelperNoNamespace", "JavaScriptTestHelperStruct", "JavaScriptTestHelperRecordClass", "JavaScriptTestHelperRecordStruct"].forEach(obj => {
const fn = dllExports[obj][name];
const currentResult = fn(arg1);
if (result) {
if (result !== currentResult) {
throw new Error(`Un expected change in result from '${result}' to '${currentResult}'`);
}
} else {
result = currentResult;
}
})

return result;
}

export async function awaitvoid(arg1) {
// console.log("awaitvoid:" + typeof arg1);
await arg1;
Expand Down