Skip to content

Commit 1d9996d

Browse files
committed
Support return after function f()
1 parent 016d574 commit 1d9996d

File tree

4 files changed

+35
-1
lines changed

4 files changed

+35
-1
lines changed

src/core/builtins/option_commands.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ pub fn set_parameters(core: &mut ShellCore, args: &[String]) -> i32 {
3939
_ => {},
4040
}
4141
core.data.position_parameters.push(args.to_vec());
42+
core.data.set_param("#", &(args.len()-1).to_string());
4243
0
4344
}
4445

src/elements/command/function_def.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,21 @@ impl FunctionDefinition {
116116
return None;
117117
}
118118
ans.text += &feeder.consume(2);
119-
command::eat_blank_with_comment(feeder, core, &mut ans.text);
119+
loop {
120+
if feeder.starts_with("\n") {
121+
ans.text += &feeder.consume(1);
122+
continue;
123+
}
124+
125+
if feeder.len() == 0 {
126+
if ! feeder.feed_additional_line(core) {
127+
return None;
128+
}
129+
}
130+
if ! command::eat_blank_with_comment(feeder, core, &mut ans.text) {
131+
break;
132+
}
133+
}
120134

121135
Self::eat_compound_command(feeder, &mut ans, core);
122136
command::eat_blank_with_comment(feeder, core, &mut ans.text);

test/test_compound.bash

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ res=$($com <<< 'f () { echo a; } ; f')
101101
res=$($com <<< 'function f () { echo a; } ; f')
102102
[ "$res" = "a" ] || err $LINENO
103103

104+
res=$($com <<< 'function _f () { echo a; } ; _f')
105+
[ "$res" = "a" ] || err $LINENO
106+
104107
res=$($com <<< 'function f () { echo $A; } ; A=OK f')
105108
[ "$res" = "OK" ] || err $LINENO
106109

@@ -344,6 +347,19 @@ EOF
344347
[ "$res" = "a
345348
a" ] || err $LINENO
346349

350+
res=$($com <<< 'function f () { echo a; if true ; then return ; fi ; echo b; } ; f')
351+
[ "$res" = "a" ] || err $LINENO
352+
353+
res=$($com << 'EOF'
354+
f()
355+
{
356+
echo a;
357+
}
358+
EOF
359+
)
360+
[ "$?" = 0 ] || err $LINENO
361+
[ "$res" = "" ] || err $LINENO
362+
347363

348364
### CASE TEST ###
349365

test/test_others.bash

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,9 @@ res=$($com <<< 'echo "" a')
379379
res=$($com <<< 'set a b c; echo a"$@"c')
380380
[ "$res" == "aa b cc" ] || err $LINENO
381381

382+
res=$($com <<< 'set a b c; echo $#')
383+
[ "$res" == "3" ] || err $LINENO
384+
382385
res=$($com <<< 'set a b c; A=( A"$@"C ); echo ${A[0]}')
383386
[ "$res" == "Aa" ] || err $LINENO
384387

0 commit comments

Comments
 (0)