Example #1
0
static int str_byte (lua_State *L) {
  size_t l;
  const char *s = luaL_check_lstr(L, 1, &l);
  long pos = posrelat(luaL_opt_long(L, 2, 1), l);
  luaL_arg_check(L, 0<pos && (size_t)pos<=l, 2,  "out of range");
  lua_pushnumber(L, (unsigned char)s[pos-1]);
  return 1;
}
Example #2
0
File: liolib.c Project: jeske/hz
static void setloc (void)
{
  static int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY, LC_NUMERIC,
                      LC_TIME};
  int op = (int)luaL_opt_number(2, 0);
  luaL_arg_check(0 <= op && op <= 5, 2, "invalid option");
  lua_pushstring(setlocale(cat[op], luaL_check_string(1)));
}
Example #3
0
static void str_byte (void)
{
  int32 l;
  const char *s = luaL_check_lstr(1, &l);
  int32 pos = posrelat((int32)luaL_opt_number(2, 1), l);
  luaL_arg_check(0<pos && pos<=l, 2,  "out of range");
  lua_pushnumber((byte)s[pos-1]);
}
Example #4
0
void* LuaBeans::check_tag(lua_State* L, int index)
{
  void* userdata = from_lua(L, index);

  luaL_arg_check(L, (userdata!=NULL), index, "Object type is wrong");

  return userdata;
}
Example #5
0
static int io_flush (lua_State *L) {
  IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1);
  FILE *f;
  lua_pop(L, 1);  /* remove upvalue */
  f = gethandle(L, ctrl, 1);
  luaL_arg_check(L, f || lua_isnull(L, 1), 1, "invalid file handle");
  return pushresult(L, fflush(f) == 0);
}
Example #6
0
static void setlocal (void) {
  lua_Object func = lua_stackedfunction(luaL_check_int(1));
  int numvar;
  luaL_arg_check(func != LUA_NOOBJECT, 1, "level out of range");
  numvar = findlocal(func, 2);
  lua_pushobject(luaL_nonnullarg(3));
  if (!lua_setlocal(func, numvar))
    lua_error("no such local variable");
}
Example #7
0
static void str_char (void) {
  int32 i = 0;
  luaL_resetbuffer();
  while (lua_getparam(++i) != LUA_NOOBJECT) {
    double c = luaL_check_number(i);
    luaL_arg_check((byte)c == c, i, "invalid value");
    luaL_addchar((int32)c);
  }
  closeandpush();
}
Example #8
0
static int setloc (lua_State *L) {
  static const int cat[] = {LC_ALL, LC_COLLATE, LC_CTYPE, LC_MONETARY,
                      LC_NUMERIC, LC_TIME};
  static const char *const catnames[] = {"all", "collate", "ctype", "monetary",
     "numeric", "time", NULL};
  int op = luaL_findstring(luaL_opt_string(L, 2, "all"), catnames);
  luaL_arg_check(L, op != -1, 2, "invalid option");
  lua_pushstring(L, setlocale(cat[op], luaL_check_string(L, 1)));
  return 1;
}
Example #9
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);
}
Example #10
0
Node *luaH_next(TObject *o, TObject *r) {
	Hash *t = avalue(o);
	if (ttype(r) == LUA_T_NIL)
		return hashnext(t, 0);
	else {
		int32 i = present(t, r);
		Node *n = node(t, i);
		luaL_arg_check(ttype(ref(n)) != LUA_T_NIL && ttype(val(n)) != LUA_T_NIL, 2, "key not found");
		return hashnext(t, i + 1);
	}
}
Example #11
0
/**
*  X-Or. Does a bit-a-bit exclusive-or of two strings.
*  @param s1: arbitrary binary string.
*  @param s2: arbitrary binary string with same length as s1.
*  @return  a binary string with same length as s1 and s2,
*   where each bit is the exclusive-or of the corresponding bits in s1-s2.
*/
static int ex_or (lua_State *L) {
  size_t l1, l2;
  const char *s1 = luaL_check_lstr(L, 1, &l1);
  const char *s2 = luaL_check_lstr(L, 2, &l2);
  luaL_Buffer b;
  luaL_arg_check( L, l1 == l2, 2, "lengths must be equal" );
  luaL_buffinit(L, &b);
  while (l1--) luaL_putchar(&b, (*s1++)^(*s2++));
  luaL_pushresult(&b);
  return 1;
}
Example #12
0
static void str_set (void)
{
  char *item = luaL_check_string(1);
  int i;
  luaL_arg_check(*luaL_item_end(item) == 0, 1, "wrong format");
  luaI_emptybuff();
  for (i=1; i<256; i++)  /* 0 cannot be part of a set */
    if (luaL_singlematch(i, item))
      luaI_addchar(i);
  lua_pushstring(luaI_addchar(0));
}
Example #13
0
static void luaB_nextvar (void) {
  TObject *o = luaA_Address(luaL_nonnullarg(1));
  TaggedString *g;
  if (ttype(o) == LUA_T_NIL)
    g = NULL;
  else {
    luaL_arg_check(ttype(o) == LUA_T_STRING, 1, "variable name expected");
    g = tsvalue(o);
  }
  if (!luaA_nextvar(g))
    lua_pushnil();
}
Example #14
0
static void nextvar() {
	TObject *o = luaA_Address(luaL_nonnullarg(1));
	TaggedString *g;
	if (ttype(o) == LUA_T_NIL)
		g = (TaggedString *)rootglobal.next;
	else {
		luaL_arg_check(ttype(o) == LUA_T_STRING, 1, "variable name expected");
		g = tsvalue(o);
		// check whether name is in global var list
		luaL_arg_check((GCnode *)g != g->head.next, 1, "variable name expected");
		g = (TaggedString *)g->head.next;
	}
	while (g && g->globalval.ttype == LUA_T_NIL) {
		// skip globals with nil
		g = (TaggedString *)g->head.next;
	}
	if (g) {
		pushstring(g);
		luaA_pushobject(&g->globalval);
	}
}
Example #15
0
void luaT_setfallback (void)
{
  static char *oldnames [] = {"error", "getglobal", "arith", "order", NULL};
  TObject oldfunc;
  lua_CFunction replace;
  char *name = luaL_check_string(1);
  lua_Object func = lua_getparam(2);
  luaL_arg_check(lua_isfunction(func), 2, "function expected");
  switch (luaL_findstring(name, oldnames)) {
    case 0:  /* old error fallback */
      oldfunc = L->errorim;
      L->errorim = *luaA_Address(func);
      replace = errorFB;
      break;
    case 1:  /* old getglobal fallback */
      oldfunc = *luaT_getim(LUA_T_NIL, IM_GETGLOBAL);
      *luaT_getim(LUA_T_NIL, IM_GETGLOBAL) = *luaA_Address(func);
      replace = nilFB;
      break;
    case 2: {  /* old arith fallback */
      int i;
      oldfunc = *luaT_getim(LUA_T_NUMBER, IM_POW);
      for (i=IM_ADD; i<=IM_UNM; i++)  /* ORDER IM */
        fillvalids(i, luaA_Address(func));
      replace = typeFB;
      break;
    }
    case 3: {  /* old order fallback */
      int i;
      oldfunc = *luaT_getim(LUA_T_NIL, IM_LT);
      for (i=IM_LT; i<=IM_GE; i++)  /* ORDER IM */
        fillvalids(i, luaA_Address(func));
      replace = typeFB;
      break;
    }
    default: {
      int e;
      if ((e = luaL_findstring(name, luaT_eventname)) >= 0) {
        oldfunc = *luaT_getim(LUA_T_NIL, e);
        fillvalids(e, luaA_Address(func));
        replace = (e == IM_GC || e == IM_INDEX) ? nilFB : typeFB;
      }
      else {
        luaL_verror("`%.50s' is not a valid fallback name", name);
        replace = NULL;  /* to avoid warnings */
      }
    }
  }
  if (oldfunc.ttype != LUA_T_NIL)
    luaA_pushobject(&oldfunc);
  else
    lua_pushcfunction(replace);
}
Example #16
0
static int luaB_settagmethod (lua_State *L) {
  int tag = luaL_check_int(L, 1);
  const char *event = luaL_check_string(L, 2);
  luaL_arg_check(L, lua_isfunction(L, 3) || lua_isnil(L, 3), 3,
                 "function or nil expected");
  if (strcmp(event, "gc") == 0)
    lua_error(L, "deprecated use: cannot set the `gc' tag method from Lua");
  lua_gettagmethod(L, tag, event);
  lua_pushvalue(L, 3);
  lua_settagmethod(L, tag, event);
  return 1;
}
Example #17
0
static Ihandle* checkihandle_table(lua_Object tab, int index)
{
  Ihandle* ih;
  lua_Object o;
  lua_beginblock();
  lua_pushobject(tab);
  lua_pushnumber(index);
  o = lua_gettable();
  luaL_arg_check(lua_tag(o)==iuplua_tag, index, "handle expected");
  ih = (Ihandle*)lua_getuserdata(o);
  lua_endblock();
  return ih;
}
Example #18
0
static int str_char (lua_State *L) {
    int n = lua_gettop(L);  /* number of arguments */
    int i;
    luaL_Buffer b;
    luaL_buffinit(L, &b);
    for (i=1; i<=n; i++) {
        int c = luaL_check_int(L, i);
        luaL_arg_check(L, (unsigned char)c == c, i, "invalid value");
        luaL_putchar(&b, (unsigned char)c);
    }
    luaL_pushresult(&b);
    return 1;
}
Example #19
0
static int f_seek (lua_State *L) {
  static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END};
  static const char *const modenames[] = {"set", "cur", "end", NULL};
  FILE *f = tofile(L, 1);
  int op = luaL_findstring(luaL_opt_string(L, 2, "cur"), modenames);
  long offset = luaL_opt_long(L, 3, 0);
  luaL_arg_check(L, op != -1, 2, "invalid mode");
  op = fseek(f, offset, mode[op]);
  if (op)
    return pushresult(L, 0);  /* error */
  else {
    lua_pushnumber(L, ftell(f));
    return 1;
  }
}
Example #20
0
/**
*  Decrypts a string. For any message, key, and seed, we have that
*  <code>decrypt(crypt(msg, key, seed), key) == msg</code>.
*  @param cyphertext: message to be decrypted (this must be the result of
   a previous call to <code>crypt</code>.
*  @param key: arbitrary binary string to be used as a key.
*  @return  The plaintext.
*/
static int decrypt (lua_State *L) {
  size_t lcyphertext;
  const char *cyphertext = luaL_check_lstr(L, 1, &lcyphertext);
  size_t lseed = cyphertext[0];
  const char *seed = cyphertext+1;
  int lblock;
  char block[BLOCKSIZE+MAXKEY];
  luaL_arg_check(L, lcyphertext >= lseed+1 && lseed <= BLOCKSIZE, 1,
                 "invalid cyphered string");
  cyphertext += lseed+1;
  lcyphertext -= lseed+1;
  lblock = initblock(L, seed, lseed, block);
  decodestream(L, cyphertext, lcyphertext, block, lblock);
  return 1;
}
Example #21
0
static void math_random (void) {
  /* the '%' avoids the (rare) case of r==1, and is needed also because on
     some systems (SunOS!) "rand()" may return a value bigger than RAND_MAX */
  double r = (double)(rand()%RAND_MAX) / (double)RAND_MAX;
  int l = luaL_opt_int(1, 0);
  if (l == 0)
    lua_pushnumber(r);
  else {
    int u = luaL_opt_int(2, 0);
    if (u == 0) {
      u = l;
      l = 1;
    }
    luaL_arg_check(l<=u, 1, "interval is empty");
    lua_pushnumber((int)(r*(u-l+1))+l);
  }
}
Example #22
0
TaggedString *luaA_nextvar (TaggedString *g) {
  if (g == NULL)
    g = (TaggedString *)L->rootglobal.next;  /* first variable */
  else {
    /* check whether name is in global var list */
    luaL_arg_check((GCnode *)g != g->head.next, 1, "variable name expected");
    g = (TaggedString *)g->head.next;  /* get next */
  }
  while (g && g->u.s.globalval.ttype == LUA_T_NIL)  /* skip globals with nil */
    g = (TaggedString *)g->head.next;
  if (g) {
    ttype(L->stack.top) = LUA_T_STRING; tsvalue(L->stack.top) = g;
    incr_top;
    luaA_pushobject(&g->u.s.globalval);
  }
  return g;
}
Example #23
0
static void luaB_tonumber (void) {
  int base = luaL_opt_int(2, 10);
  if (base == 10) {  /* standard conversion */
    lua_Object o = lua_getparam(1);
    if (lua_isnumber(o)) lua_pushnumber(lua_getnumber(o));
    else lua_pushnil();  /* not a number */
  }
  else {
    char *s = luaL_check_string(1);
    long n;
    luaL_arg_check(0 <= base && base <= 36, 2, "base out of range");
    n = strtol(s, &s, base);
    while (isspace((unsigned char)*s)) s++;  /* skip trailing spaces */
    if (*s) lua_pushnil();  /* invalid format: return nil */
    else lua_pushnumber(n);
  }
}
Example #24
0
static void profile_start(lua_State *L) {
    if (PROFILE_INIT)
        return; // already started.
    lua_Object lobj = lua_getparam(L, 1);
    if (lobj > 0) {
        luaL_arg_check(L, lua_isnumber(L, lobj), 1,
                       "Inform the minimum time results in float");
        PROFILE_RECORD_TIME = (float) lua_getnumber(L, lobj);
    }
    Meta **meta = (Meta **) malloc(MEM_BLOCKSIZE * sizeof(Meta **));

    lua_pushuserdata(L, meta);
    META_REF = lua_ref(L, 1);

    lua_setcallhook(L, callhook);
    PROFILE_START_TIME = clock();
    PROFILE_INIT = true;
}
Example #25
0
static void tonumber() {
	int32 base = (int32)luaL_opt_number(2, 10);
	if (base == 10) {  // standard conversion
		lua_Object o = lua_getparam(1);
		if (lua_isnumber(o))
			lua_pushnumber(lua_getnumber(o));
	} else {
		const char *s = luaL_check_string(1);
		char *e;
		int32 n;
		luaL_arg_check(0 <= base && base <= 36, 2, "base out of range");
		n = (int32)strtol(s, &e, base);
		while (Common::isSpace(*e))
			e++; // skip trailing spaces
		if (*e)
			return; // invalid format: return nil
		lua_pushnumber(n);
	}
}
Example #26
0
static int io_seek (lua_State *L) {
  static const int mode[] = {SEEK_SET, SEEK_CUR, SEEK_END};
  static const char *const modenames[] = {"set", "cur", "end", NULL};
  IOCtrl *ctrl = (IOCtrl *)lua_touserdata(L, -1);
  FILE *f;
  int op;
  long offset;
  lua_pop(L, 1);  /* remove upvalue */
  f = getnonullfile(L, ctrl, 1);
  op = luaL_findstring(luaL_opt_string(L, 2, "cur"), modenames);
  offset = luaL_opt_long(L, 3, 0);
  luaL_arg_check(L, op != -1, 2, "invalid mode");
  op = fseek(f, offset, mode[op]);
  if (op)
    return pushresult(L, 0);  /* error */
  else {
    lua_pushnumber(L, ftell(f));
    return 1;
  }
}
Example #27
0
static void luaB_print (void) {
  lua_Object args[MAXPRINT];
  lua_Object obj;
  int n = 0;
  int i;
  while ((obj = lua_getparam(n+1)) != LUA_NOOBJECT) {
    luaL_arg_check(n < MAXPRINT, n+1, "too many arguments");
    args[n++] = obj;
  }
  for (i=0; i<n; i++) {
    lua_pushobject(args[i]);
    if (lua_call("tostring"))
      lua_error("error in `tostring' called by `print'");
    obj = lua_getresult(1);
    if (!lua_isstring(obj))
      lua_error("`tostring' must return a string to `print'");
    if (i>0) fputs("\t", stdout);
    fputs(lua_getstring(obj), stdout);
  }
  fputs("\n", stdout);
}
Example #28
0
static int g_iofile (lua_State *L, const char *name, const char *mode) {
  if (lua_isnoneornil(L, 1)) {
    lua_pushstring(L, name);
    lua_rawget(L, lua_upvalueindex(1));
    return 1;
  }
  else {
    const char *filename = lua_tostring(L, 1);
    lua_pushstring(L, name);
    if (filename) {
      FILE *f = fopen(filename, mode);
      luaL_arg_check(L, f, 1,  strerror(errno));
      newfile(L, f);
    }
    else {
      tofile(L, 1);  /* check that it's a valid file handle */
      lua_pushvalue(L, 1);
    }
    lua_rawset(L, lua_upvalueindex(1));
    return 0;
  }
}
Example #29
0
static void add_s (lua_Object newp, lua_Object table, int n)
{
  if (lua_isstring(newp)) {
    char *news = lua_getstring(newp);
    while (*news) {
      if (*news != ESC || !isdigit((unsigned char)*++news))
        luaI_addchar(*news++);
      else {
        int l = check_cap(*news++, num_captures);
        addnchar(capture[l].init, capture[l].len);
      }
    }
  }
  else if (lua_isfunction(newp)) {
    lua_Object res;
    struct lbuff oldbuff;
    int status;
    lua_beginblock();
    if (lua_istable(table)) {
      lua_pushobject(table);
      lua_pushnumber(n);
    }
    push_captures();
    /* function may use lbuffer, so save it and create a new one */
    oldbuff = lbuffer;
    lbuffer.b = NULL; lbuffer.max = lbuffer.size = 0;
    status = lua_callfunction(newp);
    /* restore old buffer */
    free(lbuffer.b);
    lbuffer = oldbuff;
    if (status != 0)
      lua_error(NULL);
    res = lua_getresult(1);
    addstr(lua_isstring(res) ? lua_getstring(res) : "");
    lua_endblock();
  }
  else luaL_arg_check(0, 3, NULL);
}
Example #30
0
static FILE *getnonullfile (lua_State *L, IOCtrl *ctrl, int arg) {
  FILE *f = gethandle(L, ctrl, arg);
  luaL_arg_check(L, f, arg, "invalid file handle");
  return f;
}