为什么我们需要像 Lua 这样的嵌入式编程语言? [英] Why do we need an embeddable programming language like Lua?

查看:38
本文介绍了为什么我们需要像 Lua 这样的嵌入式编程语言?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用可嵌入编程语言的典型用例是什么?我是否正确理解这种语言应该嵌入到某些程序环境中并且应该能够从那里执行?

What are the typical use cases of using an embeddable programming language? Do I understand it correctly that such language should be embedded into some program environment and should be able to be executed from there?

推荐答案

由于您将问题标记为Lua",我将在此语言的上下文中给您一个答案.

Since you tagged the question as "Lua", I'll give you an answer in the context of this language.

Lua 是用 C 语言编写的(几乎完全兼容 C89 标准;如果需要,可以使用编译时开关轻松禁用不兼容的功能)并且被设计为易于与 C 代码集成.在 Lua 的上下文中,集成"意味着两个不同但相关的事物:

Lua is written in C (almost completely compatible with C89 standard; the incompatible features can be easily disabled, if needed, using compile-time switches) and has been designed to be easily integrated with C code. In the the context of Lua, "integrated" means two different, but related, things:

  1. 您可以轻松编写可用作 Lua 代码库的 C 代码.集成是通过将 C 代码静态或动态链接到 Lua 引擎代码来实现的.然后可以使用 Lua require 函数在 Lua 代码中引用链接库.

  1. You can easily write C code that can be used as a library by Lua code. The integration is achieved either by static or dynamic linking your C code to Lua engine's code. The linked library can then be referred to in your Lua code using the Lua require function.

Lua 引擎可以轻松嵌入到 C 应用程序中,即链接(再次静态或动态)到 C 应用程序代码.然后 C 应用程序可以使用 Lua 的 C 应用程序编程接口(Lua C-API).

Lua engine can be easily embedded in a C application, i.e. linked (again either statically or dynamically) to the C application code. Then the C application can interact with the Lua code using Lua's C application programming interface (Lua C-API).

注意:这可以做到,只需稍加努力,也可以使用 C++ 应用程序.

Note: this can be done, with a little more effort, also with a C++ application.

如果您的 C 应用程序嵌入了 Lua,则可以将许多(如果不是大多数)操作委托给 Lua 引擎,即委托给使用 C-API 函数编写的代码,或者更好的是 Lua 代码.Lua 代码可以作为 C 字符串嵌入到您的 C 代码中,也可以存储为外部 Lua 脚本.

If your C application embeds Lua many, if not most, operations can be delegated to the Lua engine, i.e. either to code written using the C-API functions or, better yet, Lua code. Lua code could be embedded as C strings inside your C code or be stored as external Lua scripts.

使用 Lua 代码实现部分代码逻辑有几个优点:

Having part of your code logic implemented using Lua code has several advantages:

  • Lua 比 C 更容易学习和使用(不太难),而且它的层次要高得多.它支持强大的抽象,例如函数闭包和面向对象(以一种特殊的方式,使用 Lua 表和 元方法).

Lua 是一种动态语言:它不需要离线"编译.您可以修改 Lua 脚本的文本,这就是修改应用程序行为所需的全部内容(不需要额外的编译+链接步骤).这简化了应用程序的开发和调试.

Lua is a dynamic language: it requires no "off-line" compilation. You can modify the text of your Lua script and that's all you need to modify your application behavior (no additional compilation+linking steps needed). This simplifies application development and debugging.

Lua 是一种比 C 更安全的语言:编写表现出未定义行为的 Lua 代码真的很困难,就像在 C/C++ 的上下文中一样.如果 Lua 脚本失败,它会大声"失败.此外,Lua 支持异常机制(尽管其语法与 C++ 不同),与 C 相比,该机制可用于以更简单的方式实现错误管理.

Lua is a safer language than C: it is really difficult to write Lua code that exhibits undefined behavior, as intended in the context of C/C++. If a Lua script fails, it fails "loudly". Moreover Lua supports an exception mechanism (although with a different syntax than C++) which can be employed to implement error management in a much easier way compared to C.

