From a2518377e342a62933366a457f907bb938d6720b Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Tue, 25 Aug 2009 20:43:48 -0500 Subject: [PATCH] support of structs using struct-arrays --- basis/alien/c-types/c-types.factor | 47 +++++++++------------- basis/alien/complex/functor/functor.factor | 21 +++------- basis/alien/structs/structs.factor | 12 +++++- 3 files changed, 37 insertions(+), 43 deletions(-) diff --git a/basis/alien/c-types/c-types.factor b/basis/alien/c-types/c-types.factor index 779a5e18de..4fc8dab9fe 100755 --- a/basis/alien/c-types/c-types.factor +++ b/basis/alien/c-types/c-types.factor @@ -21,19 +21,19 @@ TUPLE: abstract-c-type { getter callable } { setter callable } size -align ; - -TUPLE: c-type < abstract-c-type -boxer -unboxer -{ rep initial: int-rep } -stack-align? +align array-class array-constructor direct-array-class direct-array-constructor sequence-mixin-class ; +TUPLE: c-type < abstract-c-type +boxer +unboxer +{ rep initial: int-rep } +stack-align? ; + : ( -- type ) \ c-type new ; @@ -79,15 +79,12 @@ M: string c-type ( name -- type ) : ?require-word ( word/pair -- ) dup word? [ drop ] [ first require ] ?if ; -MIXIN: array-c-type -INSTANCE: c-type array-c-type - GENERIC: require-c-type-arrays ( c-type -- ) M: object require-c-type-arrays drop ; -M: array-c-type require-c-type-arrays +M: c-type require-c-type-arrays [ array-class>> ?require-word ] [ sequence-mixin-class>> ?require-word ] [ direct-array-class>> ?require-word ] tri ; @@ -100,33 +97,29 @@ M: array require-c-type-arrays ERROR: specialized-array-vocab-not-loaded vocab word ; -GENERIC: c-type-array-constructor ( c-type -- word ) foldable - -M: string c-type-array-constructor - c-type c-type-array-constructor ; -M: array c-type-array-constructor - first c-type c-type-array-constructor ; -M: array-c-type c-type-array-constructor +: c-type-array-constructor ( c-type -- word ) array-constructor>> dup word? - [ first2 specialized-array-vocab-not-loaded ] unless ; + [ first2 specialized-array-vocab-not-loaded ] unless ; foldable -GENERIC: c-type-direct-array-constructor ( c-type -- word ) foldable - -M: string c-type-direct-array-constructor - c-type c-type-direct-array-constructor ; -M: array c-type-direct-array-constructor - first c-type c-type-direct-array-constructor ; -M: array-c-type c-type-direct-array-constructor +: c-type-direct-array-constructor ( c-type -- word ) direct-array-constructor>> dup word? - [ first2 specialized-array-vocab-not-loaded ] unless ; + [ first2 specialized-array-vocab-not-loaded ] unless ; foldable GENERIC: ( len c-type -- array ) M: object c-type-array-constructor execute( len -- array ) ; inline +M: string + c-type ; inline +M: array + first c-type ; inline GENERIC: ( alien len c-type -- array ) M: object c-type-direct-array-constructor execute( alien len -- array ) ; inline +M: string + c-type ; inline +M: array + first c-type ; inline GENERIC: c-type-class ( name -- class ) diff --git a/basis/alien/complex/functor/functor.factor b/basis/alien/complex/functor/functor.factor index a5580318a9..7727546c00 100644 --- a/basis/alien/complex/functor/functor.factor +++ b/basis/alien/complex/functor/functor.factor @@ -1,18 +1,10 @@ ! Copyright (C) 2009 Slava Pestov. ! See http://factorcode.org/license.txt for BSD license. -USING: alien.structs alien.structs.fields alien.c-types -math math.functions sequences arrays kernel functors -vocabs.parser namespaces accessors quotations ; +USING: alien.structs alien.c-types math math.functions sequences +arrays kernel functors vocabs.parser namespaces accessors +quotations ; IN: alien.complex.functor -TUPLE: complex-c-type < struct-type - array-class - array-constructor - direct-array-class - direct-array-constructor - sequence-mixin-class ; -INSTANCE: complex-c-type array-c-type - FUNCTOR: define-complex-type ( N T -- ) T-real DEFINES ${T}-real @@ -31,10 +23,9 @@ WHERE : *T ( alien -- z ) [ T-real ] [ T-imaginary ] bi rect> ; inline -T N c-type-align [ 2 * ] [ ] bi -T current-vocab N "real" -T current-vocab N "imaginary" N c-type-align >>offset -2array complex-c-type (define-struct) +T current-vocab +{ { N "real" } { N "imaginary" } } +define-struct T c-type 1quotation >>unboxer-quot diff --git a/basis/alien/structs/structs.factor b/basis/alien/structs/structs.factor index 3d9cae1202..d8b2edf394 100755 --- a/basis/alien/structs/structs.factor +++ b/basis/alien/structs/structs.factor @@ -3,7 +3,7 @@ USING: accessors arrays assocs generic hashtables kernel kernel.private math namespaces parser sequences strings words libc fry alien.c-types alien.structs.fields cpu.architecture math.order -quotations byte-arrays ; +quotations byte-arrays struct-arrays ; IN: alien.structs TUPLE: struct-type < abstract-c-type fields return-in-registers? ; @@ -12,6 +12,16 @@ M: struct-type c-type ; M: struct-type c-type-stack-align? drop f ; +M: struct-type ( len c-type -- array ) + dup c-type-array-constructor + [ execute( len -- array ) ] + [ ] ?if ; inline + +M: struct-type ( alien len c-type -- array ) + dup c-type-direct-array-constructor + [ execute( alien len -- array ) ] + [ ] ?if ; inline + : if-value-struct ( ctype true false -- ) [ dup value-struct? ] 2dip '[ drop "void*" @ ] if ; inline