Exemple #1
0
 // we have a gc, so nothing to do
 public static void luaF_freeclosure(lua_State L, Closure c)
 {
     int size = (c.c.isC != 0) ? sizeCclosure(c.c.nupvalues) :
                           sizeLclosure(c.l.nupvalues);
       //luaM_freemem(L, c, size);
       SubtractTotalBytes(L, size);
 }
Exemple #2
0
 public static void luaH_free(lua_State L, Table t)
 {
     if (t.node[0] != dummynode)
     luaM_freearray(L, t.node);
       luaM_freearray(L, t.array);
       luaM_free(L, t);
 }
Exemple #3
0
 public static UpVal luaF_findupval(lua_State L, StkId level)
 {
     global_State g = G(L);
       GCObjectRef pp = new OpenValRef(L);
       UpVal p;
       UpVal uv;
       while (pp.get() != null && (p = ngcotouv(pp.get())).v >= level) {
     lua_assert(p.v != p.u.value);
     if (p.v == level) {  /* found a corresponding upvalue? */
       if (isdead(g, obj2gco(p)))  /* is it dead? */
         changewhite(obj2gco(p));  /* ressurect it */
       return p;
     }
     pp = new NextRef(p);
       }
       uv = luaM_new<UpVal>(L);  /* not found: create a new one */
       uv.tt = LUA_TUPVAL;
       uv.marked = luaC_white(g);
       uv.v = level;  /* current value lives in the stack */
       uv.next = pp.get();  /* chain it in the proper position */
       pp.set( obj2gco(uv) );
       uv.u.l.prev = g.uvhead;  /* double link it in `uvhead' list */
       uv.u.l.next = g.uvhead.u.l.next;
       uv.u.l.next.u.l.prev = uv;
       g.uvhead.u.l.next = uv;
       lua_assert(uv.u.l.next.u.l.prev == uv && uv.u.l.prev.u.l.next == uv);
       return uv;
 }
Exemple #4
0
 public static void luaG_aritherror(lua_State L, TValue p1, TValue p2)
 {
     TValue temp = new TValue();
       if (luaV_tonumber(p1, temp) == null)
     p2 = p1;  /* first operand is wrong */
       luaG_typeerror(L, p2, "perform arithmetic on");
 }
Exemple #5
0
 private static int db_getmetatable (lua_State L) {
     luaL_checkany(L, 1);
     if (lua_getmetatable(L, 1) == 0) {
         lua_pushnil(L);  /* no metatable */
     }
     return 1;
 }
Exemple #6
0
		public static void luaS_resize (lua_State L, int newsize) {
		  GCObject[] newhash;
		  stringtable tb;
		  int i;
		  if (G(L).gcstate == GCSsweepstring)
			return;  /* cannot resize during GC traverse */		  
		  newhash = new GCObject[newsize];
		  AddTotalBytes(L, newsize * GetUnmanagedSize(typeof(GCObjectRef)));
		  tb = G(L).strt;
		  for (i=0; i<newsize; i++) newhash[i] = null;

		  /* rehash */
		  for (i=0; i<tb.size; i++) {
			GCObject p = tb.hash[i];
			while (p != null) {  /* for each node in the list */
			  GCObject next = p.gch.next;  /* save next */
			  uint h = gco2ts(p).hash;
			  int h1 = (int)lmod(h, newsize);  /* new position */
			  lua_assert((int)(h%newsize) == lmod(h, newsize));
			  p.gch.next = newhash[h1];  /* chain it */
			  newhash[h1] = p;
			  p = next;
			}
		  }
		  //luaM_freearray(L, tb.hash);
		  if (tb.hash != null)
			  SubtractTotalBytes(L, tb.hash.Length * GetUnmanagedSize(typeof(GCObjectRef)));
		  tb.size = newsize;
		  tb.hash = newhash;
		}
