Example #1
0
 /// <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;
 }
Example #2
0
        /// <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);
        }
Example #3
0
 /// <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;
 }
Example #4
0
        /// <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);
        }
Example #5
0
 /// <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);
     }
 }
Example #6
0
 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.");
 }
Example #7
0
 /// <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;
 }
Example #8
0
 public override double get_num(ThreadContext TC, RakudoObject Object)
 {
     throw new NotImplementedException();
 }
Example #9
0
 /// <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;
 }
Example #10
0
 public override void set_str(ThreadContext TC, RakudoObject Object, string Value)
 {
     throw new InvalidOperationException("This type of representation cannot box a native string");
 }
Example #11
0
 /// <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;
 }
Example #12
0
 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.");
 }
Example #13
0
        /// <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;
        }
Example #14
0
 public override void set_str(ThreadContext TC, RakudoObject Object, string Value)
 {
     throw new NotImplementedException();
 }
Example #15
0
 public override void set_num(ThreadContext TC, RakudoObject Object, double Value)
 {
     throw new NotImplementedException();
 }
Example #16
0
 public override int hint_for(ThreadContext TC, RakudoObject ClassHandle, string Name)
 {
     throw new NotImplementedException();
 }
Example #17
0
 public override string get_str(ThreadContext TC, RakudoObject Object)
 {
     throw new NotImplementedException();
 }
Example #18
0
 /// <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);
 }
Example #19
0
 /// <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;
 }
Example #20
0
 /// <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;
 }
Example #21
0
 public override void set_num(ThreadContext TC, RakudoObject Object, double Value)
 {
     throw new InvalidOperationException("This type of representation cannot box a native num");
 }
Example #22
0
 public override RakudoObject get_attribute(ThreadContext TC, RakudoObject Object, RakudoObject ClassHandle, string Name)
 {
     throw new InvalidOperationException("Boxed native types cannot store additional attributes.");
 }
Example #23
0
 /// <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;
 }
Example #24
0
 public override int get_int(ThreadContext TC, RakudoObject Object)
 {
     return ((Instance)Object).Value;
 }
Example #25
0
 /// <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);
 }
Example #26
0
 /// <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);
 }
Example #27
0
 public override void set_int(ThreadContext TC, RakudoObject Object, int Value)
 {
     ((Instance)Object).Value = Value;
 }
Example #28
0
 public override int get_int(ThreadContext TC, RakudoObject Object)
 {
     throw new NotImplementedException();
 }
Example #29
0
 public override int get_int(ThreadContext TC, RakudoObject Object)
 {
     throw new InvalidOperationException("This type of representation cannot unbox to a native int");
 }
Example #30
0
 public override void bind_attribute(ThreadContext TC, RakudoObject Object, RakudoObject ClassHandle, string Name, RakudoObject Value)
 {
     throw new InvalidOperationException("Native captures cannot store additional attributes.");
 }