|
2 | 2 |
|
3 | 3 | _Functions_ are independent chunks of program code. _Calling_ or _invoking_ a |
4 | 4 | function causes its code to run. A function has zero or more _parameters_, |
5 | | -which are bound to _arguments_ from a call expression. If the function body |
6 | | -finishes normally, a value may be returned from the function; this value then |
7 | | -becomes the value of the call expression that invoked the function. |
| 5 | +variables which become bound to _arguments_ passed from a call expression. A |
| 6 | +function's body may finish _normally_ or _abruptly_. If it finishes normally, a |
| 7 | +value is returned from the function; this is then the value of the call |
| 8 | +expression that invoked the function. |
8 | 9 |
|
9 | 10 | ## 5.1 Function calls |
10 | 11 |
|
11 | | -Call expressions were described in section 2.8 "Call expressions"; |
| 12 | +Call expressions are described in section 2.8 "Call expressions"; |
12 | 13 | syntactically, a call expression consists of a _callable expression_ followed |
13 | | -by a (parentheses-enclosed) list of _operands_, which are also expressions. |
14 | | -At the time of evaluating the call expression, the following steps happen: |
| 14 | +by a (parentheses-enclosed) list of _operands_, which are also expressions. At |
| 15 | +the time of evaluating the call expression, these steps happen: |
15 | 16 |
|
16 | 17 | * The callable expression is fully evaluated into a value. |
17 | 18 | * This value is confirmed to be a function, or more precisely a value which |
18 | | - implements the invocation protocol; if not, then an exception is signaled |
19 | | - at runtime and evaluation stops. |
| 19 | + implements the invocation protocol; if not, an exception is signaled at |
| 20 | + runtime, and evaluation stops. |
20 | 21 | * All of the operands are evaluated, left-to-right, into values called |
21 | 22 | _arguments_. |
22 | 23 | * The `call` method of the invocation protocol is invoked, with an array of |
23 | 24 | the arguments. |
24 | | - * Parameter binding happens, explained in the next section. If successful, |
| 25 | + * Parameter binding happens, explained in section 5.8. If successful, |
25 | 26 | this results in an extended environment. |
26 | | - * The function's body is run. This is explained in section 5.3 "Function |
27 | | - body". |
28 | | - * Eventually, control might return normally, in which case a value is |
| 27 | + * The function's body is run in the extended environment. This is explained |
| 28 | + in section 5.9 "Function body". |
| 29 | + * Eventually, control might return normally, in which case a value is also |
29 | 30 | returned. This value is then the value of entire call expression. This is |
30 | | - explained in section 5.4 "Returning from a function". |
| 31 | + explained in section 5.10 "Returning from a function". |
31 | 32 |
|
32 | 33 | This describes a "call-by-value" convention, in which only values are passed |
33 | | -from call sites to functions; that is, the operand expressions are entirely |
34 | | -evaluated at the call site, and then the resulting values are passed. |
| 34 | +from call sites to functions. The operand expressions are entirely evaluated at |
| 35 | +the call site, and then the resulting values are passed. |
35 | 36 |
|
36 | | -Operator expressions, although syntactically different, can be seen as |
37 | | -syntactic sugar for the above call expressions. This is true both for built-in |
38 | | -operators and user-defined ones. |
| 37 | +Operator expressions, although syntactically different, can be viewed as |
| 38 | +syntactic sugar for the above call expressions. |
39 | 39 |
|
40 | 40 | Both in the case of call expressions and in the case of operator expressions, |
41 | 41 | if the callable expression (statically) resolves to a macro, then macro |
@@ -72,42 +72,45 @@ xxx or `@named` and `...` |
72 | 72 |
|
73 | 73 | ## 5.8 Parameter binding |
74 | 74 |
|
75 | | -After the arguments are passed to a function for invocation, and before we can |
76 | | -run the function body, an environment for running the function body needs to |
77 | | -be prepared. This happens in two steps: making sure that there is an argument |
78 | | -for each required parameter and a parameter for each passed argument, and |
| 75 | +During function invocation, when arguments have been passed to a function for |
| 76 | +invocation, and before the function body can run, an environment is constructed in which to run the function body. |
| 77 | + |
| 78 | +This happens in two steps: first, making sure that there is an argument for |
| 79 | +each required parameter and a parameter for each passed argument, and second, |
79 | 80 | binding the parameters in the new environment. |
80 | 81 |
|
| 82 | +The first step breaks down into the following smaller steps: |
| 83 | + |
81 | 84 | * Assert that at least as many positional arguments have been passed as there |
82 | 85 | are required positional parameters. If not, signal an exception. |
83 | | -* Assert that if more positional arguments were passed than positional |
84 | | - arguments (required and optional both), there's a positional rest parameter |
85 | | - present. If not, signal an exception. |
86 | | -* Assert that the set of names of named arguments is non-strictly contained by |
87 | | - the set of names of required named parameters. If not, signal an exception. |
88 | | -* Assert that if the set of names of named arguments contains a name not |
89 | | - contained in the set of names of named arguments (required and optional |
90 | | - both), there's a named rest parameter present. If not, signal an exception. |
| 86 | +* Assert that the number of positional arguments does not exceed the number of |
| 87 | + (required and optional) positional parameters, or that there's a positional |
| 88 | + rest parameter declared in the parameter list. If not, signal an exception. |
| 89 | +* Assert that, for each required named parameter, there is a named argument of |
| 90 | + that name. If not, signal an exception. |
| 91 | +* Assert that all named arguments that were passed have a corresponding named |
| 92 | + parameter, or that a named rest parameter is declared in the parameter list. |
| 93 | + If not, signal an exception. |
91 | 94 |
|
92 | 95 | At this point, we know that parameter binding won't fail because not enough |
93 | 96 | arguments were passed for the required parameters, or too many arguments were |
94 | 97 | passed that rest parameters weren't present to absorb. |
95 | 98 |
|
96 | 99 | * For each required positional parameter, bind it to the corresponding |
97 | 100 | positional argument (of which we just checked there are enough). |
98 | | -* For each optional positional parameter, bind it (in decreasing order of |
99 | | - preference) to the corresponding positional argument, the value resulting |
100 | | - from evaluating the corresponding parameter default expression, or `none`. |
101 | | -* Make an array of any remaining positional arguments, and bind the positional |
102 | | - rest parameter (which at this point must exist) to it. |
| 101 | +* For each optional positional parameter, bind it left-to-right to the |
| 102 | + corresponding positional argument, the value of the parameter default |
| 103 | + expression if present, or `none` if not. |
| 104 | +* If there is a positional rest parameter, make an array of the remaining |
| 105 | + positional arguments, and bind the positional rest parameter to this array. |
103 | 106 | * For each required named parameter, bind it to the corresponding named |
104 | 107 | argument (which we just asserted exists). |
105 | | -* For each optional named parameter, bind it (in decreasing order of |
106 | | - preference) to the corresponding named argument, the value resulting from |
107 | | - evaluating the corresponding parameter default expression, or `none`. |
108 | | -* Make a dictionary of any remaining named arguments, keys being the names and |
109 | | - values being the named arguments, and bind the named rest parameter (which at |
110 | | - this point must exist) to it. |
| 108 | +* For each optional named parameter, bind it left-to-right (in the parameter |
| 109 | + list) to the corresponding named argument, the value of the parameter default |
| 110 | + expression if present, or `none` if not. |
| 111 | +* If there is a named rest parameter, make a dictionary of the remaining named |
| 112 | + arguments (name and argument), and bind the named rest parameter to this |
| 113 | + dictionary. |
111 | 114 |
|
112 | 115 | The resulting environment is the one that will be used when running the |
113 | 116 | function body. |
|
0 commit comments