Lua struct

tolua源码分析(十)struct 向lua层传递struct,就是将C#的struct拆分成若干基本类型,依次push到lua栈,然后调用lua层对应struct的new函数,创建lua层的struct;这么做的原因是在lua层使用struct的方法时,不需要跑到C#层去调用C#的接口,对于struct来说,它往往只是一个保存数据的结构,包含的方法都比较简单,lua层去实现完全可以胜任。函数,所以这里的push实际上是把C#的Rect的每个数据成员都拆出来,作为基本类型挨个push到lua层,然后调用lua层的。 阅读详情

http://www.inf.puc-rio.br/~roberto/struct/

teststruct   http://www.inf.puc-rio.br/~roberto/struct/teststruct


经过测试,可以直接编译进官方lua 与 luajit内



Library for Converting Data to and from C Structs for Lua 5.1

(download)

This library offers basic facilities to convert Lua values to and from C structs. Its main functions are struct.pack, which packs multiple Lua values into a struct-like string; and struct.unpack, which unpacks multiple Lua values from a given struct-like string.

The fist argument to both functions is a format string, which describes the layout of the structure. The format string is a sequence of conversion elements, which respect the current endianess and the current alignment requirements. Initially, the current endianess is the machine's native endianness and the current alignment requirement is 1 (meaning no alignment at all). We can change these settings with appropriate directives in the format string.

The elements in the format string are as follows:

  • "!n" flag to set the current alignment requirement to n (necessarily a power of 2); an absent n means the machine's native alignment.
  • ">" flag to set mode to big endian.
  • "<" flag to set mode to little endian.
  • " " (empty space) ignored.
  • "x" a padding zero byte with no corresponding Lua value.
  • "b" a signed char.
  • "B" an unsigned char.
  • "h" a signed short (native size).
  • "H" an unsigned short (native size).
  • "l" a signed long (native size).
  • "L" an unsigned long (native size).
  • "in" a signed integer with n bytes (where n must be a power of 2). An absent n means the native size of an int.
  • "In" like "in" but unsigned.
  • "f" a float (native size).
  • "d" a double (native size).
  • "s" a zero-terminated string.
  • "cn" a sequence of exactly n chars corresponding to a single Lua string. An absent n means 1. When packing, the given string must have at least n characters (extra characters are discarded).
  • "c0" this is like "cn", except that the n is given by other means: When packing, n is the length of the given string; when unpacking, n is the value of the previous unpacked value (which must be a number). In that case, this previous value is not returned.

Lua API

All functions are registered inside a table struct. ul>

  • struct.pack (fmt, d1, d2, ...)

    Returns a string containing the values d1, d2, etc. packed according to the format string fmt.

  • struct.unpack (fmt, s, [i])

    Returns the values packed in string s according to the format string fmt. An optional i marks where in s to start reading (default is 1). After the read values, this function also returns the index in s where it stopped reading, which is also where you should start to read the rest of the string.

  • struct.size (fmt)

    Returns the size of a string formatted according to the format string fmt. For obvious reasons, the format string cannot contain neither the option s nor the optionc0.

    Installing

    To install, simply compile the file struct.c as a dynamic library. In Linux you can use the following command:

    > gcc -Wall -O2 -shared -o struct.so struct.c
    
    In Mac, you should define the environment variable MACOSX_DEPLOYMENT_TARGET as 10.3 and then write
    > gcc -bundle -undefined dynamic_lookup -Wall -O2 -o struct.so struct.c
    

    In Windows, you must generate a DLL exporting the single symbol luaopen_struct.

    Examples

    • The code print(struct.size("i")) prints the size of a machine's native int.

    • To pack and unpack the structure

      struct Str {
        char b;
        int i[4];
      };
      
      in Linux/gcc/Pentium (little-endian, maximum alignment of 4), you can use the string "<!4biiii".
    • If you need to code a structure with a large array, you may use string.rep to automatically generate part of the string format. For instance, for the structure

      struct Str {
        double x;
        int i[400];
      };
      
      you may build the format string with the code "d"..string.rep("i", 400).
    • To pack a string with its length coded in its first byte, use the following code:

      x = struct.pack("Bc0", string.len(s), s)
      
      To unpack that string, do as follows:
      s = struct.unpack("Bc0", x)
      
      Notice that the length (read by the element "B") is not returned.
    • Suppose we have to decode a string s with an unknown number of doubles; the end is marked by a zero value. We can use the following code:

      local a = {}
      local i = 1         -- index where to read
      while true do
        local d
        d, i = struct.unpack("d", s, i)
        if d == 0 then break end
        table.insert(a, d)
      end
      
    • To pack a string in a fixed-width field with 10 characters padded with blanks, do as follows:

      x = struct.pack("c10", s .. string.rep(" ", 10))
      

    Tests

    File teststruct contains a full test script for this package. It is also a good source of examples.

    License

    This package is distributed under the MIT license. See copyright notice at the end of file struct.c.

    $Id: struct.html,v 1.4 2008/04/18 20:10:24 roberto Exp $


