⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lapi.c.svn-base

📁 絲路server源碼 Silk Road server source
💻 SVN-BASE
📖 第 1 页 / 共 2 页
字号:
/*
** $Id: lapi.c,v 2.55 2006/06/07 12:37:17 roberto Exp $
** pve API
** See Copyright Notice in pve.h
*/


#include <assert.h>
#include <math.h>
#include <stdarg.h>
#include <string.h>

#define lapi_c
#define pve_CORE

#include "pve.h"

#include "lapi.h"
#include "ldebug.h"
#include "ldo.h"
#include "lfunc.h"
#include "lgc.h"
#include "lmem.h"
#include "lobject.h"
#include "lstate.h"
#include "lstring.h"
#include "ltable.h"
#include "ltm.h"
#include "lundump.h"
#include "lvm.h"



const char pve_ident[] =
  "$pve: " pve_RELEASE " " pve_COPYRIGHT " $\n"
  "$Authors: " pve_AUTHORS " $\n"
  "$URL: www.pve.org $\n";



#define api_checknelems(L, n)	api_check(L, (n) <= (L->top - L->base))

#define api_checkvalidindex(L, i)	api_check(L, (i) != pveO_nilobject)

#define api_incr_top(L)   {api_check(L, L->top < L->ci->top); L->top++;}



static TValue *index2adr (pve_State *L, int idx) {
  if (idx > 0) {
    TValue *o = L->base + (idx - 1);
    api_check(L, idx <= L->ci->top - L->base);
    if (o >= L->top) return cast(TValue *, pveO_nilobject);
    else return o;
  }
  else if (idx > pve_REGISTRYINDEX) {
    api_check(L, idx != 0 && -idx <= L->top - L->base);
    return L->top + idx;
  }
  else switch (idx) {  /* pseudo-indices */
    case pve_REGISTRYINDEX: return registry(L);
    case pve_ENVIRONINDEX: {
      Closure *func = curr_func(L);
      sethvalue(L, &L->env, func->c.env);
      return &L->env;
    }
    case pve_GLOBALSINDEX: return gt(L);
    default: {
      Closure *func = curr_func(L);
      idx = pve_GLOBALSINDEX - idx;
      return (idx <= func->c.nupvalues)
                ? &func->c.upvalue[idx-1]
                : cast(TValue *, pveO_nilobject);
    }
  }
}


static Table *getcurrenv (pve_State *L) {
  if (L->ci == L->base_ci)  /* no enclosing function? */
    return hvalue(gt(L));  /* use global table as environment */
  else {
    Closure *func = curr_func(L);
    return func->c.env;
  }
}


void pveA_pushobject (pve_State *L, const TValue *o) {
  setobj2s(L, L->top, o);
  api_incr_top(L);
}


pve_API int pve_checkstack (pve_State *L, int size) {
  int res;
  pve_lock(L);
  if ((L->top - L->base + size) > pveI_MAXCSTACK)
    res = 0;  /* stack overflow */
  else {
    pveD_checkstack(L, size);
    if (L->ci->top < L->top + size)
      L->ci->top = L->top + size;
    res = 1;
  }
  pve_unlock(L);
  return res;
}


pve_API void pve_xmove (pve_State *from, pve_State *to, int n) {
  int i;
  if (from == to) return;
  pve_lock(to);
  api_checknelems(from, n);
  api_check(from, G(from) == G(to));
  api_check(from, to->ci->top - to->top >= n);
  from->top -= n;
  for (i = 0; i < n; i++) {
    setobj2s(to, to->top++, from->top + i);
  }
  pve_unlock(to);
}


pve_API pve_CFunction pve_atpanic (pve_State *L, pve_CFunction panicf) {
  pve_CFunction old;
  pve_lock(L);
  old = G(L)->panic;
  G(L)->panic = panicf;
  pve_unlock(L);
  return old;
}


pve_API pve_State *pve_newthread (pve_State *L) {
  pve_State *L1;
  pve_lock(L);
  pveC_checkGC(L);
  L1 = pveE_newthread(L);
  setthvalue(L, L->top, L1);
  api_incr_top(L);
  pve_unlock(L);
  pvei_userstatethread(L, L1);
  return L1;
}



/*
** basic stack manipulation
*/


pve_API int pve_gettop (pve_State *L) {
  return cast_int(L->top - L->base);
}


pve_API void pve_settop (pve_State *L, int idx) {
  pve_lock(L);
  if (idx >= 0) {
    api_check(L, idx <= L->stack_last - L->base);
    while (L->top < L->base + idx)
      setnilvalue(L->top++);
    L->top = L->base + idx;
  }
  else {
    api_check(L, -(idx+1) <= (L->top - L->base));
    L->top += idx+1;  /* `subtract' index (index is negative) */
  }
  pve_unlock(L);
}


pve_API void pve_remove (pve_State *L, int idx) {
  StkId p;
  pve_lock(L);
  p = index2adr(L, idx);
  api_checkvalidindex(L, p);
  while (++p < L->top) setobjs2s(L, p-1, p);
  L->top--;
  pve_unlock(L);
}


