ExternalLibraryFunction.st
author Claus Gittinger <cg@exept.de>
Tue, 25 Apr 2006 17:57:26 +0200
changeset 9328 e30967f7ce5f
parent 9327 9c15276d61e3
child 9329 d1dba8a3752b
permissions -rw-r--r--
*** empty log message ***
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8728
d70396dc4e96 copyright
Claus Gittinger <cg@exept.de>
parents: 8550
diff changeset
     1
"
d70396dc4e96 copyright
Claus Gittinger <cg@exept.de>
parents: 8550
diff changeset
     2
 COPYRIGHT (c) 2004 by eXept Software AG
9322
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
     3
	      All Rights Reserved
8728
d70396dc4e96 copyright
Claus Gittinger <cg@exept.de>
parents: 8550
diff changeset
     4
d70396dc4e96 copyright
Claus Gittinger <cg@exept.de>
parents: 8550
diff changeset
     5
 This software is furnished under a license and may be used
d70396dc4e96 copyright
Claus Gittinger <cg@exept.de>
parents: 8550
diff changeset
     6
 only in accordance with the terms of that license and with the
d70396dc4e96 copyright
Claus Gittinger <cg@exept.de>
parents: 8550
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
d70396dc4e96 copyright
Claus Gittinger <cg@exept.de>
parents: 8550
diff changeset
     8
 be provided or otherwise made available to, or used by, any
d70396dc4e96 copyright
Claus Gittinger <cg@exept.de>
parents: 8550
diff changeset
     9
 other person.  No title to or ownership of the software is
d70396dc4e96 copyright
Claus Gittinger <cg@exept.de>
parents: 8550
diff changeset
    10
 hereby transferred.
d70396dc4e96 copyright
Claus Gittinger <cg@exept.de>
parents: 8550
diff changeset
    11
"
d70396dc4e96 copyright
Claus Gittinger <cg@exept.de>
parents: 8550
diff changeset
    12
8533
9065c547ea75 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    13
"{ Package: 'stx:libbasic' }"
9065c547ea75 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    14
9065c547ea75 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    15
ExternalFunction subclass:#ExternalLibraryFunction
8891
c30a030ff5ec more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents: 8728
diff changeset
    16
	instanceVariableNames:'flags moduleName callType returnType argumentTypes'
8533
9065c547ea75 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    17
	classVariableNames:''
9065c547ea75 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    18
	poolDictionaries:''
9065c547ea75 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    19
	category:'System-Support'
9065c547ea75 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    20
!
9065c547ea75 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    21
9322
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
    22
!ExternalLibraryFunction primitiveDefinitions!
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
    23
%{
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
    24
# include <ffi.h>
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
    25
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
    26
# define MAX_ARGS    128
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
    27
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
    28
%}
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
    29
! !
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
    30
8728
d70396dc4e96 copyright
Claus Gittinger <cg@exept.de>
parents: 8550
diff changeset
    31
!ExternalLibraryFunction class methodsFor:'documentation'!
d70396dc4e96 copyright
Claus Gittinger <cg@exept.de>
parents: 8550
diff changeset
    32
d70396dc4e96 copyright
Claus Gittinger <cg@exept.de>
parents: 8550
diff changeset
    33
copyright
d70396dc4e96 copyright
Claus Gittinger <cg@exept.de>
parents: 8550
diff changeset
    34
"
d70396dc4e96 copyright
Claus Gittinger <cg@exept.de>
parents: 8550
diff changeset
    35
 COPYRIGHT (c) 2004 by eXept Software AG
9322
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
    36
	      All Rights Reserved
8728
d70396dc4e96 copyright
Claus Gittinger <cg@exept.de>
parents: 8550
diff changeset
    37
d70396dc4e96 copyright
Claus Gittinger <cg@exept.de>
parents: 8550
diff changeset
    38
 This software is furnished under a license and may be used
d70396dc4e96 copyright
Claus Gittinger <cg@exept.de>
parents: 8550
diff changeset
    39
 only in accordance with the terms of that license and with the
d70396dc4e96 copyright
Claus Gittinger <cg@exept.de>
parents: 8550
diff changeset
    40
 inclusion of the above copyright notice.   This software may not
d70396dc4e96 copyright
Claus Gittinger <cg@exept.de>
parents: 8550
diff changeset
    41
 be provided or otherwise made available to, or used by, any
d70396dc4e96 copyright
Claus Gittinger <cg@exept.de>
parents: 8550
diff changeset
    42
 other person.  No title to or ownership of the software is
