1997-09-16 23:25:59 +04:00
|
|
|
/*
|
2000-10-20 20:36:32 +04:00
|
|
|
** $Id: lobject.c,v 1.54 2000/10/10 19:53:20 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-10-20 20:36:32 +04:00
|
|
|
/* maximum length of a string format for `luaO_verror' */
|
|
|
|
#define MAX_VERROR 280
|
|
|
|
|
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-20 20:36:32 +04:00
|
|
|
char buff[MAX_VERROR]; /* 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-10-09 17:47:32 +04:00
|
|
|
void luaO_chunkid (char *out, const char *source, int bufflen) {
|
2000-10-20 20:36:32 +04:00
|
|
|
if (*source == '=') {
|
|
|
|
strncpy(out, source+1, bufflen); /* remove first char */
|
|
|
|
out[bufflen-1] = '\0'; /* ensures null termination */
|
|
|
|
}
|
2000-09-12 00:29:27 +04:00
|
|
|
else {
|
2000-10-09 17:47:32 +04:00
|
|
|
if (*source == '@') {
|
|
|
|
int l;
|
|
|
|
source++; /* skip the `@' */
|
2000-10-11 20:47:50 +04:00
|
|
|
bufflen -= sizeof("file `...%s'");
|
2000-10-09 17:47:32 +04:00
|
|
|
l = strlen(source);
|
|
|
|
if (l>bufflen) {
|
|
|
|
source += (l-bufflen); /* get last part of file name */
|
2000-10-20 20:36:32 +04:00
|
|
|
sprintf(out, "file `...%.99s'", source);
|
2000-10-09 17:47:32 +04:00
|
|
|
}
|
|
|
|
else
|
2000-10-20 20:36:32 +04:00
|
|
|
sprintf(out, "file `%.99s'", source);
|
2000-10-09 17:47:32 +04:00
|
|
|
}
|
2000-09-12 00:29:27 +04:00
|
|
|
else {
|
2000-10-09 17:47:32 +04:00
|
|
|
int len = strcspn(source, "\n"); /* stop at first newline */
|
2000-10-11 20:47:50 +04:00
|
|
|
bufflen -= sizeof("string \"%.*s...\"");
|
2000-10-09 17:47:32 +04:00
|
|
|
if (len > bufflen) len = bufflen;
|
2000-10-20 20:36:32 +04:00
|
|
|
if (source[len] != '\0') { /* must truncate? */
|
|
|
|
strcpy(out, "string \"");
|
|
|
|
out += strlen(out);
|
|
|
|
strncpy(out, source, len);
|
|
|
|
strcpy(out+len, "...\"");
|
|
|
|
}
|
2000-10-09 17:47:32 +04:00
|
|
|
else
|
2000-10-20 20:36:32 +04:00
|
|
|
sprintf(out, "string \"%.99s\"", source);
|
2000-09-12 00:29:27 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|