Skip to content

Commit

Permalink
Merge pull request #6 from Over-Run/struct
Browse files Browse the repository at this point in the history
Add annotation Struct
  • Loading branch information
squid233 authored Jan 1, 2024
2 parents 7aca856 + 2878858 commit f389247
Show file tree
Hide file tree
Showing 36 changed files with 1,760 additions and 309 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2023 Overrun Organization
Copyright (c) 2023-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
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,15 @@ interface CGLFW {
double getTime();

/**
* Fixed size array.
* Sized array.
* Note: this method doesn't exist in GLFW
* <p>
* You can mark methods with Critical
*
* @param arr The array
*/
@Critical(allowHeapAccess = true)
void fixedSizeArray(@FixedSize(2) int[] arr);
void sizedArray(@Sized(2) int[] arr);

/**
* A simple method
Expand Down Expand Up @@ -101,7 +101,7 @@ class Main {
GLFW.glfwSwapInterval(1);
GLFW.glfwEnableVSync();
double time = GLFW.getTime();
GLFW.fixedSizeArray(new int[]{4, 2});
GLFW.sizedArray(new int[]{4, 2});
MemorySegment windowHandle = /*...*/createWindow();
// MemoryStack is a placeholder type
try (MemoryStack stack = /*...*/stackPush()) {
Expand Down
92 changes: 46 additions & 46 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,6 @@ plugins {
//application
}

data class PublicationRepo(
val name: String,
val usernameFrom: List<String>,
val passwordFrom: List<String>,
val snapshotRepo: String,
val releaseRepo: String,
val snapshotPredicate: (String) -> Boolean = { it.endsWith("-SNAPSHOT") }
)

val hasPublication: String by rootProject
val publicationSigning: String by rootProject
val publicationRepo: PublicationRepo? = if (hasPublication.toBoolean()) PublicationRepo(
name = "OSSRH",
usernameFrom = listOf("OSSRH_USERNAME", "ossrhUsername"),
passwordFrom = listOf("OSSRH_PASSWORD", "ossrhPassword"),
snapshotRepo = "https://s01.oss.sonatype.org/content/repositories/snapshots/",
releaseRepo = "https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/"
) else null

data class Organization(
val name: String,
val url: String
)

data class Developer(
val id: String,
val name: String? = null,
val email: String? = null,
val url: String? = null,
val organization: Organization? = projOrg,
val roles: Set<String>? = null,
val timezone: String? = null,
val properties: Map<String, String?>? = null
)

val projDevelopers = arrayOf(
Developer("squid233")
)

val hasJavadocJar: String by rootProject
val hasSourcesJar: String by rootProject

Expand All @@ -69,6 +30,45 @@ val jdkEarlyAccessDoc: String? by rootProject

val targetJavaVersion = jdkVersion.toInt()

val projDevelopers = arrayOf(
Developer("squid233")
)

data class Organization(
val name: String,
val url: String
)

data class Developer(
val id: String,
val name: String? = null,
val email: String? = null,
val url: String? = null,
val organization: Organization? = projOrg,
val roles: Set<String>? = null,
val timezone: String? = null,
val properties: Map<String, String?>? = null
)

data class PublicationRepo(
val name: String,
val usernameFrom: List<String>,
val passwordFrom: List<String>,
val snapshotRepo: String,
val releaseRepo: String,
val snapshotPredicate: (String) -> Boolean = { it.endsWith("-SNAPSHOT") }
)

val hasPublication: String by rootProject
val publicationSigning: String by rootProject
val publicationRepo: PublicationRepo? = if (hasPublication.toBoolean()) PublicationRepo(
name = "OSSRH",
usernameFrom = listOf("OSSRH_USERNAME", "ossrhUsername"),
passwordFrom = listOf("OSSRH_PASSWORD", "ossrhPassword"),
snapshotRepo = "https://s01.oss.sonatype.org/content/repositories/snapshots/",
releaseRepo = "https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/"
) else null

allprojects {
apply(plugin = "java-library")

Expand Down Expand Up @@ -126,14 +126,14 @@ allprojects {
//"Main-Class" to "org.example.Main"
)
}
}

tasks.withType<Jar> {
archiveBaseName = projArtifactId
from(rootProject.file(projLicenseFileName)).rename(
projLicenseFileName,
"${projLicenseFileName}_$projArtifactId"
)
}
tasks.withType<Jar> {
archiveBaseName = projArtifactId
from(rootProject.file(projLicenseFileName)).rename(
projLicenseFileName,
"${projLicenseFileName}_$projArtifactId"
)
}

tasks.withType<JavaCompile> {
Expand Down
6 changes: 4 additions & 2 deletions demo/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
plugins { `java-library` }

dependencies {
implementation(rootProject)
annotationProcessor(rootProject)
}

tasks.withType<Jar> {
archiveBaseName = "marshal-demo"
}
32 changes: 28 additions & 4 deletions demo/src/main/java/overrun/marshal/test/CDowncallTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2023 Overrun Organization
* Copyright (c) 2023-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
Expand All @@ -17,6 +17,7 @@
package overrun.marshal.test;

import overrun.marshal.*;
import overrun.marshal.struct.StructRef;

import java.lang.foreign.MemorySegment;

Expand Down Expand Up @@ -63,10 +64,10 @@ interface CDowncallTest {
@Access(AccessModifier.PRIVATE)
int testWithPrivate(int i);

void testWithArray(MemorySegment arr);
void testWithArray(int i, MemorySegment arr);

@Overload
void testWithArray(int[] arr);
void testWithArray(int i, int[] arr);

void testWithOneRef(@Ref MemorySegment arr);

Expand All @@ -81,7 +82,7 @@ interface CDowncallTest {
void testWithRefArray(MemorySegment arr0, MemorySegment arr1, MemorySegment arr2, MemorySegment arr3, MemorySegment arr4, MemorySegment arr5);

@Overload
void testWithRefArray(int[] arr0, @Ref int[] arr1, @Ref(nullable = true) int[] arr2, boolean[] arr3, @Ref boolean[] arr4, @FixedSize(3) int[] arr5);
void testWithRefArray(int[] arr0, @Ref int[] arr1, @Ref(nullable = true) int[] arr2, boolean[] arr3, @Ref boolean[] arr4, @Sized(3) int[] arr5);

void testWithString(MemorySegment str1, MemorySegment str2);

Expand Down Expand Up @@ -123,6 +124,16 @@ interface CDowncallTest {
@Critical(allowHeapAccess = false)
void testCriticalFalse();

@Entrypoint("testReturnSizedArr")
MemorySegment ntestReturnSizedArr();

@Overload("ntestReturnSizedArr")
@Sized(4)
int[] testReturnSizedArr();

@SizedSeg(4L)
MemorySegment testReturnSizedSeg();

void testUpcall(MemorySegment cb);

@Overload
Expand All @@ -134,6 +145,19 @@ interface CDowncallTest {
@Overload("ntestReturnUpcall")
GLFWErrorCallback testReturnUpcall();

void testStruct(MemorySegment struct);

@Overload
void testStruct(@StructRef("overrun.marshal.test.Vector3") Object struct);

@Entrypoint("testReturnStruct")
@StructRef("overrun.marshal.test.StructTest")
MemorySegment ntestReturnStruct();

@Overload("ntestReturnStruct")
@StructRef("overrun.marshal.test.StructTest")
MemorySegment testReturnStruct();

/**
* This is a test that tests all features.
*
Expand Down
69 changes: 69 additions & 0 deletions demo/src/main/java/overrun/marshal/test/CStructTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* MIT License
*
* Copyright (c) 2023-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.test;

import overrun.marshal.SizedSeg;
import overrun.marshal.Sized;
import overrun.marshal.Skip;
import overrun.marshal.StrCharset;
import overrun.marshal.struct.Const;
import overrun.marshal.struct.Padding;
import overrun.marshal.struct.Struct;
import overrun.marshal.struct.StructRef;

import java.lang.foreign.MemorySegment;

/**
* {@linkplain #_LAYOUT Layout}
*
* @author squid233
* @since 0.1.0
*/
@Struct
final class CStructTest {
@Skip
int _LAYOUT;
int x;
@Const
int y;
int index;
int[] segmentAllocator;
GLFWErrorCallback arena;
/**
* the timestamp
*/
long stamp;
byte b;
@Padding(7)
int padding;
MemorySegment segment;
@SizedSeg(16L)
MemorySegment sizedSegment;
String name;
@StrCharset("UTF-16")
String utf16Name;
int[] arr;
@Sized(4)
int[] sizedArr;
boolean[] boolArr;
String[] strArr;
GLFWErrorCallback upcall;
@StructRef("overrun.marshal.test.StructTest")
MemorySegment myStruct;
@StructRef("overrun.marshal.test.Vector3")
int anotherStruct;
}
29 changes: 29 additions & 0 deletions demo/src/main/java/overrun/marshal/test/CVector3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* MIT License
*
* Copyright (c) 2023-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.test;

import overrun.marshal.struct.Const;
import overrun.marshal.struct.Struct;

/**
* @author squid233
* @since 0.1.0
*/
@Const
@Struct
public record CVector3(int x, int y, int z) {
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2023 Overrun Organization
* Copyright (c) 2023-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
Expand Down Expand Up @@ -45,12 +45,12 @@ default MemorySegment stub(Arena arena) {

@Wrapper
static GLFWErrorCallback wrap(MemorySegment stub) {
return (error, description) -> {
return TYPE.wrap(stub, (arena, methodHandle) -> (error, description) -> {
try {
TYPE.downcall(stub).invokeExact(error, description);
methodHandle.invokeExact(error, arena.get().allocateFrom(description));
} catch (Throwable e) {
throw new RuntimeException(e);
}
};
});
}
}
3 changes: 2 additions & 1 deletion src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2023 Overrun Organization
* Copyright (c) 2023-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
Expand All @@ -22,6 +22,7 @@
*/
module io.github.overrun.marshal {
exports overrun.marshal;
exports overrun.marshal.struct;

requires java.compiler;
}
4 changes: 2 additions & 2 deletions src/main/java/overrun/marshal/Checks.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* MIT License
*
* Copyright (c) 2023 Overrun Organization
* Copyright (c) 2023-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
Expand All @@ -28,7 +28,7 @@ public final class Checks {
/**
* Check size of a fixed size array argument. Default value: {@code true}
*
* @see FixedSize
* @see Sized
*/
public static final Entry<Boolean> CHECK_ARRAY_SIZE = new Entry<>(() -> true);

Expand Down
Loading

0 comments on commit f389247

Please sign in to comment.