d70396dc4e96 copyright
Claus Gittinger <cg@exept.de>
parents: 8550
diff changeset
    43
 hereby transferred.
d70396dc4e96 copyright
Claus Gittinger <cg@exept.de>
parents: 8550
diff changeset
    44
"
9322
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
    45
!
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
    46
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
    47
example
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
    48
"
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
    49
        |f|
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
    50
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
    51
        f := ExternalLibraryFunction new.
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
    52
        f name:'MessageBeep'
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
    53
          module:'user32.dll'
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
    54
          callType:#WINAPI
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
    55
          returnType:#boolean
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
    56
          argumentTypes:#(uint).
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
    57
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
    58
        f invokeWith:1.
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
    59
"
8728
d70396dc4e96 copyright
Claus Gittinger <cg@exept.de>
parents: 8550
diff changeset
    60
! !
8533
9065c547ea75 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
    61
8550
72982f85bd41 *** empty log message ***
ca
parents: 8533
diff changeset
    62
!ExternalLibraryFunction class methodsFor:'instance creation'!
72982f85bd41 *** empty log message ***
ca
parents: 8533
diff changeset
    63
9322
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
    64
name:functionName module:moduleName callType:callType
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
    65
		  returnType:returnType argumentTypes:argTypes
8550
72982f85bd41 *** empty log message ***
ca
parents: 8533
diff changeset
    66
    ^ self new
9322
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
    67
	name:functionName module:moduleName callType:callType
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
    68
	returnType:returnType argumentTypes:argTypes
8550
72982f85bd41 *** empty log message ***
ca
parents: 8533
diff changeset
    69
! !
72982f85bd41 *** empty log message ***
ca
parents: 8533
diff changeset
    70
9321
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
    71
!ExternalLibraryFunction class methodsFor:'constants'!
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
    72
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
    73
callTypeAPI
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
    74
    ^ #callTypeAPI
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
    75
!
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
    76
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
    77
callTypeC
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
    78
    ^ #callTypeC
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
    79
!
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
    80
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
    81
callTypeCDecl
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
    82
    ^ #callTypeCDecl
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
    83
!
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
    84
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
    85
callTypeOLE
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
    86
    ^ #callTypeOLE
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
    87
! !
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
    88
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
    89
!ExternalLibraryFunction methodsFor:'accessing'!
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
    90
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
    91
argumentTypes
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
    92
    ^ argumentTypes
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
    93
! !
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
    94
8891
c30a030ff5ec more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents: 8728
diff changeset
    95
!ExternalLibraryFunction methodsFor:'invoking'!
c30a030ff5ec more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents: 8728
diff changeset
    96
c30a030ff5ec more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents: 8728
diff changeset
    97
invoke
9321
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
    98
    self hasCode ifFalse:[
9322
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
    99
	self prepareInvoke.
8891
c30a030ff5ec more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents: 8728
diff changeset
   100
    ].
9321
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   101
    ^ self invokeFFIWithArguments:#()
8891
c30a030ff5ec more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents: 8728
diff changeset
   102
!
c30a030ff5ec more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents: 8728
diff changeset
   103
c30a030ff5ec more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents: 8728
diff changeset
   104
invokeWith:arg
9321
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   105
    self hasCode ifFalse:[
9322
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
   106
	self prepareInvoke.
9321
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   107
    ].
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   108
    ^ self invokeFFIWithArguments:(Array with:arg)
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   109
!
8891
c30a030ff5ec more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents: 8728
diff changeset
   110
9321
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   111
invokeWith:arg1 with:arg2
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   112
    self hasCode ifFalse:[
9322
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
   113
	self prepareInvoke.
8891
c30a030ff5ec more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents: 8728
diff changeset
   114
    ].
9321
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   115
    ^ self invokeFFIWithArguments:(Array with:arg1 with:arg2)
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   116
!
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   117
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   118
invokeWith:arg1 with:arg2 with:arg3
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   119
    self hasCode ifFalse:[
9322
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
   120
	self prepareInvoke.
9321
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   121
    ].
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   122
    ^ self invokeFFIWithArguments:(Array with:arg1 with:arg2 with:arg3)
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   123
!
8891
c30a030ff5ec more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents: 8728
diff changeset
   124
