forked from newspeaklanguage/newspeak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CAPI.ns
384 lines (382 loc) · 26.1 KB
/
CAPI.ns
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
Newspeak3
'CAPI'
class CAPI platform: platform = (
(* The generic C language API module. Defines classes for modeling C language artifacts and the facilities for accessing the artifacts.
Copyright 2008 Cadence Design Systems, Inc.
Licensed under the Apache License, Version 2.0 (the ''License''); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*)
|
public (* bogus *) Alien = platform squeak Alien.
public (* bogus *) Callback = platform squeak Callback.
protected Map = platform squeak Dictionary.
protected List = platform squeak OrderedCollection.
protected libraries = List new.
protected exportedFunctions = Map new: 100.
|#ACCESSBOGUS) (
class CFunction library: theLibrary <CLibrary> name: theName <Symbol> argc: numArgs <Integer> = (
(* Represents a C function. *)
|
library = theLibrary.
public name = theName.
argc = numArgs.
protected alien
|) (
api = (
^outer CAPI
)
ensureLinkage = (
self isLinked ifFalse: [alien:: library linkFunction: name]
)
isLinked ^<Boolean> = (
^alien notNil and: [alien isPointer (* When this becomes an IllegalAlien, it will be false *)]
)
reportIncorrectNumberOfArguments: argumentsGiven <Integer> = (
error:
name,
': incorrect number of arguments (',
argumentsGiven printString,
' instead of ',
argc printString,
')'
)
public resetForNewImageSession = (
alien:: nil
)
public signedValue ^<Integer> = (
(* Invoke the function with zero arguments and return an Integer produced by interpreting the first four bytes of the result as a signed integer. Fail if the receiver expects a different number of arguments. *)
^value asSignedLong
)
public signedValue: arg <Alien | Integer> ^<Integer> = (
(* Invoke the function with 1 argument and return an Integer produced by interpreting the first four bytes of the result as a signed integer. Fail if the receiver expects a different number of arguments. *)
^(value: arg) asSignedLong
)
public signedValue: arg1 <Alien | Integer> value: arg2 <Alien | Integer> ^<Integer> = (
(* Invoke the function with 2 arguments and return an Integer produced by interpreting the first four bytes of the result as a signed integer. Fail if the receiver expects a different number of arguments. *)
^(value: arg1 value: arg2) asSignedLong
)
public signedValue: arg1 <Alien | Integer> value: arg2 <Alien | Integer> value: arg3 <Alien | Integer> ^<Integer> = (
(* Invoke the function with 3 arguments and return an Integer produced by interpreting the first four bytes of the result as a signed integer. Fail if the receiver expects a different number of arguments. *)
^(value: arg1 value: arg2 value: arg3) asSignedLong
)
public signedValue: arg1 <Alien | Integer> value: arg2 <Alien | Integer> value: arg3 <Alien | Integer> value: arg4 <Alien | Integer> ^<Integer> = (
(* Invoke the function with 4 arguments and return an Integer produced by interpreting the first four bytes of the result as a signed integer. Fail if the receiver expects a different number of arguments. *)
^(value: arg1 value: arg2 value: arg3 value: arg4) asSignedLong
)
public signedValue: arg1 <Alien | Integer> value: arg2 <Alien | Integer> value: arg3 <Alien | Integer> value: arg4 <Alien | Integer> value: arg5 <Alien | Integer> ^<Integer> = (
(* Invoke the function with 5 arguments and return an Integer produced by interpreting the first four bytes of the result as a signed integer. Fail if the receiver expects a different number of arguments. *)
^(value: arg1 value: arg2 value: arg3 value: arg4 value: arg5) asSignedLong
)
public signedValue: arg1 <Alien | Integer> value: arg2 <Alien | Integer> value: arg3 <Alien | Integer> value: arg4 <Alien | Integer> value: arg5 <Alien | Integer> value: arg6 <Alien | Integer> ^<Integer> = (
(* Invoke the function with 6 arguments and return an Integer produced by interpreting the first four bytes of the result as a signed integer. Fail if the receiver expects a different number of arguments. *)
^(value: arg1 value: arg2 value: arg3 value: arg4 value: arg5 value: arg6) asSignedLong
)
public signedValue: arg1 <Alien | Integer> value: arg2 <Alien | Integer> value: arg3 <Alien | Integer> value: arg4 <Alien | Integer> value: arg5 <Alien | Integer> value: arg6 <Alien | Integer> value: arg7 <Alien | Integer> ^<Integer> = (
(* Invoke the function with 7 arguments and return an Integer produced by interpreting the first four bytes of the result as a signed integer. Fail if the receiver expects a different number of arguments. *)
^(value: arg1 value: arg2 value: arg3 value: arg4 value: arg5 value: arg6 value: arg7) asSignedLong
)
public signedValue: arg1 <Alien | Integer> value: arg2 <Alien | Integer> value: arg3 <Alien | Integer> value: arg4 <Alien | Integer> value: arg5 <Alien | Integer> value: arg6 <Alien | Integer> value: arg7 <Alien | Integer> value: arg8 <Alien | Integer> ^<Integer> = (
(* Invoke the function with 8 arguments and return an Integer produced by interpreting the first four bytes of the result as a signed integer. Fail if the receiver expects a different number of arguments. *)
^(value: arg1 value: arg2 value: arg3 value: arg4 value: arg5 value: arg6 value: arg7 value: arg8) asSignedLong
)
public signedValue: arg1 <Alien | Integer> value: arg2 <Alien | Integer> value: arg3 <Alien | Integer> value: arg4 <Alien | Integer> value: arg5 <Alien | Integer> value: arg6 <Alien | Integer> value: arg7 <Alien | Integer> value: arg8 <Alien | Integer> value: arg9 <Alien | Integer> ^<Integer> = (
(* Invoke the function with 9 arguments and return an Integer produced by interpreting the first four bytes of the result as a signed integer. Fail if the receiver expects a different number of arguments. *)
^(value: arg1 value: arg2 value: arg3 value: arg4 value: arg5 value: arg6 value: arg7 value: arg8 value: arg9) asSignedLong
)
public signedValue: arg1 <Alien | Integer> value: arg2 <Alien | Integer> value: arg3 <Alien | Integer> value: arg4 <Alien | Integer> value: arg5 <Alien | Integer> value: arg6 <Alien | Integer> value: arg7 <Alien | Integer> value: arg8 <Alien | Integer> value: arg9 <Alien | Integer> value: arg10 <Alien | Integer> ^<Integer> = (
(* Invoke the function with 10 arguments and return an Integer produced by interpreting the first four bytes of the result as a signed integer. Fail if the receiver expects a different number of arguments. *)
^(value: arg1 value: arg2 value: arg3 value: arg4 value: arg5 value: arg6 value: arg7 value: arg8 value: arg9 value: arg10) asSignedLong
)
public signedValue: arg1 <Alien | Integer> value: arg2 <Alien | Integer> value: arg3 <Alien | Integer> value: arg4 <Alien | Integer> value: arg5 <Alien | Integer> value: arg6 <Alien | Integer> value: arg7 <Alien | Integer> value: arg8 <Alien | Integer> value: arg9 <Alien | Integer> value: arg10 <Alien | Integer> value: arg11 <Alien | Integer> ^<Integer> = (
(* Invoke the function with 11 arguments and return an Integer produced by interpreting the first four bytes of the result as a signed integer. Fail if the receiver expects a different number of arguments. *)
^(value: arg1 value: arg2 value: arg3 value: arg4 value: arg5 value: arg6 value: arg7 value: arg8 value: arg9 value: arg10 value: arg11) asSignedLong
)
public signedValue: arg1 <Alien | Integer> value: arg2 <Alien | Integer> value: arg3 <Alien | Integer> value: arg4 <Alien | Integer> value: arg5 <Alien | Integer> value: arg6 <Alien | Integer> value: arg7 <Alien | Integer> value: arg8 <Alien | Integer> value: arg9 <Alien | Integer> value: arg10 <Alien | Integer> value: arg11 <Alien | Integer> value: arg12 <Alien | Integer> ^<Integer> = (
(* Invoke the function with 12 arguments and return an Integer produced by interpreting the first four bytes of the result as a signed integer. Fail if the receiver expects a different number of arguments. *)
^(value: arg1 value: arg2 value: arg3 value: arg4 value: arg5 value: arg6 value: arg7 value: arg8 value: arg9 value: arg10 value: arg11 value: arg12) asSignedLong
)
public signedValueWithArguments: args <Array[Alien | Integer]> ^<Alien> = (
(* Invoke the function with the elements of the args array as the arguments and return an Integer equal to the first four bytes of the result interpreted as a signed integer. Fail if the receiver expects a different number of arguments. *)
^(valueWithArguments: args) asSignedLong
)
public unsignedValue ^<Integer> = (
(* Invoke the function with zero arguments and return an Integer produced by interpreting the first four bytes of the result as an unsigned integer. Fail if the receiver expects a different number of arguments. *)
^value asUnsignedLong
)
public unsignedValue: arg <Alien | Integer> ^<Integer> = (
(* Invoke the function with 1 argument and return an Integer produced by interpreting the first four bytes of the result as an unsigned integer. Fail if the receiver expects a different number of arguments. *)
^(value: arg) asUnsignedLong
)
public unsignedValue: arg1 <Alien | Integer> value: arg2 <Alien | Integer> ^<Integer> = (
(* Invoke the function with 2 arguments and return an Integer produced by interpreting the first four bytes of the result as an unsigned integer. Fail if the receiver expects a different number of arguments. *)
^(value: arg1 value: arg2) asUnsignedLong
)
public unsignedValue: arg1 <Alien | Integer> value: arg2 <Alien | Integer> value: arg3 <Alien | Integer> ^<Integer> = (
(* Invoke the function with 3 arguments and return an Integer produced by interpreting the first four bytes of the result as an unsigned integer. Fail if the receiver expects a different number of arguments. *)
^(value: arg1 value: arg2 value: arg3) asUnsignedLong
)
public unsignedValue: arg1 <Alien | Integer> value: arg2 <Alien | Integer> value: arg3 <Alien | Integer> value: arg4 <Alien | Integer> ^<Integer> = (
(* Invoke the function with 4 arguments and return an Integer produced by interpreting the first four bytes of the result as an unsigned integer. Fail if the receiver expects a different number of arguments. *)
^(value: arg1 value: arg2 value: arg3 value: arg4) asUnsignedLong
)
public unsignedValue: arg1 <Alien | Integer> value: arg2 <Alien | Integer> value: arg3 <Alien | Integer> value: arg4 <Alien | Integer> value: arg5 <Alien | Integer> ^<Integer> = (
(* Invoke the function with 5 arguments and return an Integer produced by interpreting the first four bytes of the result as an unsigned integer. Fail if the receiver expects a different number of arguments. *)
^(value: arg1 value: arg2 value: arg3 value: arg4 value: arg5) asUnsignedLong
)
public unsignedValue: arg1 <Alien | Integer> value: arg2 <Alien | Integer> value: arg3 <Alien | Integer> value: arg4 <Alien | Integer> value: arg5 <Alien | Integer> value: arg6 <Alien | Integer> ^<Integer> = (
(* Invoke the function with 6 arguments and return an Integer produced by interpreting the first four bytes of the result as an unsigned integer. Fail if the receiver expects a different number of arguments. *)
^(value: arg1 value: arg2 value: arg3 value: arg4 value: arg5 value: arg6) asUnsignedLong
)
public unsignedValue: arg1 <Alien | Integer> value: arg2 <Alien | Integer> value: arg3 <Alien | Integer> value: arg4 <Alien | Integer> value: arg5 <Alien | Integer> value: arg6 <Alien | Integer> value: arg7 <Alien | Integer> ^<Integer> = (
(* Invoke the function with 7 arguments and return an Integer produced by interpreting the first four bytes of the result as an unsigned integer. Fail if the receiver expects a different number of arguments. *)
^(value: arg1 value: arg2 value: arg3 value: arg4 value: arg5 value: arg6 value: arg7) asUnsignedLong
)
public unsignedValue: arg1 <Alien | Integer> value: arg2 <Alien | Integer> value: arg3 <Alien | Integer> value: arg4 <Alien | Integer> value: arg5 <Alien | Integer> value: arg6 <Alien | Integer> value: arg7 <Alien | Integer> value: arg8 <Alien | Integer> ^<Integer> = (
(* Invoke the function with 8 arguments and return an Integer produced by interpreting the first four bytes of the result as an unsigned integer. Fail if the receiver expects a different number of arguments. *)
^(value: arg1 value: arg2 value: arg3 value: arg4 value: arg5 value: arg6 value: arg7 value: arg8) asUnsignedLong
)
public unsignedValue: arg1 <Alien | Integer> value: arg2 <Alien | Integer> value: arg3 <Alien | Integer> value: arg4 <Alien | Integer> value: arg5 <Alien | Integer> value: arg6 <Alien | Integer> value: arg7 <Alien | Integer> value: arg8 <Alien | Integer> value: arg9 <Alien | Integer> ^<Integer> = (
(* Invoke the function with 9 arguments and return an Integer produced by interpreting the first four bytes of the result as an unsigned integer. Fail if the receiver expects a different number of arguments. *)
^(value: arg1 value: arg2 value: arg3 value: arg4 value: arg5 value: arg6 value: arg7 value: arg8 value: arg9) asUnsignedLong
)
public unsignedValue: arg1 <Alien | Integer> value: arg2 <Alien | Integer> value: arg3 <Alien | Integer> value: arg4 <Alien | Integer> value: arg5 <Alien | Integer> value: arg6 <Alien | Integer> value: arg7 <Alien | Integer> value: arg8 <Alien | Integer> value: arg9 <Alien | Integer> value: arg10 <Alien | Integer> ^<Integer> = (
(* Invoke the function with 10 arguments and return an Integer produced by interpreting the first four bytes of the result as an unsigned integer. Fail if the receiver expects a different number of arguments. *)
^(value: arg1 value: arg2 value: arg3 value: arg4 value: arg5 value: arg6 value: arg7 value: arg8 value: arg9 value: arg10) asUnsignedLong
)
public unsignedValue: arg1 <Alien | Integer> value: arg2 <Alien | Integer> value: arg3 <Alien | Integer> value: arg4 <Alien | Integer> value: arg5 <Alien | Integer> value: arg6 <Alien | Integer> value: arg7 <Alien | Integer> value: arg8 <Alien | Integer> value: arg9 <Alien | Integer> value: arg10 <Alien | Integer> value: arg11 <Alien | Integer> ^<Integer> = (
(* Invoke the function with 11 arguments and return an Integer produced by interpreting the first four bytes of the result as an unsigned integer. Fail if the receiver expects a different number of arguments. *)
^(value: arg1 value: arg2 value: arg3 value: arg4 value: arg5 value: arg6 value: arg7 value: arg8 value: arg9 value: arg10 value: arg11) asUnsignedLong
)
public unsignedValue: arg1 <Alien | Integer> value: arg2 <Alien | Integer> value: arg3 <Alien | Integer> value: arg4 <Alien | Integer> value: arg5 <Alien | Integer> value: arg6 <Alien | Integer> value: arg7 <Alien | Integer> value: arg8 <Alien | Integer> value: arg9 <Alien | Integer> value: arg10 <Alien | Integer> value: arg11 <Alien | Integer> value: arg12 <Alien | Integer> ^<Integer> = (
(* Invoke the function with 12 arguments and return an Integer produced by interpreting the first four bytes of the result as an unsigned integer. Fail if the receiver expects a different number of arguments. *)
^(value: arg1 value: arg2 value: arg3 value: arg4 value: arg5 value: arg6 value: arg7 value: arg8 value: arg9 value: arg10 value: arg11 value: arg12) asUnsignedLong
)
public unsignedValueWithArguments: args <Array[Alien | Integer]> ^<Alien> = (
(* Invoke the function with the elements of the args array as the arguments and return an Integer equal to the first four bytes of the result interpreted as an unsigned integer. Fail if the receiver expects a different number of arguments. *)
^(valueWithArguments: args) asUnsignedLong
)
public value ^<Alien> = (
(* Invoke the function with zero arguments and return an Alien with the result. Fail if the receiver expects a different number of arguments. *)
| result |
argc = 0 ifFalse: [^reportIncorrectNumberOfArguments: 0].
ensureLinkage.
result:: Alien new: 4.
alien primFFICallResult: result.
^result
)
public value: arg <Alien | Integer> ^<Alien> = (
(* Invoke the function with 1 argument and return an Alien with the result. Fail if the receiver expects a different number of arguments. *)
| result |
argc = 1 ifFalse: [^reportIncorrectNumberOfArguments: 1].
ensureLinkage.
result:: Alien new: 4.
alien primFFICallResult: result with: arg.
^result
)
public value: arg1 <Alien | Integer> value: arg2 <Alien | Integer> ^<Alien> = (
(* Invoke the function with 2 arguments and return an Alien with the result. Fail if the receiver expects a different number of arguments. *)
| result |
argc = 2 ifFalse: [^reportIncorrectNumberOfArguments: 2].
ensureLinkage.
result:: Alien new: 4.
alien primFFICallResult: result with: arg1 with: arg2.
^result
)
public value: arg1 <Alien | Integer> value: arg2 <Alien | Integer> value: arg3 <Alien | Integer> ^<Alien> = (
(* Invoke the function with 3 arguments and return an Alien with the result. Fail if the receiver expects a different number of arguments. *)
| result |
argc = 3 ifFalse: [^reportIncorrectNumberOfArguments: 3].
ensureLinkage.
result:: Alien new: 4.
alien primFFICallResult: result with: arg1 with: arg2 with: arg3.
^result
)
public value: arg1 <Alien | Integer> value: arg2 <Alien | Integer> value: arg3 <Alien | Integer> value: arg4 <Alien | Integer> ^<Alien> = (
(* Invoke the function with 4 arguments and return an Alien with the result. Fail if the receiver expects a different number of arguments. *)
| result |
argc = 4 ifFalse: [^reportIncorrectNumberOfArguments: 4].
ensureLinkage.
result:: Alien new: 4.
alien primFFICallResult: result with: arg1 with: arg2 with: arg3 with: arg4.
^result
)
public value: arg1 <Alien | Integer> value: arg2 <Alien | Integer> value: arg3 <Alien | Integer> value: arg4 <Alien | Integer> value: arg5 <Alien | Integer> ^<Alien> = (
(* Invoke the function with 5 arguments and return an Alien with the result. Fail if the receiver expects a different number of arguments. *)
| result |
argc = 5 ifFalse: [^reportIncorrectNumberOfArguments: 5].
ensureLinkage.
result:: Alien new: 4.
alien primFFICallResult: result with: arg1 with: arg2 with: arg3 with: arg4 with: arg5.
^result
)
public value: arg1 <Alien | Integer> value: arg2 <Alien | Integer> value: arg3 <Alien | Integer> value: arg4 <Alien | Integer> value: arg5 <Alien | Integer> value: arg6 <Alien | Integer> ^<Alien> = (
(* Invoke the function with 6 arguments and return an Alien with the result. Fail if the receiver expects a different number of arguments. *)
| result |
argc = 6 ifFalse: [^reportIncorrectNumberOfArguments: 6].
ensureLinkage.
result:: Alien new: 4.
alien primFFICallResult: result with: arg1 with: arg2 with: arg3 with: arg4 with: arg5 with: arg6.
^result
)
public value: arg1 <Alien | Integer> value: arg2 <Alien | Integer> value: arg3 <Alien | Integer> value: arg4 <Alien | Integer> value: arg5 <Alien | Integer> value: arg6 <Alien | Integer> value: arg7 <Alien | Integer> ^<Alien> = (
(* Invoke the function with 7 arguments and return an Alien with the result. Fail if the receiver expects a different number of arguments. *)
| result |
argc = 7 ifFalse: [^reportIncorrectNumberOfArguments: 7].
ensureLinkage.
result:: Alien new: 4.
alien primFFICallResult: result with: arg1 with: arg2 with: arg3 with: arg4 with: arg5 with: arg6 with: arg7.
^result
)
public value: arg1 <Alien | Integer> value: arg2 <Alien | Integer> value: arg3 <Alien | Integer> value: arg4 <Alien | Integer> value: arg5 <Alien | Integer> value: arg6 <Alien | Integer> value: arg7 <Alien | Integer> value: arg8 <Alien | Integer> ^<Alien> = (
(* Invoke the function with 8 arguments and return an Alien with the result. Fail if the receiver expects a different number of arguments. *)
| result |
argc = 8 ifFalse: [^reportIncorrectNumberOfArguments: 8].
ensureLinkage.
result:: Alien new: 4.
alien primFFICallResult: result with: arg1 with: arg2 with: arg3 with: arg4 with: arg5 with: arg6 with: arg7 with: arg8.
^result
)
public value: arg1 <Alien | Integer> value: arg2 <Alien | Integer> value: arg3 <Alien | Integer> value: arg4 <Alien | Integer> value: arg5 <Alien | Integer> value: arg6 <Alien | Integer> value: arg7 <Alien | Integer> value: arg8 <Alien | Integer> value: arg9 <Alien | Integer> ^<Alien> = (
(* Invoke the function with 9 arguments and return an Alien with the result. Fail if the receiver expects a different number of arguments. *)
| result |
argc = 9 ifFalse: [^reportIncorrectNumberOfArguments: 9].
ensureLinkage.
result:: Alien new: 4.
alien primFFICallResult: result with: arg1 with: arg2 with: arg3 with: arg4 with: arg5 with: arg6 with: arg7 with: arg8 with: arg9.
^result
)
public value: arg1 <Alien | Integer> value: arg2 <Alien | Integer> value: arg3 <Alien | Integer> value: arg4 <Alien | Integer> value: arg5 <Alien | Integer> value: arg6 <Alien | Integer> value: arg7 <Alien | Integer> value: arg8 <Alien | Integer> value: arg9 <Alien | Integer> value: arg10 <Alien | Integer> ^<Alien> = (
(* Invoke the function with 10 arguments and return an Alien with the result. Fail if the receiver expects a different number of arguments. *)
| result |
argc = 10 ifFalse: [^reportIncorrectNumberOfArguments: 10].
ensureLinkage.
result:: Alien new: 4.
alien primFFICallResult: result with: arg1 with: arg2 with: arg3 with: arg4 with: arg5 with: arg6 with: arg7 with: arg8 with: arg9 with: arg10.
^result
)
public value: arg1 <Alien | Integer> value: arg2 <Alien | Integer> value: arg3 <Alien | Integer> value: arg4 <Alien | Integer> value: arg5 <Alien | Integer> value: arg6 <Alien | Integer> value: arg7 <Alien | Integer> value: arg8 <Alien | Integer> value: arg9 <Alien | Integer> value: arg10 <Alien | Integer> value: arg11 <Alien | Integer> ^<Alien> = (
(* Invoke the function with 11 arguments and return an Alien with the result. Fail if the receiver expects a different number of arguments. *)
| result |
argc = 11 ifFalse: [^reportIncorrectNumberOfArguments: 11].
ensureLinkage.
result:: Alien new: 4.
alien primFFICallResult: result with: arg1 with: arg2 with: arg3 with: arg4 with: arg5 with: arg6 with: arg7 with: arg8 with: arg9 with: arg10 with: arg11.
^result
)
public value: arg1 <Alien | Integer> value: arg2 <Alien | Integer> value: arg3 <Alien | Integer> value: arg4 <Alien | Integer> value: arg5 <Alien | Integer> value: arg6 <Alien | Integer> value: arg7 <Alien | Integer> value: arg8 <Alien | Integer> value: arg9 <Alien | Integer> value: arg10 <Alien | Integer> value: arg11 <Alien | Integer> value: arg12 <Alien | Integer> ^<Alien> = (
(* Invoke the function with 12 arguments and return an Alien with the result. Fail if the receiver expects a different number of arguments. *)
| result |
argc = 12 ifFalse: [^reportIncorrectNumberOfArguments: 12].
ensureLinkage.
result:: Alien new: 4.
alien primFFICallResult: result with: arg1 with: arg2 with: arg3 with: arg4 with: arg5 with: arg6 with: arg7 with: arg8 with: arg9 with: arg10 with: arg11 with: arg12.
^result
)
public valueWithArguments: args <Array[Alien | Integer]> ^<Alien> = (
(* Invoke the function with the elements of the args array as the arguments and return an Alien with the result. Fail if the receiver expects a different number of arguments. *)
| result |
argc = args size ifFalse: [^reportIncorrectNumberOfArguments: args size].
ensureLinkage.
result:: Alien new: 4.
alien primFFICallResult: result withArguments: args.
^result
)
) : (
)
class CLibrary name: theName <String> = (
(* Represents an external dynamically linked library. *)
|
dllName = theName.
protected functions = Map new.
protected alien
|) (
api = (
^outer CAPI
)
createAlien = (
^Alien forPointer: (Alien primLoadLibrary: dllName)
)
ensureLinkage = (
self isLinked ifFalse: [alien:: createAlien]
)
public exportFunctionsTo: capi <CAPI> = (
(* Subclasses can redefine this to export some of their functions so that they are available by sending the corresponding messages directly to the api instance. *)
)
functionClass ^<Class> = (
(* Class used for instances of functions managed by this library. *)
^CFunction
)
functionNamed: functionName <Symbol> argc: argc <Integer> ^ <Self functionClass> = (
^functions
at: functionName
(* ifPresent: [:e |] *) (* uncomment when CAPI uses goodthink libs *)
ifAbsentPut: [functionClass library: self name: functionName argc: argc]
)
isLinked ^<Boolean> = (
^alien notNil and: [alien isPointer (* When this becomes an IllegalAlien, it will be false *)]
)
public linkFunction: symbol <String> ^<Alien> = (
(* Look up the function named by the argument in the underlying DLL and answer an Alien pointing to the function. This message is intended for the use by CFunctions and not by application code. *)
ensureLinkage.
^Alien forPointer: (alien primFindSymbol: symbol)
)
public resetForNewImageSession = (
alien:: nil.
functions do: [:each | each resetForNewImageSession]
)
unlink = (
alien notNil ifTrue:
[alien free.
alien:: nil]
)
) : (
)
class CMacro name: theName <Symbol> expansion: tokens <Array> = (
(* Represents a C macro. For now, we expect that expansion is a single Integer value. *)
|
name = theName.
expansion = tokens.
|) (
api = (
^outer CAPI
)
value = (
| head |
head:: expansion first.
head isKindOfNumber ifFalse: [error: 'expansion is not a number'].
^head
)
) : (
name: name <Symbol> value: value <Number> = (
^self name: name expansion: {value}
)
)
addLibrary: library <CLibrary> = (
(* Add the library to the list of libraries this API manages. Make library functions available at the API level. *)
libraries add: library.
library exportFunctionsTo: self
)
addLibraryNoImport: library <CLibrary> = (
(* Add the library to the list of libraries this API manages. Do not make library functions available at the API level. *)
libraries add: library.
)
bits: selectors <Array[Selector]> ^<Integer> = (
(* Send all selectors from the argument array to the receiver as messages, expecting each to answer an integer value. Combine the answers using bitOr and return the result. This is a convenience for writing the equivalent of C-style bit combinations such as (MASK1 | MASK2 | MASK3) as (api bits: {#MASK1. #MASK2. #MASK3}). *)
^selectors inject: 0 into: [:mask :each | mask bitOr: (self perform: each)]
)
doesNotUnderstand: message = (
((exportedFunctions includesKey: message selector)
and: [message arguments isEmpty])
ifTrue: [^exportedFunctions at: message selector].
^super doesNotUnderstand: message
)
public exportFunction: function <CFunction> = (
exportFunction: function as: function name
)
public exportFunction: function <CFunction> as: name <Symbol> = (
exportedFunctions at: name put: function
)
public resetForNewImageSession = (
libraries do: [:each | each resetForNewImageSession]
)
) : (
)