Skip to content

Commit

Permalink
Merge pull request #3 from PossiblyAShrub/wren-0.4.0-compat
Browse files Browse the repository at this point in the history
Update default reallocateFn & loadModuleFn to match new wren 0.4.0 signatures
  • Loading branch information
matusnovak committed Dec 28, 2020
2 parents 4857a38 + 94b1d0a commit bf0cf5d
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions include/wrenbind17/vm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,32 @@ namespace wrenbind17 {
data->config.minHeapSize = minHeap;
data->config.heapGrowthPercent = heapGrowth;
data->config.userData = data.get();
#if WREN_VERSION_NUMBER >= 4000 // >= 0.4.0
data->config.reallocateFn = [](void* memory, size_t newSize, void* userData) -> void* { return std::realloc(memory, newSize); };
data->config.loadModuleFn = [](WrenVM* vm, const char* name) -> WrenLoadModuleResult {
auto res = WrenLoadModuleResult();
auto& self = *reinterpret_cast<VM::Data*>(wrenGetUserData(vm));

const auto mod = self.modules.find(name);
if (mod != self.modules.end()) {
auto source = mod->second.str();
auto buffer = new char[source.size() + 1];
std::memcpy(buffer, &source[0], source.size() + 1);
res.source = buffer;
return res;
}

try {
auto source = self.loadFileFn(self.paths, std::string(name));
auto buffer = new char[source.size() + 1];
std::memcpy(buffer, &source[0], source.size() + 1);
res.source = buffer;
} catch (std::exception& e) {
(void)e;
}
return res;
};
#else // < 0.4.0
data->config.reallocateFn = std::realloc;
data->config.loadModuleFn = [](WrenVM* vm, const char* name) -> char* {
auto& self = *reinterpret_cast<VM::Data*>(wrenGetUserData(vm));
Expand All @@ -97,6 +123,7 @@ namespace wrenbind17 {
return nullptr;
}
};
#endif // WREN_VERSION_NUMBER >= 4000
data->config.bindForeignMethodFn = [](WrenVM* vm, const char* module, const char* className,
const bool isStatic, const char* signature) -> WrenForeignMethodFn {
auto& self = *reinterpret_cast<VM::Data*>(wrenGetUserData(vm));
Expand Down

0 comments on commit bf0cf5d

Please sign in to comment.