src/RootScopes.cpp
author David Anderson <dvander@alliedmods.net>
Wed Dec 26 17:18:15 2012 -0800 (2012-12-26)
changeset 242 b7d14a8b918e
parent 86 32ad0523b309
permissions -rw-r--r--
Fix typo.
     1 /* vim: set ts=4 sw=4 tw=99 et:
     2  *
     3  * Copyright (C) 2012 David Anderson
     4  *
     5  * This file is part of JITCraft.
     6  *
     7  * JITCraft is free software: you can redistribute it and/or modify it under
     8  * the terms of the GNU General Public License as published by the Free
     9  * Software Foundation, either version 3 of the License, or (at your option)
    10  * any later version.
    11  * 
    12  * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
    13  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    14  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    15  *
    16  * You should have received a copy of the GNU General Public License along with
    17  * JITCraft. If not, see http://www.gnu.org/licenses/.
    18  */
    19 #include <stdio.h>
    20 #include <stdlib.h>
    21 #include "RootScopes.h"
    22 
    23 using namespace ke;
    24 
    25 RootScopeManager::RootScopeManager()
    26   : blocks_(&first_),
    27     first_(NULL),
    28     scope_(NULL)
    29 {
    30 }
    31 
    32 RootScopeManager::~RootScopeManager()
    33 {
    34     while (blocks_ != &first_) {
    35         Block *temp = blocks_;
    36         delete blocks_;
    37         blocks_ = temp;
    38     }
    39 }
    40 
    41 bool
    42 RootScopeManager::grow()
    43 {
    44     assert(blocks_->cur == blocks_->end);
    45 
    46     Block *block = new Block(blocks_);
    47     if (!block) {
    48         // We are not prepared for this. We'll start dishing out NULL-valued
    49         // ScopedRoots, and the engine generally assumes infallbility. Rather
    50         // then NULL check all sorts of nodes on parse trees, or add boolean
    51         // setters, for now we just abort.
    52         fprintf(stderr, "Unrecoverable error: could not allocate a local root scope. Aborting!\n");
    53         abort();
    54         return false;
    55     }
    56 
    57     blocks_ = block;
    58     return true;
    59 }
    60 
    61 void
    62 RootScopeManager::unwindSlow(Object **pos)
    63 {
    64     for (;;) {
    65         if (pos >= blocks_->objects && pos <= blocks_->end)
    66             break;
    67         assert(blocks_ != &first_);
    68         Block *temp = blocks_;
    69         delete blocks_;
    70         blocks_ = temp;
    71     }
    72 
    73     blocks_->cur = pos;
    74 }
    75 
    76 void
    77 RootScopeManager::accept(VirtualObjectVisitor *visitor)
    78 {
    79     Block *block = blocks_;
    80     while (block) {
    81         for (Object **ptr = block->objects; ptr < block->cur; ptr++)
    82             visitor->visit(ptr);
    83         block = block->prev;
    84     }
    85 }