ROBLOX Studio: How do I run this Lua Bytecode VM? - lua

For me, I want to learn how VM's work and if they can only be ran on Mac. I've found a bytecode vm (from lua) in roblox studio as one of the scripts. I'm very confused on how to use it, its nothing like i've used before. Here it is:
https://web.roblox.com/library/117513593/EpicLua-Lua-5-1-VM
Also if you could tell me more about VM's that would also help me during this process.

Looks like it's plug-and-play.
Require the module in a script/command line on Roblox. It will return a function which you can call with a string and desired environment (optional), which again returns a function you can run to run the string-code.
Example:
local LoadString = require(module)
local func = LoadString("print('Hello world!')")
func() -- prints "Hello world!"

Related

Roblox Run Script From String in variable

I'm trying to make a ss script that runs by turning the text inside the TextBox into a variable and running the variable as a script, how do I do this?
I tried to use loadstring but it didn't work, what do I do?
script.Parent.MouseButton1Down:Connect(function()
local script = script.Parent.Parent.TextBox.Text
loadstring(script)
end)
If no errors occur loadstring returns the loaded chunk as a function.
You failed to call that function.
assert(loadstring(script))()
Please read the Lua manual. https://www.lua.org/manual/5.1/manual.html#pdf-loadstring

"attempt to call global 'tonumber' (a nil value)" in Lua, embedded (in VLC)

I use VLC media player 1.1.9 on Ubuntu 11.04. I'm trying to experiment with lua extensions for VLC; so I've added the file test.lua in ~/.local/share/vlc/lua/extensions/, which has only these two lines:
fps="25.000"
frame_duration=1/tonumber(fps)
When I run vlc with verbose output for debugging, I get (edited to split on multiple lines:):
$ vlc --verbose 2
...
[0xa213874] lua generic warning: Error loading script
~/.local/share/vlc/lua/extensions/test.lua:
.../.local/share/vlc/lua/extensions/test.lua:2:
attempt to call global 'tonumber' (a nil value)
...
Now, as far as I know, tonumber as function is part of Lua5.1 proper (Lua 5.1 Reference Manual: tonumber) - and on my system:
$ locate --regex 'lua.*so.*' | head -4
/usr/lib/libipelua.so.7.0.10
/usr/lib/liblua5.1.so
/usr/lib/liblua5.1.so.0
/usr/lib/liblua5.1.so.0.0.0
... apparently I do have Lua 5.1 installed.
So, why do I get an error on using tonumber here - and how can I use this (and other) standard functions in a VLC lua extension properly?
Documentation is sparse for VLC Lua extensions to say the least but I did find an example in the github vlc repository here: https://github.com/videolan/vlc/blob/master/share/lua/extensions/VLSub.lua
Judging from that example it appears you need to supply some basic event functions for your addon for VLC to call into when certain events happen. Some of the obvious callback handlers I've noticed:
descriptor, this should return a table that contains fields describing your addon.
activate, this seems to get called when you activate it from view menubar.
deactivate, called when you deactivate the addon from view menubar.
plus a couple of other functions like close and input_change which you can guess what they're for.
From my brief testing done on VLC 2.0.8 under Win7 it appears VLC loads the lua extension using an empty sandbox environment. This is likely the reason you're getting nil for tonumber and I'm betting none of the other standard lua functions are accessible either when you try to perform computation at this global scope.
However, if I move that code into one of the event handling functions then all those standard functions are accessible again. For example:
function descriptor()
return
{
title = "Test Ext";
version = "0.1";
author = "";
shortdesc = "Testing Lua Extension";
capabilities = {};
description = "VLC Hello Test Addon";
}
end
function activate()
print "test activating"
local fps = tonumber "25.000"
local frame_duration = 1 / fps
print(frame_duration)
return true
end
-- ...
That prints out what you would expect in the console debug log. Now the documentation (what little there is) doesn't mention any of this but what's probably happening here is VLC is injecting the standard lua functions and vlc api table into the sandboxed environment when any of these event handlers get called. But during the extension loading phase, it is done in an empty sandbox environment which explains why all those lua function calls end up being nil when you try to use it at the outter most scope.
I recommend cloning the VLC source tree from github and then performing a grep on the C source that's embedding lua to see what VLC is really doing behind the scenes. Most of the relevant code will likely be here: https://github.com/videolan/vlc/tree/master/modules/lua
Probably some extension script installed in your system overwrites the function and the Lua interpreter instance is shared between all extension scripts, so you end up not being able to call the function if that script is called before yours.
As a quick workaround, Lua being dynamically typed, you can still do things like:
1 / "25.000"
and the string will be coerced to a number.
Alternatively, you can define a tonumber equivalent like:
string_to_num = function(s) return s + 0 end
This again relies on dynamic typing.

