// SPDX-License-Identifier: GPL-2.0-or-later /* * A Janet script for the livecoding experience * * Authors: * Sol Bekic * Copyright (C) 2019 Authors * * Released under GNU GPL v2+, read the file 'COPYING' for more information. */ #include "livecode/script.h" namespace Inkscape { namespace Livecode { Script::Script(JanetTable *env, std::string const &path) : env(env) , function(nullptr) , file(Gio::File::create_for_path(path)) , monitor(file->monitor_file()) { monitor->signal_changed().connect(sigc::mem_fun(this, &Script::file_changed)); reload(); } void Script::frame() { if (function) { Janet result; JanetSignal status = janet_pcall(function, 0, NULL, &result, NULL); if (status == JANET_SIGNAL_ERROR && janet_checktype(result, JANET_STRING)) { g_message("error executing Janet Script: %s", (gchar const *)janet_unwrap_string(result)); } } } void Script::commit() { } void Script::reload() { try { uint8_t *data; size_t length; file->load_contents((char *&)data, (gsize &)length); Janet result; janet_dobytes(env, data, length, file->get_path().c_str(), &result); if (janet_checktype(result, JANET_FUNCTION)) { function = janet_unwrap_function(result); } else { g_message("Janet script didn't return a function"); } } catch (...) { g_message("error loading file"); } } void Script::file_changed(const Glib::RefPtr& file, const Glib::RefPtr& other_file, Gio::FileMonitorEvent event) { reload(); } } } /* Local Variables: mode:c++ c-file-style:"stroustrup" c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +)) indent-tabs-mode:nil fill-column:99 End: */ // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :