From 55e6c7d82b86c69f1c08b78a4f78aac424b77268 Mon Sep 17 00:00:00 2001
From: Gwenael Casaccio <mrgwen@gmail.com>
Date: Wed, 16 Oct 2013 20:43:00 +0200
Subject: [PATCH] Fix an issue in the current line number

---
 ChangeLog                    | 10 ++++++++++
 kernel/CompildCode.st        | 39 ++++++++++++++++++++------------------
 kernel/ContextPart.st        | 15 +++++++++------
 packages/debug/ChangeLog     |  4 ++++
 packages/debug/debugtests.st | 45 ++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 89 insertions(+), 24 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index cd33f5d..23791f7 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,13 @@
+2013-10-15  Gwenael Casaccio <gwenael.casaccio@gmail.com>
+
+	* kernel/ContextPart.st: Fix >>#currentLine the previous 
+	implementation had a bad ip index and diddn't raise an exception
+	if the instruction pointer was incorrect.
+	* kernel/CompildCode.st: Fix >>#sourceCodeMap since ip points to
+	the next bytecode which could be a line number, there could be a off
+	by one error. Thus when the line number bytecode is met it's stored on
+	the next bytecode.
+
 2013-10-07  Paolo Bonzini  <bonzini@redhat.com>
 
 	* kernel/BlkClosure.st: Fix off-by-one using the sp variable
diff --git a/kernel/CompildCode.st b/kernel/CompildCode.st
index f42034f..fd3f1b8 100644
--- a/kernel/CompildCode.st
+++ b/kernel/CompildCode.st
@@ -458,24 +458,27 @@ superclass for blocks and methods'>
     ]
 
     sourceCodeMap [
-	"Answer an array which maps bytecode indices to source code
-	 line numbers.  0 values represent invalid instruction
-	 pointer indices."
-
-	<category: 'testing accesses'>
-	| map line first |
-	map := ByteArray new: self size.
-	line := 1.
-	first := true.
-	self allByteCodeIndicesDo: 
-		[:each :byte :operand | 
-		(self class bytecodeInfoTable at: byte * 4 + 4) >= 128 
-		    ifTrue: 
-			[first ifFalse: [line := operand].
-			first := false.
-			operand > 255 ifTrue: [map := map asArray]].
-		map at: each put: line].
-	^map
+        "Answer an array which maps bytecode indices to source code
+         line numbers.  0 values represent invalid instruction
+         pointer indices."
+
+        <category: 'testing accesses'>
+        | map line first next |
+        map := ByteArray new: self size.
+        next := -1.
+        line := 1.
+        first := true.
+        self allByteCodeIndicesDo:
+            [ :each :byte :operand |
+                (self class bytecodeInfoTable at: byte * 4 + 4) >= 128
+                    ifTrue:
+                        [ first ifFalse: [ next := operand ].
+                          first := false.
+                          operand > 255 ifTrue: [ map := map asArray ] ].
+                map at: each put: line.
+                next = -1 ifFalse: [ line := next.
+                                     next := -1. ] ].
+        ^ map
     ]
 
     jumpDestinationAt: anIndex forward: aBoolean [
diff --git a/kernel/ContextPart.st b/kernel/ContextPart.st
index 2c6d5e8..bc0e07a 100644
--- a/kernel/ContextPart.st
+++ b/kernel/ContextPart.st
@@ -244,12 +244,15 @@ methods that can be used in inspection or debugging.'>
     ]
 
     currentLine [
-	"Answer the 1-based number of the line that is pointed to by the receiver's
-	 instruction pointer.  The DebugTools package caches information,
-	 thus making the implementation faster."
-
-	<category: 'debugging'>
-	^self method sourceCodeMap at: (self ip - 1 max: 1) ifAbsent: [1]
+        "Answer the 1-based number of the line that is pointed to by the receiver's
+         instruction pointer.  The DebugTools package caches information,
+         thus making the implementation faster."
+
+        <category: 'debugging'>
+        | res |
+        res := self method sourceCodeMap at: self ip + 1.
+        ^ res = 0 ifTrue: [ self halt ]
+                  ifFalse: [ res ]
     ]
 
     debugger [
diff --git a/packages/debug/ChangeLog b/packages/debug/ChangeLog
index e91de3f..b660c9a 100644
--- a/packages/debug/ChangeLog
+++ b/packages/debug/ChangeLog
@@ -1,3 +1,7 @@
+2013-10-15  Gwenael Casaccio  <gwenael.casaccio@gmail.com>
+
+	* debugtests.st: Add test for currentLineInFile.
+
 2013-06-18  Gwenael Casaccio  <mrgwen@gmail.com>
 
 	* DebugTools.st: Add >>#receiver for VisualGST.
diff --git a/packages/debug/debugtests.st b/packages/debug/debugtests.st
index c1d142a..c306047 100644
--- a/packages/debug/debugtests.st
+++ b/packages/debug/debugtests.st
@@ -29,6 +29,38 @@
 
 
 
+SmallInteger extend [
+
+    dbgPrintNl [
+        <category: '*DebugTools'>
+        ^ self dbgPrintString
+    ]
+
+    dbgPrintString [
+        "Return the base 10 representation of the receiver"
+
+        <category: '*DebugTools'>
+        ^self dbgPrintString: 10
+    ]
+
+    dbgPrintString: baseInteger [
+        "Return the base baseInteger representation of the receiver"
+
+        <category: '*DebugTools'>
+        | num string |
+        ^self < self zero
+            ifFalse:
+                [string := String new: (self floorLog: baseInteger) + 1.
+                self replace: string withStringBase: baseInteger]
+            ifTrue:
+                [num := self negated.
+                string := String new: (num floorLog: baseInteger) + 2.
+                string at: 1 put: $-.
+                num replace: string withStringBase: baseInteger]
+    ]
+]
+
+^L
 TestCase subclass: DebuggerTest [
     
     <comment: nil>
@@ -264,6 +296,19 @@ TestCase subclass: DebuggerTest [
 	self deny: notReached
     ]
 
+    testRegressionCurrentLine [
+        <category: 'testing'>
+
+        | debugger |
+        debugger := self debuggerOn: [ 1 dbgPrintNl ].
+        debugger step.
+
+        #(34 36 39 43 46 51 53) doWithIndex: [ :each :i |
+            self assert: debugger suspendedContext currentLineInFile == each.
+            debugger step.
+        ]
+    ]
+
     w [
 	<category: 'support'>
 	self x: [:foo | ^foo]
-- 
1.8.1.2

