LCOV - code coverage report
Current view: directory - src - lobject.c Found Hit Coverage
Test: Lua 5.1.4 Lines: 102 89 87.3 %
Date: 2009-09-13
Colors: not hit hit

       1                 : /*
       2                 : ** $Id: lobject.c,v 2.22.1.1 2007/12/27 13:02:25 roberto Exp $
       3                 : ** Some generic functions over Lua objects
       4                 : ** See Copyright Notice in lua.h
       5                 : */
       6                 : 
       7                 : #include <ctype.h>
       8                 : #include <stdarg.h>
       9                 : #include <stdio.h>
      10                 : #include <stdlib.h>
      11                 : #include <string.h>
      12                 : 
      13                 : #define lobject_c
      14                 : #define LUA_CORE
      15                 : 
      16                 : #include "lua.h"
      17                 : 
      18                 : #include "ldo.h"
      19                 : #include "lmem.h"
      20                 : #include "lobject.h"
      21                 : #include "lstate.h"
      22                 : #include "lstring.h"
      23                 : #include "lvm.h"
      24                 : 
      25                 : 
      26                 : 
      27                 : const TValue luaO_nilobject_ = {{NULL}, LUA_TNIL};
      28                 : 
      29                 : 
      30                 : /*
      31                 : ** converts an integer to a "floating point byte", represented as
      32                 : ** (eeeeexxx), where the real value is (1xxx) * 2^(eeeee - 1) if
      33                 : ** eeeee != 0 and (xxx) otherwise.
      34                 : */
      35            1092 : int luaO_int2fb (unsigned int x) {
      36            1092 :   int e = 0;  /* expoent */
      37            2184 :   while (x >= 16) {
      38               0 :     x = (x+1) >> 1;
      39               0 :     e++;
      40                 :   }
      41            1092 :   if (x < 8) return x;
      42               3 :   else return ((e+1) << 3) | (cast_int(x) - 8);
      43                 : }
      44                 : 
      45                 : 
      46                 : /* converts back */
      47           13018 : int luaO_fb2int (int x) {
      48           13018 :   int e = (x >> 3) & 31;
      49           13018 :   if (e == 0) return x;
      50               3 :   else return ((x & 7)+8) << (e - 1);
      51                 : }
      52                 : 
      53                 : 
      54           34593 : int luaO_log2 (unsigned int x) {
      55                 :   static const lu_byte log_2[256] = {
      56                 :     0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
      57                 :     6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
      58                 :     7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
      59                 :     7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
      60                 :     8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
      61                 :     8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
      62                 :     8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
      63                 :     8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8
      64                 :   };
      65           34593 :   int l = -1;
      66           34593 :   while (x >= 256) { l += 8; x >>= 8; }
      67           34593 :   return l + log_2[x];
      68                 : 
      69                 : }
      70                 : 
      71                 : 
      72            1579 : int luaO_rawequalObj (const TValue *t1, const TValue *t2) {
      73            1579 :   if (ttype(t1) != ttype(t2)) return 0;
      74            1292 :   else switch (ttype(t1)) {
      75                 :     case LUA_TNIL:
      76               1 :       return 1;
      77                 :     case LUA_TNUMBER:
      78              31 :       return luai_numeq(nvalue(t1), nvalue(t2));
      79                 :     case LUA_TBOOLEAN:
      80               6 :       return bvalue(t1) == bvalue(t2);  /* boolean true must be 1 !! */
      81                 :     case LUA_TLIGHTUSERDATA:
      82               0 :       return pvalue(t1) == pvalue(t2);
      83                 :     default:
      84                 :       lua_assert(iscollectable(t1));
      85            1254 :       return gcvalue(t1) == gcvalue(t2);
      86                 :   }
      87                 : }
      88                 : 
      89                 : 
      90            2124 : int luaO_str2d (const char *s, lua_Number *result) {
      91                 :   char *endptr;
      92            2124 :   *result = lua_str2number(s, &endptr);
      93            2124 :   if (endptr == s) return 0;  /* conversion failed */
      94            2105 :   if (*endptr == 'x' || *endptr == 'X')  /* maybe an hexadecimal constant? */
      95               0 :     *result = cast_num(strtoul(s, &endptr, 16));
      96            2105 :   if (*endptr == '\0') return 1;  /* most common case */
      97               3 :   while (isspace(cast(unsigned char, *endptr))) endptr++;
      98               3 :   if (*endptr != '\0') return 0;  /* invalid trailing characters? */
      99               2 :   return 1;
     100                 : }
     101                 : 
     102                 : 
     103                 : 
     104            3259 : static void pushstr (lua_State *L, const char *str) {
     105            3259 :   setsvalue2s(L, L->top, luaS_new(L, str));
     106            3259 :   incr_top(L);
     107            3259 : }
     108                 : 
     109                 : 
     110                 : /* this function handles only `%d', `%c', %f, %p, and `%s' formats */
     111             936 : const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
     112             936 :   int n = 1;
     113             936 :   pushstr(L, "");
     114                 :   for (;;) {
     115            2569 :     const char *e = strchr(fmt, '%');
     116            2569 :     if (e == NULL) break;
     117            1633 :     setsvalue2s(L, L->top, luaS_newlstr(L, fmt, e-fmt));
     118            1633 :     incr_top(L);
     119            1633 :     switch (*(e+1)) {
     120                 :       case 's': {
     121            1369 :         const char *s = va_arg(argp, char *);
     122            1369 :         if (s == NULL) s = "(null)";
     123            1369 :         pushstr(L, s);
     124            1369 :         break;
     125                 :       }
     126                 :       case 'c': {
     127                 :         char buff[2];
     128               6 :         buff[0] = cast(char, va_arg(argp, int));
     129               6 :         buff[1] = '\0';
     130               6 :         pushstr(L, buff);
     131               6 :         break;
     132                 :       }
     133                 :       case 'd': {
     134             246 :         setnvalue(L->top, cast_num(va_arg(argp, int)));
     135             246 :         incr_top(L);
     136             246 :         break;
     137                 :       }
     138                 :       case 'f': {
     139               0 :         setnvalue(L->top, cast_num(va_arg(argp, l_uacNumber)));
     140               0 :         incr_top(L);
     141               0 :         break;
     142                 :       }
     143                 :       case 'p': {
     144                 :         char buff[4*sizeof(void *) + 8]; /* should be enough space for a `%p' */
     145              10 :         sprintf(buff, "%p", va_arg(argp, void *));
     146              10 :         pushstr(L, buff);
     147              10 :         break;
     148                 :       }
     149                 :       case '%': {
     150               2 :         pushstr(L, "%");
     151               2 :         break;
     152                 :       }
     153                 :       default: {
     154                 :         char buff[3];
     155               0 :         buff[0] = '%';
     156               0 :         buff[1] = *(e+1);
     157               0 :         buff[2] = '\0';
     158               0 :         pushstr(L, buff);
     159                 :         break;
     160                 :       }
     161                 :     }
     162            1633 :     n += 2;
     163            1633 :     fmt = e+2;
     164            1633 :   }
     165             936 :   pushstr(L, fmt);
     166             936 :   luaV_concat(L, n+1, cast_int(L->top - L->base) - 1);
     167             936 :   L->top -= n;
     168             936 :   return svalue(L->top - 1);
     169                 : }
     170                 : 
     171                 : 
     172             184 : const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) {
     173                 :   const char *msg;
     174                 :   va_list argp;
     175             184 :   va_start(argp, fmt);
     176             184 :   msg = luaO_pushvfstring(L, fmt, argp);
     177             184 :   va_end(argp);
     178             184 :   return msg;
     179                 : }
     180                 : 
     181                 : 
     182             229 : void luaO_chunkid (char *out, const char *source, size_t bufflen) {
     183             229 :   if (*source == '=') {
     184               8 :     strncpy(out, source+1, bufflen);  /* remove first char */
     185               8 :     out[bufflen-1] = '\0';  /* ensures null termination */
     186                 :   }
     187                 :   else {  /* out = "source", or "...source" */
     188             221 :     if (*source == '@') {
     189                 :       size_t l;
     190             215 :       source++;  /* skip the `@' */
     191             215 :       bufflen -= sizeof(" '...' ");
     192             215 :       l = strlen(source);
     193             215 :       strcpy(out, "");
     194             215 :       if (l > bufflen) {
     195               0 :         source += (l-bufflen);  /* get last part of file name */
     196               0 :         strcat(out, "...");
     197                 :       }
     198             215 :       strcat(out, source);
     199                 :     }
     200                 :     else {  /* out = [string "string"] */
     201               6 :       size_t len = strcspn(source, "\n\r");  /* stop at first newline */
     202               6 :       bufflen -= sizeof(" [string \"...\"] ");
     203               6 :       if (len > bufflen) len = bufflen;
     204               6 :       strcpy(out, "[string \"");
     205               6 :       if (source[len] != '\0') {  /* must truncate? */
     206               5 :         strncat(out, source, len);
     207               5 :         strcat(out, "...");
     208                 :       }
     209                 :       else
     210               1 :         strcat(out, source);
     211               6 :       strcat(out, "\"]");
     212                 :     }
     213                 :   }
     214             229 : }

Generated by: LCOV version 1.7