2015-10-08 14:26:04 +00:00
|
|
|
#include 'Apex.st'.
|
2017-01-06 09:53:40 +00:00
|
|
|
#include 'Context.st'.
|
|
|
|
#include 'Except.st'.
|
2015-10-08 14:26:04 +00:00
|
|
|
#include 'Class.st'.
|
|
|
|
#include 'Boolean.st'.
|
2017-01-06 09:53:40 +00:00
|
|
|
#include 'Magnitu.st'.
|
2016-05-18 14:53:20 +00:00
|
|
|
#include 'Collect.st'.
|
2015-10-15 14:40:08 +00:00
|
|
|
#include 'Process.st'.
|
2015-10-03 15:29:03 +00:00
|
|
|
|
2017-01-06 09:53:40 +00:00
|
|
|
|
|
|
|
class FFI(Object)
|
2015-10-03 15:29:03 +00:00
|
|
|
{
|
2017-01-06 09:53:40 +00:00
|
|
|
dcl name handle funcs.
|
2015-10-03 15:29:03 +00:00
|
|
|
|
2017-01-06 09:53:40 +00:00
|
|
|
method(#class) new: aString
|
2015-10-03 15:29:03 +00:00
|
|
|
{
|
|
|
|
^self new open: aString.
|
|
|
|
}
|
|
|
|
|
2017-01-06 09:53:40 +00:00
|
|
|
method open: aString
|
2015-10-03 15:29:03 +00:00
|
|
|
{
|
|
|
|
self.funcs := Dictionary new.
|
|
|
|
self.name := aString.
|
2015-10-14 13:25:36 +00:00
|
|
|
|
2015-10-03 15:29:03 +00:00
|
|
|
self.handle := self privateOpen: self.name.
|
2015-10-08 14:26:04 +00:00
|
|
|
|
|
|
|
"[ self.handle := self privateOpen: self.name ]
|
2016-12-28 13:42:12 +00:00
|
|
|
on: Exception do: [
|
2015-10-08 14:26:04 +00:00
|
|
|
]
|
2016-12-28 13:42:12 +00:00
|
|
|
on: XException do: [
|
2015-10-08 14:26:04 +00:00
|
|
|
]."
|
|
|
|
|
|
|
|
^self.
|
2015-10-03 15:29:03 +00:00
|
|
|
}
|
|
|
|
|
2017-01-06 09:53:40 +00:00
|
|
|
method close
|
2015-10-03 15:29:03 +00:00
|
|
|
{
|
|
|
|
self privateClose: self.handle.
|
|
|
|
self.handle := nil.
|
|
|
|
}
|
|
|
|
|
2017-01-06 09:53:40 +00:00
|
|
|
method call: aFunctionName withSig: aString withArgs: anArray
|
2015-10-03 15:29:03 +00:00
|
|
|
{
|
|
|
|
| f |
|
|
|
|
|
2015-10-04 16:21:31 +00:00
|
|
|
## f := self.funcs at: aFunctionName.
|
|
|
|
## f isNil ifTrue: [
|
|
|
|
## f := self privateGetSymbol: aFunctionName in: self.handle.
|
|
|
|
## f isNil ifTrue: [ self error: 'No such function' ].
|
|
|
|
## self.funcs at: aFunctionName put: f.
|
|
|
|
## ].
|
|
|
|
f := self privateGetSymbol: aFunctionName in: self.handle.
|
2015-10-08 14:26:04 +00:00
|
|
|
f isNil ifTrue: [ self error: 'No such function' ].
|
2015-10-03 15:29:03 +00:00
|
|
|
|
2015-10-04 16:21:31 +00:00
|
|
|
^self privateCall: f withSig: aString withArgs: anArray
|
2015-10-03 15:29:03 +00:00
|
|
|
}
|
|
|
|
|
2017-01-06 09:53:40 +00:00
|
|
|
method privateOpen: aString
|
2015-10-03 15:29:03 +00:00
|
|
|
{
|
2015-10-15 14:40:08 +00:00
|
|
|
<primitive: #_ffi_open>
|
2015-10-08 14:26:04 +00:00
|
|
|
^nil. ## TODO: Error signal: 'can not open'
|
2015-10-03 15:29:03 +00:00
|
|
|
}
|
|
|
|
|
2017-01-06 09:53:40 +00:00
|
|
|
method privateClose: aHandle
|
2015-10-03 15:29:03 +00:00
|
|
|
{
|
2015-10-15 14:40:08 +00:00
|
|
|
<primitive: #_ffi_close>
|
2015-10-03 15:29:03 +00:00
|
|
|
}
|
|
|
|
|
2017-01-06 09:53:40 +00:00
|
|
|
method privateCall: aSymbol withSig: aString withArgs: anArray
|
2015-10-03 15:29:03 +00:00
|
|
|
{
|
2015-10-15 14:40:08 +00:00
|
|
|
<primitive: #_ffi_call>
|
2015-10-03 15:29:03 +00:00
|
|
|
}
|
|
|
|
|
2017-01-06 09:53:40 +00:00
|
|
|
method privateGetSymbol: aString in: aHandle
|
2015-10-03 15:29:03 +00:00
|
|
|
{
|
2015-10-15 14:40:08 +00:00
|
|
|
<primitive: #_ffi_getsym>
|
2015-10-08 14:26:04 +00:00
|
|
|
^nil.
|
2015-10-03 15:29:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-05-07 01:37:44 +00:00
|
|
|
|
|
|
|
#########################################################################################
|
|
|
|
|
2016-11-18 18:11:13 +00:00
|
|
|
#include 'Stdio.st'.
|
2017-01-05 10:16:04 +00:00
|
|
|
#include 'Console.st'.
|