Split out wren code, add a ticker_quick() method

This commit is contained in:
King_DuckZ 2022-05-14 07:42:34 +02:00
commit 6bd0dbb07e
5 changed files with 119 additions and 38 deletions

View file

@ -20,43 +20,47 @@
#include <wrenpp/def_configuration.hpp>
#include <wrenpp/callback_manager.hpp>
#include <utility>
#include <algorithm>
namespace {
constexpr char g_script[] =
R"script(
foreign class TickerPrice {
construct new() { }
foreign price
foreign currency
foreign exchange
foreign is_valid
}
class Bitcoinity {
foreign static ticker(currency, exchange)
}
import "bitcoinity" for Bitcoinity
class App {
construct new(currency, exchange) {
construct new(exchange, currency) {
_currency = currency
_exchange = exchange
}
start() {
var price = Bitcoinity.ticker(_currency, _exchange)
System.print("Ticker: %(price.price) %(price.currency) on %(price.exchange)")
var price_quick = Bitcoinity.ticker_quick()
System.print("Ticker (quick): %(price_quick.price) %(price_quick.currency) on %(price_quick.exchange)")
var price = Bitcoinity.ticker(_exchange, _currency)
System.print("Ticker (%(_exchange), %(_currency)): %(price.price) %(price.currency) on %(price.exchange)")
}
}
var the_app = App.new("USD", "kraken")
var the_app = App.new("kraken", "USD")
)script";
void quick_ticker_price_bitcoinity_wren (wren::VM& vm) {
using duck::TickerPrice;
using duck::bitcoinity_ticker_price;
TickerPrice* const tp = wren::make_foreign_object<TickerPrice>(vm, "ticker_price");
constexpr bool exact_match = false;
*tp = bitcoinity_ticker_price("kraken", "EUR", exact_match);
}
void ticker_price_bitcoinity_wren (wren::VM& vm) {
using duck::TickerPrice;
using duck::bitcoinity_ticker_price;
TickerPrice* const tp = wren::make_foreign_object<TickerPrice>(vm, "main");
*tp = duck::bitcoinity_ticker_price(wren::get<const char*>(vm, 1), wren::get<const char*>(vm, 2));
TickerPrice* const tp = wren::make_foreign_object<TickerPrice>(vm, "ticker_price");
constexpr bool exact_match = true;
*tp = bitcoinity_ticker_price(wren::get<const char*>(vm, 1), wren::get<const char*>(vm, 2), exact_match);
}
class MyWrenConfiguration : public wren::DefConfiguration {
@ -66,11 +70,27 @@ public:
std::string_view module,
std::string_view class_name
) {
if (module == "main" and class_name == "TickerPrice")
if (module == "ticker_price" and class_name == "TickerPrice")
return wren::make_foreign_class<duck::TickerPrice>();
else
return {nullptr, nullptr};
}
char* load_module_fn(wren::VM* vm, std::string_view module_name) {
std::string_view wren_code;
if (module_name == "bitcoinity")
wren_code = duck::g_bitcoinity_wren_module;
else if (module_name == "ticker_price")
wren_code = duck::g_ticker_price_wren_module;
else
return nullptr;
const std::size_t buff_sz = wren_code.size();
char* const buff = static_cast<char*>(MyWrenConfiguration::reallocate_fn(nullptr, buff_sz));
std::copy(wren_code.cbegin(), wren_code.cend(), buff);
return buff;
}
};
} //unnamed namespace
@ -80,11 +100,12 @@ int main() {
MyWrenConfiguration config;
wren::VM vm(&config, nullptr);
vm.callback_manager()
.add_callback(true, "main", "Bitcoinity", "ticker(_,_)", &ticker_price_bitcoinity_wren)
.add_callback(false, "main", "TickerPrice", "price", wren::make_method_bindable<&TickerPrice::price>())
.add_callback(false, "main", "TickerPrice", "currency", wren::make_method_bindable<&TickerPrice::currency>())
.add_callback(false, "main", "TickerPrice", "is_valid", wren::make_method_bindable<&TickerPrice::is_valid>())
.add_callback(false, "main", "TickerPrice", "exchange", wren::make_method_bindable<&TickerPrice::exchange>());
.add_callback(true, "bitcoinity", "Bitcoinity", "ticker(_,_)", &ticker_price_bitcoinity_wren)
.add_callback(true, "bitcoinity", "Bitcoinity", "ticker_quick()", &quick_ticker_price_bitcoinity_wren)
.add_callback(false, "ticker_price", "TickerPrice", "price", wren::make_method_bindable<&TickerPrice::price>())
.add_callback(false, "ticker_price", "TickerPrice", "currency", wren::make_method_bindable<&TickerPrice::currency>())
.add_callback(false, "ticker_price", "TickerPrice", "is_valid", wren::make_method_bindable<&TickerPrice::is_valid>())
.add_callback(false, "ticker_price", "TickerPrice", "exchange", wren::make_method_bindable<&TickerPrice::exchange>());
vm.interpret("main", g_script);
wren::call<void>(vm, {"main", "the_app"}, "start");