Example #1
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;
		}
Example #2
0
 private static int GetBoolField(LuaState L, CharPtr key)
 {
     int res;
       LuaGetField(L, -1, key);
       res = LuaIsNil(L, -1) ? -1 : LuaToBoolean(L, -1);
       LuaPop(L, 1);
       return res;
 }
Example #3
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;
 }
Example #4
0
 private static void DumpBlock(CharPtr b, uint size, DumpState D)
 {
     if (D.status==0)
      {
       LuaUnlock(D.L);
       D.status=D.writer(D.L,b,size,D.data);
       LuaLock(D.L);
      }
 }
Example #5
0
		private static void TreatStackOption (LuaState L, LuaState L1, CharPtr fname) {
		  if (L == L1) {
			LuaPushValue(L, -2);
			LuaRemove(L, -3);
		  }
		  else
			LuaXMove(L1, L, 1);
		  LuaSetField(L, -2, fname);
		}
Example #6
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);
 }
Example #7
0
 public static void LuaXLexError(LexState ls, CharPtr msg, int token)
 {
     CharPtr buff = new char[MAXSRC];
     LuaOChunkID(buff, GetStr(ls.source), MAXSRC);
     msg = LuaOPushFString(ls.L, "%s:%d: %s", buff, ls.linenumber, msg);
     if (token != 0)
         LuaOPushFString(ls.L, "%s near " + LUA_QS, msg, TextToken(ls, token));
     LuaDThrow(ls.L, LUA_ERRSYNTAX);
 }
Example #8
0
		private static int OSPushResult (LuaState L, int i, CharPtr filename) {
		  int en = errno();  /* calls to Lua API may change this value */
		  if (i != 0) {
			LuaPushBoolean(L, 1);
			return 1;
		  }
		  else {
			LuaPushNil(L);
			LuaPushFString(L, "%s: %s", filename, strerror(en));
			LuaPushInteger(L, en);
			return 3;
		  }
		}
Example #9
0
		private static int os_pushresult (lua_State L, int i, CharPtr filename) {
		  int en = errno();  /* calls to Lua API may change this value */
		  if (i != 0) {
			lua_pushboolean(L, 1);
			return 1;
		  }
		  else {
			lua_pushnil(L);
			lua_pushfstring(L, "%s: %s", filename, strerror(en));
			lua_pushinteger(L, en);
			return 3;
		  }
		}
Example #10
0
 private static int GetField(LuaState L, CharPtr key, int d)
 {
     int res;
       LuaGetField(L, -1, key);
       if (LuaIsNumber(L, -1) != 0)
     res = (int)LuaToInteger(L, -1);
       else {
     if (d < 0)
       return LuaLError(L, "field " + LUA_QS + " missing in date table", key);
     res = d;
       }
       LuaPop(L, 1);
       return res;
 }
Example #11
0
 private static int getfield(lua_State L, CharPtr key, int d)
 {
     int res;
       lua_getfield(L, -1, key);
       if (lua_isnumber(L, -1) != 0)
     res = (int)lua_tointeger(L, -1);
       else {
     if (d < 0)
       return luaL_error(L, "field " + LUA_QS + " missing in date table", key);
     res = d;
       }
       lua_pop(L, 1);
       return res;
 }
Example #12
0
 public static TString luaS_newlstr(lua_State L, CharPtr str, uint l)
 {
     GCObject o;
       uint h = (uint)l;  /* seed */
       uint step = (l>>5)+1;  /* if string is too long, don't hash all its chars */
       uint l1;
       for (l1=l; l1>=step; l1-=step)  /* compute hash */
     h = h ^ ((h<<5)+(h>>2)+(byte)str[l1-1]);
       for (o = G(L).strt.hash[lmod(h, G(L).strt.size)];
        o != null;
        o = o.gch.next) {
     TString ts = rawgco2ts(o);
     if (ts.tsv.len == l && (memcmp(str, getstr(ts), l) == 0)) {
       /* string may be dead */
       if (isdead(G(L), o)) changewhite(o);
       return ts;
     }
       }
       //return newlstr(L, str, l, h);  /* not found */
       TString res = newlstr(L, str, l, h);
       return res;
 }
