Skip to content

Commit

Permalink
Merge pull request #93 from Berstanio/feat/move-closure-to-internal
Browse files Browse the repository at this point in the history
Move not user relevant code into internal class
  • Loading branch information
Berstanio authored Jan 2, 2025
2 parents 98127b8 + eb6c24c commit 6aaa8f6
Show file tree
Hide file tree
Showing 10 changed files with 1,243 additions and 865 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,20 @@ private void addJNIComment(ClassOrInterfaceDeclaration toAddTo, String... conten

public void emit(String basePath) {
try {
CompilationUnit globalCU = new CompilationUnit(globalType.packageName());
CompilationUnit globalCUInternal = new CompilationUnit(globalType.packageName());
ClassOrInterfaceDeclaration global = globalCU.addClass(globalType.abstractType(), Keyword.PUBLIC, Keyword.FINAL);
ClassOrInterfaceDeclaration globalInternal = globalCUInternal.addClass(globalType.internalClassName(), Keyword.PUBLIC, Keyword.FINAL);


for (StackElementType stackElementType : stackElements.values()) {
CompilationUnit cu = new CompilationUnit(stackElementType.packageName());
ClassOrInterfaceDeclaration declaration = stackElementType.generateClass();
cu.addType(declaration);
stackElementType.write(cu, declaration);

ClassOrInterfaceDeclaration declarationInternal = stackElementType.generateClassInternal();
globalInternal.addMember(declarationInternal);
stackElementType.write(cu, declaration, globalCUInternal, declarationInternal);

String classContent = cu.toString();

Expand All @@ -241,16 +250,16 @@ public void emit(String basePath) {
}

HashMap<MethodDeclaration, String> patchGlobalMethods = new HashMap<>();
CompilationUnit globalCU = new CompilationUnit(globalType.packageName());

globalType.write(globalCU, patchGlobalMethods);
globalType.write(globalCU, global, globalCUInternal, globalInternal, patchGlobalMethods);
String globalFile = globalCU.toString();
for (Entry<MethodDeclaration, String> entry : patchGlobalMethods.entrySet()) {
MethodDeclaration methodDeclaration = entry.getKey();
String s = entry.getValue();
globalFile = patchMethodNative(methodDeclaration, s, globalFile);
}
Files.write(Paths.get(basePath + globalType.classFile().replace(".", "/") + ".java"), globalFile.getBytes(StandardCharsets.UTF_8));
Files.write(Paths.get(basePath + globalType.classFile().replace(".", "/") + "_Internal.java"), globalCUInternal.toString().getBytes(StandardCharsets.UTF_8));

// Macros
CompilationUnit constantsCU = new CompilationUnit(basePackage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import com.github.javaparser.ast.expr.ArrayAccessExpr;
import com.github.javaparser.ast.expr.ArrayCreationExpr;
import com.github.javaparser.ast.expr.ArrayInitializerExpr;
import com.github.javaparser.ast.expr.BinaryExpr;
import com.github.javaparser.ast.expr.Expression;
import com.github.javaparser.ast.expr.FieldAccessExpr;
import com.github.javaparser.ast.expr.IntegerLiteralExpr;
Expand Down Expand Up @@ -44,12 +43,22 @@ public ClassOrInterfaceDeclaration generateClass() {
return new ClassOrInterfaceDeclaration()
.setInterface(true)
.addExtendedType("Closure")
.addExtendedType(internalClassName())
.setModifier(Keyword.PUBLIC, true)
.setName(signature.getName());
}

@Override
public ClassOrInterfaceDeclaration generateClassInternal() {
return new ClassOrInterfaceDeclaration()
.setInterface(true)
.addExtendedType("Closure")
.setModifier(Keyword.PUBLIC, true)
.setName(internalClassName());
}

public void writeHelper(CompilationUnit cu, ClassOrInterfaceDeclaration closureHelperClass) {
importType(cu);
cu.addImport(classFile() + "." + signature.getName());
cu.addImport(ClassNameConstants.CLOSUREENCODER_CLASS);

MethodDeclaration downcallMethod = closureHelperClass.addMethod(getName() + "_downcall", Keyword.PUBLIC, Keyword.STATIC);
Expand All @@ -66,7 +75,7 @@ public void writeHelper(CompilationUnit cu, ClassOrInterfaceDeclaration closureH
.setType("ClosureEncoder")
.addArgument(new NameExpr("fnPtr"))
.addArgument(new FieldAccessExpr(
new NameExpr(getName()),
new NameExpr(internalClassName()),
"__ffi_cache"
))
)
Expand Down Expand Up @@ -115,7 +124,7 @@ public void writeHelper(CompilationUnit cu, ClassOrInterfaceDeclaration closureH
.setType("JavaTypeWrapper")
.addArgument(new ArrayAccessExpr(
new FieldAccessExpr(
new NameExpr(getName()),
new NameExpr(internalClassName()),
"__ffi_cache"
),
new IntegerLiteralExpr("0")
Expand Down Expand Up @@ -155,48 +164,54 @@ public void writeHelper(CompilationUnit cu, ClassOrInterfaceDeclaration closureH
}

@Override
public void write(CompilationUnit cu, ClassOrInterfaceDeclaration closureClass) {
public void write(CompilationUnit cuPublic, ClassOrInterfaceDeclaration toWriteToPublic, CompilationUnit cuPrivate, ClassOrInterfaceDeclaration toWriteToPrivate) {
String name = signature.getName();
TypeDefinition returnType = signature.getReturnType();
NamedType[] arguments = signature.getArguments();
if (!returnType.getMappedType().isLibFFIConvertible() || !Arrays.stream(arguments).map(namedType -> namedType.getDefinition().getMappedType()).allMatch(MappedType::isLibFFIConvertible)) {
throw new IllegalArgumentException("Unions are not allowed to be passed via stack from/to a closure, failing closure: " + name);
}

cu.addImport(ClassNameConstants.CLOSURE_CLASS);
cu.addImport(ClassNameConstants.JAVATYPEWRAPPER_CLASS);
cu.addImport(ClassNameConstants.CTYPEINFO_CLASS);
importType(cuPublic);
cuPublic.addImport(ClassNameConstants.CLOSURE_CLASS);
cuPublic.addImport(internalClass());

cuPrivate.addImport(ClassNameConstants.JAVATYPEWRAPPER_CLASS);
cuPrivate.addImport(ClassNameConstants.CTYPEINFO_CLASS);
cuPrivate.addImport(ClassNameConstants.CLOSURE_CLASS);

NodeList<Expression> arrayInitializerExpr = new NodeList<>();
ArrayCreationExpr arrayCreationExpr = new ArrayCreationExpr();
arrayCreationExpr.setElementType("CTypeInfo");
arrayCreationExpr.setInitializer(new ArrayInitializerExpr(arrayInitializerExpr));

closureClass.addFieldWithInitializer("CTypeInfo[]", "__ffi_cache", arrayCreationExpr);
toWriteToPrivate.addFieldWithInitializer("CTypeInfo[]", "__ffi_cache", arrayCreationExpr);

MethodDeclaration callMethod = closureClass.addMethod(name + "_call");
MethodDeclaration callMethod = toWriteToPublic.addMethod(name + "_call");
callMethod.setBody(null);
for (NamedType namedType : arguments) {
namedType.getDefinition().getMappedType().importType(cu);
namedType.getDefinition().getMappedType().importType(cuPublic);
callMethod.addAndGetParameter(namedType.getDefinition().getMappedType().abstractType(), namedType.getName());
MethodCallExpr getTypeID = new MethodCallExpr("getCTypeInfo");
getTypeID.setScope(new NameExpr("FFITypes"));
getTypeID.addArgument(new IntegerLiteralExpr(String.valueOf(namedType.getDefinition().getMappedType().typeID())));
arrayInitializerExpr.add(getTypeID);
}
returnType.getMappedType().importType(cu);
returnType.getMappedType().importType(cuPublic);
callMethod.setType(returnType.getMappedType().abstractType());

toWriteToPrivate.addMember(callMethod);

MethodCallExpr getTypeID = new MethodCallExpr("getCTypeInfo");
getTypeID.setScope(new NameExpr("FFITypes"));
getTypeID.addArgument(new IntegerLiteralExpr(String.valueOf(returnType.getMappedType().typeID())));
arrayInitializerExpr.add(0, getTypeID);

closureClass.addMethod("functionSignature", Keyword.DEFAULT)
toWriteToPrivate.addMethod("functionSignature", Keyword.DEFAULT)
.setType("CTypeInfo[]")
.createBody().addStatement("return __ffi_cache;");

MethodDeclaration invokeMethod = closureClass.addMethod("invoke", Keyword.DEFAULT);
MethodDeclaration invokeMethod = toWriteToPrivate.addMethod("invoke", Keyword.DEFAULT);
invokeMethod.addParameter("JavaTypeWrapper[]", "parameters");
invokeMethod.addParameter("JavaTypeWrapper", "returnType");
BlockStmt invokeBody = new BlockStmt();
Expand Down Expand Up @@ -227,7 +242,7 @@ public void write(CompilationUnit cu, ClassOrInterfaceDeclaration closureClass)

invokeMethod.setBody(invokeBody);

writeHelper(cu, closureClass);
writeHelper(cuPrivate, toWriteToPrivate);
}

private static Expression getMethodCallExpr(Expression expression, TypeDefinition type) {
Expand Down Expand Up @@ -266,6 +281,7 @@ public void importType(CompilationUnit cu) {
if (cu.getClassByName(parent.abstractType()).isPresent())
return;
cu.addImport(classFile() + "." + signature.getName());
cu.addImport(internalClass());
}

@Override
Expand All @@ -283,7 +299,7 @@ public Expression fromC(Expression cRetrieved) {
MethodCallExpr methodCallExpr = new MethodCallExpr("getClosureObject");
methodCallExpr.setScope(new NameExpr("CHandler"));
methodCallExpr.addArgument(cRetrieved);
methodCallExpr.addArgument(getName() + "::" + getName() + "_downcall");
methodCallExpr.addArgument(internalClassName() + "::" + getName() + "_downcall");
return methodCallExpr;
}

Expand All @@ -298,4 +314,14 @@ public Expression toC(Expression cSend) {
public int typeID() {
return Manager.POINTER_FFI_ID;
}

@Override
public String internalClassName() {
return getName() + "_Internal";
}

@Override
public String internalClass() {
return parent.internalClass() + "." + internalClassName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,9 @@ public void addFunction(FunctionType functionType) {
}


public void write(CompilationUnit cu, HashMap<MethodDeclaration, String> patchNativeMethods) {
ClassOrInterfaceDeclaration global = cu.addClass(globalName, Keyword.PUBLIC, Keyword.FINAL);
cu.addImport(ClassNameConstants.CXXEXCEPTION_CLASS);
cu.addImport(IllegalArgumentException.class);
public void write(CompilationUnit cuPublic, ClassOrInterfaceDeclaration global, CompilationUnit cuInternal, ClassOrInterfaceDeclaration globalInternal, HashMap<MethodDeclaration, String> patchNativeMethods) {
cuPublic.addImport(ClassNameConstants.CXXEXCEPTION_CLASS);
cuPublic.addImport(IllegalArgumentException.class);
global.addStaticInitializer()
.addStatement("CHandler.init();")
.addStatement("FFITypes.init();")
Expand All @@ -66,13 +65,15 @@ public void write(CompilationUnit cu, HashMap<MethodDeclaration, String> patchNa
+ "cxxExceptionClass = (jclass)env->NewGlobalRef(cxxException);");

for (FunctionType functionType : functions) {
functionType.write(cu, global, patchNativeMethods);
functionType.write(cuPublic, global, patchNativeMethods);
}

for (ClosureType closureType : closures.values()) {
ClassOrInterfaceDeclaration declaration = closureType.generateClass();
closureType.write(cu, declaration);
ClassOrInterfaceDeclaration declarationInternal = closureType.generateClassInternal();
closureType.write(cuPublic, declaration, cuInternal, declarationInternal);
global.addMember(declaration);
globalInternal.addMember(declarationInternal);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,12 @@ default String assertC(Expression scope) {
default boolean isLibFFIConvertible() {
return true;
}

default String internalClassName() {
return abstractType() + "_Internal";
}

default String internalClass() {
return classFile() + "_Internal";
}
}
Loading

0 comments on commit 6aaa8f6

Please sign in to comment.