Example #1
0
        public void CreateInEnvironment()
        {
            Machine machine = new Machine();
            Context environment = new Context(machine.Environment);
            machine.CurrentEnvironment = environment;
            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);

            machine.CurrentEnvironment = null;
        }
Example #2
0
 public Context(Context parent)
 {
     this.parent = parent;
 }
Example #3
0
        private Context GetOrCreateChildEnvironment(Context environment, string envname)
        {
            var names = envname.Split('.');

            foreach (var name in names)
            {
                var result = environment.GetValue(name);

                if (result != null)
                    environment = (Context)result;
                else
                {
                    var context = new Context(environment);
                    environment.SetValue(name, context);
                    environment = context;
                }
            }

            return environment;
        }
Example #4
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();
        }
Example #5
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;
        }