diff -r c9fa93ce2f1f -r efc3f91dbe42 src/main.cpp --- a/src/main.cpp Sun Aug 05 01:56:10 2012 -0700 +++ b/src/main.cpp Sun Aug 05 02:17:40 2012 -0700 @@ -1,22 +1,24 @@ -/* +/* vim: set ts=4 sw=4 tw=99 et: + * * Copyright (C) 2012 David Anderson * - * This file is part of JITCraft. + * This file is part of SourcePawn. * - * JITCraft is free software: you can redistribute it and/or modify it under + * SourcePawn is free software: you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free * Software Foundation, either version 3 of the License, or (at your option) * any later version. * - * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY + * SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. * * You should have received a copy of the GNU General Public License along with - * JITCraft. If not, see http://www.gnu.org/licenses/. + * SourcePawn. If not, see http://www.gnu.org/licenses/. */ #include #include +#include #include "../include/keima.h" using namespace Ke; @@ -52,45 +54,22 @@ } } s_Reporter; -#if 0 -static bool -Compile(const char *stream, size_t length) +intptr_t PrintFloat(void *blah, intptr_t *params) { - - printf("\n ------------ \n\n"); - - Disassemble(module->getFunction(1)->code(), stdout); - //Disassemble(module->getFunction(1), stdout); - - printf("\n ------------ \n\n"); - - Function *main = module->getFunction(1); - - Top top = ZONE()->stack().begin(); - LocalTop localTop(top); + fprintf(stdout, "%f", *reinterpret_cast(¶ms[1])); + return 0; +} - Frame *frame = ZONE()->stack().pushFrame(main); - if (!frame) - return false; - - LocalSlot result; - if (!Interpret(ZONE(), frame, &result)) - return false; - - printf("Result: %d\n", result.i32); +intptr_t PrintNum(void *blah, intptr_t *params) +{ + fprintf(stdout, "%i", *reinterpret_cast(¶ms[1])); + return 0; +} - return true; -} -#endif - -intptr_t PrintToServer(void *blah, intptr_t *params) +intptr_t PrintString(void *blah, intptr_t *params) { - assert(params[0] == 3); - const char *fmt = reinterpret_cast(params[1]); - const char *str = reinterpret_cast(params[2]); - int *d = reinterpret_cast(params[3]); - printf(fmt, str, *d); - return 16; + fprintf(stdout, "%s", *reinterpret_cast(¶ms[1])); + return 0; } int main(int argc, char **argv) @@ -102,6 +81,9 @@ if (!Keima::Initialize()) return 1; + + int rval = 0; + Keima::InstallMessageReporter(&s_Reporter); { HandleScope scope; @@ -109,19 +91,41 @@ if (!module) return 1; - Keima::CollectGarbage(); + Keima::CollectGarbage(); + + // Look for a public named "main". + Local fun; + for (unsigned i = 0; i < module->numPublics(); i++) { + if (strcmp(module->getPublic(i)->name()->chars(), "main") == 0) { + fun = module->getPublic(i); + break; + } + } + + for (unsigned i = 0; i < module->numNatives(); i++) { + Local native = module->getNative(i); - //Local native = module->getNative(0); - //native->bind(reinterpret_cast(PrintToServer)); - //module->setNativeIdentity(reinterpret_cast(5)); + if (strcmp(native->name()->chars(), "PrintFloat") == 0) + native->bind(reinterpret_cast(PrintFloat)); + else if (strcmp(native->name()->chars(), "PrintNum") == 0) + native->bind(reinterpret_cast(PrintNum)); + else if (strcmp(native->name()->chars(), "PrintString") == 0) + native->bind(reinterpret_cast(PrintString)); + } - int result; - Local fun = module->getPublic(0); - if (Keima::Invoke(fun, NULL, 0, true, &result)) - printf("%d\n", result); + + if (!fun) { + fprintf(stdout, "Plugin compiles!\nNot running; no public 'main'.\n"); + } else { + Local fun = module->getPublic(0); + if (!Keima::Invoke(fun, NULL, 0, true, &rval)) + rval = 1; + } } Keima::Shutdown(); + + return rval; } /* Overload a few things to prevent libstdc++ linking */ @@ -132,22 +136,22 @@ void *operator new(size_t size) { - return malloc(size); + return malloc(size); } void *operator new[](size_t size) { - return malloc(size); + return malloc(size); } void operator delete(void *ptr) { - free(ptr); + free(ptr); } void operator delete[](void * ptr) { - free(ptr); + free(ptr); } #endif