예제 #1
0
        private static ObjUpvalue captureUpvalue(int local)
        {
            ObjUpvalue prevUpvalue = null;
            ObjUpvalue upvalue     = vm.openUpvalues;

            while (upvalue != null && upvalue.location > local)
            {
                prevUpvalue = upvalue;
                upvalue     = upvalue.next;
            }

            if (upvalue != null && upvalue.location == local)
            {
                return(upvalue);
            }

            ObjUpvalue createdUpvalue = Object.newUpvalue(local);

            createdUpvalue.next = upvalue;

            if (prevUpvalue == null)
            {
                vm.openUpvalues = createdUpvalue;
            }
            else
            {
                prevUpvalue.next = createdUpvalue;
            }

            return(createdUpvalue);
        }
예제 #2
0
        // Generics, <T>, doesn't seem to work with classes?
        static Obj allocateObject(int size, ObjType type)
        {
            Obj object_ = null;

            switch (type)
            {
            case ObjType.OBJ_STRING:
                object_ = new ObjString();
                break;

            case ObjType.OBJ_FUNCTION:
                object_ = new ObjFunction();
                break;

            case ObjType.OBJ_INSTANCE:
                object_ = new ObjInstance();
                break;

            case ObjType.OBJ_NATIVE:
                object_ = new ObjNative();
                break;

            case ObjType.OBJ_CLOSURE:
                object_ = new ObjClosure();
                break;

            case ObjType.OBJ_UPVALUE:
                object_ = new ObjUpvalue();
                break;

            case ObjType.OBJ_CLASS:
                object_ = new ObjClass();
                break;

            case ObjType.OBJ_BOUND_METHOD:
                object_ = new ObjBoundMethod();
                break;

            default:
                object_ = null;    // clox: (Obj*)reallocate(NULL, 0, size);
                break;
            }

            object_.type     = type;
            object_.isMarked = false;

            object_.next  = VM.vm.objects;
            VM.vm.objects = object_;

#if DEBUG_LOG_GC
            System.Console.WriteLine("{0} allocate {1} for {2}", object_._mem_id.ToString(), size.ToString(), type.ToString());
#endif
            return(object_);
        }
예제 #3
0
        public static ObjUpvalue newUpvalue(int slot)
        {
            ObjUpvalue upvalue = (ObjUpvalue)ALLOCATE_OBJ(ObjType.OBJ_UPVALUE);

            upvalue.closed   = Value.NIL_VAL();
            upvalue.location = slot;
            upvalue.next     = null;

            // not needed in clox where "slot" is a pointer
            upvalue._value_src = VM.vm.stack; // default for open upvalues
            return(upvalue);
        }
예제 #4
0
        private static void closeUpvalues(int last)
        {
            while (vm.openUpvalues != null && vm.openUpvalues.location >= last)
            {
                ObjUpvalue upvalue = vm.openUpvalues;
                upvalue.closed     = vm.stack[upvalue.location];
                upvalue.location   = util.cHeap.values.store(upvalue.closed);
                upvalue._value_src = util.cHeap.values.contents;

                vm.openUpvalues = upvalue.next;
            }
        }
예제 #5
0
        private static void markRoots()
        {
            for (int slot = 0; slot < VM.vm.stackTop; slot++)
            {
                markValue(ref VM.vm.stack[slot]);
            }

            for (int i = 0; i < VM.vm.frameCount; i++)
            {
                markObject((Obj)VM.vm.frames[i].closure);
            }

            for (ObjUpvalue upvalue = VM.vm.openUpvalues; upvalue != null; upvalue = upvalue.next)
            {
                markObject((Obj)upvalue);
            }

            Table.markTable(ref VM.vm.globals);
            Compiler.markCompilerRoots();
            markObject((Obj)VM.vm.initString);
        }