Using loadstring() with formatted bytecode

@Briarfox The strings are totally different. In my program above, string b is in hex while string f is ASCII. The same thing will be true in your original program for your 2 strings, binary and formatted_binary.

@Briarfox - I have done this kind of exploration myself several times in the past - sometimes it turns out extremely useful, so is usually worth the effort.

(Besides, once the question is asked, we just HAVE to know the answer, right?)

@dave1707 The question is why. Shouldn’t they be the same. You take the string value of f, paste it as a string and assign it to a variable and it works. But if you take f its ASCII.

I’m running into the same problem when I try to read it back out of an image, it behaves like f.

@Ignatz You got that right :slight_smile:

@Briarfox When the string f is assigned to a variable and you use that variable, Lua converts that string (\027\076) back to it’s hex values, \027\076 = 2 bytes of hex. If you read f yourself, your reading each character \ 0 2 7 \ 0 7 6 which isn’t the same, \ 0 2 7 \ 0 7 6 = 8 bytes of hex. So the function loadstring sees these as 2 different strings with totally different values.

I got it! it was actually easy. needed one more loadstring to get the conversion right. I’ve got export function to photos to work. Then you just read the photo into a function and call it when needed. I’ll share that code when I get it cleaned up. Only use I can see is to hide some code but I can’t think of a good reason to do that. I wonder if and/or how it effects loading speed.


function setup()
    str = createStr()
    str("Ignatz")
    str("dave1707")
    
end

function createStr()
    b=string.dump(t)
    f="g='"
    for i = 1, string.len(b) do
        f=f..string.format("\\\\%03d",string.byte(string.sub(b,i,i)))
    end    
   f=f.."'"    
    print(f)
    loadstring(f)()
    print(g)
    test = loadstring(g)
    return test
end


function t(name)
    print(name.."! Look I ran a function!")
end

@Briarfox Actually, you’re not hiding any code. I took the above program and added another function to do a hex dump on the string b, f, and g. What I saw was a listing of the whole program along with more of function t. I’m assuming the extra bytes of function t is what gets executed with str. I was thinking that string.dump(t) would just do function t, but apparently it dumps the whole program then dumps function t. I’m not sure if that’s the way it’s supposed to work or there’s a problem with the dump statement in Codea. So the string you’re saving to a photo will contain the whole program along with the function you’re dumping. Not sure what good that’s going to do for you.

Edit: Maybe the reason it saves all of the program is because it doesn’t know what else function t might need to execute.

I didn’t realize that, although it makes sense now. As I would add code the dumped function would grow in size. Interesting. So, dump packages the whole program, then you’d be able to store the program on dropbox as one file. Then use one app to pull any photo app from dropbox and run it.

I’m not sure what you mean about saving the program, but the dumped program as a whole isn’t executable the way it is saved in the string. There are extra bytes of information in the string, so it might take another program to extract the actual program and run it. I’m not sure of the purpose of string.dump(), but so far I don’t see anything useful about it. I also tried adding a dummy tab to your program and when I dumped the string, the code in the tab wasn’t included. So the purpose of string.dump() is even more confusing.

Yeah I’m not sure what the use is either. About having it backup a program, I’m thinking I could write another app to load a photo which would run the program. The problem is going to be accessing main,draw and touch.