asherkin@263: /* asherkin@263: * Copyright (C) 2012 David Anderson asherkin@263: * asherkin@263: * This file is part of SourcePawn. asherkin@263: * asherkin@263: * SourcePawn is free software: you can redistribute it and/or modify it under asherkin@263: * the terms of the GNU General Public License as published by the Free asherkin@263: * Software Foundation, either version 3 of the License, or (at your option) asherkin@263: * any later version. asherkin@263: * asherkin@263: * SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY asherkin@263: * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS asherkin@263: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. asherkin@263: * asherkin@263: * You should have received a copy of the GNU General Public License along with asherkin@263: * SourcePawn. If not, see http://www.gnu.org/licenses/. asherkin@263: */ asherkin@263: #include asherkin@263: #include "Utility.h" asherkin@263: #include "Environments.h" asherkin@263: #include "Structures.h" asherkin@263: #include "FixedArray.h" asherkin@263: #include "Zone.h" asherkin@263: #include "compiler/NameBinding.h" asherkin@263: #include "Spaces-inl.h" asherkin@263: #include "Heap-inl.h" asherkin@263: asherkin@263: using namespace ke; asherkin@263: asherkin@263: EnvironmentSlotDescriptorArray * asherkin@263: EnvironmentSlotDescriptorArray::New(Zone *zone, unsigned nslots) asherkin@263: { asherkin@263: size_t bytesNeeded = sizeof(EnvironmentSlotDescriptorArray) + asherkin@263: sizeof(EnvironmentSlotDescriptor) * nslots; asherkin@263: asherkin@263: Local array(zone); asherkin@263: Local map(zone, zone->getMap(MapKind_EnvironmentSlotDescriptorArray)); asherkin@263: asherkin@263: array = EnvironmentSlotDescriptorArray::cast(zone->allocate(map, bytesNeeded, Heap::Tenure_Old)); asherkin@263: if (!array) asherkin@263: return NULL; asherkin@263: asherkin@263: // We zero the array for safety, since we could GC or fail before initialization. asherkin@263: array->length_ = nslots; asherkin@263: memset(array->slots(), 0, sizeof(EnvironmentSlotDescriptor) * nslots); asherkin@263: return array; asherkin@263: } asherkin@263: asherkin@263: Environment * asherkin@263: Environment::New(Zone *zone, Handle map, Heap::Tenure tenure) asherkin@263: { asherkin@263: size_t bytesNeeded = sizeof(Environment) + map->structureSize(); asherkin@263: Local env(zone, Environment::cast(zone->allocate(map, bytesNeeded, tenure))); asherkin@263: if (!env) asherkin@263: return NULL; asherkin@263: asherkin@263: // Initialize when called from C++, to make sure object slots are NULL. asherkin@263: memset((Object *)env + 1, 0, map->structureSize()); asherkin@263: return env; asherkin@263: } asherkin@263: asherkin@263: #if 0 asherkin@263: EnvironmentMap * asherkin@263: EnvironmentMap::New(Zone *zone, unsigned nvars, Variable **vars) asherkin@263: { asherkin@263: StructureBufferBuilder structure; asherkin@263: asherkin@263: Local slots(zone); asherkin@263: slots = EnvironmentSlotDescriptorArray::New(zone, nvars); asherkin@263: if (!slots) asherkin@263: return NULL; asherkin@263: asherkin@263: // Build the descriptor table, mapping each slot to an offset and type. asherkin@263: Local type(zone); asherkin@263: for (unsigned i = 0; i < nvars; i++) { asherkin@263: assert(vars[i]->slot() == i); asherkin@263: type = vars[i]->type(); asherkin@263: asherkin@263: EnvironmentSlotDescriptor &desc = slots->slots()[i]; asherkin@263: if (!structure.add(type, &desc.offset)) asherkin@263: return NULL; asherkin@263: asherkin@263: desc.type = type; asherkin@263: } asherkin@263: asherkin@263: // Allocate the actual map now. asherkin@263: size_t bytesNeeded = sizeof(EnvironmentMap) + structure.traceableOffsetCount() * sizeof(unsigned); asherkin@263: asherkin@263: Local map(zone); asherkin@263: map = EnvironmentMap::cast(zone->allocateMap(MapKind_Environment, bytesNeeded, MapKind_MetaEnvironment)); asherkin@263: if (!map) asherkin@263: return NULL; asherkin@263: asherkin@263: // Fill in the map fields, and copy the traceable offsets structure. asherkin@263: map->structureSize_ = structure.structureSize(); asherkin@263: map->traceOffsetCount_ = structure.traceableOffsetCount(); asherkin@263: map->slots_ = slots; asherkin@263: memcpy(const_cast(map->traceOffsets()), structure.offsets(), sizeof(unsigned) * structure.traceableOffsetCount()); asherkin@263: asherkin@263: return map; asherkin@263: } dvander@195: #endif