diff src/lua/src/lobject.c @ 1:f9f4f1b99eed

importing src directory
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 10:31:27 -0600
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/lua/src/lobject.c	Sat Mar 03 10:31:27 2012 -0600
     1.3 @@ -0,0 +1,214 @@
     1.4 +/*
     1.5 +** $Id: lobject.c,v 2.22.1.1 2007/12/27 13:02:25 roberto Exp $
     1.6 +** Some generic functions over Lua objects
     1.7 +** See Copyright Notice in lua.h
     1.8 +*/
     1.9 +
    1.10 +#include <ctype.h>
    1.11 +#include <stdarg.h>
    1.12 +#include <stdio.h>
    1.13 +#include <stdlib.h>
    1.14 +#include <string.h>
    1.15 +
    1.16 +#define lobject_c
    1.17 +#define LUA_CORE
    1.18 +
    1.19 +#include "lua.h"
    1.20 +
    1.21 +#include "ldo.h"
    1.22 +#include "lmem.h"
    1.23 +#include "lobject.h"
    1.24 +#include "lstate.h"
    1.25 +#include "lstring.h"
    1.26 +#include "lvm.h"
    1.27 +
    1.28 +
    1.29 +
    1.30 +const TValue luaO_nilobject_ = {{NULL}, LUA_TNIL};
    1.31 +
    1.32 +
    1.33 +/*
    1.34 +** converts an integer to a "floating point byte", represented as
    1.35 +** (eeeeexxx), where the real value is (1xxx) * 2^(eeeee - 1) if
    1.36 +** eeeee != 0 and (xxx) otherwise.
    1.37 +*/
    1.38 +int luaO_int2fb (unsigned int x) {
    1.39 +  int e = 0;  /* expoent */
    1.40 +  while (x >= 16) {
    1.41 +    x = (x+1) >> 1;
    1.42 +    e++;
    1.43 +  }
    1.44 +  if (x < 8) return x;
    1.45 +  else return ((e+1) << 3) | (cast_int(x) - 8);
    1.46 +}
    1.47 +
    1.48 +
    1.49 +/* converts back */
    1.50 +int luaO_fb2int (int x) {
    1.51 +  int e = (x >> 3) & 31;
    1.52 +  if (e == 0) return x;
    1.53 +  else return ((x & 7)+8) << (e - 1);
    1.54 +}
    1.55 +
    1.56 +
    1.57 +int luaO_log2 (unsigned int x) {
    1.58 +  static const lu_byte log_2[256] = {
    1.59 +    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,
    1.60 +    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,
    1.61 +    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,
    1.62 +    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,
    1.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,
    1.64 +    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,
    1.65 +    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,
    1.66 +    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
    1.67 +  };
    1.68 +  int l = -1;
    1.69 +  while (x >= 256) { l += 8; x >>= 8; }
    1.70 +  return l + log_2[x];
    1.71 +
    1.72 +}
    1.73 +
    1.74 +
    1.75 +int luaO_rawequalObj (const TValue *t1, const TValue *t2) {
    1.76 +  if (ttype(t1) != ttype(t2)) return 0;
    1.77 +  else switch (ttype(t1)) {
    1.78 +    case LUA_TNIL:
    1.79 +      return 1;
    1.80 +    case LUA_TNUMBER:
    1.81 +      return luai_numeq(nvalue(t1), nvalue(t2));
    1.82 +    case LUA_TBOOLEAN:
    1.83 +      return bvalue(t1) == bvalue(t2);  /* boolean true must be 1 !! */
    1.84 +    case LUA_TLIGHTUSERDATA:
    1.85 +      return pvalue(t1) == pvalue(t2);
    1.86 +    default:
    1.87 +      lua_assert(iscollectable(t1));
    1.88 +      return gcvalue(t1) == gcvalue(t2);
    1.89 +  }
    1.90 +}
    1.91 +
    1.92 +
    1.93 +int luaO_str2d (const char *s, lua_Number *result) {
    1.94 +  char *endptr;
    1.95 +  *result = lua_str2number(s, &endptr);
    1.96 +  if (endptr == s) return 0;  /* conversion failed */
    1.97 +  if (*endptr == 'x' || *endptr == 'X')  /* maybe an hexadecimal constant? */
    1.98 +    *result = cast_num(strtoul(s, &endptr, 16));
    1.99 +  if (*endptr == '\0') return 1;  /* most common case */
   1.100 +  while (isspace(cast(unsigned char, *endptr))) endptr++;
   1.101 +  if (*endptr != '\0') return 0;  /* invalid trailing characters? */
   1.102 +  return 1;
   1.103 +}
   1.104 +
   1.105 +
   1.106 +
   1.107 +static void pushstr (lua_State *L, const char *str) {
   1.108 +  setsvalue2s(L, L->top, luaS_new(L, str));
   1.109 +  incr_top(L);
   1.110 +}
   1.111 +
   1.112 +
   1.113 +/* this function handles only `%d', `%c', %f, %p, and `%s' formats */
   1.114 +const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
   1.115 +  int n = 1;
   1.116 +  pushstr(L, "");
   1.117 +  for (;;) {
   1.118 +    const char *e = strchr(fmt, '%');
   1.119 +    if (e == NULL) break;
   1.120 +    setsvalue2s(L, L->top, luaS_newlstr(L, fmt, e-fmt));
   1.121 +    incr_top(L);
   1.122 +    switch (*(e+1)) {
   1.123 +      case 's': {
   1.124 +        const char *s = va_arg(argp, char *);
   1.125 +        if (s == NULL) s = "(null)";
   1.126 +        pushstr(L, s);
   1.127 +        break;
   1.128 +      }
   1.129 +      case 'c': {
   1.130 +        char buff[2];
   1.131 +        buff[0] = cast(char, va_arg(argp, int));
   1.132 +        buff[1] = '\0';
   1.133 +        pushstr(L, buff);
   1.134 +        break;
   1.135 +      }
   1.136 +      case 'd': {
   1.137 +        setnvalue(L->top, cast_num(va_arg(argp, int)));
   1.138 +        incr_top(L);
   1.139 +        break;
   1.140 +      }
   1.141 +      case 'f': {
   1.142 +        setnvalue(L->top, cast_num(va_arg(argp, l_uacNumber)));
   1.143 +        incr_top(L);
   1.144 +        break;
   1.145 +      }
   1.146 +      case 'p': {
   1.147 +        char buff[4*sizeof(void *) + 8]; /* should be enough space for a `%p' */
   1.148 +        sprintf(buff, "%p", va_arg(argp, void *));
   1.149 +        pushstr(L, buff);
   1.150 +        break;
   1.151 +      }
   1.152 +      case '%': {
   1.153 +        pushstr(L, "%");
   1.154 +        break;
   1.155 +      }
   1.156 +      default: {
   1.157 +        char buff[3];
   1.158 +        buff[0] = '%';
   1.159 +        buff[1] = *(e+1);
   1.160 +        buff[2] = '\0';
   1.161 +        pushstr(L, buff);
   1.162 +        break;
   1.163 +      }
   1.164 +    }
   1.165 +    n += 2;
   1.166 +    fmt = e+2;
   1.167 +  }
   1.168 +  pushstr(L, fmt);
   1.169 +  luaV_concat(L, n+1, cast_int(L->top - L->base) - 1);
   1.170 +  L->top -= n;
   1.171 +  return svalue(L->top - 1);
   1.172 +}
   1.173 +
   1.174 +
   1.175 +const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) {
   1.176 +  const char *msg;
   1.177 +  va_list argp;
   1.178 +  va_start(argp, fmt);
   1.179 +  msg = luaO_pushvfstring(L, fmt, argp);
   1.180 +  va_end(argp);
   1.181 +  return msg;
   1.182 +}
   1.183 +
   1.184 +
   1.185 +void luaO_chunkid (char *out, const char *source, size_t bufflen) {
   1.186 +  if (*source == '=') {
   1.187 +    strncpy(out, source+1, bufflen);  /* remove first char */
   1.188 +    out[bufflen-1] = '\0';  /* ensures null termination */
   1.189 +  }
   1.190 +  else {  /* out = "source", or "...source" */
   1.191 +    if (*source == '@') {
   1.192 +      size_t l;
   1.193 +      source++;  /* skip the `@' */
   1.194 +      bufflen -= sizeof(" '...' ");
   1.195 +      l = strlen(source);
   1.196 +      strcpy(out, "");
   1.197 +      if (l > bufflen) {
   1.198 +        source += (l-bufflen);  /* get last part of file name */
   1.199 +        strcat(out, "...");
   1.200 +      }
   1.201 +      strcat(out, source);
   1.202 +    }
   1.203 +    else {  /* out = [string "string"] */
   1.204 +      size_t len = strcspn(source, "\n\r");  /* stop at first newline */
   1.205 +      bufflen -= sizeof(" [string \"...\"] ");
   1.206 +      if (len > bufflen) len = bufflen;
   1.207 +      strcpy(out, "[string \"");
   1.208 +      if (source[len] != '\0') {  /* must truncate? */
   1.209 +        strncat(out, source, len);
   1.210 +        strcat(out, "...");
   1.211 +      }
   1.212 +      else
   1.213 +        strcat(out, source);
   1.214 +      strcat(out, "\"]");
   1.215 +    }
   1.216 +  }
   1.217 +}