From 7aa4b5e9a3caaddc182bfb2c4ebd4d01eb41537c Mon Sep 17 00:00:00 2001 From: Stefan O'Rear Date: Sat, 2 Jun 2012 01:15:32 -0700 Subject: [PATCH] Use new array munging helpers whereever possible in the kernel --- lib/Builtins.cs | 4 +--- lib/CodeGen.cs | 26 +++++++++---------------- lib/Kernel.cs | 49 ++++++++++++++---------------------------------- lib/NieczaCLR.cs | 7 ++----- lib/ObjModel.cs | 19 ++++++------------- lib/Utils.cs | 6 +++--- 6 files changed, 35 insertions(+), 76 deletions(-) diff --git a/lib/Builtins.cs b/lib/Builtins.cs index cd26d727..e0d58514 100644 --- a/lib/Builtins.cs +++ b/lib/Builtins.cs @@ -2577,11 +2577,9 @@ class CrossSource: ItemSource { } public static Frame dispatch_fromtype(Frame th) { - Variable[] npos = new Variable[th.pos.Length - 2]; STable from = th.pos[1].Fetch().mo; string name = th.pos[2].Fetch().mo.mro_raw_Str.Get(th.pos[2]); - npos[0] = th.pos[0]; - Array.Copy(th.pos, 3, npos, 1, npos.Length - 1); + var npos = Utils.PrependArr(th.pos, th.pos[0], 3); if (!npos[0].Fetch().Does(from)) { return Kernel.Die(th, "Cannot dispatch to a method on " + diff --git a/lib/CodeGen.cs b/lib/CodeGen.cs index 9f39a943..879f25bc 100644 --- a/lib/CodeGen.cs +++ b/lib/CodeGen.cs @@ -1783,10 +1783,8 @@ class CpsOp { // TODO: it's not really necessary to force a full cps-format // node here. But we don't have universal recursion for // non-cps nodes. - ClrOp[] body = new ClrOp[z.stmts.Length + 1]; - Array.Copy(z.stmts, 0, body, 1, body.Length - 1); - body[0] = ClrSync.Instance; - return new CpsOp(body, z.head); + return new CpsOp(Utils.PrependArr(z.stmts, ClrSync.Instance), + z.head); } public static CpsOp Ternary(CpsOp cond, CpsOp iftrue, CpsOp iffalse) { @@ -2554,9 +2552,8 @@ class NamProcessor { return CpsOp.Sequence(ops.ToArray()); }; handlers["_inline"] = delegate(NamProcessor th, object[] zyg) { - object[] rzyg = new object[zyg.Length - 2]; - Array.Copy(zyg, 2, rzyg, 0, rzyg.Length); - return th.Scan(th.InlineCall((SubInfo)zyg[1], rzyg)); + return th.Scan(th.InlineCall((SubInfo)zyg[1], + Utils.TrimArr(zyg,2,0))); }; handlers["letscope"] = delegate(NamProcessor th, object[] zyg) { List xn = new List(); @@ -3233,10 +3230,8 @@ class NamProcessor { static Func RxCall(Type cps, string name) { MethodInfo mi = Tokens.RxFrame.GetMethod(name); return delegate(CpsOp[] cpses) { - CpsOp[] n = new CpsOp[cpses.Length + 1]; - Array.Copy(cpses, 0, n, 1, cpses.Length); - n[0] = CpsOp.RxFrame(); - return CpsOp.CpsCall(cps, mi, n); }; + return CpsOp.CpsCall(cps, mi, + Utils.PrependArr(cpses, CpsOp.RxFrame())); }; } static Func Constructy(ConstructorInfo mi) { @@ -3262,10 +3257,9 @@ class NamProcessor { FieldInfo f = Tokens.STable.GetField(name); MethodInfo g = f.FieldType.GetMethod("Invoke"); return delegate(CpsOp[] cpses) { - CpsOp[] args = new CpsOp[cpses.Length - 1]; - Array.Copy(cpses, 1, args, 0, args.Length); return CpsOp.Contexty(f, g, new CpsOp[2] { - cpses[0], CpsOp.NewArray(Tokens.Variable, args) }); + cpses[0], CpsOp.NewArray(Tokens.Variable, + Utils.TrimArr(cpses,1,0)) }); }; } @@ -4622,11 +4616,9 @@ public class DowncallReceiver : CallReceiver { } public static object sub_set_extend(object[] args) { SubInfo s = (SubInfo)Handle.Unbox(args[1]); - object[] val = new object[args.Length - 3]; - Array.Copy(args, 3, val, 0, val.Length); if (s.extend == null) s.extend = new Dictionary(); - s.extend[(string)args[2]] = val; + s.extend[(string)args[2]] = Utils.TrimArr(args,3,0); return null; } public static object sub_get_extend(object[] args) { diff --git a/lib/Kernel.cs b/lib/Kernel.cs index 1cc02c50..f4903981 100644 --- a/lib/Kernel.cs +++ b/lib/Kernel.cs @@ -1006,10 +1006,8 @@ public sealed class RuntimeUnit : IFreeze { n.name = tb.String(); string[] srcinfo = tb.Strings(); if (Builtins.upcall_receiver != null) { - object[] args = new object[srcinfo.Length + 1]; - Array.Copy(srcinfo, 0, args, 1, srcinfo.Length); - args[0] = "check_dated"; - object result = Builtins.UpCall(args); + object result = Builtins.UpCall(Utils.PrependArr( + srcinfo, "check_dated")); if (result is Exception) throw (Exception)result; if ((string)result != "ok") @@ -2421,8 +2419,7 @@ public class Frame: P6any, IFixup { if (sub != null && poscap.Length > 0 && sub.Does(Kernel.MethodMO)) { // Hide the self value so that using |callframe.args in a // nextwith call will DTRT - poscap = new Variable[pos.Length - 1]; - Array.Copy(pos, 1, poscap, 0, poscap.Length); + poscap = Utils.TrimArr(pos,1,0); } nw.SetSlot(Kernel.CaptureMO, "$!positionals", poscap); nw.SetSlot(Kernel.CaptureMO, "$!named", named); @@ -2663,8 +2660,7 @@ public class Frame: P6any, IFixup { goto gotit; } P6any nw = new P6opaque(Kernel.CaptureMO); - Variable[] spos = new Variable[pos.Length - posc]; - Array.Copy(pos, posc, spos, 0, spos.Length); + var spos = Utils.TrimArr(pos, posc, 0); VarHash snamed = null; if (named != null) { snamed = new VarHash(); @@ -3056,10 +3052,8 @@ class InvokeSub : InvokeHandler { class InvokeCallMethod : InvokeHandler { public override Frame Invoke(P6any th, Frame caller, Variable[] pos, VarHash named) { - Variable[] np = new Variable[pos.Length + 1]; - Array.Copy(pos, 0, np, 1, pos.Length); - np[0] = th; - return th.InvokeMethod(caller, "postcircumfix:<( )>", np, named); + return th.InvokeMethod(caller, "postcircumfix:<( )>", + Utils.PrependArr(pos, th), named); } } @@ -3071,10 +3065,7 @@ class PushyCallMethod : PushyHandler { protected override void SetData(object[]o) { method = (string)o[0]; } public override Variable Invoke(Variable obj, Variable[] args) { - Variable[] rargs = new Variable[args.Length + 1]; - Array.Copy(args, 0, rargs, 1, args.Length); - rargs[0] = obj; - return Kernel.RunInferior(obj.Fetch().InvokeMethod(Kernel.GetInferiorRoot(), method, rargs, null)); + return Kernel.RunInferior(obj.Fetch().InvokeMethod(Kernel.GetInferiorRoot(), method, Utils.PrependArr(args, obj), null)); } } @@ -4704,11 +4695,8 @@ public class Kernel { } private static Frame SubInvokeSubC(Frame th) { - Variable[] post; - post = new Variable[th.pos.Length - 1]; - Array.Copy(th.pos, 1, post, 0, th.pos.Length - 1); return CodeMO.mro_INVOKE.Invoke((P6opaque)th.pos[0].Fetch(), - th.Return(), post, th.named); + th.Return(), Utils.TrimArr(th.pos,1,0), th.named); } private static Frame JunctionFallbackC(Frame th) { @@ -4720,8 +4708,7 @@ public class Kernel { th.lex1 = new Variable[((Variable[])th.lex0).Length]; th.lex2 = jo.slots[0]; th.lex3 = th.pos[1].Fetch().mo.mro_raw_Str.Get(th.pos[1]); - th.lex4 = new Variable[th.pos.Length - 1]; - Array.Copy(th.pos, 2, (Variable[])th.lex4, 1, th.pos.Length-2); + th.lex4 = Utils.PrependArr(th.pos, null, 2); } Variable[] src = (Variable[]) th.lex0; @@ -5600,9 +5587,8 @@ internal class MMDCandidate : MultiCandidate { PushyHandler fcv = (PushyHandler) th.info.param[1]; Variable[] fullpc = UnboxAny( ((Variable)th.lex1).Fetch()); - Variable[] chop = new Variable[fullpc.Length - 1]; - Array.Copy(fullpc, 1, chop, 0, chop.Length); - th.caller.resultSlot = fcv.Invoke((Variable)th.lex0, chop); + th.caller.resultSlot = fcv.Invoke((Variable)th.lex0, + Utils.TrimArr(fullpc, 1, 0)); return th.Return(); } private static void WrapPushy(STable kl, string name, @@ -5631,11 +5617,8 @@ internal class MMDCandidate : MultiCandidate { if (pos.Length > 2) { Variable inter = ((IndexHandler)p[0]).Get(self, index); - Variable[] npos = new Variable[pos.Length - 1]; - Array.Copy(pos, 2, npos, 1, pos.Length - 2); - npos[0] = inter; return inter.Fetch().InvokeMethod(th.Return(), (string)p[4], - npos, n); + Utils.PrependArr(pos, inter, 2), n); } else if (index == null) { // TODO: handle adverbs on Zen slices (XXX does this make sense?) res = self.List ? self : Kernel.NewRWListVar(self.Fetch()); @@ -5728,9 +5711,7 @@ internal class MMDCandidate : MultiCandidate { static STable ToComposable(STable arg, STable cls) { if (arg.mo.type == P6how.CURRIED_ROLE) { STable r; - Variable[] pass = new Variable[arg.mo.curriedArgs.Length+1]; - pass[0] = cls.typeObj; - Array.Copy(arg.mo.curriedArgs, 0, pass, 1, pass.Length-1); + var pass = Utils.PrependArr(arg.mo.curriedArgs, cls.typeObj); Frame ifr = (Frame)RunInferior(arg.mo.roleFactory. Invoke(GetInferiorRoot(), pass, null)).Fetch(); @@ -6497,9 +6478,7 @@ class LastFrameNode { var tp = (Variable[]) o.slots[0]; n = o.slots[1] as VarHash; // nextdispatch copies over the old self - p = new Variable[tp.Length + 1]; - Array.Copy(tp, 0, p, 1, tp.Length); - p[0] = tf.pos[0]; + p = Utils.PrependArr(tp, tf.pos[0]); } return de.info.SetupCall(tf.caller, de.outer, de.ip6, p, n, false, de); diff --git a/lib/NieczaCLR.cs b/lib/NieczaCLR.cs index ec7240ec..8a39b56e 100644 --- a/lib/NieczaCLR.cs +++ b/lib/NieczaCLR.cs @@ -330,8 +330,7 @@ class OverloadCandidate : MultiCandidate { if (pi.Length != 0 && pi[pi.Length-1].GetCustomAttributes( typeof(ParamArrayAttribute), false).Length != 0) { - Type[] args2 = new Type[args1.Length - 1]; - Array.Copy(args1, 0, args2, 0, args2.Length); + Type[] args2 = Utils.TrimArr(args1, 0, 1); into.Add(new OverloadCandidate(what, args2, refs, args1[args1.Length - 1].GetElementType())); } @@ -651,9 +650,7 @@ public class CLRWrapperProvider { m.how = Kernel.BoxRaw(m, Kernel.ClassHOWMO); STable pm = t.BaseType == null ? Kernel.AnyMO : GetWrapper(t.BaseType); - STable[] mro = new STable[pm.mo.mro.Length + 1]; - Array.Copy(pm.mo.mro, 0, mro, 1, pm.mo.mro.Length); - mro[0] = m; + var mro = Utils.PrependArr(pm.mo.mro, m); m.FillClass(new string[] { }, new STable[] { }, new STable[] { pm }, mro); foreach (Type ity in t.GetInterfaces()) m.mo.role_typecheck_list.Add(GetWrapper(ity)); diff --git a/lib/ObjModel.cs b/lib/ObjModel.cs index 5d55c140..57019a7e 100644 --- a/lib/ObjModel.cs +++ b/lib/ObjModel.cs @@ -54,12 +54,9 @@ public abstract class P6any: Variable, IFreeze { pos, named, false, m); } if (mo.mro_methods.TryGetValue("FALLBACK", out m)) { - Variable[] npos = new Variable[pos.Length + 1]; - Array.Copy(pos, 1, npos, 2, pos.Length - 1); - npos[0] = pos[0]; - npos[1] = Kernel.BoxAnyMO(name, Kernel.StrMO); return m.info.SetupCall(caller, m.outer, m.ip6, - npos, named, false, m); + Utils.PrependArr(pos, 1, pos[0], Builtins.MakeStr(name)), + named, false, m); } return Fail(caller, "Unable to resolve method " + name); } @@ -495,10 +492,8 @@ public class DispatchSet { if (parent == null) { FillClass(slots, slot_types, new STable[] {}, new STable[] { stable }); } else { - STable[] mro = new STable[parent.mo.mro.Length + 1]; - Array.Copy(parent.mo.mro, 0, mro, 1, mro.Length-1); - mro[0] = stable; - FillClass(slots, slot_types, new STable[] { parent }, mro); + FillClass(slots, slot_types, new STable[] { parent }, + Utils.PrependArr(parent.mo.mro, stable)); } Invalidate(); } @@ -506,10 +501,8 @@ public class DispatchSet { public void FillSubset(STable super) { stable.useAcceptsType = true; type = SUBSET; rtype = "subset"; - STable[] mro = new STable[super.mo.mro.Length + 1]; - Array.Copy(super.mo.mro, 0, mro, 1, mro.Length - 1); - mro[0] = stable; - FillClass(super.all_slot, super.type_slot, new STable[] { super }, mro); + FillClass(super.all_slot, super.type_slot, new STable[] { super }, + Utils.PrependArr(super.mo.mro, stable)); Revalidate(); stable.SetupVTables(); } diff --git a/lib/Utils.cs b/lib/Utils.cs index aae7fc19..9e0226f9 100644 --- a/lib/Utils.cs +++ b/lib/Utils.cs @@ -684,9 +684,9 @@ public class Utils { return narr; } - public static T[] PrependArr(T[] arr,T addit) { - T[] narr = new T[arr.Length + 1]; - Array.Copy(arr, 0, narr, 1, arr.Length); + public static T[] PrependArr(T[] arr,T addit,int cut = 0) { + T[] narr = new T[arr.Length + 1 - cut]; + Array.Copy(arr, cut, narr, 1, arr.Length - cut); narr[0] = addit; return narr; }