-- Just for example
term = loadstring("term.")
method = loadstring(method)
term()method()
-- How would you do the term.method WITHOUT putting it all in one loadstring?
This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
Combine 2 Functions?
Started by Sphexish, 10 March 2013 - 02:23 PMPosted 10 March 2013 - 03:23 PM
Posted 10 March 2013 - 03:36 PM
I'm not sure I understand your question. You can concatenate variables using "..". For example:
print("What is your name?")
name = read()
print("Hello, "..name)
Posted 10 March 2013 - 03:51 PM
Nonono..
loadstring makes a function out of a string.
loadstring makes a function out of a string.
func = loadstring('term.write("hello")')
func()
Posted 10 March 2013 - 05:44 PM
Nonono..
loadstring makes a function out of a string.func = loadstring('term.write("hello")') func()
Please explain what you're trying to do, I'm not sure I understand.
Posted 10 March 2013 - 06:21 PM
You can make a function that takes the result of another function, and then call them like "functiona(functionb())" But other than that you're kinda stuck. The string that you pass into a loadstring has to describe a valid function, even if all the function does is return a value of some kind (eg. loadstring("return 5")).
What you seem to be trying to do is find a way to index an API using a variable. This is quite simple, just do something like this:
What you seem to be trying to do is find a way to index an API using a variable. This is quite simple, just do something like this:
method = "write"
term[method]("something to write")
Posted 10 March 2013 - 08:27 PM
I guess you could try something like this.
Something like that.
local env = getfenv()
local api = loadstring 'return term' () -- wow i love how valid this is !!!
local func = loadstring 'return "write"' ()
api[func]()
Something like that.
Posted 10 March 2013 - 10:35 PM
Kingdaro is making a funny here. Yes, you can do that. But please don't.
Posted 11 March 2013 - 12:43 AM
Wow I never knew you could do this…is it possible to do the reverse, i.e. making a string out of a function?
Posted 11 March 2013 - 01:08 AM
Not quite. there is string.dump which turns the code we all know and love and type, into Lua Bytecode.Wow I never knew you could do this…is it possible to do the reverse, i.e. making a string out of a function?
Posted 11 March 2013 - 01:14 AM
Hmm, shame…I would have loved to be able to find & print the code inside a function…. What does Lua Bytecode do? Is it just binary code?
Posted 11 March 2013 - 01:18 AM
Hmm, shame…I would have loved to be able to find & print the code inside a function…. What does Lua Bytecode do? Is it just binary code?
Yup - The bright side of using Lua bytecode is that you can load it via loadstring just like the usual Lua source code.
Posted 11 March 2013 - 01:18 AM
Lua source code is compiled into Lua bytecode. Once it's been compiled, the original source code isn't saved anywhere.
Bytecode is like machine code, but designed to be efficient for a particular language rather than a particular CPU. The bytecode is what is stored in memory and what gets run by Lua.
Bytecode is like machine code, but designed to be efficient for a particular language rather than a particular CPU. The bytecode is what is stored in memory and what gets run by Lua.
Posted 11 March 2013 - 01:24 AM
Ok so if I understand correctly…
local k = string.dump( myfunction )
print( loadstring( k ) )
--this will print the contents of myfunction?
Posted 11 March 2013 - 01:29 AM
Ok so if I understand correctly…local k = string.dump( myfunction ) print( loadstring( k ) ) --this will print the contents of myfunction?
No. That will print "function: [pointer here]". But
print(k)
WOULD print the contents of myfunction (the bytecode, of course).Posted 11 March 2013 - 01:38 AM
Ah, is there string.un_dump_into_normal_string_that_can_be_printed( ) by any chance? Bytecode is just a load of question marks on the cc screen to meOk so if I understand correctly…local k = string.dump( myfunction ) print( loadstring( k ) ) --this will print the contents of myfunction?
No. That will print "function: [pointer here]". ButWOULD print the contents of myfunction (the bytecode, of course).print(k)
Posted 11 March 2013 - 01:40 AM
Lua source code is compiled into Lua bytecode. Once it's been compiled, the original source code isn't saved anywhere.
Bytecode is like machine code, but designed to be efficient for a particular language rather than a particular CPU. The bytecode is what is stored in memory and what gets run by Lua.
So nope.
Posted 11 March 2013 - 11:30 AM
Well, you could write a decompiler if you were determined…but I'm not sure why you ever would. Lua is an interpreted language, so you should normally have access to the original Lua text somewhere.