Path: blob/main/sys/contrib/openzfs/module/lua/lobject.c
48383 views
// SPDX-License-Identifier: MIT1/*2** $Id: lobject.c,v 2.58.1.1 2013/04/12 18:48:47 roberto Exp $3** Some generic functions over Lua objects4** See Copyright Notice in lua.h5*/67#define lobject_c8#define LUA_CORE910#include <sys/lua/lua.h>1112#include "lctype.h"13#include "ldebug.h"14#include "ldo.h"15#include "lmem.h"16#include "lobject.h"17#include "lstate.h"18#include "lstring.h"19#include "lvm.h"20212223LUAI_DDEF const TValue luaO_nilobject_ = {NILCONSTANT};242526/*27** converts an integer to a "floating point byte", represented as28** (eeeeexxx), where the real value is (1xxx) * 2^(eeeee - 1) if29** eeeee != 0 and (xxx) otherwise.30*/31int luaO_int2fb (unsigned int x) {32int e = 0; /* exponent */33if (x < 8) return x;34while (x >= 0x10) {35x = (x+1) >> 1;36e++;37}38return ((e+1) << 3) | (cast_int(x) - 8);39}404142/* converts back */43int luaO_fb2int (int x) {44int e = (x >> 3) & 0x1f;45if (e == 0) return x;46else return ((x & 7) + 8) << (e - 1);47}484950int luaO_ceillog2 (unsigned int x) {51static const lu_byte log_2[256] = {520,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,536,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,547,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,557,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,568,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,578,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,588,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,598,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,860};61int l = 0;62x--;63while (x >= 256) { l += 8; x >>= 8; }64return l + log_2[x];65}666768lua_Number luaO_arith (int op, lua_Number v1, lua_Number v2) {69switch (op) {70case LUA_OPADD: return luai_numadd(NULL, v1, v2);71case LUA_OPSUB: return luai_numsub(NULL, v1, v2);72case LUA_OPMUL: return luai_nummul(NULL, v1, v2);73case LUA_OPDIV: return luai_numdiv(NULL, v1, v2);74case LUA_OPMOD: return luai_nummod(NULL, v1, v2);75case LUA_OPPOW: return luai_numpow(NULL, v1, v2);76case LUA_OPUNM: return luai_numunm(NULL, v1);77default: lua_assert(0); return 0;78}79}808182int luaO_hexavalue (int c) {83if (lisdigit(c)) return c - '0';84else return ltolower(c) - 'a' + 10;85}868788#if !defined(lua_strx2number)89909192static int isneg (const char **s) {93if (**s == '-') { (*s)++; return 1; }94else if (**s == '+') (*s)++;95return 0;96}979899static lua_Number readhexa (const char **s, lua_Number r, int *count) {100for (; lisxdigit(cast_uchar(**s)); (*s)++) { /* read integer part */101r = (r * cast_num(16.0)) + cast_num(luaO_hexavalue(cast_uchar(**s)));102(*count)++;103}104return r;105}106107108/*109** convert an hexadecimal numeric string to a number, following110** C99 specification for 'strtod'111*/112static lua_Number lua_strx2number (const char *s, char **endptr) {113lua_Number r = 0.0;114int e = 0, i = 0;115int neg = 0; /* 1 if number is negative */116*endptr = cast(char *, s); /* nothing is valid yet */117while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */118neg = isneg(&s); /* check signal */119if (!(*s == '0' && (*(s + 1) == 'x' || *(s + 1) == 'X'))) /* check '0x' */120return 0.0; /* invalid format (no '0x') */121s += 2; /* skip '0x' */122r = readhexa(&s, r, &i); /* read integer part */123if (*s == '.') {124s++; /* skip dot */125r = readhexa(&s, r, &e); /* read fractional part */126}127if (i == 0 && e == 0)128return 0.0; /* invalid format (no digit) */129e *= -4; /* each fractional digit divides value by 2^-4 */130*endptr = cast(char *, s); /* valid up to here */131if (*s == 'p' || *s == 'P') { /* exponent part? */132int exp1 = 0;133int neg1;134s++; /* skip 'p' */135neg1 = isneg(&s); /* signal */136if (!lisdigit(cast_uchar(*s)))137goto ret; /* must have at least one digit */138while (lisdigit(cast_uchar(*s))) /* read exponent */139exp1 = exp1 * 10 + *(s++) - '0';140if (neg1) exp1 = -exp1;141e += exp1;142}143*endptr = cast(char *, s); /* valid up to here */144ret:145if (neg) r = -r;146return ((e >= 0) ? (r * (1ULL << e)) : (r / (1ULL << -e)));147}148149#endif150151152int luaO_str2d (const char *s, size_t len, lua_Number *result) {153char *endptr;154if (strpbrk(s, "nN")) /* reject 'inf' and 'nan' */155return 0;156else if (strpbrk(s, "xX")) /* hexa? */157*result = lua_strx2number(s, &endptr);158else159*result = lua_str2number(s, &endptr);160if (endptr == s) return 0; /* nothing recognized */161while (lisspace(cast_uchar(*endptr))) endptr++;162return (endptr == s + len); /* OK if no trailing characters */163}164165166167static void pushstr (lua_State *L, const char *str, size_t l) {168setsvalue2s(L, L->top++, luaS_newlstr(L, str, l));169}170171172/* this function handles only `%d', `%c', %f, %p, and `%s' formats */173const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {174int n = 0;175for (;;) {176const char *e = strchr(fmt, '%');177if (e == NULL) break;178luaD_checkstack(L, 2); /* fmt + item */179pushstr(L, fmt, e - fmt);180switch (*(e+1)) {181case 's': {182const char *s = va_arg(argp, char *);183if (s == NULL) s = "(null)";184pushstr(L, s, strlen(s));185break;186}187case 'c': {188char buff;189buff = cast(char, va_arg(argp, int));190pushstr(L, &buff, 1);191break;192}193case 'd': {194setnvalue(L->top++, cast_num(va_arg(argp, int)));195break;196}197case 'f': {198setnvalue(L->top++, cast_num(va_arg(argp, l_uacNumber)));199break;200}201case 'p': {202char buff[4*sizeof(void *) + 8]; /* should be enough space for a `%p' */203int l = lcompat_sprintf(buff, sizeof(buff), "%p", va_arg(argp, void *));204pushstr(L, buff, l);205break;206}207case '%': {208pushstr(L, "%", 1);209break;210}211default: {212luaG_runerror(L,213"invalid option " LUA_QL("%%%c") " to " LUA_QL("lua_pushfstring"),214*(e + 1));215}216}217n += 2;218fmt = e+2;219}220luaD_checkstack(L, 1);221pushstr(L, fmt, strlen(fmt));222if (n > 0) luaV_concat(L, n + 1);223return svalue(L->top - 1);224}225226227const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) {228const char *msg;229va_list argp;230va_start(argp, fmt);231msg = luaO_pushvfstring(L, fmt, argp);232va_end(argp);233return msg;234}235236237/* number of chars of a literal string without the ending \0 */238#define LL(x) (sizeof(x)/sizeof(char) - 1)239240#define RETS "..."241#define PRE "[string \""242#define POS "\"]"243244#define addstr(a,b,l) ( memcpy(a,b,(l) * sizeof(char)), a += (l) )245246void luaO_chunkid (char *out, const char *source, size_t bufflen) {247size_t l = strlen(source);248if (*source == '=') { /* 'literal' source */249if (l <= bufflen) /* small enough? */250memcpy(out, source + 1, l * sizeof(char));251else { /* truncate it */252addstr(out, source + 1, bufflen - 1);253*out = '\0';254}255}256else if (*source == '@') { /* file name */257if (l <= bufflen) /* small enough? */258memcpy(out, source + 1, l * sizeof(char));259else { /* add '...' before rest of name */260addstr(out, RETS, LL(RETS));261bufflen -= LL(RETS);262memcpy(out, source + 1 + l - bufflen, bufflen * sizeof(char));263}264}265else { /* string; format as [string "source"] */266const char *nl = strchr(source, '\n'); /* find first new line (if any) */267addstr(out, PRE, LL(PRE)); /* add prefix */268bufflen -= LL(PRE RETS POS) + 1; /* save space for prefix+suffix+'\0' */269if (l < bufflen && nl == NULL) { /* small one-line source? */270addstr(out, source, l); /* keep it */271}272else {273if (nl != NULL) l = nl - source; /* stop at first newline */274if (l > bufflen) l = bufflen;275addstr(out, source, l);276addstr(out, RETS, LL(RETS));277}278memcpy(out, POS, (LL(POS) + 1) * sizeof(char));279}280}281282283