pve_API void pve_insert (pve_State *L, int idx) {
  StkId p;
  StkId q;
  pve_lock(L);
  p = index2adr(L, idx);
  api_checkvalidindex(L, p);
  for (q = L->top; q>p; q--) setobjs2s(L, q, q-1);
  setobjs2s(L, p, L->top);
  pve_unlock(L);
}


pve_API void pve_replace (pve_State *L, int idx) {
  StkId o;
  pve_lock(L);
  /* explicit test for incompatible code */
  if (idx == pve_ENVIRONINDEX && L->ci == L->base_ci)
    pveG_runerror(L, "no calling environment");
  api_checknelems(L, 1);
  o = index2adr(L, idx);
  api_checkvalidindex(L, o);
  if (idx == pve_ENVIRONINDEX) {
    Closure *func = curr_func(L);
    api_check(L, ttistable(L->top - 1)); 
    func->c.env = hvalue(L->top - 1);
    pveC_barrier(L, func, L->top - 1);
  }
  else {
    setobj(L, o, L->top - 1);
    if (idx < pve_GLOBALSINDEX)  /* function upvalue? */
      pveC_barrier(L, curr_func(L), L->top - 1);
  }
  L->top--;
  pve_unlock(L);
}


pve_API void pve_pushvalue (pve_State *L, int idx) {
  pve_lock(L);
  setobj2s(L, L->top, index2adr(L, idx));
  api_incr_top(L);
  pve_unlock(L);
}



/*
** access functions (stack -> C)
*/


pve_API int pve_type (pve_State *L, int idx) {
  StkId o = index2adr(L, idx);
  return (o == pveO_nilobject) ? pve_TNONE : ttype(o);
}


pve_API const char *pve_typename (pve_State *L, int t) {
  UNUSED(L);
  return (t == pve_TNONE) ? "no value" : pveT_typenames[t];
}


pve_API int pve_iscfunction (pve_State *L, int idx) {
  StkId o = index2adr(L, idx);
  return iscfunction(o);
}


pve_API int pve_isnumber (pve_State *L, int idx) {
  TValue n;
  const TValue *o = index2adr(L, idx);
  return tonumber(o, &n);
}


pve_API int pve_isstring (pve_State *L, int idx) {
  int t = pve_type(L, idx);
  return (t == pve_TSTRING || t == pve_TNUMBER);
}


pve_API int pve_isuserdata (pve_State *L, int idx) {
  const TValue *o = index2adr(L, idx);
  return (ttisuserdata(o) || ttislightuserdata(o));
}


pve_API int pve_rawequal (pve_State *L, int index1, int index2) {
  StkId o1 = index2adr(L, index1);
  StkId o2 = index2adr(L, index2);
  return (o1 == pveO_nilobject || o2 == pveO_nilobject) ? 0
         : pveO_rawequalObj(o1, o2);
}


pve_API int pve_equal (pve_State *L, int index1, int index2) {
  StkId o1, o2;
  int i;
  pve_lock(L);  /* may call tag method */
  o1 = index2adr(L, index1);
  o2 = index2adr(L, index2);
  i = (o1 == pveO_nilobject || o2 == pveO_nilobject) ? 0 : equalobj(L, o1, o2);
  pve_unlock(L);
  return i;
}


pve_API int pve_lessthan (pve_State *L, int index1, int index2) {
  StkId o1, o2;
  int i;
  pve_lock(L);  /* may call tag method */
  o1 = index2adr(L, index1);
  o2 = index2adr(L, index2);
  i = (o1 == pveO_nilobject || o2 == pveO_nilobject) ? 0
       : pveV_lessthan(L, o1, o2);
  pve_unlock(L);
  return i;
}



pve_API pve_Number pve_tonumber (pve_State *L, int idx) {
  TValue n;
  const TValue *o = index2adr(L, idx);
  if (tonumber(o, &n))
    return nvalue(o);
  else
    return 0;
}


pve_API pve_Integer pve_tointeger (pve_State *L, int idx) {
  TValue n;
  const TValue *o = index2adr(L, idx);
  if (tonumber(o, &n)) {
    pve_Integer res;
    pve_Number num = nvalue(o);
    pve_number2integer(res, num);
    return res;
  }
  else
    return 0;
}


pve_API int pve_toboolean (pve_State *L, int idx) {
  const TValue *o = index2adr(L, idx);
  return !l_isfalse(o);
}


pve_API const char *pve_tolstring (pve_State *L, int idx, size_t *len) {
  StkId o = index2adr(L, idx);
  if (!ttisstring(o)) {
    pve_lock(L);  /* `pveV_tostring' may create a new string */
    if (!pveV_tostring(L, o)) {  /* conversion failed? */
      if (len != NULL) *len = 0;
      pve_unlock(L);
      return NULL;
    }
    pveC_checkGC(L);
    o = index2adr(L, idx);  /* previous call may reallocate the stack */
    pve_unlock(L);
  }
  if (len != NULL) *len = tsvalue(o)->len;
  return svalue(o);
}


