Skip to content

Commit

Permalink
Add CanonicalType; update memstack to 0.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
squid233 committed Sep 15, 2024
1 parent c5607a1 commit 4cb7656
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 25 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
```

Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -35,5 +35,5 @@ jdkEnablePreview=true
jdkEarlyAccessDoc=jdk23

junitVersion=5.11.0
memstackVersion=0.2.0
memstackVersion=0.3.0
platformVersion=1.0.0
15 changes: 2 additions & 13 deletions src/main/java/overrun/marshal/gen/CType.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* <p>
* If the target is marked as {@linkplain #canonical() <i>canonical</i>}, then {@link DescriptorTransformer} will use
* the {@linkplain Linker#canonicalLayouts() canonical layout} mapped from the linker of the current operating system.
* <h2>Example</h2>
* <pre>{@code
* @CType(value = "size_t", canonical = true)
* @CType("size_t")
* long strlen(@CType("const char*") String s);
* }</pre>
*
Expand All @@ -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;
}
45 changes: 45 additions & 0 deletions src/main/java/overrun/marshal/gen/CanonicalType.java
Original file line number Diff line number Diff line change
@@ -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.
* <p>
* This annotation is not {@link Documented}. To display the native type, use {@link CType}.
* <h2>Example</h2>
* <pre>{@code
* @CanonicalType("size_t")
* long strlen(String s);
* }</pre>
*
* @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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<MemoryLayout> argLayouts = new ArrayList<>();

MemoryLayout returnLayout;
if (cType != null && cType.canonical()) {
returnLayout = findCanonicalLayout(cType.value());
if (canonicalType != null) {
returnLayout = findCanonicalLayout(canonicalType.value());
} else {
ProcessorType returnType = ProcessorTypes.fromMethod(method);
Sized sized = method.getDeclaredAnnotation(Sized.class);
Expand Down Expand Up @@ -104,9 +104,9 @@ public FunctionDescriptor process(Context context) {
var parameters = context.parameters();
for (int i = context.descriptorSkipFirstParameter() ? 1 : 0, size = parameters.size(); i < size; i++) {
Parameter parameter = parameters.get(i);
CType parameterCType = parameter.getDeclaredAnnotation(CType.class);
CanonicalType parameterCType = parameter.getDeclaredAnnotation(CanonicalType.class);
MemoryLayout layout;
if (parameterCType != null && parameterCType.canonical()) {
if (parameterCType != null) {
layout = findCanonicalLayout(parameterCType.value());
} else {
ProcessorType type = ProcessorTypes.fromParameter(parameter);
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/overrun/marshal/test/downcall/CTypeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.junit.jupiter.api.Test;
import overrun.marshal.Downcall;
import overrun.marshal.DowncallOption;
import overrun.marshal.gen.CType;
import overrun.marshal.gen.CanonicalType;

import java.lang.foreign.*;
import java.lang.invoke.MethodHandles;
Expand All @@ -38,10 +38,10 @@ public class CTypeTest {
private static Functions instance;

interface Functions {
@CType(value = "long", canonical = true)
@CanonicalType("long")
long returnLong();

long acceptLong(@CType(value = "long", canonical = true) long value);
long acceptLong(@CanonicalType("long") long value);
}

static long returnLong() {
Expand Down

0 comments on commit 4cb7656

Please sign in to comment.