2010-05-09 13:21:12 -04:00
|
|
|
! Copyright (C) 2009 Anton Gorenko.
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2010-07-25 12:18:32 -04:00
|
|
|
USING: accessors alien alien.c-types alien.parser arrays assocs
|
2010-07-17 07:17:03 -04:00
|
|
|
classes.parser classes.struct combinators
|
|
|
|
combinators.short-circuit definitions effects fry
|
|
|
|
gobject-introspection.common gobject-introspection.types kernel
|
|
|
|
math.parser namespaces parser quotations sequences
|
|
|
|
sequences.generalizations words words.constant ;
|
|
|
|
IN: gobject-introspection.ffi
|
2010-05-09 13:21:12 -04:00
|
|
|
|
|
|
|
: string>c-type ( str -- c-type )
|
2010-07-25 12:18:32 -04:00
|
|
|
dup CHAR: * swap index [ cut ] [ "" ] if*
|
|
|
|
[ replaced-c-types get-global ?at drop ] dip
|
|
|
|
append parse-c-type ;
|
2010-05-09 13:21:12 -04:00
|
|
|
|
|
|
|
: define-each ( nodes quot -- )
|
|
|
|
'[ dup @ >>ffi drop ] each ; inline
|
|
|
|
|
2010-05-30 12:45:37 -04:00
|
|
|
: function-ffi-invoker ( func -- quot )
|
2010-05-09 13:21:12 -04:00
|
|
|
{
|
|
|
|
[ return>> c-type>> string>c-type ]
|
2010-05-26 14:17:14 -04:00
|
|
|
[ drop current-lib get-global ]
|
2010-05-09 13:21:12 -04:00
|
|
|
[ identifier>> ]
|
|
|
|
[ parameters>> [ c-type>> string>c-type ] map ]
|
|
|
|
[ varargs?>> [ void* suffix ] when ]
|
2010-05-30 12:45:37 -04:00
|
|
|
} cleave function-quot ;
|
2010-05-09 13:21:12 -04:00
|
|
|
|
2010-05-30 12:45:37 -04:00
|
|
|
: function-ffi-effect ( func -- effect )
|
2010-05-09 13:21:12 -04:00
|
|
|
[ parameters>> [ name>> ] map ]
|
|
|
|
[ varargs?>> [ "varargs" suffix ] when ]
|
|
|
|
[ return>> type>> none-type? { } { "result" } ? ] tri
|
|
|
|
<effect> ;
|
|
|
|
|
|
|
|
: define-ffi-function ( func -- word )
|
|
|
|
[ identifier>> create-in dup ]
|
2010-05-30 12:45:37 -04:00
|
|
|
[ function-ffi-invoker ] [ function-ffi-effect ] tri
|
|
|
|
define-declared ;
|
2010-05-09 13:21:12 -04:00
|
|
|
|
|
|
|
: define-ffi-functions ( functions -- )
|
|
|
|
[ define-ffi-function ] define-each ;
|
|
|
|
|
2010-05-30 12:45:37 -04:00
|
|
|
: callback-ffi-invoker ( callback -- quot )
|
|
|
|
[ return>> c-type>> string>c-type ]
|
|
|
|
[ parameters>> [ c-type>> string>c-type ] map ] bi
|
|
|
|
cdecl callback-quot ;
|
2010-05-09 13:21:12 -04:00
|
|
|
|
2010-05-30 12:45:37 -04:00
|
|
|
: callback-ffi-effect ( callback -- effect )
|
2010-05-09 13:21:12 -04:00
|
|
|
[ parameters>> [ name>> ] map ]
|
|
|
|
[ return>> type>> none-type? { } { "result" } ? ] bi
|
2010-05-11 13:31:35 -04:00
|
|
|
<effect> ;
|
2010-05-09 13:21:12 -04:00
|
|
|
|
2010-05-30 12:45:37 -04:00
|
|
|
: define-ffi-callback ( callback -- word )
|
|
|
|
[ c-type>> create-in [ void* swap typedef ] keep dup ] keep
|
|
|
|
[ callback-ffi-effect "callback-effect" set-word-prop ]
|
2010-05-26 14:17:14 -04:00
|
|
|
[ drop current-lib get-global "callback-library" set-word-prop ]
|
2010-05-30 12:45:37 -04:00
|
|
|
[ callback-ffi-invoker (( quot -- alien )) define-inline ] 2tri ;
|
|
|
|
|
|
|
|
: fix-signal-param-c-type ( param -- )
|
|
|
|
dup [ c-type>> ] [ type>> ] bi
|
|
|
|
{
|
|
|
|
[ none-type? ]
|
|
|
|
[ simple-type? ]
|
|
|
|
[ enum-type? ]
|
|
|
|
[ bitfield-type? ]
|
|
|
|
} 1|| [ dup "*" tail? [ CHAR: * suffix ] unless ] unless
|
|
|
|
>>c-type drop ;
|
2010-05-09 13:21:12 -04:00
|
|
|
|
2010-05-30 12:45:37 -04:00
|
|
|
: define-ffi-signal ( signal -- word )
|
|
|
|
[ return>> fix-signal-param-c-type ]
|
|
|
|
[ parameters>> [ fix-signal-param-c-type ] each ]
|
|
|
|
[ define-ffi-callback ] tri ;
|
|
|
|
|
|
|
|
: define-ffi-signals ( signals -- )
|
|
|
|
[ define-ffi-signal ] define-each ;
|
2010-05-09 13:21:12 -04:00
|
|
|
|
|
|
|
: const-value ( const -- value )
|
|
|
|
[ value>> ] [ type>> name>> ] bi {
|
|
|
|
{ "int" [ string>number ] }
|
|
|
|
{ "double" [ string>number ] }
|
|
|
|
{ "utf8" [ ] }
|
|
|
|
} case ;
|
|
|
|
|
|
|
|
: define-ffi-enum ( enum -- word )
|
|
|
|
[
|
|
|
|
members>> [
|
|
|
|
[ c-identifier>> create-in ]
|
2010-06-18 13:33:06 -04:00
|
|
|
[ value>> ] bi define-constant
|
|
|
|
] each
|
|
|
|
] [ c-type>> (CREATE-C-TYPE) [ int swap typedef ] keep ] bi ;
|
2010-05-09 13:21:12 -04:00
|
|
|
|
|
|
|
: define-ffi-enums ( enums -- )
|
|
|
|
[ define-ffi-enum ] define-each ;
|
|
|
|
|
|
|
|
: define-ffi-bitfields ( bitfields -- )
|
|
|
|
[ define-ffi-enum ] define-each ;
|
|
|
|
|
2010-05-11 13:31:35 -04:00
|
|
|
: fields>struct-slots ( fields -- slots )
|
|
|
|
[
|
2010-05-23 06:03:45 -04:00
|
|
|
[ name>> ]
|
|
|
|
[
|
|
|
|
[ c-type>> string>c-type ] [ array-info>> ] bi
|
|
|
|
[ fixed-size>> [ 2array ] when* ] when*
|
|
|
|
]
|
2010-05-11 13:31:35 -04:00
|
|
|
[ drop { } ] tri <struct-slot-spec>
|
|
|
|
] map ;
|
|
|
|
|
|
|
|
: define-ffi-record-defer ( record -- word )
|
|
|
|
c-type>> create-in void* swap [ typedef ] keep ;
|
|
|
|
|
|
|
|
: define-ffi-records-defer ( records -- )
|
|
|
|
[ define-ffi-record-defer ] define-each ;
|
|
|
|
|
2010-05-09 13:21:12 -04:00
|
|
|
: define-ffi-record ( record -- word )
|
2010-05-11 13:31:35 -04:00
|
|
|
dup ffi>> forget
|
|
|
|
dup {
|
|
|
|
[ fields>> empty? not ]
|
|
|
|
[ c-type>> implement-structs get-global member? ]
|
|
|
|
} 1&&
|
|
|
|
[
|
|
|
|
[ c-type>> create-class-in dup ]
|
|
|
|
[ fields>> fields>struct-slots ] bi define-struct-class
|
|
|
|
] [
|
|
|
|
[ disguised?>> void* void ? ]
|
|
|
|
[ c-type>> create-in ] bi [ typedef ] keep
|
|
|
|
] if ;
|
2010-05-09 13:21:12 -04:00
|
|
|
|
|
|
|
: define-ffi-records ( records -- )
|
|
|
|
[ define-ffi-record ] define-each ;
|
|
|
|
|
|
|
|
: define-ffi-record-content ( record -- )
|
|
|
|
{
|
|
|
|
[ constructors>> define-ffi-functions ]
|
|
|
|
[ functions>> define-ffi-functions ]
|
|
|
|
[ methods>> define-ffi-functions ]
|
|
|
|
} cleave ;
|
|
|
|
|
|
|
|
: define-ffi-records-content ( records -- )
|
|
|
|
[ define-ffi-record-content ] each ;
|
|
|
|
|
|
|
|
: define-ffi-union ( union -- word )
|
|
|
|
c-type>> create-in [ void* swap typedef ] keep ;
|
|
|
|
|
|
|
|
: define-ffi-unions ( unions -- )
|
|
|
|
[ define-ffi-union ] define-each ;
|
|
|
|
|
|
|
|
: define-ffi-callbacks ( callbacks -- )
|
|
|
|
[ define-ffi-callback ] define-each ;
|
|
|
|
|
|
|
|
: define-ffi-interface ( interface -- word )
|
|
|
|
c-type>> create-in [ void swap typedef ] keep ;
|
|
|
|
|
|
|
|
: define-ffi-interfaces ( interfaces -- )
|
|
|
|
[ define-ffi-interface ] define-each ;
|
|
|
|
|
|
|
|
: define-ffi-interface-content ( interface -- )
|
|
|
|
{
|
|
|
|
[ methods>> define-ffi-functions ]
|
|
|
|
} cleave ;
|
|
|
|
|
|
|
|
: define-ffi-interfaces-content ( interfaces -- )
|
|
|
|
[ define-ffi-interface-content ] each ;
|
|
|
|
|
|
|
|
: get-type-invoker ( name -- quot )
|
2010-09-12 13:08:52 -04:00
|
|
|
! hack
|
|
|
|
[ "GType" "glib.ffi" lookup current-lib get-global ] dip
|
2010-05-09 13:21:12 -04:00
|
|
|
{ } \ alien-invoke 5 narray >quotation ;
|
|
|
|
|
|
|
|
: define-ffi-class ( class -- word )
|
|
|
|
c-type>> create-in [ void swap typedef ] keep ;
|
|
|
|
|
|
|
|
: define-ffi-classes ( class -- )
|
|
|
|
[ define-ffi-class ] define-each ;
|
|
|
|
|
|
|
|
: define-ffi-class-content ( class -- )
|
|
|
|
{
|
|
|
|
[ constructors>> define-ffi-functions ]
|
|
|
|
[ functions>> define-ffi-functions ]
|
|
|
|
[ methods>> define-ffi-functions ]
|
2010-05-31 02:42:05 -04:00
|
|
|
[ signals>> define-ffi-signals ]
|
2010-05-09 13:21:12 -04:00
|
|
|
} cleave ;
|
|
|
|
|
|
|
|
: define-ffi-classes-content ( class -- )
|
|
|
|
[ define-ffi-class-content ] each ;
|
|
|
|
|
|
|
|
: define-get-type ( node -- word )
|
|
|
|
get-type>> dup { "intern" f } member? [ drop f ]
|
|
|
|
[
|
|
|
|
[ create-in dup ] [ get-type-invoker ] bi
|
|
|
|
{ } { "type" } <effect> define-declared
|
|
|
|
] if ;
|
|
|
|
|
|
|
|
: define-get-types ( namespace -- )
|
|
|
|
{
|
|
|
|
[ enums>> [ define-get-type drop ] each ]
|
|
|
|
[ bitfields>> [ define-get-type drop ] each ]
|
|
|
|
[ records>> [ define-get-type drop ] each ]
|
|
|
|
[ unions>> [ define-get-type drop ] each ]
|
|
|
|
[ interfaces>> [ define-get-type drop ] each ]
|
|
|
|
[ classes>> [ define-get-type drop ] each ]
|
|
|
|
} cleave ;
|
|
|
|
|
|
|
|
: define-ffi-const ( const -- word )
|
2010-05-23 06:03:45 -04:00
|
|
|
[ c-identifier>> create-in dup ] [ const-value ] bi
|
|
|
|
define-constant ;
|
2010-05-09 13:21:12 -04:00
|
|
|
|
|
|
|
: define-ffi-consts ( consts -- )
|
|
|
|
[ define-ffi-const ] define-each ;
|
|
|
|
|
|
|
|
: define-ffi-alias ( alias -- )
|
|
|
|
drop ;
|
|
|
|
|
|
|
|
: define-ffi-aliases ( aliases -- )
|
|
|
|
[ define-ffi-alias ] each ;
|
|
|
|
|
|
|
|
: define-ffi-namespace ( namespace -- )
|
|
|
|
{
|
|
|
|
[ aliases>> define-ffi-aliases ]
|
|
|
|
[ consts>> define-ffi-consts ]
|
|
|
|
[ enums>> define-ffi-enums ]
|
|
|
|
[ bitfields>> define-ffi-bitfields ]
|
2010-05-11 13:31:35 -04:00
|
|
|
|
|
|
|
[ records>> define-ffi-records-defer ]
|
|
|
|
|
2010-05-09 13:21:12 -04:00
|
|
|
[ unions>> define-ffi-unions ]
|
|
|
|
[ interfaces>> define-ffi-interfaces ]
|
|
|
|
[ classes>> define-ffi-classes ]
|
|
|
|
[ callbacks>> define-ffi-callbacks ]
|
2010-05-11 13:31:35 -04:00
|
|
|
[ records>> define-ffi-records ]
|
|
|
|
|
2010-05-09 13:21:12 -04:00
|
|
|
[ records>> define-ffi-records-content ]
|
|
|
|
[ classes>> define-ffi-classes-content ]
|
|
|
|
[ interfaces>> define-ffi-interfaces-content ]
|
|
|
|
[ functions>> define-ffi-functions ]
|
2010-09-12 13:08:52 -04:00
|
|
|
|
|
|
|
[ define-get-types ]
|
2010-05-09 13:21:12 -04:00
|
|
|
} cleave ;
|
|
|
|
|
|
|
|
: define-ffi-repository ( repository -- )
|
2010-05-26 14:17:14 -04:00
|
|
|
namespace>> define-ffi-namespace ;
|
2010-05-09 13:21:12 -04:00
|
|
|
|