require function fails at lua_load with SYNTAX ERR

I am new to Lua. I have ported Lua 5.2 to another platform. I am able to run Lua scripts.
But have a problem with loading modules.
I have a sample.lua script below. In this script I am trying to call a function available in another Lua script using the require function. The require function fails at lua_load() with LUA_ERRSYNTAX. Not sure if this is problem with Lua 5.2 version.
sample.lua
require"module"
welcome()
module.lua
function welcome()
print(" Hello in module")
end
The require function in sample.lua file locates the module.lua file but fails at lua_load().
Can anyone help me with this issue ?
Could You be more specific about those different OS'es? Firstly, You should compile Lua for exactly the same OS and device Your are running *.lua scripts on. Secondly, You should check module.lua for some weird typos and invisible symbols. String.char(195) doesn't seem very visible. Also, could You check the enconding of that/those two Lua scripts?

os.execute not working with love2d

I'm trying to use os.execute(), but I'm getting this problem:
attempt to call field 'execute' (a nil value)
I've done os = require 'os' but when I do: os.execute("mkdir" .. var) it's giving me the above error.
From what I've worked out, it's because it's not calling in all of the stuff from os, but I've looked and I can't figure out how to call in everything from os. In python i'd do from os import * but I don't know the code for lua. I've tried using package.loadlib('os', 'execute') but that didn't work. :/
EDIT: I did a separate test with love2D, and it worked. so I can't see why this isn't working...
are you sure you don't have code somewhere that is overwritting/reassigning the os.execute function in the problem script? do you have any local table objects named os?
try this:
require 'os';
if package.loaded['os'] and type(package.loaded['os']) == "table" then
local os = package.loaded['os'];
--from here use the local os variable to call anything inside os.
--main block of code
end
I hope this works for you.

Reboot a System through Lua Script

I need to reboot System through a Lua Script.
I need to write some string before the Reboot happens and need to write a string in a Lua
script once the Reboot is done.
Example :
print("Before Reboot System")
Reboot the System through Lua script
print("After Reboot System")
How would I accomplish this?
You can use os.execute to issue system commands. For Windows, it's shutdown -r, for Posix systems it's just reboot. Your Lua code will therefore look like this:
Be aware that part of the reboot command is stopping active programs, like your Lua script. That means that any data stored in RAM will be lost. You need to write any data you want to keep to disk, using, for instance, table serialization.
Unfortunately, without more knowledge of your environment, I can't tell you how to call the script again. You could be able to append a call to your script to the end of ~/.bashrc or similar.
Make sure that loading this data and starting at a point after you call your reboot function is the first thing that you do when you come back! You don't want to get stuck in an endless reboot loop where the first thing your computer does when it turns on is to turn itself off. Something like this should work:
local function is_rebooted()
-- Presence of file indicates reboot status
if io.open("Rebooted.txt", "r") then
os.remove("Rebooted.txt")
return true
else
return false
end
end
local function reboot_system()
local f = assert(io.open("Rebooted.txt", "w"))
f:write("Restarted! Call On_Reboot()")
-- Do something to make sure the script is called upon reboot here
-- First line of package.config is directory separator
-- Assume that '\' means it's Windows
local is_windows = string.find(_G.package.config:sub(1,1), "\\")
if is_windows then
os.execute("shutdown -r");
else
os.execute("reboot")
end
end
local function before_reboot()
print("Before Reboot System")
reboot_system()
end
local function after_reboot()
print("After Reboot System")
end
-- Execution begins here !
if not is_rebooted() then
before_reboot()
else
after_reboot()
end
(Warning - untested code. I didn't feel like rebooting. :)
There is no way to do what you are asking in Lua. You may be able to do this using os.execute depending on your system and set up but Lua's libraries only include what is possible in the standard c libraries which does not include operating system specific functionality like restart.

Resources