Skip to content

Commit

Permalink
Merge pull request ikvmnet#578 from ikvmnet/stubenc
Browse files Browse the repository at this point in the history
Rewrite StubGen around IKVM.ByteCode.Encoding
  • Loading branch information
wasabii authored Aug 20, 2024
2 parents f380509 + cf132f2 commit 44b110a
Show file tree
Hide file tree
Showing 41 changed files with 1,562 additions and 2,605 deletions.
2 changes: 1 addition & 1 deletion IKVM.deps.targets
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project>

<ItemGroup>
<PackageReference Include="IKVM.ByteCode" Version="9.1.0" />
<PackageReference Include="IKVM.ByteCode" Version="9.1.3" />
</ItemGroup>

<Choose>
Expand Down
3 changes: 1 addition & 2 deletions src/IKVM.Java.Extensions/java/util/CollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections;
using System.Collections.Generic;
using System.Collections.Generic;

namespace java.util
{
Expand Down
33 changes: 33 additions & 0 deletions src/IKVM.Java.Tests/java/lang/ByteTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package ikvm.tests.java.java.lang;

import java.lang.*;

@cli.Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute.Annotation()
public class ByteTests {

@cli.Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute.Annotation()
public void canCastMinMax() throws Throwable {
if (!String.valueOf(Byte.MAX_VALUE).equals("127"))
throw new Exception();
if (!String.valueOf(Byte.MIN_VALUE).equals("-128"))
throw new Exception();
if (!String.valueOf((short)Byte.MAX_VALUE).equals("127"))
throw new Exception();
if (!String.valueOf((short)Byte.MIN_VALUE).equals("-128"))
throw new Exception();
if (!String.valueOf((int)Byte.MAX_VALUE).equals("127"))
throw new Exception();
if (!String.valueOf((int)Byte.MIN_VALUE).equals("-128"))
throw new Exception();
if (!String.valueOf((long)Byte.MAX_VALUE).equals("127"))
throw new Exception();
if (!String.valueOf((long)Byte.MIN_VALUE).equals("-128"))
throw new Exception();

if (!Integer.valueOf(Byte.MAX_VALUE).equals(Integer.valueOf(127)))
throw new Exception();
if (!Integer.valueOf(Byte.MIN_VALUE).equals(Integer.valueOf(-128)))
throw new Exception();
}

}
63 changes: 27 additions & 36 deletions src/IKVM.Runtime/Attributes/AnnotationDefaultAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,32 @@ public sealed class AnnotationDefaultAttribute : Attribute
public const byte TAG_ANNOTATION = (byte)'@';
public const byte TAG_ARRAY = (byte)'[';
public const byte TAG_ERROR = (byte)'?';
private object defaultValue;

internal static object Escape(object obj)
{
return EscapeOrUnescape(obj, true);
}

internal static object Unescape(object obj)
{
return EscapeOrUnescape(obj, false);
}

static object EscapeOrUnescape(object obj, bool escape)
{
var str = obj as string;
if (str != null)
return escape ? UnicodeUtil.EscapeInvalidSurrogates(str) : UnicodeUtil.UnescapeInvalidSurrogates(str);

var arr = obj as object[];
if (arr != null)
for (int i = 0; i < arr.Length; i++)
arr[i] = EscapeOrUnescape(arr[i], escape);

return obj;
}

object defaultValue;

// element_value encoding:
// primitives:
Expand All @@ -59,41 +84,7 @@ public AnnotationDefaultAttribute(object defaultValue)
this.defaultValue = Unescape(defaultValue);
}

public object Value
{
get
{
return defaultValue;
}
}

internal static object Escape(object obj)
{
return EscapeOrUnescape(obj, true);
}

internal static object Unescape(object obj)
{
return EscapeOrUnescape(obj, false);
}

private static object EscapeOrUnescape(object obj, bool escape)
{
string str = obj as string;
if (str != null)
{
return escape ? UnicodeUtil.EscapeInvalidSurrogates(str) : UnicodeUtil.UnescapeInvalidSurrogates(str);
}
object[] arr = obj as object[];
if (arr != null)
{
for (int i = 0; i < arr.Length; i++)
{
arr[i] = EscapeOrUnescape(arr[i], escape);
}
}
return obj;
}
public object Value => defaultValue;

}

Expand Down
7 changes: 5 additions & 2 deletions src/IKVM.Runtime/Attributes/ImplementsAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@ namespace IKVM.Attributes
public sealed class ImplementsAttribute : Attribute
{

private string[] interfaces;
string[] interfaces;

// NOTE this is not CLS compliant, so maybe we should have a couple of overloads
/// <summary>
/// Initializes a new instance.
/// </summary>
/// <param name="interfaces"></param>
public ImplementsAttribute(string[] interfaces)
{
this.interfaces = UnicodeUtil.UnescapeInvalidSurrogates(interfaces);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public static byte[] GenerateStub(global::java.lang.Class c)
throw new NotImplementedException();
#else
using var mem = new MemoryStream();
JVM.Context.StubGenerator.WriteClass(mem, RuntimeJavaType.FromClass(c), true, true, true, true, false);
JVM.Context.StubGenerator.Write(mem, RuntimeJavaType.FromClass(c), true, true, true, true, false);
return mem.ToArray();
#endif
}
Expand Down
2 changes: 1 addition & 1 deletion src/IKVM.Runtime/RuntimeContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#endif

#if IMPORTER == false
using IKVM.StubGen;
using IKVM.Runtime.StubGen;
#endif

namespace IKVM.Runtime
Expand Down
53 changes: 0 additions & 53 deletions src/IKVM.Runtime/StubGen/AnnotationDefaultClassFileAttribute.cs

This file was deleted.

116 changes: 0 additions & 116 deletions src/IKVM.Runtime/StubGen/BigEndianStream.cs

This file was deleted.

45 changes: 0 additions & 45 deletions src/IKVM.Runtime/StubGen/ClassFileAttribute.cs

This file was deleted.

Loading

0 comments on commit 44b110a

Please sign in to comment.