Exemple #7
0
		public static TString newlstr (lua_State L, CharPtr str, uint l,
											   uint h) {
		  TString ts;
		  stringtable tb;
		  if (l+1 > MAX_SIZET /GetUnmanagedSize(typeof(char)))
		    luaM_toobig(L);
		  ts = new TString(new char[l+1]);
		  AddTotalBytes(L, (int)(l + 1) * GetUnmanagedSize(typeof(char)) + GetUnmanagedSize(typeof(TString)));
		  ts.tsv.len = l;
		  ts.tsv.hash = h;
		  ts.tsv.marked = luaC_white(G(L));
		  ts.tsv.tt = LUA_TSTRING;
		  ts.tsv.reserved = 0;
		  //memcpy(ts+1, str, l*GetUnmanagedSize(typeof(char)));
		  memcpy(ts.str.chars, str.chars, str.index, (int)l);
		  ts.str[l] = '\0';  /* ending 0 */
		  tb = G(L).strt;
		  h = (uint)lmod(h, tb.size);
		  ts.tsv.next = tb.hash[h];  /* chain new entry */
		  tb.hash[h] = obj2gco(ts);
		  tb.nuse++;
		  if ((tb.nuse > (int)tb.size) && (tb.size <= MAX_INT/2))
		    luaS_resize(L, tb.size*2);  /* too crowded */
		  return ts;
		}
 private static int os_clock(lua_State L)
 {
     TimeSpan elapsed = DateTime.Now - start_time;
       long ticks = elapsed.Ticks / TimeSpan.TicksPerMillisecond;
       lua_pushnumber(L, ((lua_Number)ticks)/(lua_Number)1000);
       return 1;
 }
Exemple #9
0
 public static CallInfo inc_ci(lua_State L)
 {
     if (L.ci == L.end_ci) return growCI(L);
     //   (condhardstacktests(luaD_reallocCI(L, L.size_ci)), ++L.ci))
     CallInfo.inc(ref L.ci);
     return L.ci;
 }
Exemple #10
0
 private static int getboolfield(lua_State L, CharPtr key)
 {
     int res;
       lua_getfield(L, -1, key);
       res = lua_isnil(L, -1) ? -1 : lua_toboolean(L, -1);
       lua_pop(L, 1);
       return res;
 }
Exemple #11
0
 private static void addfield(lua_State L, luaL_Buffer b, int i)
 {
     lua_rawgeti(L, 1, i);
       if (lua_isstring(L, -1)==0)
     luaL_error(L, "invalid value (%s) at index %d in table for " +
                   LUA_QL("concat"), luaL_typename(L, -1), i);
     luaL_addvalue(b);
 }
Exemple #12
0
 private static int db_setmetatable (lua_State L) {
     int t = lua_type(L, 2);
     luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2,
                   "nil or table expected");
     lua_settop(L, 2);
     lua_pushboolean(L, lua_setmetatable(L, 1));
     return 1;
 }
Exemple #13
0
 private static int db_setfenv (lua_State L) {
     luaL_checktype(L, 2, LUA_TTABLE);
     lua_settop(L, 2);
     if (lua_setfenv(L, 1) == 0)
         luaL_error(L, LUA_QL("setfenv") +
                    " cannot change environment of given object");
     return 1;
 }
Exemple #14
0
		private static int os_tmpname (lua_State L) {
#if XBOX
		  luaL_error(L, "os_tmpname not supported on Xbox360");
#else
		  lua_pushstring(L, Path.GetTempFileName());
#endif
		  return 1;
		}
Exemple #15
0
 public static void luaZ_init(lua_State L, ZIO z, lua_Reader reader, object data)
 {
     z.L = L;
       z.reader = reader;
       z.data = data;
       z.n = 0;
       z.p = null;
 }
Exemple #16
0
 private static void treatstackoption (lua_State L, lua_State L1, CharPtr fname) {
     if (L == L1) {
         lua_pushvalue(L, -2);
         lua_remove(L, -3);
     }
     else
         lua_xmove(L1, L, 1);
     lua_setfield(L, -2, fname);
 }
