/// <summary> /// Create an instance of the given object. /// </summary> /// <param name="WHAT"></param> /// <returns></returns> public override RakudoObject instance_of(ThreadContext TC, RakudoObject WHAT) { var Object = new KnowHOWInstance(WHAT.STable); Object.Methods = new Dictionary<string, RakudoObject>(); Object.Attributes = new List<RakudoObject>(); return Object; }
/// <summary> /// /// </summary> /// <param name="Object"></param> /// <param name="ClassHandle"></param> /// <param name="Name"></param> /// <param name="Value"></param> public override void bind_attribute(ThreadContext TC, RakudoObject Object, RakudoObject ClassHandle, string Name, RakudoObject Value) { var I = (Instance)Object; // Try the slot allocation first. Dictionary<string, int> ClassAllocation; int Position; if (SlotAllocation != null && SlotAllocation.TryGetValue(ClassHandle, out ClassAllocation)) if (ClassAllocation.TryGetValue(Name, out Position)) { I.SlotStorage[Position] = Value; return; } // Fall back to the spill storage. if (I.SpillStorage == null) I.SpillStorage = new Dictionary<RakudoObject, Dictionary<string, RakudoObject>>(); if (!I.SpillStorage.ContainsKey(ClassHandle)) I.SpillStorage.Add(ClassHandle, new Dictionary<string, RakudoObject>()); var ClassStore = I.SpillStorage[ClassHandle]; if (ClassStore.ContainsKey(Name)) ClassStore[Name] = Value; else ClassStore.Add(Name, Value); }
/// <summary> /// Gets the attribute with the given value. /// </summary> /// <param name="ClassHandle"></param> /// <param name="Name"></param> /// <returns></returns> public override RakudoObject get_attribute(ThreadContext TC, RakudoObject Object, RakudoObject ClassHandle, string Name) { // If no storage ever allocated, trivially no value. Otherwise, // return what we find. var I = (Instance)Object; if (I.Storage == null || !I.Storage.ContainsKey(ClassHandle)) return null; var ClassStore = I.Storage[ClassHandle]; return ClassStore.ContainsKey(Name) ? ClassStore[Name] : null; }
/// <summary> /// Binds an attribute to the given value. /// </summary> /// <param name="Object"></param> /// <param name="ClassHandle"></param> /// <param name="Name"></param> /// <param name="Value"></param> public override void bind_attribute(ThreadContext TC, RakudoObject Object, RakudoObject ClassHandle, string Name, RakudoObject Value) { // If no storage at all, allocate some. var I = (Instance)Object; if (I.Storage == null) I.Storage = new Dictionary<RakudoObject, Dictionary<string, RakudoObject>>(); if (!I.Storage.ContainsKey(ClassHandle)) I.Storage.Add(ClassHandle, new Dictionary<string, RakudoObject>()); // Now stick in the name slot for the class storage, creating if it // needed. var ClassStore = I.Storage[ClassHandle]; if (ClassStore.ContainsKey(Name)) ClassStore[Name] = Value; else ClassStore.Add(Name, Value); }
/// <summary> /// Bind the attribute, using the hint if possible. /// </summary> /// <param name="Object"></param> /// <param name="ClassHandle"></param> /// <param name="Name"></param> /// <param name="Hint"></param> /// <param name="Value"></param> public override void bind_attribute_with_hint(ThreadContext TC, RakudoObject Object, RakudoObject ClassHandle, string Name, int Hint, RakudoObject Value) { var I = (Instance)Object; if (Hint < I.SlotStorage.Length) { I.SlotStorage[Hint] = Value; } else if ((Hint = hint_for(TC, ClassHandle, Name)) != Hints.NO_HINT && Hint < I.SlotStorage.Length) { I.SlotStorage[Hint] = Value; } else { if (I.SpillStorage == null) I.SpillStorage = new Dictionary<RakudoObject, Dictionary<string, RakudoObject>>(); if (!I.SpillStorage.ContainsKey(ClassHandle)) I.SpillStorage.Add(ClassHandle, new Dictionary<string, RakudoObject>()); var ClassStore = I.SpillStorage[ClassHandle]; if (ClassStore.ContainsKey(Name)) ClassStore[Name] = Value; else ClassStore.Add(Name, Value); } }
public override RakudoObject get_attribute_with_hint(ThreadContext TC, RakudoObject Object, RakudoObject ClassHandle, string Name, int Hint) { throw new InvalidOperationException("Native captures cannot store additional attributes."); }
/// <summary> /// Determines if the representation is defined or not. /// </summary> /// <param name="Obj"></param> /// <returns></returns> public override bool defined(ThreadContext TC, RakudoObject O) { var Obj = (Instance)O; return Obj.Positionals != null || Obj.Nameds != null; }
public override double get_num(ThreadContext TC, RakudoObject Object) { throw new NotImplementedException(); }
/// <summary> /// Checks if the object is defined, which boils down to "is /// this a type object", which in trun means "did we allocate /// any storage". /// </summary> /// <param name="Object"></param> /// <returns></returns> public override bool defined(ThreadContext TC, RakudoObject Object) { return ((Instance)Object).Storage != null; }
public override void set_str(ThreadContext TC, RakudoObject Object, string Value) { throw new InvalidOperationException("This type of representation cannot box a native string"); }
/// <summary> /// Allocates and returns a new object based upon the type object /// supplied. /// </summary> /// <param name="HOW"></param> /// <returns></returns> public override RakudoObject instance_of(ThreadContext TC, RakudoObject WHAT) { var Object = new Instance(WHAT.STable); Object.Storage = new Dictionary<RakudoObject, Dictionary<string, RakudoObject>>(); return Object; }
public override void bind_attribute_with_hint(ThreadContext TC, RakudoObject Object, RakudoObject ClassHandle, string Name, int Hint, RakudoObject Value) { throw new InvalidOperationException("Boxed native types cannot store additional attributes."); }
/// <summary> /// Sets up the KnowHOWAttribute object/class, which actually is a /// KnowHOW. /// </summary> /// <returns></returns> public static RakudoObject SetupKnowHOWAttribute(RakudoObject KnowHOW) { // Create a new HOW instance. var HOW = KnowHOW.STable.REPR.instance_of(null, KnowHOW) as KnowHOWREPR.KnowHOWInstance; // We base the attribute on P6str, since we just want to store an // attribute name for now. var KnowHOWAttribute = REPRRegistry.get_REPR_by_name("P6str").type_object_for(null, HOW); // Add methods new and Str. HOW.Methods.Add("new", CodeObjectUtility.WrapNativeMethod((TC, Code, Cap) => { var WHAT = CaptureHelper.GetPositional(Cap, 0).STable.WHAT; var Name = Ops.unbox_str(TC, CaptureHelper.GetNamed(Cap, "name")); return Ops.box_str(TC, Name, WHAT); })); HOW.Methods.Add("name", CodeObjectUtility.WrapNativeMethod((TC, Code, Cap) => { var self = CaptureHelper.GetPositional(Cap, 0); return Ops.box_str(TC, Ops.unbox_str(TC, self), TC.DefaultStrBoxType); })); return KnowHOWAttribute; }
public override void set_str(ThreadContext TC, RakudoObject Object, string Value) { throw new NotImplementedException(); }
public override void set_num(ThreadContext TC, RakudoObject Object, double Value) { throw new NotImplementedException(); }
public override int hint_for(ThreadContext TC, RakudoObject ClassHandle, string Name) { throw new NotImplementedException(); }
public override string get_str(ThreadContext TC, RakudoObject Object) { throw new NotImplementedException(); }
/// <summary> /// Creates an instance of the type with the given type object. /// </summary> /// <param name="WHAT"></param> /// <returns></returns> public override RakudoObject instance_of(ThreadContext TC, RakudoObject WHAT) { return new Instance(WHAT.STable); }
/// <summary> /// No hints for P6Hash. /// </summary> /// <param name="ClassHandle"></param> /// <param name="Name"></param> /// <returns></returns> public override int hint_for(ThreadContext TC, RakudoObject ClassHandle, string Name) { return Hints.NO_HINT; }
/// <summary> /// Determines if the representation is defined or not. /// </summary> /// <param name="Obj"></param> /// <returns></returns> public override bool defined(ThreadContext TC, RakudoObject Obj) { return !((Instance)Obj).Undefined; }
public override void set_num(ThreadContext TC, RakudoObject Object, double Value) { throw new InvalidOperationException("This type of representation cannot box a native num"); }
public override RakudoObject get_attribute(ThreadContext TC, RakudoObject Object, RakudoObject ClassHandle, string Name) { throw new InvalidOperationException("Boxed native types cannot store additional attributes."); }
/// <summary> /// Creates a type object that references the given HOW and /// this REPR; note we just use the singleton instance for /// all of them, since the REPR stores nothing distinct. /// </summary> /// <param name="HOW"></param> /// <returns></returns> public override RakudoObject type_object_for(ThreadContext TC, RakudoObject MetaPackage) { var STable = new SharedTable(); STable.HOW = MetaPackage; STable.REPR = this; STable.WHAT = new Instance(STable); return STable.WHAT; }
public override int get_int(ThreadContext TC, RakudoObject Object) { return ((Instance)Object).Value; }
/// <summary> /// This representation doesn't do hints, so this delegates straight /// off to the hint-less version. /// </summary> /// <param name="ClassHandle"></param> /// <param name="Name"></param> /// <param name="Hint"></param> /// <param name="Value"></param> public override void bind_attribute_with_hint(ThreadContext TC, RakudoObject Object, RakudoObject ClassHandle, string Name, int Hint, RakudoObject Value) { bind_attribute(TC, Object, ClassHandle, Name, Value); }
/// <summary> /// This representation doesn't use hints, so this just delegates /// straight off to the hint-less version. /// </summary> /// <param name="ClassHandle"></param> /// <param name="Name"></param> /// <param name="Hint"></param> /// <returns></returns> public override RakudoObject get_attribute_with_hint(ThreadContext TC, RakudoObject Object, RakudoObject ClassHandle, string Name, int Hint) { return get_attribute(TC, Object, ClassHandle, Name); }
public override void set_int(ThreadContext TC, RakudoObject Object, int Value) { ((Instance)Object).Value = Value; }
public override int get_int(ThreadContext TC, RakudoObject Object) { throw new NotImplementedException(); }
public override int get_int(ThreadContext TC, RakudoObject Object) { throw new InvalidOperationException("This type of representation cannot unbox to a native int"); }
public override void bind_attribute(ThreadContext TC, RakudoObject Object, RakudoObject ClassHandle, string Name, RakudoObject Value) { throw new InvalidOperationException("Native captures cannot store additional attributes."); }