Skip to content

Commit

Permalink
[test/spec] Preparing for ARGV changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Chu committed Apr 13, 2024
1 parent 2ababe1 commit d642ef8
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 49 deletions.
11 changes: 8 additions & 3 deletions core/executor.py
Expand Up @@ -225,9 +225,6 @@ def RunSimpleCommand(self, cmd_val, cmd_st, run_flags):
- YSH might have different, simpler rules. No special builtins, etc.
- YSH might have OILS_PATH = :| /bin /usr/bin | or something.
- Interpreters might want to define all their own builtins.
Args:
call_procs: whether to look up procs.
"""
argv = cmd_val.argv
if len(cmd_val.arg_locs):
Expand Down Expand Up @@ -265,6 +262,11 @@ def RunSimpleCommand(self, cmd_val, cmd_st, run_flags):
call_procs = not (run_flags & NO_CALL_PROCS)
# Builtins like 'true' can be redefined as functions.
if call_procs:
# TODO: Look shell functions in self.sh_funcs, but procs are
# value.Proc in the var namespace.
# Pitfall: What happens if there are two of the same name? I guess
# that's why you have = and 'type' inspect them

proc_node = self.procs.get(arg0)
if proc_node is not None:
if self.exec_opts.strict_errexit():
Expand Down Expand Up @@ -685,3 +687,6 @@ def PopProcessSub(self, compound_st):

# The CommandEvaluator could have a ProcessSubStack, which supports Push(),
# Pop(), and Top() of VALUES rather than GC objects?


# vim: sw=4
17 changes: 15 additions & 2 deletions core/state.py
Expand Up @@ -939,12 +939,25 @@ def __exit__(self, type, value, traceback):


class ctx_ProcCall(object):
"""For proc calls."""
"""For proc calls, including shell functions."""

def __init__(self, mem, mutable_opts, proc, argv):
# type: (Mem, MutableOpts, value.Proc, List[str]) -> None

# TODO:
# - argv stack shouldn't be used for procs
# - we can bind a real variable @A if we want
# - procs should be in the var namespace
#
# should we separate procs and shell functions?
# - dynamic scope is one difference
# - '$@" shift etc. are another difference

mem.PushCall(proc.name, proc.name_tok, argv)
mutable_opts.PushDynamicScope(proc.dynamic_scope)

# Dynamic scope is only for shell functions
mutable_opts.PushDynamicScope(proc.sh_compat)

# It may have been disabled with ctx_ErrExit for 'if echo $(false)', but
# 'if p' should be allowed.
self.mem = mem
Expand Down
11 changes: 6 additions & 5 deletions core/value.asdl
Expand Up @@ -133,6 +133,7 @@ module value
# leak, like glob().
| IO(any cmd_ev, any prompt_ev)

# Do we need this?
# _guts->heapId() can be used to detect object cycles.
# It's considered impure; it depends on VM implementation details. The =
# operator and 'pp value' also print the heap ID.
Expand All @@ -145,14 +146,14 @@ module value
| BuiltinFunc(any callable)
| BoundFunc(value me, value func)

# command.ShFunction and command.Proc evaluate to ProcValue.
# Procs have default args to evaluate, and no dynamic scope.
# command.ShFunction and command.Proc evaluate to value.Proc
# They each have name, name_tok, and body.
#
# TODO: this can also have frame.
# Perhaps divide this into Proc and ShFunction
# YSH procs disable dynamic scope, have default args to evaluate, and
# different @ARGV.

| Proc(str name, Token name_tok, proc_sig sig, command body,
ProcDefaults? defaults, bool dynamic_scope)
ProcDefaults? defaults, bool sh_compat)

# module may be a frame where defined
| Func(str name, Func parsed,
Expand Down
24 changes: 0 additions & 24 deletions spec/ysh-options.test.sh
Expand Up @@ -135,30 +135,6 @@ hi
hi
## END

#### ARGV is similar to "$@"
shopt -s parse_at
argv.py "$@"
argv.py @ARGV
#argv.py "${ARGV[@]}" # not useful, but it works!

set -- 'a b' c
argv.py "$@"
argv.py @ARGV

f() {
argv.py "$@"
argv.py @ARGV
}
f 1 '2 3'
## STDOUT:
[]
[]
['a b', 'c']
['a b', 'c']
['1', '2 3']
['1', '2 3']
## END

#### shopt -s strict:all
shopt -s strict:all
# normal option names
Expand Down
91 changes: 76 additions & 15 deletions spec/ysh-proc.test.sh
@@ -1,4 +1,4 @@
## oils_failures_allowed: 0
## oils_failures_allowed: 1

#### Open proc (any number of args)
shopt --set parse_proc
Expand Down Expand Up @@ -30,25 +30,28 @@ f a b # status 2
status=42
## END

#### Open proc has "$@"
shopt -s oil:all
#### Open proc has ARGV
shopt -s ysh:all
proc foo {
write ARGV "$@"
echo ARGV @ARGV
# do we care about this? I think we want to syntactically remove it from YSH
# but it can still be used for legacy
echo dollar-at "$@"
}
builtin set -- a b c
foo x y z
## STDOUT:
ARGV
x
y
z
ARGV x y z
dollar-at x y z
## END

#### Closed proc doesn't have "$@"
shopt -s oil:all
#### Closed proc has empty "$@" or ARGV
shopt -s ysh:all

proc foo(d, e, f) {
write params $d $e $f
write ARGV "$@"
argv.py dollar-at "$@"
argv.py ARGV @ARGV
}
builtin set -- a b c
foo x y z
Expand All @@ -57,10 +60,10 @@ params
x
y
z
ARGV
['dollar-at']
['ARGV']
## END


#### Proc with default args
shopt --set parse_proc

Expand All @@ -75,7 +78,7 @@ x=foo
#### Proc with word params
shopt --set parse_proc

# doesn't require oil:all
# doesn't require ysh:all
proc f(x, y, z) {
echo $x $y $z
var ret = 42
Expand All @@ -97,7 +100,7 @@ status=42
# func(**opt) # Assumes keyword args match?
# parse :grep_opts :opt @ARGV

shopt -s oil:all
shopt -s ysh:all

proc f(...names) {
write names: @names
Expand Down Expand Up @@ -357,3 +360,61 @@ Block
Command

## END

#### Global and local ARGV, like "$@"
shopt -s parse_at
argv.py "$@"
argv.py @ARGV
#argv.py "${ARGV[@]}" # not useful, but it works!

set -- 'a b' c
argv.py "$@"
argv.py @ARGV

f() {
argv.py "$@"
argv.py @ARGV
}
f 1 '2 3'
## STDOUT:
[]
[]
['a b', 'c']
['a b', 'c']
['1', '2 3']
['1', '2 3']
## END


#### Mutating global and local ARGV

$SH -c '
shopt -s ysh:upgrade
argv.py global @ARGV
# should not be ignored
call ARGV->append("global")
argv.py global @ARGV
proc p {
argv.py @ARGV
call ARGV->append("local")
argv.py @ARGV
}
p local @ARGV
argv.py global @ARGV
' dummy0 'a b' c

## STDOUT:
['global', 'a b', 'c']
['global', 'a b', 'c', 'global']
['local', 'a b', 'c']
['local', 'a b', 'c', 'local']
['global', 'a b', 'c', 'global']
## END

0 comments on commit d642ef8

Please sign in to comment.