{ $values
{ "class" class }
}
-{ $description "This macro implements " { $link boa } " for " { $link struct } " classes. User code does not need to call this word directly and should use " { $snippet "boa" } " instead." } ;
+{ $description "This macro implements " { $link boa } " for " { $link struct } " classes. A struct of the given class is constructed, and its slots are initialized using values off the top of the datastack." } ;
HELP: <struct>
{ $values
{ "class" class }
{ "struct" struct }
}
-{ $description "Allocates garbage-collected heap memory for a new " { $link struct } " of the specified " { $snippet "class" } ". The new struct's slots are left uninitialized; to allocate a struct with the slots initialized, call " { $link new } " or " { $link boa } " instead." } ;
+{ $description "Allocates garbage-collected heap memory for a new " { $link struct } " of the specified " { $snippet "class" } ". The new struct's slots are initialized with the initial values specified in the struct definition." } ;
-{ <struct> malloc-struct memory>struct } related-words
+{ <struct> <struct-boa> malloc-struct memory>struct } related-words
HELP: STRUCT:
{ $syntax "STRUCT: class { slot type } { slot type } ... ;" }
ARTICLE: "classes.struct" "Struct classes"
{ $link struct } " classes are similar to " { $link tuple } "s, but their slots exhibit value semantics, and they are backed by a contiguous structured block of memory. Structs can be used for structured access to C memory or Factor byte arrays and for passing struct values in and out of the FFI. Struct types are defined using a syntax similar to tuple syntax:"
{ $subsection POSTPONE: STRUCT: }
-"Structs can be allocated with " { $link new } " and " { $link boa } " like tuples. Additional words are provided for building structs from C memory and from existing buffers:"
+"Structs can be allocated with " { $link new } "- and " { $link boa } "-like constructor words. Additional words are provided for building structs from C memory and from existing buffers:"
+{ $subsection <struct> }
+{ $subsection <struct-boa> }
{ $subsection malloc-struct }
{ $subsection memory>struct }
"Structs have literal syntax like tuples:"
[ 12 ] [ foo heap-size ] unit-test
[ 16 ] [ bar heap-size ] unit-test
-[ 123 ] [ foo new y>> ] unit-test
-[ 123 ] [ bar new foo>> y>> ] unit-test
+[ 123 ] [ foo <struct> y>> ] unit-test
+[ 123 ] [ bar <struct> foo>> y>> ] unit-test
[ 1 2 3 t ] [
- 1 2 3 t foo boa bar boa
+ 1 2 3 t foo <struct-boa> bar <struct-boa>
{
[ w>> ]
[ foo>> x>> ]
[ 7654 ] [ S{ foo f 98 7654 f } y>> ] unit-test
[ 7654 ] [ S{ foo { y 7654 } } y>> ] unit-test
-[ 98 7654 t ] [ S{ foo f 98 7654 t } [ foo boa ] undo ] unit-test
+[ 98 7654 t ] [ S{ foo f 98 7654 t } [ foo <struct-boa> ] undo ] unit-test
UNION-STRUCT: float-and-bits
{ f single-float }
: malloc-struct ( class -- struct )
[ heap-size malloc ] keep memory>struct ; inline
-: <struct> ( class -- struct )
+: (struct) ( class -- struct )
[ heap-size <byte-array> ] keep memory>struct ; inline
-M: struct-class new
+: <struct> ( class -- struct )
dup "prototype" word-prop
- [ >c-ptr clone swap memory>struct ] [ <struct> ] if* ; inline
+ [ >c-ptr clone swap memory>struct ] [ (struct) ] if* ; inline
MACRO: <struct-boa> ( class -- quot: ( ... -- struct ) )
[
- [ \ <struct> [ ] 2sequence ]
+ [ <wrapper> \ (struct) [ ] 2sequence ]
[
"struct-slots" word-prop
[ length \ ndip ]
] bi
] [ ] output>sequence ;
-M: struct-class boa
- <struct-boa> ; inline
-
: pad-struct-slots ( slots class -- slots' class )
[ class-slots [ initial>> ] map over length tail append ] keep ;
M: struct-class boa>object
swap pad-struct-slots
- [ <struct> swap ] [ "struct-slots" word-prop ] bi
+ [ (struct) swap ] [ "struct-slots" word-prop ] bi
[ name>> setter-word execute( struct value -- struct ) ] 2each ;
! Struct slot accessors