1 ## our_shell: ysh
2 ## oils_failures_allowed: 1
3
4 #### Local place
5
6 # Work around stdin buffering issue with read --line
7 #
8 # The framework test/sh_spec.py uses echo "$code_string" | $SH
9 #
10 # But then we have TWO different values of file descriptor 0 (stdin)
11 #
12 # - the pipe with the code
13 # - the pipe created in th shell for echo zzz | read --line
14 #
15 # Shells read one line at a time, but read --line explicitly avoids this.
16 #
17 # TODO: I wonder if we should consider outlawing read --line when stdin has code
18 # Only allow it for:
19 #
20 # $SH -c 'echo hi'
21 # $SH myscript.sh
22 #
23 # There could be a warning like read --line --no-fighting or something.
24
25 cat >tmp.sh <<'EOF'
26 func f(place) {
27 var x = 'f'
28 echo zzz | read --line (place)
29 echo "f x=$x"
30 }
31
32 func fillPlace(place) {
33 var x = 'fillPlace'
34 call f(place)
35 echo "fillPlace x=$x"
36 }
37
38 proc p {
39 var x = 'hi'
40 call fillPlace(&x)
41 echo "p x=$x"
42 }
43
44 x=global
45
46 p
47
48 echo "global x=$x"
49 EOF
50
51 $SH tmp.sh
52
53 ## STDOUT:
54 f x=f
55 fillPlace x=fillPlace
56 p x=zzz
57 global x=global
58 ## END
59
60 #### place->setValue()
61
62 func f(place) {
63 var x = 'f'
64 call place->setValue('zzz')
65 echo "f x=$x"
66 }
67
68 func fillPlace(place) {
69 var x = 'fillPlace'
70 call f(place)
71 echo "fillPlace x=$x"
72 }
73
74 proc p {
75 var x = 'hi'
76 call fillPlace(&x)
77 echo "p x=$x"
78 }
79
80 x=global
81
82 p
83 echo "global x=$x"
84
85 ## STDOUT:
86 f x=f
87 fillPlace x=fillPlace
88 p x=zzz
89 global x=global
90 ## END
91
92 #### Places can't dangle; they should be passed UP the stakc only
93
94 func f() {
95 var f_local = null
96 return (&f_local)
97 }
98
99 func g() {
100 # This place is now INVALID!
101 var place = f()
102
103 # Should fail when we try to use the place
104 echo zzz | read --line (place)
105
106 # This should also fail
107 # call place->setValue('zzz')
108
109 }
110
111 call g()
112
113 echo done
114
115 ## status: 1
116 ## STDOUT:
117 ## END
118
119
120 #### Container Place (Dict)
121
122 var d = {key: 'hi'}
123
124 echo zzz | read --all (&d.key)
125
126 # I guess this works
127 echo newkey | read --all (&d.newkey)
128
129 echo key=$[d.key]
130 echo key=$[d.newkey]
131
132 ## STDOUT:
133 ## END
134
135