Ejemplo n.º 1
0
static int libE_insert (lua_State *L) {
  int e = aux_getn(L, 1) + 1;  /* first empty element */
  int pos;  /* where to insert new element */
  switch (lua_gettop(L)) {
    case 2: {  /* called with only 2 arguments */
      pos = e;  /* insert new element at the end */
      break;
    }
    case 3: {
      int i;
      pos = luaL_checkint(L, 2);  /* 2nd argument is the position */
      if (pos > e) e = pos;  /* `grow' array if necessary */
      for (i = e; i > pos; i--) {  /* move up elements */
        lua_rawgeti(L, 1, i-1);
        lua_rawseti(L, 1, i);  /* t[i] = t[i-1] */
      }
      break;
    }
    default: {
      return lxs_error(L, "wrong number of arguments to " LUA_QL("insert"));
    }
  }
  luaL_setn(L, 1, e);  /* new size */
  lua_rawseti(L, 1, pos);  /* t[pos] = v */
  return 0;
}
Ejemplo n.º 2
0
static int t_insert (lua_State *L) {
  int e = aux_getn(L, 1) + 1;
  int pos;
  int num = lua_gettop(L);
  switch (num) {
    case 0: case 1: {
      return luaL_error(L, "wrong number of arguments to " LUA_QL("insert"));
    }
    case 2: {
      pos = e;
      num = 1;
      break;
    }
    default: {
      int i;
      num -= 2;
      pos = luaL_checkint(L, 2);
      if (pos > e) e = pos;
      for (i = e+num-1; i > pos; i--) {
        lua_rawgeti(L, 1, i-num);
        lua_rawseti(L, 1, i);
      }
      pos = pos + num - 1;
      break;
    }
  }
  luaL_setn(L, 1, e);  /* new size */
  while (num-- > 0)
    lua_rawseti(L, 1, pos--);  /* t[pos] = v */
  lua_settop(L, 2);
  return 1;
}
Ejemplo n.º 3
0
static int
luaB_setn(lua_State * L)
{
  luaL_checktype(L, 1, LUA_TTABLE);
  luaL_setn(L, 1, luaL_checkint(L, 2));
  return 0;
}
Ejemplo n.º 4
0
static int libE_setn (lua_State *L) {
  luaL_checktype(L, 1, LUA_TTABLE);
#ifndef luaL_setn
  luaL_setn(L, 1, luaL_checkint(L, 2));
#else
  lxs_error(L, LUA_QL("libE_setn") " is obsolete");
#endif
  lua_pushvalue(L, 1);
  return 1;
}
Ejemplo n.º 5
0
static int ICACHE_FLASH_ATTR setn (lua_State *L) {
  luaL_checktype(L, 1, LUA_TTABLE);
#ifndef luaL_setn
  luaL_setn(L, 1, luaL_checkint(L, 2));
#else
  luaL_error(L, LUA_QL("setn") " is obsolete");
#endif
  lua_pushvalue(L, 1);
  return 1;
}
Ejemplo n.º 6
0
static int tremove (lua_State *L) {
  int e = aux_getn(L, 1);
  int pos = luaL_optint(L, 2, e);
  if (e == 0) return 0;  /* table is `empty' */
  luaL_setn(L, 1, e - 1);  /* t.n = n-1 */
  lua_rawgeti(L, 1, pos);  /* result = t[pos] */
  for ( ;pos<e; pos++) {
    lua_rawgeti(L, 1, pos+1);
    lua_rawseti(L, 1, pos);  /* t[pos] = t[pos+1] */
  }
  lua_pushnil(L);
  lua_rawseti(L, 1, e);  /* t[e] = nil */
  return 1;
}
Ejemplo n.º 7
0
static int luaB_tremove (lua_State *L) {
  int n = aux_getn(L, 1);
  int pos = luaL_optint(L, 2, n);
  if (n <= 0) return 0;  /* table is `empty' */
  luaL_setn(L, 1, n-1);  /* t.n = n-1 */
  lua_rawgeti(L, 1, pos);  /* result = t[pos] */
  for ( ;pos<n; pos++) {
    lua_rawgeti(L, 1, pos+1);
    lua_rawseti(L, 1, pos);  /* t[pos] = t[pos+1] */
  }
  lua_pushnil(L);
  lua_rawseti(L, 1, n);  /* t[n] = nil */
  return 1;
}
Ejemplo n.º 8
0
static int libE_remove (lua_State *L) {
  int e = aux_getn(L, 1);
  int pos = luaL_optint(L, 2, e);
  if (!(1 <= pos && pos <= e))  /* position is outside bounds? */
   return 0;  /* nothing to remove */
  luaL_setn(L, 1, e - 1);  /* t.n = n-1 */
  lua_rawgeti(L, 1, pos);  /* result = t[pos] */
  for ( ;pos<e; pos++) {
    lua_rawgeti(L, 1, pos+1);
    lua_rawseti(L, 1, pos);  /* t[pos] = t[pos+1] */
  }
  lua_pushnil(L);
  lua_rawseti(L, 1, e);  /* t[e] = nil */
  return 1;
}
Ejemplo n.º 9
0
static int tinsert (lua_State *L) {
  int e = aux_getn(L, 1) + 1;  /* first empty element */
  int pos;  /* where to insert new element */
  if (lua_isnone(L, 3))  /* called with only 2 arguments */
    pos = e;  /* insert new element at the end */
  else {
    int i;
    pos = luaL_checkint(L, 2);  /* 2nd argument is the position */
    if (pos > e) e = pos;  /* `grow' array if necessary */
    lua_settop(L, 3);  /* function may be called with more than 3 args */
    for (i = e; i > pos; i--) {  /* move up elements */
      lua_rawgeti(L, 1, i-1);
      lua_rawseti(L, 1, i);  /* t[i] = t[i-1] */
    }
  }
  luaL_setn(L, 1, e);  /* new size */
  lua_rawseti(L, 1, pos);  /* t[pos] = v */
  return 0;
}
Ejemplo n.º 10
0
static int luaB_tinsert (lua_State *L) {
  int v = lua_gettop(L);  /* number of arguments */
  int n = aux_getn(L, 1) + 1;
  int pos;  /* where to insert new element */
  if (v == 2)  /* called with only 2 arguments */
    pos = n;  /* insert new element at the end */
  else {
    pos = luaL_checkint(L, 2);  /* 2nd argument is the position */
    if (pos > n) n = pos;  /* `grow' array if necessary */
    v = 3;  /* function may be called with more than 3 args */
  }
  luaL_setn(L, 1, n);  /* new size */
  while (--n >= pos) {  /* move up elements */
    lua_rawgeti(L, 1, n);
    lua_rawseti(L, 1, n+1);  /* t[n+1] = t[n] */
  }
  lua_pushvalue(L, v);
  lua_rawseti(L, 1, pos);  /* t[pos] = v */
  return 0;
}
Ejemplo n.º 11
0
/**	@name	listFiles
	@text	Lists the files contained in a directory
 
	@opt	string path		Path to search. Default is current directory.
	@out	table files		A table of filenames (or nil if the path is invalid)
*/
int MOAIFileSystem::_listFiles ( lua_State* L ) {
	UNUSED ( L );
	
	STLString oldPath = USFileSys::GetCurrentPath ();
	
	cc8* dir = NULL;
	if ( lua_type ( L, 1 ) == LUA_TSTRING ) {
		dir = lua_tostring ( L, 1 );
		if( !USFileSys::SetCurrentPath ( dir )) {
			return 0;
		}
	}

	USDirectoryItr dirItr;
	
	lua_newtable ( L );
	int n = 0;
	dirItr.Start ();
	while ( dirItr.NextFile ()) {
		if ( dir ) {
			lua_pushstring ( L, dir );
			lua_pushstring ( L, "/" );
			lua_pushstring ( L, dirItr.Current ());
			lua_concat ( L, 3 );
		}
		else {
			lua_pushstring ( L, dirItr.Current ());
		}

		n++;
		luaL_setn ( L, -2, n );  // new size
		lua_rawseti ( L, -2, n );  // t[pos] = v
	}
	
	USFileSys::SetCurrentPath ( oldPath );
	
	return 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;
}
Ejemplo n.º 13
0
static int t_remove (lua_State *L) {
  int e = aux_getn(L, 1);
  int pos = luaL_optint(L, 2, e);
  int last = luaL_optint(L, 3, pos);
  int i;
  if (last < pos) return 0;
  if (!(1 <= last && pos <= e))
   return 0;
  if (pos < 1) pos = 1;
  if (last > e) last = e;
  int n = last - pos + 1;
  luaL_setn(L, 1, e - n);
  for (i=0; i<n; i++)
    lua_rawgeti(L, 1, pos+i);
  for (last++; last<=e; pos++,last++) {
    lua_rawgeti(L, 1, last);
    lua_rawseti(L, 1, pos);
  }
  for( ; pos<=e; pos++) {
    lua_pushnil(L);
    lua_rawseti(L, 1, pos);
  }
  return n;
}
Ejemplo n.º 14
0
JNIEXPORT void JNICALL Java_m_lua_Lua_LsetN
		(JNIEnv* env, jobject thiz, jlong nativeObj, jint t, jint n) {
	pushJNIEnv(env, nativeObj);
	luaL_setn((lua_State*) nativeObj, t, n);
}