Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <angelscript.h>
- #include <string.h>
- #include <assert.h>
- #include <sstream>
- #include <fstream>
- #include "scriptstdstring.h"
- #include "scriptbuilder.h"
- class ByteCodeCompilator : public asIBinaryStream
- {
- public:
- ByteCodeCompilator(FILE *fp) : f(fp) {}
- void Write(const void *ptr, asUINT size)
- {
- if( size == 0 ) return;
- fwrite(ptr, size, 1, f);
- }
- void Read(void *ptr, asUINT size)
- {
- if( size == 0 ) return;
- fread(ptr, size, 1, f);
- }
- protected:
- FILE *f;
- };
- bool TextCodeExist()
- {
- std::ifstream file("main.as");
- if(file)
- return true;
- else
- return false;
- }
- bool ByteCodeExist()
- {
- std::ifstream file("bin.asc");
- if(file)
- return true;
- else
- return false;
- }
- // Implement a simple message callback function
- void MessageCallback(const asSMessageInfo *msg, void *param)
- {
- const char *type = "ERR ";
- if( msg->type == asMSGTYPE_WARNING )
- type = "WARN";
- else if( msg->type == asMSGTYPE_INFORMATION )
- type = "INFO";
- std::cout<<msg->section<<std::endl;
- std::cout<<msg->row<<std::endl;
- std::cout<<msg->col<<std::endl;
- std::cout<<type<<std::endl;
- std::cout<<msg->message<<std::endl;
- }
- void print(int value)
- {
- std::cout<<value<<std::endl;
- }
- int main()
- {
- int r;
- asIScriptEngine * pScriptEngine = asCreateScriptEngine(ANGELSCRIPT_VERSION);
- RegisterStdString(pScriptEngine);
- if( pScriptEngine == 0 )
- {
- return -1;
- }
- r = pScriptEngine->RegisterGlobalFunction("void print(int value)", asFUNCTION(print), asCALL_CDECL);
- CScriptBuilder builder;
- r = builder.StartNewModule(pScriptEngine,"MyModule");
- asIScriptModule *mod;
- if(TextCodeExist()) //si le code text existe
- {
- FILE* FileLoaded;
- FileLoaded=fopen("bin.asc","w+");
- ByteCodeCompilator compilator(FileLoaded);
- builder.AddSectionFromFile("main.as");
- builder.BuildModule();
- mod = pScriptEngine->GetModule("MyModule");
- //and where loading the file to put the byte code in
- mod->SaveByteCode(&compilator);
- }
- else if(ByteCodeExist())
- {
- FILE* FileLoaded;
- FileLoaded=fopen("bin.asc","r+");
- ByteCodeCompilator* compilator=new ByteCodeCompilator(FileLoaded);
- builder.BuildModule();
- mod=pScriptEngine->GetModule("MyModule");
- std::cout<<mod->LoadByteCode(compilator)<<std::endl;
- }
- else
- return -100;
- //execution
- asIScriptFunction *func = mod->GetFunctionByDecl("void main()");
- asIScriptContext *ctx = pScriptEngine->CreateContext();
- ctx->Prepare(func);
- r = ctx->Execute();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement