internal RubyLambdaMethodInfo(Proc/*!*/ block, string/*!*/ definitionName, RubyMemberFlags flags, RubyModule/*!*/ declaringModule) : base(flags, declaringModule) { Assert.NotNull(block, definitionName, declaringModule); _lambda = block.ToLambda(this); _definitionName = definitionName; _id = Interlocked.Increment(ref _Id); }
public static object MethodRetry(RubyScope/*!*/ scope, Proc proc) { if (proc != null) { return RetrySingleton; } else { throw new LocalJumpError("retry used out of rescue", scope.FlowControlScope); } }
public void OverloadResolution_Block1() { var scope = Context.EmptyScope; var proc = new Proc(ProcKind.Proc, null, scope, new BlockDispatcher0(BlockSignatureAttributes.None, null, 0). SetMethod(new BlockCallTarget0((x, y) => null))); var arguments = new[] { // 1.times new CallArguments(Context, MO(scope), new[] { MO(1) }, RubyCallSignature.WithScope(0)), // 1.times &nil new CallArguments(Context, MO(scope), new[] { MO(1), MO(null) }, RubyCallSignature.WithScopeAndBlock(0)), // 1.times &p new CallArguments(Context, MO(1), new[] { MO(proc) }, RubyCallSignature.WithBlock(0)), // obj.times &p new CallArguments(Context, MO("foo"), new[] { MO(proc) }, RubyCallSignature.WithBlock(0)), }; var results = new[] { "Times2", "Times1", "Times3", "Times4", }; var metaBuilder = new MetaObjectBuilder(null); for (int i = 0; i < arguments.Length; i++) { RubyOverloadResolver resolver; var bindingTarget = RubyMethodGroupInfo.ResolveOverload( metaBuilder, arguments[i], "times", GetStaticMethods(typeof(OverloadsWithBlock), "Times*"), SelfCallConvention.SelfIsParameter, false, out resolver ); Assert(bindingTarget.Success); Assert(bindingTarget.Method.Name == results[i]); } }
public RubyImportDefinition(string contractName, Proc mutator) : base(contractName, null, null, ImportCardinality.ZeroOrOne, false, true, System.ComponentModel.Composition.CreationPolicy.Any) { if (mutator == null) throw new ArgumentNullException("mutator"); _mutator = mutator; }
// called by Proc#new rule when creating a Ruby subclass of Proc: public Subclass(RubyClass/*!*/ rubyClass, Proc/*!*/ proc) : base(proc) { Assert.NotNull(rubyClass); Debug.Assert(!rubyClass.IsSingletonClass); ImmediateClass = rubyClass; }
public static object MethodRetry(RuntimeFlowControl/*!*/ rfc, Proc proc) { if (proc != null) { return RetrySingleton; } else { throw new LocalJumpError("retry used out of rescue", rfc); } }
private static object Each(RubyContext/*!*/ context, object self, Proc/*!*/ block) { if (self is Enumerator) { return ((Enumerator)self).Each(context, block); } else { return RubySites.Each(context, self, block); } }
private static object Each(CallSiteStorage<EachSite>/*!*/ each, RubyContext/*!*/ context, object self, Proc/*!*/ block) { var enumerator = self as Enumerator; if (enumerator != null) { return enumerator.Each(context, block); } else { var site = each.GetCallSite("each", RubyCallSignature.WithBlock(0)); return site.Target(site, context, self, block); } }
public static object MethodRetry(RubyScope/*!*/ scope, Proc proc) { if (proc != null) { return BlockReturnResult.Retry; } else { // TODO: can this happen? // If proc was null then the block argument passed to the call-with-block that returned RetrySingleton would be null and thus // the call cannot yield to any block that retries. throw new LocalJumpError("retry used out of rescue", scope.FlowControlScope); } }
public RubyPart(RubyPartDefinition definition, Proc createInstance) { if (definition == null) throw new ArgumentNullException("definition"); if (createInstance == null) throw new ArgumentNullException("createInstance"); _definition = definition; _instance = new DelayedInit<object>(() => createInstance.Call(createInstance)); }
public static RuntimeFlowControl/*!*/ CreateRfcForMethod(Proc proc) { RuntimeFlowControl result = new RuntimeFlowControl(); result.IsActiveMethod = true; if (proc != null && proc.Kind == ProcKind.Block) { proc.Kind = ProcKind.Proc; proc.Converter = result; } return result; }
public RubyPartDefinition(string displayName, Proc createPart) { if (createPart == null) throw new ArgumentNullException("createPart"); if (displayName == null) throw new ArgumentNullException("displayName"); _createPart = createPart; _displayName = displayName; }
public static object Trap( RubyContext/*!*/ context, object self, object signalId, Proc proc) { if ((signalId is MutableString) && ((MutableString)signalId).ConvertToString() == "INT") { context.InterruptSignalHandler = delegate() { proc.Call(); }; } else { // TODO: For now, just ignore unknown signals. This should be changed to throw an // exception. We are not doing it yet as it is close to the V1 RTM, and throwing // an exception might cause some app to misbehave whereas it might have happenned // to work if no exception is thrown } return null; }
public static MutableString/*!*/ ToS(Proc/*!*/ self) { var context = self.LocalScope.RubyContext; var str = RubyUtils.ObjectToMutableStringPrefix(context, self); str.Append('@'); str.Append(self.SourcePath ?? "(unknown)"); str.Append(':'); str.Append(self.SourceLine.ToString()); if (context.RubyOptions.Compatibility >= RubyCompatibility.Ruby19 && self.Kind == ProcKind.Lambda) { str.Append(" (lambda)"); } str.Append('>'); return str; }
public void OverloadResolution_Block() { var t = GetType(); var gse = new RubyGlobalScope(Context, new Scope(), new object(), true); var scope = new RubyTopLevelScope(gse, null, new SymbolDictionary()); var proc = new Proc(ProcKind.Proc, null, scope, new BlockDispatcher0((x, y) => null, BlockSignatureAttributes.None)); var scopeArg = new DynamicMetaObject(Ast.Constant(proc.LocalScope), BindingRestrictions.Empty, proc.LocalScope); var contextArg = new DynamicMetaObject(Ast.Constant(Context), BindingRestrictions.Empty, Context); var instanceInt = new DynamicMetaObject(Ast.Constant(1), BindingRestrictions.Empty, 1); var str = "foo"; var instanceStr = new DynamicMetaObject(Ast.Constant(str), BindingRestrictions.Empty, str); var procArg = new DynamicMetaObject(Ast.Constant(proc), BindingRestrictions.Empty, proc); var nullArg = new DynamicMetaObject(Ast.Constant(Ast.Constant(null)), BindingRestrictions.Empty, null); var arguments = new[] { // 1.times new CallArguments(scopeArg, instanceInt, new DynamicMetaObject[0], RubyCallSignature.WithScope(0)), // 1.times &nil new CallArguments(scopeArg, instanceInt, new[] { nullArg }, RubyCallSignature.WithScopeAndBlock(0)), // 1.times &p new CallArguments(contextArg, instanceInt, new[] { procArg }, RubyCallSignature.WithBlock(0)), // obj.times &p new CallArguments(contextArg, instanceStr, new[] { procArg }, RubyCallSignature.WithBlock(0)), }; var results = new[] { "Times2", "Times1", "Times3", "Times4", }; for (int i = 0; i < arguments.Length; i++) { var bindingTarget = RubyMethodGroupInfo.ResolveOverload("times", new[] { t.GetMethod("Times1"), t.GetMethod("Times2"), t.GetMethod("Times3"), t.GetMethod("Times4"), }, arguments[i], true, false); Assert(bindingTarget.Success); Assert(bindingTarget.Method.Name == results[i]); } }
public static Proc /*!*/ DefineMethod(RubyScope /*!*/ scope, RubyModule /*!*/ self, [DefaultProtocol, NotNull] string /*!*/ methodName, [NotNull] Proc /*!*/ block) { var visibility = GetDefinedMethodVisibility(scope, self, methodName); var info = Proc.ToLambdaMethodInfo(block, methodName, visibility, self); self.AddMethod(scope.RubyContext, methodName, info); return(info.Lambda); }
public static object Load(RubyScope /*!*/ scope, RubyModule /*!*/ self, [NotNull] RubyIO /*!*/ source, [Optional] Proc proc) { BinaryReader reader = source.GetBinaryReader(); MarshalReader loader = new MarshalReader(reader, scope.RubyContext, scope.GlobalScope, proc); return(loader.Load()); }
public static Proc /*!*/ ToProc(Proc /*!*/ self) { return(self); }
public static object Load(RubyScope /*!*/ scope, RubyModule /*!*/ self, object source, [Optional] Proc proc) { Stream stream = null; if (source != null) { stream = new IOWrapper(self.Context, source, FileAccess.Read); } if (stream == null || !stream.CanRead) { throw RubyExceptions.CreateTypeError("instance of IO needed"); } BinaryReader reader = new BinaryReader(stream); MarshalReader loader = new MarshalReader(reader, scope.RubyContext, scope.GlobalScope, proc); return(loader.Load()); }
internal static object Each(CallSiteStorage <EachSite> /*!*/ each, object self, Proc /*!*/ block) { var site = each.GetCallSite("each", new RubyCallSignature(0, RubyCallFlags.HasImplicitSelf | RubyCallFlags.HasBlock)); return(site.Target(site, self, block)); }
public static object Call(Proc /*!*/ self, params object[] /*!*/ args) { RequireParameterCount(self, args.Length); return(self.CallN(args)); }
public static Proc/*!*/ Clone(Proc/*!*/ self) { return self.Copy(); }
protected Proc(Proc /*!*/ proc) : this(proc.Kind, proc.Self, proc.LocalScope, proc.Dispatcher) { _owner = proc._owner; _converter = proc._converter; }
public static object Trap(RubyContext /*!*/ context, object self, object signalId, Proc proc) { // TODO: For now, just ignore the signal handler. The full implementation will need to build on // the signal and raise functions in msvcrt. return(null); }
/// <summary> /// Creates a copy of the proc that has the same target, context and self object as this block. /// </summary> public Proc /*!*/ Create(Proc /*!*/ proc) { return(new Proc(proc)); }
public static RubyMemberInfo /*!*/ ToLambdaMethodInfo(Proc /*!*/ lambda, string /*!*/ definitionName, RubyMethodVisibility visibility, RubyModule /*!*/ owner) { return(new RubyLambdaMethodInfo(lambda, definitionName, (RubyMemberFlags)visibility, owner)); }
public static object Zip(CallSiteStorage <EachSite> /*!*/ each, ConversionStorage <IList> /*!*/ tryToAry, CallSiteStorage <EachSite> /*!*/ otherEach, BlockParam block, object self, [DefaultProtocol, NotNullItems] params object /*!*/[] /*!*/ args) { RubyArray results = (block == null) ? new RubyArray() : null; object result = results; // coerce the args into arrays var coercedArgs = new List <IList>(args.Length); foreach (var otherArrayObject in args) { IList otherArray = Protocols.TryCastToArray(tryToAry, otherArrayObject); if (otherArray != null) { coercedArgs.Add(otherArray); } else // added in MRI 1.9.2 - if to_ary fails, try call .each to extract values { otherArray = new List <object>(); Each(otherEach, otherArrayObject, Proc.Create(otherEach.Context, delegate(BlockParam /*!*/ selfBlock, object _, object item) { otherArray.Add(item); return(null); })); coercedArgs.Add(otherArray); } } int index = 0; Each(each, self, Proc.Create(each.Context, delegate(BlockParam /*!*/ selfBlock, object _, object item) { // Collect items RubyArray array = new RubyArray(args.Length + 1); array.Add(item); foreach (var otherArray in coercedArgs) { if (index < otherArray.Count) { array.Add(otherArray[index]); } else { array.Add(null); } } index += 1; if (block != null) { object blockResult; if (block.Yield(array, out blockResult)) { result = blockResult; return(selfBlock.PropagateFlow(block, blockResult)); } } else { results.Add(array); } return(null); })); return(result); }
public static object GetExtremes(CallSiteStorage <EachSite> /*!*/ each, ComparisonStorage /*!*/ comparisonStorage, BlockParam comparer, object self) { bool hasOddItem = false, hasMinMax = false, blockJumped = false; object oddItem = null; object blockResult = null; object min = null, max = null; Func <IronRuby.Runtime.BlockParam, object, object, object> blockProc = delegate(BlockParam /*!*/ selfBlock, object _, object item) { if (hasOddItem) { hasOddItem = false; int?compareResult = CompareItems(comparisonStorage, oddItem, item, comparer, out blockResult); if (compareResult == null) { goto BlockJumped; } if (compareResult > 0) { // oddItem > item object obj = item; item = oddItem; oddItem = obj; } // oddItem <= item if (hasMinMax) { compareResult = CompareItems(comparisonStorage, oddItem, min, comparer, out blockResult); if (compareResult == null) { goto BlockJumped; } if (compareResult < 0) { // oddItem < min min = oddItem; } compareResult = CompareItems(comparisonStorage, item, max, comparer, out blockResult); if (compareResult == null) { goto BlockJumped; } if (compareResult > 0) { // item > max max = item; } } else { min = oddItem; max = item; hasMinMax = true; } } else { hasOddItem = true; oddItem = item; } return(null); BlockJumped: blockJumped = true; return(selfBlock.PropagateFlow(comparer, blockResult)); }; Each(each, self, Proc.Create(each.Context, blockProc)); if (blockJumped) { return(blockResult); } if (!hasMinMax) { return(hasOddItem ? new RubyArray(2) { oddItem, oddItem } : new RubyArray(2) { null, null }); } if (hasOddItem) { int?compareResult = CompareItems(comparisonStorage, oddItem, min, comparer, out blockResult); if (compareResult == null) { return(blockResult); } if (compareResult < 0) { min = oddItem; } compareResult = CompareItems(comparisonStorage, oddItem, max, comparer, out blockResult); if (compareResult == null) { return(blockResult); } if (compareResult > 0) { max = oddItem; } } return(new RubyArray(2) { min, max }); }
internal static object Each(CallSiteStorage <EachSiteN> /*!*/ each, object self, IList /*!*/ args, Proc /*!*/ block) { var site = each.GetCallSite("each", new RubyCallSignature(0, RubyCallFlags.HasImplicitSelf | RubyCallFlags.HasBlock | RubyCallFlags.HasSplattedArgument)); return(site.Target(site, self, block, args)); }
public static bool Equal(Proc /*!*/ self, object other) { return(false); }
public static object Call(Proc/*!*/ self, object arg1) { RequireParameterCount(self, 1); return self.Call(arg1); }
public static bool Equal(Proc /*!*/ self, [NotNull] Proc /*!*/ other) { return(self.Dispatcher == other.Dispatcher && self.LocalScope == other.LocalScope && self.Kind == other.Kind); }
public static object Trap(RubyContext/*!*/ context, object self, object signalId, Proc proc) { return Signal.Trap(context, self, signalId, proc); }
internal MarshalReader(BinaryReader /*!*/ reader, RubyContext /*!*/ context, Scope /*!*/ globalScope, Proc proc) { _reader = reader; _context = context; _globalScope = globalScope; _proc = proc; _symbols = new Dictionary <int, string>(); _objects = new Dictionary <int, object>(); }
internal void InitializeRfc(Proc proc) { if (proc != null && proc.Kind == ProcKind.Block) { proc.Kind = ProcKind.Proc; proc.Converter = this; } }
public static Proc/*!*/ ToProc(Proc/*!*/ self) { return self; }
internal static object Each(CallSiteStorage<EachSiteN>/*!*/ each, object self, IList/*!*/ args, Proc/*!*/ block) { var site = each.GetCallSite("each", new RubyCallSignature(0, RubyCallFlags.HasImplicitSelf | RubyCallFlags.HasBlock | RubyCallFlags.HasSplattedArgument)); return site.Target(site, self, block, args); }
public static object Call(Proc/*!*/ self) { RequireParameterCount(self, 0); return self.Call(); }
public static Binding /*!*/ GetLocalScope(Proc /*!*/ self) { return(new Binding(self.LocalScope)); }
public static object DefineFinalizer(RubyModule/*!*/ self, object obj, Proc proc) { RubyArray result = new RubyArray(2); result.Add(0); result.Add(proc); return result; }
public static object Call(Proc /*!*/ self, object arg1) { RequireParameterCount(self, 1); return(self.Call(arg1)); }
public static Proc SetTraceListener(RubyContext/*!*/ context, object self, Proc listener) { if (listener != null && !context.RubyOptions.EnableTracing) { throw new NotSupportedException("Tracing is not supported unless -trace option is specified."); } return context.TraceListener = listener; }
protected Proc(Proc/*!*/ proc) : this(proc.Kind, proc.Self, proc.LocalScope, proc.Dispatcher) { Converter = proc.Converter; }
internal RubyMethodScope(MutableTuple locals, SymbolId[]/*!*/ variableNames, RubyScope/*!*/ parent, RubyModule/*!*/ declaringModule, string/*!*/ definitionName, object selfObject, Proc blockParameter, InterpretedFrame interpretedFrame) { Assert.NotNull(parent, declaringModule, definitionName); // RuntimeFlowControl: _activeFlowControlScope = this; // RubyScope: _parent = parent; _top = parent.Top; _selfObject = selfObject; _methodAttributes = RubyMethodAttributes.PublicInstance; _locals = locals; _variableNames = variableNames; InterpretedFrame = interpretedFrame; // RubyMethodScope: _declaringModule = declaringModule; _definitionName = definitionName; _blockParameter = blockParameter; InitializeRfc(blockParameter); SetDebugName("method " + definitionName + ((blockParameter != null) ? "&" : null)); }
/// <summary> /// Creates a lambda Proc that has the same target, context and self object as this block. /// Doesn't preserve the class of the Proc. /// </summary> public Proc/*!*/ ToLambda(RubyLambdaMethodInfo method) { Proc result = new Proc(this); result.Kind = ProcKind.Lambda; result._method = method; return result; }
public static object Call(Proc /*!*/ self, object arg1, object arg2, object arg3, object arg4) { RequireParameterCount(self, 4); return(self.Call(arg1, arg2, arg3, arg4)); }
public static object Load(RubyScope /*!*/ scope, RubyModule /*!*/ self, [NotNull] MutableString /*!*/ source, [Optional] Proc proc) { BinaryReader reader = new BinaryReader(new MemoryStream(source.ConvertToBytes())); MarshalReader loader = new MarshalReader(reader, scope.RubyContext, scope.GlobalScope, proc); return(loader.Load()); }
/// <summary> /// Creates a copy of the proc that has the same target, context and self object as this block. /// </summary> public Proc/*!*/ Create(Proc/*!*/ proc) { return new Proc(proc); }
public static int GetArity(Proc /*!*/ self) { return(self.Dispatcher.Arity); }
public static RubyLambdaMethodInfo/*!*/ ToLambdaMethodInfo(Proc/*!*/ block, string/*!*/ definitionName, RubyMethodVisibility visibility, RubyModule/*!*/ owner) { return new RubyLambdaMethodInfo(block, definitionName, (RubyMemberFlags)visibility, owner); }
public static Proc /*!*/ Clone(Proc /*!*/ self) { return(self.Copy()); }
internal static object Each(CallSiteStorage<EachSite>/*!*/ each, object self, Proc/*!*/ block) { var site = each.GetCallSite("each", new RubyCallSignature(0, RubyCallFlags.HasImplicitSelf | RubyCallFlags.HasBlock)); return site.Target(site, self, block); }
public static object Call(Proc /*!*/ self) { RequireParameterCount(self, 0); return(self.Call()); }