9321
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   125
invokeWith:arg1 with:arg2 with:arg3 with:arg4
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   126
    self hasCode ifFalse:[
9322
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
   127
	self prepareInvoke.
9321
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   128
    ].
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   129
    ^ self invokeFFIWithArguments:(Array with:arg1 with:arg2 with:arg3 with:arg4)
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   130
!
8891
c30a030ff5ec more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents: 8728
diff changeset
   131
9321
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   132
invokeWithArguments:argArray
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   133
    self hasCode ifFalse:[
9322
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
   134
	self prepareInvoke.
9321
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   135
    ].
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   136
    ^ self invokeFFIWithArguments:argArray
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   137
! !
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   138
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   139
!ExternalLibraryFunction methodsFor:'printing'!
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   140
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   141
printOn:aStream
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   142
    aStream nextPutAll:'<'.
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   143
    callType printOn:aStream.
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   144
    aStream nextPutAll:' '.
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   145
    name printOn:aStream.
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   146
    aStream nextPutAll:' module:'.
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   147
    moduleName printOn:aStream.
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   148
    aStream nextPutAll:'>'.
8891
c30a030ff5ec more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents: 8728
diff changeset
   149
! !
c30a030ff5ec more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents: 8728
diff changeset
   150
c30a030ff5ec more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents: 8728
diff changeset
   151
!ExternalLibraryFunction methodsFor:'private'!
c30a030ff5ec more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents: 8728
diff changeset
   152
9321
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   153
invokeFFIWithArguments:arguments
9322
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
   154
    |argTypeSymbols returnTypeSymbol failureCode|
9321
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   155
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   156
    argumentTypes notNil ifTrue:[
9322
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
   157
        argTypeSymbols := argumentTypes collect:[:argType | self ffiTypeSymbolForType:argType].
9321
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   158
    ].
9322
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
   159
    returnTypeSymbol := self ffiTypeSymbolForType:returnType.
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
   160
