serialization and rematerialization of CompiledMethods,assistance needed!

John.Maloney at disney.com John.Maloney at disney.com
Thu Feb 21 18:34:08 UTC 2002


Chris,

CompiledMethods are a little tricky, because they are a hybrid of a bytes
object and a pointer-field object. The method's "literals" (i.e., literal objects
such as strings, points, floats, etc. that appear in the method text, as well
as references to class and global variables which are Association objects)
are stored in the first part of the CompiledMethod. These are actual object
pointers. If you simply serialize and deserialize the CompiledMethod as bytes,
you'll end up with garbage literal object pointers on the other side, which
would be Very Bad.

The easiest thing would be to follow Bob's suggestion and use some
form of source code. Alternatively, you could use the image segment machinery,
although that's trickier. If you don't want to save the comments and local variable
names, you could use the decompiler to produce a generic source string for the
method.

	-- John


At 9:20 PM -0800 2/20/02, Chris Muller wrote:
>I need to be able to serialize a CompiledMethod to a byteArray and bring it
>back and use it.
>
>The serialization is easy since the CompiledMethod already is a ByteArray.
>
>But how do I convert that ByteArray to a usable method again?  The overridden
>basicNew: constructor urges "CompiledMethods may only be created with
>newMethod:header:".
>
>But I'm not sure where to get that first argument (labelled "numberOfBytes" in
>the method code).  I tried using the #size of the original method.  It
>functioned, but it did not report = to the original.  See my test script here:
>
>| m copy |
>m _ (MyClass >> #test).
>copy _ (CompiledMethod newMethod: m size header: m header)
>    replaceFrom: 1
>    to: m size
>    with: m
>    startingAt: 1.
>MyClass methodDict
>    at: #test
>    put: copy.
>MyClass new test.
>^m = copy
>
>I also tried creating a CompiledMethod with basicNew: and then just a
>copyFrom:to: from the old method and that seemed to work too!  It then actually
>answered true to the =.  This seems to be an improvement but I'm not sure about
>the structure of CompiledMethods.
>
>I'm also not sure whether MyClass methodDict at:put: is the appropriate way to
>install back into the class or not, but it worked..
>
>Any advice would be greatly appreciated!
>
>Regards,
>  Chris Muller





More information about the Squeak-dev mailing list