2017-02-14 08:29:30 +00:00
|
|
|
class _FFI(Object) from 'ffi'
|
2017-01-10 10:50:26 +00:00
|
|
|
{
|
2017-03-30 14:59:55 +00:00
|
|
|
method(#primitive) open(name).
|
2017-03-31 14:21:22 +00:00
|
|
|
method(#primitive) close.
|
2017-03-30 14:59:55 +00:00
|
|
|
method(#primitive) getsym(name).
|
|
|
|
|
|
|
|
(* TODO: make call variadic? method(#primitive,#variadic) call (func, sig). *)
|
|
|
|
method(#primitive) call(func, sig, args).
|
2017-01-10 10:50:26 +00:00
|
|
|
}
|
|
|
|
|
2017-01-09 13:41:11 +00:00
|
|
|
class FFI(Object)
|
|
|
|
{
|
2017-04-19 16:46:44 +00:00
|
|
|
var 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-03-30 14:59:55 +00:00
|
|
|
x := self.ffi open(name).
|
2017-01-10 10:50:26 +00:00
|
|
|
(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: [
|
2017-03-30 14:59:55 +00:00
|
|
|
f := self.ffi getsym(name).
|
2017-01-10 10:50:26 +00:00
|
|
|
(f isError) ifTrue: [^f].
|
|
|
|
self.funcs at: name put: f.
|
|
|
|
].
|
2017-01-10 13:56:19 +00:00
|
|
|
|
|
|
|
^self.ffi call(f, sig, args)
|
2017-01-09 13:41:11 +00:00
|
|
|
}
|
|
|
|
}
|