LCOV - code coverage report
Current view: top level - src - lobject.c Coverage Total Hit
Test: Lua 5.1.5 Lines: 88.7 % 115 102
Test Date: 2024-04-28 10:23:09
Legend: Lines: hit not hit

            Line data    Source code
       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         1428 : int luaO_int2fb (unsigned int x) {
      36         1428 :   int e = 0;  /* expoent */
      37         1435 :   while (x >= 16) {
      38            7 :     x = (x+1) >> 1;
      39            7 :     e++;
      40              :   }
      41         1428 :   if (x < 8) return x;
      42            9 :   else return ((e+1) << 3) | (cast_int(x) - 8);
      43              : }
      44              : 
      45              : 
      46              : /* converts back */
      47        13162 : int luaO_fb2int (int x) {
      48        13162 :   int e = (x >> 3) & 31;
      49        13162 :   if (e == 0) return x;
      50            5 :   else return ((x & 7)+8) << (e - 1);
      51              : }
      52              : 
      53              : 
      54        38263 : 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        38263 :   int l = -1;
      66        38356 :   while (x >= 256) { l += 8; x >>= 8; }
      67        38263 :   return l + log_2[x];
      68              : 
      69              : }
      70              : 
      71              : 
      72         2492 : int luaO_rawequalObj (const TValue *t1, const TValue *t2) {
      73         2492 :   if (ttype(t1) != ttype(t2)) return 0;
      74         1435 :   else switch (ttype(t1)) {
      75            1 :     case LUA_TNIL:
      76            1 :       return 1;
      77           96 :     case LUA_TNUMBER:
      78           96 :       return luai_numeq(nvalue(t1), nvalue(t2));
      79          106 :     case LUA_TBOOLEAN:
      80          106 :       return bvalue(t1) == bvalue(t2);  /* boolean true must be 1 !! */
      81          494 :     case LUA_TLIGHTUSERDATA:
      82          494 :       return pvalue(t1) == pvalue(t2);
      83          738 :     default:
      84              :       lua_assert(iscollectable(t1));
      85          738 :       return gcvalue(t1) == gcvalue(t2);
      86              :   }
      87              : }
      88              : 
      89              : 
      90         3475 : int luaO_str2d (const char *s, lua_Number *result) {
      91              :   char *endptr;
      92         3475 :   *result = lua_str2number(s, &endptr);
      93         3475 :   if (endptr == s) return 0;  /* conversion failed */
      94         3451 :   if (*endptr == 'x' || *endptr == 'X')  /* maybe an hexadecimal constant? */
      95            0 :     *result = cast_num(strtoul(s, &endptr, 16));
      96         3451 :   if (*endptr == '\0') return 1;  /* most common case */
      97            9 :   while (isspace(cast(unsigned char, *endptr))) endptr++;
      98            5 :   if (*endptr != '\0') return 0;  /* invalid trailing characters? */
      99            2 :   return 1;
     100              : }
     101              : 
     102              : 
     103              : 
     104         6039 : static void pushstr (lua_State *L, const char *str) {
     105         6039 :   setsvalue2s(L, L->top, luaS_new(L, str));
     106         6039 :   incr_top(L);
     107         6039 : }
     108              : 
     109              : 
     110              : /* this function handles only `%d', `%c', %f, %p, and `%s' formats */
     111         1811 : const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
     112         1811 :   int n = 1;
     113         1811 :   pushstr(L, "");
     114         2783 :   for (;;) {
     115         4594 :     const char *e = strchr(fmt, '%');
     116         4594 :     if (e == NULL) break;
     117         2783 :     setsvalue2s(L, L->top, luaS_newlstr(L, fmt, e-fmt));
     118         2783 :     incr_top(L);
     119         2783 :     switch (*(e+1)) {
     120         2382 :       case 's': {
     121         2382 :         const char *s = va_arg(argp, char *);
     122         2382 :         if (s == NULL) s = "(null)";
     123         2382 :         pushstr(L, s);
     124         2382 :         break;
     125              :       }
     126           16 :       case 'c': {
     127              :         char buff[2];
     128           16 :         buff[0] = cast(char, va_arg(argp, int));
     129           16 :         buff[1] = '\0';
     130           16 :         pushstr(L, buff);
     131           16 :         break;
     132              :       }
     133          366 :       case 'd': {
     134          366 :         setnvalue(L->top, cast_num(va_arg(argp, int)));
     135          366 :         incr_top(L);
     136          366 :         break;
     137              :       }
     138            0 :       case 'f': {
     139            0 :         setnvalue(L->top, cast_num(va_arg(argp, l_uacNumber)));
     140            0 :         incr_top(L);
     141            0 :         break;
     142              :       }
     143           16 :       case 'p': {
     144              :         char buff[4*sizeof(void *) + 8]; /* should be enough space for a `%p' */
     145           16 :         sprintf(buff, "%p", va_arg(argp, void *));
     146           16 :         pushstr(L, buff);
     147           16 :         break;
     148              :       }
     149            3 :       case '%': {
     150            3 :         pushstr(L, "%");
     151            3 :         break;
     152              :       }
     153            0 :       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            0 :         break;
     160              :       }
     161              :     }
     162         2783 :     n += 2;
     163         2783 :     fmt = e+2;
     164              :   }
     165         1811 :   pushstr(L, fmt);
     166         1811 :   luaV_concat(L, n+1, cast_int(L->top - L->base) - 1);
     167         1811 :   L->top -= n;
     168         1811 :   return svalue(L->top - 1);
     169              : }
     170              : 
     171              : 
     172          289 : const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) {
     173              :   const char *msg;
     174              :   va_list argp;
     175          289 :   va_start(argp, fmt);
     176          289 :   msg = luaO_pushvfstring(L, fmt, argp);
     177          289 :   va_end(argp);
     178          289 :   return msg;
     179              : }
     180              : 
     181              : 
     182          374 : void luaO_chunkid (char *out, const char *source, size_t bufflen) {
     183          374 :   if (*source == '=') {
     184           58 :     strncpy(out, source+1, bufflen);  /* remove first char */
     185           58 :     out[bufflen-1] = '\0';  /* ensures null termination */
     186              :   }
     187              :   else {  /* out = "source", or "...source" */
     188          316 :     if (*source == '@') {
     189              :       size_t l;
     190          280 :       source++;  /* skip the `@' */
     191          280 :       bufflen -= sizeof(" '...' ");
     192          280 :       l = strlen(source);
     193          280 :       strcpy(out, "");
     194          280 :       if (l > bufflen) {
     195            0 :         source += (l-bufflen);  /* get last part of file name */
     196            0 :         strcat(out, "...");
     197              :       }
     198          280 :       strcat(out, source);
     199              :     }
     200              :     else {  /* out = [string "string"] */
     201           36 :       size_t len = strcspn(source, "\n\r");  /* stop at first newline */
     202           36 :       bufflen -= sizeof(" [string \"...\"] ");
     203           36 :       if (len > bufflen) len = bufflen;
     204           36 :       strcpy(out, "[string \"");
     205           36 :       if (source[len] != '\0') {  /* must truncate? */
     206           10 :         strncat(out, source, len);
     207           10 :         strcat(out, "...");
     208              :       }
     209              :       else
     210           26 :         strcat(out, source);
     211           36 :       strcat(out, "\"]");
     212              :     }
     213              :   }
     214          374 : }
        

Generated by: LCOV version 2.0-1