Passing an object instance w/ a `this` pointer to StdLib.Serialize crashes w/o explanation
When a class has an instance method, the nccs compiler creates an array with an extra field to represent the this pointer. This field is another array with a single Pointer stack item. When the user passes an instance of this class to StdLib.Serialize, it throws in Neo.SmartContract.BinarySerializer.Deserialize without explanation.
Example:
public class TokenState
{
public UInt160 Owner;
public string Name;
public string Description;
public string Image;
public bool Validate()
{
// code to validate token state omitted
}
}
void SaveTokenState(string key, TokenState state)
{
var serializedState = StdLib.Serialize(state);
var map = new StorageMap(Storage.CurrentContext, 0x01);
map.Put(key, serializedState);
}
@devhawk How do you think we should fix it?
At a minimum, we need a better error message. And docs. The user who found this worked around the problem by converting the instance methods to static and passing the instance explicitly. Documenting that approach would be a good start.
Typically in OO programming languages, serialization doesn't include the this pointer and deserialization creates a new instance (with a fresh this pointer). But I'm not sure if the serialization infrastructure has enough info about the object being serialized to know a given Pointer instance is a this pointer or how to create a new this pointer on deserialization.