Skip to content

Commit

Permalink
Fix a crash in 'set', but also add 2 failing tests.
Browse files Browse the repository at this point in the history
We implement 'set -o' as 'set', and we don't implement 'set' at all.

Also, remove obsolete comments in core/state.py.
  • Loading branch information
Andy Chu committed Sep 22, 2018
1 parent 8781632 commit f1ea3e8
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 32 deletions.
5 changes: 4 additions & 1 deletion core/runtime.asdl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ module runtime
-- For storing a variable.
cell = (value val, bool exported, bool readonly)

var_flags = Exported | ReadOnly | Array | AssocArray
-- An undefined variable can become an indexed array with s[x]=1. But if we
-- 'declare -A' it, it will be undefined and waiting to turn into an
-- associative array.
var_flags = Exported | ReadOnly | AssocArray
scope = TempEnv | LocalOnly | GlobalOnly | Dynamic

-- For assignment, evaluated to osh_ast.lhs_expr
Expand Down
35 changes: 5 additions & 30 deletions core/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ def __init__(self, mem, readline):
# Don't need flags -e and -n. -e is $'\n', and -n is write.
self.sane_echo = False

self.vi = False
self.emacs = False

def _InitOptionsFromEnv(self, shellopts):
# e.g. errexit:nounset:pipefail
lookup = set(shellopts.split(':'))
Expand Down Expand Up @@ -826,42 +829,14 @@ def SetVar(self, lval, value, new_flags, lookup_mode, strict_array=False):
raise AssertionError(lval.__class__.__name__)

def _BindNewArrayWithEntry(self, namespace, lval, value, new_flags):
# When the array doesn't exist yet, it is created filled with None.
# Access to the array needs to explicitly filter those sentinel values.
# It also wastes memory. But indexed access is fast.

# What should be optimized for? Bash uses a linked list. Random access
# takes linear time, but iteration skips unset entries automatically.

# - Maybe represent as hash table? Then it's not an ASDL type?

# representations:
# - array_item.Str array_item.Undef
# - parallel array: val.strs, val.undefs
# - or change ASDL type checking
# - ASDL language does not allow: StrArray(string?* strs)
# - or add dict to ASDL? Didn't it support obj?
# - finding the max index is linear time?
# - also you have to sort the indices
#
# array ops:
# a=(1 2)
# a[1]=x
# a+=(1 2)
# ${a[@]} - get all
# ${#a[@]} - length
# ${!a[@]} - keys
# That seems pretty minimal.

"""Fill 'namespace' with a new indexed array entry."""
items = [None] * lval.index
items.append(value.s)
new_value = runtime.StrArray(items)
# arrays can't be exported
cell = runtime.cell(new_value, False,
var_flags_e.ReadOnly in new_flags)
cell = runtime.cell(new_value, False, var_flags_e.ReadOnly in new_flags)
namespace[lval.name] = cell


def InternalSetGlobal(self, name, new_val):
"""For setting read-only globals internally.
Expand Down
8 changes: 8 additions & 0 deletions spec/sh-options.test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,11 @@ SHELLOPTS=x
echo status=$?
## stdout: status=1
## N-I dash/mksh stdout: status=0

#### set -o lists options
set -o | grep set
## status: 0

#### set without args lists variables
set | grep PWD
## status: 0
2 changes: 1 addition & 1 deletion test/spec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ var-sub-quote() {
}

sh-options() {
sh-spec spec/sh-options.test.sh --osh-failures-allowed 3 \
sh-spec spec/sh-options.test.sh --osh-failures-allowed 5 \
${REF_SHELLS[@]} $OSH_LIST "$@"
}

Expand Down

0 comments on commit f1ea3e8

Please sign in to comment.