Is there any way I can get Erlang to forget a built in function so I can use that name?
eg. forget retrieve
There's a bit of confusion here.
retrieve is not a built-in function, you may be thinking of receive
receive is not a built-in function, but a special token in the language, much like if, case, end, and so on. These cannot be modified.
BIFs are mostly implemented in the erlang module, and you cannot redefine this one
Many of the BIFs in erlang are auto-imported in modules and such. Any module-local definition will take over these, and otherwise they're syntactic shortcuts for erlang:MyBif(...).
The shell replicates these auto-imports, but also provides additional functions. They are technically not BIFs. See Shell Commands
You can override the auto-imports for the shell by configuring your own user_default module. These will only work in the shell.
To avoid auto-imports in modules, use the -compile({no_auto_import,[Name/N]}). module attribute, so that Name(...) always uses the local function.
Related
I'm trying to write a mapreduce query in erlang for riak but I'm having trouble getting my head around it. Does anyone know where I can find an example of an erlang mapreduce query, or can write one, that will perform the SQL equivalent of a count operation? It would also be helpful if someone could explain what the actual query does line-by-line too. I've managed to write one in js but erlang is pretty different. Thank you.
Riak comes with a set of predefined mapreduce functions implemented in Erlang that you can use as a guide if you are trying to write your own functions. One of the functions provided is
reduce_count_inputs, which counts inputs (as long as the input are not integers) and might be useful for your scenario.
I have also created a library of map phase functions implemented in Erlang that you can look at.
Although I believe it is possible to pass in Erlang functions as part of the mapreduce job specification in a similar way to how you send anonymous JavaScript function, it is usually not recommended, and I have not done this myself.
I always look into riak sources to find some good examples.
Here module which implement standard mapreduce funs: riak_kv_mapreduce
This is a simples one, which just returns value of the object.
I am new to Lua and want to ask whether it is possible to restrict lua syntax in config file? I know that config loading have to be performed in jail, but how we can cope with while 1 do end in config file we want to load? Is there a way to allow only strings, assignments and tables in config and if not, then what is the best way to check that lua file doesn't contain undesirable constructs? Is manual pre-parsing the only solution?
You seem to already know about "sandboxing" in Lua. So what's left is as you say malicious constructs like infinite loops. And to solve that you need to solve the Halting Problem. Which is not practical.
Instead of "manually" parsing and hoping you find all the malicious content (you won't), how about just running your Lua interpreter with a timer set so that the script will be interrupted if it takes longer than N seconds?
If you want to explicitly forbid certain constructs in Lua, you have to actually scan the file yourself. Note that there are valid uses for those constructs, even in config files, so you are restricting what the user can do.
It wouldn't be too hard to write a simple Lua lexer that ignores the contents of strings and comments, but errors on any of the Lua keywords other than return. Given proper sandboxing (ie: no functions are available to be called), that should be sufficient to weed out anything malicious.
Also, note that Lua 5.1 doesn't make it easy to keep the parser from parsing non-text data (ie: compiled Lua bytecode). 5.2 offers specific API support for forcing the loader to only recognize text and therefore reject bytecode.
Is there any way to retrieve a list of open namespaces and modules in an FSI Session? I'm playing around with printing an F# quoted expression and I'd like to be able to distinguish between those values of a module which should be printed fully qualified versus those which should not.
I don't think there is a way to do this.
The fsi object doesn't have any way of doing that, so the simple approach like this won't work. I believe that internally, the information is stored in the FsiDynamicCompilerState record that F# Interactive uses to keep current state of the interaction (you can find it in "fsi.fs" in the sources).
Unfortunatelly, I didn't find any static property that you could use to access this state - it is kept as a field in FsiDynamicCompiler which is created in the main function and not stored anywhere else. In principle, you may want to be able to access the instance via stack trace (but I couldn't find a way to get local variables of a stack frame).
If you wanted to modify fsi.exe to make this possible, that would be another question, but I suppose that's not what you want.
Many games these days make available some lua scripting, but this is universally undocumented.
So let's say I can get a game to run my lua script (it's lua 5.1) - and the script can write what it finds to text files on disk. How much can I discover about environment the script is executing in?
For example it seems I can list keys in tables, and find out what's a function and what's some other type of object, but there's no obvious way to guess how many arguments function takes (and a mistake usually results in crash to desktop).
Most languages provide some reflection functionality that could be used here - how much is possible in embedded lua environment?
"debug" standard library has some functions, which you may find useful:
debug.getfenv - Returns the environment of object.
debug.getinfo - Returns a table with information about a function.
... and more
Lua Reference Manual also states:
several of these functions violate some assumptions about Lua code (e.g., that variables local to a function cannot be accessed from outside or that userdata metatables cannot be changed by Lua code) and therefore can compromise otherwise secure code.
So with debug library, you may access more.
Unfortunately, there is not much you can learn about functions in Lua - they by design accept any number of parameters. Without the ability to look at the sources, your only resort is the documentation and/or other samples.
The most you can do in this case is traverse the entire _G table recursively and dump every table/function, printing the results to a file.
"A mistake usually results in crash to desktop" is a sign of a really bad design - good API should tell you, that it expects A, and you passed B. For example in Lqt, a Qt binding to Lua, we check every parameter against the original Qt API, so that the programmer is notified of mistakes:
> QApplication.setFont(1, 2)
QApplication::setFont(number, number): incorrect or extra arguments, expecting: QFont*,string,.
Folks, is there a way to clone a Lua state?
In my game application the initialization procedure of the Lua virtual machine is pretty heavy(about 1 sec, since many scripts are loaded at once). I have a separate Lua VM for each autonomous agent and once the agent is created its Lua initialization affects FPS pretty badly.
I'm thinking about the following schema: what about keeping "preforked" Lua state which is then simply cloned for each agent? Is it possible?
You want to consider using Lua's coroutines for each autonomous agent, instead of a completely separate VM. Coroutines are a more lightweight solution, but may or may not be suitable for your application.
If you can't change the architecture, you might try LuaJIT. It might make the initialization fast enough for your purposes.
More options:
Rings: "Rings is a library which provides a way to create new Lua states from within Lua. It also offers a simple way to communicate between the creator (master) and the created (slave) states."
Pluto: "Pluto is a library which allows users to write arbitrarily large portions of the "Lua universe" into a flat file, and later read them back into the same or a different Lua universe."
There's also Lanes (download, docs) and within the comparison to all similar products I know.
About Rings the comparison sheet says:
Rings offers separate Lua states, but
no multithreading. This makes it
simple, but it won't use more than one
CPU core.
Note: The comparison sheet says Lanes would only marshal 'non-cyclic tables'. It does do cycles, and does marshall functions, upvalues etc. And it does the copies between Lua states as direct copies, not needing to stringify the contents in the middle. This makes it fast.
If you're on Linux, you may try lper, LPSM-based experimental library by one of Lua authors.
Notice, works with Lua 5.2 and above
You can just restrict access to this VM. Create one instance with all functions required, that will not depend on _G (global Lua state) and then create separate table for each client. That they will use as their global namespace. Setting a table as current _G is possible via _ENV. That's quite difficult topic to explain in one post. In short you prepare "virtual" _G for your new client and then just replace _G for the client's code. There is where I advice you to start.
Here's the point.
local _ENV = t -- change the environment. without the local, this would change the environment for the entire chunk
Just remove local and you'll change _ENV for all further code. Good luck with experiments!
P. S. don't forget that you may set metatable for _ENV and _G tables and forbid changing that metatable. Lua is really flexible here.