[Openmcl-devel] eval-in-package

Dan Weinreb dlw at itasoftware.com
Mon Jun 1 14:12:20 PDT 2009


It's because the the order in which you (a) bind *package* and
(b) call read-from-string.

Taoufik Dachraoui wrote:
> I would like to have more details, I do not understand why there is a 
> difference.
>
> If you look at (eval (read-from-string str)) form, in both functions 
> read-from-string
> is supposed to read from a string and I think that at that point in 
> both function the
> string is the same "(setq a 1)"
>
> so why ther eis a difference? thank you giving me more details.
>
> I am planning to use this in production for lisp server allowing many
> clients to connect to a lisp server and executes lisp commands.
If you want to write a server that allows a client to send
the printed representations of Lisp forms (over the network),
so that the server evaluates the forms, then what you should
do is bind *package* to whatever value is ought to have,
and then call the Lisp reader on the printed representation
coming in from the client, to produce Lisp data, which you
can then "eval".

Usually it's not such a good idea to have a server that
evaluates arbitrary Lisp forms, since by accident,
or maliciously, the client can cause anything at all
to happen.  However, if that's what you want to do,
go ahead...

-- Dan

>
> I will try to modify the reader so that no one can access internal 
> symbols
> from any other package. (I am not sure about how yet)
>
> -Taoufik
>
> On Jun 1, 2009, at 10:44 PM, Dan Weinreb wrote:
>
>>
>> The difference is that the first one is printing
>> in the context of "pkg" but the second is
>> printing in the context of the current package.
>>
>> This is probably all educational for you, but
>> you should know that no real Lisp program
>> would do anything like this.
>>
>>
>>
>> Taoufik Dachraoui wrote:
>>> Hi
>>>
>>> I wrote 2 functions slightly different to evaluate a lisp body in a  
>>> given package
>>> one is wrong and the other is correct at least for the test I run).
>>>
>>> The problem I could not understand why this is so, both functions  
>>> looks correct
>>> as far as I can tell;
>>>
>>> (make-package 'here)
>>> (make-package 'there)
>>>
>>> (in-package 'here)
>>>
>>> (defun eval-in-package-1 (pkg) ; wrong
>>>   (let ((req (read))
>>>     (*package* (find-package pkg)))
>>>     (eval (read-from-string (format nil "~S" req)))))
>>>
>>> (defun eval-in-package-2 (pkg)  ; correct
>>>   (let ((req (format nil "~S" (read)))
>>>     (*package* (find-package pkg)))
>>>     (eval (read-from-string req))))
>>>
>>>
>>> ? (eval-in-package-1 'there)
>>> (setq a 1)
>>>
>>> 1
>>> ?
>>> ? (eval-in-package-2 'there)
>>> (setq b 1)
>>>
>>> 1
>>> ?
>>> ? here::a                                ;;;;;;;;;; wrong; a must be 
>>> defined in package there
>>> 1
>>> ? here::b                                ;;;;; this correct, b is 
>>> defined in package there
>>> > Error: Unbound variable: B
>>> > While executing: CCL::TOPLEVEL-EVAL, in process listener(1).
>>> > Type :GO to continue, :POP to abort, :R for a list of available  
>>> restarts.
>>> > If continued: Retry getting the value of B.
>>> > Type :? for other options.
>>> 1 > :pop
>>>
>>> ? there::a                                   
>>> > Error: Unbound variable: THERE::A
>>> > While executing: CCL::TOPLEVEL-EVAL, in process listener(1).
>>> > Type :GO to continue, :POP to abort, :R for a list of available  
>>> restarts.
>>> > If continued: Retry getting the value of THERE::A.
>>> > Type :? for other options.
>>> 1 > :pop
>>>
>>> ? there::b                ;; correct
>>> 1
>>> ?
>>>
>>>
>>>
>>> Kind regards
>>>
>>> -Taoufik
>>>
>>>
>>> _______________________________________________
>>> Openmcl-devel mailing list
>>> Openmcl-devel at clozure.com
>>> http://clozure.com/mailman/listinfo/openmcl-devel
>>>
>>
>
>



More information about the Openmcl-devel mailing list