My favourite language these days is Lua. I have only one issue with it, why on earth is its default behaviour that variables in functions are global? In the similar language Icon there is a keyword "global" that is used when one really wants to use a global instead of the natural behaviour to default to local (I was bitten by this again five minutes ago). I would feel better about this if somebody could tell me the rational behind it (like the scoping difficulties that I know cause the absence of a "continue" keyword in Lua).
See Why aren't variables locally scoped by default? in the Lua uFAQ.
It certainly feels easy to only explicitly declare globals when they are in a local context. The short answer is that Lua is not Python, but there are actually good reasons why lexically-scoped local variables have to be explicitly declared. See the wiki page.
Related
I have some Lua code that I suspect is obfuscated. How do I go about de-obfuscating it?
I believe the code is obfuscated because it looks very different from normal Lua code, but I know it is valid Lua code because the Lua interpreter will still compile and run the code.
I have a legitimate interest in de-obfuscating the code and do not intend to distribute it against the authors will or modify it to circumvent any DRM-mechanism.
There are generally two ways to obfuscate Lua source code:
Obfuscate the code directly, mostly by renaming variables, introducing istraction and restructuring code to be harder to follow
Encode the source code and embed it as a string in a Lua file that only decodes, loads and runs the encoded real program.
In reality, a combination of both is often used: Programs are obfuscated, then encoded and wrapped in a string. Finally, the code that loads and runs the string is often obfuscated again.
Typical mechanisms used for making Lua code harder to follow include:
Renaming standard functions such as string.gsub, table.concat, etc.
Renaming variables to nonsense
Replacing dot- and colon-notation for table-indices with bracket-notation
Using hexadecimal notation for literal strings (often in combination with 3.)
Generally speaking, the steps to de-obfuscate such code by hand are often very similar: reformatting the code to make is easier to follow the control-flow, then figuring out what each variable represents and renaming them. For this it is often necessary to have a good understanding of the Language, as one needs to be aware of all the rules that the obfuscation takes advantage of to make the code harder to understand. A few such rules to be aware of:
Local variable shadowing: two different variables can have the same name in different scopes (or even in the same scope).
Syntactic sugar such as dot- and colon-notation
Function environments and getfenv and setfenv
Metatables and that all Strings share one metatable with __index set to string
Whitespace is often insignificant in Lua and only necessary to separate statements in some cases, which can also be done with ;.
For more in-detail help with de-obfuscating a specific snippet of Lua code, you could ask in the following other online communities:
The Lua subreddit
The Lua Scripters Discord Server
The Lua Forum
But remember: Don't ask to ask, just ask
Note that these are not official communities. For more options, see the Community page on the official Lua website.
By that I mean being able to do batch renaming in some local scope rather than having to do each place by hand. In my F# programming, I spent quite a lot of time doing it like that or using find/replace only to be amazed how great it is to have IDE support for renaming.
Agda is kind of a special case among languages as its variables can have special symbols that are difficult to type out, so that feature would be even more beneficial than is usual.
What is the prefered way to do renaming at the moment?
A couple of my friends and I run a game which allows scripting using Lua. This is implemented through LuaJ which is a java program implementing the Lua language. I recently learned native Lua has an issue with bytecode manipulation where you can change pieces of the bytecode and run it back using loadstring to access variables and environments normally out of a functions scope even to the point of accessing functions in C that Lua is written on. This could possibly prove hazardous for our game so I tried to convince my friends we need to disable loadstring however they said since we use LuaJ and not Lua that there is no way the bug exists in our version too. I don't like assuming something cant be broken just because its different from something else that can be. Would anyone here happen to know how to set up some pentests to try and manipulate LuaJ bytecode and see if it is just as vulnerable as native Lua? Below are some links from a similar hack that almost hit a game called ROBLOX. Their system was written on the native Lua not LuaJ.
http://www.corsix.org/content/lua-514-bug-recap
http://www.corsix.org/lua/bytecode_abuse_0_1.lua
my biggest concern with the game as everything should be sandboxed is one bytecode manipulation as demonstrated in the top link
Accessing other function's locals
The VM call instruction is traditionally done at the top of the stack. However, through bytecode manipulation, it can be done in the middle of the stack, and then after the call is complete, any locals used by the called function will be left at the top of the stack.
By this I meant: when you design your app side effects free, etc, will F# code be automatically distributed across all cores?
No, I'm afraid not. Given that F# isn't a pure functional language (in the strictest sense), it would be rather difficult to do so I believe. The primary way to make good use of parallelism in F# is to use Async Workflows (mainly via the Async module I believe). The TPL (Task Parallel Library), which is being introduced with .NET 4.0, is going to fulfil a similar role in F# (though notably it can be used in all .NET languages equally well), though I can't say I'm sure exactly how it's going to integrate with the existing async framework. Perhaps Microsoft will simply advise the use of the TPL for everything, or maybe they will leave both as an option and one will eventually become the de facto standard...
Anyway, here are a few articles on asynchronous programming/workflows in F# to get you started.
http://blogs.msdn.com/dsyme/archive/2007/10/11/introducing-f-asynchronous-workflows.aspx
http://strangelights.com/blog/archive/2007/09/29/1597.aspx
http://www.infoq.com/articles/pickering-fsharp-async
F# does not make it automatic, it just makes it easy.
Yet another chance to link to Luca's PDC talk. Eight minutes starting at 52:20 are an awesome demo of F# async workflows. It rocks!
No, I'm pretty sure that it won't automatically parallelise for you. It would have to know that your code was side-effect free, which could be hard to prove, for one thing.
Of course, F# can make it easier to parallelise your code, particularly if you don't have any side effects... but that's a different matter.
Like the others mentioned, F# will not automatically scale across cores and will still require a framework such as the port of ParallelFX that Josh mentioned.
F# is commonly associated with potential for parallel processing because it defaults to objects being immutable, removing the need for locking for many scenarios.
On purity annotations: Code Contracts have a Pure attribute. I remember hearing the some parts of the BCL already use this. Potentially, this attribute could be used by parallellization frameworks as well, but I'm not aware of such work at this point. Also, I' not even sure how well code contacts are usable from within F#, so a lot of unknowns here.
Still, it will be interesting to see how all this stuff comes together.
No it will not. You must still explicitly marshal calls to other threads via one of the many mechanisms supported by F#.
My understanding is that it won't but Parallel Extensions is being modified to make it consumable by F#. Which won't make it automatically multi-thread it, should make it very easy to achieve.
Well, you have your answer, but I just wanted to add that I think this is the most significant limitation of F# stemming from the fact that it is a hybrid imperative/functional language.
I would like to see some extension to F# that declares a function to be pure. That is, it has no side-effects that are not denoted by the function's type. The idea would be that a function is pure only if it references other "known-pure" functions. Of course, this would only be useful if it were then possible to require that a delegate passed as a function parameter references a pure function.
I am trying to build out a useful 3d game engine out of the Ogre3d rendering engine for mocking up some of the ideas i have come up with and have come to a bit of a crossroads. There are a number of scripting languages that are available and i was wondering if there were one or two that were vetted and had a proper following.
LUA and Squirrel seem to be the more vetted, but im open to any and all.
Optimally it would be best if there were a compiled form for the language for distribution and ease of loading.
One interesting option is stackless-python. This was used in the Eve-Online game.
The syntax is a matter of taste, Lua is like Javascript but with curly braces replaced with Pascal-like keywords. It has the nice syntactic feature that semicolons are never required but whitespace is still not significant, so you can even remove all line breaks and have it still work. As someone who started with C I'd say Python is the one with esoteric syntax compared to all the other languages.
LuaJIT is also around 10 times as fast as Python and the Lua interpreter is much much smaller (150kb or around 15k lines of C which you can actually read through and understand). You can let the user script your game without having to embed a massive language. On the other hand if you rip the parser part out of Lua it becomes even smaller.
The Python/C API manual is longer than the whole Lua manual (including the Lua/C API).
Another reason for Lua is the built-in support for coroutines (co-operative multitasking within the one OS thread). It allows one to have like 1000's of seemingly individual scripts running very fast alongside each other. Like one script per monster/weapon or so.
( Why do people write Lua in upper case so much on SO? It's "Lua" (see here). )
One more vote for Lua. Small, fast, easy to integrate, what's important for modern consoles - you can easily control its memory operations.
I'd go with Lua since writing bindings is extremely easy, the license is very friendly (MIT) and existing libraries also tend to be under said license. Scheme is also nice and easy to bind which is why it was chosen for the Gimp image editor for example. But Lua is simply great. World of Warcraft uses it, as a very high profile example. LuaJIT gives you native-compiled performance. It's less than an order of magnitude from pure C.
I wouldn't recommend LUA, it has a peculiar syntax so takes some time to get used to. Depending on who will be doing the scripting, this may not be a problem, but I would try to use something fairly accessible.
I would probably choose python. It normally compiles to bytecode, so you would need to embed the interpreter. However, if you must you can use PyPy to for example translate the code to C, and then compile it.
Embedding the interpreter is no issue. I am more interested in features and performance at this point in time. LUA and Squirrel are both interpreted, which is nice because one of the games i am building out is to include modifiable code, which has an editor in game.
I would love to hear about python, as i have seen its use within the battlefield series i believe.
python is also nice because it has actual OGRE bindings, just in case you need to modify something lower-level on the fly. I don't know of any equivalent bindings for Lua.
Since it's a C++ library, I would suggest either JavaScript or Squirrel, the latter being my personal favorite of the two for being even closer to C++, in particular to how it handles tables/structs and classes. It would be the easiest to get used to for a C++ coder because of all the similarities.
However, if you go with JavaScript and find an HTML5 version of Ogre3D, you should be able to port your game code directly into the web version with minimal (if any) changes necessary.
Both of these are a good choice, and both have their pros and cons, but both would definitely be the easiest to learn since you're likely already working in C++. If you're working with Java, the same may hold true, and if it's Game Maker, you wouldn't need either one unless you're trying to make an executable that people wouldn't need Game Maker itself to run, in which case, good luck finding an extension to run either of these.