OILS / spec / ysh-builtin-eval.test.sh View on Github | oilshell.org

140 lines, 72 significant
1# YSH specific features of eval
2
3## our_shell: ysh
4## oils_failures_allowed: 1
5
6#### Eval does not take a literal block - can restore this later
7
8var b = ^(echo obj)
9eval (b)
10
11eval (^(echo command literal))
12
13# Doesn't work because it's a positional arg
14eval { echo block }
15
16## status: 3
17## STDOUT:
18obj
19command literal
20## END
21
22
23#### Eval a block within a proc
24proc run (;;; block) {
25 eval (block)
26}
27
28run {
29 echo 'In a block!'
30}
31## STDOUT:
32In a block!
33## END
34
35#### Eval block created by calling a proc
36proc lazy-block ( ; out; ; block) {
37 call out->setValue(block)
38}
39
40var myglobal = 0
41
42lazy-block (&my_block) {
43 json write (myglobal)
44}
45
46eval (my_block)
47setvar myglobal = 1
48eval (my_block)
49## STDOUT:
500
511
52## END
53
54#### eval (block) can read variables like eval ''
55
56proc p2(code_str) {
57 var mylocal = 42
58 eval $code_str
59}
60
61p2 'echo mylocal=$mylocal'
62
63proc p (;;; block) {
64 var mylocal = 99
65 eval (block)
66}
67
68p {
69 echo mylocal=$mylocal
70}
71
72
73## STDOUT:
74mylocal=42
75mylocal=99
76## END
77
78#### eval should have a sandboxed mode
79
80proc p (;;; block) {
81 var this = 42
82
83 # like push-registers? Not sure
84 # We could use state.ctx_Temp ? There's also ctx_FuncCall etc.
85 #
86 # I think we want to provide full control over the stack.
87 push-frame {
88 eval (block)
89 }
90}
91
92p {
93 echo $this
94}
95
96## status: 1
97## STDOUT:
98TODO
99## END
100
101
102#### eval 'mystring' vs. eval (myblock)
103
104eval 'echo plain'
105echo plain=$?
106var b = ^(echo plain)
107eval (b)
108echo plain=$?
109
110echo
111
112# This calls main_loop.Batch(), which catches
113# - error.Parse
114# - error.ErrExit
115# - error.FatalRuntime - glob errors, etc.?
116
117try {
118 eval 'echo one; false; echo two'
119}
120pp test_ (_error)
121
122# This calls CommandEvaluator.EvalCommand(), as blocks do
123
124var b = ^(echo one; false; echo two)
125try {
126 eval (b)
127}
128pp test_ (_error)
129
130## STDOUT:
131plain
132plain=0
133plain
134plain=0
135
136one
137(Dict) {"code":1}
138one
139(Dict) {"code":1}
140## END