2001-04-29 02:36:36 +00:00
|
|
|
\ #if FICL_WANT_OOP
|
1998-11-03 06:11:35 +00:00
|
|
|
\ ** ficl/softwords/oo.fr
|
|
|
|
\ ** F I C L O - O E X T E N S I O N S
|
|
|
|
\ ** john sadler aug 1998
|
2000-05-26 21:35:08 +00:00
|
|
|
\
|
|
|
|
\ $FreeBSD$
|
|
|
|
|
2001-04-29 02:36:36 +00:00
|
|
|
17 ficl-vocabulary oop
|
1998-11-03 06:11:35 +00:00
|
|
|
also oop definitions
|
|
|
|
|
|
|
|
\ Design goals:
|
|
|
|
\ 0. Traditional OOP: late binding by default for safety.
|
|
|
|
\ Early binding if you ask for it.
|
|
|
|
\ 1. Single inheritance
|
|
|
|
\ 2. Object aggregation (has-a relationship)
|
|
|
|
\ 3. Support objects in the dictionary and as proxies for
|
|
|
|
\ existing structures (by reference):
|
|
|
|
\ *** A ficl object can wrap a C struct ***
|
|
|
|
\ 4. Separate name-spaces for methods - methods are
|
|
|
|
\ only visible in the context of a class / object
|
|
|
|
\ 5. Methods can be overridden, and subclasses can add methods.
|
|
|
|
\ No limit on number of methods.
|
|
|
|
|
|
|
|
\ General info:
|
|
|
|
\ Classes are objects, too: all classes are instances of METACLASS
|
|
|
|
\ All classes are derived (by convention) from OBJECT. This
|
|
|
|
\ base class provides a default initializer and superclass
|
|
|
|
\ access method
|
|
|
|
|
|
|
|
\ A ficl object binds instance storage (payload) to a class.
|
|
|
|
\ object ( -- instance class )
|
|
|
|
\ All objects push their payload address and class address when
|
2002-04-09 17:45:28 +00:00
|
|
|
\ executed.
|
1998-11-03 06:11:35 +00:00
|
|
|
|
|
|
|
\ A ficl class consists of a parent class pointer, a wordlist
|
|
|
|
\ ID for the methods of the class, and a size for the payload
|
|
|
|
\ of objects created by the class. A class is an object.
|
|
|
|
\ The NEW method creates and initializes an instance of a class.
|
|
|
|
\ Classes have this footprint:
|
|
|
|
\ cell 0: parent class address
|
|
|
|
\ cell 1: wordlist ID
|
|
|
|
\ cell 2: size of instance's payload
|
|
|
|
|
|
|
|
\ Methods expect an object couple ( instance class )
|
2002-04-09 17:45:28 +00:00
|
|
|
\ on the stack. This is by convention - ficl has no way to
|
|
|
|
\ police your code to make sure this is always done, but it
|
|
|
|
\ happens naturally if you use the facilities presented here.
|
|
|
|
\
|
1998-11-03 06:11:35 +00:00
|
|
|
\ Overridden methods must maintain the same stack signature as
|
2002-04-09 17:45:28 +00:00
|
|
|
\ their predecessors. Ficl has no way of enforcing this, either.
|
|
|
|
\
|
|
|
|
\ Revised Apr 2001 - Added Guy Carver's vtable extensions. Class now
|
|
|
|
\ has an extra field for the vtable method count. Hasvtable declares
|
|
|
|
\ refs to vtable classes
|
|
|
|
\
|
|
|
|
\ Revised Nov 2001 - metaclass debug method now finds only metaclass methods
|
|
|
|
\
|
|
|
|
\ Planned: Ficl vtable support
|
|
|
|
\ Each class has a vtable size parameter
|
|
|
|
\ END-CLASS allocates and clears the vtable - then it walks class's method
|
|
|
|
\ list and inserts all new methods into table. For each method, if the table
|
|
|
|
\ slot is already nonzero, do nothing (overridden method). Otherwise fill
|
|
|
|
\ vtable slot. Now do same check for parent class vtable, filling only
|
|
|
|
\ empty slots in the new vtable.
|
|
|
|
\ Methods are now structured as follows:
|
|
|
|
\ - header
|
|
|
|
\ - vtable index
|
|
|
|
\ - xt
|
|
|
|
\ :noname definition for code
|
|
|
|
\
|
|
|
|
\ : is redefined to check for override, fill in vtable index, increment method
|
|
|
|
\ count if not an override, create header and fill in index. Allot code pointer
|
|
|
|
\ and run :noname
|
|
|
|
\ ; is overridden to fill in xt returned by :noname
|
|
|
|
\ --> compiles code to fetch vtable address, offset by index, and execute
|
|
|
|
\ => looks up xt in the vtable and compiles it directly
|
|
|
|
|
|
|
|
|
1998-11-03 06:11:35 +00:00
|
|
|
|
|
|
|
user current-class
|
|
|
|
0 current-class !
|
|
|
|
|
|
|
|
\ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
|
|
|
|
\ ** L A T E B I N D I N G
|
|
|
|
\ Compile the method name, and code to find and
|
|
|
|
\ execute it at run-time...
|
|
|
|
\
|
2001-04-29 02:36:36 +00:00
|
|
|
|
2002-04-09 17:45:28 +00:00
|
|
|
\ p a r s e - m e t h o d
|
|
|
|
\ compiles a method name so that it pushes
|
|
|
|
\ the string base address and count at run-time.
|
|
|
|
|
1998-11-03 06:11:35 +00:00
|
|
|
: parse-method \ name run: ( -- c-addr u )
|
|
|
|
parse-word
|
2002-04-09 17:45:28 +00:00
|
|
|
postpone sliteral
|
1998-11-03 06:11:35 +00:00
|
|
|
; compile-only
|
|
|
|
|
2007-03-23 22:26:01 +00:00
|
|
|
|
|
|
|
|
|
|
|
: (lookup-method) { class 2:name -- class 0 | class xt 1 | class xt -1 }
|
|
|
|
class name class cell+ @ ( class c-addr u wid )
|
|
|
|
search-wordlist
|
|
|
|
;
|
|
|
|
|
2002-04-09 17:45:28 +00:00
|
|
|
\ l o o k u p - m e t h o d
|
|
|
|
\ takes a counted string method name from the stack (as compiled
|
|
|
|
\ by parse-method) and attempts to look this method up in the method list of
|
|
|
|
\ the class that's on the stack. If successful, it leaves the class on the stack
|
|
|
|
\ and pushes the xt of the method. If not, it aborts with an error message.
|
|
|
|
|
2001-04-29 02:36:36 +00:00
|
|
|
: lookup-method { class 2:name -- class xt }
|
2007-03-23 22:26:01 +00:00
|
|
|
class name (lookup-method) ( 0 | xt 1 | xt -1 )
|
2002-04-09 17:45:28 +00:00
|
|
|
0= if
|
|
|
|
name type ." not found in "
|
2001-04-29 02:36:36 +00:00
|
|
|
class body> >name type
|
1998-11-03 06:11:35 +00:00
|
|
|
cr abort
|
2002-04-09 17:45:28 +00:00
|
|
|
endif
|
1998-11-03 06:11:35 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
: find-method-xt \ name ( class -- class xt )
|
2002-04-09 17:45:28 +00:00
|
|
|
parse-word lookup-method
|
1998-11-03 06:11:35 +00:00
|
|
|
;
|
|
|
|
|
2001-04-29 02:36:36 +00:00
|
|
|
: catch-method ( instance class c-addr u -- <method-signature> exc-flag )
|
|
|
|
lookup-method catch
|
|
|
|
;
|
|
|
|
|
|
|
|
: exec-method ( instance class c-addr u -- <method-signature> )
|
|
|
|
lookup-method execute
|
|
|
|
;
|
1998-11-03 06:11:35 +00:00
|
|
|
|
|
|
|
\ Method lookup operator takes a class-addr and instance-addr
|
|
|
|
\ and executes the method from the class's wordlist if
|
|
|
|
\ interpreting. If compiling, bind late.
|
|
|
|
\
|
|
|
|
: --> ( instance class -- ??? )
|
|
|
|
state @ 0= if
|
2002-04-09 17:45:28 +00:00
|
|
|
find-method-xt execute
|
1998-11-03 06:11:35 +00:00
|
|
|
else
|
2002-04-09 17:45:28 +00:00
|
|
|
parse-method postpone exec-method
|
1998-11-03 06:11:35 +00:00
|
|
|
endif
|
|
|
|
; immediate
|
|
|
|
|
2001-04-29 02:36:36 +00:00
|
|
|
\ Method lookup with CATCH in case of exceptions
|
|
|
|
: c-> ( instance class -- ?? exc-flag )
|
|
|
|
state @ 0= if
|
2002-04-09 17:45:28 +00:00
|
|
|
find-method-xt catch
|
2001-04-29 02:36:36 +00:00
|
|
|
else
|
2002-04-09 17:45:28 +00:00
|
|
|
parse-method postpone catch-method
|
2001-04-29 02:36:36 +00:00
|
|
|
endif
|
|
|
|
; immediate
|
|
|
|
|
|
|
|
\ METHOD makes global words that do method invocations by late binding
|
|
|
|
\ in case you prefer this style (no --> in your code)
|
2002-04-09 17:45:28 +00:00
|
|
|
\ Example: everything has next and prev for array access, so...
|
|
|
|
\ method next
|
|
|
|
\ method prev
|
|
|
|
\ my-instance next ( does whatever next does to my-instance by late binding )
|
|
|
|
|
2001-04-29 02:36:36 +00:00
|
|
|
: method create does> body> >name lookup-method execute ;
|
|
|
|
|
1998-11-03 06:11:35 +00:00
|
|
|
|
|
|
|
\ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
|
|
|
|
\ ** E A R L Y B I N D I N G
|
|
|
|
\ Early binding operator compiles code to execute a method
|
|
|
|
\ given its class at compile time. Classes are immediate,
|
|
|
|
\ so they leave their cell-pair on the stack when compiling.
|
|
|
|
\ Example:
|
|
|
|
\ : get-wid metaclass => .wid @ ;
|
|
|
|
\ Usage
|
|
|
|
\ my-class get-wid ( -- wid-of-my-class )
|
|
|
|
\
|
2001-04-29 02:36:36 +00:00
|
|
|
1 ficl-named-wordlist instance-vars
|
|
|
|
instance-vars dup >search ficl-set-current
|
|
|
|
|
1998-11-03 06:11:35 +00:00
|
|
|
: => \ c:( class meta -- ) run: ( -- ??? ) invokes compiled method
|
2002-04-09 17:45:28 +00:00
|
|
|
drop find-method-xt compile, drop
|
1998-11-03 06:11:35 +00:00
|
|
|
; immediate compile-only
|
|
|
|
|
2001-04-29 02:36:36 +00:00
|
|
|
: my=> \ c:( -- ) run: ( -- ??? ) late bind compiled method of current-class
|
|
|
|
current-class @ dup postpone =>
|
|
|
|
; immediate compile-only
|
|
|
|
|
2002-04-09 17:45:28 +00:00
|
|
|
\ Problem: my=[ assumes that each method except the last is am obj: member
|
|
|
|
\ which contains its class as the first field of its parameter area. The code
|
|
|
|
\ detects non-obect members and assumes the class does not change in this case.
|
|
|
|
\ This handles methods like index, prev, and next correctly, but does not deal
|
|
|
|
\ correctly with CLASS.
|
2001-04-29 02:36:36 +00:00
|
|
|
: my=[ \ same as my=> , but binds a chain of methods
|
|
|
|
current-class @
|
|
|
|
begin
|
2002-04-09 17:45:28 +00:00
|
|
|
parse-word 2dup ( class c-addr u c-addr u )
|
|
|
|
s" ]" compare while ( class c-addr u )
|
|
|
|
lookup-method ( class xt )
|
|
|
|
dup compile, ( class xt )
|
|
|
|
dup ?object if \ If object member, get new class. Otherwise assume same class
|
|
|
|
nip >body cell+ @ ( new-class )
|
|
|
|
else
|
|
|
|
drop ( class )
|
|
|
|
endif
|
2001-04-29 02:36:36 +00:00
|
|
|
repeat 2drop drop
|
|
|
|
; immediate compile-only
|
|
|
|
|
1998-11-03 06:11:35 +00:00
|
|
|
|
|
|
|
\ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
|
|
|
|
\ ** I N S T A N C E V A R I A B L E S
|
|
|
|
\ Instance variables (IV) are represented by words in the class's
|
|
|
|
\ private wordlist. Each IV word contains the offset
|
|
|
|
\ of the IV it represents, and runs code to add that offset
|
|
|
|
\ to the base address of an instance when executed.
|
|
|
|
\ The metaclass SUB method, defined below, leaves the address
|
|
|
|
\ of the new class's offset field and its initial size on the
|
|
|
|
\ stack for these words to update. When a class definition is
|
|
|
|
\ complete, END-CLASS saves the final size in the class's size
|
|
|
|
\ field, and restores the search order and compile wordlist to
|
|
|
|
\ prior state. Note that these words are hidden in their own
|
|
|
|
\ wordlist to prevent accidental use outside a SUB END-CLASS pair.
|
|
|
|
\
|
|
|
|
: do-instance-var
|
|
|
|
does> ( instance class addr[offset] -- addr[field] )
|
2002-04-09 17:45:28 +00:00
|
|
|
nip @ +
|
1998-11-03 06:11:35 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
: addr-units: ( offset size "name" -- offset' )
|
|
|
|
create over , +
|
|
|
|
do-instance-var
|
|
|
|
;
|
|
|
|
|
2002-04-09 17:45:28 +00:00
|
|
|
: chars: \ ( offset nCells "name" -- offset' ) Create n char member.
|
1998-11-03 06:11:35 +00:00
|
|
|
chars addr-units: ;
|
|
|
|
|
2002-04-09 17:45:28 +00:00
|
|
|
: char: \ ( offset nCells "name" -- offset' ) Create 1 char member.
|
1998-11-03 06:11:35 +00:00
|
|
|
1 chars: ;
|
|
|
|
|
|
|
|
: cells: ( offset nCells "name" -- offset' )
|
2002-04-09 17:45:28 +00:00
|
|
|
cells >r aligned r> addr-units:
|
1998-11-03 06:11:35 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
: cell: ( offset nCells "name" -- offset' )
|
|
|
|
1 cells: ;
|
|
|
|
|
|
|
|
\ Aggregate an object into the class...
|
|
|
|
\ Needs the class of the instance to create
|
|
|
|
\ Example: object obj: m_obj
|
|
|
|
\
|
|
|
|
: do-aggregate
|
2002-04-09 17:45:28 +00:00
|
|
|
objectify
|
|
|
|
does> ( instance class pfa -- a-instance a-class )
|
|
|
|
2@ ( inst class a-class a-offset )
|
|
|
|
2swap drop ( a-class a-offset inst )
|
|
|
|
+ swap ( a-inst a-class )
|
1998-11-03 06:11:35 +00:00
|
|
|
;
|
|
|
|
|
2002-04-09 17:45:28 +00:00
|
|
|
: obj: { offset class meta -- offset' } \ "name"
|
1998-11-03 06:11:35 +00:00
|
|
|
create offset , class ,
|
2002-04-09 17:45:28 +00:00
|
|
|
class meta --> get-size offset +
|
|
|
|
do-aggregate
|
1998-11-03 06:11:35 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
\ Aggregate an array of objects into a class
|
|
|
|
\ Usage example:
|
|
|
|
\ 3 my-class array: my-array
|
|
|
|
\ Makes an instance variable array of 3 instances of my-class
|
|
|
|
\ named my-array.
|
|
|
|
\
|
|
|
|
: array: ( offset n class meta "name" -- offset' )
|
2002-04-09 17:45:28 +00:00
|
|
|
locals| meta class nobjs offset |
|
|
|
|
create offset , class ,
|
|
|
|
class meta --> get-size nobjs * offset +
|
|
|
|
do-aggregate
|
1998-11-03 06:11:35 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
\ Aggregate a pointer to an object: REF is a member variable
|
|
|
|
\ whose class is set at compile time. This is useful for wrapping
|
|
|
|
\ data structures in C, where there is only a pointer and the type
|
|
|
|
\ it refers to is known. If you want polymorphism, see c_ref
|
|
|
|
\ in classes.fr. REF is only useful for pre-initialized structures,
|
|
|
|
\ since there's no supported way to set one.
|
|
|
|
: ref: ( offset class meta "name" -- offset' )
|
2002-04-09 17:45:28 +00:00
|
|
|
locals| meta class offset |
|
|
|
|
create offset , class ,
|
|
|
|
offset cell+
|
|
|
|
does> ( inst class pfa -- ptr-inst ptr-class )
|
|
|
|
2@ ( inst class ptr-class ptr-offset )
|
|
|
|
2swap drop + @ swap
|
|
|
|
;
|
|
|
|
|
|
|
|
\ #if FICL_WANT_VCALL
|
|
|
|
\ vcall extensions contributed by Guy Carver
|
|
|
|
: vcall: ( paramcnt "name" -- )
|
|
|
|
current-class @ 8 + dup @ dup 1+ rot ! \ Kludge fix to get to .vtCount before it's defined.
|
|
|
|
create , , \ ( paramcnt index -- )
|
|
|
|
does> \ ( inst class pfa -- ptr-inst ptr-class )
|
|
|
|
nip 2@ vcall \ ( params offset inst class offset -- )
|
|
|
|
;
|
|
|
|
|
|
|
|
: vcallr: 0x80000000 or vcall: ; \ Call with return address desired.
|
|
|
|
|
|
|
|
\ #if FICL_WANT_FLOAT
|
|
|
|
: vcallf: \ ( paramcnt -<name>- f: r )
|
|
|
|
0x80000000 or
|
|
|
|
current-class @ 8 + dup @ dup 1+ rot ! \ Kludge fix to get to .vtCount before it's defined.
|
|
|
|
create , , \ ( paramcnt index -- )
|
|
|
|
does> \ ( inst class pfa -- ptr-inst ptr-class )
|
|
|
|
nip 2@ vcall f> \ ( params offset inst class offset -- f: r )
|
1998-11-03 06:11:35 +00:00
|
|
|
;
|
2002-04-09 17:45:28 +00:00
|
|
|
\ #endif /* FLOAT */
|
|
|
|
\ #endif /* VCALL */
|
1998-11-03 06:11:35 +00:00
|
|
|
|
|
|
|
\ END-CLASS terminates construction of a class by storing
|
|
|
|
\ the size of its instance variables in the class's size field
|
|
|
|
\ ( -- old-wid addr[size] 0 )
|
|
|
|
\
|
|
|
|
: end-class ( old-wid addr[size] size -- )
|
|
|
|
swap ! set-current
|
2002-04-09 17:45:28 +00:00
|
|
|
search> drop \ pop struct builder wordlist
|
1998-11-03 06:11:35 +00:00
|
|
|
;
|
|
|
|
|
2001-04-29 02:36:36 +00:00
|
|
|
\ See resume-class (a metaclass method) below for usage
|
|
|
|
\ This is equivalent to end-class for now, but that will change
|
|
|
|
\ when we support vtable bindings.
|
|
|
|
: suspend-class ( old-wid addr[size] size -- ) end-class ;
|
|
|
|
|
1998-11-03 06:11:35 +00:00
|
|
|
set-current previous
|
|
|
|
\ E N D I N S T A N C E V A R I A B L E S
|
|
|
|
|
|
|
|
|
|
|
|
\ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
|
|
|
|
\ D O - D O - I N S T A N C E
|
|
|
|
\ Makes a class method that contains the code for an
|
|
|
|
\ instance of the class. This word gets compiled into
|
|
|
|
\ the wordlist of every class by the SUB method.
|
|
|
|
\ PRECONDITION: current-class contains the class address
|
2000-05-26 21:35:08 +00:00
|
|
|
\ why use a state variable instead of the stack?
|
2002-04-09 17:45:28 +00:00
|
|
|
\ >> Stack state is not well-defined during compilation (there are
|
2000-05-26 21:35:08 +00:00
|
|
|
\ >> control structure match codes on the stack, of undefined size
|
|
|
|
\ >> easiest way around this is use of this thread-local variable
|
1998-11-03 06:11:35 +00:00
|
|
|
\
|
|
|
|
: do-do-instance ( -- )
|
|
|
|
s" : .do-instance does> [ current-class @ ] literal ;"
|
|
|
|
evaluate
|
|
|
|
;
|
|
|
|
|
|
|
|
\ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
|
|
|
|
\ ** M E T A C L A S S
|
|
|
|
\ Every class is an instance of metaclass. This lets
|
|
|
|
\ classes have methods that are different from those
|
|
|
|
\ of their instances.
|
|
|
|
\ Classes are IMMEDIATE to make early binding simpler
|
|
|
|
\ See above...
|
|
|
|
\
|
|
|
|
:noname
|
2002-04-09 17:45:28 +00:00
|
|
|
wordlist
|
|
|
|
create
|
2000-05-26 21:35:08 +00:00
|
|
|
immediate
|
2002-04-09 17:45:28 +00:00
|
|
|
0 , \ NULL parent class
|
|
|
|
dup , \ wid
|
|
|
|
\ #if FICL_WANT_VCALL
|
|
|
|
4 cells , \ instance size
|
|
|
|
\ #else
|
|
|
|
3 cells , \ instance size
|
|
|
|
\ #endif
|
|
|
|
ficl-set-current
|
|
|
|
does> dup
|
2001-04-29 02:36:36 +00:00
|
|
|
; execute metaclass
|
|
|
|
\ now brand OBJECT's wordlist (so that ORDER can display it by name)
|
|
|
|
metaclass drop cell+ @ brand-wordlist
|
1998-11-03 06:11:35 +00:00
|
|
|
|
|
|
|
metaclass drop current-class !
|
|
|
|
do-do-instance
|
|
|
|
|
|
|
|
\
|
|
|
|
\ C L A S S M E T H O D S
|
|
|
|
\
|
|
|
|
instance-vars >search
|
|
|
|
|
|
|
|
create .super ( class metaclass -- parent-class )
|
|
|
|
0 cells , do-instance-var
|
|
|
|
|
|
|
|
create .wid ( class metaclass -- wid ) \ return wid of class
|
|
|
|
1 cells , do-instance-var
|
|
|
|
|
2002-04-09 17:45:28 +00:00
|
|
|
\ #if FICL_WANT_VCALL
|
|
|
|
create .vtCount \ Number of VTABLE methods, if any
|
|
|
|
2 cells , do-instance-var
|
|
|
|
|
|
|
|
create .size ( class metaclass -- size ) \ return class's payload size
|
|
|
|
3 cells , do-instance-var
|
|
|
|
\ #else
|
1998-11-03 06:11:35 +00:00
|
|
|
create .size ( class metaclass -- size ) \ return class's payload size
|
|
|
|
2 cells , do-instance-var
|
2002-04-09 17:45:28 +00:00
|
|
|
\ #endif
|
1998-11-03 06:11:35 +00:00
|
|
|
|
|
|
|
: get-size metaclass => .size @ ;
|
|
|
|
: get-wid metaclass => .wid @ ;
|
|
|
|
: get-super metaclass => .super @ ;
|
2002-04-09 17:45:28 +00:00
|
|
|
\ #if FICL_WANT_VCALL
|
|
|
|
: get-vtCount metaclass => .vtCount @ ;
|
|
|
|
: get-vtAdd metaclass => .vtCount ;
|
|
|
|
\ #endif
|
1998-11-03 06:11:35 +00:00
|
|
|
|
|
|
|
\ create an uninitialized instance of a class, leaving
|
|
|
|
\ the address of the new instance and its class
|
|
|
|
\
|
|
|
|
: instance ( class metaclass "name" -- instance class )
|
|
|
|
locals| meta parent |
|
2002-04-09 17:45:28 +00:00
|
|
|
create
|
1998-11-03 06:11:35 +00:00
|
|
|
here parent --> .do-instance \ ( inst class )
|
|
|
|
parent meta metaclass => get-size
|
|
|
|
allot \ allocate payload space
|
|
|
|
;
|
|
|
|
|
|
|
|
\ create an uninitialized array
|
|
|
|
: array ( n class metaclass "name" -- n instance class )
|
|
|
|
locals| meta parent nobj |
|
2002-04-09 17:45:28 +00:00
|
|
|
create nobj
|
1998-11-03 06:11:35 +00:00
|
|
|
here parent --> .do-instance \ ( nobj inst class )
|
|
|
|
parent meta metaclass => get-size
|
2002-04-09 17:45:28 +00:00
|
|
|
nobj * allot \ allocate payload space
|
1998-11-03 06:11:35 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
\ create an initialized instance
|
|
|
|
\
|
|
|
|
: new \ ( class metaclass "name" -- )
|
|
|
|
metaclass => instance --> init
|
|
|
|
;
|
|
|
|
|
|
|
|
\ create an initialized array of instances
|
|
|
|
: new-array ( n class metaclass "name" -- )
|
2002-04-09 17:45:28 +00:00
|
|
|
metaclass => array
|
|
|
|
--> array-init
|
1998-11-03 06:11:35 +00:00
|
|
|
;
|
|
|
|
|
2000-05-26 21:35:08 +00:00
|
|
|
\ Create an anonymous initialized instance from the heap
|
|
|
|
: alloc \ ( class metaclass -- instance class )
|
|
|
|
locals| meta class |
|
|
|
|
class meta metaclass => get-size allocate ( -- addr fail-flag )
|
|
|
|
abort" allocate failed " ( -- addr )
|
|
|
|
class 2dup --> init
|
|
|
|
;
|
|
|
|
|
|
|
|
\ Create an anonymous array of initialized instances from the heap
|
|
|
|
: alloc-array \ ( n class metaclass -- instance class )
|
|
|
|
locals| meta class nobj |
|
|
|
|
class meta metaclass => get-size
|
|
|
|
nobj * allocate ( -- addr fail-flag )
|
|
|
|
abort" allocate failed " ( -- addr )
|
|
|
|
nobj over class --> array-init
|
|
|
|
class
|
|
|
|
;
|
|
|
|
|
2001-04-29 02:36:36 +00:00
|
|
|
\ Create an anonymous initialized instance from the dictionary
|
|
|
|
: allot { 2:this -- 2:instance }
|
|
|
|
here ( instance-address )
|
|
|
|
this my=> get-size allot
|
|
|
|
this drop 2dup --> init
|
|
|
|
;
|
|
|
|
|
|
|
|
\ Create an anonymous array of initialized instances from the dictionary
|
|
|
|
: allot-array { nobj 2:this -- 2:instance }
|
|
|
|
here ( instance-address )
|
|
|
|
this my=> get-size nobj * allot
|
|
|
|
this drop 2dup ( 2instance 2instance )
|
|
|
|
nobj -rot --> array-init
|
|
|
|
;
|
|
|
|
|
1998-11-03 06:11:35 +00:00
|
|
|
\ create a proxy object with initialized payload address given
|
|
|
|
: ref ( instance-addr class metaclass "name" -- )
|
|
|
|
drop create , ,
|
|
|
|
does> 2@
|
|
|
|
;
|
|
|
|
|
2001-04-29 02:36:36 +00:00
|
|
|
\ suspend-class and resume-class help to build mutually referent classes.
|
|
|
|
\ Example:
|
|
|
|
\ object subclass c-akbar
|
|
|
|
\ suspend-class ( put akbar on hold while we define jeff )
|
|
|
|
\ object subclass c-jeff
|
|
|
|
\ c-akbar ref: .akbar
|
|
|
|
\ ( and whatever else comprises this class )
|
|
|
|
\ end-class ( done with c-jeff )
|
|
|
|
\ c-akbar --> resume-class
|
|
|
|
\ c-jeff ref: .jeff
|
|
|
|
\ ( and whatever else goes in c-akbar )
|
|
|
|
\ end-class ( done with c-akbar )
|
|
|
|
\
|
|
|
|
: resume-class { 2:this -- old-wid addr[size] size }
|
|
|
|
this --> .wid @ ficl-set-current ( old-wid )
|
|
|
|
this --> .size dup @ ( old-wid addr[size] size )
|
|
|
|
instance-vars >search
|
|
|
|
;
|
|
|
|
|
1998-11-03 06:11:35 +00:00
|
|
|
\ create a subclass
|
2001-04-29 02:36:36 +00:00
|
|
|
\ This method leaves the stack and search order ready for instance variable
|
|
|
|
\ building. Pushes the instance-vars wordlist onto the search order,
|
|
|
|
\ and sets the compilation wordlist to be the private wordlist of the
|
|
|
|
\ new class. The class's wordlist is deliberately NOT in the search order -
|
|
|
|
\ to prevent methods from getting used with wrong data.
|
|
|
|
\ Postcondition: leaves the address of the new class in current-class
|
1998-11-03 06:11:35 +00:00
|
|
|
: sub ( class metaclass "name" -- old-wid addr[size] size )
|
|
|
|
wordlist
|
2002-04-09 17:45:28 +00:00
|
|
|
locals| wid meta parent |
|
|
|
|
parent meta metaclass => get-wid
|
|
|
|
wid wid-set-super \ set superclass
|
|
|
|
create immediate \ get the subclass name
|
2001-04-29 02:36:36 +00:00
|
|
|
wid brand-wordlist \ label the subclass wordlist
|
2002-04-09 17:45:28 +00:00
|
|
|
here current-class ! \ prep for do-do-instance
|
|
|
|
parent , \ save parent class
|
|
|
|
wid , \ save wid
|
|
|
|
\ #if FICL_WANT_VCALL
|
|
|
|
parent meta --> get-vtCount ,
|
|
|
|
\ #endif
|
|
|
|
here parent meta --> get-size dup , ( addr[size] size )
|
|
|
|
metaclass => .do-instance
|
|
|
|
wid ficl-set-current -rot
|
|
|
|
do-do-instance
|
|
|
|
instance-vars >search \ push struct builder wordlist
|
1998-11-03 06:11:35 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
\ OFFSET-OF returns the offset of an instance variable
|
|
|
|
\ from the instance base address. If the next token is not
|
|
|
|
\ the name of in instance variable method, you get garbage
|
|
|
|
\ results -- there is no way at present to check for this error.
|
|
|
|
: offset-of ( class metaclass "name" -- offset )
|
|
|
|
drop find-method-xt nip >body @ ;
|
|
|
|
|
|
|
|
\ ID returns the string name cell-pair of its class
|
|
|
|
: id ( class metaclass -- c-addr u )
|
2002-04-09 17:45:28 +00:00
|
|
|
drop body> >name ;
|
1998-11-03 06:11:35 +00:00
|
|
|
|
|
|
|
\ list methods of the class
|
|
|
|
: methods \ ( class meta -- )
|
2002-04-09 17:45:28 +00:00
|
|
|
locals| meta class |
|
|
|
|
begin
|
|
|
|
class body> >name type ." methods:" cr
|
|
|
|
class meta --> get-wid >search words cr previous
|
|
|
|
class meta metaclass => get-super
|
|
|
|
dup to class
|
|
|
|
0= until cr
|
1998-11-03 06:11:35 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
\ list class's ancestors
|
|
|
|
: pedigree ( class meta -- )
|
2002-04-09 17:45:28 +00:00
|
|
|
locals| meta class |
|
|
|
|
begin
|
|
|
|
class body> >name type space
|
|
|
|
class meta metaclass => get-super
|
|
|
|
dup to class
|
|
|
|
0= until cr
|
1998-11-03 06:11:35 +00:00
|
|
|
;
|
|
|
|
|
2002-04-09 17:45:28 +00:00
|
|
|
\ decompile an instance method
|
1998-11-03 06:11:35 +00:00
|
|
|
: see ( class meta -- )
|
|
|
|
metaclass => get-wid >search see previous ;
|
|
|
|
|
2002-04-09 17:45:28 +00:00
|
|
|
\ debug a method of metaclass
|
|
|
|
\ Eg: my-class --> debug my-method
|
|
|
|
: debug ( class meta -- )
|
|
|
|
find-method-xt debug-xt ;
|
|
|
|
|
|
|
|
previous set-current
|
1998-11-03 06:11:35 +00:00
|
|
|
\ E N D M E T A C L A S S
|
|
|
|
|
2001-04-29 02:36:36 +00:00
|
|
|
\ ** META is a nickname for the address of METACLASS...
|
1998-11-03 06:11:35 +00:00
|
|
|
metaclass drop
|
|
|
|
constant meta
|
|
|
|
|
2001-04-29 02:36:36 +00:00
|
|
|
\ ** SUBCLASS is a nickname for a class's SUB method...
|
1998-11-03 06:11:35 +00:00
|
|
|
\ Subclass compilation ends when you invoke end-class
|
|
|
|
\ This method is late bound for safety...
|
|
|
|
: subclass --> sub ;
|
|
|
|
|
2002-04-09 17:45:28 +00:00
|
|
|
\ #if FICL_WANT_VCALL
|
|
|
|
\ VTABLE Support extensions (Guy Carver)
|
|
|
|
\ object --> sub mine hasvtable
|
|
|
|
: hasvtable 4 + ; immediate
|
|
|
|
\ #endif
|
|
|
|
|
1998-11-03 06:11:35 +00:00
|
|
|
|
|
|
|
\ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
|
|
|
|
\ ** O B J E C T
|
|
|
|
\ Root of all classes
|
|
|
|
:noname
|
2002-04-09 17:45:28 +00:00
|
|
|
wordlist
|
|
|
|
create immediate
|
|
|
|
0 , \ NULL parent class
|
|
|
|
dup , \ wid
|
|
|
|
0 , \ instance size
|
|
|
|
ficl-set-current
|
|
|
|
does> meta
|
1998-11-03 06:11:35 +00:00
|
|
|
; execute object
|
2001-04-29 02:36:36 +00:00
|
|
|
\ now brand OBJECT's wordlist (so that ORDER can display it by name)
|
|
|
|
object drop cell+ @ brand-wordlist
|
1998-11-03 06:11:35 +00:00
|
|
|
|
|
|
|
object drop current-class !
|
|
|
|
do-do-instance
|
2001-04-29 02:36:36 +00:00
|
|
|
instance-vars >search
|
1998-11-03 06:11:35 +00:00
|
|
|
|
|
|
|
\ O B J E C T M E T H O D S
|
|
|
|
\ Convert instance cell-pair to class cell-pair
|
|
|
|
\ Useful for binding class methods from an instance
|
|
|
|
: class ( instance class -- class metaclass )
|
2002-04-09 17:45:28 +00:00
|
|
|
nip meta ;
|
1998-11-03 06:11:35 +00:00
|
|
|
|
|
|
|
\ default INIT method zero fills an instance
|
|
|
|
: init ( instance class -- )
|
|
|
|
meta
|
|
|
|
metaclass => get-size ( inst size )
|
|
|
|
erase ;
|
|
|
|
|
|
|
|
\ Apply INIT to an array of NOBJ objects...
|
|
|
|
\
|
|
|
|
: array-init ( nobj inst class -- )
|
2002-04-09 17:45:28 +00:00
|
|
|
0 dup locals| &init &next class inst |
|
|
|
|
\
|
|
|
|
\ bind methods outside the loop to save time
|
|
|
|
\
|
|
|
|
class s" init" lookup-method to &init
|
|
|
|
s" next" lookup-method to &next
|
|
|
|
drop
|
|
|
|
0 ?do
|
|
|
|
inst class 2dup
|
|
|
|
&init execute
|
|
|
|
&next execute drop to inst
|
|
|
|
loop
|
1998-11-03 06:11:35 +00:00
|
|
|
;
|
|
|
|
|
2000-05-26 21:35:08 +00:00
|
|
|
\ free storage allocated to a heap instance by alloc or alloc-array
|
|
|
|
\ NOTE: not protected against errors like FREEing something that's
|
|
|
|
\ really in the dictionary.
|
|
|
|
: free \ ( instance class -- )
|
2002-04-09 17:45:28 +00:00
|
|
|
drop free
|
|
|
|
abort" free failed "
|
2000-05-26 21:35:08 +00:00
|
|
|
;
|
|
|
|
|
1998-11-03 06:11:35 +00:00
|
|
|
\ Instance aliases for common class methods
|
|
|
|
\ Upcast to parent class
|
|
|
|
: super ( instance class -- instance parent-class )
|
|
|
|
meta metaclass => get-super ;
|
|
|
|
|
|
|
|
: pedigree ( instance class -- )
|
2002-04-09 17:45:28 +00:00
|
|
|
object => class
|
1998-11-03 06:11:35 +00:00
|
|
|
metaclass => pedigree ;
|
|
|
|
|
|
|
|
: size ( instance class -- sizeof-instance )
|
2002-04-09 17:45:28 +00:00
|
|
|
object => class
|
1998-11-03 06:11:35 +00:00
|
|
|
metaclass => get-size ;
|
|
|
|
|
|
|
|
: methods ( instance class -- )
|
2002-04-09 17:45:28 +00:00
|
|
|
object => class
|
1998-11-03 06:11:35 +00:00
|
|
|
metaclass => methods ;
|
|
|
|
|
|
|
|
\ Array indexing methods...
|
|
|
|
\ Usage examples:
|
|
|
|
\ 10 object-array --> index
|
|
|
|
\ obj --> next
|
|
|
|
\
|
|
|
|
: index ( n instance class -- instance[n] class )
|
2002-04-09 17:45:28 +00:00
|
|
|
locals| class inst |
|
|
|
|
inst class
|
1998-11-03 06:11:35 +00:00
|
|
|
object => class
|
2002-04-09 17:45:28 +00:00
|
|
|
metaclass => get-size * ( n*size )
|
|
|
|
inst + class ;
|
1998-11-03 06:11:35 +00:00
|
|
|
|
|
|
|
: next ( instance[n] class -- instance[n+1] class )
|
2002-04-09 17:45:28 +00:00
|
|
|
locals| class inst |
|
|
|
|
inst class
|
1998-11-03 06:11:35 +00:00
|
|
|
object => class
|
2002-04-09 17:45:28 +00:00
|
|
|
metaclass => get-size
|
|
|
|
inst +
|
|
|
|
class ;
|
1998-11-03 06:11:35 +00:00
|
|
|
|
|
|
|
: prev ( instance[n] class -- instance[n-1] class )
|
2002-04-09 17:45:28 +00:00
|
|
|
locals| class inst |
|
|
|
|
inst class
|
1998-11-03 06:11:35 +00:00
|
|
|
object => class
|
2002-04-09 17:45:28 +00:00
|
|
|
metaclass => get-size
|
|
|
|
inst swap -
|
|
|
|
class ;
|
1998-11-03 06:11:35 +00:00
|
|
|
|
2001-04-29 02:36:36 +00:00
|
|
|
: debug ( 2this -- ?? )
|
|
|
|
find-method-xt debug-xt ;
|
|
|
|
|
|
|
|
previous set-current
|
1998-11-03 06:11:35 +00:00
|
|
|
\ E N D O B J E C T
|
|
|
|
|
2002-04-09 17:45:28 +00:00
|
|
|
\ reset to default search order
|
2001-04-29 02:36:36 +00:00
|
|
|
only definitions
|
2002-04-09 17:45:28 +00:00
|
|
|
|
|
|
|
\ redefine oop in default search order to put OOP words in the search order and make them
|
|
|
|
\ the compiling wordlist...
|
|
|
|
|
|
|
|
: oo only also oop definitions ;
|
|
|
|
|
2001-04-29 02:36:36 +00:00
|
|
|
\ #endif
|