diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f91fdd7..0ff451c 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,5 +1,6 @@ add_subdirectory(engine) add_subdirectory(logic) +add_subdirectory(scripts) add_executable(sc-eng main.cpp) target_link_libraries(sc-eng engine logic) diff --git a/src/logic/logic.cpp b/src/logic/logic.cpp index 3b0c49b..5d2af09 100644 --- a/src/logic/logic.cpp +++ b/src/logic/logic.cpp @@ -4,30 +4,73 @@ #include "wren/vm/wren_vm.h" #include +#include #include #include +#include void writeOutput(WrenVM *vm, const char *text) { - std::cout << "wren output:" << text << "\n"; + std::cout << text; } void errorOutput(WrenVM *vm, WrenErrorType type, const char *module, int line, const char *message) { - std::cerr << "wren error [module \"" << module << "\", line " << line << "]: " << message << "\n"; + if (module != nullptr) { + std::cerr << "wren error [module \"" << module << "\", line " << line << "]: " << message << "\n"; + } else { + std::cerr << "wren error: " << message << "\n"; + } +} + +char *fileContent(const char *path) { + FILE *f = fopen(path, "rb"); + if (f == NULL) { + return nullptr; + } + + // Get the file length. + fseek(f, 0, SEEK_END); + size_t fileLength = ftell(f); + rewind(f); + + // Read the file. + char *fileContent = (char *)malloc(fileLength + 1); + size_t bytesRead = fread(fileContent, sizeof(char), fileLength, f); + fclose(f); + if (fileLength != bytesRead) { + free(fileContent); + return nullptr; + } + fileContent[fileLength] = '\0'; + + return fileContent; +} + +std::string getPath(const char *name) { + std::string root("/Users/ffreling/Code/scripted-engine/src/scripts/"); + return root + name + ".wren"; +} + +char *loadModule(WrenVM *vm, const char *name) { + std::string path = getPath(name); + + return fileContent(path.c_str()); } 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; + wrenConfig.loadModuleFn = &loadModule; _wrenVm = wrenNewVM(&wrenConfig); + + // Setup user-defined data such as pointer to "global" objects. + _bundleData.engine = engine; + wrenSetUserData(_wrenVm, &_bundleData); + WrenInterpretResult result = wrenInterpret(_wrenVm, "scripted-engine", "System.print(\"I am running in a VM!\")"); assert(result == WREN_RESULT_SUCCESS); } @@ -38,7 +81,7 @@ Logic::~Logic() { void get_info(WrenVM *vm) { // Retrieve "global" objects defined in userData - BundleData *bundleData = reinterpret_cast(vm->config.userData); + BundleData *bundleData = reinterpret_cast(wrenGetUserData(vm)); Engine *engine = bundleData->engine; assert(engine); @@ -48,25 +91,24 @@ void get_info(WrenVM *vm) { WrenForeignMethodFn Logic::bindForeignMethod(WrenVM *vm, const char *module, const char *className, bool isStatic, const char *signature) { - if (strcmp(module, "scripted-engine") == 0) { + if (strcmp(module, "engine") == 0) { if (strcmp(className, "Engine") == 0) { - if (isStatic && strcmp(signature, "get_info()") == 0) { + if (isStatic && strcmp(signature, "getInfo()") == 0) { return get_info; } - // Other foreign methods on Math... + // Other foreign methods on Engine... } - // Other classes in main... + // Other classes in engine... } // Other modules... return nullptr; } -void Logic::interpret() { +void Logic::interpret(const char *name) { assert(_wrenVm); - WrenInterpretResult result = wrenInterpret(_wrenVm, "scripted-engine", "class Engine {\ - foreign static get_info())\ -}\ -Engine.get_info()"); + std::string path = getPath(name); + char *script = fileContent(path.c_str()); + WrenInterpretResult result = wrenInterpret(_wrenVm, name, script); assert(result == WREN_RESULT_SUCCESS); } diff --git a/src/logic/logic.hpp b/src/logic/logic.hpp index 9de5416..5344700 100644 --- a/src/logic/logic.hpp +++ b/src/logic/logic.hpp @@ -15,7 +15,7 @@ public: static WrenForeignMethodFn bindForeignMethod(WrenVM *vm, const char *module, const char *className, bool isStatic, const char *signature); - void interpret(); + void interpret(const char *name); void add_item(); diff --git a/src/main.cpp b/src/main.cpp index c12fb4b..af3d97b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,7 +5,7 @@ int main() { Engine e; Logic l(&e); - l.interpret(); + l.interpret("logic"); return 0; } diff --git a/src/scripts/CMakeLists.txt b/src/scripts/CMakeLists.txt new file mode 100644 index 0000000..0e49096 --- /dev/null +++ b/src/scripts/CMakeLists.txt @@ -0,0 +1,4 @@ +set(MODULE scripts) + +file(GLOB_RECURSE script_list "*.wren") +add_custom_target(${MODULE} SOURCES ${script_list}) diff --git a/src/scripts/engine.wren b/src/scripts/engine.wren new file mode 100644 index 0000000..39b5f5c --- /dev/null +++ b/src/scripts/engine.wren @@ -0,0 +1,3 @@ +class Engine { + foreign static getInfo() +} diff --git a/src/scripts/logic.wren b/src/scripts/logic.wren new file mode 100644 index 0000000..688022a --- /dev/null +++ b/src/scripts/logic.wren @@ -0,0 +1,4 @@ +import "engine" for Engine + +System.print("This is logic.wren") +System.print(Engine.getInfo())