public static Proc /*!*/ CreateNew(CallSiteStorage <Func <CallSite, object, object> > /*!*/ storage, RubyScope /*!*/ scope, RubyClass /*!*/ self) { RubyMethodScope methodScope = scope.GetInnerMostMethodScope(); if (methodScope == null || methodScope.BlockParameter == null) { throw RubyExceptions.CreateArgumentError("tried to create Proc object without a block"); } var proc = methodScope.BlockParameter; // an instance of Proc class, the identity is preserved: if (self.GetUnderlyingSystemType() == typeof(Proc)) { return(proc); } // an instance of a Proc subclass: var result = new Proc.Subclass(self, proc); var initialize = storage.GetCallSite("initialize", new RubyCallSignature(0, RubyCallFlags.HasImplicitSelf)); initialize.Target(initialize, result); return(result); }
public static Proc /*!*/ CreateNew(RubyClass /*!*/ self, Proc /*!*/ proc) { Assert.NotNull(self, proc); // an instance of Proc class, the identity is preserved: if (self.GetUnderlyingSystemType() == typeof(Proc)) { return(proc); } // an instance of a Proc subclass: var result = new Proc.Subclass(self, proc); // a call to the initializer with a block: object initResult = null; do { // a new proc is created each iteration (even if a subclass is passed in, the Proc class is created): var argProc = proc.Create(proc); try { initResult = _InitializeSite.Target(_InitializeSite, self.Context, proc, argProc); } catch (EvalUnwinder u) { initResult = u.ReturnValue; } Debug.Assert(proc != argProc, "retry doesn't propagate to the caller"); } while (RubyOps.IsRetrySingleton(initResult)); return(result); }
public static object CreateNew(CallSiteStorage <Func <CallSite, object, object, object> > /*!*/ storage, BlockParam block, RubyClass /*!*/ self) { if (block == null) { throw RubyExceptions.CreateArgumentError("tried to create Proc object without a block"); } var proc = block.Proc; // an instance of Proc class, the identity is preserved: if (self.GetUnderlyingSystemType() == typeof(Proc)) { return(proc); } // an instance of a Proc subclass: var result = new Proc.Subclass(self, proc); // propagate retry and return control flow: var initialize = storage.GetCallSite("initialize", new RubyCallSignature(0, RubyCallFlags.HasImplicitSelf | RubyCallFlags.HasBlock)); object initResult = initialize.Target(initialize, result, block.Proc); if (initResult is BlockReturnResult) { return(initResult); } return(result); }
public static Exception/*!*/ CreateMissingDefaultConstructorError(RubyClass/*!*/ rubyClass, string/*!*/ initializerOwnerName) { Debug.Assert(rubyClass.IsRubyClass); Type baseType = rubyClass.GetUnderlyingSystemType().BaseType; Debug.Assert(baseType != null); return CreateTypeError("can't allocate class `{1}' that derives from type `{0}' with no default constructor;" + " define {1}#new singleton method instead of {2}#initialize", rubyClass.Context.GetTypeName(baseType, true), rubyClass.Name, initializerOwnerName ); }
private void WriteObject(object /*!*/ obj) { _writer.Write((byte)'o'); RubyClass theClass = _context.GetClassOf(obj); TestForAnonymous(theClass); WriteSymbol(theClass.Name); #if !SILVERLIGHT ISerializable serializableObj = (obj as ISerializable); if (serializableObj != null) { SerializationInfo info = new SerializationInfo(theClass.GetUnderlyingSystemType(), new FormatterConverter()); serializableObj.GetObjectData(info, _streamingContext); int count = info.MemberCount; try { // We need this attribute for CLR serialization but it's not compatible with MRI serialization // Unfortunately, there's no way to test for a value without either iterating over all values // or throwing an exception if it's not present if (info.GetValue("#class", typeof(RubyClass)) != null) { count--; } } catch (Exception) { } WriteInt32(count); foreach (SerializationEntry entry in info) { if (!entry.Name.Equals("#class")) { WriteSymbol(entry.Name); WriteAnObject(entry.Value); } } return; } #endif }
public static object/*!*/ CreateVector(ConversionStorage<Union<IList, int>>/*!*/ toAryToInt, BlockParam block, RubyClass/*!*/ self, [NotNull]object/*!*/ arrayOrSize) { var elementType = self.GetUnderlyingSystemType().GetElementType(); Debug.Assert(elementType != null); var site = toAryToInt.GetSite(CompositeConversionAction.Make(self.Context, CompositeConversion.ToAryToInt)); var union = site.Target(site, arrayOrSize); if (union.First != null) { // block ignored return CreateVectorInternal(self.Context, elementType, union.First); } else if (block != null) { return PopulateVector(self.Context, CreateVectorInternal(elementType, union.Second), block); } else { return CreateVectorInternal(elementType, union.Second); } }
public static Proc/*!*/ CreateNew(CallSiteStorage<Func<CallSite, Proc, Proc, object>>/*!*/ storage, RubyClass/*!*/ self, Proc/*!*/ proc) { Assert.NotNull(storage, self, proc); // an instance of Proc class, the identity is preserved: if (self.GetUnderlyingSystemType() == typeof(Proc)) { return proc; } // an instance of a Proc subclass: var result = new Proc.Subclass(self, proc); var initialize = storage.GetCallSite("initialize", new RubyCallSignature(0, RubyCallFlags.HasImplicitSelf | RubyCallFlags.HasBlock)); // a call to the initializer with a block: object initResult = null; do { // a new proc is created each iteration (even if a subclass is passed in, the Proc class is created): var argProc = proc.Create(proc); try { initResult = initialize.Target(initialize, proc, argProc); } catch (EvalUnwinder u) { initResult = u.ReturnValue; } Debug.Assert(proc != argProc, "retry doesn't propagate to the caller"); } while (RubyOps.IsRetrySingleton(initResult)); return result; }
/// <summary> /// Creates a blank instance of a RubyArray or its subclass given the Ruby class object. /// </summary> public static RubyArray /*!*/ CreateInstance(RubyClass /*!*/ rubyClass) { return((rubyClass.GetUnderlyingSystemType() == typeof(RubyArray)) ? new RubyArray() : new RubyArray.Subclass(rubyClass)); }
/// <summary> /// Creates a blank instance of a RubyArray or its subclass given the Ruby class object. /// </summary> public static Hash /*!*/ CreateInstance(RubyClass /*!*/ rubyClass) { return((rubyClass.GetUnderlyingSystemType() == typeof(Hash)) ? new Hash(rubyClass.Context) : new Hash.Subclass(rubyClass)); }
public static object/*!*/ CreateObjectAndSetIvars(RubyClass theClass, IDictionary<string, object> attributes = null) { Assert.NotNull(theClass); object result = null; BindingFlags bindingFlags = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance; ConstructorInfo ci; Type baseType = theClass.GetUnderlyingSystemType(); if (baseType == typeof(RubyStruct)) { return CreateStruct(theClass, baseType, attributes); // we never set ivars on structs } else if (typeof(Range).IsAssignableFrom(baseType)) { result = CreateRange(theClass, baseType, attributes); } else if (typeof(Exception).IsAssignableFrom(baseType)) { result = CreateException(theClass, baseType, attributes); } else if (IsAvailable(ci = baseType.GetConstructor(bindingFlags, null, Type.EmptyTypes, null))) { result = ci.Invoke(new object[0] { }); } else if (IsAvailable(ci = baseType.GetConstructor(bindingFlags, null, _ccTypes1, null))) { result = ci.Invoke(new object[1] { theClass }); } else if (IsAvailable(ci = baseType.GetConstructor(bindingFlags, null, _ccTypes2, null))) { result = ci.Invoke(new object[1] { theClass.Context }); } else { string message = String.Format("Class {0} does not have a valid constructor", theClass.Name); throw new NotSupportedException(message); } if (attributes != null) { foreach (var kv in attributes) { theClass.Context.SetInstanceVariable(result, kv.Key, kv.Value); } } return result; }
public Type /*!*/ GetUnderlyingSystemType() { if (_isSingletonClass) { throw new InvalidOperationException("Singleton class doesn't have underlying system type."); } if (_underlyingSystemType == null) { Interlocked.Exchange(ref _underlyingSystemType, RubyTypeDispenser.GetOrCreateType(_superClass.GetUnderlyingSystemType(), GetClrInterfaces())); } Debug.Assert(_underlyingSystemType != null); return(_underlyingSystemType); }
public static object/*!*/ CreateObject(RubyClass/*!*/ theClass) { Assert.NotNull(theClass); Type baseType = theClass.GetUnderlyingSystemType(); if (baseType == typeof(RubyStruct)) { return RubyStruct.Create(theClass); } object result; BindingFlags bindingFlags = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance; ConstructorInfo ci; if (IsAvailable(ci = baseType.GetConstructor(bindingFlags, null, Type.EmptyTypes, null))) { result = ci.Invoke(new object[0] { }); } else if (IsAvailable(ci = baseType.GetConstructor(bindingFlags, null, _ccTypes1, null))) { result = ci.Invoke(new object[1] { theClass }); } else if (IsAvailable(ci = baseType.GetConstructor(bindingFlags, null, _ccTypes2, null))) { result = ci.Invoke(new object[1] { theClass.Context }); } else { string message = String.Format("Class {0} does not have a valid constructor", theClass.Name); throw new NotSupportedException(message); } return result; }
public static Array/*!*/ CreateVectorWithValues(RubyClass/*!*/ self, [DefaultProtocol]int size, object value) { var elementType = self.GetUnderlyingSystemType().GetElementType(); Debug.Assert(elementType != null); var result = CreateVectorInternal(elementType, size); for (int i = 0; i < size; i++) { SetVectorItem(self.Context, result, i, value); } return result; }
public static object/*!*/ CreateObject(RubyClass/*!*/ theclass, Hash/*!*/ attributes, bool decorate) { Assert.NotNull(theclass, attributes); Type baseType = theclass.GetUnderlyingSystemType(); object obj; if (typeof(ISerializable).IsAssignableFrom(baseType)) { #if !SILVERLIGHT BindingFlags bindingFlags = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance; ConstructorInfo ci = baseType.GetConstructor(bindingFlags, null, _serializableTypeSignature, null); if (ci == null) { #endif string message = String.Format("Class {0} does not have a valid deserializing constructor", baseType.FullName); throw new NotSupportedException(message); #if !SILVERLIGHT } SerializationInfo info = new SerializationInfo(baseType, new FormatterConverter()); info.AddValue("#class", theclass); foreach (KeyValuePair<object, object> pair in attributes) { string key = pair.Key.ToString(); key = decorate ? "@" + key : key; info.AddValue(key, pair.Value); } obj = ci.Invoke(new object[2] { info, new StreamingContext(StreamingContextStates.Other, theclass) }); #endif } else { obj = CreateObject(theclass); foreach (KeyValuePair<object, object> pair in attributes) { string key = pair.Key.ToString(); key = decorate ? "@" + key : key; theclass.Context.SetInstanceVariable(obj, key, pair.Value); } } return obj; }
public static Proc/*!*/ CreateNew(CallSiteStorage<Func<CallSite, object, object>>/*!*/ storage, RubyScope/*!*/ scope, RubyClass/*!*/ self) { RubyMethodScope methodScope = scope.GetInnerMostMethodScope(); if (methodScope == null || methodScope.BlockParameter == null) { throw RubyExceptions.CreateArgumentError("tried to create Proc object without a block"); } var proc = methodScope.BlockParameter; // an instance of Proc class, the identity is preserved: if (self.GetUnderlyingSystemType() == typeof(Proc)) { return proc; } // an instance of a Proc subclass: var result = new Proc.Subclass(self, proc); var initialize = storage.GetCallSite("initialize", new RubyCallSignature(0, RubyCallFlags.HasImplicitSelf)); initialize.Target(initialize, result); return result; }
public static Proc/*!*/ CreateNew(RubyClass/*!*/ self, Proc/*!*/ proc) { Assert.NotNull(self, proc); // an instance of Proc class, the identity is preserved: if (self.GetUnderlyingSystemType() == typeof(Proc)) { return proc; } // an instance of a Proc subclass: var result = new Proc.Subclass(self, proc); // a call to the initializer with a block: object initResult = null; do { // a new proc is created each iteration (even if a subclass is passed in, the Proc class is created): var argProc = proc.Create(proc); try { initResult = _InitializeSite.Target(_InitializeSite, self.Context, proc, argProc); } catch (EvalUnwinder u) { initResult = u.ReturnValue; } Debug.Assert(proc != argProc, "retry doesn't propagate to the caller"); } while (RubyOps.IsRetrySingleton(initResult)); return result; }
public static object CreateNew(CallSiteStorage<Func<CallSite, object, object, object>>/*!*/ storage, BlockParam block, RubyClass/*!*/ self) { if (block == null) { throw RubyExceptions.CreateArgumentError("tried to create Proc object without a block"); } var proc = block.Proc; // an instance of Proc class, the identity is preserved: if (self.GetUnderlyingSystemType() == typeof(Proc)) { return proc; } // an instance of a Proc subclass: var result = new Proc.Subclass(self, proc); // propagate retry and return control flow: var initialize = storage.GetCallSite("initialize", new RubyCallSignature(0, RubyCallFlags.HasImplicitSelf | RubyCallFlags.HasBlock)); object initResult = initialize.Target(initialize, result, block.Proc); if (initResult is BlockReturnResult) { return initResult; } return result; }