diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-12-28 15:25:45 -0200 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-12-28 15:25:45 -0200 |
| commit | a907aeeb1e4850cb6509a61bdf6a16bc42d68c4f (patch) | |
| tree | 8816080c594b975080df3c6becfcb5c092e34199 | |
| parent | 76f62fc5a140170164857a318eabc564fad3de6b (diff) | |
| download | lua-a907aeeb1e4850cb6509a61bdf6a16bc42d68c4f.tar.gz lua-a907aeeb1e4850cb6509a61bdf6a16bc42d68c4f.tar.bz2 lua-a907aeeb1e4850cb6509a61bdf6a16bc42d68c4f.zip | |
general corrections
| -rw-r--r-- | manual.tex | 179 |
1 files changed, 90 insertions, 89 deletions
| @@ -1,4 +1,4 @@ | |||
| 1 | % $Id: manual.tex,v 1.45 2000/10/31 18:20:01 roberto Exp roberto $ | 1 | % $Id: manual.tex,v 1.47 2000/11/14 18:46:09 roberto Exp roberto $ |
| 2 | 2 | ||
| 3 | \documentclass[11pt]{article} | 3 | \documentclass[11pt]{article} |
| 4 | \usepackage{fullpage} | 4 | \usepackage{fullpage} |
| @@ -134,7 +134,7 @@ Waldemar Celes | |||
| 134 | \tecgraf\ --- Computer Science Department --- PUC-Rio | 134 | \tecgraf\ --- Computer Science Department --- PUC-Rio |
| 135 | } | 135 | } |
| 136 | 136 | ||
| 137 | \date{{\small \tt\$Date: 2000/10/31 18:20:01 $ $}} | 137 | \date{{\small \tt\$Date: 2000/11/14 18:46:09 $ $}} |
| 138 | 138 | ||
| 139 | \maketitle | 139 | \maketitle |
| 140 | 140 | ||
| @@ -332,12 +332,6 @@ This means that functions can be stored in variables, | |||
| 332 | passed as arguments to other functions, and returned as results. | 332 | passed as arguments to other functions, and returned as results. |
| 333 | Lua can call (and manipulate) functions written in Lua and | 333 | Lua can call (and manipulate) functions written in Lua and |
| 334 | functions written in C. | 334 | functions written in C. |
| 335 | The two kinds of functions can be distinguished by their tags: | ||
| 336 | all Lua functions have the same tag, | ||
| 337 | and all C~functions have the same tag, | ||
| 338 | which is different from the tag of Lua functions. | ||
| 339 | The \verb|tag| function returns the tag | ||
| 340 | of a given value \see{pdf-tag}. | ||
| 341 | 335 | ||
| 342 | The type \emph{userdata} is provided to allow | 336 | The type \emph{userdata} is provided to allow |
| 343 | arbitrary \Index{C~pointers} to be stored in Lua variables. | 337 | arbitrary \Index{C~pointers} to be stored in Lua variables. |
| @@ -422,8 +416,10 @@ are reserved for internal variables. | |||
| 422 | 416 | ||
| 423 | The following strings denote other \Index{tokens}: | 417 | The following strings denote other \Index{tokens}: |
| 424 | \begin{verbatim} | 418 | \begin{verbatim} |
| 425 | ~= <= >= < > == = + - * / % | 419 | + - * / ^ % |
| 426 | ( ) { } [ ] ; , . .. ... | 420 | ~= <= >= < > == = |
| 421 | ( ) { } [ ] | ||
| 422 | ; : , . .. ... | ||
| 427 | \end{verbatim} | 423 | \end{verbatim} |
| 428 | 424 | ||
| 429 | \IndexEmph{Literal strings} | 425 | \IndexEmph{Literal strings} |
| @@ -659,7 +655,7 @@ The numerical \rwd{for} loop has the following syntax: | |||
| 659 | \end{Produc}% | 655 | \end{Produc}% |
| 660 | A \rwd{for} statement like | 656 | A \rwd{for} statement like |
| 661 | \begin{verbatim} | 657 | \begin{verbatim} |
| 662 | for var = e1 ,e2, e3 do block end | 658 | for var = e1, e2, e3 do block end |
| 663 | \end{verbatim} | 659 | \end{verbatim} |
| 664 | is equivalent to the code: | 660 | is equivalent to the code: |
| 665 | \begin{verbatim} | 661 | \begin{verbatim} |
| @@ -1568,16 +1564,17 @@ it does not have any global variables. | |||
| 1568 | \index{state} | 1564 | \index{state} |
| 1569 | The whole state of the Lua interpreter | 1565 | The whole state of the Lua interpreter |
| 1570 | (global variables, stack, tag methods, etc.) | 1566 | (global variables, stack, tag methods, etc.) |
| 1571 | is stored in a dynamically allocated structure of type \verb|lua_State|; \DefAPI{lua_State} | 1567 | is stored in a dynamically allocated structure of type \verb|lua_State|; |
| 1568 | \DefAPI{lua_State} | ||
| 1572 | this state must be passed as the first argument to | 1569 | this state must be passed as the first argument to |
| 1573 | every function in the library (except \verb|lua_open| below). | 1570 | every function in the library (except \verb|lua_open| below). |
| 1574 | 1571 | ||
| 1575 | Before calling any API function, | 1572 | Before calling any API function, |
| 1576 | you must create a state by calling | 1573 | you must create a state by calling |
| 1577 | \DefAPI{lua_open} | ||
| 1578 | \begin{verbatim} | 1574 | \begin{verbatim} |
| 1579 | lua_State *lua_open (int stacksize); | 1575 | lua_State *lua_open (int stacksize); |
| 1580 | \end{verbatim} | 1576 | \end{verbatim} |
| 1577 | \DefAPI{lua_open} | ||
| 1581 | The sole argument to this function is the stack size for the interpreter. | 1578 | The sole argument to this function is the stack size for the interpreter. |
| 1582 | (Each function call needs one stack position for each argument, local variable, | 1579 | (Each function call needs one stack position for each argument, local variable, |
| 1583 | and temporary value, plus one position for book-keeping. | 1580 | and temporary value, plus one position for book-keeping. |
| @@ -1588,10 +1585,10 @@ If \verb|stacksize| is zero, | |||
| 1588 | then a default size of~1024 is used. | 1585 | then a default size of~1024 is used. |
| 1589 | 1586 | ||
| 1590 | To release a state created with \verb|lua_open|, call | 1587 | To release a state created with \verb|lua_open|, call |
| 1591 | \DefAPI{lua_close} | ||
| 1592 | \begin{verbatim} | 1588 | \begin{verbatim} |
| 1593 | void lua_close (lua_State *L); | 1589 | void lua_close (lua_State *L); |
| 1594 | \end{verbatim} | 1590 | \end{verbatim} |
| 1591 | \DefAPI{lua_close} | ||
| 1595 | This function destroys all objects in the given Lua environment | 1592 | This function destroys all objects in the given Lua environment |
| 1596 | (calling the corresponding garbage-collection tag methods, if any) | 1593 | (calling the corresponding garbage-collection tag methods, if any) |
| 1597 | and frees all dynamic memory used by that state. | 1594 | and frees all dynamic memory used by that state. |
| @@ -1628,25 +1625,26 @@ index~\Math{-1} also represents the last element | |||
| 1628 | (that is, the element at the top), | 1625 | (that is, the element at the top), |
| 1629 | and index \Math{-n} represents the first element. | 1626 | and index \Math{-n} represents the first element. |
| 1630 | We say that an index is \emph{valid} | 1627 | We say that an index is \emph{valid} |
| 1631 | if it lays between~1 and the stack top | 1628 | if it lies between~1 and the stack top |
| 1632 | (that is, if \verb|1 <= abs(index) <= top|). | 1629 | (that is, if \verb|1 <= abs(index) <= top|). |
| 1633 | \index{stack index} \index{valid index} | 1630 | \index{stack index} \index{valid index} |
| 1634 | 1631 | ||
| 1635 | At any time, you can get the index of the top element by calling | 1632 | At any time, you can get the index of the top element by calling |
| 1636 | \DefAPI{lua_gettop} | ||
| 1637 | \begin{verbatim} | 1633 | \begin{verbatim} |
| 1638 | int lua_gettop (lua_State *L); | 1634 | int lua_gettop (lua_State *L); |
| 1639 | \end{verbatim} | 1635 | \end{verbatim} |
| 1636 | \DefAPI{lua_gettop} | ||
| 1640 | Because indices start at~1, | 1637 | Because indices start at~1, |
| 1641 | the result of \verb|lua_gettop| is equal to the number of elements in the stack | 1638 | the result of \verb|lua_gettop| is equal to the number of elements in the stack |
| 1642 | (and so 0~means an empty stack). | 1639 | (and so 0~means an empty stack). |
| 1643 | 1640 | ||
| 1644 | When you interact with Lua API, | 1641 | When you interact with Lua API, |
| 1645 | \emph{you are responsible for controlling stack overflow}. | 1642 | \emph{you are responsible for controlling stack overflow}. |
| 1646 | The function \DefAPI{lua_stackspace} | 1643 | The function |
| 1647 | \begin{verbatim} | 1644 | \begin{verbatim} |
| 1648 | int lua_stackspace (lua_State *L); | 1645 | int lua_stackspace (lua_State *L); |
| 1649 | \end{verbatim} | 1646 | \end{verbatim} |
| 1647 | \DefAPI{lua_stackspace} | ||
| 1650 | returns the number of stack positions still available. | 1648 | returns the number of stack positions still available. |
| 1651 | Whenever Lua calls C, \DefAPI{LUA_MINSTACK} | 1649 | Whenever Lua calls C, \DefAPI{LUA_MINSTACK} |
| 1652 | it ensures that | 1650 | it ensures that |
| @@ -1667,14 +1665,14 @@ Note that 0 is not an acceptable index. | |||
| 1667 | 1665 | ||
| 1668 | \subsection{Stack Manipulation} | 1666 | \subsection{Stack Manipulation} |
| 1669 | The API offers the following functions for basic stack manipulation: | 1667 | The API offers the following functions for basic stack manipulation: |
| 1670 | \DefAPI{lua_settop}\DefAPI{lua_pushvalue} | ||
| 1671 | \DefAPI{lua_remove}\DefAPI{lua_insert} | ||
| 1672 | \begin{verbatim} | 1668 | \begin{verbatim} |
| 1673 | void lua_settop (lua_State *L, int index); | 1669 | void lua_settop (lua_State *L, int index); |
| 1674 | void lua_pushvalue (lua_State *L, int index); | 1670 | void lua_pushvalue (lua_State *L, int index); |
| 1675 | void lua_remove (lua_State *L, int index); | 1671 | void lua_remove (lua_State *L, int index); |
| 1676 | void lua_insert (lua_State *L, int index); | 1672 | void lua_insert (lua_State *L, int index); |
| 1677 | \end{verbatim} | 1673 | \end{verbatim} |
| 1674 | \DefAPI{lua_settop}\DefAPI{lua_pushvalue} | ||
| 1675 | \DefAPI{lua_remove}\DefAPI{lua_insert} | ||
| 1678 | 1676 | ||
| 1679 | \verb|lua_settop| accepts any acceptable index, | 1677 | \verb|lua_settop| accepts any acceptable index, |
| 1680 | or 0, | 1678 | or 0, |
| @@ -1686,6 +1684,7 @@ A useful macro defined in the API is | |||
| 1686 | \begin{verbatim} | 1684 | \begin{verbatim} |
| 1687 | #define lua_pop(L,n) lua_settop(L, -(n)-1) | 1685 | #define lua_pop(L,n) lua_settop(L, -(n)-1) |
| 1688 | \end{verbatim} | 1686 | \end{verbatim} |
| 1687 | \DefAPI{lua_pop} | ||
| 1689 | which pops \verb|n| elements from the stack. | 1688 | which pops \verb|n| elements from the stack. |
| 1690 | 1689 | ||
| 1691 | \verb|lua_pushvalue| pushes onto the stack a \emph{copy} of the element | 1690 | \verb|lua_pushvalue| pushes onto the stack a \emph{copy} of the element |
| @@ -1714,10 +1713,6 @@ then | |||
| 1714 | 1713 | ||
| 1715 | To check the type of a stack element, | 1714 | To check the type of a stack element, |
| 1716 | the following functions are available: | 1715 | the following functions are available: |
| 1717 | \DefAPI{lua_type}\DefAPI{lua_tag} | ||
| 1718 | \DefAPI{lua_isnil}\DefAPI{lua_isnumber}\DefAPI{lua_isstring} | ||
| 1719 | \DefAPI{lua_istable} | ||
| 1720 | \DefAPI{lua_isfunction}\DefAPI{lua_iscfunction}\DefAPI{lua_isuserdata} | ||
| 1721 | \begin{verbatim} | 1716 | \begin{verbatim} |
| 1722 | int lua_type (lua_State *L, int index); | 1717 | int lua_type (lua_State *L, int index); |
| 1723 | int lua_tag (lua_State *L, int index); | 1718 | int lua_tag (lua_State *L, int index); |
| @@ -1729,7 +1724,13 @@ the following functions are available: | |||
| 1729 | int lua_iscfunction (lua_State *L, int index); | 1724 | int lua_iscfunction (lua_State *L, int index); |
| 1730 | int lua_isuserdata (lua_State *L, int index); | 1725 | int lua_isuserdata (lua_State *L, int index); |
| 1731 | \end{verbatim} | 1726 | \end{verbatim} |
| 1727 | \DefAPI{lua_type}\DefAPI{lua_tag} | ||
| 1728 | \DefAPI{lua_isnil}\DefAPI{lua_isnumber}\DefAPI{lua_isstring} | ||
| 1729 | \DefAPI{lua_istable} | ||
| 1730 | \DefAPI{lua_isfunction}\DefAPI{lua_iscfunction}\DefAPI{lua_isuserdata} | ||
| 1732 | These functions can be called with any acceptable index. | 1731 | These functions can be called with any acceptable index. |
| 1732 | The \verb|lua_isnumber| function may have a side effect of changing the | ||
| 1733 | actual value in the stack from a string to a number. | ||
| 1733 | 1734 | ||
| 1734 | \verb|lua_type| returns one of the following constants, | 1735 | \verb|lua_type| returns one of the following constants, |
| 1735 | according to the type of the given object: | 1736 | according to the type of the given object: |
| @@ -1743,10 +1744,10 @@ If the index is non-valid | |||
| 1743 | (that is, if that stack position is ``empty''), | 1744 | (that is, if that stack position is ``empty''), |
| 1744 | then \verb|lua_type| returns \verb|LUA_TNONE|. | 1745 | then \verb|lua_type| returns \verb|LUA_TNONE|. |
| 1745 | These constants can be converted to strings with | 1746 | These constants can be converted to strings with |
| 1746 | \DefAPI{lua_typename} | ||
| 1747 | \begin{verbatim} | 1747 | \begin{verbatim} |
| 1748 | const char *lua_typename (lua_State *L, int t); | 1748 | const char *lua_typename (lua_State *L, int t); |
| 1749 | \end{verbatim} | 1749 | \end{verbatim} |
| 1750 | \DefAPI{lua_typename} | ||
| 1750 | where \verb|t| is a type returned by \verb|lua_type|. | 1751 | where \verb|t| is a type returned by \verb|lua_type|. |
| 1751 | The strings returned by \verb|lua_typename| are | 1752 | The strings returned by \verb|lua_typename| are |
| 1752 | \verb|"nil"|, \verb|"number"|, \verb|"string"|, \verb|"table"|, | 1753 | \verb|"nil"|, \verb|"number"|, \verb|"string"|, \verb|"table"|, |
| @@ -1754,6 +1755,8 @@ The strings returned by \verb|lua_typename| are | |||
| 1754 | 1755 | ||
| 1755 | \verb|lua_tag| returns the tag of a value, | 1756 | \verb|lua_tag| returns the tag of a value, |
| 1756 | or \verb|LUA_NOTAG| for a non-valid index. | 1757 | or \verb|LUA_NOTAG| for a non-valid index. |
| 1758 | The default tag value for all types is equal to the value | ||
| 1759 | returned by \verb|lua_type|. | ||
| 1757 | 1760 | ||
| 1758 | The \verb|lua_is*| functions return~1 if the object is compatible | 1761 | The \verb|lua_is*| functions return~1 if the object is compatible |
| 1759 | with the given type, and 0 otherwise. | 1762 | with the given type, and 0 otherwise. |
| @@ -1767,12 +1770,11 @@ To distinguish between numbers and numerical strings, | |||
| 1767 | you can use \verb|lua_type|. | 1770 | you can use \verb|lua_type|. |
| 1768 | 1771 | ||
| 1769 | The API also has functions to compare two values in the stack: | 1772 | The API also has functions to compare two values in the stack: |
| 1770 | \DefAPI{lua_equal} | ||
| 1771 | \DefAPI{lua_lessthan} | ||
| 1772 | \begin{verbatim} | 1773 | \begin{verbatim} |
| 1773 | int lua_equal (lua_State *L, int index1, int index2); | 1774 | int lua_equal (lua_State *L, int index1, int index2); |
| 1774 | int lua_lessthan (lua_State *L, int index1, int index2); | 1775 | int lua_lessthan (lua_State *L, int index1, int index2); |
| 1775 | \end{verbatim} | 1776 | \end{verbatim} |
| 1777 | \DefAPI{lua_equal} \DefAPI{lua_lessthan} | ||
| 1776 | These functions are equivalent to their counterparts in Lua. | 1778 | These functions are equivalent to their counterparts in Lua. |
| 1777 | Specifically, \verb|lua_lessthan| is equivalent to the \verb|lt_event| | 1779 | Specifically, \verb|lua_lessthan| is equivalent to the \verb|lt_event| |
| 1778 | described in \See{tag-method}. | 1780 | described in \See{tag-method}. |
| @@ -1780,8 +1782,6 @@ Both functions return 0 if any of the indices are non-valid. | |||
| 1780 | 1782 | ||
| 1781 | To translate a value in the stack to a specific C~type, | 1783 | To translate a value in the stack to a specific C~type, |
| 1782 | you can use the following conversion functions: | 1784 | you can use the following conversion functions: |
| 1783 | \DefAPI{lua_tonumber}\DefAPI{lua_tostring}\DefAPI{lua_strlen} | ||
| 1784 | \DefAPI{lua_tocfunction}\DefAPI{lua_touserdata} | ||
| 1785 | \begin{verbatim} | 1785 | \begin{verbatim} |
| 1786 | double lua_tonumber (lua_State *L, int index); | 1786 | double lua_tonumber (lua_State *L, int index); |
| 1787 | const char *lua_tostring (lua_State *L, int index); | 1787 | const char *lua_tostring (lua_State *L, int index); |
| @@ -1789,6 +1789,8 @@ you can use the following conversion functions: | |||
| 1789 | lua_CFunction lua_tocfunction (lua_State *L, int index); | 1789 | lua_CFunction lua_tocfunction (lua_State *L, int index); |
| 1790 | void *lua_touserdata (lua_State *L, int index); | 1790 | void *lua_touserdata (lua_State *L, int index); |
| 1791 | \end{verbatim} | 1791 | \end{verbatim} |
| 1792 | \DefAPI{lua_tonumber}\DefAPI{lua_tostring}\DefAPI{lua_strlen} | ||
| 1793 | \DefAPI{lua_tocfunction}\DefAPI{lua_touserdata} | ||
| 1792 | These functions can be called with any acceptable index. | 1794 | These functions can be called with any acceptable index. |
| 1793 | When called with a non-valid index, | 1795 | When called with a non-valid index, |
| 1794 | they act as if the given value had an incorrect type. | 1796 | they act as if the given value had an incorrect type. |
| @@ -1797,13 +1799,20 @@ they act as if the given value had an incorrect type. | |||
| 1797 | to a floating-point number. | 1799 | to a floating-point number. |
| 1798 | This value must be a number or a string convertible to number | 1800 | This value must be a number or a string convertible to number |
| 1799 | \see{coercion}; otherwise, \verb|lua_tonumber| returns~0. | 1801 | \see{coercion}; otherwise, \verb|lua_tonumber| returns~0. |
| 1802 | If the value is a string, | ||
| 1803 | \verb|lua_tonumber| also changes the | ||
| 1804 | actual value in the stack to a number. | ||
| 1800 | 1805 | ||
| 1801 | \verb|lua_tostring| converts a Lua value to a string | 1806 | \verb|lua_tostring| converts a Lua value to a string |
| 1802 | (\verb|const char*|). | 1807 | (\verb|const char*|). |
| 1803 | This value must be a string or a number; | 1808 | This value must be a string or a number; |
| 1804 | otherwise, the function returns \verb|NULL|. | 1809 | otherwise, the function returns \verb|NULL|. |
| 1810 | If the value is a number, | ||
| 1811 | \verb|lua_tostring| also changes the | ||
| 1812 | actual value in the stack to a string. | ||
| 1805 | This function returns a pointer to a string inside the Lua environment. | 1813 | This function returns a pointer to a string inside the Lua environment. |
| 1806 | Those strings always have a zero (\verb|'\0'|) after their last character (as in C), | 1814 | Those strings always have a zero (\verb|'\0'|) |
| 1815 | after their last character (as in C), | ||
| 1807 | but may contain other zeros in their body. | 1816 | but may contain other zeros in their body. |
| 1808 | If you do not know whether a string may contain zeros, | 1817 | If you do not know whether a string may contain zeros, |
| 1809 | you should use \verb|lua_strlen| to get its actual length. | 1818 | you should use \verb|lua_strlen| to get its actual length. |
| @@ -1826,9 +1835,6 @@ otherwise, \verb|lua_touserdata| returns \verb|NULL|. | |||
| 1826 | 1835 | ||
| 1827 | The API has the following functions to | 1836 | The API has the following functions to |
| 1828 | push C~values onto the stack: | 1837 | push C~values onto the stack: |
| 1829 | \DefAPI{lua_pushnumber}\DefAPI{lua_pushlstring}\DefAPI{lua_pushstring} | ||
| 1830 | \DefAPI{lua_pushcfunction}\DefAPI{lua_pushusertag} | ||
| 1831 | \DefAPI{lua_pushnil}\DefAPI{lua_pushuserdata}\label{pushing} | ||
| 1832 | \begin{verbatim} | 1838 | \begin{verbatim} |
| 1833 | void lua_pushnumber (lua_State *L, double n); | 1839 | void lua_pushnumber (lua_State *L, double n); |
| 1834 | void lua_pushlstring (lua_State *L, const char *s, size_t len); | 1840 | void lua_pushlstring (lua_State *L, const char *s, size_t len); |
| @@ -1837,6 +1843,9 @@ push C~values onto the stack: | |||
| 1837 | void lua_pushnil (lua_State *L); | 1843 | void lua_pushnil (lua_State *L); |
| 1838 | void lua_pushcfunction (lua_State *L, lua_CFunction f); | 1844 | void lua_pushcfunction (lua_State *L, lua_CFunction f); |
| 1839 | \end{verbatim} | 1845 | \end{verbatim} |
| 1846 | \DefAPI{lua_pushnumber}\DefAPI{lua_pushlstring}\DefAPI{lua_pushstring} | ||
| 1847 | \DefAPI{lua_pushcfunction}\DefAPI{lua_pushusertag} | ||
| 1848 | \DefAPI{lua_pushnil}\DefAPI{lua_pushuserdata}\label{pushing} | ||
| 1840 | These functions receive a C~value, | 1849 | These functions receive a C~value, |
| 1841 | convert it to a corresponding Lua value, | 1850 | convert it to a corresponding Lua value, |
| 1842 | and push the result onto the stack. | 1851 | and push the result onto the stack. |
| @@ -1853,8 +1862,6 @@ which accepts an explicit size. | |||
| 1853 | Lua uses two numbers to control its garbage collection. | 1862 | Lua uses two numbers to control its garbage collection. |
| 1854 | One number counts how many bytes of dynamic memory Lua is using, | 1863 | One number counts how many bytes of dynamic memory Lua is using, |
| 1855 | and the other is a threshold. | 1864 | and the other is a threshold. |
| 1856 | (This internal byte counter kept by Lua is not completely acurate; | ||
| 1857 | it is just a lower bound, usually within~10\% of the correct value.) | ||
| 1858 | When the number of bytes crosses the threshold, | 1865 | When the number of bytes crosses the threshold, |
| 1859 | Lua runs a garbage-collection cycle, | 1866 | Lua runs a garbage-collection cycle, |
| 1860 | which reclaims the memory of all ``dead'' objects | 1867 | which reclaims the memory of all ``dead'' objects |
| @@ -1864,17 +1871,17 @@ and then the threshold is reset to twice the value of the byte counter. | |||
| 1864 | 1871 | ||
| 1865 | You can access the current values of these two numbers through the | 1872 | You can access the current values of these two numbers through the |
| 1866 | following functions: | 1873 | following functions: |
| 1867 | \DefAPI{lua_getgcthreshold} \DefAPI{lua_getgccount} | ||
| 1868 | \begin{verbatim} | 1874 | \begin{verbatim} |
| 1869 | int lua_getgccount (lua_State *L); | 1875 | int lua_getgccount (lua_State *L); |
| 1870 | int lua_getgcthreshold (lua_State *L); | 1876 | int lua_getgcthreshold (lua_State *L); |
| 1871 | \end{verbatim} | 1877 | \end{verbatim} |
| 1878 | \DefAPI{lua_getgcthreshold} \DefAPI{lua_getgccount} | ||
| 1872 | Both return their respective values in Kbytes. | 1879 | Both return their respective values in Kbytes. |
| 1873 | You can change the threshold value with | 1880 | You can change the threshold value with |
| 1874 | \DefAPI{lua_setgcthreshold} | ||
| 1875 | \begin{verbatim} | 1881 | \begin{verbatim} |
| 1876 | void lua_setgcthreshold (lua_State *L, int newthreshold); | 1882 | void lua_setgcthreshold (lua_State *L, int newthreshold); |
| 1877 | \end{verbatim} | 1883 | \end{verbatim} |
| 1884 | \DefAPI{lua_setgcthreshold} | ||
| 1878 | Again, the \verb|newthreshold| value is given in Kbytes. | 1885 | Again, the \verb|newthreshold| value is given in Kbytes. |
| 1879 | When you call this function, | 1886 | When you call this function, |
| 1880 | Lua sets the new threshold and checks it against the byte counter. | 1887 | Lua sets the new threshold and checks it against the byte counter. |
| @@ -1883,7 +1890,7 @@ then Lua immediately runs the garbage collector; | |||
| 1883 | after the collection, | 1890 | after the collection, |
| 1884 | a new threshold is set according to the previous rule. | 1891 | a new threshold is set according to the previous rule. |
| 1885 | 1892 | ||
| 1886 | If you want to change the adaptative behavior of the garbage collector, | 1893 | If you want to change the adaptive behavior of the garbage collector, |
| 1887 | you can use the garbage-collection tag method for \nil\ % | 1894 | you can use the garbage-collection tag method for \nil\ % |
| 1888 | to set your own threshold | 1895 | to set your own threshold |
| 1889 | (the tag method is called after Lua resets the threshold). | 1896 | (the tag method is called after Lua resets the threshold). |
| @@ -1906,29 +1913,29 @@ with tag equal to 0. | |||
| 1906 | Userdata can have different tags, | 1913 | Userdata can have different tags, |
| 1907 | whose semantics are only known to the host program. | 1914 | whose semantics are only known to the host program. |
| 1908 | Tags are created with the function | 1915 | Tags are created with the function |
| 1909 | \DefAPI{lua_newtag} | ||
| 1910 | \begin{verbatim} | 1916 | \begin{verbatim} |
| 1911 | int lua_newtag (lua_State *L); | 1917 | int lua_newtag (lua_State *L); |
| 1912 | \end{verbatim} | 1918 | \end{verbatim} |
| 1919 | \DefAPI{lua_newtag} | ||
| 1913 | The function \verb|lua_settag| changes the tag of | 1920 | The function \verb|lua_settag| changes the tag of |
| 1914 | the object on top of the stack (without popping it): | 1921 | the object on top of the stack (without popping it): |
| 1915 | \DefAPI{lua_settag} | ||
| 1916 | \begin{verbatim} | 1922 | \begin{verbatim} |
| 1917 | void lua_settag (lua_State *L, int tag); | 1923 | void lua_settag (lua_State *L, int tag); |
| 1918 | \end{verbatim} | 1924 | \end{verbatim} |
| 1925 | \DefAPI{lua_settag} | ||
| 1919 | The object must be a userdata or a table; | 1926 | The object must be a userdata or a table; |
| 1920 | the given \verb|tag| must be a value created with \verb|lua_newtag|. | 1927 | the given \verb|tag| must be a value created with \verb|lua_newtag|. |
| 1921 | 1928 | ||
| 1922 | \subsection{Executing Lua Code}\label{luado} | 1929 | \subsection{Executing Lua Code}\label{luado} |
| 1923 | A host program can execute Lua chunks written in a file or in a string | 1930 | A host program can execute Lua chunks written in a file or in a string |
| 1924 | by using the following functions:% | 1931 | by using the following functions:% |
| 1925 | \DefAPI{lua_dofile}\DefAPI{lua_dostring}\DefAPI{lua_dobuffer}% | ||
| 1926 | \begin{verbatim} | 1932 | \begin{verbatim} |
| 1927 | int lua_dofile (lua_State *L, const char *filename); | 1933 | int lua_dofile (lua_State *L, const char *filename); |
| 1928 | int lua_dostring (lua_State *L, const char *string); | 1934 | int lua_dostring (lua_State *L, const char *string); |
| 1929 | int lua_dobuffer (lua_State *L, const char *buff, | 1935 | int lua_dobuffer (lua_State *L, const char *buff, |
| 1930 | size_t size, const char *name); | 1936 | size_t size, const char *name); |
| 1931 | \end{verbatim} | 1937 | \end{verbatim} |
| 1938 | \DefAPI{lua_dofile}\DefAPI{lua_dostring}\DefAPI{lua_dobuffer}% | ||
| 1932 | These functions return | 1939 | These functions return |
| 1933 | 0 in case of success, or one of the following error codes if they fail: | 1940 | 0 in case of success, or one of the following error codes if they fail: |
| 1934 | \begin{itemize} | 1941 | \begin{itemize} |
| @@ -1994,10 +2001,10 @@ leaving the stack as it was before the call: | |||
| 1994 | 2001 | ||
| 1995 | To read the value of a global Lua variable, | 2002 | To read the value of a global Lua variable, |
| 1996 | you call | 2003 | you call |
| 1997 | \DefAPI{lua_getglobal} | ||
| 1998 | \begin{verbatim} | 2004 | \begin{verbatim} |
| 1999 | void lua_getglobal (lua_State *L, const char *varname); | 2005 | void lua_getglobal (lua_State *L, const char *varname); |
| 2000 | \end{verbatim} | 2006 | \end{verbatim} |
| 2007 | \DefAPI{lua_getglobal} | ||
| 2001 | which pushes onto the stack the value of the given variable. | 2008 | which pushes onto the stack the value of the given variable. |
| 2002 | As in Lua, this function may trigger a tag method | 2009 | As in Lua, this function may trigger a tag method |
| 2003 | for the ``getglobal'' event \see{tag-method}. | 2010 | for the ``getglobal'' event \see{tag-method}. |
| @@ -2008,10 +2015,10 @@ use \verb|lua_rawget| over the table of globals | |||
| 2008 | 2015 | ||
| 2009 | To store a value in a global variable, | 2016 | To store a value in a global variable, |
| 2010 | you call | 2017 | you call |
| 2011 | \DefAPI{lua_setglobal} | ||
| 2012 | \begin{verbatim} | 2018 | \begin{verbatim} |
| 2013 | void lua_setglobal (lua_State *L, const char *varname); | 2019 | void lua_setglobal (lua_State *L, const char *varname); |
| 2014 | \end{verbatim} | 2020 | \end{verbatim} |
| 2021 | \DefAPI{lua_setglobal} | ||
| 2015 | which pops from the stack the value to be stored in the given variable. | 2022 | which pops from the stack the value to be stored in the given variable. |
| 2016 | As in Lua, this function may trigger a tag method | 2023 | As in Lua, this function may trigger a tag method |
| 2017 | for the ``setglobal'' event \see{tag-method}. | 2024 | for the ``setglobal'' event \see{tag-method}. |
| @@ -2022,30 +2029,27 @@ use \verb|lua_rawset| over the table of globals | |||
| 2022 | 2029 | ||
| 2023 | All global variables are kept in an ordinary Lua table. | 2030 | All global variables are kept in an ordinary Lua table. |
| 2024 | You can get this table calling | 2031 | You can get this table calling |
| 2025 | \DefAPI{lua_getglobals} | ||
| 2026 | \begin{verbatim} | 2032 | \begin{verbatim} |
| 2027 | void lua_getglobals (lua_State *L); | 2033 | void lua_getglobals (lua_State *L); |
| 2028 | \end{verbatim} | 2034 | \end{verbatim} |
| 2035 | \DefAPI{lua_getglobals} | ||
| 2029 | which pushes the current table of globals onto the stack. | 2036 | which pushes the current table of globals onto the stack. |
| 2030 | To set another table as the table of globals, | 2037 | To set another table as the table of globals, |
| 2031 | you call | 2038 | you call |
| 2032 | \DefAPI{lua_setglobals} | ||
| 2033 | \begin{verbatim} | 2039 | \begin{verbatim} |
| 2034 | void lua_setglobals (lua_State *L); | 2040 | void lua_setglobals (lua_State *L); |
| 2035 | \end{verbatim} | 2041 | \end{verbatim} |
| 2042 | \DefAPI{lua_setglobals} | ||
| 2036 | The table to be used is popped from the stack. | 2043 | The table to be used is popped from the stack. |
| 2037 | 2044 | ||
| 2038 | \subsection{Manipulating Tables in Lua} | 2045 | \subsection{Manipulating Tables in Lua} |
| 2039 | Lua tables can also be manipulated through the API. | 2046 | Lua tables can also be manipulated through the API. |
| 2040 | 2047 | ||
| 2041 | To read the value of in a table, | 2048 | To read a value from a table, call |
| 2042 | the table must reside somewhere in the stack. | ||
| 2043 | With this set, | ||
| 2044 | you call | ||
| 2045 | \DefAPI{lua_gettable} | ||
| 2046 | \begin{verbatim} | 2049 | \begin{verbatim} |
| 2047 | void lua_gettable (lua_State *L, int index); | 2050 | void lua_gettable (lua_State *L, int index); |
| 2048 | \end{verbatim} | 2051 | \end{verbatim} |
| 2052 | \DefAPI{lua_gettable} | ||
| 2049 | where \verb|index| refers to the table. | 2053 | where \verb|index| refers to the table. |
| 2050 | \verb|lua_gettable| pops a key from the stack, | 2054 | \verb|lua_gettable| pops a key from the stack, |
| 2051 | and returns (on the stack) the contents of the table at that key. | 2055 | and returns (on the stack) the contents of the table at that key. |
| @@ -2054,19 +2058,19 @@ for the ``gettable'' event. | |||
| 2054 | To get the real value of any table key, | 2058 | To get the real value of any table key, |
| 2055 | without invoking any tag method, | 2059 | without invoking any tag method, |
| 2056 | use the \emph{raw} version: | 2060 | use the \emph{raw} version: |
| 2057 | \DefAPI{lua_rawget} | ||
| 2058 | \begin{verbatim} | 2061 | \begin{verbatim} |
| 2059 | void lua_rawget (lua_State *L, int index); | 2062 | void lua_rawget (lua_State *L, int index); |
| 2060 | \end{verbatim} | 2063 | \end{verbatim} |
| 2064 | \DefAPI{lua_rawget} | ||
| 2061 | 2065 | ||
| 2062 | To store a value into a table that resides somewhere in the stack, | 2066 | To store a value into a table that resides somewhere in the stack, |
| 2063 | you push the key and the value onto the stack | 2067 | you push the key and the value onto the stack |
| 2064 | (in this order), | 2068 | (in this order), |
| 2065 | and then call | 2069 | and then call |
| 2066 | \DefAPI{lua_settable} | ||
| 2067 | \begin{verbatim} | 2070 | \begin{verbatim} |
| 2068 | void lua_settable (lua_State *L, int index); | 2071 | void lua_settable (lua_State *L, int index); |
| 2069 | \end{verbatim} | 2072 | \end{verbatim} |
| 2073 | \DefAPI{lua_settable} | ||
| 2070 | where \verb|index| refers to the table. | 2074 | where \verb|index| refers to the table. |
| 2071 | \verb|lua_settable| pops from the stack both the key and the value. | 2075 | \verb|lua_settable| pops from the stack both the key and the value. |
| 2072 | As in Lua, this operation may trigger a tag method | 2076 | As in Lua, this operation may trigger a tag method |
| @@ -2074,30 +2078,30 @@ for the ``settable'' event. | |||
| 2074 | To set the real value of any table index, | 2078 | To set the real value of any table index, |
| 2075 | without invoking any tag method, | 2079 | without invoking any tag method, |
| 2076 | use the \emph{raw} version: | 2080 | use the \emph{raw} version: |
| 2077 | \DefAPI{lua_rawset} | ||
| 2078 | \begin{verbatim} | 2081 | \begin{verbatim} |
| 2079 | void lua_rawset (lua_State *L, int index); | 2082 | void lua_rawset (lua_State *L, int index); |
| 2080 | \end{verbatim} | 2083 | \end{verbatim} |
| 2084 | \DefAPI{lua_rawset} | ||
| 2081 | 2085 | ||
| 2082 | Finally, the function | 2086 | Finally, the function |
| 2083 | \DefAPI{lua_newtable} | ||
| 2084 | \begin{verbatim} | 2087 | \begin{verbatim} |
| 2085 | void lua_newtable (lua_State *L); | 2088 | void lua_newtable (lua_State *L); |
| 2086 | \end{verbatim} | 2089 | \end{verbatim} |
| 2090 | \DefAPI{lua_newtable} | ||
| 2087 | creates a new, empty table and pushes it onto the stack. | 2091 | creates a new, empty table and pushes it onto the stack. |
| 2088 | 2092 | ||
| 2089 | \subsection{Using Tables as Arrays} | 2093 | \subsection{Using Tables as Arrays} |
| 2090 | The API has functions that help to use Lua tables as arrays, | 2094 | The API has functions that help to use Lua tables as arrays, |
| 2091 | that is, | 2095 | that is, |
| 2092 | tables indexed by numbers only: | 2096 | tables indexed by numbers only: |
| 2093 | \DefAPI{lua_rawgeti} | ||
| 2094 | \DefAPI{lua_rawseti} | ||
| 2095 | \DefAPI{lua_getn} | ||
| 2096 | \begin{verbatim} | 2097 | \begin{verbatim} |
| 2097 | void lua_rawgeti (lua_State *L, int index, int n); | 2098 | void lua_rawgeti (lua_State *L, int index, int n); |
| 2098 | void lua_rawseti (lua_State *L, int index, int n); | 2099 | void lua_rawseti (lua_State *L, int index, int n); |
| 2099 | int lua_getn (lua_State *L, int index); | 2100 | int lua_getn (lua_State *L, int index); |
| 2100 | \end{verbatim} | 2101 | \end{verbatim} |
| 2102 | \DefAPI{lua_rawgeti} | ||
| 2103 | \DefAPI{lua_rawseti} | ||
| 2104 | \DefAPI{lua_getn} | ||
| 2101 | 2105 | ||
| 2102 | \verb|lua_rawgeti| gets the value of the \M{n}-th element of the table | 2106 | \verb|lua_rawgeti| gets the value of the \M{n}-th element of the table |
| 2103 | at stack position \verb|index|. | 2107 | at stack position \verb|index|. |
| @@ -2122,19 +2126,19 @@ First, the function to be called is pushed onto the stack; | |||
| 2122 | then, the arguments to the function are pushed | 2126 | then, the arguments to the function are pushed |
| 2123 | \see{pushing} in \emph{direct order}, that is, the first argument is pushed first. | 2127 | \see{pushing} in \emph{direct order}, that is, the first argument is pushed first. |
| 2124 | Finally, the function is called using | 2128 | Finally, the function is called using |
| 2125 | \DefAPI{lua_call} | ||
| 2126 | \begin{verbatim} | 2129 | \begin{verbatim} |
| 2127 | int lua_call (lua_State *L, int nargs, int nresults); | 2130 | int lua_call (lua_State *L, int nargs, int nresults); |
| 2128 | \end{verbatim} | 2131 | \end{verbatim} |
| 2132 | \DefAPI{lua_call} | ||
| 2129 | This function returns the same error codes as \verb|lua_dostring| and | 2133 | This function returns the same error codes as \verb|lua_dostring| and |
| 2130 | friends \see{luado}. | 2134 | friends \see{luado}. |
| 2131 | If you want to propagate the error, | 2135 | If you want to propagate the error, |
| 2132 | instead of returning an error code, | 2136 | instead of returning an error code, |
| 2133 | use | 2137 | use |
| 2134 | \DefAPI{lua_rawcall} | ||
| 2135 | \begin{verbatim} | 2138 | \begin{verbatim} |
| 2136 | void lua_rawcall (lua_State *L, int nargs, int nresults); | 2139 | void lua_rawcall (lua_State *L, int nargs, int nresults); |
| 2137 | \end{verbatim} | 2140 | \end{verbatim} |
| 2141 | \DefAPI{lua_rawcall} | ||
| 2138 | 2142 | ||
| 2139 | In both functions, | 2143 | In both functions, |
| 2140 | \verb|nargs| is the number of arguments that you pushed onto the stack. | 2144 | \verb|nargs| is the number of arguments that you pushed onto the stack. |
| @@ -2174,10 +2178,10 @@ This is considered good programming practice. | |||
| 2174 | 2178 | ||
| 2175 | Some special Lua functions have their own C~interfaces. | 2179 | Some special Lua functions have their own C~interfaces. |
| 2176 | The host program can generate a Lua error calling the function | 2180 | The host program can generate a Lua error calling the function |
| 2177 | \DefAPI{lua_error} | ||
| 2178 | \begin{verbatim} | 2181 | \begin{verbatim} |
| 2179 | void lua_error (lua_State *L, const char *message); | 2182 | void lua_error (lua_State *L, const char *message); |
| 2180 | \end{verbatim} | 2183 | \end{verbatim} |
| 2184 | \DefAPI{lua_error} | ||
| 2181 | This function never returns. | 2185 | This function never returns. |
| 2182 | If \verb|lua_error| is called from a C~function that has been called from Lua, | 2186 | If \verb|lua_error| is called from a C~function that has been called from Lua, |
| 2183 | then the corresponding Lua execution terminates, | 2187 | then the corresponding Lua execution terminates, |
| @@ -2192,32 +2196,36 @@ then \verb|_ERRORMESSAGE| is not called. | |||
| 2192 | 2196 | ||
| 2193 | \medskip | 2197 | \medskip |
| 2194 | 2198 | ||
| 2195 | Tag methods can be changed with \DefAPI{lua_settagmethod} | 2199 | Tag methods can be changed with |
| 2196 | \begin{verbatim} | 2200 | \begin{verbatim} |
| 2197 | void lua_settagmethod (lua_State *L, int tag, const char *event); | 2201 | void lua_settagmethod (lua_State *L, int tag, const char *event); |
| 2198 | \end{verbatim} | 2202 | \end{verbatim} |
| 2203 | \DefAPI{lua_settagmethod} | ||
| 2199 | The second parameter is the tag, | 2204 | The second parameter is the tag, |
| 2200 | and the third is the event name \see{tag-method}; | 2205 | and the third is the event name \see{tag-method}; |
| 2201 | the new method is popped from the stack. | 2206 | the new method is popped from the stack. |
| 2202 | To get the current value of a tag method, | 2207 | To get the current value of a tag method, |
| 2203 | use the function \DefAPI{lua_gettagmethod} | 2208 | use the function |
| 2204 | \begin{verbatim} | 2209 | \begin{verbatim} |
| 2205 | void lua_gettagmethod (lua_State *L, int tag, const char *event); | 2210 | void lua_gettagmethod (lua_State *L, int tag, const char *event); |
| 2206 | \end{verbatim} | 2211 | \end{verbatim} |
| 2212 | \DefAPI{lua_gettagmethod} | ||
| 2207 | 2213 | ||
| 2208 | It is also possible to copy all tag methods from one tag | 2214 | It is also possible to copy all tag methods from one tag |
| 2209 | to another: \DefAPI{lua_copytagmethods} | 2215 | to another: |
| 2210 | \begin{verbatim} | 2216 | \begin{verbatim} |
| 2211 | int lua_copytagmethods (lua_State *L, int tagto, int tagfrom); | 2217 | int lua_copytagmethods (lua_State *L, int tagto, int tagfrom); |
| 2212 | \end{verbatim} | 2218 | \end{verbatim} |
| 2219 | \DefAPI{lua_copytagmethods} | ||
| 2213 | This function returns \verb|tagto|. | 2220 | This function returns \verb|tagto|. |
| 2214 | 2221 | ||
| 2215 | \medskip | 2222 | \medskip |
| 2216 | 2223 | ||
| 2217 | You can traverse a table with the function \DefAPI{lua_next} | 2224 | You can traverse a table with the function |
| 2218 | \begin{verbatim} | 2225 | \begin{verbatim} |
| 2219 | int lua_next (lua_State *L, int index); | 2226 | int lua_next (lua_State *L, int index); |
| 2220 | \end{verbatim} | 2227 | \end{verbatim} |
| 2228 | \DefAPI{lua_next} | ||
| 2221 | where \verb|index| refers to the table to be traversed. | 2229 | where \verb|index| refers to the table to be traversed. |
| 2222 | The function pops a key from the stack, | 2230 | The function pops a key from the stack, |
| 2223 | and pushes a key-value pair from the table | 2231 | and pushes a key-value pair from the table |
| @@ -2236,10 +2244,11 @@ A typical traversal looks like this: | |||
| 2236 | } | 2244 | } |
| 2237 | \end{verbatim} | 2245 | \end{verbatim} |
| 2238 | 2246 | ||
| 2239 | The function \DefAPI{lua_concat} | 2247 | The function |
| 2240 | \begin{verbatim} | 2248 | \begin{verbatim} |
| 2241 | void lua_concat (lua_State *L, int n); | 2249 | void lua_concat (lua_State *L, int n); |
| 2242 | \end{verbatim} | 2250 | \end{verbatim} |
| 2251 | \DefAPI{lua_concat} | ||
| 2243 | concatenates the \verb|n| values at the top of the stack, | 2252 | concatenates the \verb|n| values at the top of the stack, |
| 2244 | pops them, and leaves the result at the top; | 2253 | pops them, and leaves the result at the top; |
| 2245 | \verb|n|~must be at least 2. | 2254 | \verb|n|~must be at least 2. |
| @@ -2250,20 +2259,20 @@ Concatenation is done following the usual semantics of Lua | |||
| 2250 | \subsection{Defining C Functions} \label{LuacallC} | 2259 | \subsection{Defining C Functions} \label{LuacallC} |
| 2251 | To register a C~function to Lua, | 2260 | To register a C~function to Lua, |
| 2252 | there is the following convenience macro: | 2261 | there is the following convenience macro: |
| 2253 | \DefAPI{lua_register} | ||
| 2254 | \begin{verbatim} | 2262 | \begin{verbatim} |
| 2255 | #define lua_register(L, n, f) (lua_pushcfunction(L, f), lua_setglobal(L, n)) | 2263 | #define lua_register(L, n, f) (lua_pushcfunction(L, f), lua_setglobal(L, n)) |
| 2256 | /* const char *n; */ | 2264 | /* const char *n; */ |
| 2257 | /* lua_CFunction f; */ | 2265 | /* lua_CFunction f; */ |
| 2258 | \end{verbatim} | 2266 | \end{verbatim} |
| 2267 | \DefAPI{lua_register} | ||
| 2259 | which receives the name the function will have in Lua, | 2268 | which receives the name the function will have in Lua, |
| 2260 | and a pointer to the function. | 2269 | and a pointer to the function. |
| 2261 | This pointer must have type \verb|lua_CFunction|, | 2270 | This pointer must have type \verb|lua_CFunction|, |
| 2262 | which is defined as | 2271 | which is defined as |
| 2263 | \DefAPI{lua_CFunction} | ||
| 2264 | \begin{verbatim} | 2272 | \begin{verbatim} |
| 2265 | typedef int (*lua_CFunction) (lua_State *L); | 2273 | typedef int (*lua_CFunction) (lua_State *L); |
| 2266 | \end{verbatim} | 2274 | \end{verbatim} |
| 2275 | \DefAPI{lua_CFunction} | ||
| 2267 | that is, a pointer to a function with integer result and a single argument, | 2276 | that is, a pointer to a function with integer result and a single argument, |
| 2268 | a Lua environment. | 2277 | a Lua environment. |
| 2269 | 2278 | ||
| @@ -2309,10 +2318,11 @@ these values are passed to the function whenever it is called, | |||
| 2309 | as ordinary arguments. | 2318 | as ordinary arguments. |
| 2310 | To associate upvalues to a C~function, | 2319 | To associate upvalues to a C~function, |
| 2311 | first these values should be pushed onto the stack. | 2320 | first these values should be pushed onto the stack. |
| 2312 | Then the function \DefAPI{lua_pushcclosure} | 2321 | Then the function |
| 2313 | \begin{verbatim} | 2322 | \begin{verbatim} |
| 2314 | void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n); | 2323 | void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n); |
| 2315 | \end{verbatim} | 2324 | \end{verbatim} |
| 2325 | \DefAPI{lua_pushcclosure} | ||
| 2316 | is used to push the C~function onto the stack, | 2326 | is used to push the C~function onto the stack, |
| 2317 | with the argument \verb|n| telling how many upvalues should be | 2327 | with the argument \verb|n| telling how many upvalues should be |
| 2318 | associated with the function | 2328 | associated with the function |
| @@ -2337,13 +2347,12 @@ If the C~code needs to keep a Lua value | |||
| 2337 | outside the life span of a C~function, | 2347 | outside the life span of a C~function, |
| 2338 | then it must create a \Def{reference} to the value. | 2348 | then it must create a \Def{reference} to the value. |
| 2339 | The functions to manipulate references are the following: | 2349 | The functions to manipulate references are the following: |
| 2340 | \DefAPI{lua_ref}\DefAPI{lua_getref} | ||
| 2341 | \DefAPI{lua_unref} | ||
| 2342 | \begin{verbatim} | 2350 | \begin{verbatim} |
| 2343 | int lua_ref (lua_State *L, int lock); | 2351 | int lua_ref (lua_State *L, int lock); |
| 2344 | int lua_getref (lua_State *L, int ref); | 2352 | int lua_getref (lua_State *L, int ref); |
| 2345 | void lua_unref (lua_State *L, int ref); | 2353 | void lua_unref (lua_State *L, int ref); |
| 2346 | \end{verbatim} | 2354 | \end{verbatim} |
| 2355 | \DefAPI{lua_ref}\DefAPI{lua_getref}\DefAPI{lua_unref} | ||
| 2347 | 2356 | ||
| 2348 | \verb|lua_ref| pops a value from | 2357 | \verb|lua_ref| pops a value from |
| 2349 | the stack, creates a reference to it, | 2358 | the stack, creates a reference to it, |
| @@ -2371,10 +2380,11 @@ it should be released with a call to \verb|lua_unref|. | |||
| 2371 | 2380 | ||
| 2372 | When Lua starts, it registers a table at position | 2381 | When Lua starts, it registers a table at position |
| 2373 | \IndexAPI{LUA_REFREGISTRY}. | 2382 | \IndexAPI{LUA_REFREGISTRY}. |
| 2374 | It can be accessed through the macro\DefAPI{lua_getregistry} | 2383 | It can be accessed through the macro |
| 2375 | \begin{verbatim} | 2384 | \begin{verbatim} |
| 2376 | #define lua_getregistry(L) lua_getref(L, LUA_REFREGISTRY) | 2385 | #define lua_getregistry(L) lua_getref(L, LUA_REFREGISTRY) |
| 2377 | \end{verbatim} | 2386 | \end{verbatim} |
| 2387 | \DefAPI{lua_getregistry} | ||
| 2378 | This table can be used by C~libraries as a general registry mechanism. | 2388 | This table can be used by C~libraries as a general registry mechanism. |
| 2379 | Any C~library can store data into this table, | 2389 | Any C~library can store data into this table, |
| 2380 | as long as it chooses a key different from other libraries. | 2390 | as long as it chooses a key different from other libraries. |
| @@ -2710,7 +2720,7 @@ use function \verb|format|. | |||
| 2710 | 2720 | ||
| 2711 | 2721 | ||
| 2712 | 2722 | ||
| 2713 | \subsubsection*{\ff \T{tinsert (table [, pos] , value)}}\DefLIB{tinsert} | 2723 | \subsubsection*{\ff \T{tinsert (table, [pos,] value)}}\DefLIB{tinsert} |
| 2714 | 2724 | ||
| 2715 | Inserts element \verb|value| at table position \verb|pos|, | 2725 | Inserts element \verb|value| at table position \verb|pos|, |
| 2716 | shifting other elements to open space, if necessary. | 2726 | shifting other elements to open space, if necessary. |
| @@ -2788,7 +2798,7 @@ such as finding and extracting substrings and pattern matching. | |||
| 2788 | When indexing a string in Lua, the first character is at position~1 | 2798 | When indexing a string in Lua, the first character is at position~1 |
| 2789 | (not at~0, as in C). | 2799 | (not at~0, as in C). |
| 2790 | Also, | 2800 | Also, |
| 2791 | indices are allowed to be negative and are intepreted as indexing backwards, | 2801 | indices are allowed to be negative and are interpreted as indexing backwards, |
| 2792 | from the end of the string. Thus, the last character is at position \Math{-1}, | 2802 | from the end of the string. Thus, the last character is at position \Math{-1}, |
| 2793 | and so on. | 2803 | and so on. |
| 2794 | 2804 | ||
| @@ -2888,16 +2898,6 @@ will produce the string: | |||
| 2888 | new line" | 2898 | new line" |
| 2889 | \end{verbatim} | 2899 | \end{verbatim} |
| 2890 | 2900 | ||
| 2891 | Conversions can be applied to the \M{n}-th argument in the argument list, | ||
| 2892 | rather than the next unused argument. | ||
| 2893 | In this case, the conversion character \verb|%| is replaced | ||
| 2894 | by the sequence \verb|%d$|, where \verb|d| is a | ||
| 2895 | decimal digit in the range [1,9], | ||
| 2896 | giving the position of the argument in the argument list. | ||
| 2897 | For instance, the call \verb|format("%2$d -> %1$03d", 1, 34)| will | ||
| 2898 | result in \verb|"34 -> 001"|. | ||
| 2899 | The same argument can be used in more than one conversion. | ||
| 2900 | |||
| 2901 | The options \verb|c|, \verb|d|, \verb|E|, \verb|e|, \verb|f|, | 2901 | The options \verb|c|, \verb|d|, \verb|E|, \verb|e|, \verb|f|, |
| 2902 | \verb|g|, \verb|G|, \verb|i|, \verb|o|, \verb|u|, \verb|X|, and \verb|x| all | 2902 | \verb|g|, \verb|G|, \verb|i|, \verb|o|, \verb|u|, \verb|X|, and \verb|x| all |
| 2903 | expect a number as argument, | 2903 | expect a number as argument, |
| @@ -3220,7 +3220,7 @@ usually limited and depends on the system. | |||
| 3220 | 3220 | ||
| 3221 | \subsubsection*{\ff \T{appendto (filename)}}\DefLIB{appendto} | 3221 | \subsubsection*{\ff \T{appendto (filename)}}\DefLIB{appendto} |
| 3222 | 3222 | ||
| 3223 | Opens a file named \verb|filename| and sets it as the | 3223 | Opens a file named \verb|filename| and sets its handle as the |
| 3224 | value of \verb|_OUTPUT|. | 3224 | value of \verb|_OUTPUT|. |
| 3225 | Unlike the \verb|writeto| operation, | 3225 | Unlike the \verb|writeto| operation, |
| 3226 | this function does not erase any previous contents of the file; | 3226 | this function does not erase any previous contents of the file; |
| @@ -3377,11 +3377,11 @@ This interface is declared in \verb|luadebug.h|. | |||
| 3377 | 3377 | ||
| 3378 | \subsection{Stack and Function Information} | 3378 | \subsection{Stack and Function Information} |
| 3379 | 3379 | ||
| 3380 | \DefAPI{lua_getstack} | ||
| 3381 | The main function to get information about the interpreter stack is | 3380 | The main function to get information about the interpreter stack is |
| 3382 | \begin{verbatim} | 3381 | \begin{verbatim} |
| 3383 | int lua_getstack (lua_State *L, int level, lua_Debug *ar); | 3382 | int lua_getstack (lua_State *L, int level, lua_Debug *ar); |
| 3384 | \end{verbatim} | 3383 | \end{verbatim} |
| 3384 | \DefAPI{lua_getstack} | ||
| 3385 | It fills parts of a \verb|lua_Debug| structure with | 3385 | It fills parts of a \verb|lua_Debug| structure with |
| 3386 | an identification of the \emph{activation record} | 3386 | an identification of the \emph{activation record} |
| 3387 | of the function executing at a given level. | 3387 | of the function executing at a given level. |
| @@ -3391,7 +3391,6 @@ Usually, \verb|lua_getstack| returns 1; | |||
| 3391 | when called with a level greater than the stack depth, | 3391 | when called with a level greater than the stack depth, |
| 3392 | it returns 0. | 3392 | it returns 0. |
| 3393 | 3393 | ||
| 3394 | \DefAPI{lua_Debug} | ||
| 3395 | The structure \verb|lua_Debug| is used to carry different pieces of | 3394 | The structure \verb|lua_Debug| is used to carry different pieces of |
| 3396 | information about an active function: | 3395 | information about an active function: |
| 3397 | \begin{verbatim} | 3396 | \begin{verbatim} |
| @@ -3410,13 +3409,15 @@ information about an active function: | |||
| 3410 | ... | 3409 | ... |
| 3411 | } lua_Debug; | 3410 | } lua_Debug; |
| 3412 | \end{verbatim} | 3411 | \end{verbatim} |
| 3412 | \DefAPI{lua_Debug} | ||
| 3413 | \verb|lua_getstack| fills only the private part | 3413 | \verb|lua_getstack| fills only the private part |
| 3414 | of this structure, for future use. | 3414 | of this structure, for future use. |
| 3415 | To fill in the other fields of \verb|lua_Debug| with useful information, | 3415 | To fill in the other fields of \verb|lua_Debug| with useful information, |
| 3416 | call \DefAPI{lua_getinfo} | 3416 | call |
| 3417 | \begin{verbatim} | 3417 | \begin{verbatim} |
| 3418 | int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar); | 3418 | int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar); |
| 3419 | \end{verbatim} | 3419 | \end{verbatim} |
| 3420 | \DefAPI{lua_getinfo} | ||
| 3420 | This function returns 0 on error | 3421 | This function returns 0 on error |
| 3421 | (e.g., an invalid option in \verb|what|). | 3422 | (e.g., an invalid option in \verb|what|). |
| 3422 | Each character in the string \verb|what| | 3423 | Each character in the string \verb|what| |
| @@ -3500,13 +3501,13 @@ For the manipulation of local variables, | |||
| 3500 | The first parameter or local variable has index~1, and so on, | 3501 | The first parameter or local variable has index~1, and so on, |
| 3501 | until the last active local variable. | 3502 | until the last active local variable. |
| 3502 | 3503 | ||
| 3503 | \DefAPI{lua_getlocal}\DefAPI{lua_setlocal} | ||
| 3504 | The following functions allow the manipulation of the | 3504 | The following functions allow the manipulation of the |
| 3505 | local variables of a given activation record. | 3505 | local variables of a given activation record. |
| 3506 | \begin{verbatim} | 3506 | \begin{verbatim} |
| 3507 | const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n); | 3507 | const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n); |
| 3508 | const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n); | 3508 | const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n); |
| 3509 | \end{verbatim} | 3509 | \end{verbatim} |
| 3510 | \DefAPI{lua_getlocal}\DefAPI{lua_setlocal} | ||
| 3510 | The parameter \verb|ar| must be a valid activation record, | 3511 | The parameter \verb|ar| must be a valid activation record, |
| 3511 | filled by a previous call to \verb|lua_getstack| or | 3512 | filled by a previous call to \verb|lua_getstack| or |
| 3512 | given as argument to a hook \see{sub-hooks}. | 3513 | given as argument to a hook \see{sub-hooks}. |
| @@ -3543,16 +3544,16 @@ local variables for a function at a given level of the stack: | |||
| 3543 | The Lua interpreter offers two hooks for debugging purposes: | 3544 | The Lua interpreter offers two hooks for debugging purposes: |
| 3544 | a \emph{call} hook and a \emph{line} hook. | 3545 | a \emph{call} hook and a \emph{line} hook. |
| 3545 | Both have the same type, | 3546 | Both have the same type, |
| 3546 | \DefAPI{lua_Hook} | ||
| 3547 | \begin{verbatim} | 3547 | \begin{verbatim} |
| 3548 | typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar); | 3548 | typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar); |
| 3549 | \end{verbatim} | 3549 | \end{verbatim} |
| 3550 | \DefAPI{lua_Hook} | ||
| 3550 | and you can set them with the following functions: | 3551 | and you can set them with the following functions: |
| 3551 | \DefAPI{lua_setcallhook}\DefAPI{lua_setlinehook} | ||
| 3552 | \begin{verbatim} | 3552 | \begin{verbatim} |
| 3553 | lua_Hook lua_setcallhook (lua_State *L, lua_Hook func); | 3553 | lua_Hook lua_setcallhook (lua_State *L, lua_Hook func); |
| 3554 | lua_Hook lua_setlinehook (lua_State *L, lua_Hook func); | 3554 | lua_Hook lua_setlinehook (lua_State *L, lua_Hook func); |
| 3555 | \end{verbatim} | 3555 | \end{verbatim} |
| 3556 | \DefAPI{lua_setcallhook}\DefAPI{lua_setlinehook} | ||
| 3556 | A hook is disabled when its value is \verb|NULL|, | 3557 | A hook is disabled when its value is \verb|NULL|, |
| 3557 | which is the initial value of both hooks. | 3558 | which is the initial value of both hooks. |
| 3558 | The functions \verb|lua_setcallhook| and \verb|lua_setlinehook| | 3559 | The functions \verb|lua_setcallhook| and \verb|lua_setlinehook| |