xlua源码分析(五) struct类型优化 而数据传输的逻辑,稍微不太相同,tolua是使用lua函数进行数据传输,例如Vector3,tolua可以通过一个get函数直接返回3个float*给C#层,也可以通过一个new函数直接使用x,y,z三个参数构造出一个lua层的struct,pack和unpack的逻辑都放在了lua层里。函数是在C层实现的,那其实很简单了,就是把userdata作为要访问内存的首地址,加上偏移量offset,执行memcpy即可,如果是get,就是从userdata拷贝到value,再push到lua栈; 阅读详情

相关推荐

lua struct数据转json数据接口

直接require 该文件,然后传入struct结构体数据,返回json数据

LuaStruct

LuaStruct提供了一种简单的对Lua数据进行打包或者序列化的方式,可以方便地进行Lua与其他语言的数据传输。ToLuaRunTime中也已经集成了该库。 其核心API只有两个: struct.pack – 用于数据打包,struct.pack(格式化字符串,需要打包的数据1,需要打包的数据2 …) struct.unpack – 用于数据解包,struct.unpack(格式化字符串,需要解包的字节数组,开始解包的位置) 无论打包还是解包,使用都很简单,核心思想是:对于每个需要进行操作的数据,提

zhiai315的博客 2662

九齐NY8B062F单片机:12位ADC与4路PWM在智能家居控制中的实战应用

本文详细介绍了九齐NY8B062F单片机在智能家居控制中的实战应用,重点解析其12位ADC高精度传感器数据采集和4路PWM精准驱动技术。通过实际案例展示如何利用该单片机实现温湿度监测、灯光调节及电机控制,并分享低功耗设计与抗干扰方案,为低成本高可靠性的智能家居设备开发提供完整解决方案。

rnn9storyteller的博客 739

【亲测免费】 推荐开源项目:lua-struct — 纯Lua实现的二进制打包/解包库

推荐开源项目:lua-struct — 纯Lua实现的二进制打包/解包库 1、项目介绍 lua-struct 是一个基于纯 Lua 的二进制数据打包和解包工具库。它借鉴了 PHP 中的 pack 和 unpack 函数的理念,允许你在 Lua 环境中方便地处理二进制数据流,这对于游戏开发、网络通信协议解析或者底层系统编程等场景尤其有用。 2、项目技术分析 lua-struct 提供了一系列的类型标...

gitblog_00070的博客 860

普元EOS8.3精简版安装指南:从零开始搭建低代码开发环境

本文详细介绍了普元EOS8.3精简版的完整安装流程。从JDK1.8环境准备、MySQL数据库配置与初始化,到核心配置文件修改、服务启动与登录验证,提供了从零开始搭建低代码开发环境的清晰步骤和常见问题排坑指南,帮助开发者快速部署并体验平台核心功能。

ik678901的博客 1106

别再死记硬背公式了!用Multisim仿真软件,5分钟搞懂三极管放大电路的工作原理

本文介绍如何利用Multisim仿真软件快速掌握三极管放大电路的工作原理,避免死记硬背公式。通过动态实验和参数调整,直观理解三极管的工作状态和放大效果,提升学习效率和实践能力。