9324
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   161
%{  /* STACK: 32000 */
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   162
    ffi_cif __cif;
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   163
    ffi_type *__argTypes[MAX_ARGS];
9328
e30967f7ce5f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9327
diff changeset
   164
    ffi_type *__returnType = NULL;
9322
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
   165
    union {
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
   166
        int iVal;
9324
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   167
    } __argValues[MAX_ARGS];
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   168
    void *__argValuePointers[MAX_ARGS];
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   169
    int __numArgs;
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   170
    static int null = 0;
9321
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   171
    int i;
9324
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   172
    ffi_abi __callType = FFI_DEFAULT_ABI;
9321
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   173
9328
e30967f7ce5f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9327
diff changeset
   174
printf("uint type2: %x\n", __get_ffi_type_uint());
e30967f7ce5f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9327
diff changeset
   175
9321
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   176
    if (arguments == nil) {
9324
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   177
        __numArgs = 0;
9322
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
   178
        if (argTypeSymbols != nil) {
9324
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   179
            if (! __isArray(argTypeSymbols)
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   180
             || (__arraySize(argTypeSymbols) != __numArgs)) {
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   181
                failureCode = @symbol(ArgumentCountMismatch);
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   182
                goto error;
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   183
            }
9321
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   184
        }
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   185
    } else {
9324
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   186
        if (! __isArray(arguments)
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   187
         || ! __isArray(argTypeSymbols)
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   188
         || (__arraySize(argTypeSymbols) != (__numArgs = __arraySize(arguments)))) {
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   189
            failureCode = @symbol(ArgumentCountMismatch);
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   190
            goto error;
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   191
        }
9321
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   192
    }
9324
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   193
    if (__numArgs > MAX_ARGS) {
9321
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   194
        failureCode = @symbol(TooManyArguments);
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   195
        goto error;
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   196
    }
9322
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
   197
9328
e30967f7ce5f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9327
diff changeset
   198
    if (returnTypeSymbol == @symbol(int)) {
e30967f7ce5f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9327
diff changeset
   199
printf("ret int\n");
e30967f7ce5f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9327
diff changeset
   200
        __returnType = __get_ffi_type_sint();
e30967f7ce5f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9327
diff changeset
   201
    } else if (returnTypeSymbol == @symbol(uint)) {
e30967f7ce5f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9327
diff changeset
   202
printf("ret uint\n");
e30967f7ce5f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9327
diff changeset
   203
        __returnType = __get_ffi_type_uint();
e30967f7ce5f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9327
diff changeset
   204
    } else if (returnTypeSymbol == @symbol(boolean)) {
e30967f7ce5f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9327
diff changeset
   205
printf("ret boolean\n");
e30967f7ce5f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9327
diff changeset
   206
        __returnType = __get_ffi_type_uint();
e30967f7ce5f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9327
diff changeset
   207
    } else if (returnTypeSymbol == @symbol(void)) {
e30967f7ce5f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9327
diff changeset
   208
printf("void\n");
e30967f7ce5f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9327
diff changeset
   209
        __returnType = __get_ffi_type_void();
e30967f7ce5f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9327
diff changeset
   210
    } else {
e30967f7ce5f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9327
diff changeset
   211
        failureCode = @symbol(UnknownReturnType);
e30967f7ce5f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9327
diff changeset
   212
        goto error;
e30967f7ce5f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9327
diff changeset
   213
    }
e30967f7ce5f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9327
diff changeset
   214
e30967f7ce5f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9327
diff changeset
   215
    for (i=0; i<__numArgs; i++) {
9324
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   216
        ffi_type *thisType;
9322
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
   217
        void *argValuePtr;
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
   218
        OBJ typeSymbol;
9324
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   219
        OBJ arg;
9322
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
   220
9328
e30967f7ce5f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9327
diff changeset
   221
printf("arg%d\n", i);
e30967f7ce5f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9327
diff changeset
   222
        typeSymbol = __ArrayInstPtr(argTypeSymbols)->a_element[i];
e30967f7ce5f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9327
diff changeset
   223
        arg = __ArrayInstPtr(arguments)->a_element[i];
9324
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   224
9322
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
   225
        if (typeSymbol == @symbol(int)) {
9328
e30967f7ce5f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9327
diff changeset
   226
            thisType = __get_ffi_type_sint();
9324
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   227
            if (__isSmallInteger(arg)) {
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   228
                __argValues[i].iVal = __intVal(arg);
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   229
            } else {
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   230
                __argValues[i].iVal = __signedLongIntVal(arg);
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   231
                if (__argValues[i].iVal == 0) {
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   232
                    failureCode = @symbol(InvalidArgument);
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   233
                    goto error;
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   234
                }
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   235
            }
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   236
            argValuePtr = &(__argValues[i].iVal);
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   237
        } else if (typeSymbol == @symbol(uint)) {
9328
e30967f7ce5f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9327
diff changeset
   238
printf("uint\n");
e30967f7ce5f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9327
diff changeset
   239
            thisType = __get_ffi_type_uint();
9324
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   240
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   241
            if (__isSmallInteger(arg)) {
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   242
                __argValues[i].iVal = __intVal(arg);
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   243
            } else {
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   244
                __argValues[i].iVal = __unsignedLongIntVal(arg);
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   245
                if (__argValues[i].iVal == 0) {
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   246
                    failureCode = @symbol(InvalidArgument);
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   247
                    goto error;
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   248
                }
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   249
            }
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   250
            argValuePtr = &(__argValues[i].iVal);
9322
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
   251
        } else if (typeSymbol == @symbol(void)) {
9328
e30967f7ce5f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9327
diff changeset
   252
printf("void\n");
e30967f7ce5f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9327
diff changeset
   253
            thisType = __get_ffi_type_void();
9322
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
   254
            argValuePtr = &null;
9324
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   255
        } else if (typeSymbol == @symbol(boolean)) {
9328
e30967f7ce5f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9327
diff changeset
   256
printf("boolean\n");
e30967f7ce5f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9327
diff changeset
   257
            thisType = __get_ffi_type_uint();
9324
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   258
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   259
            if (arg == true) {
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   260
                __argValues[i].iVal = 1;
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   261
            } else if (arg == false) {
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   262
                __argValues[i].iVal = 0;
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   263
            } else if (__isSmallInteger(arg)) {
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   264
                __argValues[i].iVal = __intVal(arg);
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   265
            } else {
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   266
                __argValues[i].iVal = __unsignedLongIntVal(arg);
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   267
                if (__argValues[i].iVal == 0) {
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   268
                    failureCode = @symbol(InvalidArgument);
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   269
                    goto error;
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   270
                }
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   271
            }
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   272
            argValuePtr = &(__argValues[i].iVal);
9322
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
   273
        } else {
9328
e30967f7ce5f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9327
diff changeset
   274
            failureCode = @symbol(UnknownArgumentType);
9322
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
   275
            goto error;
9321
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   276
        }
9322
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
   277
9328
e30967f7ce5f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9327
diff changeset
   278
        __argTypes[i] = thisType;
e30967f7ce5f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9327
diff changeset
   279
        __argValuePointers[i] = argValuePtr;
e30967f7ce5f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9327
diff changeset
   280
printf("__argType[%d]: %x\n", i, thisType);
9321
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   281
    }
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   282
9324
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   283
    if (__INST(callType) == @symbol(callTypeC)) {
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   284
        __callType = FFI_DEFAULT_ABI;
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   285
#ifdef WIN32
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   286
    } else if (__INST(callType) == @symbol(callTypeAPI)) {
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   287
        __callType = FFI_STDCALL;
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   288
    } else if (__INST(callType) == @symbol(WINAPI)) {
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   289
        __callType = FFI_STDCALL;
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   290
#endif /* WIN32 */
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   291
    } else if (__INST(callType) == @symbol(callTypeCDecl)) {
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   292
        __callType = FFI_DEFAULT_ABI;
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   293
    } else {
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   294
        failureCode = @symbol(UnknownCallType);
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   295
        goto error;
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   296
    }
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   297
9328
e30967f7ce5f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9327
diff changeset
   298
printf("return type: %x\n", __returnType);
e30967f7ce5f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9327
diff changeset
   299
printf("return type size: %x\n", __returnType->size);
e30967f7ce5f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9327
diff changeset
   300
printf("return type alignment: %x\n", __returnType->alignment);
e30967f7ce5f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9327
diff changeset
   301
e30967f7ce5f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9327
diff changeset
   302
printf("arg type: %x\n", __argTypes[0]);
e30967f7ce5f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9327
diff changeset
   303
printf("arg type size: %x\n", __argTypes[0]->size);
e30967f7ce5f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9327
diff changeset
   304
printf("arg type alignment: %x\n", __argTypes[0]->alignment);
e30967f7ce5f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9327
diff changeset
   305
9324
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   306
    if (ffi_prep_cif(&__cif, __callType, __numArgs, __returnType, __argTypes) != FFI_OK) {
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   307
        failureCode = @symbol(FFIPrepareFailed);
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   308
        goto error;
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   309
    }
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   310
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   311
9322
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
   312
# if 0
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
   313
    /* Initialize the cif */
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
   314
    CHECK(ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 1,
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
   315
                       &ffi_type_sint64, args) == FFI_OK);
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
   316
