c 使用lua 示例
#include <stdio.h>
#include <string.h>
#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
static int say_hello(lua_State *L)
{
const char *s = lua_tostring(L,1);
printf("hell,%s\n",s);
return 0;
}
int main(void)
{
lua_State *L = luaL_newstate();;
luaL_openlibs(L);
//写入全局变量
lua_pushnumber( L, 211 );
lua_setglobal( L, "hp" );
//lua_pop(L,2);
//读取全局变量
lua_getglobal( L, "hp" );
int res = lua_tointeger(L, -1);
printf("%d\n",res);
lua_pop(L,1);
//设置一个有名称的table
lua_newtable(L);
lua_pushinteger(L, 10025);
lua_setfield(L, 1, "id");
lua_pushinteger(L, 3);
lua_setfield(L, 1, "good_count");
lua_pushinteger(L, 100);
lua_setfield(L, 1, "count");
lua_pushinteger(L, 15);
lua_setfield(L, 1, "outlet_count");
lua_setglobal(L,"customer_table");
//读取table中的值
int result;
lua_getglobal(L,"customer_table");
lua_pushstring(L,"count");
lua_gettable(L,-2);
result = lua_tointeger(L,-1);
printf("%d\n",result);
lua_pop(L,1);
lua_pushstring(L,"outlet_count");
lua_gettable(L,-2);
result = lua_tointeger(L,-1);
printf("%d\n",result);
lua_pop(L,1);
//函数相互调用
lua_register(L,"say_hello",say_hello);
const char* dosomething = " \
function dosomething(words)\
say_hello(words) \
end \
";
luaL_dostring(L,dosomething);
lua_getglobal(L, "dosomething");
lua_pushstring(L,"yexuqiang");
res = lua_pcall(L, 1, 1,0);
printf("res:%d\n",res);
lua_close(L);
return 0;
}
c 使用lua 示例的更多相关文章
- Lua开发环境配置
Lua(英语发音:/ˈluːə/)程序设计语言是一个简洁.轻量.可扩展的脚本语言,是葡萄牙语中“Luna”(月亮)的意思. Lua is a powerful, fast, lightweight, ...
- 【精选】Nginx模块Lua-Nginx-Module学习笔记(二)Lua指令详解(Directives)
源码地址:https://github.com/Tinywan/Lua-Nginx-Redis Nginx与Lua编写脚本的基本构建块是指令. 指令用于指定何时运行用户Lua代码以及如何使用结果. 下 ...
- 用 Lua 控制 MIDI 合成器来播放自定义格式乐谱
用 Lua 控制 MIDI 合成器来播放自定义格式乐谱 作者: FreeBlues 最新: https://www.cnblogs.com/freeblues/p/9936844.html 说明: 本 ...
- Nginx模块Lua-Nginx-Module学习笔记(二)Lua指令详解(Directives)
源码地址:https://github.com/Tinywan/Lua-Nginx-Redis Nginx与Lua编写脚本的基本构建块是指令. 指令用于指定何时运行用户Lua代码以及如何使用结果. 下 ...
- lua工具库penlight--08额外的库(二)
执行一系列的参数 类型说明符也可以 是' ('MIN '..' MAX)' 的形式. local lapp = require 'pl.lapp' local args = lapp [[ Setti ...
- Lua与C交换
1.C调用Lua函数 (1) 首先要进行Lua的初始化,这个主要是lua_open和luaL_openlibs函数 (2)然后是解析并编译lua的代码,这个主要是luaL_dofile函数 (3) ...
- 考勤的lua脚本
ngx.header.content_type = "text/plain;charset=utf-8" local cjson = require "cjson&quo ...
- 转载一篇将C/C++ 与lua混合使用入门讲的比较好的文章
转自 http://www.open-open.com/home/space-6246-do-blog-id-1426.html Lua是一个嵌入式的脚本语言,它不仅可以单独使用还能与其它语言混合调用 ...
- 转载:lua和c的交互
extern "C" { #include "lua.h" #include "lualib.h" #include "lauxl ...
随机推荐
- 林大妈的CSS知识清单(二)可见格式化模型(内含margin塌陷与浮动闭合的解决方案)
盒模型.浮动和定位是CSS中最重要的三个概念.它们共同决定了一个元素在页面中以怎样的形式进行排布与显示. 一.盒模型 1. 定义 盒模型是CSS的核心概念.一个页面中,所有的元素(不管他最终显示是圆形 ...
- 阿里云linux配置ftp服务
阿里云linux配置ftp服务 一.ftp服务安装 运行以下命令安装ftp yum install -y vsftpd 运行以下命令打开及查看etc/vsftpd cd /etc/vsftpd ls ...
- Odoo的 base 模型
Odoo 内核中有一个base插件模块.它提供了 Odoo 应用所需的基本功能.然后有一组内置插件模块来提供标准产品中的官方应用和功能.base模块中包含两类模型: 信息仓库(Information ...
- stackstorm docker中配置ssh免密码登录方式
在docker中配置st2的ssh登录方式折腾了好久,今天终于彻底搞懂了如何重启容器后也不丢失之前的配置,只要容器起来后就可以正常ssh 执行st2中的remote-shell-script 和rem ...
- 裸磁盘上ext4与xfs在线扩容,非LVM
虚拟机添加一个20G的硬盘,磁盘为sdb,分区为ext4 格式化一个5Gib的磁盘出来,用dd命令写入4G数据. 一.需求是容量为5G的磁盘,文件系统为ext4的sdb1扩容到10G. 操作步骤为 1 ...
- 【计算机网络】网络地址转换NAT
网络地址转换NAT 要知道到每个IP使能的设备都需要一个IP地址.以一个家庭为例,假设当地的ISP已为该家庭分配过一块地址,但是后期家庭中的智能设备增加(智能手机.电脑等),这些都需要IP地址才可上网 ...
- BPTT
RNN 的 BP —— Back Propagation Through Time. 参考:零基础入门深度学习(5) - 循环神经网络.知乎. 1 def backward(self, sensiti ...
- Android及java中list循环添加时覆盖的问题-20171021
鉴于新浪博客太渣,转到这来. 最近在工程设计时,使用list循环添加map对象发现,最终全部变为最后一个map的值,但是list的数值还是正确的,也就是说添加了N(list长度或者说循环的次数)个相同 ...
- Redis——解决“org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'redisReferenceResolver': Unsatisfied dependency expressed through constructor parameter 0”
错误栈: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ...
- C# socket异步通讯
Server: using System; using System.Net; using System.Net.Sockets; using System.Text; namespace TCP_S ...