/*
  Copyright (c) 2007-2008 Gordon Gremme <gordon@gremme.org>
  Copyright (c) 2007-2008 Center for Bioinformatics, University of Hamburg

  Permission to use, copy, modify, and distribute this software for any
  purpose with or without fee is hereby granted, provided that the above
  copyright notice and this permission notice appear in all copies.

  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

#include "lua.h"
#include "annotationsketch/luastyle.h"

/* key used to store the Style object in the Lua registry */
#define STYLE_KEY gt_style_new

void gt_lua_put_style_in_registry(lua_State *L, GtStyle *style)
{
#ifndef NDEBUG
  int stack_size;
#endif
  gt_assert(L && style);
#ifndef NDEBUG
  stack_size = lua_gettop(L);
#endif
  lua_pushlightuserdata(L, STYLE_KEY);
  lua_pushlightuserdata(L, style);
  lua_rawset(L, LUA_REGISTRYINDEX);
  gt_assert(lua_gettop(L) == stack_size); /* make sure the stack doesn't grow */
}

GtStyle* gt_lua_get_style_from_registry(lua_State *L)
{
#ifndef NDEBUG
  int stack_size;
#endif
  GtStyle *style;
  gt_assert(L);
#ifndef NDEBUG
  stack_size = lua_gettop(L);
#endif
  lua_pushlightuserdata(L, STYLE_KEY);
  lua_rawget(L, LUA_REGISTRYINDEX);
  gt_assert(lua_islightuserdata(L, -1));
  style = lua_touserdata(L, -1);
  lua_pop(L, 1);
  gt_assert(lua_gettop(L) == stack_size); /* make sure the stack doesn't grow */
  return style;
}