9321
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   317
    argtypes = (ffi_type **)C_alloca(sizeof(ffi_type *) * (n + 3));
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   318
    argvalues = (void **)C_alloca(sizeof(void *) * (n + 3));
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   319
    argtypes[ 0 ] = &ffi_type_pointer;
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   320
    argtypes[ 1 ] = &ffi_type_pointer;
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   321
    argtypes[ 2 ] = &ffi_type_pointer;
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   322
    c = n + 2;
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   323
    argvalues[ 0 ] = &c;
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   324
    argvalues[ 1 ] = &fn;
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   325
    argvalues[ 2 ] = &k;
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   326
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   327
    for(i = 0; i < n; ++i) {
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   328
      argtypes[ i + 3 ] = &ffi_type_pointer;
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   329
      argvalues[ i + 3 ] = C_temporary_stack_bottom - (i + 1);
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   330
    }
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   331
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   332
    C_temporary_stack = C_temporary_stack_bottom;
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   333
    status = ffi_prep_cif(&cif, FFI_DEFAULT_ABI, n + 3, &ffi_type_void, argtypes);
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   334
    assert(status == FFI_OK);
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   335
    ffi_call(&cif, (void *)C_block_item(fn, 0), NULL, argvalues);
9322
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
   336
# endif
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
   337
9321
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   338
error: ;
9324
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   339
%}.
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   340
    failureCode notNil ifTrue:[
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   341
        self primitiveFailed
96279896d95f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9322
diff changeset
   342
    ].
8891
c30a030ff5ec more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents: 8728
diff changeset
   343
!
c30a030ff5ec more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents: 8728
diff changeset
   344
c30a030ff5ec more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents: 8728
diff changeset
   345
linkToModule
c30a030ff5ec more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents: 8728
diff changeset
   346
    "link this function to the external module.
c30a030ff5ec more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents: 8728
diff changeset
   347
     I.e. retrieve the module handle and the code pointer."
c30a030ff5ec more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents: 8728
diff changeset
   348
c30a030ff5ec more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents: 8728
diff changeset
   349
    |handle code|
c30a030ff5ec more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents: 8728
diff changeset
   350