weixin_30566063的博客 573

lua struct.pack struct.unpack

#include #include #include #include #include "lua.h" #include "lauxlib.h" /* ** {====================================================== ** Library for packing/unpacking structures.

taobao755624068的专栏 7546

SRAM:SRAM入门指南与工作原理

SRAM(Static Random Access Memory),即静态随机存取存储器,是一种能够读取和写入数据的内存(RAM)。它使用由晶体管组成的触发器来存储数据,只要有电源供应,数据就可以稳定地保存。与动态随机存取存储器(DRAM)不同,SRAM 不需要周期性地刷新来保持数据。

weixin_42896509的博客 1万+

Lua 类和结构体

Lua中默认是没有面向对象的,需要靠我们自己实现 成员变量 成员函数 Student = { age = 18, sex = true, Up = function () print('长大了') end } Lua中类的表现更像是一个类中包含有很多的静态变量和函数 print(Student.age) Student.Up() 在声明表后,可以在表外去声明表有的变量和方法 表内是没有name这个属性的,我们在表外给它添加了 Student.name= '少年' --表外加函数有两种写

LuckyMechanic的博客 4734

SAP成本核算-事中控制(成本对象控制/成本归集与结算)

本文详细介绍了SAP系统中工单成本归集与结算的全流程管理。主要内容包括:1)工单成本三阶段归集(投料、报工、入库)的具体操作与计算方法;2)工单实际成本结算七步骤:从实际费用归集、成本分割、作业价格计算到差异结算的全过程;3)关键注意事项如测试运行验证、价格单位确认、无在制品处理等。通过标准价计算、实际作业重估等机制,系统实现了生产成本从计划到实际的精确核算,最终生成财务所需的差异结算凭证。所有操作均提供单工单和批量处理两种方式,满足不同业务场景需求。

小九啥也不会 1382

硬件工程师学习-硬件的单元测试(UT)(1)电源与时钟的测试

我们在设计的时候,通过控制电源的上电使能管脚或者通过MOSFET、或者专用的电源上电控制芯片,控制不同电源的上电时序,以确保每个芯片的电源上电时序是满足芯片的要求的。我们在测试的时候,需要对相关的要求一一测试,以确认每个上电时序满足要求的。在维基百科中,相位噪声的定义是“时域不稳定性(抖动)导致波形在相位上发生快速、短期、随机的波动,这种波动在频域中的表现即为相位噪声”。单元测试,一般是指基于开发人员自行开展的功能测试及各个功能单元的单元测试,是硬件信号级的测试,分为基本测试和信号完整性测试。

qq_43416206的博客 4054

Lua程序数据类型及其使用

声明下:以下是别人文章中的内容。 Robert Z 2010-1 初阶 数据类型 函数 表 简单对象的实现 简单继承 数据类型 八种基本类型: 数值(number) 内部以double表示 字符串(string) 总是以零结尾,但可以包含任意字符(包括零),因此并不等价于C字符串,而是其超集。 布尔(boolean) 只有“true”或者

陈嘉怡的专栏 1724

UCIE Specification详解(八)

Logical Physical Layer(逻辑物理层)

Crazzy_M的博客 1622

lua struct

这两天做TCP协议,数据的传输都是二进制的,需要解释,于是用到了struct 看到这样一句代码:    Python代码   length = struct.unpack('>I', self.buffer[:4])[0]    当时没有明白format=">I"是什么意思,从google找了一下,有人说这个东西,可都是比较笼统,没能让我明白,于是硬着头皮看A

在梦中享一刻静谧,等待梦中的你 4277

uniapp(全端兼容) - 最新详细实现瀑布流列表及布局设计,uni-app全平台兼容的瀑布流示例,view容器自适应瀑布流布局,图片瀑布流布局、图像+文字卡片式瀑布流,类似小红书(详细示例代码)

