Skip to content

Commit

Permalink
Added __scope table things, math.factorial, some new compiler updates…
Browse files Browse the repository at this point in the history
… (its still horribly broken though...)
  • Loading branch information
efrederickson committed Nov 10, 2012
1 parent 150e267 commit e58efd5
Show file tree
Hide file tree
Showing 16 changed files with 174 additions and 40 deletions.
2 changes: 1 addition & 1 deletion NuGetPackage/CreateNuGetPackage.bat
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mkdir net40
cd net40

:: copy binaries
copy /Y ..\..\..\..\SharpLua\bin\debug\SharpLua.dll .
copy /Y ..\..\..\..\bin\SharpLua.dll .

:: create NuGet package
cd ..\..
Expand Down
4 changes: 2 additions & 2 deletions NuGetPackage/Package/SharpLua.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
<package >
<metadata>
<id>SharpLua</id>
<version>2.1.1.0</version>
<version>2.1.1.1</version>
<authors>mlnlover11</authors>
<owners>mlnlover11</owners>
<projectUrl>https://github.com/mlnlover11/SharpLua</projectUrl>
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Lua implementation in C#</description>
<releaseNotes>Major upgrade</releaseNotes>
<releaseNotes></releaseNotes>
<copyright>Copyright 2012 LoDC</copyright>
<tags>Lua</tags>
</metadata>
Expand Down
4 changes: 4 additions & 0 deletions SharpLua.Interactive/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,11 @@ public static void Main(string[] args)
{
object[] v = LuaRuntime.Run(line);
if (v == null || v.Length == 0)
#if DEBUG
Console.WriteLine("=> [no returned value]");
#else
;
#endif
else
{
Console.Write("=> ");
Expand Down
16 changes: 16 additions & 0 deletions SharpLua/LuaCore/Libraries/lmathlib.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,21 @@ private static int math_max(LuaState L)
return 1;
}

static int factorial(int n)
{
if (n <= 1)
return n;
else
return n * factorial(n - 1);
}

private static int math_factorial(LuaState L)
{
int num = luaL_checkint(L, 1);
lua_pushnumber(L, factorial(num));
return 1;
}

private static Random rng = new Random();

private static int math_random(LuaState L)
Expand Down Expand Up @@ -274,6 +289,7 @@ private static int math_randomseed(LuaState L)
new luaL_Reg("sqrt", math_sqrt),
new luaL_Reg("tanh", math_tanh),
new luaL_Reg("tan", math_tan),
new luaL_Reg("factorial", math_factorial),
new luaL_Reg(null, null)
};

Expand Down
2 changes: 1 addition & 1 deletion SharpLua/LuaCore/VM/ldebug.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static int pcRel(InstructionPtr pc, Proto p)
Debug.Assert(pc.codes == p.code);
return pc.pc - 1;
}
public static int getline(Proto f, int pc) { return (f.lineinfo != null) ? f.lineinfo[pc] : 0; }
public static int getline(Proto f, int pc) { return (f.lineinfo != null && f.lineinfo.Length - 1 >= pc) ? f.lineinfo[pc] : 0; }
public static void resethookcount(LuaState L) { L.hookcount = L.basehookcount; }


Expand Down
10 changes: 9 additions & 1 deletion SharpLua/LuaCore/VM/ldo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,21 @@ public static int luaD_rawrunprotected(LuaState L, Pfunc f, object ud)
f(L, ud);
}
#if CATCH_EXCEPTIONS
catch (Exception ex)
catch (LuaException ex)
{
Debug.WriteLine("Caught exception: " + ex.ToString());

if (lj.status == 0)
lj.status = -1;
}
/*catch (Exception ex)
{
Debug.WriteLine("Caught exception: " + ex.ToString());
throw ex;
//if (lj.status == 0)
// lj.status = -1;
}*/
#endif
L.errorJmp = lj.previous; /* restore old error handler */
return lj.status;
Expand Down
106 changes: 91 additions & 15 deletions SharpLua/LuaCore/VM/lvm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,9 @@ public static int equalobj(LuaState L, TValue o1, TValue o2)
return ((ttype(o1) == ttype(o2)) && (luaV_equalval(L, o1, o2) != 0)) ? 1 : 0;
}


/* limit for table tag-method chains (to avoid loops) */
public const int MAXTAGLOOP = 100;


public static TValue luaV_tonumber(TValue obj, TValue n)
{
lua_Number num;
Expand All @@ -53,7 +51,6 @@ public static TValue luaV_tonumber(TValue obj, TValue n)
return null;
}


