Beispiel #1
0
static int tinsert (lv_State *L) {
    lv_clearFirstTableValue(L);
    int e = aux_getn(L, 1) + 1;  /* first empty element */
    int pos;  /* where to insert new element */
    switch (lv_gettop(L)) {
    case 2: {  /* called with only 2 arguments */
        pos = e;  /* insert new element at the end */
        break;
    }
    case 3: {
        int i;
        pos = lvL_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 */
            lv_rawgeti(L, 1, i-1);
            lv_rawseti(L, 1, i);  /* t[i] = t[i-1] */
        }
        break;
    }
    default: {
        return lvL_error(L, "wrong number of arguments to " LV_QL("insert"));
    }
    }
    lvL_setn(L, 1, e);  /* new size */
    lv_rawseti(L, 1, pos);  /* t[pos] = v */
    return 0;
}
Beispiel #2
0
static void addfield (lv_State *L, lvL_Buffer *b, int i) {
    lv_rawgeti(L, 1, i);
    if (!lv_isstring(L, -1))
        lvL_error(L, "invalid value (%s) at index %d in table for "
                  LV_QL("concat"), lvL_typename(L, -1), i);
    lvL_addvalue(b);
}
Beispiel #3
0
static int db_setfenv (lv_State *L) {
    lvL_checktype(L, 2, LV_TTABLE);
    lv_settop(L, 2);
    if (lv_setfenv(L, 1) == 0)
        lvL_error(L, LV_QL("setfenv")
                  " cannot change environment of given object");
    return 1;
}
Beispiel #4
0
static int setn (lv_State *L) {
    lv_clearFirstTableValue(L);
    lvL_checktype(L, 1, LV_TTABLE);
#ifndef lvL_setn
    lvL_setn(L, 1, lvL_checkint(L, 2));
#else
    lvL_error(L, LV_QL("setn") " is obsolete");
#endif
    lv_pushvalue(L, 1);
    return 1;
}
Beispiel #5
0
static int doargs(int argc, char* argv[])
{
 int i;
 int version=0;
 if (argv[0]!=NULL && *argv[0]!=0) progname=argv[0];
 for (i=1; i<argc; i++)
 {
  if (*argv[i]!='-')			/* end of options; keep it */
   break;
  else if (IS("--"))			/* end of options; skip it */
  {
   ++i;
   if (version) ++version;
   break;
  }
  else if (IS("-"))			/* end of options; use stdin */
   break;
  else if (IS("-l"))			/* list */
   ++listing;
  else if (IS("-o"))			/* output file */
  {
   output=argv[++i];
   if (output==NULL || *output==0) usage(LV_QL("-o") " needs argument");
   if (IS("-")) output=NULL;
  }
  else if (IS("-p"))			/* parse only */
   dumping=0;
  else if (IS("-s"))			/* strip debug information */
   stripping=1;
  else if (IS("-v"))			/* show version */
   ++version;
  else					/* unknown option */
   usage(argv[i]);
 }
 if (i==argc && (listing || !dumping))
 {
  dumping=0;
  argv[--i]=Output;
 }
 if (version)
 {
  printf("%s  %s\n",LV_RELEASE,LV_COPYRIGHT);
  if (version==argc-1) exit(EXIT_SUCCESS);
 }
 return i;
}
Beispiel #6
0
static void usage(const char* message)
{
 if (*message=='-')
  fprintf(stderr,"%s: unrecognized option " LV_QS "\n",progname,message);
 else
  fprintf(stderr,"%s: %s\n",progname,message);
 fprintf(stderr,
 "usage: %s [options] [filenames].\n"
 "Available options are:\n"
 "  -        process stdin\n"
 "  -l       list\n"
 "  -o name  output to file " LV_QL("name") " (default is \"%s\")\n"
 "  -p       parse only\n"
 "  -s       strip debug information\n"
 "  -v       show version information\n"
 "  --       stop handling options\n",
 progname,Output);
 exit(EXIT_FAILURE);
}
Beispiel #7
0
/*
** returns the index of a `key' for table traversals. First goes all
** elements in the array part, then elements in the hash part. The
** beginning of a traversal is signalled by -1.
*/
static int findindex (lv_State *L, Table *t, StkId key) {
    int i;
    if (ttisnil(key)) return -1;  /* first iteration */
    i = arrayindex(key);
    if (0 < i && i <= t->sizearray)  /* is `key' inside array part? */
        return i-1;  /* yes; that's the index (corrected to C) */
    else {
        Node *n = mainposition(t, key);
        do {  /* check whether `key' is somewhere in the chain */
            /* key may be dead already, but it is ok to use it in `next' */
            if (lvO_rawequalObj(key2tval(n), key) ||
                    (ttype(gkey(n)) == LV_TDEADKEY && iscollectable(key) &&
                     gcvalue(gkey(n)) == gcvalue(key))) {
                i = cast_int(n - gnode(t, 0));  /* key index in hash table */
                /* hash elements are numbered after array ones */
                return i + t->sizearray;
            }
            else n = gnext(n);
        } while (n);
        lvG_runerror(L, "invalid key to " LV_QL("next"));  /* key not found */
        return 0;  /* to avoid warnings */
    }
}