diff --git a/examples/calendar/main.cpp b/examples/calendar/main.cpp index 1b33e56..443bcbd 100644 --- a/examples/calendar/main.cpp +++ b/examples/calendar/main.cpp @@ -102,12 +102,16 @@ System.print("You have %(cale.appointment_count()) appointment(s)") char* load_module_fn(wren::VM* vm, std::string_view module_name) { if (module_name == "calendar") { constexpr const std::size_t buff_sz = sizeof(g_calendar_src); - char* const buff = static_cast(MyConf::reallocate_fn(nullptr, buff_sz)); + char* const buff = new char[buff_sz]; std::copy(g_calendar_src, g_calendar_src + buff_sz / sizeof(g_calendar_src[0]), buff); return buff; } return nullptr; } + + void load_module_complete_fn(wren::VM*, std::string_view, char* buffer) { + delete[] buffer; + } }; } //unnamed namespace diff --git a/examples/math_vector/main.cpp b/examples/math_vector/main.cpp index a7e187b..5b975e4 100644 --- a/examples/math_vector/main.cpp +++ b/examples/math_vector/main.cpp @@ -81,9 +81,10 @@ System.print("vec3 modified by scripting: <%(vec3.x), %(vec3.y), %(vec3.z)>") public: char* load_module_fn(wren::VM* vm, std::string_view module_name) { if (module_name == "math_vector") { - constexpr const std::size_t buff_sz = g_math_vector_src.size(); + constexpr const std::size_t buff_sz = g_math_vector_src.size() + 1; char* const buff = static_cast(MyConf::reallocate_fn(nullptr, buff_sz)); std::copy(g_math_vector_src.cbegin(), g_math_vector_src.cend(), buff); + buff[buff_sz - 1] = '\0'; return buff; } return nullptr; diff --git a/include/wrenpp/vm.hpp b/include/wrenpp/vm.hpp index db27206..72a9968 100644 --- a/include/wrenpp/vm.hpp +++ b/include/wrenpp/vm.hpp @@ -51,7 +51,7 @@ namespace wren { char* (*load_module_fn)(Configuration&, VM*, wren_string_t) {nullptr}; foreign_method_t (*foreign_method_fn)(Configuration&, VM*, wren_string_t, wren_string_t, bool, wren_string_t) {nullptr}; foreign_class_t (*foreign_class_fn)(Configuration&, VM*, wren_string_t, wren_string_t) {nullptr}; - void (*load_module_complete_fn)(Configuration&, VM*, wren_string_t); + void (*load_module_complete_fn)(Configuration&, VM*, wren_string_t, char*) {nullptr}; Configuration* config_obj {nullptr}; VM* owner {nullptr}; @@ -130,6 +130,7 @@ namespace wren { define_method_info(load_module_fn, load_module, char*, VM*, wren_string_t); define_method_info(foreign_method_fn, foreign_method, foreign_method_t, VM*, wren_string_t, wren_string_t, bool, wren_string_t); define_method_info(foreign_class_fn, foreign_class, foreign_class_t, VM*, wren_string_t, wren_string_t); + define_method_info(load_module_complete_fn, load_module_complete, void, VM*, wren_string_t, char*); template struct AnyFunctionWrap; template @@ -185,6 +186,9 @@ namespace wren { if constexpr (detail::method_foreign_class::exists::value) ret.foreign_class_fn = &detail::AnyFunctionWrap::template call<&T::foreign_class_fn>; + if constexpr (detail::method_load_module_complete::exists::value) + ret.load_module_complete_fn = &detail::AnyFunctionWrap::template call<&T::load_module_complete_fn>; + return ret; } diff --git a/src/def_configuration.cpp b/src/def_configuration.cpp index 2763549..9c03a5c 100644 --- a/src/def_configuration.cpp +++ b/src/def_configuration.cpp @@ -21,6 +21,7 @@ #include "wrenpp/vm.hpp" #include #include +#include namespace wren { namespace { @@ -48,25 +49,12 @@ namespace wren { } void* DefConfiguration::reallocate_fn (void* ptr, std::size_t size) { - if (ptr and size) { - //reallocate - char* const old_mem = static_cast(ptr); - char* const new_mem = new char[size]; - std::copy(old_mem, old_mem + size, new_mem); - delete[] old_mem; - return new_mem; + if (0 == size) { + std::free(ptr); + return nullptr; } - else if (ptr and not size) { - //free - char* const old_mem = static_cast(ptr); - delete[] old_mem; - } - else if (not ptr and size) { - //allocate - char* const new_mem = new char[size]; - return new_mem; - } - return nullptr; + + return std::realloc(ptr, size); } foreign_method_t DefConfiguration::foreign_method_fn (VM* vm, wren_string_t module_name, wren_string_t class_name, bool is_static, wren_string_t signature) { diff --git a/src/vm.cpp b/src/vm.cpp index ecc1d1b..dff58c8 100644 --- a/src/vm.cpp +++ b/src/vm.cpp @@ -77,7 +77,7 @@ namespace wren { return ::WrenLoadModuleResult{ .source = source, .onComplete = (cb->load_module_complete_fn ? &load_module_complete_fn : nullptr), - .userData = user_data + .userData = nullptr }; } @@ -102,7 +102,11 @@ namespace wren { assert(cb); assert(cb->load_module_complete_fn and cb->config_obj and cb->owner); - (*cb->load_module_complete_fn)(*cb->config_obj, cb->owner, name); + //Documentation says result is the same object we returned from + //load_module_fn. It should then be safe to const_cast source, + //since it wasn't const when we got it there + char* const non_const_buff = const_cast(result.source); + (*cb->load_module_complete_fn)(*cb->config_obj, cb->owner, name, non_const_buff); } WrenForeignClassMethods foreign_class_fn (WrenVM* wvm, const char* module, const char* class_name) {