• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/ucode/tests/custom/00_syntax/15_function_declarations

  1 Function declarations follow the ECMAScript 5 syntax. Functions can be
  2 declared anonymously, which is useful for "throw-away" functions such
  3 as sort or filter callbacks or for building objects or arrays of function
  4 values.
  5 
  6 If functions are declared with a name, the resulting function value is
  7 automatically assigned under the given name to the current scope.
  8 
  9 When function values are stringifed, the resulting string will describe
 10 the declaration of the function.
 11 
 12 Nesting function declarations is possible as well.
 13 
 14 
 15 -- Expect stdout --
 16 function() { ... }
 17 function test_fn(a, b) { ... }
 18 function test2_fn(a, b) { ... }
 19 
 20 A function declaration using the alternative syntax:
 21 The function was called with arguments 123 and 456.
 22 
 23 -- End --
 24 
 25 -- Testcase --
 26 {%
 27         // declare an anonymous function and
 28         // assign resulting value
 29         anon_fn = function() {
 30                 return "test";
 31         };
 32 
 33         // declare a named function
 34         function test_fn(a, b) {
 35                 return a + b;
 36         }
 37 
 38         // nesting functions is legal
 39         function test2_fn(a, b) {
 40                 function test3_fn(a, b) {
 41                         return a * b;
 42                 }
 43 
 44                 return a + test3_fn(a, b);
 45         }
 46 
 47         print(anon_fn, "\n");
 48         print(test_fn, "\n");
 49         print(test2_fn, "\n");
 50 %}
 51 
 52 A function declaration using the alternative syntax:
 53 {% function test3_fn(a, b): %}
 54 The function was called with arguments {{ a }} and {{ b }}.
 55 {% endfunction %}
 56 {{ test3_fn(123, 456) }}
 57 -- End --
 58 
 59 
 60 Additionally, ucode implements ES6-like "rest" argument syntax to declare
 61 variadic functions.
 62 
 63 -- Expect stdout --
 64 function non_variadic(a, b, c, d, e) { ... }
 65 [ 1, 2, 3, 4, 5 ]
 66 function variadic_1(a, b, ...args) { ... }
 67 [ 1, 2, [ 3, 4, 5 ] ]
 68 function variadic_2(...args) { ... }
 69 [ [ 1, 2, 3, 4, 5 ] ]
 70 -- End --
 71 
 72 -- Testcase --
 73 {%
 74         // ordinary, non-variadic function
 75         function non_variadic(a, b, c, d, e) {
 76                 return [ a, b, c, d, e ];
 77         }
 78 
 79         // fixed amount of arguments with variable remainder
 80         function variadic_1(a, b, ...args) {
 81                 return [ a, b, args ];
 82         }
 83 
 84         // only variable arguments
 85         function variadic_2(...args) {
 86                 return [ args ];
 87         }
 88 
 89         print(join("\n", [
 90                 non_variadic,
 91                 non_variadic(1, 2, 3, 4, 5),
 92                 variadic_1,
 93                 variadic_1(1, 2, 3, 4, 5),
 94                 variadic_2,
 95                 variadic_2(1, 2, 3, 4, 5)
 96         ]), "\n");
 97 %}
 98 -- End --
 99 
100 
101 Complementary to the "rest" argument syntax, the spread operator may be
102 used in function call arguments to pass arrays of values as argument list.
103 
104 -- Expect stdout --
105 [ 1, 2, 3, 4, 5, 6 ]
106 [ 4, 5, 6, 1, 2, 3 ]
107 [ 1, 2, 3, 1, 2, 3 ]
108 [ 1, 2, 3 ]
109 -- End --
110 
111 -- Testcase --
112 {%
113         arr = [ 1, 2, 3 ];
114 
115         function test(...args) {
116                 return args;
117         }
118 
119         print(join("\n", [
120                 test(...arr, 4, 5, 6),
121                 test(4, 5, 6, ...arr),
122                 test(...arr, ...arr),
123                 test(...arr)
124         ]), "\n");
125 %}
126 -- End --
127 
128 
129 Rest arguments may be only used once in a declaration and they must always
130 be the last item in the argument list.
131 
132 -- Expect stderr --
133 Syntax error: Unexpected token
134 Expecting ')'
135 In line 2, byte 26:
136 
137  `    function illegal(...args, ...args2) {}`
138   Near here ------------------^
139 
140 
141 -- End --
142 
143 -- Testcase --
144 {%
145         function illegal(...args, ...args2) {}
146 %}
147 -- End --
148 
149 -- Expect stderr --
150 Syntax error: Unexpected token
151 Expecting ')'
152 In line 2, byte 26:
153 
154  `    function illegal(...args, a, b) {}`
155   Near here ------------------^
156 
157 
158 -- End --
159 
160 -- Testcase --
161 {%
162         function illegal(...args, a, b) {}
163 %}
164 -- End --

This page was automatically generated by LXR 0.3.1.  •  OpenWrt