! See http://factorcode.org/license.txt for BSD license.
USING: alien alien.strings alien.c-types alien.accessors alien.structs
arrays words sequences math kernel namespaces fry libc cpu.architecture
-io.encodings.utf8 ;
+io.encodings.utf8 accessors ;
IN: alien.arrays
UNION: value-type array struct-type ;
M: array c-type-boxed-class drop object ;
-M: array heap-size unclip [ product ] [ heap-size ] bi* * ;
+: array-length ( seq -- n )
+ [ dup word? [ def>> call( -- object ) ] when ] [ * ] map-reduce ;
+
+M: array heap-size unclip [ array-length ] [ heap-size ] bi* * ;
M: array c-type-align first c-type-align ;
M: array c-type-boxer-quot
unclip
- [ product ]
+ [ array-length ]
[ [ require-c-type-arrays ] keep ] bi*
[ <c-type-direct-array> ] 2curry ;
CONSTANT: xyz 123
-[ { "blah" 123 } ] [ { "blah" xyz } expand-constants ] unit-test
+[ 492 ] [ { "int" xyz } heap-size ] unit-test
[ -1 ] [ -1 <char> *char ] unit-test
[ -1 ] [ -1 <short> *short ] unit-test
[ define-out ]
tri ;
-: expand-constants ( c-type -- c-type' )
- dup array? [
- unclip [
- [
- dup word? [
- def>> call( -- object )
- ] when
- ] map
- ] dip prefix
- ] when ;
-
: malloc-file-contents ( path -- alien len )
binary file-contents [ malloc-byte-array ] [ length ] bi ;
field-spec new
0 >>offset
swap >>name
- swap expand-constants >>type
+ swap >>type
3dup name>> swap reader-word >>reader
3dup name>> swap writer-word >>writer
2nip ;
[ define-field ] each ;
: define-union ( name members -- )
- [ expand-constants ] map
[ [ heap-size ] [ max ] map-reduce ] keep
compute-struct-align f struct-type (define-struct) ;
TUPLE: alien-callback-params < alien-node-params quot xt ;
-: pop-parameters ( -- seq )
- pop-literal nip [ expand-constants ] map ;
-
: param-prep-quot ( node -- quot )
parameters>> [ c-type c-type-unboxer-quot ] map spread>quot ;
: infer-alien-invoke ( -- )
alien-invoke-params new
! Compile-time parameters
- pop-parameters >>parameters
+ pop-literal nip >>parameters
pop-literal nip >>function
pop-literal nip >>library
pop-literal nip >>return
alien-indirect-params new
! Compile-time parameters
pop-literal nip >>abi
- pop-parameters >>parameters
+ pop-literal nip >>parameters
pop-literal nip >>return
! Quotation which coerces parameters to required types
dup param-prep-quot [ dip ] curry infer-quot-here
alien-callback-params new
pop-literal nip >>quot
pop-literal nip >>abi
- pop-parameters >>parameters
+ pop-literal nip >>parameters
pop-literal nip >>return
gensym >>xt
dup callback-bottom