예제 #1
0
static void luaB_call (void) {
  lua_Object f = luaL_nonnullarg(1);
  Hash *arg = gethash(2);
  char *options = luaL_opt_string(3, "");
  lua_Object err = lua_getparam(4);
  int narg = (int)getnarg(arg);
  int i, status;
  if (err != LUA_NOOBJECT) {  /* set new error method */
    lua_pushobject(err);
    err = lua_seterrormethod();
  }
  /* push arg[1...n] */
  luaD_checkstack(narg);
  for (i=0; i<narg; i++)
    *(L->stack.top++) = *luaH_getint(arg, i+1);
  status = lua_callfunction(f);
  if (err != LUA_NOOBJECT) {  /* restore old error method */
    lua_pushobject(err);
    lua_seterrormethod();
  }
  if (status != 0) {  /* error in call? */
    if (strchr(options, 'x')) {
      lua_pushnil();
      return;  /* return nil to signal the error */
    }
    else
      lua_error(NULL);
  }
  else {  /* no errors */
    if (strchr(options, 'p'))
      luaA_packresults();
    else
      luaA_passresults();
  }
}
예제 #2
0
static void luaB_sort (void) {
  lua_Object t = lua_getparam(1);
  Hash *a = gethash(1);
  int n = (int)getnarg(a);
  lua_Object func = lua_getparam(2);
  luaL_arg_check(func == LUA_NOOBJECT || lua_isfunction(func), 2,
                 "function expected");
  luaD_checkstack(4);  /* for Pivot, f, a, b (sort_comp) */
  auxsort(a, 1, n, func);
  lua_pushobject(t);
}
예제 #3
0
static void luaB_tremove (void) {
  Hash *a = gethash(1);
  int n = (int)getnarg(a);
  int pos = luaL_opt_int(2, n);
  if (n <= 0) return;  /* table is "empty" */
  luaA_pushobject(luaH_getint(a, pos));  /* result = a[pos] */
  for ( ;pos<n; pos++)
    luaH_move(a, pos+1, pos);  /* a[pos] = a[pos+1] */
  luaV_setn(a, n-1);  /* a.n = n-1 */
  luaH_setint(a, n, &luaO_nilobject);  /* a[n] = nil */
}
예제 #4
0
static void luaB_tinsert (void) {
  Hash *a = gethash(1);
  lua_Object v = lua_getparam(3);
  int n = (int)getnarg(a);
  int pos;
  if (v != LUA_NOOBJECT)
    pos = luaL_check_int(2);
  else {  /* called with only 2 arguments */
    v = luaL_nonnullarg(2);
    pos = n+1;
  }
  luaV_setn(a, n+1);  /* a.n = n+1 */
  for ( ;n>=pos; n--)
    luaH_move(a, n, n+1);  /* a[n+1] = a[n] */
  luaH_setint(a, pos, luaA_Address(v));  /* a[pos] = v */
}
예제 #5
0
static void luaB_foreachi (void) {
  Hash *t = gethash(1);
  int i;
  int n = (int)getnarg(t);
  TObject f;
  /* 'f' cannot be a pointer to TObject, because it is on the stack, and the
     stack may be reallocated by the call. Moreover, some C compilers do not
     initialize structs, so we must do the assignment after the declaration */
  f = *luaA_Address(luaL_functionarg(2));
  luaD_checkstack(3);  /* for f, ref, and val */
  for (i=1; i<=n; i++) {
    *(L->stack.top++) = f;
    ttype(L->stack.top) = LUA_T_NUMBER; nvalue(L->stack.top++) = i;
    *(L->stack.top++) = *luaH_getint(t, i);
    luaD_calln(2, 1);
    if (ttype(L->stack.top-1) != LUA_T_NIL)
      return;
    L->stack.top--;
  }
}
예제 #6
0
static void luaI_call (void)
{
  lua_Object f = luaL_nonnullarg(1);
  lua_Object arg = luaL_tablearg(2);
  const char *options = luaL_opt_string(3, "");
  lua_Object err = lua_getparam(4);
  int32 narg = getnarg(arg);
  int32 i, status;
  if (err != LUA_NOOBJECT) {  /* set new error method */
    lua_pushobject(err);
    err = lua_seterrormethod();
  }
  /* push arg[1...n] */
  for (i=0; i<narg; i++) {
    lua_Object temp;
    /* temp = arg[i+1] */
    lua_pushobject(arg); lua_pushnumber(i+1); temp = lua_rawgettable();
    if (narg == MAX_INT && lua_isnil(temp))
      break;
    lua_pushobject(temp);
  }
  status = lua_callfunction(f);
  if (err != LUA_NOOBJECT) {  /* restore old error method */
    lua_pushobject(err);
    lua_seterrormethod();
  }
  if (status != 0) {  /* error in call? */
    if (strchr(options, 'x')) {
      lua_pushnil();
      return;  /* return nil to signal the error */
    }
    else
      lua_error(NULL);
  }
  else { /* no errors */
    if (strchr(options, 'p'))
      luaA_packresults();
    else
      luaA_passresults();
  }
}
예제 #7
0
static void luaB_getn (void) {
  lua_pushnumber(getnarg(gethash(1)));
}