/* Copyright 2020-2022, Michele Santullo
* This file is part of wrenpp.
*
* Wrenpp is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Wrenpp is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with wrenpp. If not, see .
*/
#include "wrenpp/vm_fun.hpp"
#include
#include
#include
namespace wren {
namespace detail {
} //namespace detail
void set (VM& vm, int slot_num, const char* beg, const char* end) {
vm.set_slot_bytes(slot_num, beg, std::distance(beg, end));
}
std::string_view slot_string_view (VM& vm, int slot_num) {
assert(SlotType::String == vm.slot_type(slot_num));
auto ptr = vm.slot_bytes(slot_num);
return {ptr.first, static_cast(ptr.second)};
}
template<> const char* get (VM& vm, int slot_num) {
return vm.slot_string(slot_num);
}
template <> double get (VM& vm, int slot_num) {
return vm.slot_double(slot_num);
}
template <> bool get (VM& vm, int slot_num) {
return vm.slot_bool(slot_num);
}
template <> std::pair get> (VM& vm, int slot_num) {
return vm.slot_bytes(slot_num);
}
template<> int get (VM& vm, int slot_num) {
return static_cast(vm.slot_double(slot_num));
}
template<> std::size_t get (VM& vm, int slot_num) {
return static_cast(vm.slot_double(slot_num));
}
template<> std::string get (VM& vm, int slot_num) {
return {vm.slot_string(slot_num)};
}
template <> std::string_view get (VM& vm, int slot_num) {
auto arr = get>(vm, slot_num);
return std::string_view{arr.first, static_cast(arr.second)};
}
template <> std::vector get> (VM& vm, int slot_num) {
auto arr = get>(vm, slot_num);
return std::vector{arr.first, arr.first + arr.second};
}
void variable(VM& vm, ModuleAndName mod_and_name, int slot) {
vm.variable_or_throw(mod_and_name.module_name_char(), mod_and_name.class_name_char(), slot);
}
void variable (VM& vm, const Handle& handle, int slot) {
vm.set_slot_handle(handle, slot);
}
void variable_ensure_slot(VM& vm, ModuleAndName mod_and_name, int slot) {
vm.ensure_slots(1);
variable(vm, mod_and_name, slot);
}
void variable_ensure_slot (VM& vm, const Handle& handle, int slot) {
vm.ensure_slots(1);
variable(vm, handle, slot);
}
} //namespace wren