1997-09-16 23:25:59 +04:00
|
|
|
/*
|
2000-10-05 16:14:08 +04:00
|
|
|
** $Id: lobject.c,v 1.51 2000/10/03 14:03:21 roberto Exp roberto $
|
1997-09-16 23:25:59 +04:00
|
|
|
** Some generic functions over Lua objects
|
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
1998-12-27 23:25:20 +03:00
|
|
|
#include <ctype.h>
|
2000-09-12 00:29:27 +04:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
1997-09-16 23:25:59 +04:00
|
|
|
#include <stdlib.h>
|
2000-09-12 00:29:27 +04:00
|
|
|
#include <string.h>
|
1997-09-16 23:25:59 +04:00
|
|
|
|
|
|
|
#include "lua.h"
|
|
|
|
|
2000-09-11 21:38:42 +04:00
|
|
|
#include "lmem.h"
|
2000-06-12 17:52:05 +04:00
|
|
|
#include "lobject.h"
|
2000-09-11 21:38:42 +04:00
|
|
|
#include "lstate.h"
|
2000-06-12 17:52:05 +04:00
|
|
|
|
1997-09-16 23:25:59 +04:00
|
|
|
|
2000-10-03 00:10:55 +04:00
|
|
|
|
2000-10-05 16:14:08 +04:00
|
|
|
const TObject luaO_nilobject = {LUA_TNIL, {NULL}};
|
1997-09-16 23:25:59 +04:00
|
|
|
|
|
|
|
|
2000-10-05 16:14:08 +04:00
|
|
|
const char *const luaO_typenames[] = {
|
|
|
|
"userdata", "nil", "number", "string", "table", "function"
|
|
|
|
};
|
2000-10-03 00:10:55 +04:00
|
|
|
|
1997-11-03 23:45:23 +03:00
|
|
|
|
|
|
|
|
1999-11-26 21:59:20 +03:00
|
|
|
/*
|
1999-12-14 21:33:29 +03:00
|
|
|
** returns smaller power of 2 larger than `n' (minimum is MINPOWER2)
|
1999-11-26 21:59:20 +03:00
|
|
|
*/
|
2000-05-24 17:54:49 +04:00
|
|
|
lint32 luaO_power2 (lint32 n) {
|
|
|
|
lint32 p = MINPOWER2;
|
1999-11-26 21:59:20 +03:00
|
|
|
while (p<=n) p<<=1;
|
|
|
|
return p;
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-04-25 20:55:09 +04:00
|
|
|
int luaO_equalObj (const TObject *t1, const TObject *t2) {
|
|
|
|
if (ttype(t1) != ttype(t2)) return 0;
|
1997-09-16 23:25:59 +04:00
|
|
|
switch (ttype(t1)) {
|
2000-10-05 16:14:08 +04:00
|
|
|
case LUA_TNUMBER:
|
1999-12-23 21:19:57 +03:00
|
|
|
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:
|
2000-06-08 22:27:13 +04:00
|
|
|
return hvalue(t1) == hvalue(t2);
|
2000-10-05 16:14:08 +04:00
|
|
|
case LUA_TFUNCTION:
|
2000-03-30 00:19:20 +04:00
|
|
|
return clvalue(t1) == clvalue(t2);
|
1997-09-16 23:25:59 +04:00
|
|
|
default:
|
2000-10-05 16:14:08 +04:00
|
|
|
LUA_ASSERT(ttype(t1) == LUA_TNIL, "invalid type");
|
|
|
|
return 1; /* LUA_TNIL */
|
1997-09-16 23:25:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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);
|
2000-09-29 16:42:13 +04:00
|
|
|
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 */
|
2000-10-03 18:03:21 +04:00
|
|
|
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;
|
1999-09-06 17:55:09 +04:00
|
|
|
return 1;
|
1998-12-27 23:25:20 +03:00
|
|
|
}
|
|
|
|
|
2000-09-12 00:29:27 +04:00
|
|
|
|
2000-09-29 16:42:13 +04:00
|
|
|
/* this function needs to handle only '%d' and '%.XXs' formats */
|
2000-09-12 00:29:27 +04:00
|
|
|
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 */
|
2000-09-12 00:29:27 +04:00
|
|
|
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")), "...\"");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|