当前位置:网站首页>【lua】int64的支持
【lua】int64的支持
2022-07-04 22:17:00 【米莱虾】
之前的版本中,lua 只默认支持 double 类型,虽然可以通过修改 luaconf.h 里的定义把 lua number改为 int64,但为了 int64 类型放弃 double 显然不合适,属于顾此失彼。
那么,对于 64bit 的解决方案有很多,基本思路都是使用 8bytes 的string / lightuserdata / userdata修改元表来实现。
我们一般使用 8bytes 长的 string 来表示一个 int64 。这样既可以做唯一的 key 用,又不用做复杂的扩展,属于比较好的处理方式。
如果想要在项目中直接处理 64bit 的 timestamp。这就需要对int64做数学运算了。在 luajit 中,是定义了一个 userdata 并重载其运算符完成的。即可以用 ffi.cast("int64_t",0) 来构造一个 64bit 的0。对于这种做法,姑且不谈 userdata 的额外开销问题,这样做有一个问题就是当 64bit 的 cdata 做 table 的 key 的时候,相同值的 int64 并不是同一个 key 。
因此,有了一个更轻量的方式来解决 int64 支持的问题。那就是在 64 位平台上,我们完全可以用 lightuserdata 无损失的表示一个 int64,通过给 lightuserdata 设置 metatable ,我们可以接管它的数据运算。唯一不足的是,比较一个 int64 和普通的 lua number 是否相等时,lua 不能隐式的做转换(大于小于比较没有问题)。
由此,风云大神花了点时间构造了一个库。这个库只提供了一个显式的 api ,即构造一个 int64 数字。可以从 lua number 构造,也支持从一个 8 字节宽的小头的字符串来构造。实际在内存储存的是一个 lightuserdata 即一个 64bit 指针(所以这个库不适用于 32 位平台)。也可以通过 C 接口 lua_pushlightuserdata 来把一个 64bit 整数压入堆栈。
把 int64 转换为普通的 lua number 借用了 # 操作符。
边栏推荐
- Close system call analysis - Performance Optimization
- Alibaba launched a new brand "Lingyang" and is committed to becoming a "digital leader"
- UML图记忆技巧
- LOGO特训营 第三节 首字母创意手法
- Force buckle 3_ 383. Ransom letter
- TLA+ 入门教程(1):形式化方法简介
- 2022-07-04:以下go语言代码输出什么?A:true;B:false;C:编译错误。 package main import “fmt“ func main() { fmt.Pri
- i.MX6ULL驱动开发 | 24 - 基于platform平台驱动模型点亮LED
- Now MySQL cdc2.1 is parsing the datetime class with a value of 0000-00-00 00:00:00
- Why should garment enterprises talk about informatization?
猜你喜欢

Why is Dameng data called the "first share" of domestic databases?

More than 30 institutions jointly launched the digital collection industry initiative. How will it move forward in the future?

抖音实战~评论数量同步更新

B站大量虚拟主播被集体强制退款:收入蒸发,还倒欠B站;乔布斯被追授美国总统自由勋章;Grafana 9 发布|极客头条

BigFilter全局交易防重组件的介绍与应用

Xiangjiang Kunpeng joined the shengteng Wanli partnership program and continued to write a new chapter of cooperation with Huawei

Introduction and application of bigfilter global transaction anti duplication component

Logo special training camp section III initial creative techniques

Logo special training camp Section V font structure and common design techniques

NFT Insider #64:电商巨头eBay提交NFT相关商标申请,毕马威将在Web3和元宇宙中投入3000万美元
随机推荐
测试必会:BUG的分类及推进解决
[acwing] solution of the 58th weekly match
Prosperity is exhausted, things are right and people are wrong: where should personal webmasters go
常用的开源无代码测试工具
都说软件测试很简单有手就行,但为何仍有这么多劝退的?
The Sandbox 和数字好莱坞达成合作,通过人力资源开发加速创作者经济的发展
md5工具类
力扣3_383. 赎金信
醒悟的日子,我是怎么一步一步走向软件测试的道路
LOGO特訓營 第一節 鑒別Logo與Logo設計思路
Common shortcut keys for hbuilder x
不同环境相同配置项的内容如何diff差异?
Service online governance
Flask 上下文详解
Machine learning notes mutual information
将QA引入软件开发生命周期是工程师要遵循的最佳实践
【OpenGL】笔记二十九、抗锯齿(MSAA)
2022-07-04: what is the output of the following go language code? A:true; B:false; C: Compilation error. package main import “fmt“ func main() { fmt.Pri
Huawei Nova 10 series released Huawei application market to build a solid application security firewall
Domestic database chaos