src/API.cpp
changeset 195 c1ba166f3fc4
parent 188 bab29bcec7fe
child 209 5cbec15fadfe
     1.1 --- a/src/API.cpp	Mon Sep 17 00:10:01 2012 -0700
     1.2 +++ b/src/API.cpp	Sat Nov 17 18:41:01 2012 -0800
     1.3 @@ -20,11 +20,7 @@
     1.4  #include "../include/keima.h"
     1.5  #include "Zone.h"
     1.6  #include "RootScopes.h"
     1.7 -#include "CompileContext.h"
     1.8 -#include "Parser.h"
     1.9 -#include "Scopes.h"
    1.10 -#include "SemanticAnalysis.h"
    1.11 -#include "BytecodeCompiler.h"
    1.12 +#include "compiler/CompileContext.h"
    1.13  #include "Modules.h"
    1.14  #include "GlobalRoots.h"
    1.15  #include "Reference.h"
    1.16 @@ -32,6 +28,7 @@
    1.17  #include "Interpreter.h"
    1.18  #include "Stack.h"
    1.19  #include "Strings.h"
    1.20 +#include "NativeTable.h"
    1.21  #include "Spaces-inl.h"
    1.22  #include "Heap-inl.h"
    1.23  
    1.24 @@ -94,76 +91,25 @@
    1.25      return Ke::Local<To>(reinterpret_cast<To *>(Ke::HandleScope::CreateHandle(from)));
    1.26  }
    1.27  
    1.28 -static ke::Module *
    1.29 -CompileFromSource(CompileContext &cc, const char *path, const char *stream, size_t length)
    1.30 -{
    1.31 -    Parser parser(ZONE(), cc, stream, length);
    1.32 -    ParseTree *tree = parser.parse();
    1.33 -    if (!tree)
    1.34 -        return NULL;
    1.35 -    tree->dump(stdout);
    1.36 -
    1.37 -    if (!Scope::Bind(cc, tree))
    1.38 -        return NULL;
    1.39 -
    1.40 -    SemanticAnalysis semantics(ZONE(), cc, tree);
    1.41 -    if (!semantics.analyze())
    1.42 -        return NULL;
    1.43 -
    1.44 -    Scope::Allocate(tree);
    1.45 -
    1.46 -    BytecodeCompiler compiler(ZONE(), tree, NULL);
    1.47 -    ke::Local<Module> module(ke::ZONE(), compiler.compile());
    1.48 -    if (!module)
    1.49 -        return NULL;
    1.50 -
    1.51 -    ke::InvokeArgs args;
    1.52 -    if (!ke::ZONE()->stack().pushModuleFrame(module, args))
    1.53 -        return NULL;
    1.54 -    if (!ke::Interpret(ke::ZONE(), NULL, NULL))
    1.55 -        return NULL;
    1.56 -
    1.57 -    return module;
    1.58 -}
    1.59 -
    1.60  ke::Module *
    1.61  InternalCompile(const char *path)
    1.62  {
    1.63 +    PoolScope poolScope(ZONE()->pool());
    1.64      RootScope roots(ZONE());
    1.65      CompileContext cc(ZONE(), path);
    1.66  
    1.67 -    char *bytes = NULL;
    1.68 -    size_t length;
    1.69 -    {
    1.70 -        FILE *fp = fopen(path, "rb");
    1.71 -        if (!fp) {
    1.72 -            cc.reportError(SourcePosition(), Message_CantOpenFile);
    1.73 -            return NULL;
    1.74 -        }
    1.75 +    TranslationUnit *unit = new (ZONE()->pool()) TranslationUnit();
    1.76 +    unit->path_ = path;
    1.77 +
    1.78 +    if (!cc.addTranslationUnit(unit))
    1.79 +        return NULL;
    1.80  
    1.81 -        if (fseek(fp, 0, SEEK_END)) {
    1.82 -            cc.reportError(SourcePosition(), Message_ErrorSeekingFile);
    1.83 -            fclose(fp);
    1.84 -            return NULL;
    1.85 -        }
    1.86 -        long size = ftell(fp);
    1.87 -        if (size < 0) {
    1.88 -            cc.reportError(SourcePosition(), Message_ErrorSeekingFile);
    1.89 -            fclose(fp);
    1.90 -            return NULL;
    1.91 -        }
    1.92 -        rewind(fp);
    1.93 -        length = size_t(size);
    1.94 -        bytes = new char[length + 1];
    1.95 -        if (fread(bytes, 1, length, fp) != length) {
    1.96 -            cc.reportError(SourcePosition(), Message_ErrorReadingFile);
    1.97 -            fclose(fp);
    1.98 -            delete [] bytes;
    1.99 -            return NULL;
   1.100 -        }
   1.101 -        fclose(fp);
   1.102 -    }
   1.103 +    if (!cc.compile())
   1.104 +        return NULL;
   1.105  
   1.106 +    return NULL;
   1.107 +
   1.108 +#if 0
   1.109      ke::Local<ke::Module> module(ZONE(),  CompileFromSource(cc, path, bytes, length));
   1.110      delete [] bytes;
   1.111      if (!module)
   1.112 @@ -175,6 +121,8 @@
   1.113  
   1.114      module->setPath(pathstr);
   1.115      return module;
   1.116 +#endif
   1.117 +    return NULL;
   1.118  }
   1.119  
   1.120  // This is split out from InternalCompile so the RootScope doesn't conflict
   1.121 @@ -238,10 +186,9 @@
   1.122  }
   1.123  
   1.124  ke::HeapRoot *
   1.125 -Ke::Internals::NewHeapRoot(ke::Object **ptr)
   1.126 +Ke::Internals::NewHeapRoot()
   1.127  {
   1.128 -    ke::Local<ke::Object> obj(ZONE(), *ptr);
   1.129 -    ke::HeapRoot *root = ke::ZONE()->globalRoots()->create(obj);
   1.130 +    ke::HeapRoot *root = ke::ZONE()->globalRoots()->create();
   1.131      if (!root)
   1.132          return NULL;
   1.133  
   1.134 @@ -250,15 +197,9 @@
   1.135  }
   1.136  
   1.137  void
   1.138 -Ke::Internals::IncRef(ke::HeapRoot *root)
   1.139 +Ke::Internals::Destroy(ke::HeapRoot *root)
   1.140  {
   1.141 -    ZONE()->globalRoots()->incref(FromAPI(root));
   1.142 -}
   1.143 -
   1.144 -void
   1.145 -Ke::Internals::DecRef(ke::HeapRoot *root)
   1.146 -{
   1.147 -    ZONE()->globalRoots()->decref(FromAPI(root));
   1.148 +    ZONE()->globalRoots()->destroy(FromAPI(root));
   1.149  }
   1.150  
   1.151  bool
   1.152 @@ -280,40 +221,6 @@
   1.153      ke::ZONE()->clearPendingException();
   1.154  }
   1.155  
   1.156 -unsigned
   1.157 -Ke::Module::numPublics()
   1.158 -{
   1.159 -    ke::Local<ke::Module> module(ke::ZONE(), Open<Ke::Module, ke::Module>(this));
   1.160 -    if (!module->publics())
   1.161 -        return 0;
   1.162 -    return module->publics()->length();
   1.163 -}
   1.164 -
   1.165 -Ke::Local<Ke::Function>
   1.166 -Ke::Module::getPublic(unsigned index)
   1.167 -{
   1.168 -    ke::Local<ke::Module> module(ke::ZONE(), Open<Ke::Module, ke::Module>(this));
   1.169 -    ke::Local<ke::Function> fun(ke::ZONE(), ke::Function::cast(module->publics()->at(index)));
   1.170 -    return ToLocal<ke::Function, Ke::Function>(fun);
   1.171 -}
   1.172 -
   1.173 -unsigned
   1.174 -Ke::Module::numNatives()
   1.175 -{
   1.176 -    ke::Local<ke::Module> module(ke::ZONE(), Open<Ke::Module, ke::Module>(this));
   1.177 -    if (!module->natives())
   1.178 -        return 0;
   1.179 -    return module->natives()->length();
   1.180 -}
   1.181 -
   1.182 -Ke::Local<Ke::Native>
   1.183 -Ke::Module::getNative(unsigned index)
   1.184 -{
   1.185 -    ke::Local<ke::Module> module(ke::ZONE(), Open<Ke::Module, ke::Module>(this));
   1.186 -    ke::Local<ke::Native> fun(ke::ZONE(), ke::Native::cast(module->natives()->at(index)));
   1.187 -    return ToLocal<ke::Native, Ke::Native>(fun);
   1.188 -}
   1.189 -
   1.190  void
   1.191  Ke::Module::setNativeIdentity(void *identity)
   1.192  {
   1.193 @@ -342,20 +249,6 @@
   1.194      return ToLocal<ke::String, Ke::String>(fun->name());
   1.195  }
   1.196  
   1.197 -Ke::Local<Ke::String>
   1.198 -Ke::Native::name()
   1.199 -{
   1.200 -    ke::Local<ke::Native> native(ke::ZONE(), Open<Ke::Native, ke::Native>(this));
   1.201 -    return ToLocal<ke::String, Ke::String>(native->name());
   1.202 -}
   1.203 -
   1.204 -void
   1.205 -Ke::Native::bind(void *function)
   1.206 -{
   1.207 -    ke::Local<ke::Native> native(ke::ZONE(), Open<Ke::Native, ke::Native>(this));
   1.208 -    native->bind(function);
   1.209 -}
   1.210 -
   1.211  static inline const char *
   1.212  GetInvokeTypeName(Ke::Parameter::Type type)
   1.213  {
   1.214 @@ -535,3 +428,25 @@
   1.215      *retval = result.i32;
   1.216      return true;
   1.217  }
   1.218 +
   1.219 +bool
   1.220 +Ke::Keima::BindNative(const char *name, void *address)
   1.221 +{
   1.222 +    return ke::ZONE()->nativeTable()->bind(name, address);
   1.223 +}
   1.224 +
   1.225 +void
   1.226 +Ke::Keima::SetPublicFunctionListener(IPublicFunctionListener *listener)
   1.227 +{
   1.228 +    ke::ZONE()->setPublicFunctionListener(listener);
   1.229 +}
   1.230 +
   1.231 +void
   1.232 +Zone::registerPublic(ke::Handle<Function> fun)
   1.233 +{
   1.234 +    if (!publicFunctionListener_)
   1.235 +        return;
   1.236 +
   1.237 +    Ke::Local<Ke::Function> local = ToLocal<ke::Function, Ke::Function>(fun);
   1.238 +    publicFunctionListener_->registerPublic(local);
   1.239 +}