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

Rednet and Metatables

Started by powerboat9, 29 November 2015 - 04:23 PM
powerboat9 #1
Posted 29 November 2015 - 05:23 PM
If I were to send a table that had a metatable, would it keep the metatable? Would the metatable keep it's functions and coroutines?
Edited on 29 November 2015 - 04:24 PM
Lupus590 #2
Posted 29 November 2015 - 05:31 PM
Table.serialise is a function which converts a table to a string format. In order to send a table (via a modem or rednet) you need to serialise it first.

Unfortunately the current version of lua that CC uses cannot serialise functions and coroutines. So a table with any functions/coroutines will lose these when serialised.

As for the metatable thing, I have no idea.

As a work around for the function thing, you could include the source for the functions when you serialise it, the receiving program can then reconstruct the missing functions. This may be a bit messy, but it should work. You will not be able to do this with coroutines however.
Edited on 29 November 2015 - 04:31 PM
Yevano #3
Posted 29 November 2015 - 05:35 PM
If I were to send a table that had a metatable, would it keep the metatable? Would the metatable keep it's functions and coroutines?

You could easily test this yourself, but I think the metatable is not sent. Coroutines will definitely not be sent as there's no way to serialize them, and textutils.serialize won't serialize functions either. You can, however, call string.dump on your functions to convert them to strings, but then you also run into the problem of sending non-plaintext strings over rednet, which IIRC is still broken. You would have to encode the binary string to something like base64 to fix that. On the other side, you would decode base64, then recompile the function with loadstring.

There's actually a lot you need to consider when sending functions across a network like this, as they may not even work on the other side depending on whether they're using upvalues (local variables outside the function), or the computers have different libraries loaded.

Why do you need to do this?
H4X0RZ #4
Posted 29 November 2015 - 08:47 PM
You can send raw tables through modems, i believe.
Lupus590 #5
Posted 29 November 2015 - 10:16 PM
You can send raw tables through modems, i believe.

I think that the modem methods just serialise the table for you, I'm not entirely sure.
Lyqyd #6
Posted 29 November 2015 - 10:20 PM
No, table serialization is not done on the Lua side. I'd imagine a similar process is done on the Java side, since IIRC, metatables are indeed stripped, and the table that arrives at the client is a copy rather than a duplicate.
Anavrins #7
Posted 29 November 2015 - 10:25 PM
You can send raw tables through modems, i believe.
Yes, but the metatables does not come with it.
powerboat9 #8
Posted 30 November 2015 - 01:16 AM
Why do you need to do this?

I was wondering if you could send a table that had a metatable, and if the metatable could be used for code injection.
Anavrins #9
Posted 30 November 2015 - 02:31 AM
It would be too easy to be true lol, thought some trivia about "code injection", I remember Dan tweeting something a long time ago about a bug in GPS/unserialize code that allowed remote code execution.
It's been patched since, of course.
Edit: there it is… https://twitter.com/DanTwoHundred/status/415979117746475008
Edited on 30 November 2015 - 02:15 AM