annotate src/lua/lobject.c @ 441:e682c9ed3056

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