forked from vygr/OSX-Forth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.f
129 lines (110 loc) · 1.66 KB
/
class.f
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
\ mini oof words
\ root object class
create object-class 1 cells , 2 cells ,
\ ( class -- class m-offset s-offset)
: begin-class
dup 2@
;
\ ( m-offset s-offset "name" -- m-offset' s-offset ) ; ( -- m-offset )
: method
create
over , swap cell+ swap
does>
@
;
\ ( ... o "name" -- ... )
: ->
' execute
state @
if
->,
else
over @ + @ execute
then
; immediate
\ ( class m-offset s-offset "name" -- )
: end-class
create
here >r , dup , 2 cells
?do
['] noop ,
1 cells
+loop
cell+ dup cell+ r> rot @ 2 cells /string move
;
\ ( xt class "name" -- )
: defines
' >body @ + !
;
\ ( class "name" -- )
: ::
' >body @ + @ compile,
;
\ ( class -- o | 0 )
: new
dup @ calloc
dup
if
\ vtable
swap over !
else
nip
then
;
\ ( o | 0 -- )
: delete
free
;
\ base class for reference counting objects
object-class begin-class
field _cnt
method base-ref
method base-deref
method base-init
method base-deinit
method base-construct
method base-destroy
end-class base-class
\ ( o -- )
:noname
1 swap >field _cnt +!
; base-class defines base-ref
\ ( o -- )
:noname
>r
1 r@ >field _cnt -!
r@ @field _cnt 0=
if
r@ -> base-destroy
then
rdrop
; base-class defines base-deref
\ ( ... o -- 0 | -err_code )
:noname
-> base-ref 0
; base-class defines base-init
\ ( o -- )
:noname
drop
; base-class defines base-deinit
\ ( ... class -- ... 0 | -err_code | o )
: construct
new dup
if
>r r@ -> base-init ?dup
if
r> delete
else
r>
then
then
;
\ ( ... o -- ... 0 | -err_code | o' )
:noname
@ construct
; base-class defines base-construct
\ ( o -- )
: destroy
dup -> base-deinit delete
; ' destroy base-class defines base-destroy
hide _cnt