17

Considering the following requirementes:

  • Must be supported on Windows. Preferably works also on other platforms.
  • Must support multi threading. By that I mean that the engine can work in parallel in multiple threads.
  • Readability is important.
  • License must be compatible with closed-source projects.

I like Python for its readablity. I am also have more experience with Python than other scripting languages. However CPython is not multi-threaded, and IronPython requires hosting the CLR and a compatible language (C++/CLI or C#).

John
  • 5,561
  • 1
  • 23
  • 39
  • 1
    Well, it *is* multi threaded. But yeah, if you want to use threading for concurrency, then CPython is not the right choice. But is this really an issue when embedding? Can you expand on the usecase some more? – Lennart Regebro Jan 21 '11 at 07:59
  • I have a few worker threads and a UI thread, both workers and UI will have parts written in C++, but as much of the code as possible should be extensible and modifiable without re-compilation. – John Jan 21 '11 at 08:13
  • From what I read stackless uses cooperative scheduling which means its microthreads are similar to fibers. My goal with multi-threading is to use multiple CPU cores at the same time. – John Jan 21 '11 at 17:03
  • It sounds to me more that you want to embed C/C++ in a scripting language. :) – Lennart Regebro Jan 29 '11 at 19:21
  • Doesn't it depend on what "parallel" means? I use threaded python for I/O bound tasks all the time and it works great. I don't think protein folding would go nearly as well... – whitey04 Jan 31 '11 at 00:41
  • 1
    Javascript doesn't support threading... – Endophage Jan 31 '11 at 22:23
  • I would think about which scripting language your target audience would have an easier time learning, or be likelier to come already equipped with. – Ates Goral Feb 01 '11 at 07:21

12 Answers12

29

Lua might be worth checking out. It can be used in a thread-safe manner, and the language supports a 'co-routine' concept which might fit your requirements.

Paul Dixon
  • 295,876
  • 54
  • 310
  • 348
  • 1
    Also, LUA has been explicitly designed for in-application scripting, and there is a good number of performance-optimized, multi-threaded, graphics-intensive C++ applications (AKA "video games") out there using LUA for user scripting. You also get to choose which parts you expose to the scripting side. – foo Jan 21 '11 at 08:14
  • Co-routines are not running concurrently. – John Jan 21 '11 at 16:49
  • lua can be used in a thread safe manner by serializing all access to lua_State objects. This means co-routines are effectively single threaded anyway. – Chris Becke Jan 23 '11 at 13:29
  • Lua's simple syntax makes it less enjoyable to _write_ than Ruby (or, I assume, Python), but makes it very _readable_. – Phrogz Jan 29 '11 at 17:09
  • 2
    Take a look at Lua Lanes: http://luaforge.net/projects/lanes/ for multi-core threading. – sworoc Feb 01 '11 at 21:56
  • Lua is not multithreading. Multithreading is defined by using the same memory space. And the performance of serializing all access to lua_State is a joke recommendation. It reduces performance to 1/10 to 1/50 of the speed. It only was a theoretical test. – Lothar Nov 25 '14 at 14:54
12

Lua is the best choice. Python, Ruby and JavaScript are big languages and they are not designed for to embed. But Lua is different, designed to embed.

You should consider the "restriction" more than any other things for your script language. Embed scripts can use for hack (bad meaning) easily.

For example, by default Lua can not print to console. As I know, Blizzard uses the Lua because of that.

Uordek
  • 121
  • 3
  • "For example, by default Lua can not print to console." You can however easily avoid unwanted functions by setting them to nil, or not loading the containing library at all in your initialization code. – jpjacobs Jan 26 '11 at 22:18
  • What I meant to say is, that's not true: by default, Lua can print to console – jpjacobs Jan 30 '11 at 20:22
  • 4
    I agree that Lua is a good choice, but both Python and JavaScript *were* designed to be embedded. Python was designed to also work well as a standalone, but it has always been quite embeddable by design. JavaScript was embedded (in web browsers) from day 1; stand-alone JavaScript is a relatively recent invention. – Michael Ekstrand Feb 01 '11 at 12:54
11

I've been in the same dilemma an choose Lua over Python and JScript. The thing which Lua does best is the great interop with C/C++ code using libraries like luabridge and luabind. That is, you can call lua from C++ and have the script call back into C++ without a problem, access c++ data from the script and vice versa.

