Add arbitrary method to foreign_method_t conversion helper

It might not work in every case, you still are restricted
to types that set() and get() understand.
This commit is contained in:
King_DuckZ 2020-05-03 12:23:06 +02:00
parent 34d2317f11
commit 6a30725a62
3 changed files with 49 additions and 2 deletions

View file

@ -31,6 +31,7 @@ namespace {
construct new() {}
foreign static today()
foreign add_appointment(desc)
foreign appointment_count()
}
)script";
@ -39,6 +40,7 @@ R"script(import "calendar" for Calendar
var cale = Calendar.new()
System.print("Today is %(Calendar.today())")
cale.add_appointment("go get a haircut")
System.print("You have %(cale.appointment_count()) appointment(s)")
)script";
auto today_unique_str() {
@ -69,8 +71,8 @@ cale.add_appointment("go get a haircut")
vm.set_slot_string(0, today_unique_str().get());
}
void add_appointment (wren::VM& vm) {
m_appointments.push_back(wren::get<std::string>(vm, 1));
void add_appointment (std::string&& appointment) {
m_appointments.push_back(std::move(appointment));
}
void print_appointments() const {
@ -85,6 +87,10 @@ cale.add_appointment("go get a haircut")
}
}
std::size_t appointment_count() const {
return m_appointments.size();
}
private:
std::vector<std::string> m_appointments;
};
@ -111,6 +117,8 @@ cale.add_appointment("go get a haircut")
return &Calendar::today;
else if (not is_static and signature == "add_appointment(_)")
return wren::make_method_bindable<&Calendar::add_appointment>();
else if (not is_static and signature == "appointment_count()")
return wren::make_method_bindable<&Calendar::appointment_count>();
}
return nullptr;
}