Skip to content

Commit 56a653b

Browse files
author
Olivier Chafik
committed
JNAerator: started cleanup of function pointers
1 parent 7f1998a commit 56a653b

File tree

5 files changed

+33
-16
lines changed

5 files changed

+33
-16
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
tmp
22
.DS_Store
3+
build_out
4+
_jnaerator.*

libraries/jnaerator/jnaerator/src/main/java/com/ochafik/lang/jnaerator/BridJTypeConversion.java

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ public <M extends ModifiableElement> M annotateRawType(M element) throws Unsuppo
159159
break;
160160
case Struct:
161161
case ComplexDouble:
162+
case FunctionSignature:
162163
case Default:
163164
//throw new UnsupportedConversionException(typeRef, "Struct by value not supported yet");
164165
break;
@@ -206,14 +207,7 @@ public NL4JConversion convertTypeToNL4J(TypeRef valueType, Identifier libraryCla
206207
JavaPrim prim = getPrimitive(valueType, libraryClassName);
207208
if (prim != null) {
208209
return convertPrimitiveTypeRefToNL4J(prim, structIOExpr, fieldIndex, valueExpr);
209-
} else if (valueType instanceof TypeRef.Pointer) {
210-
// Treat callback pointers as callbacks.
211-
TypeRef targetRef = ((TypeRef.Pointer) (valueType)).getTarget();
212-
if (targetRef instanceof TypeRef.FunctionSignature) {
213-
valueType = targetRef;
214-
}
215210
}
216-
217211
if (valueType instanceof TypeRef.TargettedTypeRef) {
218212
TypeRef targetRef = ((TypeRef.TargettedTypeRef) valueType).getTarget();
219213

@@ -276,12 +270,7 @@ public NL4JConversion convertTypeToNL4J(TypeRef valueType, Identifier libraryCla
276270
} else if (valueType.getResolvedJavaIdentifier() != null) {
277271
conv.typeRef = typeRef(valueType.getResolvedJavaIdentifier().clone());
278272
if (valueType instanceof TypeRef.FunctionSignature) {
279-
conv.type = ConvType.Pointer;
280-
conv.typeRef = typeRef(ident(result.config.runtime.pointerClass, expr(conv.typeRef)));
281-
if (structIOExpr != null) {
282-
conv.setExpr = methodCall(structIOExpr.clone(), "setPointerField", thisRef(), expr(fieldIndex), valueExpr);
283-
conv.getExpr = methodCall(structIOExpr.clone(), "getPointerField", thisRef(), expr(fieldIndex));
284-
}
273+
conv.type = ConvType.FunctionSignature;
285274
} else if (valueType instanceof Enum) {
286275
conv.type = ConvType.Enum;
287276
conv.typeRef = typeRef(ident(IntValuedEnum.class, expr(conv.typeRef)));

libraries/jnaerator/jnaerator/src/main/java/com/ochafik/lang/jnaerator/TypeConversion.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -952,6 +952,7 @@ public enum ConvType {
952952

953953
Enum,
954954
Pointer,
955+
FunctionSignature,
955956
Primitive,
956957
Struct,
957958
NativeLong,
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,31 @@
1+
typedef struct sdk_private sdk_private;
2+
typedef sdk_private *sdk_t;
3+
void sdk_new(sdk_t *sdk, const char* msg);
4+
void sdk_free(sdk_t sdk);
5+
16
typedef struct _cl_event * cl_event;
27
void f(cl_event, cl_event*);
8+
9+
--
10+
#runtime(JNAerator)
11+
12+
import test.*;
13+
import static test.TestLibrary.*;
14+
15+
sdk_new((PointerByReference)null, (String)null);
16+
sdk_new((Pointer)null, (Pointer)null);
17+
sdk_free((sdk_t)null);
18+
19+
f((Pointer)null, (Pointer)null);
20+
f((cl_event)null, (PointerByReference)null);
21+
322
--
423
#runtime(BridJ)
24+
525
import test.*;
626
import static test.TestLibrary.*;
727

28+
sdk_new((Pointer<sdk_t>)null, (Pointer<Byte>)null);
29+
sdk_free((sdk_t)null);
30+
831
f((cl_event)null, (Pointer<cl_event>)null);

libraries/jnaerator/jnaerator/src/test/resources/com/ochafik/lang/jnaerator/tests/typedefs.test

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ struct ReSampleContext;
66
typedef struct ReSampleContext ReSampleContext;
77
void f2(ReSampleContext*);
88

9-
typedef void FC(int);
10-
void f3(FC a, FC* b);
9+
typedef float FC(int);
10+
void f3(FC* a);
11+
FC f4;
1112

1213
typedef void (fshort1)(short x);
1314
typedef void (fshort2)(short);
@@ -40,7 +41,8 @@ f2((Pointer<ReSampleContext>)null);
4041

4142
Callback cb = (FC)null;
4243
Pointer<FC> pfc = null;
43-
f3(pfc, pfc);
44+
f3(pfc);
45+
float ret = f4(0);
4446

4547
((fshort1)null).apply((short)0);
4648
((fshort2)null).apply((short)0);

0 commit comments

Comments
 (0)