Esempio n. 1
0
LUA_API int lua_getn (lua_State *L, int index) {
  StkId t;
  const TObject *value;
  int n;
  lua_lock(L);
  t = luaA_index(L, index);
  api_check(L, ttype(t) == LUA_TTABLE);
  value = luaH_getstr(hvalue(t), luaS_newliteral(L, "n"));  /* = t.n */
  if (ttype(value) == LUA_TNUMBER)
    lua_number2int(n, nvalue(value));
  else {
    Node *nd;
    Table *a = hvalue(t);
    lua_Number max = 0;
    int i;
    i = a->sizearray;
    while (i--) {
      if (ttype(&a->array[i]) != LUA_TNIL)
        break;
    }
    max = i+1;
    i = sizenode(a);
    nd = a->node;
    while (i--) {
      if (ttype(gkey(nd)) == LUA_TNUMBER &&
          ttype(gval(nd)) != LUA_TNIL &&
          nvalue(gkey(nd)) > max)
        max = nvalue(gkey(nd));
      nd++;
    }
    lua_number2int(n, max);
  }
  lua_unlock(L);
  return n;
}
Esempio n. 2
0
static int addk (FuncState *fs, TValue *key, TValue *v) {
  lua_State *L = fs->ls->L;
  TValue *idx = luaH_set(L, fs->h, key);
  Proto *f = fs->f;
  int k, oldsize;
  if (ttisnumber(idx)) {
    lua_Number n = nvalue(idx);
    lua_number2int(k, n);
    if (luaV_rawequalobj(&f->k[k], v))
      return k;
    /* else may be a collision (e.g., between 0.0 and "\0\0\0\0\0\0\0\0");
       go through and create a new entry for this value */
  }
  /* constant not found; create a new entry */
  oldsize = f->sizek;
  k = fs->nk;
  /* numerical value does not need GC barrier;
     table has no metatable, so it does not need to invalidate cache */
  setnvalue(idx, cast_num(k));
  luaM_growvector(L, f->k, k, f->sizek, TValue, MAXARG_Ax, "constants");
  while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]);
  setobj(L, &f->k[k], v);
  fs->nk++;
  luaC_barrier(L, f, v);
  return k;
}
Esempio n. 3
0
int main(void)
{
	int a = 0;
	double b = 1.2;
	lua_number2int(a, b);
	printf("%d\n", a);
	return 0;
}
Esempio n. 4
0
/*
** returns the index for `key' if `key' is an appropriate key to live in
** the array part of the table, -1 otherwise.
*/
static int arrayindex (const TValue *key) {
  if (ttisnumber(key)) {
    lua_Number n = nvalue(key);
    int k;
    lua_number2int(k, n);
    if (luai_numeq(cast_num(k), n))
      return k;
  }
  return -1;  /* `key' did not match some condition */
}
Esempio n. 5
0
static int f_setvbuf(lua_State * L)
{
    int sz, op, res;
    static const int mode[] = { _IONBF, _IOFBF, _IOLBF };
    static const char *const modenames[] = { "no", "full", "line", NULL };
    FILE *f = tofile(L);
    op = luaL_checkoption(L, 2, NULL, modenames);
    lua_number2int(sz, luaL_optinteger(L, 3, LUAL_BUFFERSIZE));
    res = setvbuf(f, NULL, mode[op], (size_t) sz);
    return pushresult(L, res == 0, NULL);
}
Esempio n. 6
0
File: rng.c Progetto: LuaDist/numlua
static int seed_rng (lua_State *L) {
  nl_RNG *r = getrng(L);
  if (lua_isnoneornil(L, 1))
    init_genrand(r, RNG_SEED);
  else if (lua_isnumber(L, 1)) /* seed? */
    init_genrand(r, lua_tointeger(L, 1));
  else { /* vector */
    unsigned long initkey[RNG_MAXSTATES];
    int i, k;
    lua_Number *e;
    nl_Matrix *m = nl_checkmatrix(L, 1);
    checkrvector(L, m, 1);
    for (i = 0, e = m->data; i < m->size; i++, e += m->stride) {
      lua_number2int(k, *e);
      initkey[i] = (unsigned long) k;
    }
    init_by_array(r, initkey, m->size);
  }
  return 0;
}
Esempio n. 7
0
static int cdf_qpois (lua_State *L) {
  /* stack should contain p and xlam */
  lua_Number p = luaL_checknumber(L, 1);
  lua_Number xlam = luaL_checknumber(L, 2);
  int si = 0;
  check_pois(L, 2, p, xlam);
  if (p==1) {
    lua_pushnumber(L, HUGE_VAL);
    return 1;
  }
  if (p>0) {
    lua_Number q = 1-p;
    lua_Number s, bound;
    int which = 2;
    int status;
    cdfpoi(&which, &p, &q, &s, &xlam, &status, &bound);
    check_status(status, bound);
    lua_number2int(si, s);
  }
  lua_pushinteger(L, si);
  return 1;
}
Esempio n. 8
0
static int cdf_qbinom (lua_State *L) {
  /* stack should contain p, xn, pr */
  lua_Number p = luaL_checknumber(L, 1);
  lua_Number xn = luaL_checknumber(L, 2);
  lua_Number pr = luaL_checknumber(L, 3);
  lua_Number s;
  int si;
  check_binom(L, 2, p, xn, pr);
  if (p==0 || p==1) s = p*xn;
  else {
    lua_Number q = 1-p;
    lua_Number ompr = 1-pr;
    lua_Number bound;
    int which = 2;
    int status;
    cdfbin(&which, &p, &q, &s, &xn, &pr, &ompr, &status, &bound);
    check_status(status, bound);
  }
  lua_number2int(si, s);
  lua_pushinteger(L, si);
  return 1;
}
Esempio n. 9
0
static int cdf_qnbinom (lua_State *L) {
  /* stack should contain p, xn, pr */
  lua_Number p = luaL_checknumber(L, 1);
  lua_Number xn = luaL_checknumber(L, 2);
  lua_Number pr = luaL_checknumber(L, 3);
  int si = 0;
  check_nbinom(L, 2, p, xn, pr);
  if (p==1) {
    lua_pushnumber(L, HUGE_VAL);
    return 1;
  }
  if (p>0) {
    lua_Number q = 1-p;
    lua_Number ompr = 1-pr;
    lua_Number s, bound;
    int which = 2;
    int status;
    cdfnbn(&which, &p, &q, &s, &xn, &pr, &ompr, &status, &bound);
    check_status(status, bound);
    lua_number2int(si, s);
  }
  lua_pushinteger(L, si);
  return 1;
}