lua笔记之userdata
1.一直使用框架里封装好的c库,想着自己一点一点的写些例子,学习下,以后需要c库,可以自己写了。
下边是一个简单的userdata的例子--数组操作。
newarray.c
#include "lua.h"
#include "lauxlib.h"
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h> typedef struct NumArray
{
int size;
double values[];
}NumArray; static int
lnew(lua_State *L)
{
int arr_size = luaL_checkint(L, );
if (arr_size <= )
{
assert(NULL);
} size_t nbytes = sizeof(NumArray) + (arr_size - ) * sizeof(double);
NumArray *na = (NumArray *)lua_newuserdata(L, nbytes);
na->size = arr_size; return ;
} static int
lset(lua_State *L)
{
NumArray *na = (NumArray *)lua_touserdata(L, );
int idx = luaL_checkint(L, );
double value = luaL_checknumber(L, );
printf("idx %d, value %f\n", idx, value);
luaL_argcheck(L, na != NULL, , "array expected");
luaL_argcheck(L, <= idx && idx <= na->size , , "index out of range"); na->values[idx - ] = value; return ;
} static int
lget(lua_State *L)
{
NumArray *na = (NumArray *)lua_touserdata(L, );
int idx = luaL_checkinteger(L, ); luaL_argcheck(L, na != NULL, , "array expected");
luaL_argcheck(L, <= idx && idx <= na->size, , "index out of range"); lua_pushnumber(L, na->values[idx - ]); return ;
} static int
lfind(lua_State *L)
{
NumArray *na = (NumArray *)lua_touserdata(L, );
assert(na != NULL);
double elem = luaL_checknumber(L, ); int idx;
for (idx = ; idx < na->size; idx++)
{
if (na->values[idx] == elem)
{
lua_pushnumber(L, idx + );
return ;
}
} lua_pushnumber(L, -);
return ;
} int
luaopen_newarray(lua_State *L)
{
luaL_checkversion(L);
luaL_Reg l[] =
{
{"new", lnew},
{"set", lset},
{"get", lget},
{"find", lfind},
{NULL, NULL},
}; luaL_newlib(L, l); return ;
}
编译这个c文件,生成动态库 , 命令: gcc -O2 -g -Wall -shared -fPIC -o newarray.so newarray.c -I/usr/include/lua5.2 -llua5.2当然我这里用的是lua5.2,用其它版本的需要更改-I,和-l的头文件和链接库的内容。
测试代码:test.lua
local newarray = require "newarray" print(newarray)
local na = newarray.new() newarray.set(na, , ) local value = newarray.find(na, ) if value ~= - then
print("value is ", value)
else
print("not exists")
end
编译好newarray动态库,就可以运行测试代码了,安装lua是,编译后在src文件下有lua可执行文件,带着这个运行 ./.../lua test.lua.
结果如下:

先让程序跑起来,理解。
后续会加上详细讲解.望多多提意见,指正。。
lua笔记之userdata的更多相关文章
- Lua中的userdata
[话从这里说起] 在我发表<Lua中的类型与值>这篇文章时,就有读者给我留言了,说:你应该好好总结一下Lua中的function和userdata类型.现在是时候总结了.对于functio ...
- lua学习笔记之userdata
这一段时间看了<programming in lua>中的第28章,看一遍并不是很难,但是只是朦胧的感觉,雾里看花,水中望月.最终还是决定敲出来自己看看,练练手,结果受益不少,也遇到了一些 ...
- Lua 笔记
lua命令: #enter shell lua #excute script file lua xxx.lua lua脚本: #!/usr/local/bin/lua 核心概念: As a exten ...
- Lua的Full UserData、Light UserData和metatable
http://lua.2524044.n2.nabble.com/LightUserData-and-metatables-td3807698.html https://www.lua.org/man ...
- Lua笔记
闭包 示例一 function newCounter() return function() -- anonymous function i = i + return i end end c1 = n ...
- Lua笔记(2)
函数高级特性 像新版c++一样,lua支持lambda表达式.也就是可以在函数内部声明函数.内层的函数可以访问外一层函数的局部变量. 一个函数以及它会访问到的外层函数的变量,称为闭包.这些变量又被称为 ...
- Lua笔记(1)
今天开始学习Lua,下面把一些重点记下来: 单行注释-- ,多行注释 --[[ ....... --]] Lua中false和nil表示条件判断的假,其余的,包括空字符串,0,都表示真. Lua没 ...
- LUA笔记之表
表据说是LUA的核, 呵呵, 看例子吧, 看上去, 跟java的list很像, 又有点像json: a = {} -- create a table and store its reference i ...
- LUA笔记之字符串
LUA提供了自动数值转换, 基本上说是乱来也不为过, 就当做是不熟悉数据结构吧, 例子: print("10" + 1) --> 11 print("10 + 1& ...
随机推荐
- dubbo 序列化 问题 属性值 丢失 ArrayList 解决
参考文章:http://blog.csdn.net/wanyanxgf/article/details/6944733 http://tianya23.blog.51cto.com/1081650/5 ...
- iOS 百度地图获取当前地理位置
// // ViewController.m // BaiDuDemo // // Created by Chocolate. on 15-3-2. // Copyright (c) 2015年 re ...
- Angular入门(三) 引入boostrap4
1.cnpm install ngx-bootstrap bootstrap --save ※可能缺少jquery cnpm i jquery 2. 打开 angular-cli.json (项目 ...
- POJ 3020 Antenna Placement【二分匹配——最小路径覆盖】
链接: http://poj.org/problem?id=3020 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
- easyui的datagrid无数据时下方滚动条不显示的解决办法(标题栏显示不完全)
easyui在写datagrid的时候标题栏有时候因为太多.太长所以无法显示所有的列,而且没数据的时候下方的滚动条是不显示的,这样就无法显示所有的列了.解决办法如下: onLoadSuccess: f ...
- Codeblocks自动代码格式化+快捷键
Codeblocks自动代码格式化+快捷键 - u010112268的博客 - CSDN博客 https://blog.csdn.net/u010112268/article/details/8110 ...
- 用gunicorn+gevent启动Flask项目
转自:https://blog.csdn.net/dutsoft/article/details/51452598 Flask,webpy,Django都带着 WSGI server,当然性能都不好, ...
- 基于vue + typescrpt +vuecli 搭建开发环境
打算学习typeScript与vue集成,先放几个链接,留着自己学习用,后续自己写使用新的~ https://segmentfault.com/a/1190000013676663 https://j ...
- Leetcode 之 Valid Triangle Number
611. Valid Triangle Number 1.Problem Given an array consists of non-negative integers, your task is ...
- (4.9)SQL Server 数据库规范
SQL Server 数据库规范 一. 命名规范 常用对象命名规范,使用帕斯卡命名法(Pascal,单词首字母大写),统一使用英文. 1. 表.英文单数名词,尽量写完整单词名 ...