ExternalLibraryFunction.st
changeset 23829 209adc65cf44
parent 23828 cbcc99660889
child 23831 c741a73646f0
equal deleted inserted replaced
23828:cbcc99660889 23829:209adc65cf44
   878     "call the function with no arguments"
   878     "call the function with no arguments"
   879 
   879 
   880     self hasCode ifFalse:[
   880     self hasCode ifFalse:[
   881         self prepareInvoke.
   881         self prepareInvoke.
   882     ].
   882     ].
   883     ^ self invokeFFIwithArguments:nil forCPPInstance:nil
   883     ^ self invokeFFIwithArguments:nil forInstance:nil
   884 
   884 
   885     "Modified (comment): / 04-03-2019 / 09:41:20 / Claus Gittinger"
   885     "Modified (comment): / 04-03-2019 / 09:41:20 / Claus Gittinger"
   886 !
   886 !
   887 
   887 
   888 invokeCPPVirtualOn:anInstance
   888 invokeCPPVirtualOn:anInstance
   889     "call a CPP virtual function with no arguments"
   889     "call a CPP virtual function with no arguments"
   890     
   890     
   891     self hasCode ifFalse:[
   891     self hasCode ifFalse:[
   892         self prepareInvoke.
   892         self prepareInvoke.
   893     ].
   893     ].
   894     ^ self invokeFFIwithArguments:nil forCPPInstance:anInstance
   894     ^ self invokeFFIwithArguments:nil forInstance:anInstance
   895 
   895 
   896     "Modified (comment): / 04-03-2019 / 09:39:27 / Claus Gittinger"
   896     "Modified (comment): / 04-03-2019 / 09:39:27 / Claus Gittinger"
   897 !
   897 !
   898 
   898 
   899 invokeCPPVirtualOn:anInstance with:arg
   899 invokeCPPVirtualOn:anInstance with:arg
   902     self hasCode ifFalse:[
   902     self hasCode ifFalse:[
   903         self prepareInvoke.
   903         self prepareInvoke.
   904     ].
   904     ].
   905     ^ self 
   905     ^ self 
   906         invokeFFIwithArguments:(Array with:arg) 
   906         invokeFFIwithArguments:(Array with:arg) 
   907         forCPPInstance:anInstance
   907         forInstance:anInstance
   908 
   908 
   909     "Modified (comment): / 04-03-2019 / 09:39:34 / Claus Gittinger"
   909     "Modified (comment): / 04-03-2019 / 09:39:34 / Claus Gittinger"
   910 !
   910 !
   911 
   911 
   912 invokeCPPVirtualOn:anInstance with:arg1 with:arg2
   912 invokeCPPVirtualOn:anInstance with:arg1 with:arg2
   915     self hasCode ifFalse:[
   915     self hasCode ifFalse:[
   916         self prepareInvoke.
   916         self prepareInvoke.
   917     ].
   917     ].
   918     ^ self 
   918     ^ self 
   919         invokeFFIwithArguments:(Array with:arg1 with:arg2) 
   919         invokeFFIwithArguments:(Array with:arg1 with:arg2) 
   920         forCPPInstance:anInstance
   920         forInstance:anInstance
   921 
   921 
   922     "Modified (comment): / 04-03-2019 / 09:39:38 / Claus Gittinger"
   922     "Modified (comment): / 04-03-2019 / 09:39:38 / Claus Gittinger"
   923 !
   923 !
   924 
   924 
   925 invokeCPPVirtualOn:anInstance with:arg1 with:arg2 with:arg3
   925 invokeCPPVirtualOn:anInstance with:arg1 with:arg2 with:arg3
   928     self hasCode ifFalse:[
   928     self hasCode ifFalse:[
   929         self prepareInvoke.
   929         self prepareInvoke.
   930     ].
   930     ].
   931     ^ self 
   931     ^ self 
   932         invokeFFIwithArguments:(Array with:arg1 with:arg2 with:arg3) 
   932         invokeFFIwithArguments:(Array with:arg1 with:arg2 with:arg3) 
   933         forCPPInstance:anInstance
   933         forInstance:anInstance
   934 
   934 
   935     "Modified (comment): / 04-03-2019 / 09:39:43 / Claus Gittinger"
   935     "Modified (comment): / 04-03-2019 / 09:39:43 / Claus Gittinger"
   936 !
   936 !
   937 
   937 
   938 invokeCPPVirtualOn:anInstance with:arg1 with:arg2 with:arg3 with:arg4
   938 invokeCPPVirtualOn:anInstance with:arg1 with:arg2 with:arg3 with:arg4
   941     self hasCode ifFalse:[
   941     self hasCode ifFalse:[
   942         self prepareInvoke.
   942         self prepareInvoke.
   943     ].
   943     ].
   944     ^ self 
   944     ^ self 
   945         invokeFFIwithArguments:(Array with:arg1 with:arg2 with:arg3 with:arg4) 
   945         invokeFFIwithArguments:(Array with:arg1 with:arg2 with:arg3 with:arg4) 
   946         forCPPInstance:anInstance
   946         forInstance:anInstance
   947 
   947 
   948     "Modified (comment): / 04-03-2019 / 09:39:48 / Claus Gittinger"
   948     "Modified (comment): / 04-03-2019 / 09:39:48 / Claus Gittinger"
   949 !
   949 !
   950 
   950 
   951 invokeCPPVirtualOn:anInstance withArguments:args
   951 invokeCPPVirtualOn:anInstance withArguments:args
   954     self hasCode ifFalse:[
   954     self hasCode ifFalse:[
   955         self prepareInvoke.
   955         self prepareInvoke.
   956     ].
   956     ].
   957     ^ self 
   957     ^ self 
   958         invokeFFIwithArguments:args 
   958         invokeFFIwithArguments:args 
   959         forCPPInstance:anInstance
   959         forInstance:anInstance
   960 
   960 
   961     "Modified (comment): / 04-03-2019 / 09:39:57 / Claus Gittinger"
   961     "Modified (comment): / 04-03-2019 / 09:39:57 / Claus Gittinger"
   962 !
   962 !
   963 
   963 
   964 invokeObjCOn:anInstance
   964 invokeObjCOn:anInstance
   965     "call an ObjC method with no arguments"
   965     "call an ObjC method with no arguments"
   966 
   966 
   967     self hasCode ifFalse:[
   967     self hasCode ifFalse:[
   968         self prepareObjCInvoke.
   968         self prepareObjCInvoke.
   969     ].
   969     ].
   970     ^ self invokeFFIwithArguments:nil forCPPInstance:anInstance
   970     ^ self invokeFFIwithArguments:nil forInstance:anInstance
   971 
   971 
   972     "Created: / 04-03-2019 / 09:12:12 / Claus Gittinger"
   972     "Created: / 04-03-2019 / 09:12:12 / Claus Gittinger"
   973 !
   973 !
   974 
   974 
   975 invokeObjCOn:anInstance withArguments:args
   975 invokeObjCOn:anInstance withArguments:args
   976     "call an ObjC method with up to maxArgs (15) arguments"
   976     "call an ObjC method with up to maxArgs (15) arguments"
   977 
   977 
   978     self hasCode ifFalse:[
   978     self hasCode ifFalse:[
   979         self prepareObjCInvoke.
   979         self prepareObjCInvoke.
   980     ].
   980     ].
   981     ^ self invokeFFIwithArguments:args forCPPInstance:anInstance
   981     ^ self invokeFFIwithArguments:args forInstance:anInstance
   982 
   982 
   983     "Created: / 04-03-2019 / 09:39:03 / Claus Gittinger"
   983     "Created: / 04-03-2019 / 09:39:03 / Claus Gittinger"
   984 !
   984 !
   985 
   985 
   986 invokeWith:arg
   986 invokeWith:arg
   987     "call the function with 1 argument"
   987     "call the function with 1 argument"
   988 
   988 
   989     self hasCode ifFalse:[
   989     self hasCode ifFalse:[
   990         self prepareInvoke.
   990         self prepareInvoke.
   991     ].
   991     ].
   992     ^ self invokeFFIwithArguments:(Array with:arg) forCPPInstance:nil
   992     ^ self invokeFFIwithArguments:(Array with:arg) forInstance:nil
   993 
   993 
   994     "Modified (comment): / 04-03-2019 / 09:41:14 / Claus Gittinger"
   994     "Modified (comment): / 04-03-2019 / 09:41:14 / Claus Gittinger"
   995 !
   995 !
   996 
   996 
   997 invokeWith:arg1 with:arg2
   997 invokeWith:arg1 with:arg2
   998     "call the function with 2 arguments"
   998     "call the function with 2 arguments"
   999 
   999 
  1000     self hasCode ifFalse:[
  1000     self hasCode ifFalse:[
  1001         self prepareInvoke.
  1001         self prepareInvoke.
  1002     ].
  1002     ].
  1003     ^ self invokeFFIwithArguments:(Array with:arg1 with:arg2) forCPPInstance:nil
  1003     ^ self invokeFFIwithArguments:(Array with:arg1 with:arg2) forInstance:nil
  1004 
  1004 
  1005     "Modified (comment): / 04-03-2019 / 09:41:07 / Claus Gittinger"
  1005     "Modified (comment): / 04-03-2019 / 09:41:07 / Claus Gittinger"
  1006 !
  1006 !
  1007 
  1007 
  1008 invokeWith:arg1 with:arg2 with:arg3
  1008 invokeWith:arg1 with:arg2 with:arg3
  1009     "call the function with 3 arguments"
  1009     "call the function with 3 arguments"
  1010 
  1010 
  1011     self hasCode ifFalse:[
  1011     self hasCode ifFalse:[
  1012         self prepareInvoke.
  1012         self prepareInvoke.
  1013     ].
  1013     ].
  1014     ^ self invokeFFIwithArguments:(Array with:arg1 with:arg2 with:arg3) forCPPInstance:nil
  1014     ^ self invokeFFIwithArguments:(Array with:arg1 with:arg2 with:arg3) forInstance:nil
  1015 
  1015 
  1016     "Modified (comment): / 04-03-2019 / 09:41:01 / Claus Gittinger"
  1016     "Modified (comment): / 04-03-2019 / 09:41:01 / Claus Gittinger"
  1017 !
  1017 !
  1018 
  1018 
  1019 invokeWith:arg1 with:arg2 with:arg3 with:arg4
  1019 invokeWith:arg1 with:arg2 with:arg3 with:arg4
  1020     "call the function with 4 arguments"
  1020     "call the function with 4 arguments"
  1021 
  1021 
  1022     self hasCode ifFalse:[
  1022     self hasCode ifFalse:[
  1023         self prepareInvoke.
  1023         self prepareInvoke.
  1024     ].
  1024     ].
  1025     ^ self invokeFFIwithArguments:(Array with:arg1 with:arg2 with:arg3 with:arg4) forCPPInstance:nil
  1025     ^ self invokeFFIwithArguments:(Array with:arg1 with:arg2 with:arg3 with:arg4) forInstance:nil
  1026 
  1026 
  1027     "Modified (comment): / 04-03-2019 / 09:40:56 / Claus Gittinger"
  1027     "Modified (comment): / 04-03-2019 / 09:40:56 / Claus Gittinger"
  1028 !
  1028 !
  1029 
  1029 
  1030 invokeWithArguments:argArray
  1030 invokeWithArguments:argArray
  1031     "call the function with up to maxArgs (15) arguments"
  1031     "call the function with up to maxArgs (15) arguments"
  1032 
  1032 
  1033     self hasCode ifFalse:[
  1033     self hasCode ifFalse:[
  1034         self prepareInvoke.
  1034         self prepareInvoke.
  1035     ].
  1035     ].
  1036     ^ self invokeFFIwithArguments:argArray forCPPInstance:nil
  1036     ^ self invokeFFIwithArguments:argArray forInstance:nil
  1037 
  1037 
  1038     "Modified: / 01-08-2006 / 16:04:08 / cg"
  1038     "Modified: / 01-08-2006 / 16:04:08 / cg"
  1039     "Modified (comment): / 04-03-2019 / 09:40:48 / Claus Gittinger"
  1039     "Modified (comment): / 04-03-2019 / 09:40:48 / Claus Gittinger"
  1040 ! !
  1040 ! !
  1041 
  1041 
  1300 ! !
  1300 ! !
  1301 
  1301 
  1302 !ExternalLibraryFunction methodsFor:'private-invoking'!
  1302 !ExternalLibraryFunction methodsFor:'private-invoking'!
  1303 
  1303 
  1304 invokeCPPVirtualFFIOn:instance withArguments:arguments
  1304 invokeCPPVirtualFFIOn:instance withArguments:arguments
  1305     ^ self invokeFFIwithArguments:arguments forCPPInstance:instance
  1305     ^ self invokeFFIwithArguments:arguments forInstance:instance
  1306 
  1306 
  1307     "Modified: / 01-08-2006 / 13:55:30 / cg"
  1307     "Modified: / 01-08-2006 / 13:55:30 / cg"
  1308 !
  1308 !
  1309 
  1309 
  1310 invokeFFIWithArguments:arguments
  1310 invokeFFIWithArguments:arguments
  1311     ^ self invokeFFIwithArguments:arguments forCPPInstance:nil
  1311     ^ self invokeFFIwithArguments:arguments forInstance:nil
  1312 
  1312 
  1313     "Modified: / 01-08-2006 / 13:55:35 / cg"
  1313     "Modified: / 01-08-2006 / 13:55:35 / cg"
  1314 !
  1314 !
  1315 
  1315 
  1316 invokeFFIwithArguments:argumentsOrNilIn forCPPInstance:aReceiverOrNil
  1316 invokeFFIwithArguments:argumentsOrNilIn forInstance:aReceiverOrNil
  1317     "basic invoke mechanism. Calls the function represented by the receiver with argumentsOrNil.
  1317     "basic invoke mechanism. Calls the function represented by the receiver with argumentsOrNil.
  1318      For cplusplus, aReceiverOrNil is required to be an externalStructure like object;
  1318      For cplusplus, aReceiverOrNil is required to be an externalStructure like object;
  1319      for objectiveC, it must be an ObjectiveC object"
  1319      for objectiveC, it must be an ObjectiveC object"
  1320 
  1320 
  1321     |argTypeSymbols returnTypeSymbol failureCode failureArgNr failureInfo returnValue stClass vtOffset
  1321     |argTypeSymbols returnTypeSymbol failureCode failureArgNr failureInfo returnValue stClass vtOffset
  2867             ((oldReturnType ~= returnType) or:[oldArgumentTypes ~= argumentTypes]) ifTrue:[
  2867             ((oldReturnType ~= returnType) or:[oldArgumentTypes ~= argumentTypes]) ifTrue:[
  2868                 thisContext restart
  2868                 thisContext restart
  2869             ].
  2869             ].
  2870         ].
  2870         ].
  2871         (failureCode == #BadArgForAsyncCall) ifTrue:[
  2871         (failureCode == #BadArgForAsyncCall) ifTrue:[
  2872             ^ self tryAgainWithAsyncSafeArguments:argumentsOrNil forCPPInstance:aReceiverOrNil
  2872             ^ self tryAgainWithAsyncSafeArguments:argumentsOrNil forInstance:aReceiverOrNil
  2873         ].
  2873         ].
  2874         (failureCode == #FFINotSupported) ifTrue:[
  2874         (failureCode == #FFINotSupported) ifTrue:[
  2875             ^ self primitiveFailed:'FFI support missing in this build'.
  2875             ^ self primitiveFailed:'FFI support missing in this build'.
  2876         ].
  2876         ].
  2877 
  2877 
  2919     "Modified: / 15-05-2017 / 19:48:52 / mawalch"
  2919     "Modified: / 15-05-2017 / 19:48:52 / mawalch"
  2920     "Modified: / 01-08-2017 / 14:03:52 / cg"
  2920     "Modified: / 01-08-2017 / 14:03:52 / cg"
  2921     "Modified: / 01-03-2019 / 15:40:14 / Claus Gittinger"
  2921     "Modified: / 01-03-2019 / 15:40:14 / Claus Gittinger"
  2922 !
  2922 !
  2923 
  2923 
  2924 tryAgainWithAsyncSafeArguments:argumentsOrNil forCPPInstance:aReceiverOrNil
  2924 tryAgainWithAsyncSafeArguments:argumentsOrNil forInstance:aReceiverOrNil
  2925     "invoked by the call primitive, iff GC-unsave arguments where passed to the call.
  2925     "invoked by the call primitive, iff GC-unsave arguments where passed to the call.
  2926      Here, allocate non-movable blocks of memory and copy the arguments into them,
  2926      Here, allocate non-movable blocks of memory and copy the arguments into them,
  2927      then try the call again, copy changed values back, and release the memeory."
  2927      then try the call again, copy changed values back, and release the memeory."
  2928 
  2928 
  2929     |saveArguments anyBadArg result originalToSaveArgMapping|
  2929     |saveArguments anyBadArg result originalToSaveArgMapping|
  2963     anyBadArg ifFalse:[
  2963     anyBadArg ifFalse:[
  2964 	"avoid recursion..."
  2964 	"avoid recursion..."
  2965 	^ self primitiveFailed
  2965 	^ self primitiveFailed
  2966     ].
  2966     ].
  2967 
  2967 
  2968     result := self invokeFFIwithArguments:saveArguments forCPPInstance:aReceiverOrNil.
  2968     result := self invokeFFIwithArguments:saveArguments forInstance:aReceiverOrNil.
  2969 
  2969 
  2970     "/ copy back !!
  2970     "/ copy back !!
  2971     originalToSaveArgMapping keysAndValuesDo:[:arg :saveArg |
  2971     originalToSaveArgMapping keysAndValuesDo:[:arg :saveArg |
  2972 	arg isSymbol ifFalse:[
  2972 	arg isSymbol ifFalse:[
  2973 	    arg replaceFrom:1 to:(arg size) with:saveArg startingAt:1.
  2973 	    arg replaceFrom:1 to:(arg size) with:saveArg startingAt:1.