Inheritance: BaseClassDescription, IClass
Beispiel #1
0
        public Machine(bool iscurrent)
        {
            if (iscurrent)
                this.SetCurrent();

            IMetaClass meta = new BaseMetaClass(null, null, this, string.Empty);
            this.nilclass = (BaseClass)meta.CreateClass("UndefinedObject", string.Empty);

            // TODO review, nil object never receives a message, see Send in Execution block
            this.nilclass.DefineInstanceMethod(new BehaviorDoesNotUnderstandMethod(this, this.nilclass));
            this.nilclass.DefineClassMethod(new BehaviorDoesNotUnderstandMethod(this, this.nilclass));
            this.nilclass.DefineInstanceMethod(new FunctionalMethod("ifNil:", this.nilclass, this.IfNil));
            this.nilclass.DefineInstanceMethod(new FunctionalMethod("ifNotNil:", this.nilclass, this.IfNotNil));
            this.nilclass.DefineInstanceMethod(new FunctionalMethod("isNil", this.nilclass, this.IsNil));
            this.nilclass.DefineInstanceMethod(new FunctionalMethod("isNotNil", this.nilclass, this.IsNotNil));

            // Native Behaviors
            var nativeObjectBehavior = new NativeObjectBehavior(meta, null, this);
            var enumerableBehavior = new EnumerableBehavior(meta, nativeObjectBehavior, this);
            var booleanBehavior = new BooleanBehavior(meta, nativeObjectBehavior, this);
            var listBehavior = new ListBehavior(meta, enumerableBehavior, this);
            var stringBehavior = new StringBehavior(meta, nativeObjectBehavior, this);

            this.RegisterNativeBehavior(typeof(IEnumerable), enumerableBehavior);
            this.RegisterNativeBehavior(typeof(bool), booleanBehavior);
            this.RegisterNativeBehavior(typeof(IList), listBehavior);
            this.RegisterNativeBehavior(typeof(Block), new BlockBehavior(meta, this.nilclass, this));
            this.RegisterNativeBehavior(typeof(string), stringBehavior);

            // Global Objects
            this.SetGlobalObject("UndefinedObject", this.nilclass);
            this.SetGlobalObject("Machine", this);
            this.SetGlobalObject("Smalltalk", this.environment);
        }
Beispiel #2
0
 public void Create()
 {
     Machine machine = new Machine();
     BaseClass bclass = new BaseClass("Class", machine);
     Assert.IsNotNull(bclass);
     Assert.AreEqual("Class", bclass.Name);
     Assert.AreEqual(machine, bclass.Machine);
     Assert.IsNull(bclass.SuperClass);
     Assert.AreEqual(0, bclass.NoInstanceVariables);
     Assert.AreSame(machine.CurrentEnvironment, bclass.Scope);
 }
Beispiel #3
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);
        }
Beispiel #4
0
        public void DefineAndCreateAgent()
        {
            Machine machine = new Machine();
            BaseClass bclass = new BaseClass("Agent", machine);
            bclass.IsAgentClass = true;

            object result = bclass.NewObject();
            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(AgentObject));

            AgentObject agent = (AgentObject)result;
            Assert.AreEqual(bclass, agent.Behavior);
        }
Beispiel #5
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);
        }
Beispiel #6
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);
        }
Beispiel #7
0
        public IClass CreateClass(string name, string varnames)
        {
            if (this.classInstance != null)
                throw new InvalidOperationException("Metaclass has an instance class");

            IBehavior super = null;

            if (this.SuperClass != null)
            {
                IMetaClass meta = this.SuperClass as IMetaClass;
                if (meta != null)
                    super = meta.ClassInstance;
            }

            BaseClass cls = new BaseClass(this, name, super, this.Machine, varnames);
            this.classInstance = cls;

            return cls;
        }
Beispiel #8
0
        public void DefineObjectMethod()
        {
            Machine machine = new Machine();
            BaseClass cls = new BaseClass("MyClass", machine);
            BaseObject bo = (BaseObject)cls.NewObject();

            Assert.AreEqual(cls, bo.Behavior);

            IMethod method = new Method(cls, "mymethod");

            bo.DefineObjectMethod(method);

            Assert.AreNotEqual(cls, bo.Behavior);
            Assert.IsNotNull(bo.Behavior.GetInstanceMethod("mymethod"));
            Assert.AreEqual(method, bo.Behavior.GetInstanceMethod("mymethod"));

            Assert.AreNotEqual(cls, method.Behavior);
            Assert.AreEqual(bo.Behavior, method.Behavior);

            Assert.IsTrue(bo.IsPrototype);
            Assert.AreEqual(true, bo.SendMessage(machine, "isPrototype", null));
        }