The problem with languages like Python and Lua is that the language is not really multi-threaded in the regular sense of the word: if one C++ thread it using the language scripting engine to run a script, you cannot use the same engine to run another script. Both languages has an engine-wide lock which can be used in those cases to make sure the engine integrity is maintained. However, both languages are multi-threaded in the sense that you can run function in the background and interact with any synchronization object you want (just like from C++). So I choose to have all threads created from C++ and scripting code only run in a dedicated threads (thread per engine) and interact with other threads in the application in the regular ways.

If you need to pass data and control from C++ to a script and vice versa, Lua is much better than Python. Beside that, I would not host the CLR in a C++ project. It's too messy.

Uri Cohen
  • 3,488
  • 1
  • 29
  • 46
9

You might consider embedding a popular JavaScript engine. Not only will they be fast and well-supported, but so many people know how to program in JavaScript that it will be easily adopted and read by a large audience.

According to this answer the SpiderMonkey engine is thread-safe, while Google/Chrome's V8 may not be.

Community
  • 1
  • 1
Phrogz
  • 296,393
  • 112
  • 651
  • 745
  • Seconded... also, running scripts as "workers" may be a better use as well. SpiderMonkey also has the E4X ace up its sleeve as well. Though V8 has CommonJS and a lot of rapidly growing libraries that may not fit with moz embedded. – Tracker1 Jan 30 '11 at 11:03
  • +1, javascript is very popular, and FAST! – arthurprs Feb 01 '11 at 05:47
4

JScript is a great solution. It is already supported with Windows Script Host and emulates multithreading with events. It is easier to use than Python, I promise you. This MSDN article is a great reference.

4

One could use Guile, which is embeddable Scheme.

Andrew Amis
  • 142
  • 1
  • 4
  • 1
    Does not address the requirements. I do not consider lisp as a readable language. And GPL is not compatible with closed-source projects. – John Jan 31 '11 at 01:07
  • It is LGPL, and is perfectly compatible with closed-source projects. – Thomas Feb 11 '11 at 13:25
  • 1
    I do consider Lisp a readable language. Now what? ;-) – Thomas May 29 '11 at 17:01
  • Since Guile is a VM it supports other languages besides Scheme. ECMAscript is the only one that is officially supported I believe but there is a git branch that supports Lua as well. – Andreas Raster Apr 23 '12 at 11:05
2

Lua's really easy to integrate, and can work in multiple threads using something like Lua Lanes (which is cross platform Windows/ Linux/ MacOS).

jpjacobs
  • 9,359
  • 36
  • 45
1

You can try spidermonkey. Check out the Libjspp C++ template based wrapper for embedding and extending Javascript engine spidermonkey: http://code.google.com/p/libjspp/

Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Anand Rathi
  • 790
  • 4
  • 11
1

Im pretty sure Stackless Python is going to be the only one supporting multithreading out the box. Stackless Python was chosen by CCP for their MMO: Eve-Online specifically because the stackless nature of the code allowed them to schedule the continuations on any OS thread they needed once they'd built the necessary primitives in to make the whole thing thread safe.

Lua can be used in a multi threaded environment, but each concurrent thread would need a separate lua_State object so you'd need to build your own interthread message passing system for lua code, executing in the context of separate lua states, to communicate.

Chris Becke
  • 34,244
  • 12
  • 79
  • 148
0

Try Falcon programming language

Sergey
  • 1
0

Entirely preference-based. Most languages have a way to embed in C with options for exports into the scripting environment.

If it were me, I'd go with V8 Javascript.

Fordi
  • 2,798
  • 25
  • 20
0

By "multithreaded" I'm assuming you mean "can effectively exploit multiple cores"? If your system only has a single CPU, then you're only going to be done one thing at a time regardless of the scripting language you choose.

If you do decide to go the CPython route, then there the main thing to remember is that it is only the scripting engine itself that is protected by the global interpreter lock. If the script being executed spends a lot of time calling out to non-Python code (including I/O and other system level operations) then it can exploit multiple threads quite happily.

Another factor to consider is that Python's introspection capabilities make it inherently difficult to secure properly (Google have done it for AppEngine, but they had to disallow quite a few things in the process).

Given the prevalence of Javascript engines in browsers and Lua engines in PC games, one of those is likely to be an easier way forward.

ncoghlan
  • 40,168
  • 10
  • 71
  • 80