Skip to content

Commit

Permalink
Add >=0.4.0 version check
Browse files Browse the repository at this point in the history
  • Loading branch information
PossiblyAShrub committed Dec 27, 2020
1 parent d6b7e33 commit 94b1d0a
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions include/wrenbind17/vm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ 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();
Expand All @@ -83,22 +84,46 @@ namespace wrenbind17 {
const auto mod = self.modules.find(name);
if (mod != self.modules.end()) {
auto source = mod->second.str();
char* buffer = new char[source.size() + 1];
std::memcpy(static_cast<void*>(buffer), &source[0], source.size() + 1);
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));
char* buffer = new char[source.size() + 1];
std::memcpy(static_cast<void*>(buffer), &source[0], source.size() + 1);
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));

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);
return buffer;
}

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);
return buffer;
} catch (std::exception& e) {
(void)e;
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 94b1d0a

Please sign in to comment.