From 054561b145da06221386fdb1408d84a77306a07d Mon Sep 17 00:00:00 2001 From: Stefan O'Rear Date: Mon, 17 Oct 2011 23:30:20 -0700 Subject: [PATCH] Thaw code for all LAD, Variable, ViviHook subclasses --- lib/Builtins.cs | 97 ++++++++++++++++++++++++++--------------------- lib/Cursor.cs | 99 +++++++++++++++++++++++++++++++++++++++--------- lib/Kernel.cs | 31 ++++++++++++--- lib/Serialize.cs | 68 ++++++++++++++++++++++----------- 4 files changed, 206 insertions(+), 89 deletions(-) diff --git a/lib/Builtins.cs b/lib/Builtins.cs index 85f99efd..6f08b3a8 100644 --- a/lib/Builtins.cs +++ b/lib/Builtins.cs @@ -92,6 +92,59 @@ class PosixWrapper { f_ctime = Stat.GetField("st_ctime"); } } + + class SubstrLValue: Variable { + Variable backing; + int from; + int length; + + private SubstrLValue() {} + public SubstrLValue(Variable backing, int from, int length) { + this.backing = backing; + this.from = from; + this.length = length; + // XXX Should binding a substr lvalue count as binding the original? + this.whence = null; + this.rw = backing.rw; + this.type = Kernel.StrMO; + } + + public override P6any Fetch() { + string str = backing.Fetch().mo.mro_raw_Str.Get(backing); + string sub = Builtins.LaxSubstring2(str, from, length); + return sub == null ? Kernel.StrMO.typeObject : + Kernel.BoxRaw(sub,Kernel.StrMO); + } + + public override void Store(P6any v) { + string str = backing.Fetch().mo.mro_raw_Str.Get(backing); + int left = (from < 0) ? 0 : (from > str.Length) ? str.Length : from; + int right = ((length > (str.Length - left)) ? (str.Length - left) : + (length < 0) ? 0 : length) + left; + string lfr = str.Substring(0, left); + string mfr = v.mo.mro_raw_Str.Get(Kernel.NewROScalar(v)); + string rfr = str.Substring(right); + backing.Store(Kernel.BoxRaw(lfr + mfr + rfr, Kernel.StrMO)); + } + + public override Variable GetVar() { + return Kernel.BoxAnyMO(this, Kernel.ScalarMO); + } + public override void Freeze(Niecza.Serialization.FreezeBuffer fb) { + fb.Byte((byte)Niecza.Serialization.SerializationCode.SubstrLValue); + fb.ObjRef(backing); + fb.Int(from); + fb.Int(length); + } + internal static object Thaw(Niecza.Serialization.ThawBuffer tb) { + var n = new SubstrLValue(); + tb.Register(n); + n.backing = (Variable) tb.ObjRef(); + n.from = tb.Int(); + n.length = tb.Int(); + return n; + } + } } public partial class Builtins { @@ -187,50 +240,6 @@ public partial class Builtins { } } - class SubstrLValue: Variable { - Variable backing; - int from; - int length; - - public SubstrLValue(Variable backing, int from, int length) { - this.backing = backing; - this.from = from; - this.length = length; - // XXX Should binding a substr lvalue count as binding the original? - this.whence = null; - this.rw = backing.rw; - this.type = Kernel.StrMO; - } - - public override P6any Fetch() { - string str = backing.Fetch().mo.mro_raw_Str.Get(backing); - string sub = Builtins.LaxSubstring2(str, from, length); - return sub == null ? Kernel.StrMO.typeObject : - Kernel.BoxRaw(sub,Kernel.StrMO); - } - - public override void Store(P6any v) { - string str = backing.Fetch().mo.mro_raw_Str.Get(backing); - int left = (from < 0) ? 0 : (from > str.Length) ? str.Length : from; - int right = ((length > (str.Length - left)) ? (str.Length - left) : - (length < 0) ? 0 : length) + left; - string lfr = str.Substring(0, left); - string mfr = v.mo.mro_raw_Str.Get(Kernel.NewROScalar(v)); - string rfr = str.Substring(right); - backing.Store(Kernel.BoxRaw(lfr + mfr + rfr, Kernel.StrMO)); - } - - public override Variable GetVar() { - return Kernel.BoxAnyMO(this, Kernel.ScalarMO); - } - public override void Freeze(Niecza.Serialization.FreezeBuffer fb) { - fb.Byte((byte)Niecza.Serialization.SerializationCode.SubstrLValue); - fb.ObjRef(backing); - fb.Int(from); - fb.Int(length); - } - } - public static string LaxSubstring(string str, int from) { if (from < 0 || from > str.Length) return null; diff --git a/lib/Cursor.cs b/lib/Cursor.cs index ff07cfb7..2ec6dfd7 100644 --- a/lib/Cursor.cs +++ b/lib/Cursor.cs @@ -957,7 +957,7 @@ public abstract class LAD : IFreeze { } public class LADStr : LAD { - public readonly string text; + public string text; public LADStr(string text) { this.text = text; } public override LAD Reify(NFA pad) { return this; } @@ -985,11 +985,18 @@ public class LADStr : LAD { fb.Byte((byte) SerializationCode.LADStr); fb.String(text); } + internal static LADStr Thaw(ThawBuffer tb) { + LADStr n = new LADStr(null); + tb.Register(n); + n.text = tb.String(); + return n; + } } public class LADStrNoCase : LAD { - public readonly string text; + public string text; public LADStrNoCase(string text) { this.text = text; } + private LADStrNoCase() { } public override LAD Reify(NFA pad) { return this; } public override void QueryLiteral(NFA pad, out int len, out bool cont) { @@ -1016,10 +1023,17 @@ public class LADStrNoCase : LAD { fb.Byte((byte) SerializationCode.LADStrNoCase); fb.String(text); } + internal static LADStrNoCase Thaw(ThawBuffer tb) { + LADStrNoCase n = new LADStrNoCase(); + tb.Register(n); + n.text = tb.String(); + return n; + } } public class LADCC : LAD { - public readonly CC cc; + public CC cc; + private LADCC() { } public LADCC(CC cc) { this.cc = cc; } public override LAD Reify(NFA pad) { return this; } @@ -1032,7 +1046,13 @@ public class LADCC : LAD { } public override void Freeze(FreezeBuffer fb) { fb.Byte((byte) SerializationCode.LADCC); - fb.Ints(cc.vec); + fb.ObjRef(cc); + } + internal static LADCC Thaw(ThawBuffer tb) { + LADCC n = new LADCC(); + tb.Register(n); + n.cc = (CC) tb.ObjRef(); + return n; } } @@ -1098,8 +1118,9 @@ public class LADDot : LAD { } public class LADStar : LAD { - public readonly LAD child; + public LAD child; public LADStar(LAD child) { this.child = child; } + private LADStar() {} public override LAD Reify(NFA pad) { return new LADStar(child.Reify(pad)); } public override void ToNFA(NFA pad, int from, int to) { @@ -1117,12 +1138,19 @@ public class LADStar : LAD { fb.Byte((byte) SerializationCode.LADStar); fb.ObjRef(child); } + internal static object Thaw(ThawBuffer tb) { + var n = new LADStar(); + tb.Register(n); + n.child = (LAD) tb.ObjRef(); + return n; + } } public class LADOpt : LAD { - public readonly LAD child; + public LAD child; public override LAD Reify(NFA pad) { return new LADOpt(child.Reify(pad)); } public LADOpt(LAD child) { this.child = child; } + private LADOpt() {} public override void ToNFA(NFA pad, int from, int to) { pad.AddEdge(from, to, null); @@ -1137,12 +1165,19 @@ public class LADOpt : LAD { fb.Byte((byte) SerializationCode.LADOpt); fb.ObjRef(child); } + internal static object Thaw(ThawBuffer tb) { + var n = new LADOpt(); + tb.Register(n); + n.child = (LAD) tb.ObjRef(); + return n; + } } public class LADPlus : LAD { - public readonly LAD child; + public LAD child; public override LAD Reify(NFA pad) { return new LADPlus(child.Reify(pad)); } public LADPlus(LAD child) { this.child = child; } + private LADPlus() { } public override void QueryLiteral(NFA pad, out int len, out bool cont) { child.QueryLiteral(pad, out len, out cont); @@ -1165,11 +1200,18 @@ public class LADPlus : LAD { fb.Byte((byte) SerializationCode.LADPlus); fb.ObjRef(child); } + internal static object Thaw(ThawBuffer tb) { + var n = new LADPlus(); + tb.Register(n); + n.child = (LAD) tb.ObjRef(); + return n; + } } public class LADSequence : LAD { - public readonly LAD[] args; + public LAD[] args; public LADSequence(LAD[] args) { this.args = args; } + private LADSequence() { } public override LAD Reify(NFA pad) { LAD[] nc = new LAD[args.Length]; for (int i = 0; i < args.Length; i++) @@ -1204,15 +1246,20 @@ public class LADSequence : LAD { } public override void Freeze(FreezeBuffer fb) { fb.Byte((byte) SerializationCode.LADSequence); - fb.Int(args.Length); - foreach (LAD l in args) - fb.ObjRef(l); + fb.Refs(args); + } + internal static object Thaw(ThawBuffer tb) { + var n = new LADSequence(); + tb.Register(n); + n.args = tb.RefsA(); + return n; } } public class LADAny : LAD { - public readonly LAD[] zyg; + public LAD[] zyg; public LADAny(LAD[] zyg) { this.zyg = zyg; } + private LADAny() { } public override LAD Reify(NFA pad) { LAD[] nc = new LAD[zyg.Length]; for (int i = 0; i < zyg.Length; i++) @@ -1232,14 +1279,19 @@ public class LADAny : LAD { } public override void Freeze(FreezeBuffer fb) { fb.Byte((byte) SerializationCode.LADAny); - fb.Int(zyg.Length); - foreach (LAD l in zyg) - fb.ObjRef(l); + fb.Refs(zyg); + } + internal static object Thaw(ThawBuffer tb) { + var n = new LADAny(); + tb.Register(n); + n.zyg = tb.RefsA(); + return n; } } public class LADParam : LAD { - public readonly string name; + public string name; + private LADParam() {} public LADParam(string name) { this.name = name; } public override LAD Reify(NFA pad) { @@ -1303,12 +1355,19 @@ public class LADParam : LAD { fb.Byte((byte) SerializationCode.LADParam); fb.String(name); } + internal static LADParam Thaw(ThawBuffer tb) { + LADParam n = new LADParam(); + tb.Register(n); + n.name = tb.String(); + return n; + } } public class LADMethod : LAD { - public readonly string name; + public string name; public LADMethod(string name) { this.name = name; } + private LADMethod() {} public override void ToNFA(NFA pad, int from, int to) { throw new InvalidOperationException(); @@ -1358,6 +1417,12 @@ public class LADMethod : LAD { fb.Byte((byte) SerializationCode.LADMethod); fb.String(name); } + internal static LADMethod Thaw(ThawBuffer tb) { + LADMethod n = new LADMethod(); + tb.Register(n); + n.name = tb.String(); + return n; + } } // Only really makes sense if used in the static scope of a proto diff --git a/lib/Kernel.cs b/lib/Kernel.cs index 99e1c4b9..7f0dd48d 100644 --- a/lib/Kernel.cs +++ b/lib/Kernel.cs @@ -93,8 +93,11 @@ public class SubViviHook : ViviHook { fb.Byte((byte)SerializationCode.SubViviHook); fb.ObjRef(sub); } - internal static SubViviHook Thaw(ThawBuffer tb) { - return new SubViviHook((P6any)tb.ObjRef()); + internal static object Thaw(ThawBuffer tb) { + var n = new SubViviHook(null); + tb.Register(n); + n.sub = (P6any) tb.ObjRef(); + return n; } } @@ -112,7 +115,11 @@ public class HashViviHook : ViviHook { fb.String(key); } internal static IFreeze Thaw(ThawBuffer tb) { - return new HashViviHook((P6any)tb.ObjRef(), tb.String()); + var n = new HashViviHook(null, null); + tb.Register(n); + n.hash = (P6any) tb.ObjRef(); + n.key = tb.String(); + return n; } } @@ -131,7 +138,11 @@ public class NewHashViviHook : ViviHook { fb.String(key); } internal static IFreeze Thaw(ThawBuffer tb) { - return new NewHashViviHook((Variable)tb.ObjRef(), tb.String()); + var n = new NewHashViviHook(null, null); + tb.Register(n); + n.hashv = (Variable) tb.ObjRef(); + n.key = tb.String(); + return n; } } @@ -151,7 +162,11 @@ public class ArrayViviHook : ViviHook { fb.Int(key); } internal static IFreeze Thaw(ThawBuffer tb) { - return new ArrayViviHook((P6any)tb.ObjRef(), tb.Int()); + var n = new ArrayViviHook(null, 0); + tb.Register(n); + n.ary = (P6any) tb.ObjRef(); + n.key = tb.Int(); + return n; } } @@ -175,7 +190,11 @@ public class NewArrayViviHook : ViviHook { fb.Int(key); } internal static IFreeze Thaw(ThawBuffer tb) { - return new NewArrayViviHook((Variable)tb.ObjRef(), tb.Int()); + var n = new NewArrayViviHook(null, 0); + tb.Register(n); + n.ary = (Variable) tb.ObjRef(); + n.key = tb.Int(); + return n; } } diff --git a/lib/Serialize.cs b/lib/Serialize.cs index a78d40a4..ab1688c2 100644 --- a/lib/Serialize.cs +++ b/lib/Serialize.cs @@ -711,28 +711,52 @@ class ThawBuffer { case SerializationCode.SimpleVariable_3: return SimpleVariable.Thaw(this, (int)tag - (int)SerializationCode.SimpleVariable); - //SubstrLValue, - //TiedVariable, - //SubViviHook, - //ArrayViviHook, - //NewArrayViviHook, - //HashViviHook, - //NewHashViviHook, - //LADNone, // no-args - //LADNull, - //LADDot, - //LADDispatcher, - //LADImp, - //LADStr, // string - //LADStrNoCase, - //LADMethod, - //LADParam, - //LADOpt, // LAD - //LADPlus, - //LADStar, - //LADSequence, // LAD[] - //LADAny, - //LADCC, // CC + case SerializationCode.SubstrLValue: + return SubstrLValue.Thaw(this); + case SerializationCode.TiedVariable: + return TiedVariable.Thaw(this); + + case SerializationCode.SubViviHook: + return SubViviHook.Thaw(this); + case SerializationCode.ArrayViviHook: + return ArrayViviHook.Thaw(this); + case SerializationCode.NewArrayViviHook: + return NewArrayViviHook.Thaw(this); + case SerializationCode.HashViviHook: + return HashViviHook.Thaw(this); + case SerializationCode.NewHashViviHook: + return NewHashViviHook.Thaw(this); + + case SerializationCode.LADNone: + return Register(new LADNone()); + case SerializationCode.LADNull: + return Register(new LADNull()); + case SerializationCode.LADDot: + return Register(new LADDot()); + case SerializationCode.LADDispatcher: + return Register(new LADDispatcher()); + case SerializationCode.LADImp: + return Register(new LADImp()); + case SerializationCode.LADStr: + return LADStr.Thaw(this); + case SerializationCode.LADStrNoCase: + return LADStrNoCase.Thaw(this); + case SerializationCode.LADMethod: + return LADMethod.Thaw(this); + case SerializationCode.LADParam: + return LADParam.Thaw(this); + case SerializationCode.LADOpt: + return LADOpt.Thaw(this); + case SerializationCode.LADPlus: + return LADPlus.Thaw(this); + case SerializationCode.LADStar: + return LADStar.Thaw(this); + case SerializationCode.LADSequence: + return LADSequence.Thaw(this); + case SerializationCode.LADAny: + return LADAny.Thaw(this); + case SerializationCode.LADCC: + return LADCC.Thaw(this); default: throw new ThawException("unexpected object tag " + tag); }