Add support for foreign types to get()

wren::get() can now be used to get foreign types by pointer.
Invoking get<A>() will return an A*. With this change it's
now possible to use variables() to get mixed foreign
and core types.
This commit is contained in:
King_DuckZ 2020-05-03 11:32:43 +02:00
commit 34d2317f11
3 changed files with 49 additions and 16 deletions

View file

@ -34,36 +34,36 @@ namespace wren {
return {ptr.first, static_cast<std::size_t>(ptr.second)};
}
template<> const char* get (VM& vm, int slot_num) {
template<> const char* get<const char*> (VM& vm, int slot_num) {
return vm.slot_string(slot_num);
}
template <> double get (VM& vm, int slot_num) {
template <> double get<double> (VM& vm, int slot_num) {
return vm.slot_double(slot_num);
}
template <> bool get (VM& vm, int slot_num) {
template <> bool get<bool> (VM& vm, int slot_num) {
return vm.slot_bool(slot_num);
}
template <> std::pair<const char*, int> get (VM& vm, int slot_num) {
template <> std::pair<const char*, int> get<std::pair<const char*,int>> (VM& vm, int slot_num) {
return vm.slot_bytes(slot_num);
}
template<> int get (VM& vm, int slot_num) {
template<> int get<int> (VM& vm, int slot_num) {
return static_cast<int>(vm.slot_double(slot_num));
}
template<> std::string get (VM& vm, int slot_num) {
template<> std::string get<std::string> (VM& vm, int slot_num) {
return {vm.slot_string(slot_num)};
}
template <> std::string_view get (VM& vm, int slot_num) {
template <> std::string_view get<std::string_view> (VM& vm, int slot_num) {
auto arr = get<std::pair<const char*, int>>(vm, slot_num);
return std::string_view{arr.first, static_cast<std::size_t>(arr.second)};
}
template <> std::vector<char> get (VM& vm, int slot_num) {
template <> std::vector<char> get<std::vector<char>> (VM& vm, int slot_num) {
auto arr = get<std::pair<const char*, int>>(vm, slot_num);
return std::vector<char>{arr.first, arr.first + arr.second};
}