9321
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   351
    moduleName isNil ifTrue:[
9322
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
   352
	self error:'Missing moduleName'.
9321
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   353
    ].
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   354
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   355
    handle := ObjectFileLoader loadDynamicObject:moduleName.
8891
c30a030ff5ec more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents: 8728
diff changeset
   356
    handle isNil ifTrue:[
9322
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
   357
	self error:'Cannot load module: ', moduleName.
8891
c30a030ff5ec more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents: 8728
diff changeset
   358
    ].
c30a030ff5ec more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents: 8728
diff changeset
   359
    moduleHandle := handle.
c30a030ff5ec more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents: 8728
diff changeset
   360
    code := moduleHandle getFunctionAddress:name into:self.
c30a030ff5ec more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents: 8728
diff changeset
   361
    code isNil ifTrue:[
9322
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
   362
	self error:'Missing function: ', name, ' in module: ', moduleName.
9321
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   363
    ].
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   364
!
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   365
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   366
prepareInvoke
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   367
    self hasCode ifFalse:[
9322
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
   368
	moduleHandle isNil ifTrue:[
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
   369
	    self linkToModule.
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
   370
	    self setupFFI.
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
   371
	].
8891
c30a030ff5ec more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents: 8728
diff changeset
   372
    ].
c30a030ff5ec more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents: 8728
diff changeset
   373
!
c30a030ff5ec more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents: 8728
diff changeset
   374
c30a030ff5ec more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents: 8728
diff changeset
   375
setupFFI
c30a030ff5ec more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents: 8728
diff changeset
   376
    "setup foreign function interface"
c30a030ff5ec more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents: 8728
diff changeset
   377
9321
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   378
"/    %{
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   379
"/    #if defined(WIN32) && defined(i386)
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   380
"/        /* Have special code for this case - no need to use of ffi code. */
734c7c432461 not yet finished
Claus Gittinger <cg@exept.de>
parents: 8891
diff changeset
   381
"/    #else
9322
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
   382
"/        if (ffi_prep_cif(&cif, FFI_DEFAULT_ABI, 1, &ffi_type_uint, args)) 
8891
c30a030ff5ec more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents: 8728
diff changeset
   383
! !
c30a030ff5ec more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents: 8728
diff changeset
   384
8550
72982f85bd41 *** empty log message ***
ca
parents: 8533
diff changeset
   385
!ExternalLibraryFunction methodsFor:'private-accessing'!
72982f85bd41 *** empty log message ***
ca
parents: 8533
diff changeset
   386
9327
9c15276d61e3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9324
diff changeset
   387
ffiTypeSymbolForType:aType
9322
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
   388
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
   389
    "/ kludge for those who do not have the CType package...
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
   390
    aType isSymbol ifTrue:[ ^ aType ].
9327
9c15276d61e3 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9324
diff changeset
   391
9322
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
   392
    CType isNil ifTrue:[
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
   393
        self error:'unknown type'.
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
   394
    ].
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
   395
    ^ aType typeSymbol
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
   396
!
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
   397
41c391bfbf03 *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9321
diff changeset
   398
name:functionName module:aModuleName callType:aCallType returnType:aReturnType argumentTypes:argTypes
8891
c30a030ff5ec more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents: 8728
diff changeset
   399
    name := functionName.
c30a030ff5ec more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents: 8728
diff changeset
   400
    moduleName := aModuleName.
c30a030ff5ec more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents: 8728
diff changeset
   401
    callType := aCallType.
c30a030ff5ec more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents: 8728
diff changeset
   402
    returnType := aReturnType.
c30a030ff5ec more FFI (foreign function interface) support - still incomplete
Stefan Vogel <sv@exept.de>
parents: 8728
diff changeset
   403
    argumentTypes := argTypes.
8550
72982f85bd41 *** empty log message ***
ca
parents: 8533
diff changeset
   404
! !
72982f85bd41 *** empty log message ***
ca
parents: 8533
diff changeset
   405
8533
9065c547ea75 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   406
!ExternalLibraryFunction class methodsFor:'documentation'!
9065c547ea75 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   407
9065c547ea75 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   408
version
9328
e30967f7ce5f *** empty log message ***
Claus Gittinger <cg@exept.de>
parents: 9327
diff changeset
   409
    ^ '$Header: /cvs/stx/stx/libbasic/ExternalLibraryFunction.st,v 1.9 2006-04-25 15:57:26 cg Exp $'
8533
9065c547ea75 initial checkin
Claus Gittinger <cg@exept.de>
parents:
diff changeset
   410
! !