How can I get the meomory of any Process in Lua?
Is it possible in Lua? Is C# it is possible but I am not sure.
In C# we can use PerformanceCounter class to get the
PerformanceCounter WorkingSetMemoryCounter = new PerformanceCounter("Process",
"Working Set", ReqProcess.ProcessName);
What is equivalent of this in Lua?
Suppose there are 5 process of IE (Internet Explorer) running . How to get a List of those process?
To elaborate on what others have said, Lua is a scripting language designed for embedding. That means that it runs inside another application.
Lua.exe is one such application. But it is not the only application that Lua scripts can be written to be executed on.
When within a Lua script, you have access to exactly and only what the surrounding application environment allows. If the application does not explicitly allow you to access things like "files" or "the operating system", then you don't get to access them. Period.
Lua's standard library (which an application can forbid scripts to use. Lua.exe allows it, but there are some embedded environments that do not) is very small. It doesn't offer a lot of amenities, which make Lua ideal for embedded environments: small standard libraries mean smaller executables. Which is why you see a lot more Lua in mobile applications than, say, Python. Also, the standard library is cross-platform, so it does not access platform-specific libraries.
Modules, user-written programs (either in Lua or C/C++) can be loaded into Lua.exe's environment. Such a module could give your Lua script access to things like "processes", how much memory the process is taking, and so forth. But if you do not have access to such a module, then you're not getting that info from within a Lua script.
The most you are going to be able to do is get the size of the memory that this particular Lua environment is directly allocating and using, as #lhf said: collectgarbage "count".
To get the memory allocated by Lua in Kbytes, use collectgarbage"count".
Lua does not comes with this built-in functionality. You could write a binding to a library that provides you that or you could interface with a program to do that (like reading the output of "ps -aux | grep firefox").
Related
I’m trying to use Lua/Moonsharp API scripting. There’s a command library, but there isn’t a function that I need. My question is, am I able to create my own function, or am I limited to what’s been written?
Specifically, for a software called BobCAD a Lua plugin is available. There are lists of commands like Bcc.SetCamObjParameter, though, there isn’t the command that I need. I’m assuming that some aspects of the BobCAD software are inaccessible to API, but am I limited to the library in this plugin, or can I add my own commands? I thought that there would be some C# file somewhere in the program directory where I can read the functions and possibly learn how to create my own, but I don’t see anything like that. (Or are scripting functions set up internally to the software, and I’m only given access to what has been provided?)
(N.b. I'm not familiar with BobCAD specifically, but this answer should be generally applicable.)
Generally speaking, for use cases like this (where Lua scripting is provided as part of a larger program, or by a plugin for a larger program), it's the developer of the program or plugin who decides what API is available to Lua. In the latter case they may also be limited by what the program's plugin API allows; in the case of BobCAD you are likely limited to what the BobCAD plugin API permits, and then further limited by what parts of that API the developers of the Lua plugin chose to make available to Lua itself.
You can of course write your own functions in Lua, but in terms of actually talking to the host program you are restricted to the API that program makes available, unless they make available some mechanism (like LuaJIT's Foreign Function Interface) for reaching into the program from Lua and calling functions that were not made explicitly available -- which most do not.
As for finding "a C# file somewhere in the program directory" -- C# is a compiled language; C# libraries are generally shipped as pre-compiled CLR binaries (with a .dll extension) and do not contain source code. If the source code for BobCAD and/or the Lua plugin is available you could always modify it and re-compile it, but as BobCAD is commercial, closed-source software, I would not expect it to be available.
At that point, your options are basically:
figure out a way to do what you need with the commands that are available;
try to reverse engineer enough of the program to do what you need without access to the source code;
or look for another program that has the features you need.
Hey so I was thinking about learning Lua and this question struck to me, that can I program a game which only has Lua code no C++,C, Or side-libraries of C, C++?
Lua is an embedded scripting language. Like most scripting languages, it runs inside of some application that actually executes what the language says for it to do. So if you want to get technical, it is impossible to run Lua without first running something involving "C", because that "something" is what makes Lua code possible to execute at all.
Because Lua is an embedded scripting language, what you can do with it is ultimately defined by the environment into which it is embedded. lua.exe is one embedded environment, but there are others.
So this question is probably intended to be this: Can I make a game in Lua written against the lua.exe environment, without adding supplemental, non-Lua modules?
Sure: so long as that game works within the limitations of the text console that lua.exe executes within. The standard Lua environment provides access to all of the Lua standard library, but that library is actually quite tiny. The only IO it provides is access to standard in, standard out, standard error, and files.
That's it.
You can make a text adventure game with that. With some knowledge of how to control the text console more directly (which is platform-specific), you can use the console to display ASCII-art graphics. So you could make an old-school Rogue-like, so long as timing isn't a factor, since the Lua standard library has no mechanism for doing that.
Yes, you can make a game of some kind. But the kinds of games you can make with it are probably not what you're thinking of when you said "game". The Lua standard library offers nothing that would allow you to do audio of any kind or anything visual except for sending text to standard out.
Because Lua standard library is intended for any application that needs to embed Lua, it is quite tiny, providing a small baseline of functionality and nothing more. It isn't intended for you to create graphics-intensive applications.
For that, you would need to explore a Lua environment that is designed for that, which provides functionality specific to those needs.
Of course, there are many games made only in Lua and others where you can program mods or servers inside them with Lua only like Roblox or FiveM. If you want to make an entire game using only Lua scripts, you can try using a proper engine for this as for example GameGuru
You can also try some Lua libraries made to program games like Love2D and CoronaSDK
In C language, we use #include statement and using statement in C# to be able to attach its build-in function. But in Lua, we dont need to do anything, then we can use the coroutine, table, io, etc.
It's because Lua "pre-imports" some of the libraries for you. You can re-configure your copy of Lua to load a different set of libraries. See lualib.h file in the Lua distribution for the list of pre-loaded libraries.
The Lua interpreter exports all basic functions and library tables to Lua programs by calling luaL_openlibs before running the program.
EDIT:
Why don't all languages do this?
It's a trade-off. If a language exposes its entire standard library by default, it saves us a lot of boilerplate. On the other hand, it can pollute the namespace, use more memory, and increase startup time. Lua's standard library is small, so it doesn't cost very much. Most compiled languages try to be as lean as possible, so they make us import everything explicitly.
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.
What are the typical use cases of using an embeddable programming language? Do I understand it correctly that such language should be embedded into some program environment and should be able to be executed from there?
Since you tagged the question as "Lua", I'll give you an answer in the context of this language.
Introduction
Lua is written in C (almost completely compatible with C89 standard; the incompatible features can be easily disabled, if needed, using compile-time switches) and has been designed to be easily integrated with C code. In the the context of Lua, "integrated" means two different, but related, things:
You can easily write C code that can be used as a library by Lua code. The integration is achieved either by static or dynamic linking your C code to Lua engine's code. The linked library can then be referred to in your Lua code using the Lua require function.
Lua engine can be easily embedded in a C application, i.e. linked (again either statically or dynamically) to the C application code. Then the C application can interact with the Lua code using Lua's C application programming interface (Lua C-API).
Note: this can be done, with a little more effort, also with a C++ application.
Advantages of embedding a Lua engine
If your C application embeds Lua many, if not most, operations can be delegated to the Lua engine, i.e. either to code written using the C-API functions or, better yet, Lua code. Lua code could be embedded as C strings inside your C code or be stored as external Lua scripts.
Having part of your code logic implemented using Lua code has several advantages:
Lua is simpler (less tricky) to learn and use than C, and it is much more high-level. It supports powerful abstractions, such as function closures and object orientation (in a peculiar way, using Lua tables and metamethods).
Lua is a dynamic language: it requires no "off-line" compilation. You can modify the text of your Lua script and that's all you need to modify your application behavior (no additional compilation+linking steps needed). This simplifies application development and debugging.
Lua is a safer language than C: it is really difficult to write Lua code that exhibits undefined behavior, as intended in the context of C/C++. If a Lua script fails, it fails "loudly". Moreover Lua supports an exception mechanism (although with a different syntax than C++) which can be employed to implement error management in a much easier way compared to C.
Lua, as most dynamic languages, is garbage collected. This means that the programmer is spared the pain of manually managing dynamic memory, which is a major cause of bugs, leaks, instability and security loopholes in languages that lack garbage collection.
Lua can "eat its own dog food", i.e. you can build a string at runtime (even in Lua itself) and if it is valid Lua code, your program can execute it on the fly. This is something not frequently seen even in other dynamic languages (still it is not LISP, but it gets closer, and with much more readable syntax). This enables Lua scripts to:
employ powerful text-based metaprogramming techniques, where Lua code can generate other Lua code and execute it on the fly;
implement domain specific languages (DSLs) in an easy way; Lua code can load at runtime other Lua code that is crafted so as to reflect the specific problem domain in which it is used (Lua syntax is simple, yet flexible enough to allow such things);
be used as a configuration language with ease: your application (written in a mix of C and Lua) can use some lua files as configuration files without the need to craft an ad-hoc parser for a specific configuration file format. Therefore you don't need to parse *.properties, *.csv, *.ini, or whichever other format you would choose if you hadn't the option of using Lua files for that purpose.
Lua engine has a very small memory footprint (some hundreds kBs), packing powerful capabilities. With very few C code lines and a bunch of Lua files you could create a complete application that would require thousands of C code lines otherwise. The standard Lua standalone interpreter can be seen as just an example of embedding Lua in a C application!
Lua has a very liberal open-source license, which enables its use even in commercial applications without much hassle. This also allows the modification of its source code to adapt it to special needs.
Small memory footprint and easily tweakable C sources make Lua a perfect candidate for porting it on embedded systems or small microcomputer systems (microcontrollers, etc.). Many parts of the standard Lua distributions can be stripped off, reducing the core Lua engine in the ~100kB range. As an example, take the eLua project, a modified distribution of Lua designed for embedded devices.
Lua, and other scripting languages, provide various benefits that are dependant on your needs.
Provide rapid iteration of development.
Allow run-time code changes, such as reloading your UI in World of Warcraft which re-loads all scripts without stopping the game engine itself or logging you out.
Provide a distinct API for your application for users to extend, without exposing critical parts of your system to the public. Such as text editors providing a macro language to allow you to integrate custom behaviour without giving you unfettered access to the internals of the editor itself.
The uses are really quite extensive and depends on the developer.