1
votes

Ok, I'm wanting to know if there's a way of running scripts from an external source with Lua. Be it another file in .txt format or from pastebin, what have you, and run the code and wait for said code to finish, and then continue on with the rest of the function. I'm not quite sure at all about how it'd work, but this is basically the idea I'm going by and isn't actual code.

function runStuff()
print("checking for stuff")
run.script(derp.txt)
wait for script
   if run.script == finished
   continue program
      elseif nil
   print("looks like this script sucks.")
   end
end
runStuff()

And for example what "derp.txt" contains is:

function lookFile()
   if herp.txt exists then
      rename herp.txt to herp.lua
   else
      nil
   end
end
lookFile()

I'm still new to Lua so I'm coding like a moron here, but you get my picture hopefully. I'm working on a project that'll act like an installer package for repositories based from pastebin or anywhere else that'll supply raw format outputs of lua scripts and I'm going to use that idea for it to call on scripts to run externally. So when I supply a "First Time Run" version of the program, it'll call out to a lua script that'll call to another lua script and install that script, then close.

This is for minecraft, mind you. ComputerCraft made me take interest in Lua, but anyway, hopefully you got the gist of what I'm trying to figure out. Hopefully that's doable and if not, I'll just have to figure something else out.

1
take a look at loadfile or dofile. And try to at least skim through the documentations before posting on SO. - Bartek Banachewicz
So would loadfile work from a pastebin raw link? What I'm wanting to do here is run a script from an external source, not load a file in particular but open up said document and read the script inside and run that script. So for example running a script that's on pastebin, rather than just grabbing the script and saving it as a file and running it on your machine, loading up the link that the script is on and running the instructions given. - BluZero
Please, RTFM before you start asking more questions that doesn't make sense whatsoever. You have to supply a file with lua code in it, what's hard to understand here? If you want to grab the code directly from pastebin, you will need luasocket and a http query for the file contents. - Bartek Banachewicz
luasocket, I'll look into that. Thanks, and sorry if I irritated you or something. What I'm working with does have an http.request function in it so it's atleast got that ability. I can pull up the instructions and output it into the window and see it if I want to, just taking those instructions and rather than viewing them, just running them from there is what I'm trying to achieve, I'll look more into it and see if there's more functions I'm missing here. - BluZero
So if you have the contents loaded, you can transform the string version into something callable by load function. As in f = load([[ print "foo" ]]); f(); - Bartek Banachewicz

1 Answers

1
votes

To load and execute a Lua code fragment you can use something like this:

local http = require("socket.http")
local response, err = http.request("url to some Lua code")
if not response then error(err) end
local f, err = (loadstring or load)(response)
if not f then error(err) end
print("done with "..response)
-- make sure you read on (in)security implications of running remote code
-- f() -- call the function based on the remote code

You probably need to use sandboxing.