public static int luaV_tostring(LuaState L, StkId obj)
{
if (!ttisnumber(obj))
Expand All @@ -67,7 +64,6 @@ public static int luaV_tostring(LuaState L, StkId obj)
}
}


private static void traceexec(LuaState L, InstructionPtr pc)
{
lu_byte mask = L.hookmask;
Expand Down Expand Up @@ -136,6 +132,10 @@ public static void luaV_gettable(LuaState L, TValue t, TValue key, StkId val)
}
else // __GETINDEX meta function
{
// This makes __getindex the get all end all of table functions.
// If someone wanted to lock the key "a", __getindex could return nil
// for a key of "a", ending all searches there.
// Otherwise, return rawget(t, k, v) ...
goto callfunc;
}

Expand All @@ -155,7 +155,7 @@ public static void luaV_gettable(LuaState L, TValue t, TValue key, StkId val)
setobj2s(L, val, res);
return;
}
*/
*/
/* else will try the tag method */
}
else if (ttisnil(tm = luaT_gettmbyobj(L, t, TMS.TM_INDEX)))
Expand All @@ -175,14 +175,15 @@ public static void luaV_gettable(LuaState L, TValue t, TValue key, StkId val)

public static void luaV_settable(LuaState L, TValue t, TValue key, StkId val)
{
Table h = null;
int loop;
TValue temp = null;
for (loop = 0; loop < MAXTAGLOOP; loop++)
{
TValue tm;
if (ttistable(t))
{ /* `t' is a table? */
Table h = hvalue(t);
h = hvalue(t);
TValue oldval = luaH_set(L, h, key); /* do a primitive set */
if (!ttisnil(oldval)) /* old is not nil? */
{
Expand All @@ -191,6 +192,7 @@ public static void luaV_settable(LuaState L, TValue t, TValue key, StkId val)
setobj2t(L, oldval, val); /* write barrier */
h.flags = 0;
luaC_barriert(L, h, val);
CallMTChanged(L, h, t, key, val);
return;
}
else
Expand All @@ -207,6 +209,7 @@ public static void luaV_settable(LuaState L, TValue t, TValue key, StkId val)
h.flags = 0;
luaC_barriert(L, h, val);
/* write barrier */
CallMTChanged(L, h, t, key, val);
return;
}
else
Expand All @@ -224,18 +227,95 @@ public static void luaV_settable(LuaState L, TValue t, TValue key, StkId val)
if (ttisfunction(tm))
{
callTM(L, tm, t, key, val);
CallMTChanged(L, h, t, key, val);
return;
}
//t = tm;
/* else repeat with `tm' */
// TODO: potential issue here, it occured all of one time.
setobj(L, temp, tm); /* avoid pointing inside table (may rehash) */
t = temp;

}
luaG_runerror(L, "loop in settable");
}

private static void CallMTChanged(LuaState L, Table table, TValue a, TValue b, TValue c)
{
// TODO: Nested table's parents aren't updated when a subtable is changed...

if (table != null)
{
TValue tm = fasttm(L, table.metatable, TMS.TM_CHANGED);
if (tm != null)
{
callTM(L, tm, a, b, c);
}
}
}

static int gettablescope(LuaState L, TValue ra, TValue rb,
InstructionPtr pc, LClosure cl, StkId pbase)
{
/* Look for closest containing "o {....}" where o
has a __scope metamethod. */
StkId base_ = pbase;
InstructionPtr pi;
TValue tm = null;
int previousIndex = pc.pc;
for (pi = new InstructionPtr(pc.codes, pc.pc); ; pi.pc++)
{
/* Search forward for end of any "o {....}".
Note: "o {....}" is terminated by
(NEWTABLE,CALL) for empty table
(SETLIST,CALL) for table with array part or "..."
(SETTABLE,CALL) for table with hash part only
*/
if (pi.pc >= /*cl.p.code + */cl.p.sizecode)// || pi.pc >= pi.codes.Length)
break;
if (GET_OPCODE(pi.codes[pi.pc]) == OpCode.OP_CALL &&
(GET_OPCODE(pi.codes[pi.pc - 1]) == OpCode.OP_SETLIST ||
GET_OPCODE(pi.codes[pi.pc - 1]) == OpCode.OP_SETTABLE))
{
/* Determine whether this "o {....}" contains the GETGLOBAL by
searching for start of this "o {....}", which is implied
by condition RA(NEWTABLE) == RA(SETLIST or SETTABLE).
*/
InstructionPtr pi2;
bool is_containing = false;
for (pi2 = new InstructionPtr(pi.codes, pi.pc); ; pi2.pc--)
{
lua_assert(pi2.pc >= /*cl.p.code*/cl.p.sizecode);
if (GET_OPCODE(pi2.codes[pi2.pc]) == OpCode.OP_NEWTABLE &&
RA(L, base_, pi2.codes[pi2.pc]) == RA(L, base_, pi.codes[pi.pc - 1]))
{
is_containing = (pi2 < pc);
break;
}
}
/* Search complete if o has __scope metamethod (tm). */
if (is_containing &&
!ttisnil(tm = luaT_gettmbyobj(L, RA(L, base_, pi.codes[pi.pc]), TMS.TM_SCOPE)))
{
break;
}
}
//Console.WriteLine(pi.pc);
} /* pi */

pc.pc = previousIndex;
/* Lookup key first in __scope (if available). Found if non-nil. */
if (tm != null && !ttisnil(tm))
{
luaV_gettable(L, tm, rb, ra);
if (!ttisnil(ra))
{
//Console.WriteLine("Found __scope object!");
return 1;
}
}
return 0;
}


