Advertisement
Guest User

problem

a guest
Jul 29th, 2013
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.72 KB | None | 0 0
  1. #include <iostream>
  2. #include <angelscript.h>
  3. #include <string.h>
  4. #include <assert.h>
  5. #include <sstream>
  6. #include <fstream>
  7.  
  8. #include "scriptstdstring.h"
  9. #include "scriptbuilder.h"
  10.  
  11. class ByteCodeCompilator : public asIBinaryStream
  12. {
  13. public:
  14.   ByteCodeCompilator(FILE *fp) : f(fp) {}
  15.   void Write(const void *ptr, asUINT size)
  16.   {
  17.     if( size == 0 ) return;
  18.     fwrite(ptr, size, 1, f);
  19.   }
  20.   void Read(void *ptr, asUINT size)
  21.   {
  22.     if( size == 0 ) return;
  23.     fread(ptr, size, 1, f);
  24.   }
  25. protected:
  26.   FILE *f;
  27. };
  28.  
  29. bool TextCodeExist()
  30. {
  31.     std::ifstream file("main.as");
  32.     if(file)
  33.         return true;
  34.     else
  35.         return false;
  36. }
  37.  
  38. bool ByteCodeExist()
  39. {
  40.     std::ifstream file("bin.asc");
  41.     if(file)
  42.         return true;
  43.     else
  44.         return false;
  45. }
  46.  
  47. // Implement a simple message callback function
  48. void MessageCallback(const asSMessageInfo *msg, void *param)
  49. {
  50.   const char *type = "ERR ";
  51.   if( msg->type == asMSGTYPE_WARNING )
  52.     type = "WARN";
  53.   else if( msg->type == asMSGTYPE_INFORMATION )
  54.     type = "INFO";
  55.   std::cout<<msg->section<<std::endl;
  56.   std::cout<<msg->row<<std::endl;
  57.   std::cout<<msg->col<<std::endl;
  58.   std::cout<<type<<std::endl;
  59.   std::cout<<msg->message<<std::endl;
  60. }
  61.  
  62.  
  63. void print(int value)
  64. {
  65.     std::cout<<value<<std::endl;
  66. }
  67.  
  68. int main()
  69. {
  70.     int  r;
  71.     asIScriptEngine * pScriptEngine = asCreateScriptEngine(ANGELSCRIPT_VERSION);
  72.     RegisterStdString(pScriptEngine);
  73.     if( pScriptEngine == 0 )
  74.     {
  75.         return -1;
  76.     }
  77.  
  78.     r = pScriptEngine->RegisterGlobalFunction("void print(int value)", asFUNCTION(print), asCALL_CDECL);
  79.  
  80.     CScriptBuilder builder;
  81.     r = builder.StartNewModule(pScriptEngine,"MyModule");
  82.  
  83.     asIScriptModule *mod;
  84.  
  85.     if(TextCodeExist()) //si le code text existe
  86.     {
  87.         FILE* FileLoaded;
  88.         FileLoaded=fopen("bin.asc","w+");
  89.         ByteCodeCompilator compilator(FileLoaded);
  90.  
  91.         builder.AddSectionFromFile("main.as");
  92.         builder.BuildModule();
  93.         mod = pScriptEngine->GetModule("MyModule");
  94.         //and where loading the file to put the byte code in
  95.         mod->SaveByteCode(&compilator);
  96.     }
  97.     else if(ByteCodeExist())
  98.     {
  99.         FILE* FileLoaded;
  100.         FileLoaded=fopen("bin.asc","r+");
  101.         ByteCodeCompilator* compilator=new ByteCodeCompilator(FileLoaded);
  102.  
  103.         builder.BuildModule();
  104.         mod=pScriptEngine->GetModule("MyModule");
  105.         std::cout<<mod->LoadByteCode(compilator)<<std::endl;
  106.     }
  107.     else
  108.         return -100;
  109.  
  110.  
  111.  
  112.     //execution
  113.     asIScriptFunction *func = mod->GetFunctionByDecl("void main()");
  114.     asIScriptContext *ctx = pScriptEngine->CreateContext();
  115.     ctx->Prepare(func);
  116.     r = ctx->Execute();
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement