Skip to content

Commit

Permalink
[demo/survey-loop] Repro of issue with mutating container in 'for'
Browse files Browse the repository at this point in the history
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.
99 changes: 99 additions & 0 deletions demo/survey-loop.sh
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
}


"$@"

0 comments on commit 2139a39

Please sign in to comment.