added MOO_VMPRIM_OPENDL_PFMOD.

started refactoring ffi handling code
This commit is contained in:
hyunghwan.chung
2017-01-10 10:50:26 +00:00
parent 95885e4dec
commit faf6ca28e5
16 changed files with 419 additions and 219 deletions

View File

@ -1,69 +1,48 @@
class(#byte) _FFI(Module) from 'ffi'
{
}
class FFI(Object)
{
dcl name handle funcs.
dcl name ffi funcs.
method(#class) new: aString
{
^self new open: aString.
}
method open: aString
method initialize
{
self.funcs := Dictionary new.
self.name := aString.
self.ffi := _FFI new.
}
self.handle := self privateOpen: self.name.
method open: name
{
| x |
self.funcs removeAllKeys.
self.name := name.
"[ self.handle := self privateOpen: self.name ]
on: Exception do: [
]
on: XException do: [
]."
x := self.ffi open: name.
(x isError) ifTrue: [^x].
^self.
}
method close
{
self privateClose: self.handle.
self.handle := nil.
self.ffi close.
}
method call: aFunctionName withSig: aString withArgs: anArray
method call: name signature: sig arguments: args
{
| f |
## 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.
f isNil ifTrue: [ self error: 'No such function' ].
^self privateCall: f withSig: aString withArgs: anArray
}
method privateOpen: aString
{
<primitive: #_ffi_open>
^nil. ## TODO: Error signal: 'can not open'
}
method privateClose: aHandle
{
<primitive: #_ffi_close>
}
method privateCall: aSymbol withSig: aString withArgs: anArray
{
<primitive: #_ffi_call>
}
method privateGetSymbol: aString in: aHandle
{
<primitive: #_ffi_getsym>
^nil.
f := self.funcs at: name.
(f isError) ifTrue: [
f := self.ffi getsym: name.
(f isError) ifTrue: [^f].
self.funcs at: name put: f.
].
^self.ffi call: f sig: sig with: args
}
}

View File

@ -41,7 +41,7 @@ class MyObject(Object)
{
method(#class) main
{
| d a |
| d a ffi |
(*k := Mill new.
k register: #abc call: [ Dictionary new ].
@ -83,5 +83,13 @@ class MyObject(Object)
(*[
[Exception hash dump] ensure: ['xxxx' dump].
] on: Exception do: [:ex | ('Exception caught - ' & ex asString) dump ].*)
ffi := FFI new: '/lib64/libc.so.6'.
(ffi isError)
ifTrue: [System logNl: 'cannot open libc.so' ]
ifFalse: [
(ffi call: #getpid signature: 'i' arguments: nil) dump.
ffi close.
]
}
}

18
moo/kernel/Module.moo Normal file
View File

@ -0,0 +1,18 @@
class(#byte) Module(Object)
{
method(#class) _newInstSize
{
self subclassResponsibility: #_newInstSize
}
method(#class) new: size
{
## ignore the specified size
^(super new: (self _newInstSize))
}
method(#class) new
{
^(super new: (self _newInstSize))
}
}

View File

@ -8,7 +8,8 @@
#include 'Process.moo'.
(* -------------------------------------------------------------------------- *)
#include 'Module.moo'.
(*#include 'FFI.moo'.*)
#include 'FFI.moo'.
#include 'Stdio.moo'.
#include 'Console.moo'.

View File

@ -1,4 +1,5 @@
class(#byte) Stdio(Object) from 'stdio'
class(#byte) Stdio(Module) from 'stdio'
{
dcl(#class) in out err.
@ -9,17 +10,6 @@ class(#byte) Stdio(Object) from 'stdio'
* method close { <primitive: #stdio.close> }
*)
method(#class) new: size
{
## ignore the specified size
^(super new: (self _newInstSize))
}
method(#class) new
{
^(super new: (self _newInstSize))
}
method(#class) open: name for: mode
{
^(self new) open: name for: mode