private static int call_binTM(LuaState L, TValue p1, TValue p2,
StkId res, TMS event_)
Expand All @@ -248,7 +328,6 @@ private static int call_binTM(LuaState L, TValue p1, TValue p2,
return 1;
}


private static TValue get_compTM(LuaState L, Table mt1, Table mt2,
TMS event_)
{
Expand All @@ -263,7 +342,6 @@ private static TValue get_compTM(LuaState L, Table mt1, Table mt2,
return null;
}


private static int call_orderTM(LuaState L, TValue p1, TValue p2,
TMS event_)
{
Expand All @@ -277,7 +355,6 @@ private static int call_orderTM(LuaState L, TValue p1, TValue p2,
return l_isfalse(L._top) == 0 ? 1 : 0;
}


private static int l_strcmp(TString ls, TString rs)
{
CharPtr l = getstr(ls);
Expand Down Expand Up @@ -317,7 +394,6 @@ public static int luaV_lessthan(LuaState L, TValue l, TValue r)
return luaG_ordererror(L, l, r);
}


private static int lessequal(LuaState L, TValue l, TValue r)
{
int res;
Expand Down Expand Up @@ -434,8 +510,6 @@ public static void Arith(LuaState L, StkId ra, TValue rb,
luaG_aritherror(L, rb, rc);
}



/*
** some macros for common tasks in `luaV_execute'
*/
Expand All @@ -461,10 +535,8 @@ public static void Arith(LuaState L, StkId ra, TValue rb,
internal static TValue RKC(LuaState L, StkId base_, Instruction i, TValue[] k) { return ISK(GETARG_C(i)) != 0 ? k[INDEXK(GETARG_C(i))] : base_ + GETARG_C(i); }
internal static TValue KBx(LuaState L, Instruction i, TValue[] k) { return k[GETARG_Bx(i)]; }


public static void dojump(LuaState L, InstructionPtr pc, int i) { pc.pc += i; luai_threadyield(L); }


//#define Protect(x) { L.savedpc = pc; {x;}; base = L.base_; }

public static void arith_op(LuaState L, op_delegate op, TMS tm, StkId base_, Instruction i, TValue[] k, StkId ra, InstructionPtr pc)
Expand Down Expand Up @@ -638,6 +710,10 @@ public static void luaV_execute(LuaState L, int nexeccalls)
sethvalue(L, g, cl.env);
lua_assert(ttisstring(rb));

/* PATCH - TABLESCOPE */
if (gettablescope(L, ra, rb, pc, cl, L.base_) == 1)
continue;

//Protect(
L.savedpc = InstructionPtr.Assign(pc);
luaV_gettable(L, g, rb, ra);
Expand Down
3 changes: 3 additions & 0 deletions SharpLua/LuaCore/ltm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ public enum TMS
TM_TABLESCOPE,
TM_BITNEGATE,
TM_BITXOR,
TM_CHANGED,
TM_SCOPE,
TM_N /* number of elements in the enum */
};

Expand All @@ -72,6 +74,7 @@ public static TValue gfasttm(GlobalState g, Table et, TMS e)
"__pow", "__unm", "__len", "__lt", "__le",
"__concat", "__call", "__rshift", "__lshift", "__bitand", "__bitor",
"__usedindex", "__getindex", "__iter", "__scope", "__bitnegate", "__bitxor",
"__changed", "__scope"
};

public static void luaT_init(LuaState L)
Expand Down
Loading

0 comments on commit e58efd5

Please sign in to comment.