[cffi-devel] using defcenum keywords in function call

Stelian Ionescu sionescu at cddr.org
Sat Sep 26 03:19:15 UTC 2009


On Sat, 2009-09-26 at 04:09 +0200, Johan Ur Riise wrote:
> I have some enums defined, a defcfun and I use the keywords of the enum when I
> call the function.
> 
> The documentation says:	
> Keywords will be automatically converted to values and vice-versa when being passed as
> arguments to or returned from foreign functions, respectively.
> 
> But this does not seem to be the case, I get a type error. 
> 
> The following should be a minimalistic runnable example. What am I doing wrong?
> 
> CL-USER> (require 'cffi)
> ; loading system definition from C:/prg/libcl/babel/babel.asd into #<Package "ASDF0">
> ; registering #<SYSTEM BABEL #x14DE5106> as BABEL
> ; loading system definition from C:/prg/libcl/alexandria/alexandria.asd into #<Package "ASDF0">
> ; registering #<SYSTEM :ALEXANDRIA #x14E038B6> as ALEXANDRIA
> ; loading system definition from C:/prg/libcl/trivial-features/trivial-features.asd into #<Package "ASDF0">
> ; registering #<SYSTEM TRIVIAL-FEATURES #x14E5DAE6> as TRIVIAL-FEATURES
> CFFI
> NIL
> CL-USER> (in-package :cffi)
> #<Package "CFFI">
> CFFI> (defctype SOCKET :uint)
> SOCKET
> CFFI> (defcenum af
>   :af-unspec
>   :af-unix
>   :af-inet)
> AF
> CFFI> (defcenum sock-type
>   (:sock-stream 1)
>   :sock-dgram
>   :sock-raw)
> SOCK-TYPE
> CFFI> (defcenum pf
>   :pf-unspec
>   :pf-unix
>   :pf-inet)
> PF
> CFFI> (defcfun ("socket" win-socket) socket
>   "Creates a socket with the overlapped mode as default"
>   (af :int32)
>   (type :int)
>   (protocol :int))
> WIN-SOCKET

DEFCENUM doesn't define constants(as in C), but a type which gets
translated to integers if you specify it as argument type. In other
words, this is correct:

(defcfun ("socket" win-socket) socket
  "Creates a socket with the overlapped mode as default"
  (af       af)
  (type     sock-type)
  (protocol pf))

> CFFI> (defun make-socket ()
>   (win-socket :af-inet :sock-stream :pf-inet))
> MAKE-SOCKET
> CFFI> (make-socket)
> ; Evaluation aborted.
> CFFI> ;->value :AF-INET is not of the expected type (SIGNED-BYTE 32).
>    ;[Condition of type TYPE-ERROR]

This is correct because the type you've specified is :int while you
passed a keyword.

-- 
Stelian Ionescu a.k.a. fe[nl]ix
Quidquid latine dictum sit, altum videtur.
http://common-lisp.net/project/iolib
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part
URL: <https://mailman.common-lisp.net/pipermail/cffi-devel/attachments/20090926/9cd28a64/attachment.sig>


More information about the cffi-devel mailing list