与大多数动态语言一样,Lua 是垃圾收集的.这意味着程序员可以免于手动管理动态内存的痛苦,而动态内存是缺乏垃圾收集的语言中的错误、泄漏、不稳定和安全漏洞的主要原因.

Lua, as most dynamic languages, is garbage collected. This means that the programmer is spared the pain of manually managing dynamic memory, which is a major cause of bugs, leaks, instability and security loopholes in languages that lack garbage collection.

Lua 可以吃自己的狗粮",即你可以在运行时构建一个字符串(甚至在 Lua 本身中),如果它是有效的 Lua 代码,你的程序可以即时执行它.这在其他动态语言中也不常见(它仍然不是 LISP,但它变得更接近,并且具有更易读的语法).这使 Lua 脚本能够:

Lua can "eat its own dog food", i.e. you can build a string at runtime (even in Lua itself) and if it is valid Lua code, your program can execute it on the fly. This is something not frequently seen even in other dynamic languages (still it is not LISP, but it gets closer, and with much more readable syntax). This enables Lua scripts to:

  • 采用强大的基于文本的元编程技术,其中 Lua 代码可以生成其他 Lua 代码并即时执行;

  • employ powerful text-based metaprogramming techniques, where Lua code can generate other Lua code and execute it on the fly;

以简单的方式实现领域特定语言(DSL);Lua 代码可以在运行时加载其他经过精心设计的 Lua 代码,以反映使用它的特定问题域(Lua 语法简单,但足够灵活以允许此类操作);

implement domain specific languages (DSLs) in an easy way; Lua code can load at runtime other Lua code that is crafted so as to reflect the specific problem domain in which it is used (Lua syntax is simple, yet flexible enough to allow such things);

轻松用作配置语言:您的应用程序(用 C 和 Lua 混合编写)可以使用一些 lua 文件作为配置文件,而无需为特定配置文件制作临时解析器格式.因此,您无需解析 *.properties*.csv*.ini 或任何其他格式没有为此目的使用 Lua 文件的选项.

be used as a configuration language with ease: your application (written in a mix of C and Lua) can use some lua files as configuration files without the need to craft an ad-hoc parser for a specific configuration file format. Therefore you don't need to parse *.properties, *.csv, *.ini, or whichever other format you would choose if you hadn't the option of using Lua files for that purpose.

Lua 引擎具有非常小的内存占用(大约数百 kB),具有强大的功能.使用很少的 C 代码行和一堆 Lua 文件,您可以创建一个完整的应用程序,否则将需要数千行 C 代码行.标准 Lua 独立解释器 可以看作只是嵌入 Lua 的一个例子在 C 应用程序中!

Lua engine has a very small memory footprint (some hundreds kBs), packing powerful capabilities. With very few C code lines and a bunch of Lua files you could create a complete application that would require thousands of C code lines otherwise. The standard Lua standalone interpreter can be seen as just an example of embedding Lua in a C application!

Lua 有一个非常自由的开源许可证,这使得它即使在在商业应用中没有太多麻烦.这也允许修改其源代码以使其适应特殊需求.

Lua has a very liberal open-source license, which enables its use even in commercial applications without much hassle. This also allows the modification of its source code to adapt it to special needs.

小内存占用和易于调整的 C 源代码使 Lua 成为将其移植到嵌入式系统或小型微型计算机系统(微控制器等)的理想选择.标准 Lua 发行版的许多部分都可以剥离,从而将核心 Lua 引擎减少到大约 100kB 的范围内.例如,以 eLua 项目为例,这是为嵌入式设备设计的 Lua 修改版.

Small memory footprint and easily tweakable C sources make Lua a perfect candidate for porting it on embedded systems or small microcomputer systems (microcontrollers, etc.). Many parts of the standard Lua distributions can be stripped off, reducing the core Lua engine in the ~100kB range. As an example, take the eLua project, a modified distribution of Lua designed for embedded devices.

这篇关于为什么我们需要像 Lua 这样的嵌入式编程语言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