Example #13
0
 public static void lua_getfield(lua_State L, int idx, CharPtr k)
 {
     StkId t;
       TValue key = new TValue();
       lua_lock(L);
       t = index2adr(L, idx);
       api_checkvalidindex(L, t);
       setsvalue(L, key, luaS_new(L, k));
       luaV_gettable(L, t, key, L.top);
       api_incr_top(L);
       lua_unlock(L);
 }
Example #14
0
 public static uint luaZ_read(ZIO z, CharPtr b, uint n)
 {
     b = new CharPtr(b);
     while (n != 0)
     {
         uint m;
         if (luaZ_lookahead(z) == EOZ)
             return n;  // return number of missing bytes
         m = (n <= z.n) ? n : z.n;  // min. between n and z.n
         memcpy(b, z.p, m);
         z.n -= m;
         z.p += m;
         b = b + m;
         n -= m;
     }
     return 0;
 }
Example #15
0
		public static TString luaS_newliteral(lua_State L, CharPtr s) { return luaS_newlstr(L, s, (uint)strlen(s)); }
Example #16
0
 public static int LuaOStr2d(CharPtr s, out LuaNumberType result)
 {
     CharPtr endptr;
       result = lua_str2number(s, out endptr);
       if (endptr == s) return 0;  /* conversion failed */
       if (endptr[0] == 'x' || endptr[0] == 'X')  /* maybe an hexadecimal constant? */
     result = CastNum(strtoul(s, out endptr, 16));
       if (endptr[0] == '\0') return 1;  /* most common case */
       while (isspace(endptr[0])) endptr = endptr.next();
       if (endptr[0] != '\0') return 0;  /* invalid trailing characters? */
       return 1;
 }
Example #17
0
 public static CharPtr lua_pushvfstring(lua_State L, CharPtr fmt,
     object[] argp)
 {
     CharPtr ret;
       lua_lock(L);
       luaC_checkGC(L);
       ret = luaO_pushvfstring(L, fmt, argp);
       lua_unlock(L);
       return ret;
 }
Example #18
0
 public static void lua_pushlstring(lua_State L, CharPtr s, uint len)
 {
     lua_lock(L);
       luaC_checkGC(L);
       setsvalue2s(L, L.top, luaS_newlstr(L, s, len));
       api_incr_top(L);
       lua_unlock(L);
 }
Example #19
0
        /*
        * make header
        */
        public static void luaU_header(CharPtr h)
        {
            h = new CharPtr(h);
             int x=1;
             memcpy(h, LUA_SIGNATURE, LUA_SIGNATURE.Length);
             h = h.add(LUA_SIGNATURE.Length);
             h[0] = (char)LUAC_VERSION;
             h.inc();
             h[0] = (char)LUAC_FORMAT;
             h.inc();
             //*h++=(char)*(char*)&x;				/* endianness */
             h[0] = (char)x;						/* endianness */
             h.inc();
             h[0] = (char)sizeof(int);
             h.inc();
             h[0] = (char)sizeof(uint);
             h.inc();
             h[0] = (char)sizeof(Instruction);
             h.inc();
             h[0] = (char)sizeof(lua_Number);
             h.inc();

              //(h++)[0] = ((lua_Number)0.5 == 0) ? 0 : 1;		/* is lua_Number integral? */
             h[0] = (char)0;	// always 0 on this build
        }
