编程与软件工程notebook.kulchenko.com
Lua:优点、缺点与丑陋之处(2012)
Lua: Good, Bad, and Ugly Parts (2012)
2013 — 2021,它一次次回到首页
黑点是上首页(≥ 50 分)的那次投稿,空心点是没冲上去的投稿,点越大分数越高。
导读
这篇文章写于2012年,作者系统梳理了Lua语言中「好、坏、丑」的三类特性:好的方面包括小巧、高效、易于嵌入C/C++;坏的方面包括标准库简陋、生态系统薄弱、Unicode支持差等;丑的方面则是一些容易踩坑的语法或行为细节。文章既是对Lua的客观评价,也是理解轻量级脚本语言设计取舍的经典参考。
为什么经久不衰:Lua作为嵌入式脚本语言的典范,其设计理念和取舍在今天仍被广泛借鉴(如游戏、Nginx、Neovim等场景),这篇十多年前的总结依然准确且富有启发性。
HN 评论精选
Lua极适合嵌入C/C++,源码易读、文档全,是添加脚本功能的首选。
Lua是一项非常棒的技术。它的源代码很容易上手,文档也很完整。它确实有怪癖,但如果我需要给软件添加脚本功能,我会优先考虑Lua而不是自己写DSL,因为你可以直接把Lua源码作为静态库嵌入到C/C++软件中。基于栈的交互方式让与C/C++的对接变得非常简单,我最近出于好奇看了Rust绑定,看起来很有前景。
linkdd查看英文原文
Lua is a very nice piece of technology. Its source code is pretty easy to get into, the documentation is complete. It has its quirks yes, but if I need to add scripting to a software, I'd consider Lua before considering writing a DSL, simply because you can pretty much embed Lua's source in your C/C++ software as a static library[0]. The stack-based approach makes it so easy to interact with C/C++, and I've been looking at the Rust bindings[1] recently out of curiosity, looks promising. [0] - https://github.com/lubgr/lua-cmake [1] - https://github.com/amethyst/rlua/blob/master/examples/guided_tour.rs
Lua生态薄弱,常见功能缺乏明确的主流库选择,在非嵌入场景下会拖后腿。
Lua很合我的口味,我想用它。它似乎设计良好,还有高性能的JIT。但对于非嵌入场景,一个很大的弱点是生态系统。我最近一个小项目想用Lua,涉及sqlite数据库、老式3DES加密和SMTP客户端,很多语言都有明确的主流库,但Lua的sqlite就有三四种选择,没有明显赢家。我最终用了Ruby。
BoogerDan查看英文原文
Lua appeals to my sensibilites and I want to make use of it. It seems to be a well-designed language and it has a very performant JIT. But a big weakness for non-embedded use cases seems to be its ecosystem. A recent little project I wanted to use Lua on included a sqlite database, oldschool 3DES encryption, and an SMTP client. For many languages, there would be a clearly mob-approved library for each of these. But I found 3 or 4 possibilities to use sqlite in Lua, with no clear winner. I ended up using Ruby instead.
LuaJIT性能惊人,某些场景甚至超过C,但Lua.js尚不成熟。
Lua很棒。我最近用它重写了一个小的音频效果框架,基于我早先的C项目。令我惊讶的是,没有做任何优化,LuaJIT版本的一个效果比C版本还快,另一个更复杂的滤波器效果也只花了C版本2.01倍的时间。而且代码简单干净、大部分是声明式的。项目目标之一是用Lua.js把效果代码编译成JavaScript在浏览器里跑,但不幸遇到了Lua.js的一个bug,破坏了我创建模块的方式。Lua.js是实验性项目,远不如LuaJIT和标准Lua成熟,但非常酷。等Lua.js更成熟,就能用Lua写高性能代码并移植到几乎所有环境。
graue查看英文原文
Lua's great. I recently used it to rewrite a small audio effect framework[1] based on an older project of mine in C[2]. To my astonishment, without any optimization efforts on my part, the LuaJIT version of one effect ran faster than the C version, while another more complicated effect (a filter) only took 2.01x as much time as C. And this was with simple, clean, mostly declarative code (read it on GitHub and see what you think). One of the goals of this project was to compile effects code to JavaScript with Lua.js[3] and produce a demo that ran in the browser. There, unfortunately, I ran into a showstopper with a Lua.js bug[4] that breaks my approach to creating modules. Unlike LuaJIT and regular Lua, Lua.js is an experimental project and far less mature - though totally awesome. I might make another attempt to fix the problem myself at some point. With a more mature Lua.js, you could write fast Lua code and port it to nearly every environment. [1] https://github.com/graue/luasynth [2] https://github.com/graue/synth [3] https://github.com/mherkender/lua.js [4] https://github.com/mherkender/lua.js/issues/13#issuecomment-9220080
Lua在嵌入式场景下性能极佳,但缺少原生整数类型(需LNUM补丁)是一大痛点。
我在一个项目里用Lua很久了,用它替代Python作为嵌入式脚本语言。它作为语言和解释器一直让我惊叹。当然,语法偶尔会咬你一口,缺少内置功能有时也烦人。但是,我可以把回调加到每秒执行一千万次的模拟引擎里,结果几乎没有性能损失。就像文章提到的,字符串匹配库不如Perl强大,但对我所有用途都够用。我唯一的愿望是有一个更新版的LNUM补丁支持Lua 5.2——我项目里整数用得太多了,没有它很烦。
ethereal查看英文原文
I've been using Lua in a project for quite some time, replacing Python as my embedded scripting language of choice. It never ceases to amaze me, as a language and as an interpreter. Sure, the syntax bites you occasionally, and sure, the lack of built-in functionality is occasionally annoying. But hey, I can add callbacks into a simulation engine that execute 10 million times per second with almost no slowdown in the result. Like the article mentions, the string matching library isn't as powerful as Perl's, but it's more than adequate for all of my uses. My only wish is for an updated LNUM patch that works with Lua 5.2. I deal with integers too much in my projects for its absence to not be annoying.
历次讨论
同类还值得读
开源应用架构
The Architecture of Open Source Applications
《开源软件架构》(AOSA)是一本在线免费书,邀请知名开源项目的作者亲自剖析其内部设计,涵盖 SQLAlchemy、LLVM、Audacity、Apache、Bash 等系统。
编程与软件工程上首页 9 次 · 2011–2024aosabook.org编辑精选编程语言简史:简短、不完整且多半错误
A Brief, Incomplete, and Mostly Wrong History of Programming Languages
一篇以戏谑、故意不准确的方式回顾编程语言史的博客文章,用夸张轶事和讽刺定义串起 Lisp、C、C++、Java、JavaScript、PHP 等语言。
编程与软件工程上首页 8 次 · 2009–2021james-iry.blogspot.comSQLite 是如何测试的
How SQLite is tested
SQLite 官方文档页,系统介绍 SQLite 的测试体系:多种测试方法(TCL 测试、模糊测试、回归测试、静态分析等)和海量测试用例,展示了这个小型开源项目如何靠极致测试获得…
编程与软件工程上首页 8 次 · 2009–2025sqlite.org编辑精选高级编程语言(2009)
Advanced programming languages (2009)
Matt Might 的经典文章,按用途推荐“高级编程语言”:如果追求就业就选主流语言,如果追求表达力、学术研究或创业,则推荐 Haskell、OCaml、Scala 等函数式语…
编程与软件工程上首页 8 次 · 2009–2022matt.might.net我的 Forth 与栈机器经历
Yosefk - My history with Forth & stack machines
作者以亲身经历回顾了使用 Forth 语言和栈式机器的历程:既赞叹其简洁优雅、代码量小、可自举的优点,也坦率指出栈操作在实际编程中的痛苦,以及 Forth 社区文化中的问题。
编程与软件工程上首页 8 次 · 2010–2024yosefk.com编辑精选