diff options
| -rw-r--r-- | manual/manual.of | 47 |
1 files changed, 26 insertions, 21 deletions
diff --git a/manual/manual.of b/manual/manual.of index 00ab4cd5..7b5b9385 100644 --- a/manual/manual.of +++ b/manual/manual.of | |||
| @@ -306,11 +306,14 @@ Lua calls this function to perform the addition. | |||
| 306 | 306 | ||
| 307 | The key for each event in a metatable is a string | 307 | The key for each event in a metatable is a string |
| 308 | with the event name prefixed by two underscores; | 308 | with the event name prefixed by two underscores; |
| 309 | the corresponding values are called @def{metamethods}. | 309 | the corresponding value is called a @def{metavalue}. |
| 310 | For most events, the metavalue must be a function, | ||
| 311 | which is then called a @def{metamethod}. | ||
| 310 | In the previous example, the key is the string @St{__add} | 312 | In the previous example, the key is the string @St{__add} |
| 311 | and the metamethod is the function that performs the addition. | 313 | and the metamethod is the function that performs the addition. |
| 312 | Unless stated otherwise, | 314 | Unless stated otherwise, |
| 313 | metamethods should be function values. | 315 | a metamethod may in fact be any @x{callable value}, |
| 316 | which is either a function or a value with a @id{__call} metamethod. | ||
| 314 | 317 | ||
| 315 | You can query the metatable of any value | 318 | You can query the metatable of any value |
| 316 | using the @Lid{getmetatable} function. | 319 | using the @Lid{getmetatable} function. |
| @@ -468,19 +471,19 @@ Behavior similar to the less than operation. | |||
| 468 | The indexing access operation @T{table[key]}. | 471 | The indexing access operation @T{table[key]}. |
| 469 | This event happens when @id{table} is not a table or | 472 | This event happens when @id{table} is not a table or |
| 470 | when @id{key} is not present in @id{table}. | 473 | when @id{key} is not present in @id{table}. |
| 471 | The metamethod is looked up in the metatable of @id{table}. | 474 | The metavalue is looked up in the metatable of @id{table}. |
| 472 | 475 | ||
| 473 | Despite the name, | 476 | The metavalue for this event can be either a function, a table, |
| 474 | the metamethod for this event can be either a function or a table. | 477 | or any value with an @id{__index} metavalue. |
| 475 | If it is a function, | 478 | If it is a function, |
| 476 | it is called with @id{table} and @id{key} as arguments, | 479 | it is called with @id{table} and @id{key} as arguments, |
| 477 | and the result of the call | 480 | and the result of the call |
| 478 | (adjusted to one value) | 481 | (adjusted to one value) |
| 479 | is the result of the operation. | 482 | is the result of the operation. |
| 480 | If it is a table, | 483 | Otherwise, |
| 481 | the final result is the result of indexing this table with @id{key}. | 484 | the final result is the result of indexing this metavalue with @id{key}. |
| 482 | This indexing is regular, not raw, | 485 | This indexing is regular, not raw, |
| 483 | and therefore can trigger another metamethod. | 486 | and therefore can trigger another @id{__index} metavalue. |
| 484 | } | 487 | } |
| 485 | 488 | ||
| 486 | @item{@idx{__newindex}| | 489 | @item{@idx{__newindex}| |
| @@ -488,18 +491,20 @@ The indexing assignment @T{table[key] = value}. | |||
| 488 | Like the index event, | 491 | Like the index event, |
| 489 | this event happens when @id{table} is not a table or | 492 | this event happens when @id{table} is not a table or |
| 490 | when @id{key} is not present in @id{table}. | 493 | when @id{key} is not present in @id{table}. |
| 491 | The metamethod is looked up in @id{table}. | 494 | The metavalue is looked up in the metatable of @id{table}. |
| 492 | 495 | ||
| 493 | Like with indexing, | 496 | Like with indexing, |
| 494 | the metamethod for this event can be either a function or a table. | 497 | the metavalue for this event can be either a function, a table, |
| 498 | or any value with an @id{__newindex} metavalue. | ||
| 495 | If it is a function, | 499 | If it is a function, |
| 496 | it is called with @id{table}, @id{key}, and @id{value} as arguments. | 500 | it is called with @id{table}, @id{key}, and @id{value} as arguments. |
| 497 | If it is a table, | 501 | Otherwise, |
| 498 | Lua does an indexing assignment to this table with the same key and value. | 502 | Lua repeats the indexing assignment over this metavalue |
| 503 | with the same key and value. | ||
| 499 | This assignment is regular, not raw, | 504 | This assignment is regular, not raw, |
| 500 | and therefore can trigger another metamethod. | 505 | and therefore can trigger another @id{__newindex} metavalue. |
| 501 | 506 | ||
| 502 | Whenever there is a @idx{__newindex} metamethod, | 507 | Whenever a @idx{__newindex} metavalue is invoked, |
| 503 | Lua does not perform the primitive assignment. | 508 | Lua does not perform the primitive assignment. |
| 504 | If needed, | 509 | If needed, |
| 505 | the metamethod itself can call @Lid{rawset} | 510 | the metamethod itself can call @Lid{rawset} |
| @@ -760,7 +765,7 @@ In any case, if either the key or the value is collected, | |||
| 760 | the whole pair is removed from the table. | 765 | the whole pair is removed from the table. |
| 761 | The weakness of a table is controlled by the | 766 | The weakness of a table is controlled by the |
| 762 | @idx{__mode} field of its metatable. | 767 | @idx{__mode} field of its metatable. |
| 763 | This field, if present, must be one of the following strings: | 768 | This metavalue, if present, must be one of the following strings: |
| 764 | @St{k}, for a table with weak keys; | 769 | @St{k}, for a table with weak keys; |
| 765 | @St{v}, for a table with weak values; | 770 | @St{v}, for a table with weak values; |
| 766 | or @St{kv}, for a table with both weak keys and values. | 771 | or @St{kv}, for a table with both weak keys and values. |
| @@ -3836,7 +3841,7 @@ Similar to @Lid{lua_gettable}, but does a raw access | |||
| 3836 | Pushes onto the stack the value @T{t[n]}, | 3841 | Pushes onto the stack the value @T{t[n]}, |
| 3837 | where @id{t} is the table at the given index. | 3842 | where @id{t} is the table at the given index. |
| 3838 | The access is raw, | 3843 | The access is raw, |
| 3839 | that is, it does not invoke the @idx{__index} metamethod. | 3844 | that is, it does not use the @idx{__index} metavalue. |
| 3840 | 3845 | ||
| 3841 | Returns the type of the pushed value. | 3846 | Returns the type of the pushed value. |
| 3842 | 3847 | ||
| @@ -3849,7 +3854,7 @@ Pushes onto the stack the value @T{t[k]}, | |||
| 3849 | where @id{t} is the table at the given index and | 3854 | where @id{t} is the table at the given index and |
| 3850 | @id{k} is the pointer @id{p} represented as a light userdata. | 3855 | @id{k} is the pointer @id{p} represented as a light userdata. |
| 3851 | The access is raw; | 3856 | The access is raw; |
| 3852 | that is, it does not invoke the @idx{__index} metamethod. | 3857 | that is, it does not use the @idx{__index} metavalue. |
| 3853 | 3858 | ||
| 3854 | Returns the type of the pushed value. | 3859 | Returns the type of the pushed value. |
| 3855 | 3860 | ||
| @@ -3885,7 +3890,7 @@ and @id{v} is the value on the top of the stack. | |||
| 3885 | 3890 | ||
| 3886 | This function pops the value from the stack. | 3891 | This function pops the value from the stack. |
| 3887 | The assignment is raw, | 3892 | The assignment is raw, |
| 3888 | that is, it does not invoke the @idx{__newindex} metamethod. | 3893 | that is, it does not use the @idx{__newindex} metavalue. |
| 3889 | 3894 | ||
| 3890 | } | 3895 | } |
| 3891 | 3896 | ||
| @@ -3899,7 +3904,7 @@ and @id{v} is the value on the top of the stack. | |||
| 3899 | 3904 | ||
| 3900 | This function pops the value from the stack. | 3905 | This function pops the value from the stack. |
| 3901 | The assignment is raw, | 3906 | The assignment is raw, |
| 3902 | that is, it does not invoke @idx{__newindex} metamethod. | 3907 | that is, it does not use the @idx{__newindex} metavalue. |
| 3903 | 3908 | ||
| 3904 | } | 3909 | } |
| 3905 | 3910 | ||
| @@ -6275,7 +6280,7 @@ Returns a boolean. | |||
| 6275 | 6280 | ||
| 6276 | @LibEntry{rawget (table, index)| | 6281 | @LibEntry{rawget (table, index)| |
| 6277 | Gets the real value of @T{table[index]}, | 6282 | Gets the real value of @T{table[index]}, |
| 6278 | without invoking the @idx{__index} metamethod. | 6283 | without using the @idx{__index} metavalue. |
| 6279 | @id{table} must be a table; | 6284 | @id{table} must be a table; |
| 6280 | @id{index} may be any value. | 6285 | @id{index} may be any value. |
| 6281 | 6286 | ||
| @@ -6291,7 +6296,7 @@ Returns an integer. | |||
| 6291 | 6296 | ||
| 6292 | @LibEntry{rawset (table, index, value)| | 6297 | @LibEntry{rawset (table, index, value)| |
| 6293 | Sets the real value of @T{table[index]} to @id{value}, | 6298 | Sets the real value of @T{table[index]} to @id{value}, |
| 6294 | without invoking the @idx{__newindex} metamethod. | 6299 | without using the @idx{__newindex} metavalue. |
| 6295 | @id{table} must be a table, | 6300 | @id{table} must be a table, |
| 6296 | @id{index} any value different from @nil and @x{NaN}, | 6301 | @id{index} any value different from @nil and @x{NaN}, |
| 6297 | and @id{value} any Lua value. | 6302 | and @id{value} any Lua value. |
