-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathffi.4th
200 lines (170 loc) · 4.66 KB
/
ffi.4th
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
\ @+leo-ver=4-thin
\ @+node:leonardoce.20090701090549.4:@thin ffi.4th
\ @@language forth
\ @+others
\ @+node:leonardoce.20090701090549.5:Buffer di uso generico C
variable c-buffer-orig 1 cells allot
variable c-buffer 1 cells allot
: init-buffer
( caddr -- )
dup c-buffer-orig !
c-buffer ! ;
: get-buffer
( -- caddr )
c-buffer-orig @ ;
: c-in-buffer
( c -- )
c-buffer @ cmem!
c-buffer @ 1+ c-buffer ! ;
: c-from-buffer
( -- c )
c-buffer @ cmem@
c-buffer @ 1+ c-buffer ! ;
\ @-node:leonardoce.20090701090549.5:Buffer di uso generico C
\ @+node:leonardoce.20090701090549.6:Utili
: emit-if-not-zero
( c -- )
dup 0<> if emit else drop then ;
: 0! ( addr -- )
0 swap ! ;
: 1+! ( addr -- )
1 swap +! ;
\ @nonl
\ @-node:leonardoce.20090701090549.6:Utili
\ @+node:leonardoce.20090701090549.7:Gestione stringhe C
: cstring-new
( c-addr u -- newaddr )
dup 1+ cmalloc \ ( s: c-addr u nuova-memoria )
init-buffer \ ( s: c-addr u )
over + swap do
i c@ c-in-buffer
loop
0 c-in-buffer
get-buffer ;
variable temp-len 1 cells allot
: cstring-len
( c-addr -- len )
temp-len 0!
begin
temp-len 1+!
dup cmem@ 0=
swap 1+ swap
until
drop
temp-len @ 1- ;
: cstring-type
( c-addr -- )
begin
dup cmem@ \ ( s: caddr c )
swap 1+ swap \ ( s: caddr+1 c )
dup emit-if-not-zero \ ( s: caddr+1 c )
0=
until
drop ;
: cstring-delete
( c-addr -- )
cfree ;
\ @nonl
\ @-node:leonardoce.20090701090549.7:Gestione stringhe C
\ @+node:leonardoce.20090701090549.8:Utili FFI
\ Ci sono impazzito ma funziona alla
\ grande.
: ext"
( compile: "cccc<quote>" -- ) ( runtime: -- caddr )
state@ if
[char] " parse allot \ ( addr )
0 c, \ zero terminator
postpone literal \ compile address into defining function
['] addr>c \ compile address translation
postpone literal
['] execute \ compile exec
compile,
\ postpone execute
then ; immediate
: cloadlibrary-check ( saddr -- handle )
cloadlibrary dup 0= if abort" Errore in caricamento libreria native" then ;
: cprocaddress-check ( handle procname -- handle )
dup >r
cprocaddress dup 0= if ." Errore in caricamento entrypoint: '" r@ cstring-type ." '" abort then
r> drop ;
: 0ccall ( procaddr -- result ) 0 swap ccall ;
: 1ccall ( procaddr -- result ) 1 swap ccall ;
: 2ccall ( procaddr -- result ) 2 swap ccall ;
: 3ccall ( procaddr -- result ) 3 swap ccall ;
: 4ccall ( procaddr -- result ) 4 swap ccall ;
\ @-node:leonardoce.20090701090549.8:Utili FFI
\ @+node:leonardoce.20090706154105.2:Database FFI
\ @+others
\ @+node:leonardoce.20090706154105.3:Stato
0 value current-lib
0 value current-entrypoint
: current-lib: ' to current-lib ;
: entrypoint" [char] " parse dup allot drop to current-entrypoint
0 here 1 allot c! ;
\ @nonl
\ @-node:leonardoce.20090706154105.3:Stato
\ @+node:leonardoce.20090701090549.9:Definizioni DLL
begin-structure dllstructure
1 cells +field dll-handle
1 cells +field dll-name
end-structure
: runtime-defdll dup @ 0=
if dup 1 cells + @ addr>c cloadlibrary-check over ! then @ ;
: defdll: drop create
here dllstructure allot
0 over dll-handle !
dll-name !
does> runtime-defdll ;
\ @-node:leonardoce.20090701090549.9:Definizioni DLL
\ @+node:leonardoce.20090701090549.11:Definizioni Proc
begin-structure procstructure
1 cells +field ps-dll-xt
1 cells +field ps-handle
1 cells +field ps-name
1 cells +field ps-nparams
end-structure
: func-execute ( args addr ) dup ps-nparams @ swap ps-handle @ ccall ;
: func-create ( addr )
dup ps-dll-xt @ execute
over ps-name @ addr>c
cprocaddress-check
swap ps-handle ! ;
: func-runtime ( args -- ret ) dup ps-handle @ 0= if dup func-create then func-execute ;
0 value func-temp
: func-compile
here procstructure allot to func-temp
current-lib func-temp ps-dll-xt !
0 func-temp ps-handle !
current-entrypoint func-temp ps-name !
func-temp ps-nparams ! ;
: vfunc: create func-compile does> func-runtime drop ;
: func: create func-compile does> func-runtime ;
\ @-node:leonardoce.20090701090549.11:Definizioni Proc
\ @+node:leonardoce.20090706154105.4:Test
\ @+at
\
\ : sdldll s" sdl.dll" ;
\ sdldll defdll: sdl
\ current-lib: sdl
\
\ entrypoint" SDL_Init" 1 vfunc: sdl_init
\ @-at
\ @nonl
\ @-node:leonardoce.20090706154105.4:Test
\ @-others
\ @+doc
\
\ Uso tipico:
\
\ : sdl.dll s" sdl.dll" ;
\ sdl.dll defdll: sdl
\
\ @-doc
\ @+at
\ @-at
\ @nonl
\ @-node:leonardoce.20090706154105.2:Database FFI
\ @-others
\ @nonl
\ @-node:leonardoce.20090701090549.4:@thin ffi.4th
\ @-leo