1997-09-17 03:25:59 +08:00
|
|
|
/*
|
2000-10-09 21:47:32 +08:00
|
|
|
** $Id: lobject.c,v 1.52 2000/10/05 12:14:08 roberto Exp roberto $
|
1997-09-17 03:25:59 +08:00
|
|
|
** Some generic functions over Lua objects
|
|
|
|
** See Copyright Notice in lua.h
|
|
|
|
*/
|
|
|
|
|
1998-12-28 04:25:20 +08:00
|
|
|
#include <ctype.h>
|
2000-09-12 04:29:27 +08:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
1997-09-17 03:25:59 +08:00
|
|
|
#include <stdlib.h>
|
2000-09-12 04:29:27 +08:00
|
|
|
#include <string.h>
|
1997-09-17 03:25:59 +08:00
|
|
|
|
|
|
|
#include "lua.h"
|
|
|
|
|
2000-09-12 01:38:42 +08:00
|
|
|
#include "lmem.h"
|
2000-06-12 21:52:05 +08:00
|
|
|
#include "lobject.h"
|
2000-09-12 01:38:42 +08:00
|
|
|
#include "lstate.h"
|
2000-06-12 21:52:05 +08:00
|
|
|
|
1997-09-17 03:25:59 +08:00
|
|
|
|
2000-10-03 04:10:55 +08:00
|
|
|
|
2000-10-05 20:14:08 +08:00
|
|
|
const TObject luaO_nilobject = {LUA_TNIL, {NULL}};
|
1997-09-17 03:25:59 +08:00
|
|
|
|
|
|
|
|
2000-10-05 20:14:08 +08:00
|
|
|
const char *const luaO_typenames[] = {
|
|
|
|
"userdata", "nil", "number", "string", "table", "function"
|
|
|
|
};
|
2000-10-03 04:10:55 +08:00
|
|
|
|
1997-11-04 04:45:23 +08:00
|
|
|
|
|
|
|
|
1999-11-27 02:59:20 +08:00
|
|
|
/*
|
1999-12-15 02:33:29 +08:00
|
|
|
** returns smaller power of 2 larger than `n' (minimum is MINPOWER2)
|
1999-11-27 02:59:20 +08:00
|
|
|
*/
|
2000-05-24 21:54:49 +08:00
|
|
|
lint32 luaO_power2 (lint32 n) {
|
|
|
|
lint32 p = MINPOWER2;
|
1999-11-27 02:59:20 +08:00
|
|
|
while (p<=n) p<<=1;
|
|
|
|
return p;
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-04-26 00:55:09 +08:00
|
|
|
int luaO_equalObj (const TObject *t1, const TObject *t2) {
|
|
|
|
if (ttype(t1) != ttype(t2)) return 0;
|
1997-09-17 03:25:59 +08:00
|
|
|
switch (ttype(t1)) {
|
2000-10-05 20:14:08 +08:00
|
|
|
case LUA_TNUMBER:
|
1999-12-24 02:19:57 +08:00
|
|
|
return nvalue(t1) == nvalue(t2);
|
2000-10-05 20:14:08 +08:00
|
|
|
case LUA_TSTRING: case LUA_TUSERDATA:
|
2000-04-26 21:43:10 +08:00
|
|
|
return tsvalue(t1) == tsvalue(t2);
|
2000-10-05 20:14:08 +08:00
|
|
|
case LUA_TTABLE:
|
2000-06-09 02:27:13 +08:00
|
|
|
return hvalue(t1) == hvalue(t2);
|
2000-10-05 20:14:08 +08:00
|
|
|
case LUA_TFUNCTION:
|
2000-03-30 04:19:20 +08:00
|
|
|
return clvalue(t1) == clvalue(t2);
|
1997-09-17 03:25:59 +08:00
|
|
|
default:
|
2000-10-05 20:14:08 +08:00
|
|
|
LUA_ASSERT(ttype(t1) == LUA_TNIL, "invalid type");
|
|
|
|
return 1; /* LUA_TNIL */
|
1997-09-17 03:25:59 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-09-12 01:38:42 +08:00
|
|
|
char *luaO_openspace (lua_State *L, size_t n) {
|
|
|
|
if (n > L->Mbuffsize) {
|
|
|
|
luaM_reallocvector(L, L->Mbuffer, n, char);
|
2000-09-29 20:42:13 +08:00
|
|
|
L->nblocks += (n - L->Mbuffsize)*sizeof(char);
|
2000-09-12 01:38:42 +08:00
|
|
|
L->Mbuffsize = n;
|
|
|
|
}
|
|
|
|
return L->Mbuffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-03-11 02:37:44 +08:00
|
|
|
int luaO_str2d (const char *s, Number *result) { /* LUA_NUMBER */
|
2000-10-03 22:03:21 +08: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 21:55:09 +08:00
|
|
|
return 1;
|
1998-12-28 04:25:20 +08:00
|
|
|
}
|
|
|
|
|
2000-09-12 04:29:27 +08:00
|
|
|
|
2000-09-29 20:42:13 +08:00
|
|
|
/* this function needs to handle only '%d' and '%.XXs' formats */
|
2000-09-12 04:29:27 +08:00
|
|
|
void luaO_verror (lua_State *L, const char *fmt, ...) {
|
|
|
|
va_list argp;
|
2000-10-05 20:14:08 +08:00
|
|
|
char buff[600]; /* to hold formatted message */
|
2000-09-12 04:29:27 +08:00
|
|
|
va_start(argp, fmt);
|
|
|
|
vsprintf(buff, fmt, argp);
|
|
|
|
va_end(argp);
|
|
|
|
lua_error(L, buff);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-10-09 21:47:32 +08:00
|
|
|
#define EXTRALEN sizeof(" string \"s...\" ")
|
2000-09-12 04:29:27 +08:00
|
|
|
|
2000-10-09 21:47:32 +08:00
|
|
|
void luaO_chunkid (char *out, const char *source, int bufflen) {
|
|
|
|
if (*source == '=')
|
|
|
|
sprintf(out, "%.*s", bufflen, source+1); /* remove first char */
|
2000-09-12 04:29:27 +08:00
|
|
|
else {
|
2000-10-09 21:47:32 +08: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);
|
|
|
|
}
|
2000-09-12 04:29:27 +08:00
|
|
|
else {
|
2000-10-09 21:47:32 +08: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);
|
2000-09-12 04:29:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|