Inheritance: IObject, ISerializable
Esempio n. 1
0
 public void SetAndGetOriginalValue()
 {
     IObject obj = new BaseObject(null, new object[] { 1, 2, 3 });
     TransactionalValue tvalue = new TransactionalValue(this.trobj, 2);
     Assert.AreEqual(3, tvalue.GetValue(this.CreateTransaction()));
     Assert.AreEqual(3, tvalue.GetValue(0));
 }
Esempio n. 2
0
        public void CreateWithVariables()
        {
            BaseObject bo = new BaseObject(null, new object[] { 1, 2, 3 });

            Assert.AreEqual(1, bo[0]);
            Assert.AreEqual(2, bo[1]);
            Assert.AreEqual(3, bo[2]);
        }
Esempio n. 3
0
 public void RaiseIfTwoTransactionChangeTheSameSlot()
 {
     IObject obj = new BaseObject(null, new object[] { 1, 2, 3 });
     TransactionalValue tvalue = new TransactionalValue(this.trobj, 0);
     Transaction transaction1 = this.CreateTransaction();
     tvalue.SetValue(transaction1, 2);
     Transaction transaction2 = this.CreateTransaction();
     tvalue.SetValue(transaction2, 3);
 }
Esempio n. 4
0
        public void AccessVariables()
        {
            BaseObject obj = new BaseObject(null, new object[] { 1, 2, 3 });
            RemoteObject ro = new RemoteObject(obj, null);

            Assert.AreEqual(1, ro[0]);
            Assert.AreEqual(2, ro[1]);
            Assert.AreEqual(3, ro[2]);
        }
Esempio n. 5
0
        public void RaiseIfTransactionChangeACommittedSlot()
        {
            IObject obj = new BaseObject(null, new object[] { 1, 2, 3 });
            TransactionalValue tvalue = new TransactionalValue(this.trobj, 0);
            Transaction transaction1 = this.CreateTransaction();

            Transaction transaction2 = this.CreateTransaction();
            tvalue.SetValue(transaction2, 3);
            transaction2.Commit(this.manager.Time + 1);
            ////tvalue.CommitValue(transaction2);
            tvalue.SetValue(transaction1, 2);
        }
Esempio n. 6
0
        public void CreateWithVariablesAndClass()
        {
            Machine machine = new Machine();
            BaseClass cls = new BaseClass("MyClass", machine);
            BaseObject bo = new BaseObject(cls, new object[] { 1, 2, 3 });

            Assert.AreEqual(1, bo[0]);
            Assert.AreEqual(2, bo[1]);
            Assert.AreEqual(3, bo[2]);

            Assert.AreEqual(cls, bo.Behavior);
        }
Esempio n. 7
0
        public void GetBehavior()
        {
            Machine machine = new Machine();
            BaseClass cls = new BaseClass("MyClass", machine);
            BaseObject obj = new BaseObject(cls, new object[] { 1, 2, 3 });
            RemoteObject ro = new RemoteObject(obj, null);

            Assert.AreEqual(1, ro[0]);
            Assert.AreEqual(2, ro[1]);
            Assert.AreEqual(3, ro[2]);

            Assert.AreEqual(cls, ro.Behavior);
        }
Esempio n. 8
0
        public void SendMessage()
        {
            Machine machine = new Machine();
            IClass cls = machine.CreateClass("MyClass", null, "x", string.Empty);
            VmCompiler compiler = new VmCompiler();
            var method = compiler.CompileInstanceMethod("x: value x := value", cls);
            cls.DefineInstanceMethod(method);
            BaseObject obj = new BaseObject(cls, new object[1]);
            RemoteObject ro = new RemoteObject(obj, null);

            machine.SendMessage(ro, "x:", new object[] { 10 }, null);

            Assert.AreEqual(10, obj[0]);
        }
Esempio n. 9
0
        public void SerializeAndDeserializeCompositeObject()
        {
            Machine machine = new Machine(true);
            BaseClass cls = new BaseClass("MyClass", machine);
            BaseObject bso1 = new BaseObject(cls, new object[] { 1, 2, 3 });
            BaseObject bso2 = new BaseObject(cls, new object[] { 2, 3, 4 });
            BaseObject bso3 = new BaseObject(cls, new object[] { 4, 5, 6 });
            BaseObject bo = new BaseObject(cls, new object[] { bso1, bso2, bso3, bso2 });
            bso3[2] = bo;

            BinaryFormatter formatter = new BinaryFormatter();
            BaseObject bo2;
            Machine machine2;
            BaseClass cls2;

            using (MemoryStream stream = new MemoryStream())
            {
                formatter.Serialize(stream, bo);
                stream.Seek(0, SeekOrigin.Begin);
                machine2 = new Machine(true);
                Assert.AreSame(Machine.Current, machine2);
                cls2 = new BaseClass("MyClass", machine2);
                machine2.SetGlobalObject("MyClass", cls2);
                bo2 = (BaseObject)formatter.Deserialize(stream);
            }

            Assert.IsNotNull(bo2[0]);
            Assert.IsNotNull(bo2[1]);
            Assert.IsNotNull(bo2[2]);
            Assert.IsNotNull(bo2[3]);

            Assert.AreEqual(bo2[1], bo2[3]);

            Assert.IsNotNull(bo2.Behavior);
            Assert.AreSame(cls2, bo2.Behavior);

            Assert.IsInstanceOfType(bo2[0], typeof(BaseObject));
            Assert.IsInstanceOfType(bo2[1], typeof(BaseObject));
            Assert.IsInstanceOfType(bo2[2], typeof(BaseObject));
            Assert.IsInstanceOfType(bo2[3], typeof(BaseObject));

            BaseObject bso32 = (BaseObject)bo2[2];
            Assert.AreSame(cls2, bso32.Behavior);
            Assert.AreSame(bo2, bso32[2]);

            Assert.AreEqual(cls, bo.Behavior);
        }
