Lua1.0 代码分析 inout.c

转载出处:http://my.oschina.net/xhan/blog/307797

 

inout.c 代码分析
主要看下对于文件的处理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/*
** Function to open a file to be input unit.
** Return 0 on success or 1 on error.
*/
int lua_openfile (char *fn)
{
 lua_linenumber = 1;
 lua_setinput (fileinput);
 lua_setunput (fileunput);
 fp = fopen (fn, "r");
 if (fp == NULL) return 1;
 if (lua_addfile (fn)) return 1;
 return 0;
}

传入脚本文件名,设置当前的输入行为1 设置文件读取和放回。 打开文件,设置文件句柄。 把文件加到文件列表里。lua_addfile 在 table.c 中定义,等到 table.c 中再做分析。

1
2
3
4
5
6
7
8
/*
** Function to get the next character from the input file
*/
static int fileinput (void)
{
 int c = fgetc (fp);
 return (c == EOF ? 0 : c);
}

从文件句柄里读取一个字符

1
2
3
4
5
6
7
/*
** Function to unget the next character from to input file
*/
static void fileunput (int c)
{
 ungetc (c, fp);
}

把一个字符放回文件句柄
int lua_openstring (char *s) 和 lua_openfile 差不多,只不过文件名用的是一个 “String" 开头的字符串表示。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/*
** Called to execute SETFUNCTION opcode, this function pushs a function into
** function stack. Return 0 on success or 1 on error.
*/
int lua_pushfunction (int file, int function)
{
 if (nfuncstack >= MAXFUNCSTACK-1)
 {
  lua_error ("function stack overflow");
  return 1;
 }
 funcstack[nfuncstack].file = file;
 funcstack[nfuncstack].function = function;
 nfuncstack++;
 return 0;
}

设置脚本中的函数到函数这个函数栈中。记录其文件名和函数地址。

posted @ 2015-11-03 20:16  vd01  阅读(182)  评论(0编辑  收藏  举报