zoukankan      html  css  js  c++  java
  • lua快速阅读 socketref,再见!高德 C++博客

    lua-快速阅读 - socketref,再见!高德 - C++博客


    table 是个怪物,有很多facets,类似array,map,struct,整个是个混合物,用起来也比较怪异。
    t={1,2,3,a="gen",b=100}
    t={[0]=1;["name"]="ultra"}
    t.a, t.b , t[0] , t["name"]

    表操作函数:
    ipairs,pairs迭代函数
    table.getn(t)   len of table

    ================================================================
    function() 可以接受任意多的参数,如果实参数过多将丢弃,过少将默认设置为nil
    同样可以返回多个参数
    a,b=foo()

    表作为参数传递到function
    function rename( arg ) os.rename(arg.old,arg.new) end
    rename{old="";new=""}

    匿名函数(lambda in python )
    foo = function(x) return x*2 end
    局部函数 local f=function(x) ... end
    ================================================================
    for n=start,end,step do ... end
    while b do   ... end
    repeat do .... until

    if then .. elseif then ...  end;

    有意思的语法表达式:
        print a or b or c   如果a=false,尝试b...

    注释: --     --{ --} 


    字符串操作:    .. 连接

    ==================================================
    io 函数:
    loadfile('test.lua')()    execute external lua script
    loadstring('print 100')()


    代码测试:
    =======================
    c程序调用lua函数
    c 程序:
    void call_lua_func(){
        lua_State *s = lua_open();
        luaL_openlibs(s);
        int c = lua_gettop(s);
        luaL_dofile(s,"/nandflashpartition/test1.lua");
        lua_getglobal(s,"add");
        lua_pushnumber(s,0.25);
        lua_pushnumber(s,8);
        if( lua_pcall(s,2,1,0)){
            std::cout<< lua_tostring(s,-1)<<std::endl;
        }
        double r;
        r = lua_tonumber(s,-1);
        lua_close(s);
    }
    lua程序:
    function add(x,y)
        return x*y
    end
    --------------------------------
    lua访问c程序空间变量

    1.定义变量student.h
    extern char * gender;
    extern int class_count;

    2.创建pkg文件 student.pkg
    $#include "student.h"
    extern char * gender;
    extern int class_count;

    3.产生tolua++存根框架
    tolua++ -o student.pkg

    4.创建lua测试代码 call_c.lua
    print(gender)
    print(class_count)  访问c 空间的变量

    5.c测试代码
    char * gender;
    int class_count;
    void lua_call_c(){
        int  tolua_student_open (lua_State* tolua_S);
        lua_State * s = lua_open();
        luaopen_base(s);

        gender ="my gender is male!";
        class_count = 100;
        tolua_student_open(s);
        luaL_dofile(s,"/nandflashpartition/call_c.lua");
        lua_close(s);
    }

    6.build && test it!
  • 相关阅读:
    高并发下秒杀商品,必须知道的9个细节
    linux下关闭网络命令
    Linux系统模拟网络测试
    20211215
    观影大数据分析(上)
    2021冬季学期有感
    观影大数据分析(中)
    Docker安装Oracle
    2022寒假安排
    Docker安装Mongo
  • 原文地址:https://www.cnblogs.com/lexus/p/2851685.html
Copyright © 2011-2022 走看看