src/Modules.h
author David Anderson <dvander@alliedmods.net>
Sun Jan 06 13:52:21 2013 -0800 (2013-01-06)
changeset 256 3c184218462d
parent 251 ca99a81745fe
child 263 ba85a47ee414
permissions -rw-r--r--
Initial implementation of module imports.
     1 /*
     2  * Copyright (C) 2012 David Anderson
     3  *
     4  * This file is part of SourcePawn.
     5  *
     6  * SourcePawn is free software: you can redistribute it and/or modify it under
     7  * the terms of the GNU General Public License as published by the Free
     8  * Software Foundation, either version 3 of the License, or (at your option)
     9  * any later version.
    10  * 
    11  * SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY
    12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    14  *
    15  * You should have received a copy of the GNU General Public License along with
    16  * SourcePawn. If not, see http://www.gnu.org/licenses/.
    17  */
    18 #ifndef _include_jc_module_h_
    19 #define _include_jc_module_h_
    20 
    21 #include "Heap.h"
    22 #include "Code.h"
    23 #include "Functions.h"
    24 #include "FixedArray.h"
    25 #include "Packages.h"
    26 
    27 namespace ke {
    28 
    29 class Environment;
    30 class Struct;
    31 class Package;
    32 struct TranslationUnit;
    33 
    34 class Module : public Importable
    35 {
    36     Barriered<Code> code_;
    37     Barriered<Struct> globals_;
    38     void *nativeIdentity_;
    39     TranslationUnit *unit_;
    40     Barriered<FixedArray> imports_;
    41 
    42   public:
    43     static Module *New(Zone *zone, Handle<Package> package, Handle<String> path, Handle<String> name);
    44     
    45     Code *code() {
    46         return code_;
    47     }
    48     Struct *globals() {
    49         return globals_;
    50     }
    51 
    52     bool compiled() {
    53         return !!code_;
    54     }
    55 
    56     // Modules are pre-created, and may have their intermediate compilation
    57     // data attached while they are being compiled.
    58     bool compiling() {
    59         return !!unit_;
    60     }
    61     TranslationUnit *translationUnit() {
    62         return unit_;
    63     }
    64     void setTranslationUnit(TranslationUnit *unit) {
    65         unit_ = unit;
    66     }
    67 
    68     unsigned imports() {
    69         return imports_ ? imports_->length() / 2 : 0;
    70     }
    71     Importable *getImportAt(unsigned index) {
    72         return Importable::cast(imports_->at(index * 2));
    73     }
    74     FixedArray *importPathAt(unsigned index) {
    75         return FixedArray::cast(imports_->at(index * 2 + 1));
    76     }
    77     void setImports(Handle<FixedArray> imports) {
    78         imports_ = imports;
    79     }
    80     void setCode(Handle<Code> code) {
    81         code_ = code;
    82     }
    83     void setGlobals(Handle<Struct> globals) {
    84         globals_ = globals;
    85     }
    86 
    87     // Begin GC Descriptor.
    88     static inline size_t offsetOfCode() {
    89         return OFFSETOF(Module, code_);
    90     }
    91     static inline size_t offsetOfGlobals() {
    92         return OFFSETOF(Module, globals_);
    93     }
    94     static inline size_t offsetOfImports() {
    95         return OFFSETOF(Module, imports_);
    96     }
    97     // End GC Descriptor.
    98 
    99     static inline Module *cast(Object *obj) {
   100         assert(!obj || obj->is(MapKind_Module));
   101         return reinterpret_cast<Module *>(obj);
   102     }
   103 };
   104 
   105 }
   106 
   107 #endif // _include_jc_module_h_