#include "api.h"

int luaopen_system(lua_State *L);
int luaopen_renderer(lua_State *L);
int luaopen_renwindow(lua_State *L);
int luaopen_regex(lua_State *L);
int luaopen_process(lua_State *L);
int luaopen_thread(lua_State* L);
int luaopen_dirmonitor(lua_State* L);
int luaopen_shmem(lua_State* L);
int luaopen_utf8extra(lua_State* L);
int luaopen_encoding(lua_State* L);
int luaopen_diff(lua_State *L);
int luaopen_canvas(lua_State* L);
int luaopen_tokenizer(lua_State *L);

#ifdef PRAGTICAL_NET
  int luaopen_net(lua_State* L);
  #define LUA_NET { "net",  luaopen_net  },
#else
  #define LUA_NET
#endif

#ifdef PRAGTICAL_REPL_HISTORY
  int luaopen_linenoise(lua_State *L);
  #define LUA_REPL { "repl",  luaopen_linenoise  },
#else
  #define LUA_REPL
#endif

#if LUA_VERSION_NUM < 503
  int luaopen_compat53_io(lua_State *L);
  int luaopen_compat53_string(lua_State *L);
  int luaopen_compat53_table(lua_State *L);
  int luaopen_compat53_utf8(lua_State *L);
  #ifndef LUA_JITLIBNAME
    int luaopen_bit(lua_State *L);
    #define LUABIT_COMPATIBILITY { "bit", luaopen_bit },
  #else
    #define LUABIT_COMPATIBILITY
  #endif
  #define LUA53_COMPATIBILITY \
    { "compat53.io", luaopen_compat53_io }, \
    { "compat53.string", luaopen_compat53_string }, \
    { "compat53.table", luaopen_compat53_table }, \
    { "compat53.utf8", luaopen_compat53_utf8 }, \
    LUABIT_COMPATIBILITY
#else
  #define LUA53_COMPATIBILITY
#endif

static const luaL_Reg libs[] = {
  { "system",     luaopen_system     },
  { "renderer",   luaopen_renderer   },
  { "renwindow",  luaopen_renwindow  },
  { "regex",      luaopen_regex      },
  { "process",    luaopen_process    },
  { "thread",     luaopen_thread     },
  { "dirmonitor", luaopen_dirmonitor },
  { "utf8extra",  luaopen_utf8extra  },
  { "encoding",   luaopen_encoding   },
  { "shmem",      luaopen_shmem      },
  { "diff",       luaopen_diff       },
  { "canvas",     luaopen_canvas     },
  { "tokenizer",  luaopen_tokenizer  },
  LUA_NET
  LUA_REPL
  LUA53_COMPATIBILITY
  { NULL, NULL }
};

#undef LUA53_COMPATIBILITY
#undef LUABIT_COMPATIBILITY

void api_load_libs(lua_State *L) {
  for (int i = 0; libs[i].name; i++)
    luaL_requiref(L, libs[i].name, libs[i].func, 1);
}
