ona/source/runtime.cpp

97 lines
2.7 KiB
C++

export module runtime;
import app;
import coral;
import coral.files;
import coral.io;
import coral.math;
import coral.sequence;
import kym;
import kym.environment;
extern "C" int main(int argc, char const * const * argv) {
return app::display("Ona Runtime", [](app::system & system, app::graphics & graphics) -> int {
constexpr coral::path config_path{"config.kym"};
bool is_config_loaded{false};
system.res_fs().read_file(config_path, [&](coral::reader & file) {
coral::allocator * const allocator{&system.thread_safe_allocator()};
kym::vm vm{allocator, [&system](coral::slice<char const> const & error_message) {
system.log(app::log_level::error, error_message);
}};
if (!vm.init({.datastack_size = 64, .callstack_size = 64})) {
system.log(app::log_level::error, "failed to allocate memory for config vm");
return;
}
coral::stack<coral::u8> script_source{allocator};
{
coral::u8 stream_buffer[1024]{0};
coral::contiguous_writer script_writer{&script_source};
if (!coral::stream(script_writer, file, stream_buffer).is_ok()) return;
}
vm.with_object(vm.compile(script_source.as_slice().as_chars()), [&](kym::bound_object & script) {
vm.with_object(script.call({}), [&](kym::bound_object & config) {
constexpr auto as_u16 = [](coral::i64 value) -> coral::optional<coral::i16> {
if ((value >= 0) && (value <= coral::u16_max))
return static_cast<coral::u16>(value);
return {};
};
coral::u16 const width{config.get_field("width").
as_integer().map(as_u16).value_or(0)};
if (width == 0) return system.log(app::log_level::error,
"failed to decode `width` property of config");
coral::u16 const height{config.get_field("height").
as_integer().map(as_u16).value_or(0)};
if (height == 0) return system.log(app::log_level::error,
"failed to decode `height` property of config");
if (graphics.show(width, height) != app::graphics::show_result::ok)
return system.log(app::log_level::error, "failed to initialize window");
vm.with_object(config.get_field("title"), [&](kym::bound_object & title) {
// TODO: implement.
is_config_loaded = true;
});
});
});
});
if (!is_config_loaded) {
coral::stack<coral::u8> error_message{&system.thread_safe_allocator()};
{
coral::contiguous_writer error_writer{&error_message};
if (!error_writer.write(coral::slice{"failed to load "}.as_bytes()).is_ok())
return coral::u8_max;
if (!error_writer.write(config_path.as_slice().as_bytes()).is_ok())
return coral::u8_max;
}
return coral::u8_max;
}
// app::canvas canvas_2d();
while (system.poll()) {
// canvas_2d.render(graphics);
graphics.present();
}
return 0;
});
}