From 4cb7656bb87ec118293ff56b342b55514506ffec Mon Sep 17 00:00:00 2001 From: squid233 <60126026+squid233@users.noreply.github.com> Date: Sun, 15 Sep 2024 17:10:13 +0800 Subject: [PATCH] Add CanonicalType; update memstack to 0.3.0 --- README.md | 2 +- gradle.properties | 4 +- src/main/java/overrun/marshal/gen/CType.java | 15 +------ .../overrun/marshal/gen/CanonicalType.java | 45 +++++++++++++++++++ .../gen/processor/DescriptorTransformer.java | 12 ++--- .../marshal/test/downcall/CTypeTest.java | 6 +-- 6 files changed, 59 insertions(+), 25 deletions(-) create mode 100644 src/main/java/overrun/marshal/gen/CanonicalType.java diff --git a/README.md b/README.md index 8539b27..a71f0d2 100644 --- a/README.md +++ b/README.md @@ -88,7 +88,7 @@ Import as a Gradle dependency: ```groovy dependencies { - implementation("io.github.over-run:marshal:0.1.0-alpha.33-jdk23") + implementation("io.github.over-run:marshal:0.1.0-alpha.34-jdk23") } ``` diff --git a/gradle.properties b/gradle.properties index 5affdc9..34b2a21 100644 --- a/gradle.properties +++ b/gradle.properties @@ -12,7 +12,7 @@ projGroupId=io.github.over-run projArtifactId=marshal # The project name should only contain lowercase letters, numbers and hyphen. projName=marshal -projVersion=0.1.0-alpha.33-jdk23 +projVersion=0.1.0-alpha.34-jdk23 projDesc=Marshaler of native libraries # Uncomment them if you want to publish to maven repository. projUrl=https://github.com/Over-Run/marshal @@ -35,5 +35,5 @@ jdkEnablePreview=true jdkEarlyAccessDoc=jdk23 junitVersion=5.11.0 -memstackVersion=0.2.0 +memstackVersion=0.3.0 platformVersion=1.0.0 diff --git a/src/main/java/overrun/marshal/gen/CType.java b/src/main/java/overrun/marshal/gen/CType.java index 071eb37..e36d65d 100644 --- a/src/main/java/overrun/marshal/gen/CType.java +++ b/src/main/java/overrun/marshal/gen/CType.java @@ -16,19 +16,13 @@ package overrun.marshal.gen; -import overrun.marshal.gen.processor.DescriptorTransformer; - import java.lang.annotation.*; -import java.lang.foreign.Linker; /** * This marker annotation is for methods and parameters to mark the native type of them. - *
- * If the target is marked as {@linkplain #canonical() canonical}, then {@link DescriptorTransformer} will use - * the {@linkplain Linker#canonicalLayouts() canonical layout} mapped from the linker of the current operating system. *
{@code - * @CType(value = "size_t", canonical = true) + * @CType("size_t") * long strlen(@CType("const char*") String s); * }* @@ -37,15 +31,10 @@ */ @Documented @Target(ElementType.TYPE_USE) -@Retention(RetentionPolicy.RUNTIME) +@Retention(RetentionPolicy.SOURCE) public @interface CType { /** * {@return the native (C) type of the marked type} */ String value(); - - /** - * {@return {@code true} if {@link DescriptorTransformer} should use the canonical layout} - */ - boolean canonical() default false; } diff --git a/src/main/java/overrun/marshal/gen/CanonicalType.java b/src/main/java/overrun/marshal/gen/CanonicalType.java new file mode 100644 index 0000000..8ea00ea --- /dev/null +++ b/src/main/java/overrun/marshal/gen/CanonicalType.java @@ -0,0 +1,45 @@ +/* + * MIT License + * + * Copyright (c) 2024 Overrun Organization + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + */ + +package overrun.marshal.gen; + +import overrun.marshal.gen.processor.DescriptorTransformer; + +import java.lang.annotation.*; +import java.lang.foreign.Linker; + +/** + * Marks a method or a parameter, as {@link DescriptorTransformer} will use + * the {@linkplain Linker#canonicalLayouts() canonical layout} mapped from the linker of the current operating system. + *
+ * This annotation is not {@link Documented}. To display the native type, use {@link CType}. + *
{@code + * @CanonicalType("size_t") + * long strlen(String s); + * }+ * + * @author squid233 + * @since 0.1.0 + */ +@Target(ElementType.TYPE_USE) +@Retention(RetentionPolicy.RUNTIME) +public @interface CanonicalType { + /** + * {@return the canonical type of the marked type} + */ + String value(); +} diff --git a/src/main/java/overrun/marshal/gen/processor/DescriptorTransformer.java b/src/main/java/overrun/marshal/gen/processor/DescriptorTransformer.java index f88d446..3b00755 100644 --- a/src/main/java/overrun/marshal/gen/processor/DescriptorTransformer.java +++ b/src/main/java/overrun/marshal/gen/processor/DescriptorTransformer.java @@ -16,7 +16,7 @@ package overrun.marshal.gen.processor; -import overrun.marshal.gen.CType; +import overrun.marshal.gen.CanonicalType; import overrun.marshal.gen.Sized; import overrun.marshal.struct.ByValue; @@ -67,12 +67,12 @@ public record Context( @Override public FunctionDescriptor process(Context context) { Method method = context.method(); - CType cType = method.getDeclaredAnnotation(CType.class); + CanonicalType canonicalType = method.getDeclaredAnnotation(CanonicalType.class); List