1 ! Copyright (C) 2004, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors assocs kernel math namespaces sequences system
4 kernel.private byte-arrays byte-vectors arrays init
5 continuations.private ;
8 PREDICATE: pinned-alien < alien underlying>> not ;
10 UNION: pinned-c-ptr pinned-alien POSTPONE: f ;
12 GENERIC: element-size ( seq -- n ) flushable
14 M: byte-array element-size drop 1 ; inline
16 M: byte-vector element-size drop 1 ; inline
18 M: slice element-size seq>> element-size ; inline
20 M: f element-size drop 1 ; inline
22 GENERIC: byte-length ( obj -- n ) flushable
24 M: object byte-length [ length ] [ element-size ] bi * ; inline
26 GENERIC: >c-ptr ( obj -- c-ptr ) flushable
28 M: c-ptr >c-ptr ; inline
31 [ [ from>> ] [ element-size ] bi * ] [ seq>> >c-ptr ] bi
32 <displaced-alien> ; inline
36 M: object >c-ptr underlying>> ; inline
38 : binary-object ( obj -- c-ptr n )
39 [ >c-ptr ] [ byte-length ] bi ; inline
41 GENERIC: expired? ( c-ptr -- ? ) flushable
43 M: alien expired? expired>> ;
45 M: f expired? drop t ;
47 : <alien> ( address -- alien )
48 f <displaced-alien> { pinned-c-ptr } declare ; inline
50 : <bad-alien> ( -- alien )
51 -1 <alien> t >>expired ; inline
55 2dup [ expired? ] either? [
58 [ alien-address ] bi@ =
64 M: pinned-alien hashcode*
65 nip dup expired>> [ drop 1234 ] [ alien-address ] if ;
67 SINGLETONS: stdcall thiscall fastcall cdecl mingw ;
69 UNION: abi stdcall thiscall fastcall cdecl mingw ;
71 : callee-cleanup? ( abi -- ? )
72 { stdcall fastcall thiscall } member? ;
74 ERROR: alien-callback-error ;
76 : alien-callback ( return parameters abi quot -- alien )
77 alien-callback-error ;
79 ERROR: alien-indirect-error ;
81 : alien-indirect ( args... funcptr return parameters abi -- return... )
82 alien-indirect-error ;
84 ERROR: alien-invoke-error library symbol ;
86 : alien-invoke ( args... return library function parameters -- return... )
87 2over alien-invoke-error ;
89 ERROR: alien-assembly-error code ;
91 : alien-assembly ( args... return parameters abi quot -- return... )
92 dup alien-assembly-error ;
96 ! Callbacks are registered in a global hashtable. Note that they
97 ! are also pinned in a special callback area, so clearing this
98 ! hashtable will not reclaim callbacks. It should only be
102 [ H{ } clone callbacks set-global ] "alien" add-startup-hook
104 ! Every callback invocation has a unique identifier in the VM.
105 ! We make sure that the current callback is the right one before
106 ! returning from it, to avoid a bad interaction between threads
107 ! and callbacks. See basis/compiler/tests/alien.factor for a
109 : wait-to-return ( yield-quot: ( -- ) callback-id -- )
110 dup current-callback eq?
111 [ 2drop ] [ over call wait-to-return ] if ; inline recursive
113 ! Used by compiler.codegen to wrap callback bodies
114 : do-callback ( callback-quot yield-quot: ( -- ) -- )
118 [ 2drop call ] [ wait-to-return drop ] 3bi ; inline
120 ! A utility for defining global variables that are recompiled in
122 TUPLE: expiry-check object alien ;
124 : recompute-value? ( check -- ? )
125 dup [ alien>> expired? ] [ drop t ] if ;
129 : initialize-alien ( symbol quot -- )
130 swap dup get-global dup recompute-value?
131 [ drop [ call dup 31337 <alien> expiry-check boa ] dip set-global ]
132 [ 2nip object>> ] if ; inline