Esempio n. 10
0
 public void InvokeNativeGetAt()
 {
     BaseObject obj = new BaseObject(null, new object[] { 1, 2, 3 });
     Assert.AreEqual(1, DotNetObject.SendNativeMessage(null, obj, "ngetat", new object[] { "Item", 0 }));
     Assert.AreEqual(2, DotNetObject.SendNativeMessage(null, obj, "ngetat", new object[] { "Item", 1 }));
     Assert.AreEqual(3, DotNetObject.SendNativeMessage(null, obj, "ngetat", new object[] { "Item", 2 }));
 }
Esempio n. 11
0
        public void SerializeDeserializeObjectWithNilVariables()
        {
            IObject obj = new BaseObject(null, new object[10]);
            var result = this.Process(obj, null);

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(IObject));

            var iobj = (IObject)result;
            Assert.IsNull(iobj.Behavior);
            Assert.AreEqual(10, iobj.NoVariables);

            for (int k = 0; k < 10; k++)
                Assert.IsNull(iobj[k]);
        }
Esempio n. 12
0
        public void SerializeDeserializeObjectsWithCycle()
        {
            IObject obja = new BaseObject(null, 1);
            IObject objb = new BaseObject(null, 1);
            obja[0] = objb;
            objb[0] = obja;

            MemoryStream stream = new MemoryStream();
            BinaryWriter writer = new BinaryWriter(stream);
            ImageSerializer serializer = new ImageSerializer(writer);

            serializer.Serialize(obja);
            serializer.Serialize(objb);

            writer.Close();
            stream = new MemoryStream(stream.ToArray());
            BinaryReader reader = new BinaryReader(stream);

            ImageSerializer deserializer = new ImageSerializer(reader, null);

            var resulta = deserializer.Deserialize();
            var resultb = deserializer.Deserialize();

            Assert.IsNotNull(resulta);
            Assert.IsInstanceOfType(resulta, typeof(IObject));

            var iobja = (IObject)resulta;
            Assert.IsNull(iobja.Behavior);
            Assert.AreEqual(1, iobja.NoVariables);

            Assert.IsNotNull(resultb);
            Assert.IsInstanceOfType(resultb, typeof(IObject));

            var iobjb = (IObject)resultb;
            Assert.IsNull(iobjb.Behavior);
            Assert.AreEqual(1, iobjb.NoVariables);

            Assert.AreEqual(iobja, iobjb[0]);
            Assert.AreEqual(iobjb, iobja[0]);
        }
Esempio n. 13
0
        public void SerializeDeserializeEmptyObject()
        {
            IObject obj = new BaseObject();
            var result = this.Process(obj, null);

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(IObject));

            var iobj = (IObject)result;
            Assert.IsNull(iobj.Behavior);
            Assert.AreEqual(0, iobj.NoVariables);
        }
Esempio n. 14
0
        public void Create()
        {
            BaseObject bo = new BaseObject();

            Assert.IsNull(bo.Behavior);
        }
Esempio n. 15
0
 public void InvokeDefaultProperty()
 {
     BaseObject obj = new BaseObject(null, new object[] { 1, 2, 3 });
     Assert.AreEqual(2, DotNetObject.SendNativeMessage(null, obj, string.Empty, new object[] { 1 }));
 }
Esempio n. 16
0
 public void InvokeNativeSetAtPut()
 {
     BaseObject obj = new BaseObject(null, new object[] { 1, 2, 3 });
     Assert.AreEqual(10, DotNetObject.SendNativeMessage(null, obj, "nsetatput", new object[] { "Item", 0, 10 }));
     Assert.AreEqual(10, obj[0]);
 }
Esempio n. 17
0
        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();
        }
Esempio n. 18
0
        public void TwoSerializedTransactions()
        {
            IObject obj = new BaseObject(null, new object[] { 1, 2, 3 });
            TransactionalValue tvalue = new TransactionalValue(this.trobj, 0);
            Transaction transaction1 = this.CreateTransaction();
            tvalue.SetValue(transaction1, 2);
            tvalue.CommitValue(transaction1);

            Assert.AreEqual(2, tvalue.GetValue(0));

            Transaction transaction2 = this.CreateTransaction();
            tvalue.SetValue(transaction2, 3);
            tvalue.CommitValue(transaction2);

            Assert.AreEqual(3, tvalue.GetValue(0));
        }
Esempio n. 19
0
        public void SerializeAndDeserializeSimpleObject()
        {
            Machine machine = new Machine();
            BaseClass cls = new BaseClass("MyClass", machine);
            BaseObject bo = new BaseObject(cls, new object[] { 1, 2, 3 });

            BinaryFormatter formatter = new BinaryFormatter();
            BaseObject bo2;
            Machine machine2;
            BaseClass cls2;

            using (MemoryStream stream = new MemoryStream())
            {
                formatter.Serialize(stream, bo);
                stream.Seek(0, SeekOrigin.Begin);
                machine2 = new Machine(true);
                Assert.AreSame(Machine.Current, machine2);
                cls2 = new BaseClass("MyClass", machine2);
                machine2.SetGlobalObject("MyClass", cls2);
                bo2 = (BaseObject)formatter.Deserialize(stream);
            }

            Assert.AreEqual(1, bo2[0]);
            Assert.AreEqual(2, bo2[1]);
            Assert.AreEqual(3, bo2[2]);
            Assert.IsNotNull(bo2.Behavior);
            Assert.AreSame(cls2, bo2.Behavior);

            Assert.AreEqual(cls, bo.Behavior);
        }