This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Sphexish's profile picture

Combine 2 Functions?

Started by Sphexish, 10 March 2013 - 02:23 PM
Sphexish #1
Posted 10 March 2013 - 03:23 PM

-- 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?
Dlcruz129 #2
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)
Sphexish #3
Posted 10 March 2013 - 03:51 PM
Nonono..
loadstring makes a function out of a string.

func = loadstring('term.write("hello")')
func()
Dlcruz129 #4
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.
ChunLing #5
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:
method = "write"
term[method]("something to write")
Kingdaro #6
Posted 10 March 2013 - 08:27 PM
I guess you could try something like this.


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.
ChunLing #7
Posted 10 March 2013 - 10:35 PM
Kingdaro is making a funny here. Yes, you can do that. But please don't.
Exerro #8
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?
theoriginalbit #9
Posted 11 March 2013 - 01:08 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?
Not quite. there is string.dump which turns the code we all know and love and type, into Lua Bytecode.
Exerro #10
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?
LBPHacker #11
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.
immibis #12
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.
Exerro #13
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?
LBPHacker #14
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).
Exerro #15
Posted 11 March 2013 - 01:38 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).
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 me
LBPHacker #16
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.
ChunLing #17
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.