Example #1
0
LUALIB_API int luaL_callmeta (lua_State *L, int obj, const char *event) {
  obj = abs_index(L, obj);
  if (!luaL_getmetafield(L, obj, event))  /* no metafield? */
    return 0;
  lua_pushvalue(L, obj);
  lua_call(L, 1, 1);
  return 1;
}
Example #2
0
LUALIB_API void luaL_unref (lua_State *L, int t, int ref) {
  if (ref >= 0) {
    t = abs_index(L, t);
    lua_rawgeti(L, t, FREELIST_REF);
    lua_rawseti(L, t, ref);  /* t[ref] = t[FREELIST_REF] */
    lua_pushinteger(L, ref);
    lua_rawseti(L, t, FREELIST_REF);  /* t[FREELIST_REF] = ref */
  }
}
Example #3
0
LUALIB_API int luaL_getn (lua_State *L, int t) {
  int n;
  t = abs_index(L, t);
  lua_pushliteral(L, "n");  /* try t.n */
  lua_rawget(L, t);
  if ((n = checkint(L, 1)) >= 0) return n;
  getsizes(L);  /* else try sizes[t] */
  lua_pushvalue(L, t);
  lua_rawget(L, -2);
  if ((n = checkint(L, 2)) >= 0) return n;
  return (int)lua_objlen(L, t);
}
Example #4
0
/**
 * Register the lua object at index obj_i so it is keyed off of the
 * obj pointer.
 *
 * [-0, +0, ?]
 */
static void register_obj(lua_State*L, int obj_i, void* obj) {
    obj_i = abs_index(L, obj_i);

    lua_pushlightuserdata(L, &obj_registry);
    lua_rawget(L,            LUA_REGISTRYINDEX);
    assert(lua_istable(L, -1) /* create_obj_registry() should have ran */);

    lua_pushlightuserdata(L, obj);
    lua_pushvalue(L,         obj_i);
    lua_rawset(L,            -3);
    lua_pop(L,               1);
}
Example #5
0
LUALIB_API int luaX_ref(lua_State *L, int t, int l) {
    int ref;
    t = abs_index(L, t);
    l = abs_index(L, l);
    if (lua_isnil(L, -1)) {
        lua_pop(L, 1);     /* remove from stack */
        return LUA_REFNIL; /* 'nil' has a unique fixed reference */
    }
    lua_rawgeti(L, l, FREELIST_REF);   /* get first free element */
    ref = (int)lua_tointeger(L, -1);   /* ref = l[FREELIST_REF] */
    lua_pop(L, 1);                     /* remove it from stack */
    if (ref != 0) {                    /* any free element? */
        lua_rawgeti(L, l, ref);          /* remove it from list */
        lua_rawseti(L, l, FREELIST_REF); /* (l[FREELIST_REF] = l[ref]) */
    } else {                           /* no free elements */
        ref = (int)lua_objlen(L, l);
        ref++; /* create new reference */
    }
    lua_pushboolean(L, 1);
    lua_rawseti(L, l, ref); /* l[ref] = true */
    lua_rawseti(L, t, ref); /* t[ref] = value */
    return ref;
}
Example #6
0
LUALIB_API void luaL_setn (lua_State *L, int t, int n) {
  t = abs_index(L, t);
  lua_pushliteral(L, "n");
  lua_rawget(L, t);
  if (checkint(L, 1) >= 0) {  /* is there a numeric field `n'? */
    lua_pushliteral(L, "n");  /* use it */
    lua_pushinteger(L, n);
    lua_rawset(L, t);
  }
  else {  /* use `sizes' */
    getsizes(L);
    lua_pushvalue(L, t);
    lua_pushinteger(L, n);
    lua_rawset(L, -3);  /* sizes[t] = n */
    lua_pop(L, 1);  /* remove `sizes' */
  }
}
int luaL_getn (lua_State *L, int t) {
  int n;
  t = abs_index(L, t);
  lua_pushliteral(L, "n");  /* try t.n */
  lua_rawget(L, t);
  if ((n = checkint(L, 1)) >= 0) return n;
  getsizes(L);  /* else try sizes[t] */
  lua_pushvalue(L, t);
  lua_rawget(L, -2);
  if ((n = checkint(L, 2)) >= 0) return n;
  for (n = 1; ; n++) {  /* else must count elements */
    lua_rawgeti(L, t, n);
    if (lua_isnil(L, -1)) break;
    lua_pop(L, 1);
  }
  lua_pop(L, 1);
  return n - 1;
}
LUALIB_API int luaL_ref (lua_State *L, int t) {
  int ref;
  t = abs_index(L, t);
  if (lua_isnil(L, -1)) {
    lua_pop(L, 1);  /* remove from stack */
    return LUA_REFNIL;  /* `nil' has a unique fixed reference */
  }
  lua_rawgeti(L, t, FREELIST_REF);  /* get first free element */
  ref = (int)lua_tonumber(L, -1);  /* ref = t[FREELIST_REF] */
  lua_pop(L, 1);  /* remove it from stack */
  if (ref != 0) {  /* any free element? */
    lua_rawgeti(L, t, ref);  /* remove it from list */
    lua_rawseti(L, t, FREELIST_REF);  /* (t[FREELIST_REF] = t[ref]) */
  }
  else {  /* no free elements */
    ref = luaL_getn(L, t);
    if (ref < RESERVED_REFS)
      ref = RESERVED_REFS;  /* skip reserved references */
    ref++;  /* create new reference */
    luaL_setn(L, t, ref);
  }
  lua_rawseti(L, t, ref);
  return ref;
}
Example #9
0
String Util::describe_lua_value( lua_State * L , int index )
{
	index = abs_index( L , index );

	switch( lua_type( L , index ) )
	{
		case LUA_TNUMBER:
		{
			lua_pushvalue( L , index );
			String result = lua_tostring( L , -1 );
			lua_pop( L , 1 );
			return result;
		}
		case LUA_TSTRING:
			return Util::format( "\"%s\"" , lua_tostring( L , index ) );
		case LUA_TBOOLEAN:
			return lua_toboolean( L , index ) ? "true" : "false";
		case LUA_TNIL:
			return "nil";
		default:
			return UserData::describe( L , index );
	}

}
Example #10
0
static std::map<std::string, std::string> tableToMap(lua_State *L, int index)
{
    luaL_checktype(L, index, LUA_TTABLE);
    
    std::map<std::string, std::string> result;
    
    int t = abs_index(L, index);
    
	lua_pushnil(L);
	while (lua_next(L, t) != 0)
	{
		lua_pushvalue(L, -2);
        std::string key = luaL_checkstring(L, -1);
		lua_pop(L, 1);
		
        std::string value = luaL_checkstring(L, -1);
		
		result[key] = value;
		
		lua_pop(L, 1);
	}
    
    return result;
}
static void luaL_rawgetptr(lua_State *L, int idx, void *ptr)
{
	idx = abs_index(L, idx);
	lua_pushlightuserdata(L, ptr);
	lua_rawget(L, idx);
}