62
62
import com .oracle .truffle .api .library .CachedLibrary ;
63
63
import com .oracle .truffle .api .profiles .ValueProfile ;
64
64
import com .oracle .truffle .api .source .Source ;
65
+ import com .oracle .truffle .nfi .api .SignatureLibrary ;
65
66
66
67
/**
67
68
* Wraps a native library loaded via NFI and provides caching for functions looked up in the
@@ -174,7 +175,7 @@ private Object getCachedFunction(PythonContext context, NativeFunction function)
174
175
// This should be a one-off thing for each context
175
176
CompilerDirectives .transferToInterpreter ();
176
177
synchronized (this ) {
177
- dummy = getFunction (lib , function );
178
+ dummy = getFunction (context , lib , function );
178
179
// it is OK to overwrite cachedFunctions[functionIndex] that may have been
179
180
// written from another thread: no need to double check that it's still null.
180
181
// dummy is volatile, the object must be fully initialized at this point
@@ -187,15 +188,21 @@ private Object getCachedFunction(PythonContext context, NativeFunction function)
187
188
private Object getFunction (PythonContext context , NativeFunction function ) {
188
189
CompilerAsserts .neverPartOfCompilation ();
189
190
Object lib = getCachedLibrary (context );
190
- return getFunction (lib , function );
191
+ return getFunction (context , lib , function );
191
192
}
192
193
193
- private Object getFunction (Object lib , NativeFunction function ) {
194
+ private Object parseSignature (PythonContext context , String signature ) {
195
+ Source sigSource = Source .newBuilder ("nfi" , nfiBackend .withClause + signature , "python-nfi-signature" ).build ();
196
+ return context .getEnv ().parseInternal (sigSource ).call ();
197
+ }
198
+
199
+ private Object getFunction (PythonContext context , Object lib , NativeFunction function ) {
194
200
CompilerAsserts .neverPartOfCompilation ();
195
201
try {
202
+ Object signature = parseSignature (context , function .signature ());
196
203
Object symbol = cachedLibraryInterop .readMember (lib , function .name ());
197
- return InteropLibrary .getUncached ().invokeMember ( symbol , "bind" , function . signature () );
198
- } catch (UnsupportedMessageException | UnknownIdentifierException | ArityException | UnsupportedTypeException e ) {
204
+ return SignatureLibrary .getUncached ().bind ( signature , symbol );
205
+ } catch (UnsupportedMessageException | UnknownIdentifierException e ) {
199
206
throw new IllegalStateException (String .format ("Cannot load symbol '%s' from the internal shared library '%s'" , function .name (), name ), e );
200
207
}
201
208
}
@@ -250,9 +257,9 @@ protected Object callUncached(PythonContext context, NativeFunction f, Object...
250
257
final Object lib = getCachedLibrary (context );
251
258
if (lib != null ) {
252
259
try {
260
+ Object signature = parseSignature (context , f .signature ());
253
261
Object symbol = cachedLibraryInterop .readMember (lib , f .name ());
254
- Object function = InteropLibrary .getUncached (symbol ).invokeMember (symbol , "bind" , f .signature ());
255
- return InteropLibrary .getUncached (function ).execute (function , args );
262
+ return SignatureLibrary .getUncached ().call (signature , symbol , args );
256
263
} catch (Exception e ) {
257
264
throw CompilerDirectives .shouldNotReachHere (f .name (), e );
258
265
}
0 commit comments