Skip to content

Commit

Permalink
panama-backend - add feature
Browse files Browse the repository at this point in the history
  • Loading branch information
jcrist1 committed Sep 20, 2024
1 parent 81cd96b commit 73bccdc
Show file tree
Hide file tree
Showing 307 changed files with 78,279 additions and 1 deletion.
26 changes: 26 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[workspace]
members = [
"feature",
"cli",
"jextrsact"
]
3 changes: 3 additions & 0 deletions build-feature.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
mkdir feature/tmp
cargo run -p diplomat-java -- -e feature/src/lib.rs -l feature/diplomat-java-conf.toml feature/tmp/
mv feature/tmp/* feature/java/somelib/
2 changes: 1 addition & 1 deletion cli/src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ pub(crate) fn run<'a>(
.render()
.expect("Failed to render Lib.java file");
files.add_file(
format!("src/main/{domain_path}/{lib_name}/Lib.java"),
format!("src/main/java/{domain_path}/{lib_name}/Lib.java"),
lib_file,
);
(files, errors)
Expand Down
1 change: 1 addition & 0 deletions feature/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a.out
17 changes: 17 additions & 0 deletions feature/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "diplomat-feature-tests"
version = "0.0.0"
publish = false
authors = [
"Jan Cristina <[email protected]>",
"Manish Goregaokar <[email protected]>",
]
edition = "2021"

[lib]
crate-type = ["staticlib", "rlib", "cdylib"]

[dependencies]
diplomat-runtime = { git = "https://github.com/jcrist1/diplomat", branch = "tool-and-c-backend-pub", package = "diplomat-runtime"}
diplomat = { git = "https://github.com/jcrist1/diplomat", branch = "tool-and-c-backend-pub", package = "diplomat" }
log = { version = "0.4" }
2 changes: 2 additions & 0 deletions feature/diplomat-java-conf.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
domain = "dev.diplomattest"
lib_name = "somelib"
45 changes: 45 additions & 0 deletions feature/java/somelib/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/

### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
.idea/encodings.xml
.idea/misc.xml
.idea/vcs.xml
.idea/codeStyles
.idea/**
*.iws
*.iml
*.ipr

### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/

### VS Code ###
.vscode/

### Mac OS ###
.DS_Store

libsomelib.dylib
55 changes: 55 additions & 0 deletions feature/java/somelib/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>dev.diplomattest.somelib</groupId>
<artifactId>somelib</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>22</maven.compiler.source>
<maven.compiler.target>22</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>5.14.0</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>1.37</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>1.37</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>3.5.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.5.0</version>
</plugin>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package dev.diplomattest.somelib;

import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.infra.Blackhole;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

public class OpaqueBench {
@Benchmark
public static void benchOpaque(Blackhole bh) {
var opaque = Opaque.fromStr("it's amazing to be here");
bh.consume(opaque);
// opaque.destroy();
}

public static void main(String[] args) throws RunnerException {
Options opt = new OptionsBuilder()
.include(OpaqueBench.class.getSimpleName())
.warmupIterations(2)
.measurementIterations(2)
.forks(1)
.build();

new Runner(opt).run();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package dev.diplomattest.somelib;

import dev.diplomattest.somelib.ntv.*;


import java.lang.foreign.Arena;
import java.lang.foreign.MemorySegment;
import java.lang.foreign.SegmentAllocator;
import java.lang.ref.Cleaner;
import java.util.List;
import static java.lang.foreign.ValueLayout.*;
import java.nio.charset.StandardCharsets;
import java.util.stream.Stream;

public class Bar {

MemorySegment internal;
Cleaner.Cleanable cleanable;
SegmentAllocator arena;

List<Object> selfEdges = List.of();
List<Object> bEdges = List.of();
List<Object> aEdges = List.of();


static class BarCleaner implements Runnable {

MemorySegment segment;
BarCleaner(MemorySegment segment) {
this.segment = segment;
}

public void run() {
somelib_h.Bar_destroy(this.segment);
}
}

Bar() {}
Bar(MemorySegment handle, List<Object> selfEdges, List<Object> bEdges, List<Object> aEdges) {
this.internal = handle;
this.selfEdges = selfEdges;
this.bEdges = bEdges;
this.aEdges = aEdges;


}


public Foo foo() {


var nativeVal = somelib_h.Bar_foo(internal);

List<Object> selfEdges = List.of(this);



List<Object> aEdges = List.of(this);
var returnVal = new Foo(nativeVal, selfEdges, aEdges);
return returnVal;

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package dev.diplomattest.somelib;

import dev.diplomattest.somelib.ntv.*;

import java.lang.foreign.Arena;
import java.lang.foreign.MemorySegment;
import java.lang.ref.Cleaner;
import java.lang.foreign.SegmentAllocator;
import java.util.List;
import static java.lang.foreign.ValueLayout.*;
import java.nio.charset.StandardCharsets;
import java.util.stream.Stream;

public class BorrowedFields {
String a;
String b;
String c;


List<Object> selfEdges = List.of();
List<Object> aEdges = List.of();


private BorrowedFields() {
}

BorrowedFields(MemorySegment structSegment, List<Object> aEdges) {
this.selfEdges = selfEdges;
this.aEdges = aEdges;


var aNative = dev.diplomattest.somelib.ntv.BorrowedFields.a(structSegment);
var aVal = SliceUtils.readUtf16(aNative);
this.a = aVal;
var bNative = dev.diplomattest.somelib.ntv.BorrowedFields.b(structSegment);
var bVal = SliceUtils.readUtf8(bNative);
this.b = bVal;
var cNative = dev.diplomattest.somelib.ntv.BorrowedFields.c(structSegment);
var cVal = SliceUtils.readUtf8(cNative);
this.c = cVal;


}

MemorySegment toNative(SegmentAllocator arena) {
var returnVal = dev.diplomattest.somelib.ntv.BorrowedFields.allocate(arena);

var aData = arena.allocateFrom(a, StandardCharsets.UTF_16);
var aLen = aData.byteSize() - 1; // allocated strings are null terminated
var aView = DiplomatString16View.allocate(arena);
DiplomatString16View.len(aView, aLen);
DiplomatString16View.data(aView, aData);
dev.diplomattest.somelib.ntv.BorrowedFields.a(returnVal, aView);
var bData= arena.allocateFrom(b, StandardCharsets.UTF_8);
var bLen = bData.byteSize() - 1; // allocated strings are null terminated
var bView = DiplomatStringView.allocate(arena);
DiplomatStringView.len(bView, bLen);
DiplomatStringView.data(bView, bData);
dev.diplomattest.somelib.ntv.BorrowedFields.b(returnVal, bView);
var cData= arena.allocateFrom(c, StandardCharsets.UTF_8);
var cLen = cData.byteSize() - 1; // allocated strings are null terminated
var cView = DiplomatStringView.allocate(arena);
DiplomatStringView.len(cView, cLen);
DiplomatStringView.data(cView, cData);
dev.diplomattest.somelib.ntv.BorrowedFields.c(returnVal, cView);


return returnVal;

}

public static BorrowedFields fromBarAndStrings(Bar bar,String dstr16,String utf8Str) {

try (var arena = Arena.ofConfined()) {

var barNative = bar.internal;
var dstr16Data = Arena.ofAuto().allocateFrom(dstr16, StandardCharsets.UTF_16);
var dstr16Len = dstr16Data.byteSize() - 1; // allocated strings are null terminated
var dstr16View = DiplomatString16View.allocate(Arena.ofAuto());
DiplomatString16View.len(dstr16View, dstr16Len);
DiplomatString16View.data(dstr16View, dstr16Data);
var utf8StrData= Arena.ofAuto().allocateFrom(utf8Str, StandardCharsets.UTF_8);
var utf8StrLen = utf8StrData.byteSize() - 1; // allocated strings are null terminated
var utf8StrView = DiplomatStringView.allocate(Arena.ofAuto());
DiplomatStringView.len(utf8StrView, utf8StrLen);
DiplomatStringView.data(utf8StrView, utf8StrData);
var nativeVal = somelib_h.BorrowedFields_from_bar_and_strings(arena, barNative, dstr16View, utf8StrView);

var returnVal = new BorrowedFields(nativeVal, List.of(bar, dstr16, utf8Str));
return returnVal;

}
}

}

Loading

0 comments on commit 73bccdc

Please sign in to comment.