Example #1
0
int luaV_tostring (lua_State *L, StkId obj) {
  if (!ttisnumber(obj))
    return 0;
  else {
    char s[LUAI_MAXNUMBER2STR];
    lua_Number n = nvalue(obj);
    int l = lua_number2str(s, n);
    setsvalue2s(L, obj, luaS_newlstr(L, s, l));
    return 1;
  }
}
Example #2
0
LUA_API int lua_getglobal (lua_State *L, const char *name) {
  Table *reg = hvalue(&G(L)->l_registry);
  const TValue *gt;  /* global table */
  lua_lock(L);
  gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
  setsvalue2s(L, L->top, luaS_new(L, name));
  api_incr_top(L);
  luaV_gettable(L, gt, L->top - 1, L->top - 1);
  lua_unlock(L);
  return ttnov(L->top - 1);
}
Example #3
0
LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
  StkId t;
  lua_lock(L);
  api_checknelems(L, 1);
  t = index2addr(L, idx);
  api_checkvalidindex(L, t);
  setsvalue2s(L, L->top++, luaS_new(L, k));
  luaV_settable(L, t, L->top - 1, L->top - 2);
  L->top -= 2;  /* pop value and key */
  lua_unlock(L);
}
Example #4
0
LUA_API void lua_setglobal (lua_State *L, const char *var) {
  Table *reg = hvalue(&G(L)->l_registry);
  const TValue *gt;  /* global table */
  lua_lock(L);
  api_checknelems(L, 1);
  gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
  setsvalue2s(L, L->top++, luaS_new(L, var));
  luaV_settable(L, gt, L->top - 1, L->top - 2);
  L->top -= 2;  /* pop value and key */
  lua_unlock(L);
}
Example #5
0
void luaD_seterrorobj (lua_State *L, int errcode, StkId oldtop) {
  switch (errcode) {
    case LUA_ERRMEM: {
      ptrdiff_t oldtopr = savestack(L, oldtop);
      setsvalue2s(L, restorestack(L, oldtopr), luaS_newliteral(L, MEMERRMSG));
      break;
    }
    case LUA_ERRERR: {
      ptrdiff_t oldtopr = savestack(L, oldtop);
      setsvalue2s(L, restorestack(L, oldtopr), luaS_newliteral(L, "error in error handling"));
      break;
    }
    case LUA_ERRSYNTAX:
    case LUA_ERRRUN: {
      setobjs2s(L, oldtop, L->top - 1);  /* error message on current top */
      break;
    }
  }
  L->top = oldtop + 1;
}
Example #6
0
int ICACHE_FLASH_ATTR luaV_tostring (lua_State *L, StkId obj) {
  if (!ttisnumber(obj))
    return 0;
  else {
    char s[LUAI_MAXNUMBER2STR];
    lua_Number n = nvalue(obj);
    lua_number2str(s, n);
    setsvalue2s(L, obj, luaS_new(L, s));
    return 1;
  }
}
Example #7
0
/* this function handles only `%d', `%c', %f, %p, and `%s' formats */
const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
  int n = 0;
  for (;;) {
    const char *e = strchr(fmt, '%');
    if (e == NULL) break;
    setsvalue2s(L, L->top, luaS_newlstr(L, fmt, e-fmt));
    incr_top(L);
    switch (*(e+1)) {
      case 's': {
        const char *s = va_arg(argp, char *);
        if (s == NULL) s = "(null)";
        pushstr(L, s, strlen(s));
        break;
      }
      case 'c': {
        char buff;
        buff = cast(char, va_arg(argp, int));
        pushstr(L, &buff, 1);
        break;
      }
      case 'd': {
        setnvalue(L->top, cast_num(va_arg(argp, int)));
        incr_top(L);
        break;
      }
      case 'f': {
      	// Floats passed through ... are promoted to doubles...
        setnvalue(L->top, cast_num(va_arg(argp, double)));
        incr_top(L);
        break;
      }
      case 'p': {
        char buff[4*sizeof(void *) + 8]; /* should be enough space for a `%p' */
        int l = sprintf(buff, "%p", va_arg(argp, void *));
        pushstr(L, buff, l);
        break;
      }
      case '%': {
        pushstr(L, "%", 1);
        break;
      }
      default: {
        luaG_runerror(L,
            "invalid option " LUA_QL("%%%c") " to " LUA_QL("lua_pushfstring"),
            *(e + 1));
      }
    }
    n += 2;
    fmt = e+2;
  }
  pushstr(L, fmt, strlen(fmt));
  if (n > 0) luaV_concat(L, n + 1);
  return svalue(L->top - 1);
}
Example #8
0
int luaV_tostring (lua_State *L, StkId obj) {
  if (!ttisnumber(obj))
    return 0;
  else {
    char s[LUAI_MAXNUMBER2STR];
    ptrdiff_t objr = savestack(L, obj);
    lua_Number n = nvalue(obj);
    lua_number2str(s, n);
    setsvalue2s(L, restorestack(L, objr), luaS_new(L, s));
    return 1;
  }
}
Example #9
0
File: lvm.c Project: lriki/Volkoff
void luaV_concat (lua_State *L, int total) {
  lua_assert(total >= 2);
  do {
    StkId top = L->top;
    int n = 2;  /* number of elements handled in this pass (at least 2) */
    if (!(ttisstring(top-2) || ttisnumber(top-2)) || !tostring(L, top-1)) {
      if (!call_binTM(L, top-2, top-1, top-2, TM_CONCAT))
        luaG_concaterror(L, top-2, top-1);
    }
    else if (tsvalue(top-1)->len == 0)  /* second operand is empty? */
      (void)tostring(L, top - 2);  /* result is first operand */
    else if (ttisstring(top-2) && tsvalue(top-2)->len == 0) {
      setsvalue2s(L, top-2, rawtsvalue(top-1));  /* result is second op. */
    }
    else {
      /* at least two non-empty string values; get as many as possible */
      size_t tl = tsvalue(top-1)->len;
      char *buffer;
      int i;
      /* collect total length */
      for (i = 1; i < total && tostring(L, top-i-1); i++) {
        size_t l = tsvalue(top-i-1)->len;
        if (l >= (MAX_SIZET/sizeof(char)) - tl)
          luaG_runerror(L, "string length overflow");
        tl += l;
      }
      buffer = luaZ_openspace(L, &G(L)->buff, tl);
      tl = 0;
      n = i;
      do {  /* concat all strings */
        size_t l = tsvalue(top-i)->len;
        memcpy(buffer+tl, svalue(top-i), l * sizeof(char));
        tl += l;
      } while (--i > 0);
      setsvalue2s(L, top-n, luaS_newlstr(L, buffer, tl));
    }
    total -= n-1;  /* got 'n' strings to create 1 new */
    L->top -= n-1;  /* popped 'n' strings and pushed one */
  } while (total > 1);  /* repeat until only 1 result left */
}
Example #10
0
/*
** t[k] = value at the top of the stack (where 'k' is a string)
*/
static void auxsetstr (lua_State *L, const TValue *t, const char *k) {
  const TValue *slot;
  TString *str = luaS_new(L, k);
  api_checknelems(L, 1);
  if (luaV_fastset(L, t, str, slot, luaH_getstr, L->top - 1))
    L->top--;  /* pop value */
  else {
    setsvalue2s(L, L->top, str);  /* push 'str' (to make it a TValue) */
    api_incr_top(L);
    luaV_finishset(L, t, L->top - 1, L->top - 2, slot);
    L->top -= 2;  /* pop value and key */
  }
  lua_unlock(L);  /* lock done by caller */
}
Example #11
0
LUA_API const char *lua_pushstring (lua_State *L, const char *s) {
  lua_lock(L);
  if (s == NULL)
    setnilvalue(L->top);
  else {
    TString *ts;
    luaC_checkGC(L);
    ts = luaS_new(L, s);
    setsvalue2s(L, L->top, ts);
    s = getstr(ts);  /* internal copy's address */
  }
  api_incr_top(L);
  lua_unlock(L);
  return s;
}
Example #12
0
static int auxgetstr (lua_State *L, const TValue *t, const char *k) {
  const TValue *slot;
  TString *str = luaS_new(L, k);
  if (luaV_fastget(L, t, str, slot, luaH_getstr)) {
    setobj2s(L, L->top, slot);
    api_incr_top(L);
  }
  else {
    setsvalue2s(L, L->top, str);
    api_incr_top(L);
    luaV_finishget(L, t, L->top - 1, L->top - 1, slot);
  }
  lua_unlock(L);
  return ttnov(L->top - 1);
}
Example #13
0
LUA_API const char *lua_pushstring (lua_State *L, const char *s) {
  if (s == NULL) {
    lua_pushnil(L);
    return NULL;
  }
  else {
    TString *ts;
    lua_lock(L);
    luaC_checkGC(L);
    ts = luaS_new(L, s);
    setsvalue2s(L, L->top, ts);
    api_incr_top(L);
    lua_unlock(L);
    return getstr(ts);
  }
}
Example #14
0
File: Lapi.c Project: mniip/LUA
LUA_API const char *LUA_pushstring (LUA_State *L, const char *s) {
  if (s == NULL) {
    LUA_pushnil(L);
    return NULL;
  }
  else {
    TString *ts;
    LUA_lock(L);
    LUAC_checkGC(L);
    ts = LUAS_new(L, s);
    setsvalue2s(L, L->top, ts);
    api_incr_top(L);
    LUA_unlock(L);
    return getstr(ts);
  }
}
Example #15
0
int luaG_ordererror (lua_State *L, const TValue *p1, const TValue *p2) {
  //logt("luaG_ordererror");
  const char *t1 = luaT_typenames[ttype(p1)];
  const char *t2 = luaT_typenames[ttype(p2)];
#if 0
  // This does not cause a crash on Symbian.
  setsvalue2s(L, L->top, luaS_new(L, "dummy message"));
  incr_top(L);
  luaD_throw(L, LUA_ERRRUN);
#else
  if (t1[2] == t2[2])
    luaG_runerror_m(L, "attempt to compare two %s values", t1);
  else
    luaG_runerror_m(L, "attempt to compare %s with %s", t1, t2);
#endif
  return 0;
}
Example #16
0
void luaV_concat (lua_State *L, int total, int last) {
  lu_mem max_sizet = MAX_SIZET;
  if (G(L)->memlimit < max_sizet) max_sizet = G(L)->memlimit;
  do {
    /* Any call which does a memory allocation may trim the stack,
       invalidating top unless the stack is fixed duri  ng the allocation */ 
    StkId top = L->base + last + 1;
    fixedstack(L);
    int n = 2;  /* number of elements handled in this pass (at least 2) */
    if (!(ttisstring(top-2) || ttisnumber(top-2)) || !tostring(L, top-1)) {
      unfixedstack(L);
      if (!call_binTM(L, top-2, top-1, top-2, TM_CONCAT)) {
        /* restore 'top' pointer, since stack might have been reallocted */
        top = L->base + last + 1;
        luaG_concaterror(L, top-2, top-1);
      }
    } else if (tsvalue(top-1)->len == 0) { /* second op is empty? */
      (void)tostring(L, top - 2);  /* result is first op (as string) */
    } else {
      /* at least two string values; get as many as possible */
      size_t tl = tsvalue(top-1)->len;
      char *buffer;
      int i;
      /* collect total length */
      for (n = 1; n < total && tostring(L, top-n-1); n++) {
        size_t l = tsvalue(top-n-1)->len;
        if (l >= max_sizet - tl) luaG_runerror(L, "string length overflow");
        tl += l;
      }
      G(L)->buff.n = tl;
      buffer = luaZ_openspace(L, &G(L)->buff, tl);
      tl = 0;
      for (i=n; i>0; i--) {  /* concat all strings */
        size_t l = tsvalue(top-i)->len;
        c_memcpy(buffer+tl, svalue(top-i), l);
        tl += l;
      }
      setsvalue2s(L, top-n, luaS_newlstr(L, buffer, tl));
      luaZ_resetbuffer(&G(L)->buff);
    }
    total -= n-1;  /* got `n' strings to create 1 new */
    last -= n-1;
    unfixedstack(L);
  } while (total > 1);  /* repeat until only 1 result left */
}
Example #17
0
/*
** creates a new string and anchors it in scanner's table so that
** it will not be collected until the end of the compilation
** (by that time it should be anchored somewhere)
*/
TString *luaX_newstring (LexState *ls, const char *str, size_t l) {
  lua_State *L = ls->L;
  TValue *o;  /* entry for 'str' */
  TString *ts = luaS_newlstr(L, str, l);  /* create new string */
  setsvalue2s(L, L->top++, ts);  /* temporarily anchor it in stack */
  o = luaH_set(L, ls->h, L->top - 1);
  if (ttisnil(o)) {  /* not in use yet? */
    /* boolean value does not need GC barrier;
       table has no metatable, so it does not need to invalidate cache */
    setbvalue(o, 1);  /* t[string] = true */
    luaC_checkGC(L);
  }
  else {  /* string already present */
    ts = tsvalue(keyfromval(o));  /* re-use value previously stored */
  }
  L->top--;  /* remove string from stack */
  return ts;
}
Example #18
0
Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, const char *name) {
  struct LexState lexstate;
  struct FuncState funcstate;
  TString *tname = luaS_new(L, name);
  setsvalue2s(L, L->top, tname);  /* protect name */
  incr_top(L);
  lexstate.buff = buff;
  luaX_setinput(L, &lexstate, z, tname);
  open_func(&lexstate, &funcstate);
  funcstate.f->is_vararg = VARARG_ISVARARG;  /* main func. is always vararg */
  luaX_next(&lexstate);  /* read first token */
  chunk(&lexstate);
  check(&lexstate, TK_EOS);
  close_func(&lexstate);
  L->top--; /* remove 'name' from stack */
  lua_assert(funcstate.prev == NULL);
  lua_assert(funcstate.f->nups == 0);
  lua_assert(lexstate.fs == NULL);
  return funcstate.f;
}
Example #19
0
int luaV_tostring (lua_State *L, StkId obj) {
  if (!ttisnumber(obj))
    return 0;
  else {
    char buff[MAXNUMBER2STR];
    size_t len;
    if (ttisinteger(obj))
      len = lua_integer2str(buff, ivalue(obj));
    else {
      len = lua_number2str(buff, fltvalue(obj));
      if (strspn(buff, "-0123456789") == len) {  /* look like an integer? */
        buff[len++] = '.';  /* add a '.0' */
        buff[len++] = '0';
        buff[len] = '\0';
      }
    }
    setsvalue2s(L, obj, luaS_newlstr(L, buff, len));
    return 1;
  }
}
Example #20
0
void luaV_concat (lua_State *L, int total, int last)
{
    do
    {
        StkId top = L->base + last + 1;
        int n = 2;  /* number of elements handled in this pass (at least 2) */
        if (!(ttisstring(top-2) || ttisnumber(top-2)) || !tostring(L, top-1))
        {
            if (!call_binTM(L, top-2, top-1, top-2, TM_CONCAT))
                luaG_concaterror(L, top-2, top-1);
        }
        else if (tsvalue(top-1)->len == 0)    /* second op is empty? */
            (void)tostring(L, top - 2);  /* result is first op (as string) */
        else
        {
            /* at least two string values; get as many as possible */
            size_t tl = tsvalue(top-1)->len;
            char *buffer;
            int i;
            /* collect total length */
            for (n = 1; n < total && tostring(L, top-n-1); n++)
            {
                size_t l = tsvalue(top-n-1)->len;
                if (l >= MAX_SIZET - tl) luaG_runerror(L, "string length overflow");
                tl += l;
            }
            buffer = luaZ_openspace(L, &G(L)->buff, tl);
            tl = 0;
            for (i=n; i>0; i--)    /* concat all strings */
            {
                size_t l = tsvalue(top-i)->len;
                memcpy(buffer+tl, svalue(top-i), l);
                tl += l;
            }
            setsvalue2s(L, top-n, luaS_newlstr(L, buffer, tl));
        }
        total -= n-1;  /* got `n' strings to create 1 new */
        last -= n-1;
    }
    while (total > 1);    /* repeat until only 1 result left */
}
Example #21
0
/*
** Main operation for concatenation: concat 'total' values in the stack,
** from 'L->top - total' up to 'L->top - 1'.
*/
void luaV_concat (lua_State *L, int total) {
  lua_assert(total >= 2);
  do {
    StkId top = L->top;
    int n = 2;  /* number of elements handled in this pass (at least 2) */
    if (!(ttisstring(top-2) || cvt2str(top-2)) || !tostring(L, top-1))
      luaT_trybinTM(L, top-2, top-1, top-2, TM_CONCAT);
    else if (isemptystr(top - 1))  /* second operand is empty? */
      cast_void(tostring(L, top - 2));  /* result is first operand */
    else if (isemptystr(top - 2)) {  /* first operand is an empty string? */
      setobjs2s(L, top - 2, top - 1);  /* result is second op. */
    }
    else {
      /* at least two non-empty string values; get as many as possible */
      size_t tl = vslen(top - 1);
      TString *ts;
      /* collect total length and number of strings */
      for (n = 1; n < total && tostring(L, top - n - 1); n++) {
        size_t l = vslen(top - n - 1);
        if (l >= (MAX_SIZE/sizeof(char)) - tl)
          luaG_runerror(L, "string length overflow");
        tl += l;
      }
      if (tl <= LUAI_MAXSHORTLEN) {  /* is result a short string? */
        char buff[LUAI_MAXSHORTLEN];
        copy2buff(top, n, buff);  /* copy strings to buffer */
        ts = luaS_newlstr(L, buff, tl);
      }
      else {  /* long string; copy strings directly to final result */
        ts = luaS_createlngstrobj(L, tl);
        copy2buff(top, n, getstr(ts));
      }
      setsvalue2s(L, top - n, ts);  /* create result */
    }
    total -= n-1;  /* got 'n' strings to create 1 new */
    L->top -= n-1;  /* popped 'n' strings and pushed one */
  } while (total > 1);  /* repeat until only 1 result left */
}
Example #22
0
static void pushstr (lua_State *L, const char *str, size_t l) {
  setsvalue2s(L, L->top++, luaS_newlstr(L, str, l));
}
Example #23
0
static void pushstr (lua_State *L, const char *str) {
  setsvalue2s(L, L->top, luaS_new(L, str));
  incr_top(L);
}
Example #24
0
// It seems that on Symbian, merely invoking a function with varargs
// can mess up the stack so that an exception (or possibly some other
// kind of a return) will then cause a KERN-EXEC 3. Possibly we will
// have to avoid invoking this function on Symbian until we can find a
// fix. In any case, this is a known problem, see
// http://discussion.forum.nokia.com/forum/showthread.php?t=115248.
// Upgrading to GCCE 4 might possibly help?
void luaG_runerror_wa (lua_State *L, const char *s) {
  //logt("luaG_runerror_wa");
  setsvalue2s(L, L->top, luaS_new(L, s));
  incr_top(L);
  luaG_errormsg(L);
}
Example #25
0
/* this function handles only `%d', `%c', %f, %p, and `%s' formats */
const char* luaO_pushvfstring(lua_State* L, const char* fmt, va_list argp)
{
	int n = 1;
	pushstr(L, "");
	for (;;)
	{
		const char* e = strchr(fmt, '%');
		if (e == NULL) break;
		setsvalue2s(L, L->top, luaS_newlstr(L, fmt, e - fmt));
		incr_top(L);
		switch (*(e + 1))
		{
			case 's':
			{
				const char* s = va_arg(argp, char*);
				if (s == NULL) s = "(null)";
				pushstr(L, s);
				break;
			}
			case 'c':
			{
				char buff[2];
				buff[0] = cast(char, va_arg(argp, int));
				buff[1] = '\0';
				pushstr(L, buff);
				break;
			}
			case 'd':
			{
				setnvalue(L->top, cast_num(va_arg(argp, int)));
				incr_top(L);
				break;
			}
			case 'f':
			{
				setnvalue(L->top, cast_num(va_arg(argp, l_uacNumber)));
				incr_top(L);
				break;
			}
			case 'p':
			{
				char buff[4 * sizeof(void*) + 8]; /* should be enough space for a `%p' */
				sprintf(buff, "%p", va_arg(argp, void*));
				pushstr(L, buff);
				break;
			}
			case '%':
			{
				pushstr(L, "%");
				break;
			}
			default:
			{
				char buff[3];
				buff[0] = '%';
				buff[1] = *(e + 1);
				buff[2] = '\0';
				pushstr(L, buff);
				break;
			}
		}
		n += 2;
		fmt = e + 2;
	}
	pushstr(L, fmt);
	luaV_concat(L, n + 1, cast_int(L->top - L->base) - 1);
	L->top -= n;
	return svalue(L->top - 1);
}
Example #26
0
File: lvm.c Project: zapline/zlib
void luaV_concat (lua_State *L, int total, int last) {
  int useType = LUA_TSTRING;
  int i;
  StkId top = L->base + last + 1;

  for (i = 0; i < total; ++i)
  {
    if (ttype(top-1-i) == LUA_TSTRING || ttype(top-1-i) == LUA_TWSTRING)
    {
      useType = ttype(top-1-i);
      break;
    }
  }

  if (useType == LUA_TSTRING)
  {
    do {
      StkId top = L->base + last + 1;
      int n = 2;  /* number of elements handled in this pass (at least 2) */
      if (!tostring(L, top-2) || !tostring(L, top-1)) {
        if (!call_binTM(L, top-2, top-1, top-2, TM_CONCAT))
          luaG_concaterror(L, top-2, top-1);
      } else if (tsvalue(top-1)->len > 0) {  /* if len=0, do nothing */
        /* at least two string values; get as many as possible */
        size_t tl = tsvalue(top-1)->len;
        char *buffer;
        int i;
        /* collect total length */
        for (n = 1; n < total && tostring(L, top-n-1); n++) {
          size_t l = tsvalue(top-n-1)->len;
          if (l >= MAX_SIZET - tl) luaG_runerror(L, "string length overflow");
          tl += l;
        }
        buffer = luaZ_openspace(L, &G(L)->buff, tl);
        tl = 0;
        for (i=n; i>0; i--) {  /* concat all strings */
          size_t l = tsvalue(top-i)->len;
          memcpy(buffer+tl, svalue(top-i), l);
          tl += l;
#if LUA_REFCOUNT
          luarc_cleanvalue(top-i);
#endif /* LUA_REFCOUNT */
        }
        setsvalue2s(L, top-n, luaS_newlstr(L, buffer, tl));
      }
      total -= n-1;  /* got `n' strings to create 1 new */
      last -= n-1;
    } while (total > 1);  /* repeat until only 1 result left */
  } else {
    do {
      StkId top = L->base + last + 1;
      int n = 2;  /* number of elements handled in this pass (at least 2) */
      if (!towstring(L, top-2) || !towstring(L, top-1)) {
        if (!call_binTM(L, top-2, top-1, top-2, TM_CONCAT))
          luaG_concaterror(L, top-2, top-1);
      } else if (tsvalue(top-1)->len > 0) {  /* if len=0, do nothing */
        /* at least two string values; get as many as possible */
        size_t tl = tsvalue(top-1)->len;
        char *buffer;
        int i;
        /* collect total length */
        for (n = 1; n < total && towstring(L, top-n-1); n++) {
          size_t l = tsvalue(top-n-1)->len;
          if (l >= MAX_SIZET - tl) luaG_runerror(L, "string length overflow");
          tl += l;
        }
        buffer = luaZ_openspace(L, &G(L)->buff, tl*2);
        tl = 0;
        for (i=n; i>0; i--) {  /* concat all strings */
          size_t l = tsvalue(top-i)->len;
          memcpy(buffer+tl*2, wsvalue(top-i), l*2);
          tl += l;
#if LUA_REFCOUNT
          luarc_cleanvalue(top-i);
#endif /* LUA_REFCOUNT */
        }
        setwsvalue2s(L, top-n, luaS_newlwstr(L, (const lua_WChar*)buffer, tl));
      }
      total -= n-1;  /* got `n' strings to create 1 new */
      last -= n-1;
    } while (total > 1);  /* repeat until only 1 result left */
  }
}