77 lines
2.2 KiB
C++
77 lines
2.2 KiB
C++
#include <iostream>
|
|
#include <memory>
|
|
|
|
#include <vfspp/VirtualFileSystem.hpp>
|
|
#include <vfspp/MemoryFileSystem.hpp>
|
|
#include <angelscript.h>
|
|
#include <scriptstdstring/scriptstdstring.h>
|
|
#include <scriptbuilder/scriptbuilder.h>
|
|
|
|
|
|
void print(const std::string& in) {
|
|
std::cout << in;
|
|
}
|
|
|
|
|
|
int main(int argc, char* argv[]) {
|
|
int error;
|
|
|
|
// prepare a virtual file system
|
|
auto fileSystem = std::make_unique<vfspp::VirtualFileSystem>();
|
|
|
|
auto fileSystemTmp = std::make_shared<vfspp::MemoryFileSystem>();
|
|
fileSystemTmp->Initialize();
|
|
fileSystem->AddFileSystem("/tmp", fileSystemTmp);
|
|
|
|
// engine
|
|
asIScriptEngine* engine = asCreateScriptEngine();
|
|
if (engine == nullptr)
|
|
throw std::runtime_error("Could not create the AngelScript Engine.");
|
|
|
|
RegisterStdString(engine);
|
|
|
|
// functions
|
|
error = engine->RegisterGlobalFunction("void print(const string& in)", asFUNCTION(print), asCALL_CDECL);
|
|
if (error < 0)
|
|
throw std::runtime_error("Could not register the print function.");
|
|
|
|
// script
|
|
CScriptBuilder builder;
|
|
|
|
error = builder.StartNewModule(engine, "script");
|
|
if (error < 0)
|
|
throw std::runtime_error("Could not start a new module.");
|
|
|
|
error = builder.AddSectionFromMemory("script", "void main() { print(\"hello world !\"); }");
|
|
if (error < 0)
|
|
throw std::runtime_error("Could not add a section to the module.");
|
|
|
|
error = builder.BuildModule();
|
|
if (error < 0)
|
|
throw std::runtime_error("Could not build the module.");
|
|
|
|
// execution
|
|
asIScriptModule *module = engine->GetModule("script");
|
|
if (module == nullptr)
|
|
throw std::runtime_error("Could not get the module.");
|
|
|
|
asIScriptFunction *function = module->GetFunctionByDecl("void main()");
|
|
if (function == nullptr)
|
|
throw std::runtime_error("Could not get the main function.");
|
|
|
|
asIScriptContext *context = engine->CreateContext();
|
|
if (context == nullptr)
|
|
throw std::runtime_error("Could not create the context.");
|
|
|
|
error = context->Prepare(function);
|
|
if (error < 0)
|
|
throw std::runtime_error("Could not register the function in the context.");
|
|
|
|
error = context->Execute();
|
|
|
|
// clean up
|
|
context->Release();
|
|
engine->Release();
|
|
|
|
return error;
|
|
}
|