75 lines
2.2 KiB
C++
75 lines
2.2 KiB
C++
#include "logic.hpp"
|
|
|
|
#include "engine.hpp"
|
|
#include "wren/vm/wren_vm.h"
|
|
|
|
#include <cassert>
|
|
#include <functional>
|
|
#include <iostream>
|
|
|
|
void writeOutput(WrenVM *vm, const char *text) {
|
|
std::cout << "wren output:" << text << "\n";
|
|
}
|
|
|
|
void errorOutput(WrenVM *vm, WrenErrorType type, const char *module, int line, const char *message) {
|
|
std::cerr << "wren error [module \"" << module << "\", line " << line << "]: " << message << "\n";
|
|
}
|
|
|
|
Logic::Logic(Engine *engine) {
|
|
WrenConfiguration wrenConfig;
|
|
wrenInitConfiguration(&wrenConfig);
|
|
|
|
// Setup user-defined data such as pointer to "global" objects.
|
|
_bundleData.engine = engine;
|
|
wrenConfig.userData = &_bundleData;
|
|
|
|
wrenConfig.bindForeignMethodFn = &Logic::bindForeignMethod;
|
|
wrenConfig.writeFn = &writeOutput;
|
|
wrenConfig.errorFn = &errorOutput;
|
|
|
|
_wrenVm = wrenNewVM(&wrenConfig);
|
|
WrenInterpretResult result = wrenInterpret(_wrenVm, "scripted-engine", "System.print(\"I am running in a VM!\")");
|
|
assert(result == WREN_RESULT_SUCCESS);
|
|
}
|
|
|
|
Logic::~Logic() {
|
|
wrenFreeVM(_wrenVm);
|
|
}
|
|
|
|
void get_info(WrenVM *vm) {
|
|
// Retrieve "global" objects defined in userData
|
|
BundleData *bundleData = reinterpret_cast<BundleData *>(vm->config.userData);
|
|
Engine *engine = bundleData->engine;
|
|
assert(engine);
|
|
|
|
wrenEnsureSlots(vm, 1);
|
|
wrenSetSlotDouble(vm, 0, engine->get_info());
|
|
}
|
|
|
|
WrenForeignMethodFn
|
|
Logic::bindForeignMethod(WrenVM *vm, const char *module, const char *className, bool isStatic, const char *signature) {
|
|
if (strcmp(module, "scripted-engine") == 0) {
|
|
if (strcmp(className, "Engine") == 0) {
|
|
if (isStatic && strcmp(signature, "get_info()") == 0) {
|
|
return get_info;
|
|
}
|
|
// Other foreign methods on Math...
|
|
}
|
|
// Other classes in main...
|
|
}
|
|
// Other modules...
|
|
return nullptr;
|
|
}
|
|
|
|
void Logic::interpret() {
|
|
assert(_wrenVm);
|
|
WrenInterpretResult result = wrenInterpret(_wrenVm, "scripted-engine", "class Engine {\
|
|
foreign static get_info())\
|
|
}\
|
|
Engine.get_info()");
|
|
assert(result == WREN_RESULT_SUCCESS);
|
|
}
|
|
|
|
void Logic::add_item() {
|
|
}
|