Beispiel #9
0
 public void RaiseIfNameIsNull()
 {
     Machine machine = new Machine();
     BaseClass bclass = new BaseClass(null, machine);
 }
Beispiel #10
0
 public void RaiseIfMachineIsNull()
 {
     BaseClass bclass = new BaseClass("Class", null);
 }
Beispiel #11
0
        public void GetSetClassVariablesInSubclass()
        {
            Machine machine = new Machine();
            IClass bclass = new BaseClass("MyClass", machine);
            IClass bsubclass = new BaseClass(null, "MySubClass", bclass, machine, null);

            bclass.DefineClassVariable("Count");
            bclass.DefineClassVariable("Items");
            bsubclass.DefineClassVariable("Value");

            int countoffset = bsubclass.GetClassVariableOffset("Count");
            int itemsoffset = bsubclass.GetClassVariableOffset("Items");
            int valueoffset = bsubclass.GetClassVariableOffset("Value");

            Assert.AreEqual(0, countoffset);
            Assert.AreEqual(1, itemsoffset);
            Assert.AreEqual(2, valueoffset);

            bsubclass.SetClassVariable(countoffset, 1);
            bsubclass.SetClassVariable(itemsoffset, "foo");
            bsubclass.SetClassVariable(valueoffset, "value");

            Assert.AreEqual(1, bclass.GetClassVariable(countoffset));
            Assert.AreEqual("foo", bclass.GetClassVariable(itemsoffset));
            Assert.AreEqual(1, bsubclass.GetClassVariable(countoffset));
            Assert.AreEqual("foo", bsubclass.GetClassVariable(itemsoffset));
            Assert.AreEqual("value", bsubclass.GetClassVariable(valueoffset));
        }
Beispiel #12
0
        public void GetSetClassVariables()
        {
            Machine machine = new Machine();
            IClass bclass = new BaseClass("MyClass", machine);

            bclass.DefineClassVariable("Count");
            bclass.DefineClassVariable("Items");

            int countoffset = bclass.GetClassVariableOffset("Count");
            int itemsoffset = bclass.GetClassVariableOffset("Items");

            Assert.AreEqual(0, countoffset);
            Assert.AreEqual(1, itemsoffset);

            bclass.SetClassVariable(countoffset, 1);
            bclass.SetClassVariable(itemsoffset, "foo");

            Assert.AreEqual(1, bclass.GetClassVariable(countoffset));
            Assert.AreEqual("foo", bclass.GetClassVariable(itemsoffset));
        }
Beispiel #13
0
        public void GetDefineString()
        {
            Machine machine = new Machine();
            IMetaClass meta = BaseMetaClass.CreateMetaClass(null, machine);
            BaseClass bclass = new BaseClass(meta, "Class", null, machine, string.Empty);

            bclass.DefineInstanceVariable("x");
            bclass.DefineInstanceVariable("y");

            string definition = bclass.ToDefineString();

            Assert.IsNotNull(definition);
            Assert.IsTrue(definition.Contains("subclass: #Class"));
            Assert.IsTrue(definition.Contains("instanceVariableNames: 'x y'"));
            Assert.IsTrue(definition.Contains("classVariableNames: ''"));
            Assert.IsTrue(definition.Contains("poolDictionaries: ''"));
            Assert.IsTrue(definition.Contains("category: ''"));
        }
Beispiel #14
0
        public void DefineInstanceVariables()
        {
            Machine machine = new Machine();
            BaseClass bclass = new BaseClass("Class", machine);

            bclass.DefineInstanceVariable("x");
            bclass.DefineInstanceVariable("y");

            Assert.AreEqual(2, bclass.NoInstanceVariables);
            Assert.AreEqual(0, bclass.GetInstanceVariableOffset("x"));
            Assert.AreEqual(1, bclass.GetInstanceVariableOffset("y"));
            Assert.AreEqual(-1, bclass.GetInstanceVariableOffset("z"));
            Assert.AreEqual("x y", bclass.GetInstanceVariableNamesAsString());
            Assert.AreEqual(string.Empty, bclass.GetClassVariableNamesAsString());
            Assert.AreEqual(null, bclass.GetClassVariableNames());

            var result = bclass.GetInstanceVariableNames();

            Assert.IsNotNull(result);
            Assert.AreEqual(2, result.Count);
            Assert.AreEqual("x", result.ElementAt(0));
            Assert.AreEqual("y", result.ElementAt(1));
        }
Beispiel #15
0
        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;
        }
Beispiel #16
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);
        }