2 \ ** ficl/softwords/oo.fr
3 \ ** F I C L O - O E X T E N S I O N S
4 \ ** john sadler aug 1998
12 \ 0. Traditional OOP: late binding by default for safety.
13 \ Early binding if you ask for it.
14 \ 1. Single inheritance
15 \ 2. Object aggregation (has-a relationship)
16 \ 3. Support objects in the dictionary and as proxies for
17 \ existing structures (by reference):
18 \ *** A ficl object can wrap a C struct ***
19 \ 4. Separate name-spaces for methods - methods are
20 \ only visible in the context of a class / object
21 \ 5. Methods can be overridden, and subclasses can add methods.
22 \ No limit on number of methods.
25 \ Classes are objects, too: all classes are instances of METACLASS
26 \ All classes are derived (by convention) from OBJECT. This
27 \ base class provides a default initializer and superclass
30 \ A ficl object binds instance storage (payload) to a class.
31 \ object ( -- instance class )
32 \ All objects push their payload address and class address when
35 \ A ficl class consists of a parent class pointer, a wordlist
36 \ ID for the methods of the class, and a size for the payload
37 \ of objects created by the class. A class is an object.
38 \ The NEW method creates and initializes an instance of a class.
39 \ Classes have this footprint:
40 \ cell 0: parent class address
42 \ cell 2: size of instance's payload
44 \ Methods expect an object couple ( instance class )
45 \ on the stack. This is by convention - ficl has no way to
46 \ police your code to make sure this is always done, but it
47 \ happens naturally if you use the facilities presented here.
49 \ Overridden methods must maintain the same stack signature as
50 \ their predecessors. Ficl has no way of enforcing this, either.
52 \ Revised Apr 2001 - Added Guy Carver's vtable extensions. Class now
53 \ has an extra field for the vtable method count. Hasvtable declares
54 \ refs to vtable classes
56 \ Revised Nov 2001 - metaclass debug method now finds only metaclass methods
58 \ Planned: Ficl vtable support
59 \ Each class has a vtable size parameter
60 \ END-CLASS allocates and clears the vtable - then it walks class's method
61 \ list and inserts all new methods into table. For each method, if the table
62 \ slot is already nonzero, do nothing (overridden method). Otherwise fill
63 \ vtable slot. Now do same check for parent class vtable, filling only
64 \ empty slots in the new vtable.
65 \ Methods are now structured as follows:
69 \ :noname definition for code
71 \ : is redefined to check for override, fill in vtable index, increment method
72 \ count if not an override, create header and fill in index. Allot code pointer
74 \ ; is overridden to fill in xt returned by :noname
75 \ --> compiles code to fetch vtable address, offset by index, and execute
76 \ => looks up xt in the vtable and compiles it directly
83 \ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
84 \ ** L A T E B I N D I N G
85 \ Compile the method name, and code to find and
86 \ execute it at run-time...
89 \ p a r s e - m e t h o d
90 \ compiles a method name so that it pushes
91 \ the string base address and count at run-time.
93 : parse-method \ name run: ( -- c-addr u )
100 : (lookup-method) { class 2:name -- class 0 | class xt 1 | class xt -1 }
101 class name class cell+ @ ( class c-addr u wid )
105 \ l o o k u p - m e t h o d
106 \ takes a counted string method name from the stack (as compiled
107 \ by parse-method) and attempts to look this method up in the method list of
108 \ the class that's on the stack. If successful, it leaves the class on the stack
109 \ and pushes the xt of the method. If not, it aborts with an error message.
111 : lookup-method { class 2:name -- class xt }
112 class name (lookup-method) ( 0 | xt 1 | xt -1 )
114 name type ." not found in "
115 class body> >name type
120 : find-method-xt \ name ( class -- class xt )
121 parse-word lookup-method
124 : catch-method ( instance class c-addr u -- <method-signature> exc-flag )
128 : exec-method ( instance class c-addr u -- <method-signature> )
129 lookup-method execute
132 \ Method lookup operator takes a class-addr and instance-addr
133 \ and executes the method from the class's wordlist if
134 \ interpreting. If compiling, bind late.
136 : --> ( instance class -- ??? )
138 find-method-xt execute
140 parse-method postpone exec-method
144 \ Method lookup with CATCH in case of exceptions
145 : c-> ( instance class -- ?? exc-flag )
149 parse-method postpone catch-method
153 \ METHOD makes global words that do method invocations by late binding
154 \ in case you prefer this style (no --> in your code)
155 \ Example: everything has next and prev for array access, so...
158 \ my-instance next ( does whatever next does to my-instance by late binding )
160 : method create does> body> >name lookup-method execute ;
163 \ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
164 \ ** E A R L Y B I N D I N G
165 \ Early binding operator compiles code to execute a method
166 \ given its class at compile time. Classes are immediate,
167 \ so they leave their cell-pair on the stack when compiling.
169 \ : get-wid metaclass => .wid @ ;
171 \ my-class get-wid ( -- wid-of-my-class )
173 1 ficl-named-wordlist instance-vars
174 instance-vars dup >search ficl-set-current
176 : => \ c:( class meta -- ) run: ( -- ??? ) invokes compiled method
177 drop find-method-xt compile, drop
178 ; immediate compile-only
180 : my=> \ c:( -- ) run: ( -- ??? ) late bind compiled method of current-class
181 current-class @ dup postpone =>
182 ; immediate compile-only
184 \ Problem: my=[ assumes that each method except the last is am obj: member
185 \ which contains its class as the first field of its parameter area. The code
186 \ detects non-obect members and assumes the class does not change in this case.
187 \ This handles methods like index, prev, and next correctly, but does not deal
188 \ correctly with CLASS.
189 : my=[ \ same as my=> , but binds a chain of methods
192 parse-word 2dup ( class c-addr u c-addr u )
193 s" ]" compare while ( class c-addr u )
194 lookup-method ( class xt )
195 dup compile, ( class xt )
196 dup ?object if \ If object member, get new class. Otherwise assume same class
197 nip >body cell+ @ ( new-class )
202 ; immediate compile-only
205 \ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
206 \ ** I N S T A N C E V A R I A B L E S
207 \ Instance variables (IV) are represented by words in the class's
208 \ private wordlist. Each IV word contains the offset
209 \ of the IV it represents, and runs code to add that offset
210 \ to the base address of an instance when executed.
211 \ The metaclass SUB method, defined below, leaves the address
212 \ of the new class's offset field and its initial size on the
213 \ stack for these words to update. When a class definition is
214 \ complete, END-CLASS saves the final size in the class's size
215 \ field, and restores the search order and compile wordlist to
216 \ prior state. Note that these words are hidden in their own
217 \ wordlist to prevent accidental use outside a SUB END-CLASS pair.
220 does> ( instance class addr[offset] -- addr[field] )
224 : addr-units: ( offset size "name" -- offset' )
229 : chars: \ ( offset nCells "name" -- offset' ) Create n char member.
232 : char: \ ( offset nCells "name" -- offset' ) Create 1 char member.
235 : cells: ( offset nCells "name" -- offset' )
236 cells >r aligned r> addr-units:
239 : cell: ( offset nCells "name" -- offset' )
242 \ Aggregate an object into the class...
243 \ Needs the class of the instance to create
244 \ Example: object obj: m_obj
248 does> ( instance class pfa -- a-instance a-class )
249 2@ ( inst class a-class a-offset )
250 2swap drop ( a-class a-offset inst )
251 + swap ( a-inst a-class )
254 : obj: { offset class meta -- offset' } \ "name"
255 create offset , class ,
256 class meta --> get-size offset +
260 \ Aggregate an array of objects into a class
262 \ 3 my-class array: my-array
263 \ Makes an instance variable array of 3 instances of my-class
266 : array: ( offset n class meta "name" -- offset' )
267 locals| meta class nobjs offset |
268 create offset , class ,
269 class meta --> get-size nobjs * offset +
273 \ Aggregate a pointer to an object: REF is a member variable
274 \ whose class is set at compile time. This is useful for wrapping
275 \ data structures in C, where there is only a pointer and the type
276 \ it refers to is known. If you want polymorphism, see c_ref
277 \ in classes.fr. REF is only useful for pre-initialized structures,
278 \ since there's no supported way to set one.
279 : ref: ( offset class meta "name" -- offset' )
280 locals| meta class offset |
281 create offset , class ,
283 does> ( inst class pfa -- ptr-inst ptr-class )
284 2@ ( inst class ptr-class ptr-offset )
288 \ #if FICL_WANT_VCALL
289 \ vcall extensions contributed by Guy Carver
290 : vcall: ( paramcnt "name" -- )
291 current-class @ 8 + dup @ dup 1+ rot ! \ Kludge fix to get to .vtCount before it's defined.
292 create , , \ ( paramcnt index -- )
293 does> \ ( inst class pfa -- ptr-inst ptr-class )
294 nip 2@ vcall \ ( params offset inst class offset -- )
297 : vcallr: 0x80000000 or vcall: ; \ Call with return address desired.
299 \ #if FICL_WANT_FLOAT
300 : vcallf: \ ( paramcnt -<name>- f: r )
302 current-class @ 8 + dup @ dup 1+ rot ! \ Kludge fix to get to .vtCount before it's defined.
303 create , , \ ( paramcnt index -- )
304 does> \ ( inst class pfa -- ptr-inst ptr-class )
305 nip 2@ vcall f> \ ( params offset inst class offset -- f: r )
310 \ END-CLASS terminates construction of a class by storing
311 \ the size of its instance variables in the class's size field
312 \ ( -- old-wid addr[size] 0 )
314 : end-class ( old-wid addr[size] size -- )
316 search> drop \ pop struct builder wordlist
319 \ See resume-class (a metaclass method) below for usage
320 \ This is equivalent to end-class for now, but that will change
321 \ when we support vtable bindings.
322 : suspend-class ( old-wid addr[size] size -- ) end-class ;
325 \ E N D I N S T A N C E V A R I A B L E S
328 \ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
329 \ D O - D O - I N S T A N C E
330 \ Makes a class method that contains the code for an
331 \ instance of the class. This word gets compiled into
332 \ the wordlist of every class by the SUB method.
333 \ PRECONDITION: current-class contains the class address
334 \ why use a state variable instead of the stack?
335 \ >> Stack state is not well-defined during compilation (there are
336 \ >> control structure match codes on the stack, of undefined size
337 \ >> easiest way around this is use of this thread-local variable
339 : do-do-instance ( -- )
340 s" : .do-instance does> [ current-class @ ] literal ;"
344 \ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
345 \ ** M E T A C L A S S
346 \ Every class is an instance of metaclass. This lets
347 \ classes have methods that are different from those
348 \ of their instances.
349 \ Classes are IMMEDIATE to make early binding simpler
356 0 , \ NULL parent class
358 \ #if FICL_WANT_VCALL
359 4 cells , \ instance size
361 3 cells , \ instance size
366 \ now brand OBJECT's wordlist (so that ORDER can display it by name)
367 metaclass drop cell+ @ brand-wordlist
369 metaclass drop current-class !
373 \ C L A S S M E T H O D S
375 instance-vars >search
377 create .super ( class metaclass -- parent-class )
378 0 cells , do-instance-var
380 create .wid ( class metaclass -- wid ) \ return wid of class
381 1 cells , do-instance-var
383 \ #if FICL_WANT_VCALL
384 create .vtCount \ Number of VTABLE methods, if any
385 2 cells , do-instance-var
387 create .size ( class metaclass -- size ) \ return class's payload size
388 3 cells , do-instance-var
390 create .size ( class metaclass -- size ) \ return class's payload size
391 2 cells , do-instance-var
394 : get-size metaclass => .size @ ;
395 : get-wid metaclass => .wid @ ;
396 : get-super metaclass => .super @ ;
397 \ #if FICL_WANT_VCALL
398 : get-vtCount metaclass => .vtCount @ ;
399 : get-vtAdd metaclass => .vtCount ;
402 \ create an uninitialized instance of a class, leaving
403 \ the address of the new instance and its class
405 : instance ( class metaclass "name" -- instance class )
406 locals| meta parent |
408 here parent --> .do-instance \ ( inst class )
409 parent meta metaclass => get-size
410 allot \ allocate payload space
413 \ create an uninitialized array
414 : array ( n class metaclass "name" -- n instance class )
415 locals| meta parent nobj |
417 here parent --> .do-instance \ ( nobj inst class )
418 parent meta metaclass => get-size
419 nobj * allot \ allocate payload space
422 \ create an initialized instance
424 : new \ ( class metaclass "name" -- )
425 metaclass => instance --> init
428 \ create an initialized array of instances
429 : new-array ( n class metaclass "name" -- )
434 \ Create an anonymous initialized instance from the heap
435 : alloc \ ( class metaclass -- instance class )
437 class meta metaclass => get-size allocate ( -- addr fail-flag )
438 abort" allocate failed " ( -- addr )
442 \ Create an anonymous array of initialized instances from the heap
443 : alloc-array \ ( n class metaclass -- instance class )
444 locals| meta class nobj |
445 class meta metaclass => get-size
446 nobj * allocate ( -- addr fail-flag )
447 abort" allocate failed " ( -- addr )
448 nobj over class --> array-init
452 \ Create an anonymous initialized instance from the dictionary
453 : allot { 2:this -- 2:instance }
454 here ( instance-address )
455 this my=> get-size allot
456 this drop 2dup --> init
459 \ Create an anonymous array of initialized instances from the dictionary
460 : allot-array { nobj 2:this -- 2:instance }
461 here ( instance-address )
462 this my=> get-size nobj * allot
463 this drop 2dup ( 2instance 2instance )
464 nobj -rot --> array-init
467 \ create a proxy object with initialized payload address given
468 : ref ( instance-addr class metaclass "name" -- )
473 \ suspend-class and resume-class help to build mutually referent classes.
475 \ object subclass c-akbar
476 \ suspend-class ( put akbar on hold while we define jeff )
477 \ object subclass c-jeff
478 \ c-akbar ref: .akbar
479 \ ( and whatever else comprises this class )
480 \ end-class ( done with c-jeff )
481 \ c-akbar --> resume-class
483 \ ( and whatever else goes in c-akbar )
484 \ end-class ( done with c-akbar )
486 : resume-class { 2:this -- old-wid addr[size] size }
487 this --> .wid @ ficl-set-current ( old-wid )
488 this --> .size dup @ ( old-wid addr[size] size )
489 instance-vars >search
493 \ This method leaves the stack and search order ready for instance variable
494 \ building. Pushes the instance-vars wordlist onto the search order,
495 \ and sets the compilation wordlist to be the private wordlist of the
496 \ new class. The class's wordlist is deliberately NOT in the search order -
497 \ to prevent methods from getting used with wrong data.
498 \ Postcondition: leaves the address of the new class in current-class
499 : sub ( class metaclass "name" -- old-wid addr[size] size )
501 locals| wid meta parent |
502 parent meta metaclass => get-wid
503 wid wid-set-super \ set superclass
504 create immediate \ get the subclass name
505 wid brand-wordlist \ label the subclass wordlist
506 here current-class ! \ prep for do-do-instance
507 parent , \ save parent class
509 \ #if FICL_WANT_VCALL
510 parent meta --> get-vtCount ,
512 here parent meta --> get-size dup , ( addr[size] size )
513 metaclass => .do-instance
514 wid ficl-set-current -rot
516 instance-vars >search \ push struct builder wordlist
519 \ OFFSET-OF returns the offset of an instance variable
520 \ from the instance base address. If the next token is not
521 \ the name of in instance variable method, you get garbage
522 \ results -- there is no way at present to check for this error.
523 : offset-of ( class metaclass "name" -- offset )
524 drop find-method-xt nip >body @ ;
526 \ ID returns the string name cell-pair of its class
527 : id ( class metaclass -- c-addr u )
530 \ list methods of the class
531 : methods \ ( class meta -- )
534 class body> >name type ." methods:" cr
535 class meta --> get-wid >search words cr previous
536 class meta metaclass => get-super
541 \ list class's ancestors
542 : pedigree ( class meta -- )
545 class body> >name type space
546 class meta metaclass => get-super
551 \ decompile an instance method
552 : see ( class meta -- )
553 metaclass => get-wid >search see previous ;
555 \ debug a method of metaclass
556 \ Eg: my-class --> debug my-method
557 : debug ( class meta -- )
558 find-method-xt debug-xt ;
561 \ E N D M E T A C L A S S
563 \ ** META is a nickname for the address of METACLASS...
567 \ ** SUBCLASS is a nickname for a class's SUB method...
568 \ Subclass compilation ends when you invoke end-class
569 \ This method is late bound for safety...
572 \ #if FICL_WANT_VCALL
573 \ VTABLE Support extensions (Guy Carver)
574 \ object --> sub mine hasvtable
575 : hasvtable 4 + ; immediate
579 \ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
581 \ Root of all classes
585 0 , \ NULL parent class
591 \ now brand OBJECT's wordlist (so that ORDER can display it by name)
592 object drop cell+ @ brand-wordlist
594 object drop current-class !
596 instance-vars >search
598 \ O B J E C T M E T H O D S
599 \ Convert instance cell-pair to class cell-pair
600 \ Useful for binding class methods from an instance
601 : class ( instance class -- class metaclass )
604 \ default INIT method zero fills an instance
605 : init ( instance class -- )
607 metaclass => get-size ( inst size )
610 \ Apply INIT to an array of NOBJ objects...
612 : array-init ( nobj inst class -- )
613 0 dup locals| &init &next class inst |
615 \ bind methods outside the loop to save time
617 class s" init" lookup-method to &init
618 s" next" lookup-method to &next
623 &next execute drop to inst
627 \ free storage allocated to a heap instance by alloc or alloc-array
628 \ NOTE: not protected against errors like FREEing something that's
629 \ really in the dictionary.
630 : free \ ( instance class -- )
635 \ Instance aliases for common class methods
636 \ Upcast to parent class
637 : super ( instance class -- instance parent-class )
638 meta metaclass => get-super ;
640 : pedigree ( instance class -- )
642 metaclass => pedigree ;
644 : size ( instance class -- sizeof-instance )
646 metaclass => get-size ;
648 : methods ( instance class -- )
650 metaclass => methods ;
652 \ Array indexing methods...
654 \ 10 object-array --> index
657 : index ( n instance class -- instance[n] class )
661 metaclass => get-size * ( n*size )
664 : next ( instance[n] class -- instance[n+1] class )
668 metaclass => get-size
672 : prev ( instance[n] class -- instance[n-1] class )
676 metaclass => get-size
680 : debug ( 2this -- ?? )
681 find-method-xt debug-xt ;
686 \ reset to default search order
689 \ redefine oop in default search order to put OOP words in the search order and make them
690 \ the compiling wordlist...
692 : oo only also oop definitions ;