zoukankan      html  css  js  c++  java
  • 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;
    }

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

  • 相关阅读:
    oculus按键大全
    10 soundJs 初体验
    09 获取服务器时间
    08 基本数据类型转换
    07 如果再使用animateCC2018或者苹果系统使用animate时出现Uncaught ReferenceError: lib is not defined的错误
    AS3.0和php数据交互POST方式
    06 显示fps帧频
    05 js利用ajax向服务器发送请求并返回json值
    04 ajax执行php并传递参数
    03php拉取服务器信息并生成json
  • 原文地址:https://www.cnblogs.com/vd01/p/4934268.html
Copyright © 2011-2022 走看看