Added printing natives.
authorDavid Anderson <dvander@alliedmods.net>
Sun Aug 05 02:17:40 2012 -0700 (2012-08-05)
changeset 163efc3f91dbe42
parent 162 c9fa93ce2f1f
child 164 3bf7c007883d
Added printing natives.
src/main.cpp
     1.1 --- a/src/main.cpp	Sun Aug 05 01:56:10 2012 -0700
     1.2 +++ b/src/main.cpp	Sun Aug 05 02:17:40 2012 -0700
     1.3 @@ -1,22 +1,24 @@
     1.4 -/*
     1.5 +/* vim: set ts=4 sw=4 tw=99 et:
     1.6 + *
     1.7   * Copyright (C) 2012 David Anderson
     1.8   *
     1.9 - * This file is part of JITCraft.
    1.10 + * This file is part of SourcePawn.
    1.11   *
    1.12 - * JITCraft is free software: you can redistribute it and/or modify it under
    1.13 + * SourcePawn is free software: you can redistribute it and/or modify it under
    1.14   * the terms of the GNU General Public License as published by the Free
    1.15   * Software Foundation, either version 3 of the License, or (at your option)
    1.16   * any later version.
    1.17   * 
    1.18 - * Foobar is distributed in the hope that it will be useful, but WITHOUT ANY
    1.19 + * SourcePawn is distributed in the hope that it will be useful, but WITHOUT ANY
    1.20   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    1.21   * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
    1.22   *
    1.23   * You should have received a copy of the GNU General Public License along with
    1.24 - * JITCraft. If not, see http://www.gnu.org/licenses/.
    1.25 + * SourcePawn. If not, see http://www.gnu.org/licenses/.
    1.26   */
    1.27  #include <stdio.h>
    1.28  #include <stdlib.h>
    1.29 +#include <string.h>
    1.30  #include "../include/keima.h"
    1.31  
    1.32  using namespace Ke;
    1.33 @@ -52,45 +54,22 @@
    1.34      }
    1.35  } s_Reporter;
    1.36  
    1.37 -#if 0
    1.38 -static bool
    1.39 -Compile(const char *stream, size_t length)
    1.40 +intptr_t PrintFloat(void *blah, intptr_t *params)
    1.41  {
    1.42 -
    1.43 -    printf("\n ------------ \n\n");
    1.44 -
    1.45 -    Disassemble(module->getFunction(1)->code(), stdout);
    1.46 -    //Disassemble(module->getFunction(1), stdout);
    1.47 -
    1.48 -    printf("\n ------------ \n\n");
    1.49 -
    1.50 -    Function *main = module->getFunction(1);
    1.51 -
    1.52 -    Top top = ZONE()->stack().begin();
    1.53 -    LocalTop localTop(top);
    1.54 +    fprintf(stdout, "%f", *reinterpret_cast<float *>(&params[1]));
    1.55 +    return 0;
    1.56 +}
    1.57  
    1.58 -    Frame *frame = ZONE()->stack().pushFrame(main);
    1.59 -    if (!frame)
    1.60 -        return false;
    1.61 -
    1.62 -    LocalSlot result;
    1.63 -    if (!Interpret(ZONE(), frame, &result))
    1.64 -        return false;
    1.65 -
    1.66 -    printf("Result: %d\n", result.i32);
    1.67 +intptr_t PrintNum(void *blah, intptr_t *params)
    1.68 +{
    1.69 +    fprintf(stdout, "%i", *reinterpret_cast<int *>(&params[1]));
    1.70 +    return 0;
    1.71 +}
    1.72  
    1.73 -    return true;
    1.74 -}
    1.75 -#endif
    1.76 -
    1.77 -intptr_t PrintToServer(void *blah, intptr_t *params)
    1.78 +intptr_t PrintString(void *blah, intptr_t *params)
    1.79  {
    1.80 -    assert(params[0] == 3);
    1.81 -    const char *fmt = reinterpret_cast<const char *>(params[1]);
    1.82 -    const char *str = reinterpret_cast<const char *>(params[2]);
    1.83 -    int *d = reinterpret_cast<int *>(params[3]);
    1.84 -    printf(fmt, str, *d);
    1.85 -    return 16;
    1.86 +    fprintf(stdout, "%s", *reinterpret_cast<const char **>(&params[1]));
    1.87 +    return 0;
    1.88  }
    1.89  
    1.90  int main(int argc, char **argv)
    1.91 @@ -102,6 +81,9 @@
    1.92  
    1.93      if (!Keima::Initialize())
    1.94          return 1;
    1.95 +
    1.96 +    int rval = 0;
    1.97 +
    1.98      Keima::InstallMessageReporter(&s_Reporter);
    1.99      {
   1.100          HandleScope scope;
   1.101 @@ -109,19 +91,41 @@
   1.102          if (!module)
   1.103              return 1;
   1.104  
   1.105 -	Keima::CollectGarbage();
   1.106 +        Keima::CollectGarbage();
   1.107 +
   1.108 +        // Look for a public named "main".
   1.109 +        Local<Function> fun;
   1.110 +        for (unsigned i = 0; i < module->numPublics(); i++) {
   1.111 +            if (strcmp(module->getPublic(i)->name()->chars(), "main") == 0) {
   1.112 +                fun = module->getPublic(i);
   1.113 +                break;
   1.114 +            }
   1.115 +        }
   1.116 +
   1.117 +        for (unsigned i = 0; i < module->numNatives(); i++) {
   1.118 +            Local<Native> native = module->getNative(i);
   1.119  
   1.120 -        //Local<Native> native = module->getNative(0);
   1.121 -        //native->bind(reinterpret_cast<void *>(PrintToServer));
   1.122 -        //module->setNativeIdentity(reinterpret_cast<void *>(5));
   1.123 +            if (strcmp(native->name()->chars(), "PrintFloat") == 0)
   1.124 +                native->bind(reinterpret_cast<void *>(PrintFloat));
   1.125 +            else if (strcmp(native->name()->chars(), "PrintNum") == 0)
   1.126 +                native->bind(reinterpret_cast<void *>(PrintNum));
   1.127 +            else if (strcmp(native->name()->chars(), "PrintString") == 0)
   1.128 +                native->bind(reinterpret_cast<void *>(PrintString));
   1.129 +        }
   1.130  
   1.131 -        int result;
   1.132 -        Local<Function> fun = module->getPublic(0);
   1.133 -        if (Keima::Invoke(fun, NULL, 0, true, &result))
   1.134 -            printf("%d\n", result);
   1.135 +
   1.136 +        if (!fun) {
   1.137 +            fprintf(stdout, "Plugin compiles!\nNot running; no public 'main'.\n");
   1.138 +        } else {
   1.139 +            Local<Function> fun = module->getPublic(0);
   1.140 +            if (!Keima::Invoke(fun, NULL, 0, true, &rval))
   1.141 +                rval = 1;
   1.142 +        }
   1.143      }
   1.144  
   1.145      Keima::Shutdown();
   1.146 +
   1.147 +    return rval;
   1.148  }
   1.149  
   1.150  /* Overload a few things to prevent libstdc++ linking */
   1.151 @@ -132,22 +136,22 @@
   1.152  
   1.153  void *operator new(size_t size)
   1.154  {
   1.155 -	return malloc(size);
   1.156 +    return malloc(size);
   1.157  }
   1.158  
   1.159  void *operator new[](size_t size) 
   1.160  {
   1.161 -	return malloc(size);
   1.162 +    return malloc(size);
   1.163  }
   1.164  
   1.165  void operator delete(void *ptr) 
   1.166  {
   1.167 -	free(ptr);
   1.168 +    free(ptr);
   1.169  }
   1.170  
   1.171  void operator delete[](void * ptr)
   1.172  {
   1.173 -	free(ptr);
   1.174 +    free(ptr);
   1.175  }
   1.176  #endif
   1.177