diff -r b029d63565a2 -r c1ba166f3fc4 src/API.cpp --- a/src/API.cpp Mon Sep 17 00:10:01 2012 -0700 +++ b/src/API.cpp Sat Nov 17 18:41:01 2012 -0800 @@ -20,11 +20,7 @@ #include "../include/keima.h" #include "Zone.h" #include "RootScopes.h" -#include "CompileContext.h" -#include "Parser.h" -#include "Scopes.h" -#include "SemanticAnalysis.h" -#include "BytecodeCompiler.h" +#include "compiler/CompileContext.h" #include "Modules.h" #include "GlobalRoots.h" #include "Reference.h" @@ -32,6 +28,7 @@ #include "Interpreter.h" #include "Stack.h" #include "Strings.h" +#include "NativeTable.h" #include "Spaces-inl.h" #include "Heap-inl.h" @@ -94,76 +91,25 @@ return Ke::Local(reinterpret_cast(Ke::HandleScope::CreateHandle(from))); } -static ke::Module * -CompileFromSource(CompileContext &cc, const char *path, const char *stream, size_t length) -{ - Parser parser(ZONE(), cc, stream, length); - ParseTree *tree = parser.parse(); - if (!tree) - return NULL; - tree->dump(stdout); - - if (!Scope::Bind(cc, tree)) - return NULL; - - SemanticAnalysis semantics(ZONE(), cc, tree); - if (!semantics.analyze()) - return NULL; - - Scope::Allocate(tree); - - BytecodeCompiler compiler(ZONE(), tree, NULL); - ke::Local module(ke::ZONE(), compiler.compile()); - if (!module) - return NULL; - - ke::InvokeArgs args; - if (!ke::ZONE()->stack().pushModuleFrame(module, args)) - return NULL; - if (!ke::Interpret(ke::ZONE(), NULL, NULL)) - return NULL; - - return module; -} - ke::Module * InternalCompile(const char *path) { + PoolScope poolScope(ZONE()->pool()); RootScope roots(ZONE()); CompileContext cc(ZONE(), path); - char *bytes = NULL; - size_t length; - { - FILE *fp = fopen(path, "rb"); - if (!fp) { - cc.reportError(SourcePosition(), Message_CantOpenFile); - return NULL; - } + TranslationUnit *unit = new (ZONE()->pool()) TranslationUnit(); + unit->path_ = path; + + if (!cc.addTranslationUnit(unit)) + return NULL; - if (fseek(fp, 0, SEEK_END)) { - cc.reportError(SourcePosition(), Message_ErrorSeekingFile); - fclose(fp); - return NULL; - } - long size = ftell(fp); - if (size < 0) { - cc.reportError(SourcePosition(), Message_ErrorSeekingFile); - fclose(fp); - return NULL; - } - rewind(fp); - length = size_t(size); - bytes = new char[length + 1]; - if (fread(bytes, 1, length, fp) != length) { - cc.reportError(SourcePosition(), Message_ErrorReadingFile); - fclose(fp); - delete [] bytes; - return NULL; - } - fclose(fp); - } + if (!cc.compile()) + return NULL; + return NULL; + +#if 0 ke::Local module(ZONE(), CompileFromSource(cc, path, bytes, length)); delete [] bytes; if (!module) @@ -175,6 +121,8 @@ module->setPath(pathstr); return module; +#endif + return NULL; } // This is split out from InternalCompile so the RootScope doesn't conflict @@ -238,10 +186,9 @@ } ke::HeapRoot * -Ke::Internals::NewHeapRoot(ke::Object **ptr) +Ke::Internals::NewHeapRoot() { - ke::Local obj(ZONE(), *ptr); - ke::HeapRoot *root = ke::ZONE()->globalRoots()->create(obj); + ke::HeapRoot *root = ke::ZONE()->globalRoots()->create(); if (!root) return NULL; @@ -250,15 +197,9 @@ } void -Ke::Internals::IncRef(ke::HeapRoot *root) +Ke::Internals::Destroy(ke::HeapRoot *root) { - ZONE()->globalRoots()->incref(FromAPI(root)); -} - -void -Ke::Internals::DecRef(ke::HeapRoot *root) -{ - ZONE()->globalRoots()->decref(FromAPI(root)); + ZONE()->globalRoots()->destroy(FromAPI(root)); } bool @@ -280,40 +221,6 @@ ke::ZONE()->clearPendingException(); } -unsigned -Ke::Module::numPublics() -{ - ke::Local module(ke::ZONE(), Open(this)); - if (!module->publics()) - return 0; - return module->publics()->length(); -} - -Ke::Local -Ke::Module::getPublic(unsigned index) -{ - ke::Local module(ke::ZONE(), Open(this)); - ke::Local fun(ke::ZONE(), ke::Function::cast(module->publics()->at(index))); - return ToLocal(fun); -} - -unsigned -Ke::Module::numNatives() -{ - ke::Local module(ke::ZONE(), Open(this)); - if (!module->natives()) - return 0; - return module->natives()->length(); -} - -Ke::Local -Ke::Module::getNative(unsigned index) -{ - ke::Local module(ke::ZONE(), Open(this)); - ke::Local fun(ke::ZONE(), ke::Native::cast(module->natives()->at(index))); - return ToLocal(fun); -} - void Ke::Module::setNativeIdentity(void *identity) { @@ -342,20 +249,6 @@ return ToLocal(fun->name()); } -Ke::Local -Ke::Native::name() -{ - ke::Local native(ke::ZONE(), Open(this)); - return ToLocal(native->name()); -} - -void -Ke::Native::bind(void *function) -{ - ke::Local native(ke::ZONE(), Open(this)); - native->bind(function); -} - static inline const char * GetInvokeTypeName(Ke::Parameter::Type type) { @@ -535,3 +428,25 @@ *retval = result.i32; return true; } + +bool +Ke::Keima::BindNative(const char *name, void *address) +{ + return ke::ZONE()->nativeTable()->bind(name, address); +} + +void +Ke::Keima::SetPublicFunctionListener(IPublicFunctionListener *listener) +{ + ke::ZONE()->setPublicFunctionListener(listener); +} + +void +Zone::registerPublic(ke::Handle fun) +{ + if (!publicFunctionListener_) + return; + + Ke::Local local = ToLocal(fun); + publicFunctionListener_->registerPublic(local); +}