diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1999-03-29 11:21:37 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1999-03-29 11:21:37 -0300 |
| commit | e0ff4e5d22bd2dbe74dd588f0710c18ae8a671ab (patch) | |
| tree | 8e6d13aecaef1cd40a0a95b14201572becdeb43e | |
| parent | bf7f85d609291023bcc53df862198f5877ca00d6 (diff) | |
| download | lua-e0ff4e5d22bd2dbe74dd588f0710c18ae8a671ab.tar.gz lua-e0ff4e5d22bd2dbe74dd588f0710c18ae8a671ab.tar.bz2 lua-e0ff4e5d22bd2dbe74dd588f0710c18ae8a671ab.zip | |
first version about Lua debug library.
Diffstat (limited to '')
| -rw-r--r-- | manual.tex | 210 |
1 files changed, 172 insertions, 38 deletions
| @@ -1,4 +1,4 @@ | |||
| 1 | % $Id: manual.tex,v 1.26 1999/03/10 14:09:45 roberto Exp roberto $ | 1 | % $Id: manual.tex,v 1.27 1999/03/11 19:00:12 roberto Exp roberto $ |
| 2 | 2 | ||
| 3 | \documentclass[11pt]{article} | 3 | \documentclass[11pt]{article} |
| 4 | \usepackage{fullpage,bnf} | 4 | \usepackage{fullpage,bnf} |
| @@ -41,7 +41,7 @@ Waldemar Celes | |||
| 41 | \tecgraf\ --- Computer Science Department --- PUC-Rio | 41 | \tecgraf\ --- Computer Science Department --- PUC-Rio |
| 42 | } | 42 | } |
| 43 | 43 | ||
| 44 | %\date{\small \verb$Date: 1999/03/10 14:09:45 $} | 44 | %\date{\small \verb$Date: 1999/03/11 19:00:12 $} |
| 45 | 45 | ||
| 46 | \maketitle | 46 | \maketitle |
| 47 | 47 | ||
| @@ -2624,8 +2624,8 @@ When called with two arguments, \Math{l} and \Math{u}, | |||
| 2624 | 2624 | ||
| 2625 | \subsection{I/O Facilities} \label{libio} | 2625 | \subsection{I/O Facilities} \label{libio} |
| 2626 | 2626 | ||
| 2627 | All input and output operations in Lua are done over two | 2627 | All input and output operations in Lua are done, by default, |
| 2628 | \Def{file handles}, one for reading and one for writing. | 2628 | over two \Def{file handles}, one for reading and one for writing. |
| 2629 | These handles are stored in two Lua global variables, | 2629 | These handles are stored in two Lua global variables, |
| 2630 | called \verb|_INPUT| and \verb|_OUTPUT|. | 2630 | called \verb|_INPUT| and \verb|_OUTPUT|. |
| 2631 | The global variables | 2631 | The global variables |
| @@ -2638,20 +2638,45 @@ Initially, \verb|_INPUT=_STDIN| and \verb|_OUTPUT=_STDOUT|. | |||
| 2638 | 2638 | ||
| 2639 | A file handle is a userdata containing the file stream \verb|FILE*|, | 2639 | A file handle is a userdata containing the file stream \verb|FILE*|, |
| 2640 | and with a distinctive tag created by the I/O library. | 2640 | and with a distinctive tag created by the I/O library. |
| 2641 | 2641 | Whenever a file handle is collected by the garbage collector, | |
| 2642 | its correspondent stream is automatically closed. | ||
| 2642 | 2643 | ||
| 2643 | Unless otherwise stated, | 2644 | Unless otherwise stated, |
| 2644 | all I/O functions return \nil\ on failure and | 2645 | all I/O functions return \nil\ on failure and |
| 2645 | some value different from \nil\ on success. | 2646 | some value different from \nil\ on success. |
| 2646 | 2647 | ||
| 2647 | \subsubsection*{\ff \T{readfrom (filename [, mode])}}\Deffunc{readfrom} | 2648 | \subsubsection*{\ff \T{openfile (filename, mode)}}\Deffunc{openfile} |
| 2649 | |||
| 2650 | This function opens a file, | ||
| 2651 | in the mode specified in the string \verb|mode|. | ||
| 2652 | It returns a new file handle, | ||
| 2653 | or, in case of errors, \nil\ plus a string describing the error. | ||
| 2654 | This function does not modify neither \verb|_INPUT| nor \verb|_OUTPUT|. | ||
| 2655 | |||
| 2656 | The string mode can be any of the following: | ||
| 2657 | \begin{description} | ||
| 2658 | \item["r"] read mode; | ||
| 2659 | \item["w"] write mode; | ||
| 2660 | \item["a"] append mode; | ||
| 2661 | \item["r+"] update mode, all previous data is preserved; | ||
| 2662 | \item["w+"] update mode, all previous data is erased; | ||
| 2663 | \item["a+"] append update mode, previous data is preserved, | ||
| 2664 | writing is only allowed at the end of file. | ||
| 2665 | \end{description} | ||
| 2666 | The string mode may also have a \verb|b| at the end, | ||
| 2667 | which is needed in some systems to open the file in binary mode. | ||
| 2668 | |||
| 2669 | \subsubsection*{\ff \T{closefile (handle)}}\Deffunc{closefile} | ||
| 2670 | |||
| 2671 | This function closes the given file. | ||
| 2672 | It does not modify neither \verb|_INPUT| nor \verb|_OUTPUT|. | ||
| 2673 | |||
| 2674 | \subsubsection*{\ff \T{readfrom (filename)}}\Deffunc{readfrom} | ||
| 2648 | 2675 | ||
| 2649 | This function may be called in two ways. | 2676 | This function may be called in two ways. |
| 2650 | When called with a file name, it opens the named file, | 2677 | When called with a file name, it opens the named file, |
| 2651 | sets its handle as the value of \verb|_INPUT|, | 2678 | sets its handle as the value of \verb|_INPUT|, |
| 2652 | and returns this value. | 2679 | and returns this value. |
| 2653 | An optional \verb|mode| argument with the string \verb|"binary"| | ||
| 2654 | opens file in binary mode (where this applies). | ||
| 2655 | It does not close the current input file. | 2680 | It does not close the current input file. |
| 2656 | When called without parameters, | 2681 | When called without parameters, |
| 2657 | it closes the \verb|_INPUT| file, | 2682 | it closes the \verb|_INPUT| file, |
| @@ -2670,15 +2695,13 @@ the number of files that can be open at the same time is | |||
| 2670 | usually limited and depends on the system. | 2695 | usually limited and depends on the system. |
| 2671 | \end{quotation} | 2696 | \end{quotation} |
| 2672 | 2697 | ||
| 2673 | \subsubsection*{\ff \T{writeto (filename [, mode])}}\Deffunc{writeto} | 2698 | \subsubsection*{\ff \T{writeto (filename)}}\Deffunc{writeto} |
| 2674 | 2699 | ||
| 2675 | This function may be called in two ways. | 2700 | This function may be called in two ways. |
| 2676 | When called with a file name, | 2701 | When called with a file name, |
| 2677 | it opens the named file, | 2702 | it opens the named file, |
| 2678 | sets its handle as the value of \verb|_OUTPUT|, | 2703 | sets its handle as the value of \verb|_OUTPUT|, |
| 2679 | and returns this value. | 2704 | and returns this value. |
| 2680 | An optional \verb|mode| argument with the string \verb|"binary"| | ||
| 2681 | opens file in binary mode (where this applies). | ||
| 2682 | It does not close the current output file. | 2705 | It does not close the current output file. |
| 2683 | Note that, if the file already exists, | 2706 | Note that, if the file already exists, |
| 2684 | then it will be \emph{completely erased} with this operation. | 2707 | then it will be \emph{completely erased} with this operation. |
| @@ -2700,20 +2723,15 @@ the number of files that can be open at the same time is | |||
| 2700 | usually limited and depends on the system. | 2723 | usually limited and depends on the system. |
| 2701 | \end{quotation} | 2724 | \end{quotation} |
| 2702 | 2725 | ||
| 2703 | \subsubsection*{\ff \T{appendto (filename [, mode])}}\Deffunc{appendto} | 2726 | \subsubsection*{\ff \T{appendto (filename)}}\Deffunc{appendto} |
| 2704 | 2727 | ||
| 2705 | Opens a file named \verb|filename| and sets it as the | 2728 | Opens a file named \verb|filename| and sets it as the |
| 2706 | value of \verb|_OUTPUT|. | 2729 | value of \verb|_OUTPUT|. |
| 2707 | An optional \verb|mode| argument with the string \verb|"binary"| | ||
| 2708 | opens file in binary mode (where this applies). | ||
| 2709 | Unlike the \verb|writeto| operation, | 2730 | Unlike the \verb|writeto| operation, |
| 2710 | this function does not erase any previous content of the file. | 2731 | this function does not erase any previous content of the file. |
| 2711 | If this function fails, it returns \nil, | 2732 | If this function fails, it returns \nil, |
| 2712 | plus a string describing the error. | 2733 | plus a string describing the error. |
| 2713 | 2734 | ||
| 2714 | Note that function \verb|writeto| is | ||
| 2715 | available to close an output file opened by \verb|appendto|. | ||
| 2716 | |||
| 2717 | \subsubsection*{\ff \T{remove (filename)}}\Deffunc{remove} | 2735 | \subsubsection*{\ff \T{remove (filename)}}\Deffunc{remove} |
| 2718 | 2736 | ||
| 2719 | Deletes the file with the given name. | 2737 | Deletes the file with the given name. |
| @@ -2741,9 +2759,9 @@ measured in bytes from the beginning of the file, | |||
| 2741 | to the position given by \verb|offset| plus a base | 2759 | to the position given by \verb|offset| plus a base |
| 2742 | specified by the string \verb|whence|, as follows: | 2760 | specified by the string \verb|whence|, as follows: |
| 2743 | \begin{description} | 2761 | \begin{description} |
| 2744 | \item[\tt "set"] base is position 0 (beginning of the file); | 2762 | \item["set"] base is position 0 (beginning of the file); |
| 2745 | \item[\tt "cur"] base is current position; | 2763 | \item["cur"] base is current position; |
| 2746 | \item[\tt "end"] base is end of file; | 2764 | \item["end"] base is end of file; |
| 2747 | \end{description} | 2765 | \end{description} |
| 2748 | In case of success, function \verb|seek| returns the final file position, | 2766 | In case of success, function \verb|seek| returns the final file position, |
| 2749 | measured in bytes from the beginning of the file. | 2767 | measured in bytes from the beginning of the file. |
| @@ -2765,18 +2783,17 @@ Returns a string with a file name that can safely | |||
| 2765 | be used for a temporary file. | 2783 | be used for a temporary file. |
| 2766 | The file must be explicitly removed when no longer needed. | 2784 | The file must be explicitly removed when no longer needed. |
| 2767 | 2785 | ||
| 2768 | \subsubsection*{\ff \T{read ([filehandle] [readpattern])}}\Deffunc{read} | 2786 | \subsubsection*{\ff \T{read ([filehandle] {readpattern})}}\Deffunc{read} |
| 2769 | 2787 | ||
| 2770 | Reads file \verb|_INPUT|, | 2788 | Reads file \verb|_INPUT|, |
| 2771 | or \verb|filehandle| if this argument is given, | 2789 | or \verb|filehandle| if this argument is given, |
| 2772 | according to a read pattern, which specifies how much to read; | 2790 | according to read patterns, which specify how much to read. |
| 2773 | characters are read from the input file until | 2791 | For each pattern, |
| 2774 | the read pattern fails or ends. | 2792 | the function returns a string with the characters read, |
| 2775 | The function \verb|read| returns a string with the characters read, | ||
| 2776 | even if the pattern succeeds only partially, | 2793 | even if the pattern succeeds only partially, |
| 2777 | or \nil\ if the read pattern fails \emph{and} | 2794 | or \nil\ if the read pattern fails \emph{and} |
| 2778 | the result string would be empty. | 2795 | the result string would be empty. |
| 2779 | When called without parameters, | 2796 | When called without patterns, |
| 2780 | it uses a default pattern that reads the next line | 2797 | it uses a default pattern that reads the next line |
| 2781 | (see below). | 2798 | (see below). |
| 2782 | 2799 | ||
| @@ -2803,20 +2820,21 @@ that describe \Def{skips}. | |||
| 2803 | Characters matching a skip are read, | 2820 | Characters matching a skip are read, |
| 2804 | but are not included in the resulting string. | 2821 | but are not included in the resulting string. |
| 2805 | 2822 | ||
| 2806 | Following are some examples of read patterns and their meanings: | 2823 | There are some predefined patterns, as follows: |
| 2807 | \begin{itemize} | 2824 | \begin{description} |
| 2808 | \item \verb|"."| returns the next character, or \nil\ on end of file. | 2825 | \item["*n"] reads a number; |
| 2809 | \item \verb|".*"| reads the whole file. | 2826 | this is the only pattern that returns a number instead of a string. |
| 2810 | \item \verb|"[^\n]*{\n}"| returns the next line | 2827 | \item["*l"] returns the next line |
| 2811 | (skipping the end of line), or \nil\ on end of file. | 2828 | (skipping the end of line), or \nil\ on end of file. |
| 2812 | This is the default pattern. | 2829 | This is the default pattern. |
| 2813 | \item \verb|"{%s*}%S%S*"| returns the next word | 2830 | It is equivalent to the pattern \verb|"[^\n]*{\n}"|. |
| 2831 | \item["*a"] reads the whole file. | ||
| 2832 | It is equivalent to the pattern \verb|".*"|. | ||
| 2833 | \item["*w"] returns the next word | ||
| 2814 | (maximal sequence of non white-space characters), | 2834 | (maximal sequence of non white-space characters), |
| 2815 | skipping spaces if necessary, | 2835 | skipping spaces if necessary, or \nil\ on end of file. |
| 2816 | or \nil\ on end of file. | 2836 | It is equivalent to the pattern \verb|"{%s*}%S%S*"|. |
| 2817 | \item \verb|"{%s*}[+-]?%d%d*"| returns the next integer | 2837 | \end{description} |
| 2818 | or \nil\ if the next characters do not conform to an integer format. | ||
| 2819 | \end{itemize} | ||
| 2820 | 2838 | ||
| 2821 | \subsubsection*{\ff \T{write ([filehandle, ] value1, ...)}}\Deffunc{write} | 2839 | \subsubsection*{\ff \T{write ([filehandle, ] value1, ...)}}\Deffunc{write} |
| 2822 | 2840 | ||
| @@ -3010,7 +3028,7 @@ the line of code it is executing. | |||
| 3010 | Its only parameter is the line number | 3028 | Its only parameter is the line number |
| 3011 | (the same information which is provided by the call | 3029 | (the same information which is provided by the call |
| 3012 | \verb|lua_currentline(lua_stackedfunction(0))|). | 3030 | \verb|lua_currentline(lua_stackedfunction(0))|). |
| 3013 | This second hook is only called if the active function | 3031 | This second hook is called only if the active function |
| 3014 | has been compiled with debug information \see{pragma}. | 3032 | has been compiled with debug information \see{pragma}. |
| 3015 | 3033 | ||
| 3016 | A hook is disabled when its value is \verb|NULL|, | 3034 | A hook is disabled when its value is \verb|NULL|, |
| @@ -3020,6 +3038,122 @@ set their corresponding hooks and return their previous values. | |||
| 3020 | 3038 | ||
| 3021 | 3039 | ||
| 3022 | 3040 | ||
| 3041 | \subsection{The Reflexive Debugger Interface} | ||
| 3042 | |||
| 3043 | The library \verb|ldblib| provides | ||
| 3044 | the functionallity of the debugger interface to Lua programs. | ||
| 3045 | If you want to use this library, | ||
| 3046 | your host application must open it first, | ||
| 3047 | calling \verb|lua_dblibopen|. | ||
| 3048 | |||
| 3049 | You should exert great care when using this library. | ||
| 3050 | Functions provided here should be used exclusively for debugging | ||
| 3051 | and similar tasks (e.g. profiling): | ||
| 3052 | Please resist the temptation to use them as a | ||
| 3053 | usual programming tool. | ||
| 3054 | They are slow and violate some (otherwise) secure aspects of the | ||
| 3055 | language (e.g. privacy of local variables). | ||
| 3056 | As a general rule, if your program does not need this library, | ||
| 3057 | do not open it. | ||
| 3058 | |||
| 3059 | |||
| 3060 | \subsubsection*{\ff \T{funcinfo (function)}}\Deffunc{funcinfo} | ||
| 3061 | |||
| 3062 | This function returns a table with information about the given function. | ||
| 3063 | The table contains the following fields: | ||
| 3064 | \begin{description} | ||
| 3065 | \item[kind]: may be \verb|"C"|, if this is a C function, | ||
| 3066 | \verb|"chunk"|, if this is the main part of a chunk, | ||
| 3067 | or \verb|"Lua"| if this is a Lua function. | ||
| 3068 | |||
| 3069 | \item[source] the source where the function was defined. | ||
| 3070 | If the function was defined in a string, | ||
| 3071 | \verb|source| is that string; | ||
| 3072 | If the function was defined in a file, | ||
| 3073 | \verb|source| starts with a \verb|@| followed by the file name. | ||
| 3074 | |||
| 3075 | \item[def_line] the line where the function was defined in the source | ||
| 3076 | (only valid if this is a Lua function). | ||
| 3077 | |||
| 3078 | \item[where] can be \verb|"global"| if this function has a global name, | ||
| 3079 | or \verb|"tag-method"| if this function is a tag method handler. | ||
| 3080 | |||
| 3081 | \item[name] if \verb|where| = \verb|global|, | ||
| 3082 | \verb|name| is the global name of the function; | ||
| 3083 | if \verb|where| = \verb|tag-method|, | ||
| 3084 | \verb|name| is the event name of the tag method. | ||
| 3085 | \end{description} | ||
| 3086 | |||
| 3087 | \subsubsection*{\ff \T{getstack (index)}}\Deffunc{getstack} | ||
| 3088 | |||
| 3089 | This function returns a table with informations about the function | ||
| 3090 | running at level \verb|index| of the stack. | ||
| 3091 | Index 0 is the current function (\verb|getstack| itself). | ||
| 3092 | If \verb|index| is bigger than the number of active functions, | ||
| 3093 | the function returns \nil. | ||
| 3094 | The table contains all the fields returned by \verb|funcinfo|, | ||
| 3095 | plus the following: | ||
| 3096 | \begin{description} | ||
| 3097 | \item[func] the function at that level. | ||
| 3098 | \item[current] the current line on the function execution; | ||
| 3099 | this will be available only when the function is | ||
| 3100 | precompiled with debug information. | ||
| 3101 | \end{description} | ||
| 3102 | |||
| 3103 | \subsubsection*{\ff \T{getlocal (index [, local])}}\Deffunc{getlocal} | ||
| 3104 | |||
| 3105 | This function returns information about the local variables of the | ||
| 3106 | function at level \verb|index| of the stack. | ||
| 3107 | It can be called in three ways. | ||
| 3108 | When called without a \verb|local| argument, | ||
| 3109 | it returns a table, which associates variable names to their values. | ||
| 3110 | When called with a name (a string) as \verb|local|, | ||
| 3111 | it returns the value of the local variable with that name. | ||
| 3112 | Finally, when called with an index (a number), | ||
| 3113 | it returns the value and the name of the local variable | ||
| 3114 | with that index. | ||
| 3115 | (The first parameter has index 1, and so on, | ||
| 3116 | until the last active local variable.) | ||
| 3117 | In that case, the function returns \nil\ if there is no local | ||
| 3118 | variable with the given index. | ||
| 3119 | The specification by index is the only way to distinguish | ||
| 3120 | homonym variables in a function. | ||
| 3121 | |||
| 3122 | \subsubsection*{\ff \T{setlocal (index, local, newvalue)}}\Deffunc{setlocal} | ||
| 3123 | |||
| 3124 | This function changes the values of the local variables of the | ||
| 3125 | function at level \verb|index| of the stack. | ||
| 3126 | The local variable can be specified by name or by index; | ||
| 3127 | see function \verb|getlocal|. | ||
| 3128 | |||
| 3129 | \subsubsection*{\ff \T{setcallhook (hook)}}\Deffunc{setcallhook} | ||
| 3130 | |||
| 3131 | Sets the function \verb|hook| as the call hook; | ||
| 3132 | this hook will be called every time the interpreter starts and | ||
| 3133 | exits the execution of a function. | ||
| 3134 | When Lua enters a function, | ||
| 3135 | the hook is called with the function been called, | ||
| 3136 | plus the source and the line where the function is defined. | ||
| 3137 | When Lua exits a function, | ||
| 3138 | the hook is called with no arguments. | ||
| 3139 | |||
| 3140 | When called without arguments, | ||
| 3141 | this function turns off call hooks. | ||
| 3142 | |||
| 3143 | \subsubsection*{\ff \T{setlinehook (hook)}}\Deffunc{setlinehook} | ||
| 3144 | |||
| 3145 | Sets the function \verb|hook| as the line hook; | ||
| 3146 | this hook will be called every time the interpreter changes | ||
| 3147 | the line of code it is executing. | ||
| 3148 | The only argument to the hook is the line number the interpreter | ||
| 3149 | is about to execut. | ||
| 3150 | This hook is called only if the active function | ||
| 3151 | has been compiled with debug information \see{pragma}. | ||
| 3152 | |||
| 3153 | When called without arguments, | ||
| 3154 | this function turns off line hooks. | ||
| 3155 | |||
| 3156 | |||
| 3023 | \section{\Index{Lua Stand-alone}} \label{lua-sa} | 3157 | \section{\Index{Lua Stand-alone}} \label{lua-sa} |
| 3024 | 3158 | ||
| 3025 | Although Lua has been designed as an extension language, | 3159 | Although Lua has been designed as an extension language, |
