new FFI
[k8lst.git] / modules / ffi / ffi.st
bloba81ed7bee516f157b3ca72e57703442657ed025e
2  coded by Ketmar // Vampire Avalon (psyc://ketmar.no-ip.org/~Ketmar)
3  Understanding is not required. Only obedience.
5  This program is free software. It comes without any warranty, to
6  the extent permitted by applicable law. You can redistribute it
7  and/or modify it under the terms of the Do What The Fuck You Want
8  To Public License, Version 2, as published by Sam Hocevar. See
9  http://sam.zoy.org/wtfpl/COPYING for more details.
11 Package [
12   FFI
16 class: FfiLibrary [
17   | h name |
19   ^loadLib: aName [
20     aName := aName + System suffixForDLL.
21     <#FFILoadLib aName>.
22     self error: 'can''t load library '+aName
23   ]
25   ^closeLib: h [
26     <#FFICloseLib h>.
27   ]
29   ^new [
30     self error: 'FfiLibrary should be created with #new:'
31   ]
33   ^new: aName [
34     | obj h |
35     h := self loadLib: aName.
36     obj := self basicNew.
37     self;
38       in: obj at: 1 put: h;
39       in: obj at: 2 put: aName.
40     obj addToBeFinalized.
41     ^obj
42   ]
44   handle [
45     ^h
46   ]
48   name [
49     ^name
50   ]
52   finalize [
53     h ifNotNil: [ self closeLib: h ].
54     h := name := nil.
55   ]
59 class: FfiValue [
60   | func lib name |
62   ^loadSym: aLib name: aName [
63     <#FFIResolveName aLib aName>.
64     self error: 'can''t load value '+aName
65   ]
67   ^new [
68     self error: 'FfiValue should be created with #new:name:'
69   ]
71   ^new: aLib name: aName [
72     | obj h |
73     h := self loadSym: aLib handle name: aName.
74     obj := self basicNew.
75     self;
76       in: obj at: 1 put: h;
77       in: obj at: 2 put: aLib;
78       in: obj at: 3 put: aName.
79     obj addToBeFinalized.
80     ^obj
81   ]
83   finalize [
84     func := lib := name := nil.
85   ]
87   as: aType [
88     <#FFIGetVal func aType>.
89     self primitiveFailed.
90   ]
94 class: FfiFunction [
95   | func lib name |
97   ^loadSym: aLib name: aName [
98     <#FFIResolveName aLib aName>.
99     self error: 'can''t load function '+aName
100   ]
102   ^new [
103     self error: 'FfiFunction should be created with #new:name:'
104   ]
106   ^new: aLib name: aName [
107     | obj h |
108     h := self loadSym: aLib handle name: aName.
109     obj := self basicNew.
110     self;
111       in: obj at: 1 put: h;
112       in: obj at: 2 put: aLib;
113       in: obj at: 3 put: aName.
114     obj addToBeFinalized.
115     ^obj
116   ]
118   finalize [
119     func := lib := name := nil.
120   ]
122   call [
123     <#FFICall func nil>.
124     self primitiveFailed.
125   ]
127   retType: aType [
128     <#FFICall func aType>.
129     self primitiveFailed.
130   ]
133 Requires [ ffi/fficall ]