lua/lobject.c

118 lines
2.8 KiB
C
Raw Normal View History

/*
2000-10-09 17:47:32 +04:00
** $Id: lobject.c,v 1.52 2000/10/05 12:14:08 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);
}
2000-10-09 17:47:32 +04:00
#define EXTRALEN sizeof(" string \"s...\" ")
2000-10-09 17:47:32 +04:00
void luaO_chunkid (char *out, const char *source, int bufflen) {
if (*source == '=')
sprintf(out, "%.*s", bufflen, source+1); /* remove first char */
else {
2000-10-09 17:47:32 +04:00
bufflen -= EXTRALEN;
if (*source == '@') {
int l;
source++; /* skip the `@' */
l = strlen(source);
if (l>bufflen) {
source += (l-bufflen); /* get last part of file name */
sprintf(out, "file `...%s'", source);
}
else
sprintf(out, "file `%s'", source);
}
else {
2000-10-09 17:47:32 +04:00
int len = strcspn(source, "\n"); /* stop at first newline */
if (len > bufflen) len = bufflen;
if (source[len] != '\0') /* must truncate? */
sprintf(out, "string \"%.*s...\"", len, source);
else
sprintf(out, "string \"%s\"", source);
}
}
}