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 ...
随机推荐
- 【持续集成工具】 Jenkins
一.什么是持续集成 持续集成(CI):简单来说就是指将开发者的工作内容频繁地集成到主干中. 而持续集成工具可以将开发者频繁需要构建,编译,测试,部署等操作自动进行,为开发提供了非常大便利. 二.持续集 ...
- 微服务之Nacos配置中心源码解析(二)
Nacos配置中心源码解析 源码入口 ConfigFactory.createConfigService ConfigService configService = NacosFactory.crea ...
- How to setup a native windows server 2003 tftpd
expand tftpd.ex_ %windir%\system32\tftpd.exe instsrv tftpd %windir%\system32\tftpd.exe reg add hklm\ ...
- 在values中添加colors.xml
如何在values中添加colors.xml文件?按钮上的文字,背景色的动态变化的xml放在res/drawable里,现在我就说静态的xml文件吧. res/values/colors.xml< ...
- 1.python环境安装
一:安装Python与环境配置 二:安装pip 三:Anaconda安装和使用 3.1 什么是 Anaconda? Anaconda是专注于数据分析的Python发行版本,支持 Linux, Mac, ...
- gulp connect.static is not a function
npm install --save serve-static var serveStatic = require('serve-static');
- 如何设置zencart买满多少免运费?
有时候会希望客户买满多少免运费,当订单总金额大于免运费的订单金额设值时,免运费.下面介绍一下zencart设置买满多少免运费: 1.进入后台–模块管理(Modules)–总额计算(Order Tota ...
- Autoprefixer:一个以最好的方式处理浏览器前缀的后处理程序
Autoprefixer解析CSS文件并且添加浏览器前缀到CSS规则里,使用Can I Use的数据来决定哪些前缀是需要的. 所有你需要做的就是把它添加到你的资源构建工具(例如 Grunt)并且可 ...
- 题解 【NOIP2014】解方程
题面 解析 这题的数据看起来似乎特别吓人... 但实际上, 这题非常好想. 只需要模一个大质数就行了(我模的是1e9+7)(实测有效) 另外,a要用快读读入,再一边模Mod(因为实在太大了). 然后, ...
- IDEA运行有问题debug正常解决方案
朋友们!有没有遇到这样的问题,IDEA运行有问题,debug确是正常的,不经怀疑人生! 不要慌!点击maven,clean一下,再compile一下,就好啦! 不要慌!点击maven,clean一下, ...