2017-01-10 10:50:26 +00:00
|
|
|
class(#byte) _FFI(Module) from 'ffi'
|
|
|
|
{
|
2017-01-10 13:56:19 +00:00
|
|
|
(*
|
|
|
|
* the ffi module installs the following methods
|
|
|
|
* method(#class) _newInstSize
|
|
|
|
* method open: name
|
|
|
|
* method close
|
|
|
|
* method call
|
|
|
|
* method call: func sig: sig with: args.
|
|
|
|
*)
|
2017-01-10 10:50:26 +00:00
|
|
|
}
|
|
|
|
|
2017-01-09 13:41:11 +00:00
|
|
|
class FFI(Object)
|
|
|
|
{
|
2017-01-10 10:50:26 +00:00
|
|
|
dcl name ffi funcs.
|
2017-01-09 13:41:11 +00:00
|
|
|
|
|
|
|
method(#class) new: aString
|
|
|
|
{
|
|
|
|
^self new open: aString.
|
|
|
|
}
|
|
|
|
|
2017-01-10 10:50:26 +00:00
|
|
|
method initialize
|
2017-01-09 13:41:11 +00:00
|
|
|
{
|
|
|
|
self.funcs := Dictionary new.
|
2017-01-10 10:50:26 +00:00
|
|
|
self.ffi := _FFI new.
|
|
|
|
}
|
2017-01-09 13:41:11 +00:00
|
|
|
|
2017-01-10 10:50:26 +00:00
|
|
|
method open: name
|
|
|
|
{
|
|
|
|
| x |
|
|
|
|
self.funcs removeAllKeys.
|
|
|
|
self.name := name.
|
2017-01-09 13:41:11 +00:00
|
|
|
|
2017-01-10 10:50:26 +00:00
|
|
|
x := self.ffi open: name.
|
|
|
|
(x isError) ifTrue: [^x].
|
2017-01-09 13:41:11 +00:00
|
|
|
|
|
|
|
^self.
|
|
|
|
}
|
|
|
|
|
|
|
|
method close
|
|
|
|
{
|
2017-01-10 10:50:26 +00:00
|
|
|
self.ffi close.
|
2017-01-09 13:41:11 +00:00
|
|
|
}
|
|
|
|
|
2017-01-10 10:50:26 +00:00
|
|
|
method call: name signature: sig arguments: args
|
2017-01-09 13:41:11 +00:00
|
|
|
{
|
|
|
|
| f |
|
2017-01-10 10:50:26 +00:00
|
|
|
f := self.funcs at: name.
|
|
|
|
(f isError) ifTrue: [
|
|
|
|
f := self.ffi getsym: name.
|
|
|
|
(f isError) ifTrue: [^f].
|
|
|
|
self.funcs at: name put: f.
|
|
|
|
].
|
2017-01-10 13:56:19 +00:00
|
|
|
|
|
|
|
(*^self.ffi call: f sig: sig with: args*)
|
|
|
|
^self.ffi call(f, sig, args)
|
2017-01-09 13:41:11 +00:00
|
|
|
}
|
|
|
|
}
|