jitcraft/main.cpp
author David Anderson <dvander@alliedmods.net>
Wed Jul 11 01:12:54 2012 -0700 (2012-07-11)
changeset 69 dfce0539a77d
parent 68 b40b0e18a918
child 71 f7e573226c2b
permissions -rw-r--r--
Added error reporting API and fixed all known error paths.
     1 /*
     2  * Copyright (C) 2012 David Anderson
     3  *
     4  * This file is part of JITCraft.
     5  *
     6  * JITCraft 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  * Foobar 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  * JITCraft. If not, see http://www.gnu.org/licenses/.
    17  */
    18 #include <stdio.h>
    19 #include "Utility.h"
    20 #include "Scanner.h"
    21 #include "Parser.h"
    22 #include "SemanticAnalysis.h"
    23 #include "Zone.h"
    24 #include "BytecodeCompiler.h"
    25 #include "Code.h"
    26 #include "Interpreter.h"
    27 #include "Modules.h"
    28 #include "RootScopes.h"
    29 #include "../include/keima.h"
    30 
    31 using namespace ke;
    32 
    33 class MessageReporter : public Ke::IMessageReporter
    34 {
    35   public:
    36     void onOutOfMemory() {
    37         fprintf(stderr, "Out of memory!\n");
    38     }
    39     void onUncaughtException(const char *message) {
    40         fprintf(stderr, "Uncaught exception: %s\n", message);
    41 
    42         Ke::IStackFrameIterator *iter = Ke::Keima::NewStackFrameIterator();
    43         unsigned index = 0;
    44         while (!iter->done()) {
    45             fprintf(stderr, "[%d] %s (%s line %d)\n",
    46                     index,
    47                     iter->function(),
    48                     iter->source(),
    49                     iter->lineNumber());
    50             iter->prev();
    51             index++;
    52         }
    53     }
    54 } s_Reporter;
    55 
    56 #if 0
    57 static bool
    58 Compile(const char *stream, size_t length)
    59 {
    60 
    61     printf("\n ------------ \n\n");
    62 
    63     Disassemble(module->getFunction(1)->code(), stdout);
    64     //Disassemble(module->getFunction(1), stdout);
    65 
    66     printf("\n ------------ \n\n");
    67 
    68     Function *main = module->getFunction(1);
    69 
    70     Top top = ZONE()->stack().begin();
    71     LocalTop localTop(top);
    72 
    73     Frame *frame = ZONE()->stack().pushFrame(main);
    74     if (!frame)
    75         return false;
    76 
    77     LocalSlot result;
    78     if (!Interpret(ZONE(), frame, &result))
    79         return false;
    80 
    81     printf("Result: %d\n", result.i32);
    82 
    83     return true;
    84 }
    85 #endif
    86 
    87 intptr_t PrintToServer(void *blah, intptr_t *params)
    88 {
    89     assert(params[0] == 3);
    90     const char *fmt = reinterpret_cast<const char *>(params[1]);
    91     const char *str = reinterpret_cast<const char *>(params[2]);
    92     int *d = reinterpret_cast<int *>(params[3]);
    93     printf(fmt, str, *d);
    94     return 16;
    95 }
    96 
    97 int main(int argc, char **argv)
    98 {
    99     if (argc < 2) {
   100         fprintf(stderr, "Usage: <file>\n");
   101         return 1;
   102     }
   103 
   104     if (!Ke::Keima::Initialize())
   105         return 1;
   106     Ke::Keima::InstallMessageReporter(&s_Reporter);
   107     {
   108         Ke::HandleScope scope;
   109         Ke::Local<Ke::Module> module = Ke::Compile(argv[1]);
   110         if (!module)
   111             return 1;
   112 
   113         Ke::Local<Ke::Native> native = module->getNative(0);
   114         native->bind(reinterpret_cast<void *>(PrintToServer));
   115         module->setNativeIdentity(reinterpret_cast<void *>(5));
   116 
   117         int result;
   118         Ke::Local<Ke::Function> fun = module->getPublic(0);
   119         if (Ke::Keima::Invoke(fun, NULL, 0, &result))
   120             printf("%d\n", result);
   121     }
   122 
   123     Ke::Keima::Shutdown();
   124 }