From: Ken Tilton
Subject: Specials across callbacks from C called by Lisp
Date: 
Message-ID: <_xP6g.48$Ey5.42@fe12.lga>
Well, i thought the easy way to connect a widget to its C pointer was:

    (let ((*togl* (make-instance 'togl)))
	(tk-interp "togl <toggle params as a string tk will grok>"))

tk-interp is a C function reached via CFFI. The interpreter invokes the 
togl command, implemented as more C that eventually calls a registered 
togl_create callback.

then in the callback:

(defun togl_create (togl-pointer)
     (setf (togl-ptr *togl*) togl-pointer)
     ...etc ..))

Works on AllegroCL, does not work on Lispworks, which maintains *togl* 
is unbound.

Which behavior is correct, do you think?

kenny


-- 
Cells: http://common-lisp.net/project/cells/

"Have you ever been in a relationship?"
    Attorney for Mary Winkler, confessed killer of her
    minister husband, when asked if the couple had
    marital problems.

From: James Bielman
Subject: Re: Specials across callbacks from C called by Lisp
Date: 
Message-ID: <87vesk878b.fsf@jamesjb.com>
Ken Tilton <·········@gmail.com> writes:

> Well, i thought the easy way to connect a widget to its C pointer was:
>
>     (let ((*togl* (make-instance 'togl)))
> 	(tk-interp "togl <toggle params as a string tk will grok>"))
>
> tk-interp is a C function reached via CFFI. The interpreter invokes
> the togl command, implemented as more C that eventually calls a
> registered togl_create callback.
>
> then in the callback:
>
> (defun togl_create (togl-pointer)
>      (setf (togl-ptr *togl*) togl-pointer)
>      ...etc ..))
>
> Works on AllegroCL, does not work on Lispworks, which maintains *togl*
> is unbound.
>
> Which behavior is correct, do you think?

I'd think this should work, unless the callback ends up running in
some other thread or something (I do something similar in another
project).  It's probably worth adding some tests to CFFI for this.

James
From: Ken Tilton
Subject: Re: Specials across callbacks from C called by Lisp
Date: 
Message-ID: <hAQ6g.58$Ey5.5@fe12.lga>
James Bielman wrote:
> Ken Tilton <·········@gmail.com> writes:
> 
> 
>>Well, i thought the easy way to connect a widget to its C pointer was:
>>
>>    (let ((*togl* (make-instance 'togl)))
>>	(tk-interp "togl <toggle params as a string tk will grok>"))
>>
>>tk-interp is a C function reached via CFFI. The interpreter invokes
>>the togl command, implemented as more C that eventually calls a
>>registered togl_create callback.
>>
>>then in the callback:
>>
>>(defun togl_create (togl-pointer)
>>     (setf (togl-ptr *togl*) togl-pointer)
>>     ...etc ..))
>>
>>Works on AllegroCL, does not work on Lispworks, which maintains *togl*
>>is unbound.
>>
>>Which behavior is correct, do you think?
> 
> 
> I'd think this should work, unless the callback ends up running in
> some other thread or something (I do something similar in another
> project).

Yeah, if one is going to support callbacks, well, dynamic scope is 
dynamic scope, crossing language barriers be damned. :) (Yes, same thread.)

>  It's probably worth adding some tests to CFFI for this.

I was going to respond "Hey, it is not your fault.", but then I realized 
that one of CFFIs contributions to CL was getting implementations to 
patch as necessary to make things work. I was not privy to those 
exchanges, but I understand there has been some nice give and take with 
implementors?

Anyway, i think the above says it all in re a test case. If you create a 
test case and Lispworks does /not/ fail (a) boy is my face red and (b) I 
will restore the failing code (all open) and you can see if it indeed is 
an LW problem.

kenneth

-- 
Cells: http://common-lisp.net/project/cells/

"Have you ever been in a relationship?"
    Attorney for Mary Winkler, confessed killer of her
    minister husband, when asked if the couple had
    marital problems.
From: James Bielman
Subject: Re: Specials across callbacks from C called by Lisp
Date: 
Message-ID: <87ejz87ynf.fsf@jamesjb.com>
Ken Tilton <·········@gmail.com> writes:

> Anyway, i think the above says it all in re a test case. If you
> create a test case and Lispworks does /not/ fail (a) boy is my face
> red and (b) I will restore the failing code (all open) and you can
> see if it indeed is an LW problem.

Well, I wrote the following test case:

;;; Accessing bound special variables from a callback.
#-cffi-features:no-foreign-funcall
(progn
  (defvar *forty-two*)

  (defcallback special-cb :int () *forty-two*)

  (deftest callbacks.special
      (let ((*forty-two* 42))
        (foreign-funcall (callback special-cb) :int))
    42))

and it does indeed pass for me on Lispworks/Enterprise/Linux 4.3.7,
but I'm just calling the callback from Lisp without really going
through any complicated C code.

Can you try running the same code snippet (minus the DEFTEST forms,
etc.) in your Lispworks?

James
From: Ken Tilton
Subject: Re: Specials across callbacks from C called by Lisp
Date: 
Message-ID: <tgT6g.86$Ey5.60@fe12.lga>
James Bielman wrote:
> Ken Tilton <·········@gmail.com> writes:
> 
> 
>>Anyway, i think the above says it all in re a test case. If you
>>create a test case and Lispworks does /not/ fail (a) boy is my face
>>red and (b) I will restore the failing code (all open) and you can
>>see if it indeed is an LW problem.
> 
> 
> Well, I wrote the following test case:
> 
> ;;; Accessing bound special variables from a callback.
> #-cffi-features:no-foreign-funcall
> (progn
>   (defvar *forty-two*)
> 
>   (defcallback special-cb :int () *forty-two*)
> 
>   (deftest callbacks.special
>       (let ((*forty-two* 42))
>         (foreign-funcall (callback special-cb) :int))
>     42))
> 
> and it does indeed pass for me on Lispworks/Enterprise/Linux 4.3.7,
> but I'm just calling the callback from Lisp without really going
> through any complicated C code.
> 
> Can you try running the same code snippet (minus the DEFTEST forms,
> etc.) in your Lispworks?

That works. lispworks-personal-4460.exe, btw.

kt

-- 
Cells: http://common-lisp.net/project/cells/

"Have you ever been in a relationship?"
    Attorney for Mary Winkler, confessed killer of her
    minister husband, when asked if the couple had
    marital problems.