From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: egnarts-ms Newsgroups: gmane.emacs.devel Subject: Edebug & lexical scope Date: Fri, 18 May 2012 06:30:00 -0700 (PDT) Message-ID: <33870303.post@talk.nabble.com> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Trace: dough.gmane.org 1337347826 22691 80.91.229.3 (18 May 2012 13:30:26 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Fri, 18 May 2012 13:30:26 +0000 (UTC) To: Emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Fri May 18 15:30:25 2012 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1SVNG8-0007wl-0q for ged-emacs-devel@m.gmane.org; Fri, 18 May 2012 15:30:20 +0200 Original-Received: from localhost ([::1]:42511 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SVNG7-0002ij-EO for ged-emacs-devel@m.gmane.org; Fri, 18 May 2012 09:30:19 -0400 Original-Received: from eggs.gnu.org ([208.118.235.92]:50821) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SVNFw-0002h3-O6 for Emacs-devel@gnu.org; Fri, 18 May 2012 09:30:16 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SVNFq-0001NO-8b for Emacs-devel@gnu.org; Fri, 18 May 2012 09:30:08 -0400 Original-Received: from sam.nabble.com ([216.139.236.26]:38693) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SVNFq-0001M8-3r for Emacs-devel@gnu.org; Fri, 18 May 2012 09:30:02 -0400 Original-Received: from telerig.nabble.com ([192.168.236.162]) by sam.nabble.com with esmtp (Exim 4.72) (envelope-from ) id 1SVNFo-0005QN-0f for Emacs-devel@gnu.org; Fri, 18 May 2012 06:30:00 -0700 X-Nabble-From: egnartsms@gmail.com X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 2) X-Received-From: 216.139.236.26 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:150556 Archived-At: Hi, Currently (Emacs 24.0.97) Edebug cannot evaluate lexical variables when it is suspended (by "e" or "M-:"). To be more precise: when you have "lexical-binding" variable set to non-nil in a buffer, and you instrument a defun from this buffer, and then debug it with Edebug, and you're suspended at some stop point inside this defun, you cannot see the value of a (lexical) variable. Example: (defun solve-square-equation (a b c) (if (= a 0.0) (/ (- c) b) (let ((D (- (* b b) (* 4 (* a c))))) (cond ((= D 0.0) (/ (- b) (* 2 a))) ((< D 0.0) nil) (t (let ((sqD (sqrt D))) (list (/ (+ (- b) sqD) (* 2 a)) (/ (- (- b) sqD) (* 2 a))))))))) It is impossible to see the value of D when the program is stopped in any of the cond's branches. The reason for that is simple: when you are asking Edebug to evaluate something, it does so by calling `eval', and the latter just binds Qinternal_interpreter_environment (internal variable holding the current lexical environment, or nil in dynamic scoping) to "(t)" (provided that lexical scoping is on). So the values of all lexical variables are lost. Nevertheless, when you press "G" or "c", the execution resumes and those lexical variables are perfectly accessible. By all evidence, it is just due to the fact that Qinternal_interpreter_environment gets unbound, and takes on its previous value (which is what we would also like to access when evaluating by "M-:"). It seems that I've managed to make M-: evaluate lexical variables. I've created the following C subroutine; it does the same job as `eval', but first of all it scans "specpdl" (the stack of bindings of symbols) searching for the most recent value of Qinternal_interpreter_environment which is not eq to nil. (That is, we want to find out the most recent lexical environment to which Qinternal_interpreter_environment was bound by some code upward the stack.) If that value is found, Qinternal_interpreter_environment gets bound to it again, and usual `eval_sub' is called. Otherwise, `eval_sub' is called as well, but this would be in dynamic environment now. DEFUN ("eval-in-most-recent-lex-env", Seval_in_mo....) (Lisp_Object form) { struct specbinding* ptr = specpdl_ptr; while (ptr != specpdl) { if (EQ (ptr->symbol, Qinternal_interpreter_environment) && !EQ (ptr->old_value, Qnil)) { break; } --ptr; } if (ptr != specpdl) { int count = SPECPDL_INDEX (); specbind (Qinternal_interpreter_environment, ptr->old_value); return unbind_to (count, eval_sub (form)); } return eval_sub (form); } The second step is pretty easy: there's a function named `edebug-eval'. One should just change it to call `eval-in-most-recent-lex-env' rather than `eval'. That seems to work fine in Emacs 24.0.97 pretest. I'd be very glad to hear any critics or comments. Best regards, egnarts-ms. -- View this message in context: http://old.nabble.com/Edebug---lexical-scope-tp33870303p33870303.html Sent from the Emacs - Dev mailing list archive at Nabble.com.