What are the most effective ways to use Lua with C

2020-06-23 09:35发布

问题:

From my understanding, Lua is an embeddable scripting language that can execute methods on objects. What are the pitfalls to avoid? Is is it feasible to use Lua as an interpreter and execute methods in an web environment or as a rule engine?

回答1:

Lua is very fast - scripts can be precompiled to bytecodes and functions execute close to C++ virtual method calls. That is the reason why it is used in gaming industry for scripting AI, plugins, and other hi-level stuff in games.

But you put C# and web server in your question tags.

If you are not thinking of embedded web servers - than Lua is not very strong. Lua is ANSI C - it compiles to native code, and as such it is not very welcome to your asp.net server, nor to your C# code. Think of "medium trust" or "binding" or "64bit".

Enough of pitfalls. If you are allready using C#, there is no reason to avoid it on web server. It gets compiled and cached. It servers huge sites like this one.



回答2:

Lua scripting is on track to becoming a core module for Apache web server.

There are a couple tools for binding Lua and C#/.NET code here. LuaInterface is the most up to date.



回答3:

We used to use Lua as our scripting language in a medium-sized C# biomedical instrument control application. It was a great way to teach the hardware guys how to write their own little bits of test code.

Once IronPython reached maturity, we decided that since Python is a little more full-featured and well-integrated with VS (as well as having more people who use it), we would replace all our Lua with Python. So far that's working out pretty well. Lua is a fine little language, I'm not sure I can think of any advantages of Lua over Python at this point.

Lua can be a bit faster, but if you're already splitting your domains into script-y and non-script-y, presumably that's not a big factor.



回答4:

Why I like Lua is that it excels as a language, especially when used together with C/C++ as it's supposed to be. To me, it's the merger of these two that "makes" the language.

Pitfalls.. The lack of extension modules could be seen as one. Like no CPAN, but there is LuaRocks. Depends if you have enough bindings to go for your problem. If not, you might need to be making them.

Language pitfalls.. There are some wishes in the Lua wiki, which can be seen as "pitfalls". No "switch" statement. No proper "cond ? a : b" format. If you don't want to stretch your mind above such rather superficial syntax things, then maybe JavaScript is for you?

As a whole, it's a language that makes me 10-100 times more productive than programming without it.