diff --git a/extra/classes/struct/struct-docs.factor b/extra/classes/struct/struct-docs.factor index 18c012b61c..90247a0495 100644 --- a/extra/classes/struct/struct-docs.factor +++ b/extra/classes/struct/struct-docs.factor @@ -7,16 +7,16 @@ HELP: { $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: { $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." } ; -{ malloc-struct memory>struct } related-words +{ malloc-struct memory>struct } related-words HELP: STRUCT: { $syntax "STRUCT: class { slot type } { slot type } ... ;" } @@ -75,7 +75,9 @@ HELP: struct-class 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 } +{ $subsection } { $subsection malloc-struct } { $subsection memory>struct } "Structs have literal syntax like tuples:" diff --git a/extra/classes/struct/struct-tests.factor b/extra/classes/struct/struct-tests.factor index 3c64b30b25..0d4f97a70a 100644 --- a/extra/classes/struct/struct-tests.factor +++ b/extra/classes/struct/struct-tests.factor @@ -14,11 +14,11 @@ STRUCT: bar [ 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 y>> ] unit-test +[ 123 ] [ bar foo>> y>> ] unit-test [ 1 2 3 t ] [ - 1 2 3 t foo boa bar boa + 1 2 3 t foo bar { [ w>> ] [ foo>> x>> ] @@ -30,7 +30,7 @@ STRUCT: bar [ 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 ] undo ] unit-test UNION-STRUCT: float-and-bits { f single-float } diff --git a/extra/classes/struct/struct.factor b/extra/classes/struct/struct.factor index 2a7679bb0d..90224c96d5 100644 --- a/extra/classes/struct/struct.factor +++ b/extra/classes/struct/struct.factor @@ -27,16 +27,16 @@ M: struct >c-ptr : malloc-struct ( class -- struct ) [ heap-size malloc ] keep memory>struct ; inline -: ( class -- struct ) +: (struct) ( class -- struct ) [ heap-size ] keep memory>struct ; inline -M: struct-class new +: ( class -- struct ) dup "prototype" word-prop - [ >c-ptr clone swap memory>struct ] [ ] if* ; inline + [ >c-ptr clone swap memory>struct ] [ (struct) ] if* ; inline MACRO: ( class -- quot: ( ... -- struct ) ) [ - [ \ [ ] 2sequence ] + [ \ (struct) [ ] 2sequence ] [ "struct-slots" word-prop [ length \ ndip ] @@ -44,15 +44,12 @@ MACRO: ( class -- quot: ( ... -- struct ) ) ] bi ] [ ] output>sequence ; -M: struct-class 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 - [ swap ] [ "struct-slots" word-prop ] bi + [ (struct) swap ] [ "struct-slots" word-prop ] bi [ name>> setter-word execute( struct value -- struct ) ] 2each ; ! Struct slot accessors