Exemple #17
0
 public static lua_CFunction lua_atpanic(lua_State L, lua_CFunction panicf)
 {
     lua_CFunction old;
       lua_lock(L);
       old = G(L).panic;
       G(L).panic = panicf;
       lua_unlock(L);
       return old;
 }
Exemple #18
0
 private static int str_reverse (lua_State L) {
     uint l;
     luaL_Buffer b = new luaL_Buffer();
     CharPtr s = luaL_checklstring(L, 1, out l);
     luaL_buffinit(L, b);
     while ((l--) != 0) luaL_addchar(b, s[l]);
     luaL_pushresult(b);
     return 1;
 }
Exemple #19
0
 public static int luaV_tostring (lua_State L, StkId obj) {
     if (!ttisnumber(obj))
         return 0;
     else {
         lua_Number n = nvalue(obj);
         CharPtr s = lua_number2str(n);
         setsvalue2s(L, obj, luaS_new(L, s));
         return 1;
     }
 }
Exemple #20
0
 public static void luaL_openlibs(lua_State L)
 {
     for (int i=0; i<lualibs.Length-1; i++)
       {
     luaL_Reg lib = lualibs[i];
     lua_pushcfunction(L, lib.func);
     lua_pushstring(L, lib.name);
     lua_call(L, 1, 0);
       }
 }
Exemple #21
0
 private static lua_State getthread (lua_State L, out int arg) {
     if (lua_isthread(L, 1)) {
         arg = 1;
         return lua_tothread(L, 1);
     }
     else {
         arg = 0;
         return L;
     }
 }
Exemple #22
0
 /*
 ** Open string library
 */
 public static int luaopen_string(lua_State L)
 {
     luaL_register(L, LUA_STRLIBNAME, strlib);
     #if LUA_COMPAT_GFIND
       lua_getfield(L, -1, "gmatch");
       lua_setfield(L, -2, "gfind");
     #endif
       createmetatable(L);
       return 1;
 }
Exemple #23
0
 public static void luaD_checkstack(lua_State L, int n) {
     if ((L.stack_last - L.top) <= n)
         luaD_growstack(L, n);
     else
     {
         #if HARDSTACKTESTS
         luaD_reallocstack(L, L.stacksize - EXTRA_STACK - 1);
         #endif
     }
 }
Exemple #24
0
 public static void luaF_freeproto(lua_State L, Proto f)
 {
     luaM_freearray<Instruction>(L, f.code);
       luaM_freearray<Proto>(L, f.p);
       luaM_freearray<TValue>(L, f.k);
       luaM_freearray<Int32>(L, f.lineinfo);
       luaM_freearray<LocVar>(L, f.locvars);
       luaM_freearray<TString>(L, f.upvalues);
       luaM_free(L, f);
 }
Exemple #25
0
 public static void luaX_init(lua_State L)
 {
     int i;
       for (i=0; i<NUM_RESERVED; i++) {
     TString ts = luaS_new(L, luaX_tokens[i]);
     luaS_fix(ts);  /* reserved words are never collected */
     lua_assert(luaX_tokens[i].Length+1 <= TOKEN_LEN);
     ts.tsv.reserved = cast_byte(i+1);  /* reserved word */
       }
 }
Exemple #26
0
 private static int str_upper (lua_State L) {
     uint l;
     uint i;
     luaL_Buffer b = new luaL_Buffer();
     CharPtr s = luaL_checklstring(L, 1, out l);
     luaL_buffinit(L, b);
     for (i=0; i<l; i++)
         luaL_addchar(b, toupper(s[i]));
     luaL_pushresult(b);
     return 1;
 }
