Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Truly vile hack to allow defining dualvar types
  • Loading branch information
sorear committed Jan 17, 2012
1 parent 0ae0fa0 commit b5b8434
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
8 changes: 8 additions & 0 deletions lib/Builtins.cs
Expand Up @@ -2554,6 +2554,14 @@ class CrossSource: ItemSource {
}
}

public static Variable dualvar(Variable obj, Variable type, Variable str) {
P6any nobj = obj.Fetch().ReprClone();
nobj.ChangeType(type.Fetch().mo);
nobj.SetSlot(Kernel.PseudoStrMO, "$!value",
Kernel.UnboxAny<string>(str.Fetch()));
return Kernel.NewROScalar(nobj);
}

public static void EstablishSlot(P6any n, P6how.AttrInfo ai,
Variable vx) {
Variable obj;
Expand Down
2 changes: 2 additions & 0 deletions lib/CORE.setting
Expand Up @@ -2780,6 +2780,8 @@ grammar Niecza::NumSyntax {
$a ~~ FatRat ?? $a.numerator / $a.denominator !! $a
}
}
class Niecza::PseudoStr is Str { has $!value }
# }}}
# I/O stuff {{{
sub slurp($path) is unsafe { Q:CgOp { (box Str (slurp (unbox str (@ {$path})))) } }
Expand Down
13 changes: 12 additions & 1 deletion lib/Kernel.cs
Expand Up @@ -4547,7 +4547,17 @@ public class Kernel {
}

public static T UnboxAny<T>(P6any o) {
return ((BoxObject<T>)o).value;
var co = o as BoxObject<T>;
if (co != null) {
return co.value;
} else {
if (typeof(T) == typeof(string) && o.Does(Kernel.PseudoStrMO)) {
// Truly vile hack to make dualvars work.
return (T)o.GetSlot(Kernel.PseudoStrMO, "$!value");
} else {
throw new NieczaException("Cannot unbox a {0} from an object of repr {1}", typeof(T).Name, o.ReprName());
}
}
}

public static Frame Take(Frame th, Variable payload) {
Expand Down Expand Up @@ -4739,6 +4749,7 @@ public class Kernel {
[CORESaved] public static STable BlockMO;
[CORESaved] public static STable RegexMO;
[CORESaved] public static STable StrMO;
[CORESaved] public static STable PseudoStrMO;
[CORESaved] public static STable NumMO;
[CORESaved] public static STable IntMO;
[CORESaved] public static STable RatMO;
Expand Down
2 changes: 1 addition & 1 deletion lib/Serialize.cs
Expand Up @@ -64,7 +64,7 @@ struct ObjRef {
internal static HashAlgorithm NewHash() { return new SHA256Managed(); }

static readonly string signature = "Niecza-Serialized-Module";
static readonly int version = 22;
static readonly int version = 23;

// Routines for use by serialization code
public bool CheckWriteObject(SerUnit into, object o,
Expand Down

0 comments on commit b5b8434

Please sign in to comment.