public object Deserialize() { byte bt = this.reader.ReadByte(); switch ((ImageCode)bt) { case ImageCode.Nil: return null; case ImageCode.Integer: return this.reader.ReadInt32(); case ImageCode.String: return this.reader.ReadString(); case ImageCode.Reference: return this.objects[this.reader.ReadInt32()]; case ImageCode.Context: Context ctx = new Context(); this.objects.Add(ctx); int nnames = this.reader.ReadInt32(); for (int k = 0; k < nnames; k++) { string valuename = this.reader.ReadString(); object value = this.Deserialize(); ctx.SetValue(valuename, value); } return ctx; case ImageCode.NativeBehavior: string typename = (string)this.Deserialize(); Type type = TypeUtilities.GetType(typename); var nbehavior = this.machine.GetNativeBehavior(type); if (nbehavior == null) { nbehavior = (NativeBehavior)this.machine.CreateNativeBehavior(null, type); this.machine.RegisterNativeBehavior(type, (NativeBehavior)nbehavior); } this.objects.Add(nbehavior); IClass superclass = (IClass)this.Deserialize(); IBehavior metaclasssuperclass = (IBehavior)this.Deserialize(); ((NativeBehavior)nbehavior).SetSuperClass(superclass); // TODO Review this weird chain (See SerializeDeserializeMachineWithLibrary test) if (metaclasssuperclass != null) ((BaseMetaClass)nbehavior.MetaClass).SetSuperClass(metaclasssuperclass); else if (superclass != null) ((BaseMetaClass)nbehavior.MetaClass).SetSuperClass(superclass.MetaClass); this.DeserializeMethods(nbehavior); return nbehavior; case ImageCode.Class: string name = (string)this.Deserialize(); string category = (string)this.Deserialize(); string instvarnames = (string)this.Deserialize(); string classvarnames = (string)this.Deserialize(); var klass = this.machine.CreateClass(name, null, instvarnames, classvarnames); this.objects.Add(klass); superclass = (IClass)this.Deserialize(); metaclasssuperclass = (IBehavior)this.Deserialize(); ((BaseClass)klass).SetSuperClass(superclass); // TODO Review this weird chain (See SerializeDeserializeMachineWithLibrary test) if (metaclasssuperclass != null) ((BaseMetaClass)klass.MetaClass).SetSuperClass(metaclasssuperclass); else if (superclass != null) ((BaseMetaClass)klass.MetaClass).SetSuperClass(superclass.MetaClass); klass.Category = category; var global = this.machine.GetGlobalObject(name); this.DeserializeMethods(klass); return klass; case ImageCode.Object: BaseObject bobj = new BaseObject(); this.objects.Add(bobj); IBehavior behavior = (IBehavior)this.Deserialize(); bobj.SetBehavior(behavior); int nvariables = (int)this.Deserialize(); if (nvariables == 0) return bobj; object[] variables = new object[nvariables]; for (int k = 0; k < nvariables; k++) variables[k] = this.Deserialize(); bobj.SetVariables(variables); return bobj; case ImageCode.Machine: this.machine = new Machine(false); this.objects.Add(this.machine); this.objects.Add(this.machine.Environment); nnames = this.reader.ReadInt32(); for (int k = 0; k < nnames; k++) { name = this.reader.ReadString(); object obj = this.Deserialize(); this.machine.SetGlobalObject(name, obj); } return this.machine; } throw new InvalidDataException(); }
public void SetEnvironmentObject(Context environment, string objname, object value) { environment.SetValue(objname, value); if (environment != this.environment) return; if (objname == "Metaclass" && value is IClass) this.DefineMetaclass((IClass)value); else if (objname == "Class" && value is IClass) this.classclass = (IClass)value; else if (objname == "UndefinedObject" && value is IClass) this.nilclass = (BaseClass)value; }