Exemple #27
0
 private static int auxupvalue(lua_State L, int get)
 {
     CharPtr name;
       int n = luaL_checkint(L, 2);
       luaL_checktype(L, 1, LUA_TFUNCTION);
       if (lua_iscfunction(L, 1)) return 0;  /* cannot touch C upvalues from Lua */
       name = (get!=0) ? lua_getupvalue(L, 1, n) : lua_setupvalue(L, 1, n);
       if (name == null) return 0;
       lua_pushstring(L, name);
       lua_insert(L, -(get+1));
       return get + 1;
 }
Exemple #28
0
 public static void luaG_errormsg(lua_State L)
 {
     if (L.errfunc != 0) {  /* is there an error handling function? */
     StkId errfunc = restorestack(L, L.errfunc);
     if (!ttisfunction(errfunc)) luaD_throw(L, LUA_ERRERR);
     setobjs2s(L, L.top, L.top - 1);  /* move argument */
     setobjs2s(L, L.top - 1, errfunc);  /* push function */
     incr_top(L);
     luaD_call(L, L.top - 2, 1);  /* call it */
       }
       luaD_throw(L, LUA_ERRRUN);
 }
Exemple #29
0
 private static int str_sub (lua_State L) {
     uint l;
     CharPtr s = luaL_checklstring(L, 1, out l);
     ptrdiff_t start = posrelat(luaL_checkinteger(L, 2), l);
     ptrdiff_t end = posrelat(luaL_optinteger(L, 3, -1), l);
     if (start < 1) start = 1;
     if (end > (ptrdiff_t)l) end = (ptrdiff_t)l;
     if (start <= end)
         lua_pushlstring(L, s+start-1, (uint)(end-start+1));
     else lua_pushliteral(L, "");
     return 1;
 }
