lua/lobject.c

110 lines
2.6 KiB
C
Raw Normal View History

/*
2000-10-05 16:14:08 +04:00
** $Id: lobject.c,v 1.51 2000/10/03 14:03:21 roberto Exp roberto $
** Some generic functions over Lua objects
** See Copyright Notice in lua.h
*/
#include <ctype.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lua.h"
2000-09-11 21:38:42 +04:00
#include "lmem.h"
#include "lobject.h"
2000-09-11 21:38:42 +04:00
#include "lstate.h"
2000-10-05 16:14:08 +04:00
const TObject luaO_nilobject = {LUA_TNIL, {NULL}};
2000-10-05 16:14:08 +04:00
const char *const luaO_typenames[] = {
"userdata", "nil", "number", "string", "table", "function"
};
1997-11-03 23:45:23 +03:00
/*
1999-12-14 21:33:29 +03:00
** returns smaller power of 2 larger than `n' (minimum is MINPOWER2)
*/
2000-05-24 17:54:49 +04:00
lint32 luaO_power2 (lint32 n) {
lint32 p = MINPOWER2;
while (p<=n) p<<=1;
return p;
}
2000-04-25 20:55:09 +04:00
int luaO_equalObj (const TObject *t1, const TObject *t2) {
if (ttype(t1) != ttype(t2)) return 0;
switch (ttype(t1)) {
2000-10-05 16:14:08 +04:00
case LUA_TNUMBER:
return nvalue(t1) == nvalue(t2);
2000-10-05 16:14:08 +04:00
case LUA_TSTRING: case LUA_TUSERDATA:
2000-04-26 17:43:10 +04:00
return tsvalue(t1) == tsvalue(t2);
2000-10-05 16:14:08 +04:00
case LUA_TTABLE:
return hvalue(t1) == hvalue(t2);
2000-10-05 16:14:08 +04:00
case LUA_TFUNCTION:
return clvalue(t1) == clvalue(t2);
default:
2000-10-05 16:14:08 +04:00
LUA_ASSERT(ttype(t1) == LUA_TNIL, "invalid type");
return 1; /* LUA_TNIL */
}
}
2000-09-11 21:38:42 +04:00
char *luaO_openspace (lua_State *L, size_t n) {
if (n > L->Mbuffsize) {
luaM_reallocvector(L, L->Mbuffer, n, char);
L->nblocks += (n - L->Mbuffsize)*sizeof(char);
2000-09-11 21:38:42 +04:00
L->Mbuffsize = n;
}
return L->Mbuffer;
}
2000-03-10 21:37:44 +03:00
int luaO_str2d (const char *s, Number *result) { /* LUA_NUMBER */
char *endptr;
Number res = lua_str2number(s, &endptr);
if (endptr == s) return 0; /* no conversion */
while (isspace((unsigned char)*endptr)) endptr++;
if (*endptr != '\0') return 0; /* invalid trailing characters? */
*result = res;
return 1;
}
/* this function needs to handle only '%d' and '%.XXs' formats */
void luaO_verror (lua_State *L, const char *fmt, ...) {
va_list argp;
2000-10-05 16:14:08 +04:00
char buff[600]; /* to hold formatted message */
va_start(argp, fmt);
vsprintf(buff, fmt, argp);
va_end(argp);
lua_error(L, buff);
}
#define EXTRALEN sizeof("string \"...\"0")
void luaO_chunkid (char *out, const char *source, int len) {
if (*source == '(') {
strncpy(out, source+1, len-1); /* remove first char */
out[len-1] = '\0'; /* make sure `out' has an end */
out[strlen(out)-1] = '\0'; /* remove last char */
}
else {
len -= EXTRALEN;
if (*source == '@')
sprintf(out, "file `%.*s'", len, source+1);
else {
const char *b = strchr(source , '\n'); /* stop at first new line */
int lim = (b && (b-source)<len) ? b-source : len;
sprintf(out, "string \"%.*s\"", lim, source);
strcpy(out+lim+(EXTRALEN-sizeof("...\"0")), "...\"");
}
}
}