@@ -58,14 +58,14 @@ public static void generate(SourceFileBuilder builder, String className, ClassSo
58
58
fib .emitFunctionalInterfaceMethod ();
59
59
fib .emitDescriptorDecl ();
60
60
fib .emitFunctionalFactories ();
61
- fib .emitFunctionalFactoryForPointer ();
61
+ fib .emitInvoke ();
62
62
fib .classEnd ();
63
63
}
64
64
65
65
private void emitFunctionalInterfaceMethod () {
66
66
appendIndentedLines (STR ."""
67
67
68
- \{methodType .returnType ().getSimpleName ()} apply(\{paramExprs ("" )});
68
+ \{methodType .returnType ().getSimpleName ()} apply(\{paramExprs ()});
69
69
""" );
70
70
}
71
71
@@ -80,22 +80,21 @@ static MemorySegment allocate(\{className()} fi, Arena scope) {
80
80
""" );
81
81
}
82
82
83
- private void emitFunctionalFactoryForPointer () {
83
+ private void emitInvoke () {
84
84
boolean needsAllocator = Utils .isStructOrUnion (funcType .returnType ());
85
- String allocArg = needsAllocator ? ", (SegmentAllocator)arena" : "" ;
85
+ String allocParam = needsAllocator ? ", SegmentAllocator alloc" : "" ;
86
+ String allocArg = needsAllocator ? ", alloc" : "" ;
87
+ String paramStr = methodType .parameterCount () != 0 ? STR .",\{paramExprs ()}" : "" ;
86
88
appendIndentedLines (STR ."""
87
89
88
90
MethodHandle DOWN$MH = Linker.nativeLinker().downcallHandle($DESC);
89
91
90
- static \{className ()} ofAddress(MemorySegment addr, Arena arena) {
91
- MemorySegment symbol = addr.reinterpret(arena, null);
92
- return (\{paramExprs ("_" )}) -> {
93
- try {
94
- \{retExpr ()} DOWN$MH.invokeExact(symbol\{allocArg }\{otherArgExprs ()});
95
- } catch (Throwable ex$) {
96
- throw new AssertionError("should not reach here", ex$);
97
- }
98
- };
92
+ static \{methodType .returnType ().getSimpleName ()} invoke(MemorySegment funcPtr\{allocParam }\{paramStr }) {
93
+ try {
94
+ \{retExpr ()} DOWN$MH.invokeExact(funcPtr\{allocArg }\{otherArgExprs ()});
95
+ } catch (Throwable ex$) {
96
+ throw new AssertionError("should not reach here", ex$);
97
+ }
99
98
}
100
99
""" );
101
100
}
@@ -109,13 +108,13 @@ private String parameterName(int i) {
109
108
return name .isEmpty ()? "_x" + i : name ;
110
109
}
111
110
112
- private String paramExprs (String paramNamePrefix ) {
111
+ private String paramExprs () {
113
112
StringBuilder result = new StringBuilder ();
114
113
String delim = "" ;
115
114
for (int i = 0 ; i < methodType .parameterCount (); i ++) {
116
115
result .append (delim ).append (methodType .parameterType (i ).getSimpleName ());
117
116
result .append (" " );
118
- result .append (paramNamePrefix ). append ( parameterName (i ));
117
+ result .append (parameterName (i ));
119
118
delim = ", " ;
120
119
}
121
120
return result .toString ();
@@ -133,7 +132,7 @@ private String otherArgExprs() {
133
132
String argsExprs = "" ;
134
133
if (methodType .parameterCount () > 0 ) {
135
134
argsExprs += ", " + IntStream .range (0 , methodType .parameterCount ())
136
- .mapToObj (i -> "_" + parameterName ( i ) )
135
+ .mapToObj (this :: parameterName )
137
136
.collect (Collectors .joining (", " ));
138
137
}
139
138
return argsExprs ;
0 commit comments