ExternalLibraryFunction.st
changeset 24097 fe2d568b25c9
parent 24078 13363abcd11f
child 24098 25db884de25a
equal deleted inserted replaced
24096:98fa8c26986e 24097:fe2d568b25c9
    19 	instanceVariableNames:'flags moduleName returnType argumentTypes owningClass'
    19 	instanceVariableNames:'flags moduleName returnType argumentTypes owningClass'
    20 	classVariableNames:'CALLTYPE_API CALLTYPE_C CALLTYPE_MASK CALLTYPE_OLE
    20 	classVariableNames:'CALLTYPE_API CALLTYPE_C CALLTYPE_MASK CALLTYPE_OLE
    21 		CALLTYPE_UNIX64 CALLTYPE_V8 CALLTYPE_V9 DLLPATH DllMapping
    21 		CALLTYPE_UNIX64 CALLTYPE_V8 CALLTYPE_V9 DLLPATH DllMapping
    22 		FLAG_ASYNC FLAG_NONVIRTUAL FLAG_OBJECTIVEC FLAG_RETVAL_IS_CONST
    22 		FLAG_ASYNC FLAG_NONVIRTUAL FLAG_OBJECTIVEC FLAG_RETVAL_IS_CONST
    23 		FLAG_UNLIMITEDSTACK FLAG_VIRTUAL LastModuleHandleHolder
    23 		FLAG_UNLIMITEDSTACK FLAG_VIRTUAL LastModuleHandleHolder
    24 		LastModuleHandleName Verbose FLAG_RETVAL_MUST_FREE'
    24 		LastModuleHandleName Verbose FLAG_RETVAL_MUST_FREE TypeMap'
    25 	poolDictionaries:''
    25 	poolDictionaries:''
    26 	category:'System-Support'
    26 	category:'System-Support'
    27 !
    27 !
    28 
    28 
    29 !ExternalLibraryFunction primitiveDefinitions!
    29 !ExternalLibraryFunction primitiveDefinitions!
   546 
   546 
   547 !ExternalLibraryFunction class methodsFor:'type name mapping'!
   547 !ExternalLibraryFunction class methodsFor:'type name mapping'!
   548 
   548 
   549 ffiTypeSymbolForType:aType
   549 ffiTypeSymbolForType:aType
   550     "map aType to one of the ffi-supported ones:
   550     "map aType to one of the ffi-supported ones:
   551 	sint8, sint16, sint32, sint64
   551         sint8, sint16, sint32, sint64
   552 	uint8, uint16, uint32, uint64
   552         uint8, uint16, uint32, uint64
   553 	long ulong int uint
   553         long ulong int uint
   554 	bool float double void pointer handle
   554         bool float double void pointer handle
   555     "
   555     "
   556 
   556 
       
   557     |mappedType|
       
   558     
       
   559     TypeMap notNil ifTrue:[
       
   560         (mappedType := TypeMap at:aType ifAbsent:nil) notNil ifTrue:[^ mappedType].
       
   561     ].
       
   562     
   557     aType == #sint8           ifTrue:[^ aType ].
   563     aType == #sint8           ifTrue:[^ aType ].
   558     aType == #sint16          ifTrue:[^ aType ].
   564     aType == #sint16          ifTrue:[^ aType ].
   559     aType == #sint32          ifTrue:[^ aType ].
   565     aType == #sint32          ifTrue:[^ aType ].
   560     aType == #sint64          ifTrue:[^ aType ].
   566     aType == #sint64          ifTrue:[^ aType ].
   561     aType == #uint8           ifTrue:[^ aType ].
   567     aType == #uint8           ifTrue:[^ aType ].
   597     aType == #unsignedShort   ifTrue:[^ #uint16 ].
   603     aType == #unsignedShort   ifTrue:[^ #uint16 ].
   598     aType == #unsignedLong    ifTrue:[^ #ulong ].
   604     aType == #unsignedLong    ifTrue:[^ #ulong ].
   599     aType == #unsignedChar    ifTrue:[^ #uint8 ].
   605     aType == #unsignedChar    ifTrue:[^ #uint8 ].
   600 
   606 
   601     "/ windefs
   607     "/ windefs
   602     aType == #dword           ifTrue:[^ #uint32 ].
   608     (aType == #dword or:[aType == #DWORD])   ifTrue:[^ #uint32 ].
   603     aType == #sdword          ifTrue:[^ #sint32 ].
   609     (aType == #sdword or:[aType == #SDWORD]) ifTrue:[^ #sint32 ].
   604     aType == #word            ifTrue:[^ #uint16 ].
   610     (aType == #word or:[aType == #WORD])     ifTrue:[^ #uint16 ].
   605     aType == #sword           ifTrue:[^ #sint16 ].
   611     (aType == #sword or:[aType == #SWORD])   ifTrue:[^ #sint16 ].
   606     aType == #handle          ifTrue:[^ #pointer ].
   612     (aType == #dwordlong or:[aType == #DWORDLONG])   ifTrue:[^ #uint64 ].
       
   613     (aType == #dword32 or:[aType == #DWORD32])   ifTrue:[^ #uint32 ].
       
   614     (aType == #dword64 or:[aType == #DWORD64])   ifTrue:[^ #uint64 ].
       
   615     (aType == #handle or:[aType == #HANDLE])     ifTrue:[^ #pointer ].
       
   616     (aType == #hbitmap or:[aType == #HBITMAP])   ifTrue:[^ #pointer ].
       
   617     (aType == #hdc or:[aType == #HDC])           ifTrue:[^ #pointer ].
       
   618     (aType == #hfile or:[aType == #HFILE])       ifTrue:[^ #int ].
   607     aType == #lpstr           ifTrue:[^ #charPointer ].
   619     aType == #lpstr           ifTrue:[^ #charPointer ].
   608     "/ aType == #hresult         ifTrue:[^ #uint32 ].  -- keep this; it is translated later (in invoke)
       
   609     aType == #boolean         ifTrue:[^ #bool ].
   620     aType == #boolean         ifTrue:[^ #bool ].
   610     "/ care for 64bit machines
   621     "/ care for 64bit machines
   611     aType == #ulongReturn     ifTrue:[^ ExternalBytes sizeofPointer == 8 ifTrue:[#uint64] ifFalse:[#uint32]].
   622     aType == #ulongReturn     ifTrue:[^ ExternalBytes sizeofPointer == 8 ifTrue:[#uint64] ifFalse:[#uint32]].
   612     aType == #none            ifTrue:[^ #void ].
   623     aType == #none            ifTrue:[^ #void ].
   613     aType == #struct          ifTrue:[^ #pointer ].
   624     aType == #struct          ifTrue:[^ #pointer ].
   618 
   629 
   619     aType == #ATOM            ifTrue:[^ #uint16 ].
   630     aType == #ATOM            ifTrue:[^ #uint16 ].
   620     aType == #BOOL            ifTrue:[^ #int ].
   631     aType == #BOOL            ifTrue:[^ #int ].
   621     aType == #BOOLEAN         ifTrue:[^ #uint8 ].
   632     aType == #BOOLEAN         ifTrue:[^ #uint8 ].
   622     aType == #BYTE            ifTrue:[^ #uint8 ].
   633     aType == #BYTE            ifTrue:[^ #uint8 ].
   623     aType == #DWORD           ifTrue:[^ #uint32 ].
   634     (aType == #hresult or:[aType == #HRESULT])   ifTrue:[^ #hresult ].
   624     aType == #HANDLE          ifTrue:[^ #pointer ].
   635     (aType == #lparam or:[aType == #LPARAM])   ifTrue:[^ #pointer ].
   625     aType == #HRESULT         ifTrue:[^ #hresult ].
   636     (aType == #wparam or:[aType == #WPARAM])   ifTrue:[^ #pointer ].
   626 
   637 
   627     aType == #LPWSTR          ifTrue:[^ #wcharPointer].
   638     aType == #LPWSTR          ifTrue:[^ #wcharPointer].
   628     aType == #BSTR            ifTrue:[^ #wcharPointer].
   639     aType == #BSTR            ifTrue:[^ #wcharPointer].
   629 
   640 
   630     "/ care for 64bit machines
   641     "/ care for 64bit machines
   631     aType == #SIZE_T          ifTrue:[^ ExternalBytes sizeofPointer == 8 ifTrue:[#uint64] ifFalse:[#uint32]].
   642     aType == #SIZE_T          ifTrue:[^ ExternalBytes sizeofPointer == 8 ifTrue:[#uint64] ifFalse:[#uint32]].
   632 
   643 
   633     (aType isString or:[aType isSymbol]) ifFalse:[
   644     (aType isString or:[aType isSymbol]) ifFalse:[
   634 	aType isArray ifTrue:[
   645         aType isArray ifTrue:[
   635 	    ^ aType collect:[:each | self ffiTypeSymbolForType:each]
   646             ^ aType collect:[:each | self ffiTypeSymbolForType:each]
   636 	].
   647         ].
   637 
   648 
   638 	CType isNil ifTrue:[
   649         CType isNil ifTrue:[
   639 	    self error:'unknown type'.
   650             self error:'unknown type'.
   640 	].
   651         ].
   641 	"/ assume it is a ctype
   652         "/ assume it is a ctype
   642 	^ aType typeSymbol.
   653         ^ aType typeSymbol.
   643     ].
   654     ].
   644 
   655 
   645     (aType endsWith:'*') ifTrue:[
   656     (aType endsWith:'*') ifTrue:[
   646 	^ #pointer.
   657         ^ #pointer.
   647     ].
   658     ].
   648     (aType endsWith:'Pointer') ifTrue:[
   659     (aType endsWith:'Pointer') ifTrue:[
   649 	^ #pointer.
   660         ^ #pointer.
   650     ].
   661     ].
   651     ^ aType
   662     ^ aType
   652 
   663 
   653     "Modified: / 04-08-2017 / 10:36:37 / cg"
   664     "Modified: / 04-08-2017 / 10:36:37 / cg"
       
   665     "Modified: / 30-04-2019 / 18:13:25 / Claus Gittinger"
       
   666 !
       
   667 
       
   668 mapType:aTypeSymbol toFFI:mappedType
       
   669     "additional user defined type map:
       
   670         eg. self mapType:#INT8 toFFI:#int8
       
   671      allows use of INT8 in external function api declarations.
       
   672      mappedType should be one of the ffi-supported ones:
       
   673         sint8, sint16, sint32, sint64
       
   674         uint8, uint16, uint32, uint64
       
   675         long ulong int uint
       
   676         bool float double void pointer handle"
       
   677 
       
   678     TypeMap isNil ifTrue:[
       
   679         TypeMap := Dictionary new.
       
   680     ].
       
   681     TypeMap at:aTypeSymbol put:mappedType
       
   682 
       
   683     "Created: / 30-04-2019 / 18:15:06 / Claus Gittinger"
   654 ! !
   684 ! !
   655 
   685 
   656 !ExternalLibraryFunction methodsFor:'accessing'!
   686 !ExternalLibraryFunction methodsFor:'accessing'!
   657 
   687 
   658 argumentTypes
   688 argumentTypes