Exemple #30
0
		{
		 if (D.status==0)
		 {
		  lua_unlock(D.L);
		  D.status=D.writer(D.L,b,size,D.data);
		  lua_lock(D.L);
		 }
		}

		private static void DumpChar(int y, DumpState D)
		{
		 char x=(char)y;
Exemple #31
0
 public static T[] luaM_reallocvector <T>(lua_State L, ref T[] v, int oldn, int n)
 {
     Debug.Assert((v == null && oldn == 0) || (v.Length == oldn));
     v = luaM_reallocv <T>(L, v, n);
     return(v);
 }
 partial void SetEventStatus(lua_State lua);
 public static CharPtr luaL_typename(lua_State L, int i)
 {
     return(lua_typename(L, lua_type(L, i)));
 }
 public static lua_Number luaL_opt(lua_State L, luaL_opt_delegate f, int n, lua_Number d)
 {
     return(lua_isnoneornil(L, n) ? d : f(L, n));
 }
 public static lua_Unsigned luaL_opt_unsigned(lua_State L, luaL_opt_delegate_unsigned f, int n, lua_Unsigned u)
 {
     return((lua_Unsigned)(lua_isnoneornil(L, n) ? u : f(L, (n))));
 }
        /* }====================================================== */



        /* compatibility with old module system */
        //#if defined(LUA_COMPAT_MODULE)

        public static void luaL_register(lua_State L, CharPtr n, luaL_Reg[] l)
        {
            luaL_openlib(L, n, l, 0);
        }
 public static int luaL_loadfile(lua_State L, CharPtr f)
 {
     return(luaL_loadfilex(L, f, null));
 }
Exemple #38
0
 static void SubtractTotalBytes(lua_State L, uint num_bytes)
 {
     G(L).totalbytes -= num_bytes;
 }
 public static CharPtr luaL_checkstring(lua_State L, int n)
 {
     return(luaL_checklstring(L, n));
 }
Exemple #40
0
 public static void luaM_free <T>(lua_State L, T b)
 {
     luaM_realloc_ <T>(L, new T[] { b }, 0);
 }
Exemple #41
0
 public static void luaM_freearray <T>(lua_State L, T[] b)
 {
     luaM_reallocv(L, b, 0);
 }
Exemple #42
0
 public static T luaM_new <T>(lua_State L)
 {
     return((T)luaM_realloc_ <T>(L));
 }
 static partial void GetInstance(lua_State lua);
Exemple #44
0
        /*
        ** lua_popen spawns a new process connected to the current one through
        ** the file streams.
        */
        //#if !defined(lua_popen)	/* { */

        //#if defined(LUA_USE_POPEN)	/* { */

        //#define lua_popen(L,c,m)        ((void)L, fflush(NULL), popen(c,m))
        //#define lua_pclose(L,file)      ((void)L, pclose(file))

        //#elif defined(LUA_WIN)		/* }{ */

        private static StreamProxy lua_popen(lua_State L, CharPtr c, CharPtr m) /*(void)L,*/ return
        {
            (_popen(c, m));
        }                                                    //FIXME: changed, sizeof(l)/sizeof((l)[0]) - 1)

        public static void luaL_newlib(lua_State L, luaL_Reg[] l)
        {
            luaL_newlibtable(L, l); luaL_setfuncs(L, l, 0);
        }
Exemple #46
0
 static void AddTotalBytes(lua_State L, uint num_bytes)
 {
     G(L).totalbytes += num_bytes;
 }
        /*
        ** ===============================================================
        ** some useful macros
        ** ===============================================================
        */


        public static void luaL_newlibtable(lua_State L, luaL_Reg[] l)
        {
            lua_createtable(L, 0, l.Length - 1);
        }                                                    //FIXME: changed, sizeof(l)/sizeof((l)[0]) - 1)
Exemple #48
0
 public static T[] luaM_reallocv <T>(lua_State L, T[] block, int new_size)
 {
     return((T[])luaM_realloc_(L, block, new_size));
 }
 public static void luaL_checkversion(lua_State L)
 {
     luaL_checkversion_(L, LUA_VERSION_NUM);
 }
Exemple #50
0
 public static object luaM_toobig(lua_State L)
 {
     luaG_runerror(L, "memory allocation error: block too big");
     return(null);       /* to avoid warnings */
 }
 public static int luaL_loadbuffer(lua_State L, CharPtr s, uint sz, CharPtr n)
 {
     return(luaL_loadbufferx(L, s, sz, n, null));
 }
 public static long luaL_checklong(lua_State L, int n)
 {
     return(luaL_checkinteger(L, n));
 }
 public static lua_Integer luaL_opt_integer(lua_State L, luaL_opt_delegate_integer f, int n, lua_Integer d)
 {
     return(lua_isnoneornil(L, n) ? d : f(L, n));
 }
 public static int luaL_optint(lua_State L, int n, lua_Integer d)
 {
     return((int)luaL_optinteger(L, n, d));
 }
        //#define luaL_dofile(L, fn) \
        //    (luaL_loadfile(L, fn) || lua_pcall(L, 0, LUA_MULTRET, 0))

        //#define luaL_dostring(L, s) \
        //    (luaL_loadstring(L, s) || lua_pcall(L, 0, LUA_MULTRET, 0))

        public static void luaL_getmetatable(lua_State L, CharPtr n)
        {
            lua_getfield(L, LUA_REGISTRYINDEX, n);
        }
 public static int luaL_checkint(lua_State L, int n)
 {
     return((int)luaL_checkinteger(L, n));
 }
 public static long luaL_optlong(lua_State L, int n, lua_Integer d)
 {
     return(luaL_optinteger(L, n, d));
 }
 public static CharPtr luaL_optstring(lua_State L, int n, CharPtr d)
 {
     uint len; return(luaL_optlstring(L, n, d, out len));
 }
Exemple #59
0
 //#define setsvalue2n	setsvalue
 public static void setsvalue2n(lua_State L, TValue obj, TString x)
 {
     setsvalue(L, obj, x);
 }
Exemple #60
0
 public static T[] luaM_newvector <T>(lua_State L, int n)
 {
     return(luaM_reallocv <T>(L, null, n));
 }