“Compiled programming languages and scripting languages each have unique advantages, but what if you could use both to create rich applications? Lua is an embeddable scripting language that is small, fast, and very powerful. Before you create yet another configuration file or resource format (and yet another parser to accompany it), try Lua.”
Lua is great and all, but I think JavaScript is wrongly overlooked. Using Mozilla’s spidermonkey JavaScript parser, it’s trivial to embed JavaScript into your application and I find JS to be a much more natural language to script in than Lua (as well as documentation (for the language, not for embedding it) being abundant for it, in comparison to Lua).
I’ll try to write a similar article/guide about it come summer.
What kind of licensing issues are there? Can you include it in a commercial/propietary application without paying any licensing fees?
spidermonkey – http://www.mozilla.org/js/spidermonkey/ – From the readme: ‘The contents of this file are subject to the Mozilla Public License Version 1.1 (the “License”)’ – There’s a FAQ here: http://www.mozilla.org/MPL/FAQ.html
ah, sorry, my mistake – it’s under a tri-license – MPL 1.1/GPL 2.0/LGPL 2.1 – I’ve no idea how that works I’m afraid Also, I think that might be the wrong URL, try http://www.mozilla.org/MPL/mpl-faq.html
Thanks. Basically what I wanted to know is if I could integrate it into my app without opening the source of my app. Since I would integrated it through a plugin (on windows it would be a DLL), I don’t have to open the source of the app, just the plugin itself, which is fine.
>Since I would integrated it through a plugin (on windows it would be a DLL), I don’t have to open the source of the app, just the plugin itself, which is fine.
You are describing LGPL licensing. Lua comes with MIT, allowing you to do that, or to “bake in” the whole Lua engine alongside your normal code, no restrictions. Allows also for further mods, of course, just as long as you won’t call the resulting modded (enhanced, or reduced) version ‘Lua’.
Made this note “for the record” so others won’t think Lua licensing is restricted. It is not.
(about sandboxing, Lua _rules_ in that
Well, actually, I was asking about Javascript
Ah, you beat me to the post. Yeah, Javascript is a more c-esque scripting language and even has a hint of object orientedness. Much more powerful, in my opinion. And, as you mentioned, Spidermonkey[1] is a fast (there exists[2] a raytracer written in Javascript) and very simple parser to implement.
[1] http://www.mozilla.org/js/spidermonkey/
[2] http://www.slimeland.com/raytrace/
JavaScript is also written to be sandboxed, which is important in some applications. I would choose it over Lua or Python if sandboxing was a requirement.
and even has a hint of object orientedness.
Some would argue that it’s more than a hint, even one of the most powerful OOP languages: http://www.crockford.com/javascript/
Lua makes me think of Ion (as in the window manager). IMHO Ion 3, and its programmable interface written in and for scripting in Lua, totally rocks.
Ion: http://modeemi.fi/~tuomov/ion/
Lua looks pretty interesting. Reminds me somewhat of Matlab programming.
>What kind of licensing issues are there? Can you include it in a commercial/propietary application without paying any licensing fees?
Absolutely. Lua is available in MIT license, which is one of the least restrictive OSI licenses (no need to even mention you’re embedding Lua).
Lua is being used in multiple videogames (XBox, PS2, probably XBox-360) without our knowing, exactly for this very reason.
In fact, one of the best debuggers for Lua is coming from the Microsoft camp (being open source, by one MS developer). Small world….
I hinted them to go for Lua, when they were selecting a scripting language. They’d already made up their mind (I still think they chose the slower horse In things like this also language PR counts, and sometimes counts a lot.
(Take the name of JavaScript, for example. It really was LiveScript but Java had sooooo much buzz at that time, they renamed the scripting. yak!)
Before you create yet another configuration file or resource format (and yet another parser to accompany it), try Lua.
This is good advice. I created and maintain our company’s manufacturing test application, which initially used an ad hoc scripting engine that I created and extended as needed. But it got unwieldy and I am the only one in the whole world that understands it, so I had been looking for a very simple scripting language that I could plug in as a replacement.
Last year I found Lua! I was very pleased at how manageable the whole task was–which I was able to accomplish fairly quickly. And now it’s in regular use for our most recent product.
Ken, pls. get in touch with me if you want to have IVI or GPIB (test measurement equipment) bindings for Lua.
That is the way I got “into” the language, and I still think there’s a huuuge cake to be eaten (delicious!) in gathering Lua and major measurement tool drivers together. My current job is not inclined to that, however.
http://kotisivu.dnainternet.net/askok/
Well I am using GPIB, but I just call my existing C functions from Lua to do it. I don’t use VISA, SICL or any other intermediate software, but just send SCPI text commands to the interface board’s driver using the documented C API.
Its a very good way to have lightweight scripting, and its bindable to C quite (though not brain-dead) easily.
It does the job really well, and if I had put my scripting engine in before I wrote my map/resource loading i would have used lua instead of XML.
The only thing i don’t really like about it is the error reporting, but maybe thats just because I havent figured out how to report the errors sanely.
WOW has LUA embedded into it so that you can write addons for the game. In fact, if I remember correctly, most on the non-performance critical stuff in Warcraft in written in LUA
> Before you create yet another configuration file or
> resource format (and yet another parser to accompany
> it), try Lua.
Write configuration files and resources in a *turing-equivalent* language? I did that once and learned my lesson: Nontermination, no WYSIWYG editors, very very very limited possibilities to analyze and transform program-enclosed data, …
> Before you create yet another configuration file or
> resource format (and yet another parser to accompany
> it), try Lua.
Before creating yet another embedded scripting language that can be used to parse configuration files or resource formats, try Tcl.
Write configuration files and resources in a *turing-equivalent* language? I did that once and learned my lesson: Nontermination, no WYSIWYG editors, very very very limited possibilities to analyze and transform program-enclosed data, …
In Tcl you can parse an untrusted configuration file in a safe slave interpreter. You can use any subset of Tcl (and your own) commands in the safe interpreter – you can omit all commands except ‘set’ if you wish – so you do not have to have loops or non-termination. You can catch parsing errors and handle them gracefully in the main interpreter (e.g. report that the file has a problem). You can choose either to ignore forbidden commands (such as exec wget trojan) or to throw an error. You can call the slave interpreter from a function in the main interpreter that throws away all variables set by the slave except the ones that you want the configuration file to set. All in just a few lines of code.
If you want loops (e.g. for repetitive configuration), you can set a trace to force the slave interpreter to exit if the number of commands exceeds a certain number. If you want functions, you can specify a recursion limit.
Of course, you have to check the values extracted by the slave interpreter from the untrusted file, and if you want a WYSIWYG editor you have to write it yourself. The same is true if you create your own configuration file format. It might be difficult to analyze and transform program-enclosed data in the WYSIWYG editor, but that depends on how you wrote your program, and if you don’t like this situation you can just use ‘set’ and not bother with real programmability – and still have the same facilities as a custom configuration file format.
Tcl has a bigger foot print than Lua, but it includes such things as regexp pattern matching, which you probably want so you can verify your data input. Also, its codebase is much more mature.
It is something of a mystery why Tcl is not more widely used.