git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 2/2] git-gui: create a new namespace for chord script evaluation

From
Pratyush Yadav <me@yadavpratyush.com>
Date
Mar 17, 2020, 13:29 UTC
Message-ID
<20200317132921.7222-3-me@yadavpratyush.com>
In-Reply-To
<20200317132921.7222-1-me@yadavpratyush.com>

Evaluating the script in the same namespace as the chord itself creates potential for variable name collision. And in that case the script would unknowingly use the chord's variables.

For example, say the script has a variable called 'is_completed', which also exists in the chord's namespace. The script then calls 'eval' and sets 'is_completed' to 1 thinking it is setting its own variable, completely unaware of how the chord works behind the scenes. This leads to the chord never actually executing because it sees 'is_completed' as true and thinks it has already completed.

Avoid the potential collision by creating a separate namespace for the script that is a child of the chord's namespace.

Signed-off-by: Pratyush Yadav <me@yadavpratyush.com>
---
 lib/chord.tcl | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/lib/chord.tcl b/lib/chord.tcl
index 7de7cba..e21e7d3 100644
--- a/lib/chord.tcl
+++ b/lib/chord.tcl
@@ -64,6 +64,7 @@ class SimpleChord {
 	field notes
 	field body
 	field is_completed
+	field eval_ns
 
 	# Constructor:
 	#   set chord [SimpleChord::new {body}]
@@ -74,6 +75,7 @@ class SimpleChord {
 		set notes [list]
 		set body $i_body
 		set is_completed 0
+		set eval_ns "[namespace qualifiers $this]::eval"
 		return $this
 	}
 
@@ -83,7 +85,7 @@ class SimpleChord {
 	#     the chord body will be evaluated. This can be used to set variable
 	#     values for the chord body to use.
 	method eval {script} {
-		namespace eval [namespace qualifiers $this] $script
+		namespace eval $eval_ns $script
 	}
 
 	# Method:
@@ -111,7 +113,7 @@ class SimpleChord {
 
 			set is_completed 1
 
-			namespace eval [namespace qualifiers $this] $body
+			namespace eval $eval_ns $body
 			delete_this
 		}
 	}
-- 
2.26.0.rc1.11.g30e9940356
Previous: Pratyush YadavNext: Eric Sunshine
Message 10 of 12 in “git-gui: reduce Tcl version requirement from 8.6 to 8.5”
  1. 0/2 git-gui: reduce Tcl version requirement from 8.6 to 8.5Pratyush Yadav, Mar 14, 2020
  2. 2/2 git-gui: create a new namespace for chord script evaluationPratyush Yadav, Mar 14, 2020
  3. 1/2 git-gui: reduce Tcl version requirement from 8.6 to 8.5Pratyush Yadav, Mar 14, 2020
  4. Eric SunshineMar 15, 2020
  5. Junio C HamanoMar 16, 2020
  6. Pratyush YadavMar 17, 2020
  7. Eric SunshineMar 19, 2020
  8. 0/2 git-gui: reduce Tcl version requirement from 8.6 to 8.5Pratyush Yadav, Mar 17, 2020
  9. 1/2 git-gui: reduce Tcl version requirement from 8.6 to 8.5Pratyush Yadav, Mar 17, 2020
  10. 2/2 git-gui: create a new namespace for chord script evaluationPratyush Yadav, Mar 17, 2020
  11. Eric SunshineMar 19, 2020
  12. Pratyush YadavMar 19, 2020

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.