factor/library/compiler/alien.factor

85 lines
2.6 KiB
Factor
Raw Normal View History

2004-09-25 16:18:11 -04:00
! :folding=indent:collapseFolds=1:
! $Id$
!
! Copyright (C) 2004 Slava Pestov.
!
! Redistribution and use in source and binary forms, with or without
! modification, are permitted provided that the following conditions are met:
!
! 1. Redistributions of source code must retain the above copyright notice,
! this list of conditions and the following disclaimer.
!
! 2. Redistributions in binary form must reproduce the above copyright notice,
! this list of conditions and the following disclaimer in the documentation
! and/or other materials provided with the distribution.
!
! THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
! INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
! FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
! DEVELOPERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
! PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
! OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
! WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
! OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
! ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
IN: alien
USE: compiler
USE: errors
USE: kernel
2004-09-25 16:18:11 -04:00
USE: lists
USE: math
USE: namespaces
USE: parser
USE: words
: BEGIN-ENUM:
2004-09-28 00:24:36 -04:00
#! C-style enumerations. Their use is not encouraged unless
2004-09-25 16:18:11 -04:00
#! it is for C library interfaces. Used like this:
#!
#! BEGIN-ENUM 0
#! ENUM: x
#! ENUM: y
#! ENUM: z
#! END-ENUM
#!
#! This is the same as : x 0 ; : y 1 ; : z 2 ;.
scan str>number ; parsing
: ENUM:
dup CREATE swap unit define-compound succ ; parsing
: END-ENUM
drop ; parsing
: alien-call ( ... returns library function parameters -- ... )
#! Call a C library function.
#! 'returns' is a type spec, and 'parameters' is a list of
#! type specs. 'library' is an entry in the "libraries"
#! namespace.
"alien-call cannot be interpreted." throw ;
: library ( name -- handle )
2004-10-09 21:43:14 -04:00
"libraries" get [
dup get dup dll? [
nip
] [
dlopen tuck put
] ifte
] bind ;
2004-09-25 16:18:11 -04:00
: alien-function ( function library -- )
2004-10-27 21:21:31 -04:00
[ library dlsym ] [ dlsym-self ] ifte* ;
2004-09-25 16:18:11 -04:00
: compile-alien-call
pop-literal reverse PARAMETERS >r
2004-09-28 00:24:36 -04:00
pop-literal pop-literal alien-function CALL JUMP-FIXUP
2004-09-25 16:18:11 -04:00
r> CLEANUP
pop-literal RETURNS ;
global [ <namespace> "libraries" set ] bind
2004-10-16 21:55:13 -04:00
\ alien-call [ compile-alien-call ] "compiling" set-word-property