github.com/upcmd/up@v0.8.1-0.20230108151705-ad8b797bf04f/tests/functests/c0113.yml (about) 1 doc_meta: | 2 folder: call-func 3 title: return values 4 head: | 5 As we know you will actually implicitly creaet a whole private call stack and spawn a private local vars in this scope, in such a case the work you have done, including register some values to global(relatively to this private stack), will not be reflected in your caller's var scope. So how do you keep your result in case that you really need to do so. 6 7 We can use return cmd to achieve that. When you use return cmd, all the listed var names' values will be registered to its caller's var scope 8 9 sections: 10 - title: Demo 11 log: yes 12 13 notes: 14 goal: 15 - add new feature of return from callee task 16 - | 17 application could be use as auto returned vars to parent var scope, 18 or use as a callerable task module to do a callback data injection 19 for other client caller tasks 20 - notice the difference between c0107 and this case 21 22 vars: 23 tom: this is tom in global 24 jerry: this is jerry in global 25 26 tasks: 27 - 28 name: task 29 desc: main entry 30 task: 31 - 32 func: call 33 desc: call subtask and exam the return value in following steps 34 do: subtask 35 36 - 37 func: cmd 38 desc: check value of tom 39 do: 40 - name: print 41 cmd: 'in main task print3: {{.tom}}' 42 43 - 44 name: assert 45 desc: since .tom is returned from callee, it should be accessible here 46 cmd: 47 - '{{eq .tom "tom created in sub_loop"}}' 48 49 - 50 name: subtask 51 desc: subtask to test reg and return 52 task: 53 - 54 func: cmd 55 desc: check value of tom after it is registered in current task stack 56 vars: 57 john: john in sub_loop func1 58 do: 59 - name: reg 60 desc: by default hitom is registered in to global context 61 cmd: 62 name: tom 63 value: 'tom created in sub_loop' 64 65 - name: print 66 cmd: 'in sub_loop print1: {{.tom}}' 67 68 - 69 name: assert 70 cmd: 71 - '{{eq .tom "tom created in sub_loop"}}' 72 73 - 74 func: cmd 75 desc: check value of tom and it should be available in current stack 76 vars: 77 john: john in sub_loop func2 78 do: 79 - name: print 80 cmd: 'in sub_loop print2: {{.tom}}' 81 82 - 83 name: assert 84 desc: since .tom is in callee's global, it should be accessible here 85 cmd: 86 - '{{eq .tom "tom created in sub_loop"}}' 87 88 - name: return 89 desc: | 90 it should return and merge tom to parent's vars scope 91 it should report warning as jason does not exist 92 cmd: 93 - tom 94 - jason 95