public static MutableString /*!*/ Inspect(RubyStruct /*!*/ self) { RubyContext context = self.ImmediateClass.Context; using (IDisposable handle = RubyUtils.InfiniteInspectTracker.TrackObject(self)) { // #<struct Struct::Foo name=nil, val=nil> var result = MutableString.CreateMutable(RubyEncoding.Binary); result.Append("#<struct "); result.Append(context.Inspect(context.GetClassOf(self))); if (handle == null) { return(result.Append(":...>")); } result.Append(' '); object[] data = self.Values; var members = self.GetNames(); for (int i = 0; i < data.Length; i++) { if (i != 0) { result.Append(", "); } // TODO (encoding): result.Append(members[i]); result.Append('='); result.Append(context.Inspect(data[i])); } result.Append('>'); return(result); } }
public static MutableString /*!*/ Inspect(RubyStruct /*!*/ self) { RubyContext context = self.Class.Context; using (IDisposable handle = RubyUtils.InfiniteInspectTracker.TrackObject(self)) { // #<struct Struct::Foo name=nil, val=nil> MutableString str = MutableString.Create("#<struct "); str.Append(RubySites.Inspect(context, context.GetClassOf(self))); if (handle == null) { return(str.Append(":...>")); } str.Append(' '); object[] data = self.Values; var members = self.GetNames(); for (int i = 0; i < data.Length; i++) { if (i != 0) { str.Append(", "); } str.Append(members[i]); str.Append("="); str.Append(RubySites.Inspect(context, data[i])); } str.Append('>'); return(str); } }
private RubyStruct /*!*/ ReadStruct() { RubyStruct obj = (UnmarshalNewObject() as RubyStruct); if (obj == null) { throw RubyExceptions.CreateArgumentError("non-initialized struct"); } var names = obj.GetNames(); int count = ReadInt32(); if (count != names.Count) { throw RubyExceptions.CreateArgumentError("struct size differs"); } for (int i = 0; i < count; i++) { string name = ReadIdentifier(); if (name != names[i]) { RubyClass theClass = Context.GetClassOf(obj); throw RubyExceptions.CreateTypeError("struct {0} not compatible ({1} for {2})", theClass.Name, name, names[i]); } obj[i] = ReadAnObject(false); } return(obj); }
private static void FillTimes(RubyStruct /*!*/ result) { var process = Process.GetCurrentProcess(); RubyStructOps.TmsSetUserTime(result, process.UserProcessorTime.TotalSeconds); RubyStructOps.TmsSetSystemTime(result, process.PrivilegedProcessorTime.TotalSeconds); RubyStructOps.TmsSetChildUserTime(result, 0.0); RubyStructOps.TmsSetChildSystemTime(result, 0.0); }
public static RubyStruct /*!*/ InitializeCopy(RubyStruct /*!*/ self, [NotNull] RubyStruct /*!*/ source) { if (self.Class != source.Class) { throw RubyExceptions.CreateTypeError("wrong argument class"); } self.SetValues(source.Values); return(self); }
/// <summary> /// Struct#new /// Creates Struct classes with the specified name and members /// </summary> private static object Create(BlockParam block, RubyClass /*!*/ self, string className, string /*!*/[] /*!*/ attributeNames) { var result = RubyStruct.DefineStruct(self, className, attributeNames); if (block != null) { return(RubyUtils.EvaluateInModule(result, block, result)); } return(result); }
public static RubyStruct /*!*/ InitializeCopy(RubyStruct /*!*/ self, [NotNull] RubyStruct /*!*/ source) { // TODO: compare non-singleton classes? if (self.ImmediateClass.GetNonSingletonClass() != source.ImmediateClass.GetNonSingletonClass()) { throw RubyExceptions.CreateTypeError("wrong argument class"); } self.SetValues(source.Values); return(self); }
public static bool Equals(BinaryOpStorage /*!*/ equals, RubyStruct /*!*/ self, object obj) { var other = obj as RubyStruct; if (!self.StructReferenceEquals(other)) { return(false); } Debug.Assert(self.ItemCount == other.ItemCount); return(IListOps.Equals(equals, self.Values, other.Values)); }
public static object Each(BlockParam block, RubyStruct/*!*/ self) { if (block == null && self.ItemCount > 0) { throw RubyExceptions.NoBlockGiven(); } foreach (var value in self.Values) { object result; if (block.Yield(value, out result)) { return result; } } return self; }
public static RubyStruct /*!*/ GetTimes(RubyModule /*!*/ self) { var result = RubyStruct.Create(RubyStructOps.GetTmsClass(self.Context)); try { FillTimes(result); } catch (SecurityException) { RubyStructOps.TmsSetUserTime(result, 0.0); RubyStructOps.TmsSetSystemTime(result, 0.0); RubyStructOps.TmsSetChildUserTime(result, 0.0); RubyStructOps.TmsSetChildSystemTime(result, 0.0); } return(result); }
public static object EachPair(BlockParam block, RubyStruct/*!*/ self) { if (block == null && self.ItemCount > 0) { throw RubyExceptions.NoBlockGiven(); } var context = self.ImmediateClass.Context; foreach (KeyValuePair<string, object> entry in self.GetItems()) { object result; if (block.Yield(context.EncodeIdentifier(entry.Key), entry.Value, out result)) { return result; } } return self; }
public static object EachPair(BlockParam block, RubyStruct /*!*/ self) { if (block == null && self.ItemCount > 0) { throw RubyExceptions.NoBlockGiven(); } foreach (KeyValuePair <string, object> entry in self.GetItems()) { object result; if (block.Yield(SymbolTable.StringToId(entry.Key), entry.Value, out result)) { return(result); } } return(self); }
public static object Each(BlockParam block, RubyStruct /*!*/ self) { if (block == null && self.ItemCount > 0) { throw RubyExceptions.NoBlockGiven(); } foreach (var value in self.Values) { object result; if (block.Yield(value, out result)) { return(result); } } return(self); }
private void WriteStruct(RubyStruct /*!*/ obj) { SubclassData instanceWriter = new SubclassData(this, obj, typeof(RubyStruct)); _writer.Write((byte)'S'); RubyClass theClass = _context.GetClassOf(obj); TestForAnonymous(theClass); WriteSymbol(theClass.Name); var names = obj.GetNames(); WriteInt32(names.Count); foreach (string name in names) { int index = obj.GetIndex(name); WriteSymbol(name); WriteAnObject(obj[index]); } }
private void WriteStruct(RubyStruct /*!*/ obj) { WriteSubclassData(obj, typeof(RubyStruct)); _writer.Write((byte)'S'); RubyClass theClass = _context.GetClassOf(obj); TestForAnonymous(theClass); WriteModuleName(theClass); var names = obj.GetNames(); WriteInt32(names.Count); foreach (string name in names) { int index = obj.GetIndex(name); // TODO (encoding): WriteSymbol(name, _context.GetIdentifierEncoding()); WriteAnObject(obj[index]); } }
public static RubyArray /*!*/ ValuesAt(RubyStruct /*!*/ self, [NotNull] params object[] values) { RubyArray result = new RubyArray(); object[] data = self.Values; for (int i = 0; i < values.Length; ++i) { Range range = values[i] as Range; if (range != null) { bool excludeEnd; int begin, end; Protocols.ConvertToIntegerRange(self.Class.Context, range, out begin, out end, out excludeEnd); if (excludeEnd) { end -= 1; } begin = NormalizeIndex(data.Length, begin); end = NormalizeIndex(data.Length, end); Debug.Assert(end - begin <= data.Length); // because we normalized the indicies if (end - begin > 0) { result.Capacity += (end - begin); for (int j = begin; j <= end; j++) { result.Add(data[j]); } } } else { int index = NormalizeIndex(data.Length, Protocols.CastToFixnum(self.Class.Context, values[i])); result.Add(data[index]); } } return(result); }
public static object EachPair(BlockParam block, RubyStruct /*!*/ self) { if (block == null && self.ItemCount > 0) { throw RubyExceptions.NoBlockGiven(); } var context = self.ImmediateClass.Context; foreach (KeyValuePair <string, object> entry in self.GetItems()) { object result; if (block.Yield(context.EncodeIdentifier(entry.Key), entry.Value, out result)) { return(result); } } return(self); }
public static bool Equals(RubyStruct /*!*/ self, object obj) { var other = obj as RubyStruct; if (!self.StructReferenceEquals(other)) { return(false); } Debug.Assert(self.ItemCount == other.ItemCount); for (int i = 0; i < self.Values.Length; i++) { if (!RubySites.Equal(self.Class.Context, self.Values[i], other.Values[i])) { return(false); } } return(true); }
public static bool Equals(BinaryOpStorage /*!*/ equals, RubyStruct /*!*/ self, object obj) { var other = obj as RubyStruct; if (!self.StructReferenceEquals(other)) { return(false); } Debug.Assert(self.ItemCount == other.ItemCount); if (self.Values.Length > 0) { var site = equals.GetCallSite("=="); for (int i = 0; i < self.Values.Length; i++) { if (RubyOps.IsFalse(site.Target(site, self.Values[i], other.Values[i]))) { return(false); } } } return(true); }
public static void TmsSetChildUserTime(RubyStruct /*!*/ tms, double value) { tms[2] = value; }
public static object GetValue(RubyStruct /*!*/ self, int index) { return(self._data[index]); }
private void WriteStruct(RubyStruct/*!*/ obj) { WriteSubclassData(obj, typeof(RubyStruct)); _writer.Write((byte)'S'); RubyClass theClass = _context.GetClassOf(obj); TestForAnonymous(theClass); WriteModuleName(theClass); var names = obj.GetNames(); WriteInt32(names.Count); foreach (string name in names) { int index = obj.GetIndex(name); // TODO (encoding): WriteSymbol(name, _context.GetIdentifierEncoding()); WriteAnObject(obj[index]); } }
public static RubyArray /*!*/ Values(RubyStruct /*!*/ self) { return(new RubyArray(self.Values)); }
public static object Select(CallSiteStorage<EachSite>/*!*/ each, BlockParam predicate, RubyStruct/*!*/ self) { return Enumerable.Select(each, predicate, self); }
public static void Reinitialize(RubyStruct/*!*/ self, params object[]/*!*/ items) { self.SetValues(items); }
public static int Hash(UnaryOpStorage/*!*/ hashStorage, ConversionStorage<int>/*!*/ fixnumCast, RubyStruct/*!*/ self) { return self.GetHashCode(hashStorage, fixnumCast); }
public static bool Equals(BinaryOpStorage/*!*/ equals, RubyStruct/*!*/ self, object obj) { var other = obj as RubyStruct; if (!self.StructReferenceEquals(other)) { return false; } Debug.Assert(self.ItemCount == other.ItemCount); return IListOps.Equals(equals, self.Values, other.Values); }
public static bool Equal(RubyStruct /*!*/ self, object other) { return(self.Equals(other)); }
public bool StructReferenceEquals(RubyStruct other) { // TODO: compare non-singleton classes? return(ReferenceEquals(this, other) || (other != null && ImmediateClass.GetNonSingletonClass() == other.ImmediateClass.GetNonSingletonClass())); }
public static RubyArray /*!*/ GetMembers(RubyStruct /*!*/ self) { Debug.Assert(self.StructInfo != null); return(self.StructInfo.GetMembers(self.ImmediateClass.Context)); }
public static object SetValue(RubyStruct /*!*/ self, object index, object value) { return(self[NormalizeIndex(self.ItemCount, Protocols.CastToFixnum(self.Class.Context, index))] = value); }
public static RubyArray /*!*/ Select(BlockParam predicate, RubyStruct /*!*/ self) { return(Enumerable.Select(self.Class.Context, predicate, self)); }
public static RubyArray/*!*/ Values(RubyStruct/*!*/ self) { return new RubyArray(self.Values); }
public static object SetValue(RubyStruct /*!*/ self, int index, object value) { return(self._data[index] = value); }
public static bool Equal(BinaryOpStorage/*!*/ eqlStorage, RubyStruct/*!*/ self, object other) { return self.Equals(eqlStorage, other); }
public static Node ToYamlNode(RubyStruct/*!*/ self, [NotNull]RubyRepresenter/*!*/ rep) { var fieldNames = self.GetNames(); var map = new Dictionary<MutableString, object>(fieldNames.Count); for (int i = 0; i < fieldNames.Count; i++) { map[MutableString.Create(fieldNames[i])] = self.GetValue(i); } rep.AddYamlProperties(self, map); return rep.Map(self, map); }
public static MutableString/*!*/ Inspect(RubyStruct/*!*/ self) { RubyContext context = self.ImmediateClass.Context; using (IDisposable handle = RubyUtils.InfiniteInspectTracker.TrackObject(self)) { // #<struct Struct::Foo name=nil, val=nil> var result = MutableString.CreateMutable(RubyEncoding.Binary); result.Append("#<struct "); result.Append(context.Inspect(context.GetClassOf(self))); if (handle == null) { return result.Append(":...>"); } result.Append(' '); object[] data = self.Values; var members = self.GetNames(); for (int i = 0; i < data.Length; i++) { if (i != 0) { result.Append(", "); } // TODO (encoding): result.Append(members[i]); result.Append('='); result.Append(context.Inspect(data[i])); } result.Append('>'); return result; } }
public static Node ToYamlNode(RubyStruct/*!*/ self, [NotNull]RubyRepresenter/*!*/ rep) { RubyContext context = self.Class.Context; RubyArray members = _Members.Target(_Members, context, self); RubyArray values = _Values.Target(_Values, context, self); if (members.Count != values.Count) { throw new ArgumentException("Struct values and members returned arrays of different lengths"); } Hash map = new Hash(self.Class.Context); for (int i = 0; i < members.Count; i++) { IDictionaryOps.SetElement(context, map, members[i], values[i]); } RubyRepresenter.AddYamlProperties(context, self, map); return rep.Map(self, map); }
public static RubyArray/*!*/ ValuesAt(ConversionStorage<int>/*!*/ fixnumCast, RubyStruct/*!*/ self, params object[]/*!*/ values) { RubyArray result = new RubyArray(); object[] data = self.Values; for (int i = 0; i < values.Length; ++i) { Range range = values[i] as Range; if (range != null) { int begin = Protocols.CastToFixnum(fixnumCast, range.Begin); int end = Protocols.CastToFixnum(fixnumCast, range.End); if (range.ExcludeEnd) { end -= 1; } begin = NormalizeIndex(data.Length, begin); end = NormalizeIndex(data.Length, end); Debug.Assert(end - begin <= data.Length); // because we normalized the indicies if (end - begin > 0) { result.AddCapacity(end - begin); for (int j = begin; j <= end; j++) { result.Add(data[j]); } } } else { int index = NormalizeIndex(data.Length, Protocols.CastToFixnum(fixnumCast, values[i])); result.Add(data[index]); } } return result; }
// friend: RubyContext // tracker: non-null => show members declared on the tracker internal RubyClass(RubyContext/*!*/ context, string name, Type type, object singletonClassOf, Action<RubyModule> methodsInitializer, Action<RubyModule> constantsInitializer, Delegate/*!*/[] factories, RubyClass superClass, RubyModule/*!*/[] expandedMixins, TypeTracker tracker, RubyStruct.Info structInfo, bool isRubyClass, bool isSingletonClass, ModuleRestrictions restrictions) : base(context, name, methodsInitializer, constantsInitializer, expandedMixins, type != typeof(object) ? null : context.Namespaces, tracker, restrictions) { Debug.Assert(context.Namespaces != null, "Namespaces should be initialized"); Debug.Assert(superClass != null || structInfo == null, "BasicObject is not a struct"); Debug.Assert(!isRubyClass || tracker == null, "Ruby class cannot have a tracker"); Debug.Assert(singletonClassOf != null || !isSingletonClass, "Singleton classes don't have a type"); Debug.Assert(superClass != this); _underlyingSystemType = type; _superClass = superClass; _isSingletonClass = isSingletonClass; _isRubyClass = isRubyClass; _singletonClassOf = singletonClassOf; _factories = factories ?? Utils.EmptyDelegates; if (superClass != null) { _level = superClass.Level + 1; _structInfo = structInfo ?? superClass._structInfo; } else { _level = 0; } }
public static RubyStruct/*!*/ InitializeCopy(RubyStruct/*!*/ self, [NotNull]RubyStruct/*!*/ source) { // TODO: compare non-singleton classes? if (self.ImmediateClass.GetNonSingletonClass() != source.ImmediateClass.GetNonSingletonClass()) { throw RubyExceptions.CreateTypeError("wrong argument class"); } self.SetValues(source.Values); return self; }
public static void TmsSetChildUserTime(RubyStruct/*!*/ tms, double value) { tms[2] = value; }
public static RubyArray/*!*/ GetMembers(RubyStruct/*!*/ self) { Debug.Assert(self.Class.StructInfo != null); return self.Class.StructInfo.GetMembers(); }
public static object SetValue(RubyStruct /*!*/ self, MutableString /*!*/ name, object value) { return(self[name.ConvertToString()] = value); }
internal static RubyClass /*!*/ CreateTmsClass(RubyModule /*!*/ module) { // class is available for the library even if the constant is removed => store it on the context: return((RubyClass)module.Context.GetOrCreateLibraryData(TmsStructClassKey, () => RubyStruct.DefineStruct( (RubyClass)module, "Tms", new[] { "utime", "stime", "cutime", "cstime" } ))); }
public static object SetValue(RubyStruct/*!*/ self, [NotNull]MutableString/*!*/ name, object value) { return self[name.ConvertToString()] = value; }
public static void TmsSetChildSystemTime(RubyStruct /*!*/ tms, double value) { tms[3] = value; }
public static object SetValue(ConversionStorage<int>/*!*/ conversionStorage, RubyStruct/*!*/ self, object index, object value) { return self[NormalizeIndex(self.ItemCount, Protocols.CastToFixnum(conversionStorage, index))] = value; }
public static bool Equals(BinaryOpStorage/*!*/ equals, RubyStruct/*!*/ self, object obj) { var other = obj as RubyStruct; if (!self.StructReferenceEquals(other)) { return false; } Debug.Assert(self.ItemCount == other.ItemCount); if (self.Values.Length > 0) { var site = equals.GetCallSite("=="); for (int i = 0; i < self.Values.Length; i++) { if (RubyOps.IsFalse(site.Target(site, self.Values[i], other.Values[i]))) { return false; } } } return true; }
public static int Hash(RubyStruct /*!*/ self) { return(self.GetHashCode()); }
// friend: RubyContext // tracker: non-null => show members declared on the tracker internal RubyClass(RubyContext/*!*/ context, string name, Type type, object singletonClassOf, Action<RubyModule> methodsInitializer, Action<RubyModule> constantsInitializer, Delegate/*!*/[] factories, RubyClass superClass, RubyModule/*!*/[] expandedMixins, TypeTracker tracker, RubyStruct.Info structInfo, bool isRubyClass, bool isSingletonClass, ModuleRestrictions restrictions) : base(context, name, methodsInitializer, constantsInitializer, expandedMixins, null, tracker, restrictions) { Debug.Assert((superClass == null) == (type == typeof(object)), "All classes have a superclass, except for Object"); Debug.Assert(superClass != null || structInfo == null, "Object is not a struct"); Debug.Assert(!isRubyClass || tracker == null, "Ruby class cannot have a tracker"); Debug.Assert(singletonClassOf != null || !isSingletonClass, "Singleton classes don't have a type"); Debug.Assert(superClass != this); _underlyingSystemType = type; _superClass = superClass; _isSingletonClass = isSingletonClass; _isRubyClass = isRubyClass; _singletonClassOf = singletonClassOf; _factories = factories ?? Utils.EmptyDelegates; if (superClass != null) { _level = superClass.Level + 1; _structInfo = structInfo ?? superClass._structInfo; } else { _level = 0; } _weakSelf = new WeakReference(this); Version = new VersionHandle(Interlocked.Increment(ref _globalVersion)); Version.SetName(name); }
private void WriteStruct(RubyStruct/*!*/ obj) { SubclassData instanceWriter = new SubclassData(this, obj, typeof(RubyStruct)); _writer.Write((byte)'S'); RubyClass theClass = _context.GetClassOf(obj); TestForAnonymous(theClass); WriteSymbol(theClass.Name); var names = obj.GetNames(); WriteInt32(names.Count); foreach (string name in names) { int index = obj.GetIndex(name); WriteSymbol(name); WriteAnObject(obj[index]); } }
public static MutableString TagUri(RubyStruct/*!*/ self) { MutableString str = MutableString.Create("tag:ruby.yaml.org,2002:struct:"); string name = self.Class.Name; string structPrefix = "Struct::"; if (name.StartsWith(structPrefix)) { name = name.Substring(structPrefix.Length); } return str.Append(name); }
public static RubyArray/*!*/ GetMembers(RubyStruct/*!*/ self) { return RubyStruct.GetMembers(self); }
private static void FillTimes(RubyStruct/*!*/ result) { var process = Process.GetCurrentProcess(); RubyStructOps.TmsSetUserTime(result, process.UserProcessorTime.TotalSeconds); RubyStructOps.TmsSetSystemTime(result, process.PrivilegedProcessorTime.TotalSeconds); RubyStructOps.TmsSetChildUserTime(result, 0.0); RubyStructOps.TmsSetChildSystemTime(result, 0.0); }
public static int GetSize(RubyStruct/*!*/ self) { return self.ItemCount; }
public static void TmsSetChildSystemTime(RubyStruct/*!*/ tms, double value) { tms[3] = value; }
public static object SetValue(RubyStruct/*!*/ self, int index, object value) { return self[NormalizeIndex(self.ItemCount, index)] = value; }
public bool StructReferenceEquals(RubyStruct other) { return ReferenceEquals(this, other) || (other != null && Class == other.Class); }
public static object SetValue(RubyStruct/*!*/ self, [NotNull]RubySymbol/*!*/ name, object value) { return self[name.ToString()] = value; }