Add make_foreign_object() function.

It creates an instance of the given foreign class and
returns a pointer to it. The wren object is stored in
slot 0.
This commit is contained in:
King_DuckZ 2022-04-29 02:08:23 +02:00
parent 31ba3af348
commit a63a8c33d8

View file

@ -24,6 +24,7 @@
#include <vector>
#include <type_traits>
#include <cstddef>
#include <utility>
namespace wren {
typedef std::tuple<const char*, const char*> ModuleAndName;
@ -63,6 +64,9 @@ namespace wren {
template <typename T>
foreign_class_t make_foreign_class();
template <typename T, typename... Args>
T* make_foreign_object(VM& vm, const ModuleAndName& mn, Args&&... args);
template <auto V>
foreign_method_t make_method_bindable();
@ -303,6 +307,14 @@ namespace wren {
return ret;
}
template <typename T, typename... Args>
inline T* make_foreign_object(VM& vm, const ModuleAndName& mn, Args&&... args) {
variable(vm, mn, 0);
void* const mem = vm.set_slot_new_foreign(0, 0, sizeof(T));
T* const obj = new(mem) T{std::forward<Args>(args)...};
return obj;
}
template <auto V>
inline foreign_method_t make_method_bindable() {
return detail::MakeMethodBindable<V>::make();