Example #20
0
		private static int os_date (lua_State L) {
		  CharPtr s = new CharPtr(luaL_optstring(L, 1, "%c"));
		  DateTime stm;
		  if (s[0] == '!') {  /* UTC? */
			stm = DateTime.UtcNow;
			s.inc();  /* skip `!' */
		  }
		  else
			  stm = DateTime.Now;
		  if (strcmp(s, "*t") == 0) {
			lua_createtable(L, 0, 9);  /* 9 = number of fields */
			setfield(L, "sec", stm.Second);
			setfield(L, "min", stm.Minute);
			setfield(L, "hour", stm.Hour);
			setfield(L, "day", stm.Day);
			setfield(L, "month", stm.Month);
			setfield(L, "year", stm.Year);
			setfield(L, "wday", (int)stm.DayOfWeek);
			setfield(L, "yday", stm.DayOfYear);
			setboolfield(L, "isdst", stm.IsDaylightSavingTime() ? 1 : 0);
		  }
		  else {
			  luaL_error(L, "strftime not implemented yet"); // todo: implement this - mjf
#if false
			CharPtr cc = new char[3];
			luaL_Buffer b;
			cc[0] = '%'; cc[2] = '\0';
			luaL_buffinit(L, b);
			for (; s[0] != 0; s.inc()) {
			  if (s[0] != '%' || s[1] == '\0')  /* no conversion specifier? */
				luaL_addchar(b, s[0]);
			  else {
				uint reslen;
				CharPtr buff = new char[200];  /* should be big enough for any conversion result */
				s.inc();
				cc[1] = s[0];
				reslen = strftime(buff, buff.Length, cc, stm);
				luaL_addlstring(b, buff, reslen);
			  }
			}
			luaL_pushresult(b);
#endif // #if 0
		  }
			return 1;
		}
Example #21
0
		private static void setboolfield (lua_State L, CharPtr key, int value) {
		  if (value < 0)  /* undefined? */
			return;  /* does not set field */
		  lua_pushboolean(L, value);
		  lua_setfield(L, -2, key);
		}
Example #22
0
		/*
		** {======================================================
		** Time/Date operations
		** { year=%Y, month=%m, day=%d, hour=%H, min=%M, sec=%S,
		**   wday=%w+1, yday=%j, isdst=? }
		** =======================================================
		*/

		private static void setfield (lua_State L, CharPtr key, int value) {
		  lua_pushinteger(L, value);
		  lua_setfield(L, -2, key);
		}
Example #23
0
 private static void PushStr(LuaState L, CharPtr str)
 {
     SetSValue2S(L, L.top, luaS_new(L, str));
       IncrTop(L);
 }
Example #24
0
 public static int lua_load(lua_State L, lua_Reader reader, object data,
     CharPtr chunkname)
 {
     ZIO z = new ZIO();
       int status;
       lua_lock(L);
       if (chunkname == null) chunkname = "?";
       luaZ_init(L, z, reader, data);
       status = luaD_protectedparser(L, z, chunkname);
       lua_unlock(L);
       return status;
 }
Example #25
0
 public static CharPtr lua_pushfstring(lua_State L, CharPtr fmt)
 {
     CharPtr ret;
     lua_lock(L);
     luaC_checkGC(L);
     ret = luaO_pushvfstring(L, fmt, null);
     lua_unlock(L);
     return ret;
 }
Example #26
0
 /*
 ** load precompiled chunk
 */
 public static Proto luaU_undump(lua_State L, ZIO Z, Mbuffer buff, CharPtr name)
 {
     LoadState S = new LoadState();
      if (name[0] == '@' || name[0] == '=')
       S.name = name+1;
      else if (name[0]==LUA_SIGNATURE[0])
       S.name="binary string";
      else
       S.name=name;
      S.L=L;
      S.Z=Z;
      S.b=buff;
      LoadHeader(S);
      return LoadFunction(S,luaS_newliteral(L,"=?"));
 }
Example #27
0
 public static void lua_pushstring(lua_State L, CharPtr s)
 {
     if (s == null)
     lua_pushnil(L);
       else
     lua_pushlstring(L, s, (uint)strlen(s));
 }
Example #28
0
 static void error(LoadState S, CharPtr why)
 {
     luaO_pushfstring(S.L,"%s: %s in precompiled chunk",S.name,why);
      luaD_throw(S.L,LUA_ERRSYNTAX);
 }
Example #29
0
 public static void lua_setfield(lua_State L, int idx, CharPtr k)
 {
     StkId t;
       TValue key = new TValue();
       lua_lock(L);
       api_checknelems(L, 1);
       t = index2adr(L, idx);
       api_checkvalidindex(L, t);
       setsvalue(L, key, luaS_new(L, k));
       luaV_settable(L, t, key, L.top - 1);
       StkId.dec(ref L.top);  /* pop value */
       lua_unlock(L);
 }
Example #30
0
 private static void LoadBlock(LoadState S, CharPtr b, int size)
 {
     uint r=luaZ_read(S.Z, b, (uint)size);
      IF (r!=0, "unexpected end");
 }