6262import com .oracle .truffle .api .library .CachedLibrary ;
6363import com .oracle .truffle .api .profiles .ValueProfile ;
6464import com .oracle .truffle .api .source .Source ;
65+ import com .oracle .truffle .nfi .api .SignatureLibrary ;
6566
6667/**
6768 * 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)
174175 // This should be a one-off thing for each context
175176 CompilerDirectives .transferToInterpreter ();
176177 synchronized (this ) {
177- dummy = getFunction (lib , function );
178+ dummy = getFunction (context , lib , function );
178179 // it is OK to overwrite cachedFunctions[functionIndex] that may have been
179180 // written from another thread: no need to double check that it's still null.
180181 // dummy is volatile, the object must be fully initialized at this point
@@ -187,15 +188,21 @@ private Object getCachedFunction(PythonContext context, NativeFunction function)
187188 private Object getFunction (PythonContext context , NativeFunction function ) {
188189 CompilerAsserts .neverPartOfCompilation ();
189190 Object lib = getCachedLibrary (context );
190- return getFunction (lib , function );
191+ return getFunction (context , lib , function );
191192 }
192193
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 ) {
194200 CompilerAsserts .neverPartOfCompilation ();
195201 try {
202+ Object signature = parseSignature (context , function .signature ());
196203 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 ) {
199206 throw new IllegalStateException (String .format ("Cannot load symbol '%s' from the internal shared library '%s'" , function .name (), name ), e );
200207 }
201208 }
@@ -250,9 +257,9 @@ protected Object callUncached(PythonContext context, NativeFunction f, Object...
250257 final Object lib = getCachedLibrary (context );
251258 if (lib != null ) {
252259 try {
260+ Object signature = parseSignature (context , f .signature ());
253261 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 );
256263 } catch (Exception e ) {
257264 throw CompilerDirectives .shouldNotReachHere (f .name (), e );
258265 }
0 commit comments