/*
 * wnix, a Unix-like operating system for WebAssembly applications.
 * Copyright (C) 2025  Xavier Del Campo Romero
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include <bin.h>
#include <bin/wnix.h>
#include <bin/mod.h>
#include <bin/types.h>
#include <nanowasm/nw.h>

static const struct nw_import imports[BIN_IMPORTS] =
{
    {
        .kind = NW_KIND_FUNCTION,
        .module = "wnix",
        .field = "exit",
        .u.function =
        {
            .fn = bin_wnix_exit,
            .signature = "(i)"
        }
    },

    {
        .kind = NW_KIND_FUNCTION,
        .module = "wnix",
        .field = "argc",
        .u.function =
        {
            .fn = bin_wnix_argc,
            .signature = "i()"
        }
    },

    {
        .kind = NW_KIND_FUNCTION,
        .module = "wnix",
        .field = "arglen",
        .u.function =
        {
            .fn = bin_wnix_arglen,
            .signature = "i(i)"
        }
    },

    {
        .kind = NW_KIND_FUNCTION,
        .module = "wnix",
        .field = "argcopy",
        .u.function =
        {
            .fn = bin_wnix_argcopy,
            .signature = "i(iii)"
        }
    },

    {
        .kind = NW_KIND_FUNCTION,
        .module = "wnix",
        .field = "mkdir",
        .u.function =
        {
            .fn = bin_wnix_mkdir,
            .signature = "i(iii)"
        }
    },

    {
        .kind = NW_KIND_FUNCTION,
        .module = "wnix",
        .field = "mount",
        .u.function =
        {
            .fn = bin_wnix_mount,
            .signature = "i(iiiiii)"
        }
    },

    {
        .kind = NW_KIND_FUNCTION,
        .module = "wnix",
        .field = "write",
        .u.function =
        {
            .fn = bin_wnix_write,
            .signature = "i(iiii)"
        }
    },

    {
        .kind = NW_KIND_FUNCTION,
        .module = "wnix",
        .field = "umask",
        .u.function =
        {
            .fn = bin_wnix_umask,
            .signature = "i(i)"
        }
    },

    {
        .kind = NW_KIND_FUNCTION,
        .module = "wnix",
        .field = "open",
        .u.function =
        {
            .fn = bin_wnix_open,
            .signature = "i(iiii)"
        }
    },

    {
        .kind = NW_KIND_FUNCTION,
        .module = "wnix",
        .field = "close",
        .u.function =
        {
            .fn = bin_wnix_close,
            .signature = "i(ii)"
        }
    }
};

const struct nw_mod_cfg bin_mod_cfg =
{
    .imports = imports,
    .n_imports = sizeof imports / sizeof *imports,
    .io =
    {
        .read = bin_mod_read,
        .eof = bin_mod_eof,
        .pc = bin_mod_pc,
        .seek = bin_mod_seek,
        .tell = bin_mod_tell,
    }
};