uni-app,瀑布流,安卓app,苹果app,微信小程序,移动端,H5网页,手机网站,微信小程序、支付宝小程序,手机app端,nvue,瀑布流布局,如何实现图片瀑布流,怎么做文字+图像瀑布流,uniapp仿小红书笔记瀑布流效果,uniAPP仿快手抖音布局列表,瀑布流列表详细教程,编译所有平台都能用的瀑布流列表,商品瀑布流列表,图文列表,小程序瀑布流,页面触底懒加载,瀑布流插件,详细示例代码教程,uni-app H5实现瀑布流效果,uniapp小程序做瀑布流,纯css瀑布流,用uniapp写瀑布流商品列表,

✅ 精选推荐文章 1万+

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

lua中如何识别C语言中的struct

不如用c写比较简单呢 都要在lua中解析好数据结构,不过也可以用ffi呢。。。proto.lua 转载于:https://www.cnblogs.com/honpey/p/8021571.html

weixin_30765577的博客 237

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

xLua(五)——C#访问Lua的table

首先在Lua脚本中定义一个table 一:映射到Class/Struct 新建映射类/结构体中的字段名必须与Lua中table的键名相同(Lua中键为name,那么映射类中字段名也要是name) using UnityEngine; using XLua; public class CsCallLua : MonoBehaviour { private void Start() ...

L的博客 5827

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

XLua官方教程 05 XLua复杂值类型(struct)gc优化指南

一、复杂值类型的gc问题 xLua复杂值类型(struct)的默认传递方式是引用传递,这种方式要求先对值类型boxing,传递给lua,lua使用后释放该引用。由于值类型每次boxing将产生一个新对象,当lua侧使用完毕释放该对象的引用时,则产生一次gc。 为此,xLua实现了一套struct的gc优化方案,您只要通过简单的配置,则可以实现满足条件的struct传递到lua侧无gc。 二、struct需要满足什么条件? 1、struct允许嵌套其它struct,但它以及它嵌套的struct只能包含

yhx956058885的博客 1687

改写 alien struct使其适配lua5.3

lua5.3直接调用windows的dll,使得lua可以强大到做任何对于windows操作系统的操作。

C++,Lua,python3 1534

lua面向对象-table的使用

lua table的使用 其实非常简单,我们换个角度去看它。

2301_80826391的博客 230

lua使用rust代码(第三期)--Vec<struct>

撸了今年阿里、头条和美团的面试,我有一个重要发现.......>>> ...

zhnxin_163的博客 407

由union引发的Struct占用内存空间和大小端问题的思考

union变量如何在内存中存放?struct如何计算内存空间大小?union对于解决大小端的问题有什么方便之处?大小端有何异同?字节在内存地址中如何放置?这里一一与 大家探讨

andyqingliu的博客 719

C++:和Lua互传结构体(附带源码)

C++:和Lua互传结构体(附带源码)

欢迎来到我的博客!这里是一个专注于计算机技术的分享平台,涵盖编程开发、算法研究、系统架构、软件工程等多个领域。无论你是初学者还是资深开发者,都能在这里找到有价值的内容。 1814

lua面向对象-self的使用

lua self的使用非常简单,它相当于是把函数体写在了外面,并且用 ":"符号去定义和调用。但是看这篇文章之前,我希望你知道table作为struct的用法。

2301_80826391的博客 352

Library for Converting Data to and from C Structs for Lua 5.1

Library for Converting Data to and from C Structs for Lua 5.1 (download) This library offers basic facilities to convert Lua values to and from C structs. Its main functions are struct.pack, which

taobao755624068的专栏 1045

Lua脚本调用C函数小结

 Lua脚本调用C函数小结 仔细的学习了一下,发现功能的确非常强大。用脚本调用C的函数,都希望有如下特性:1. 多输出Lua 本身就支持函数返回多个值,语法如下:x,y = testext.Test()2. 可以使用类似C的结构的形式输入输出Lua 的Table可以在形式上很好的模仿C的结构,a = {}a.i = 101a.name = "jason"a.subtable = {}a.s

JEXO 3381
上一篇: 在dll内 不熟悉__declspec(thread)的话 尽量不要用
下一篇: 五笔新手,打开搜狗五笔输入法自动联想功能
whitehack
博客等级 码龄19年 67粉丝 123原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址