-
-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[demo/survey-loop] Repro of issue with mutating container in 'for'
This is issue #2243.
- Loading branch information
Andy C
committed
Jan 31, 2025
1 parent
b2da0db
commit 2139a39
Showing
1 changed file
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
#!/usr/bin/env bash | ||
# | ||
# Survey loop behavior | ||
# | ||
# Usage: | ||
# demo/survey-str-api.sh <function name> | ||
|
||
set -o nounset | ||
set -o pipefail | ||
set -o errexit | ||
|
||
source build/dev-shell.sh # python3 in $PATH | ||
|
||
# Python and JS string and regex replacement APIs | ||
|
||
mutate-py() { | ||
echo --- | ||
echo PY | ||
|
||
python3 -c ' | ||
mylist = [1,2,3] | ||
for x in mylist: | ||
if x == 2: | ||
mylist.append(99) | ||
print(x) | ||
' | ||
} | ||
|
||
mutate-js() { | ||
echo --- | ||
echo 'JS let' | ||
|
||
nodejs -e ' | ||
let mylist = [1,2,3] | ||
for (let x of mylist) { | ||
if (x === 2) { | ||
mylist.push(99) | ||
} | ||
console.log(x) | ||
} | ||
' | ||
|
||
echo --- | ||
echo 'JS var' | ||
|
||
nodejs -e ' | ||
var mylist = [1,2,3] | ||
for (var x of mylist) { | ||
if (x === 2) { | ||
mylist.push(99) | ||
} | ||
console.log(x) | ||
} | ||
' | ||
} | ||
|
||
mutate-sh() { | ||
local sh=${1:-bash} | ||
|
||
echo --- | ||
echo sh=$sh | ||
|
||
$sh -c ' | ||
declare -a mylist=(1 2 3) | ||
for x in "${mylist[@]}"; do | ||
if test $x == 2; then | ||
mylist+=(99) | ||
fi | ||
echo $x | ||
done | ||
echo "${mylist[@]}" | ||
' | ||
} | ||
|
||
mutate-ysh() { | ||
echo --- | ||
echo 'YSH' | ||
|
||
ysh -c ' | ||
var mylist = [1,2,3] | ||
for x in (mylist) { | ||
if (x === 2) { | ||
call mylist->append(99) | ||
} | ||
echo $x | ||
} | ||
' | ||
} | ||
|
||
compare-mutate() { | ||
mutate-py | ||
mutate-js | ||
mutate-sh bash | ||
mutate-sh osh | ||
mutate-ysh | ||
} | ||
|
||
|
||
"$@" |