Add reset() method

This commit is contained in:
King_DuckZ 2021-02-12 03:25:57 +01:00
parent 719a57d3e8
commit dc10c38a3f
2 changed files with 42 additions and 33 deletions

View file

@ -105,6 +105,7 @@ namespace wren {
bool has_user_data() const;
std::size_t dynafunc_byte_size() const;
void reset (Configuration* conf);
private:
struct LocalData;

View file

@ -137,39 +137,7 @@ namespace wren {
VM::VM (Configuration* conf, const detail::Callbacks& cb, void* user_data, std::uint32_t user_data_type) :
m_local(std::make_unique<LocalData>(cb))
{
WrenConfiguration wconf;
wrenInitConfiguration(&wconf);
wconf.userData = static_cast<void*>(&m_local->callbacks);
wconf.initialHeapSize = conf->initial_heap_size();
wconf.minHeapSize = conf->min_heap_size();
wconf.heapGrowthPercent = conf->heap_growth_percent();
if (cb.write_fn)
wconf.writeFn = &write_fn;
if (cb.error_fn)
wconf.errorFn = &error_fn;
if (cb.reallocate_fn)
wconf.reallocateFn = cb.reallocate_fn;
if (cb.resolve_module_fn)
wconf.resolveModuleFn = &resolve_module_fn;
if (cb.load_module_fn)
wconf.loadModuleFn = &load_module_fn;
if (cb.foreign_method_fn)
wconf.bindForeignMethodFn = &foreign_method_fn;
if (cb.foreign_class_fn)
wconf.bindForeignClassFn = &foreign_class_fn;
m_local->wvm = wrenNewVM(&wconf);
if (not m_local->wvm)
throw std::runtime_error("Failed to initialize Wren VM");
this->reset(conf);
m_local->user_data = user_data;
m_local->user_data_type = user_data_type;
}
@ -275,6 +243,46 @@ namespace wren {
return m_local->dynafunc.total_memory();
}
void VM::reset (Configuration* conf) {
WrenConfiguration wconf;
wrenInitConfiguration(&wconf);
wconf.userData = static_cast<void*>(&m_local->callbacks);
wconf.initialHeapSize = conf->initial_heap_size();
wconf.minHeapSize = conf->min_heap_size();
wconf.heapGrowthPercent = conf->heap_growth_percent();
auto& cb = m_local->callbacks;
if (cb.write_fn)
wconf.writeFn = &write_fn;
if (cb.error_fn)
wconf.errorFn = &error_fn;
if (cb.reallocate_fn)
wconf.reallocateFn = cb.reallocate_fn;
if (cb.resolve_module_fn)
wconf.resolveModuleFn = &resolve_module_fn;
if (cb.load_module_fn)
wconf.loadModuleFn = &load_module_fn;
if (cb.foreign_method_fn)
wconf.bindForeignMethodFn = &foreign_method_fn;
if (cb.foreign_class_fn)
wconf.bindForeignClassFn = &foreign_class_fn;
if (m_local->wvm)
wrenFreeVM(m_local->wvm);
m_local->wvm = wrenNewVM(&wconf);
if (not m_local->wvm)
throw std::runtime_error("Failed to initialize Wren VM");
m_local->dynafunc.clear();
}
void VM::ensure_slots (int num_slots) {
wrenEnsureSlots(m_local->wvm, num_slots);
}