Jump to content




Need to run code inside program.


  • You cannot reply to this topic
8 replies to this topic

#1 nutcase84

  • Members
  • 711 posts
  • LocationIn My Lonely Little Computer Corner

Posted 31 March 2013 - 09:43 AM

Can't figure out how. Like runCode(code).

#2 MudkipTheEpic

  • Members
  • 639 posts
  • LocationWhere you'd least expect it.

Posted 31 March 2013 - 10:16 AM

pcall(setfenv(loadstring(text),getfenv(1))

#3 nutcase84

  • Members
  • 711 posts
  • LocationIn My Lonely Little Computer Corner

Posted 31 March 2013 - 10:22 AM

View PostMudkipTheEpic, on 31 March 2013 - 10:16 AM, said:

pcall(setfenv(loadstring(text),getfenv(1))

Thank you!

But I had to add another ) to the end.

#4 MudkipTheEpic

  • Members
  • 639 posts
  • LocationWhere you'd least expect it.

Posted 31 March 2013 - 10:28 AM

Lol, that's what I thought when I posted it. XD

#5 Mads

  • Members
  • 604 posts
  • LocationCopenhagen, Denmark

Posted 31 March 2013 - 10:43 AM

Or just, for simplicity, loadstring("print('lol')")()

#6 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 31 March 2013 - 10:46 AM

Eh, it works but it's a little "dirty" for my tastes. I'd rather do something like:

-- sets only "ok" to false, while other variables are nil
local ok, func, err = false

-- if there was a syntax error, it will be in err
func, err = loadstring("your=code(here)")

-- if there is a syntax error, it will skip this and keep it in err, and ok will be false
-- if there is no syntax error,
if func then
  -- try for a coding error, and change err to the coding err
  -- if there is no coding error, ok will be true
  setfenv(func, getfenv(0))
  ok, err = pcall(func)
end

-- so in the end, ok will be true if successful, and false if not successful for whatever reason.
if not ok then
  print(err)
end

It's longer, but it's easier for me to read, understand, and build upon, and isn't as syntactically cluttered.

#7 MudkipTheEpic

  • Members
  • 639 posts
  • LocationWhere you'd least expect it.

Posted 31 March 2013 - 10:46 AM

View PostMads, on 31 March 2013 - 10:43 AM, said:

Or just, for simplicity, loadstring("print('lol')")()

It would error with attempt to call nil, you have not supplied it an environment.

I think.

#8 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 31 March 2013 - 10:49 AM

View PostMudkipTheEpic, on 31 March 2013 - 10:46 AM, said:

View PostMads, on 31 March 2013 - 10:43 AM, said:

Or just, for simplicity, loadstring("print('lol')")()

It would error with attempt to call nil, you have not supplied it an environment.

I think.
IIRC, functions made with loadstring are supplied with all of the default functions/variables, just not those defined in the script.

EDIT: Just tried it,
> bacon = 5
> loadstring "print(bacon)" ()
5
I'm not sure if this applies in CC though; I did this in my terminal.

#9 MudkipTheEpic

  • Members
  • 639 posts
  • LocationWhere you'd least expect it.

Posted 31 March 2013 - 10:50 AM

That makes sense now. :wacko:





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users