Hi!
----
The following testcase shows an issue with "typeset -m" which may
leave some "leftovers" when an array was moved:
-- snip --
function _assert
{
integer line=$1
shift
print -u2 -f "ASSERT line=${line}: %q\n" "$@"
# exit 1
}
alias _assert='_assert $LINENO'
# stack of objects
typeset -T objstack_t=(
compound -a st
integer st_n=0
# push an object
# argument is the name of a variable which will
# be moved into the stack space
function pushobj
{
nameref obj=$1
typeset -m "_.st[$((_.st_n++))].obj=obj"
}
# print absolute variable name of object in head
function printhead
{
printf '%s%s\n' "${!_}" ".st[${_.st_n}].obj"
}
# pop an object and return it to the location
# specified by the variable name passed in
function popobj
{
nameref obj=$1
typeset -m "obj=_.st[$((--_.st_n))].obj"
# "obj" should be removed from _.st[_.st_n] by
# now... complain if there are any "leftovers"
s="$(typeset -p _.st[_.st_n].obj)"
[[ "$s" == '' ]] || \
_assert "_.st[_.st_n].obj == \"$s\""
# remove empty array node which was created
# when "pushobj" moved the obj into the array
[[ "$(typeset -p _.st[_.st_n])" != '' ]] || \
_assert "_.st[_.st_n] is gone"
# unset _.st[_.st_n]
}
)
function main
{
compound c
objstack_t c.ost
# push some objects
compound foo=( integer val=5 )
typeset -a bar=( 2 3 4 )
c.ost.pushobj foo
c.ost.pushobj bar
# get object from stack head and store it
# in compound variable "dummy"
compound dummy
c.ost.popobj dummy
print -v c
return 0
}
set -o nounset
main
-- snip --
AFAIK the testcase should print:
-- snip --
$ ~/bin/ksh stk.sh
(
objstack_t ost=(
typeset -l -i st_n=1
st[0]=(
obj=(
typeset -l -i val=5
)
)
st[1]=(
)
)
)
-- snip --
... but ast-ksh.2013-05-03+latest patches prints:
-- snip --
$ ~/bin/ksh stk.sh
ASSERT line=42: '_.st[_.st_n].obj == "typeset -a c.ost.c.ost.st[1].obj"'
(
objstack_t ost=(
(
typeset -l -i st_n=1
st[0]=(
obj=(
typeset -l -i val=5
)
)
st[1]=(
typeset -a obj
)
)
)
-- snip --
The issue is that c.ost.st[1].obj is still an (empty) indexed array
after the function c.ost.popobj has been called (which moves the array
into the compound variable "dummy").
The assert ASSERT line=42: '_.st[_.st_n].obj == "typeset -a
c.ost.c.ost.st[1].obj"' highlights the problem (and note the mangled
variable name "c.ost.c.ost.st[1].obj" instead of the correct
"c.ost.st[1].obj" ...
----
Bye,
Roland
P.S.: The other (minor for now) issue is the extra line with "(" when
the objstack_t object is printed in the container compound variable
"c" via $ print -v c # ...
--
__ . . __
(o.\ \/ /.o) [email protected]
\__\/\/__/ MPEG specialist, C&&JAVA&&Sun&&Unix programmer
/O /==\ O\ TEL +49 641 3992797
(;O/ \/ \O;)
_______________________________________________
ast-developers mailing list
[email protected]
http://lists.research.att.com/mailman/listinfo/ast-developers