pve_API size_t pve_objlen (pve_State *L, int idx) {
  StkId o = index2adr(L, idx);
  switch (ttype(o)) {
    case pve_TSTRING: return tsvalue(o)->len;
    case pve_TUSERDATA: return uvalue(o)->len;
    case pve_TTABLE: return pveH_getn(hvalue(o));
    case pve_TNUMBER: {
      size_t l;
      pve_lock(L);  /* `pveV_tostring' may create a new string */
      l = (pveV_tostring(L, o) ? tsvalue(o)->len : 0);
      pve_unlock(L);
      return l;
    }
    default: return 0;
  }
}


pve_API pve_CFunction pve_tocfunction (pve_State *L, int idx) {
  StkId o = index2adr(L, idx);
  return (!iscfunction(o)) ? NULL : clvalue(o)->c.f;
}


pve_API void *pve_touserdata (pve_State *L, int idx) {
  StkId o = index2adr(L, idx);
  switch (ttype(o)) {
    case pve_TUSERDATA: return (rawuvalue(o) + 1);
    case pve_TLIGHTUSERDATA: return pvalue(o);
    default: return NULL;
  }
}


pve_API pve_State *pve_tothread (pve_State *L, int idx) {
  StkId o = index2adr(L, idx);
  return (!ttisthread(o)) ? NULL : thvalue(o);
}


pve_API const void *pve_topointer (pve_State *L, int idx) {
  StkId o = index2adr(L, idx);
  switch (ttype(o)) {
    case pve_TTABLE: return hvalue(o);
    case pve_TFUNCTION: return clvalue(o);
    case pve_TTHREAD: return thvalue(o);
    case pve_TUSERDATA:
    case pve_TLIGHTUSERDATA:
      return pve_touserdata(L, idx);
    default: return NULL;
  }
}



/*
** push functions (C -> stack)
*/


pve_API void pve_pushnil (pve_State *L) {
  pve_lock(L);
  setnilvalue(L->top);
  api_incr_top(L);
  pve_unlock(L);
}


pve_API void pve_pushnumber (pve_State *L, pve_Number n) {
  pve_lock(L);
  setnvalue(L->top, n);
  api_incr_top(L);
  pve_unlock(L);
}


pve_API void pve_pushinteger (pve_State *L, pve_Integer n) {
  pve_lock(L);
  setnvalue(L->top, cast_num(n));
  api_incr_top(L);
  pve_unlock(L);
}


pve_API void pve_pushlstring (pve_State *L, const char *s, size_t len) {
  pve_lock(L);
  pveC_checkGC(L);
  setsvalue2s(L, L->top, pveS_newlstr(L, s, len));
  api_incr_top(L);
  pve_unlock(L);
}


pve_API void pve_pushstring (pve_State *L, const char *s) {
  if (s == NULL)
    pve_pushnil(L);
  else
    pve_pushlstring(L, s, strlen(s));
}


pve_API const char *pve_pushvfstring (pve_State *L, const char *fmt,
                                      va_list argp) {
  const char *ret;
  pve_lock(L);
  pveC_checkGC(L);
  ret = pveO_pushvfstring(L, fmt, argp);
  pve_unlock(L);
  return ret;
}


pve_API const char *pve_pushfstring (pve_State *L, const char *fmt, ...) {
  const char *ret;
  va_list argp;
  pve_lock(L);
  pveC_checkGC(L);
  va_start(argp, fmt);
  ret = pveO_pushvfstring(L, fmt, argp);
  va_end(argp);
  pve_unlock(L);
  return ret;
}


pve_API void pve_pushcclosure (pve_State *L, pve_CFunction fn, int n) {
  Closure *cl;
  pve_lock(L);
  pveC_checkGC(L);
  api_checknelems(L, n);
  cl = pveF_newCclosure(L, n, getcurrenv(L));
  cl->c.f = fn;
  L->top -= n;
  while (n--)
    setobj2n(L, &cl->c.upvalue[n], L->top+n);
  setclvalue(L, L->top, cl);
  pve_assert(iswhite(obj2gco(cl)));
  api_incr_top(L);
  pve_unlock(L);
}


pve_API void pve_pushboolean (pve_State *L, int b) {
  pve_lock(L);
  setbvalue(L->top, (b != 0));  /* ensure that true is 1 */
  api_incr_top(L);
  pve_unlock(L);
}


pve_API void pve_pushlightuserdata (pve_State *L, void *p) {
  pve_lock(L);
  setpvalue(L->top, p);
  api_incr_top(L);
  pve_unlock(L);
}


pve_API int pve_pushthread (pve_State *L) {
  pve_lock(L);
  setthvalue(L, L->top, L);
  api_incr_top(L);
  pve_unlock(L);
  return (G(L)->mainthread == L);
}



/*
** get functions (pve -> stack)
*/


pve_API void pve_gettable (pve_State *L, int idx) {
  StkId t;
  pve_lock(L);
  t = index2adr(L, idx);
  api_checkvalidindex(L, t);
  pveV_gettable(L, t, L->top - 1, L->top - 1);
  pve_unlock(L);
}


pve_API void pve_getfield (pve_State *L, int idx, const char *k) {
  StkId t;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -