/* **************************************************************************** * * Copyright (c) Microsoft Corporation. * * This source code is subject to terms and conditions of the Apache License, Version 2.0. A * copy of the license can be found in the License.html file at the root of this distribution. If * you cannot locate the Apache License, Version 2.0, please send an email to * ironruby@microsoft.com. By using this source code in any fashion, you are agreeing to be bound * by the terms of the Apache License, Version 2.0. * * You must not remove this notice, or any other, from this software. * * * ***************************************************************************/ using MSA = System.Linq.Expressions; using System; using System.Collections; using System.Collections.Concurrent; using System.Collections.Generic; using System.Diagnostics; using System.Dynamic; using System.Globalization; using System.IO; using System.Reflection; using System.Runtime.CompilerServices; using System.Runtime.Serialization; using System.Text; using System.Threading; using IronRuby.Builtins; using Range = IronRuby.Builtins.Range; using IronRuby.Compiler; using IronRuby.Compiler.Generation; using IronRuby.Runtime.Calls; using Microsoft.Scripting; using Microsoft.Scripting.Interpreter; using System.Numerics; using Microsoft.Scripting.Runtime; using Microsoft.Scripting.Utils; using IronRuby.Runtime.Conversions; namespace IronRuby.Runtime { [ReflectionCached, CLSCompliant(false)] public static partial class RubyOps { [Emitted] public static readonly object DefaultArgument = new object(); // Returned by a virtual site if a base call should be performed. [Emitted] public static readonly object ForwardToBase = new object(); // Returned by the rule of a call site in a light-frame method that would have read the method's scope // (RubyCallFlags.HasLightScope): the method makes the scope and calls through an ordinary site. public static readonly object LightScopeRequired = new object(); // Returned by the rule of a `block_given?' site in a light frame when the call is Kernel#block_given? itself // (DispatchIntrinsics.TryBuildBlockGiven): the answer is whether the frame has a block, which a light-frame method // reads from its block parameter without making its scope to be asked through; a block's frame has no block // parameter to read, and makes the call with its scope as for LightScopeRequired. public static readonly object LightBlockGiven = new object(); // How many refine blocks are running in the process (RubyUtils.EvaluateRefinementBlock). While none is, no Proc // has a refine block's override, so the lexical walk steps over block scopes (RubyScope.GetLexicalRefinements), // and the rule of a refined call made in a block written in a method can test the method's record instead // (RubyCallAction.Resolve) - with this test. [Emitted] public static int RefineBlocksRunning; // an instance of a dummy type that causes any rule based on instance type check to fail private sealed class _NeedsUpdate { } [Emitted] public static readonly object NeedsUpdate = new _NeedsUpdate(); #region Scopes [Emitted] public static MutableTuple GetLocals(RubyScope/*!*/ scope) { return scope.Locals; } [Emitted] public static MutableTuple GetParentLocals(RubyScope/*!*/ scope) { return scope.Parent.Locals; } [Emitted] public static RubyScope/*!*/ GetParentScope(RubyScope/*!*/ scope) { return scope.Parent; } [Emitted] public static Proc GetMethodBlockParameter(RubyScope/*!*/ scope) { var methodScope = scope.GetInnerMostMethodScope(); return methodScope != null ? methodScope.BlockParameter : null; } [Emitted] public static object GetMethodBlockParameterSelf(RubyScope/*!*/ scope) { Proc proc = scope.GetInnerMostMethodScope().BlockParameter; Debug.Assert(proc != null, "CreateBfcForYield is called before this method and it checks non-nullity"); return proc.Self; } [Emitted] public static object GetProcSelf(Proc/*!*/ proc) { return proc.Self; } [Emitted] public static int GetProcArity(Proc/*!*/ proc) { return proc.Dispatcher.Arity; } [Emitted] public static void InitializeScope(RubyScope/*!*/ scope, MutableTuple locals, string[] variableNames, InterpretedFrame interpretedFrame) { if (!scope.LocalsInitialized) { scope.SetLocals(locals, variableNames ?? ArrayUtils.EmptyStrings); } scope.InterpretedFrame = interpretedFrame; // While a file's top level runs, a proc created in it can return from it, as MRI's // `proc { return }.call` at the top of a file does (see IsTopLevelReturn). var topLevel = scope as RubyTopLevelScope; if (topLevel != null) { topLevel._activeFlowControlScope = topLevel; topLevel.ActiveThreadId = Environment.CurrentManagedThreadId; } } [Emitted] public static void InitializeScopeNoLocals(RubyScope/*!*/ scope, InterpretedFrame interpretedFrame) { scope.InterpretedFrame = interpretedFrame; } /// /// An eval's own locals exist from the start, as in any other scope - `if false; a = 1; end' /// leaves `a' defined as nil for the next eval through the same binding - so the ones the /// string declares are defined up front rather than on their first assignment. /// [Emitted] public static void InitializeEvalScope(RubyScope/*!*/ scope, string/*!*/[]/*!*/ declaredNames, InterpretedFrame interpretedFrame) { scope.InterpretedFrame = interpretedFrame; // Declared last to first: a scope lists the variables it was given later first // (RubyScope.GetDeclaredLocalSymbols), and MRI lists the ones of a single eval in // the order the string has them. for (int i = declaredNames.Length - 1; i >= 0; i--) { scope.DeclareLocalVariable(declaredNames[i]); } } [Emitted] public static void SetDataConstant(RubyScope/*!*/ scope, string/*!*/ dataPath, int dataOffset) { Debug.Assert(dataOffset >= 0); RubyContext context = scope.RubyContext; if (!context.DomainManager.Platform.FileExists(dataPath)) { // -e and stdin scripts also compile as TopScopeFactoryKind.Main and can contain // __END__. MRI does not define DATA for those; defining it as nil would make // defined?(DATA) truthy, so leave the constant undefined instead. return; } RubyFile dataFile = new RubyFile(context, dataPath, IOMode.ReadOnly); dataFile.Seek(dataOffset, SeekOrigin.Begin); context.ObjectClass.SetConstant("DATA", dataFile); } [Emitted] public static RubyModuleScope/*!*/ CreateModuleScope(MutableTuple locals, string[] variableNames, RubyScope/*!*/ parent, RubyModule/*!*/ module) { if (parent.RubyContext != module.Context) { throw RubyExceptions.CreateTypeError("Cannot open a module '{0}' defined in a foreign runtime #{1}", module.Name, module.Context.RuntimeId); } RubyModuleScope scope = new RubyModuleScope(parent, module); scope.SetDebugName((module.IsClass ? "class" : "module") + " " + module.Name); scope.SetLocals(locals, variableNames ?? ArrayUtils.EmptyStrings); return scope; } /// /// MRI's stack check at a method or block entry: a recursion too deep is a SystemStackError, which a program can /// rescue, not a CLR stack overflow, which ends the process (#128). Asked where a Ruby frame starts - method /// and block scopes here, JIT-compiled methods in JitRuntime.CheckStack - and cheap: it compares the stack /// pointer with a limit it keeps per thread (StackLimit, which says why the runtime's own check is not enough). /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void RequireSufficientStack() { if (!StackLimit.IsSufficient()) { ThrowStackLevelTooDeep(); } } [MethodImpl(MethodImplOptions.NoInlining)] internal static void ThrowStackLevelTooDeep() { throw new SystemStackError("stack level too deep"); } [Emitted] public static RubyMethodScope/*!*/ CreateMethodScope(MutableTuple locals, string[] variableNames, int visibleParameterCount, RubyScope/*!*/ parentScope, RubyModule/*!*/ declaringModule, string/*!*/ definitionName, object selfObject, Proc blockParameter, InterpretedFrame interpretedFrame) { // Method entry is a safe point, as in MRI: a thread that recurses or calls methods in a // loop is reachable by Thread#raise / Thread#kill even where no back edge is. RubyUtils.SafePoint(); RequireSufficientStack(); var scope = new RubyMethodScope( locals, variableNames ?? ArrayUtils.EmptyStrings, visibleParameterCount, parentScope, declaringModule, definitionName, selfObject, blockParameter, interpretedFrame ); if (_aliasCallSeen) { string callee = _pendingCallee; if (callee != null) { _pendingCallee = null; if (_pendingCalleeDefinition == definitionName) { scope.CalleeName = callee; } } } if ((TracePoint.ActiveEvents & (int)(TraceEvents.Call | TraceEvents.CCall)) != 0) { TracePoint.OnMethodCall(scope); } return scope; } #region Parked method scopes // A method whose block literals all reuse their Procs (BlockDispatcher.ReusesProc: no lambda or nested block in them) // and which calls no super makes a scope and a Proc per literal on every call, the Proc's parent (`def each; (a..b).each // { |i| yield @s[i] }; end'). Once such a call has returned normally, if nothing captured the scope (RubyScope.IsCaptured: // binding, eval, a method or Symbol proc made in it, a TracePoint, a Proc of a literal replaced) and the last Proc of // each literal is one only the frame holds (Proc.IsReusableAndInactive: given to light frames and [BlockNotKept] // iterators only, and no scope it ran in captured), nothing can reach the scope or its Procs: they are parked in the // body's ParkedScopeSlot, cleared of everything of the call that ended, and the next call takes them with an atomic // exchange into a variable of its frame. A call that does not get them - a recursive call, one on another thread, or one // after a call that could not park - makes a plain scope and Procs exactly as before. IR_PARKED_SCOPES=0 turns it off. /// /// The parked scope and Procs of the slot, now this call's, if they were parked by a call of the same definition. /// [Emitted] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ParkedProcs TakeParkedScope(ParkedScopeSlot/*!*/ slot, RubyScope/*!*/ parentScope, RubyModule/*!*/ declaringModule) { var parked = slot.Parked; if (parked == null) { return null; } return TakeParkedScopeSlow(slot, parked, parentScope, declaringModule); } private static ParkedProcs TakeParkedScopeSlow(ParkedScopeSlot/*!*/ slot, ParkedProcs/*!*/ parked, RubyScope/*!*/ parentScope, RubyModule/*!*/ declaringModule) { // Taken first, looked at after: until the exchange succeeds another call may take it, fail to park it and empty it. if (Interlocked.CompareExchange(ref slot.Parked, null, parked) != parked) { return null; } // the same body may be defined in more than one module or runtime: a scope is only reused for the definition it was // made for (one parked for another goes back, or is dropped if a call parked meanwhile) var scope = parked.Scope; if (scope._parent != parentScope || scope.DeclaringModule != declaringModule) { Interlocked.CompareExchange(ref slot.Parked, parked, null); return null; } return parked; } /// /// CreateMethodScope with the parked scope TakeParkedScope took: that scope, made the scope of this call. /// [Emitted] public static RubyMethodScope/*!*/ ReuseParkedScope(ParkedProcs/*!*/ parked, MutableTuple locals, object selfObject, Proc blockParameter, InterpretedFrame interpretedFrame) { RubyUtils.SafePoint(); RequireSufficientStack(); var scope = parked.Scope; scope.Reinitialize(locals, selfObject, blockParameter, interpretedFrame, null, null); if (_aliasCallSeen) { string callee = _pendingCallee; if (callee != null) { _pendingCallee = null; if (_pendingCalleeDefinition == scope.DefinitionName) { scope.CalleeName = callee; } } } if ((TracePoint.ActiveEvents & (int)(TraceEvents.Call | TraceEvents.CCall)) != 0) { TracePoint.OnMethodCall(scope); } return scope; } /// /// CreateMethodScopeLazy of a light-frame body with the parked scope TakeParkedScope took. /// [Emitted] public static RubyMethodScope/*!*/ ReuseParkedScopeLazy(ParkedProcs/*!*/ parked, MutableTuple locals, object selfObject, Proc blockParameter, InterpretedFrame interpretedFrame, RuntimeFlowControl/*!*/ rfc, string calleeName) { // (as CreateMethodScopeLazy: the scope holds the block, and whatever asked for the scope may keep it) if (blockParameter != null) { blockParameter.ClearReusable(); } var scope = parked.Scope; scope.Reinitialize(locals, selfObject, blockParameter, interpretedFrame, rfc, calleeName); return scope; } /// /// InstantiateReusableBlock of a literal (the index-th) of a body with a parking slot: the Proc parked with the scope, if /// this call took that scope, and a previous Proc that cannot be made again marks the scope captured - it may be held /// somewhere, and it reaches the scope. /// [Emitted] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static Proc InstantiateParkedBlock(Proc previous, RubyScope/*!*/ scope, object self, BlockDispatcher/*!*/ dispatcher, ParkedProcs parked, int index) { // the first evaluation of the literal in a call that took nothing: InstantiateReusableBlock's own if (previous == null && parked == null) { return dispatcher.HasMethod ? new Proc(ProcKind.Block, self, scope, dispatcher) : null; } return InstantiateParkedBlockSlow(previous, scope, self, dispatcher, parked, index); } private static Proc InstantiateParkedBlockSlow(Proc previous, RubyScope/*!*/ scope, object self, BlockDispatcher/*!*/ dispatcher, ParkedProcs parked, int index) { if (previous != null) { if (previous.TryReuse()) { return previous; } scope.IsCaptured = true; } else if (parked != null && parked.Scope == scope) { var procs = parked.Procs; var proc = procs[index]; if (proc != null) { procs[index] = null; proc.ReuseParked(self, parked.ThreadId != Environment.CurrentManagedThreadId); if (proc.TryReuse()) { return proc; } } } return dispatcher.HasMethod ? new Proc(ProcKind.Block, self, scope, dispatcher) : null; } /// /// The normal end of a call of a body with a parking slot (an exception, a throw, Thread#kill skip it): if its scope may /// be parked, where to stage its Procs (StageParkedProc, then EndPark) - what the call took, or an emptied one. /// [Emitted] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static ParkedProcs BeginPark(ParkedScopeSlot/*!*/ slot, RubyScope scope, ParkedProcs taken) { // a call that made no scope, or a slot that gave up: nothing to do, inlined into the body if (scope == null || slot.Failures > ParkedScopeSlot.MaxFailures) { return null; } return BeginParkSlow(slot, scope, taken); } private static ParkedProcs BeginParkSlow(ParkedScopeSlot/*!*/ slot, RubyScope/*!*/ scope, ParkedProcs taken) { var methodScope = scope as RubyMethodScope; if (methodScope == null || slot.IsOff || TracePoint.ActiveEvents != 0) { return null; } if (methodScope.IsCaptured || methodScope.HasRareState || methodScope._activeFlowControlScope == methodScope) { slot.Failures++; return null; } if (taken != null) { return taken; } var spare = slot.Spare; if (spare != null && Interlocked.CompareExchange(ref slot.Spare, null, spare) == spare) { return spare; } return new ParkedProcs(slot.ProcCount); } /// /// The last Proc of the index-th literal, staged for parking; false if something else may hold it. /// [Emitted] public static bool StageParkedProc(ParkedProcs/*!*/ parked, int index, Proc proc) { if (proc != null && !proc.IsReusableAndInactive) { return false; } parked.Procs[index] = proc; return true; } /// /// Parks the scope with the staged Procs, cleared of the call that ended; or, if a Proc may be held or another call /// parked first, keeps the emptied ParkedProcs for the next. /// [Emitted] public static void EndPark(ParkedScopeSlot/*!*/ slot, ParkedProcs/*!*/ parked, RubyScope/*!*/ scope, bool procsParkable) { if (procsParkable) { if (slot.Failures != 0) { slot.Failures = 0; } foreach (var proc in parked.Procs) { proc?.ClearForPark(); } var methodScope = (RubyMethodScope)scope; methodScope.ClearCallState(); parked.Scope = methodScope; parked.ThreadId = Environment.CurrentManagedThreadId; if (Interlocked.CompareExchange(ref slot.Parked, parked, null) == null) { return; } } else { slot.Failures++; } parked.Clear(); Volatile.Write(ref slot.Spare, parked); } #endregion #region Light method frames // A method whose body never needs its scope - yield and return over self, its parameters, its // locals and literals (MethodDefinition.TransformLightBody) - makes none on a call. Its flow-control // frame is the literal block it was called with, converted as a library method's is // (CreateRfcForMethod), or the shared inactive frame; the scope is made only when something asks // for one: a TracePoint hook, or the exception filter. That is the 160-byte RubyMethodScope every // other call of a Ruby method allocates. /// /// The entry of a light-frame method: the safe point CreateMethodScope makes, then the call's /// flow-control frame. /// [Emitted] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static RuntimeFlowControl/*!*/ EnterLightMethod(Proc blockParameter) { RubyUtils.SafePoint(); // a call without a block, the common case, is inlined into the method body return (blockParameter == null) ? InactiveRfc : EnterLightMethodWithBlock(blockParameter); } /// /// EnterLightMethod of a method with a block parameter (`def m(&b)'): the body can hand the block on or keep /// it without making a scope - `def capture(&b); b; end' - so the block is converted as any call's is, and is /// never made again for the next evaluation of its literal. /// public static RuntimeFlowControl/*!*/ EnterLightMethodNamingBlock(Proc blockParameter) { RubyUtils.SafePoint(); if (blockParameter != null && blockParameter.Kind == ProcKind.Block) { blockParameter.ConvertAsMethodFrame(); return blockParameter; } return InactiveRfc; } private static RuntimeFlowControl/*!*/ EnterLightMethodWithBlock(Proc/*!*/ blockParameter) { // A light frame can only yield to its block: a literal one may be made again once the call returns (Proc.TryReuse). if (blockParameter.Kind == ProcKind.Block) { blockParameter.ConvertAsLightFrame(); return blockParameter; } return InactiveRfc; } /// /// The name the method was called by when that was an alias (CreateMethodScope keeps it in the /// scope; a light frame keeps it for the scope it may make). Taken either way, as CreateMethodScope does. /// [Emitted] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static string TakePendingCallee(string/*!*/ definitionName) { return _aliasCallSeen ? TakePendingCalleeSlow(definitionName) : null; } private static string TakePendingCalleeSlow(string/*!*/ definitionName) { string callee = _pendingCallee; if (callee != null) { _pendingCallee = null; if (_pendingCalleeDefinition == definitionName) { return callee; } } return null; } /// /// The scope of a light-frame method made at its entry because a TracePoint listens for calls, which /// it is then reported to; null otherwise. /// [Emitted] public static RubyMethodScope CreateMethodScopeIfTraced(MethodFrameShape/*!*/ shape, RubyScope/*!*/ parentScope, RubyModule/*!*/ declaringModule, MutableTuple locals, object selfObject, Proc blockParameter, InterpretedFrame interpretedFrame, RuntimeFlowControl/*!*/ rfc, string calleeName) { if ((TracePoint.ActiveEvents & (int)(TraceEvents.Call | TraceEvents.CCall)) != 0) { var scope = CreateMethodScopeLazy(shape, parentScope, declaringModule, locals, selfObject, blockParameter, interpretedFrame, rfc, calleeName); TracePoint.OnMethodCall(scope); return scope; } return null; } /// /// The scope of a light-frame method, made when something asks for it (see the RubyMethodScope constructor). /// [Emitted] public static RubyMethodScope/*!*/ CreateMethodScopeLazy(MethodFrameShape/*!*/ shape, RubyScope/*!*/ parentScope, RubyModule/*!*/ declaringModule, MutableTuple locals, object selfObject, Proc blockParameter, InterpretedFrame interpretedFrame, RuntimeFlowControl/*!*/ rfc, string calleeName) { // the scope holds the block, and whatever asked for the scope may keep it if (blockParameter != null) { blockParameter.ClearReusable(); } return new RubyMethodScope(shape, parentScope, declaringModule, locals, selfObject, blockParameter, interpretedFrame, rfc, calleeName); } /// /// MethodYield of a light-frame method: the frame is the scope's if one was made, the call's otherwise. /// [Emitted] [MethodImpl(MethodImplOptions.AggressiveOptimization)] public static bool LightMethodYield(RuntimeFlowControl/*!*/ rfc, RubyScope scope, BlockParam/*!*/ yieldedBlockFlowControl, object returnValue) { if (yieldedBlockFlowControl.ReturnReason == BlockReturnReason.Undefined) { return false; } return MethodYieldRfc((scope != null) ? scope.FlowControlScope : rfc, yieldedBlockFlowControl, returnValue); } /// /// LeaveMethodFrame of a light-frame method: the call's frame, and the scope if one was made. /// [Emitted] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void LeaveLightMethodFrame(RuntimeFlowControl/*!*/ rfc, RubyScope scope) { // nothing to do for a call without a block that made no scope, the common case, inlined into the method body if (rfc._activeFlowControlScope != null || scope != null) { LeaveLightMethodFrameSlow(rfc, scope); } } private static void LeaveLightMethodFrameSlow(RuntimeFlowControl/*!*/ rfc, RubyScope scope) { LeaveMethodFrame(rfc); if (scope != null) { if (scope.IsActiveMethod) { scope.LeaveMethod(); } ReleaseScopeFrame(scope); } } #endregion // The name an aliased method was called by, handed from the call site to the scope the // method body creates first thing (MRI keeps it in the frame for __callee__). [ThreadStatic] private static string _pendingCallee; [ThreadStatic] private static string _pendingCalleeDefinition; private static bool _aliasCallSeen; /// /// Wraps the last argument of a call to a method through an alias, so that the name is /// recorded after every argument has been evaluated, just before the method body runs. /// [Emitted] public static object MarkAliasCall(object lastArgument, string/*!*/ calleeName, string/*!*/ definitionName) { _aliasCallSeen = true; _pendingCallee = calleeName; _pendingCalleeDefinition = definitionName; return lastArgument; } [Emitted] public static RubyScope/*!*/ CreateFileInitializerScope(MutableTuple locals, string[] variableNames, RubyScope/*!*/ parent) { return new RubyFileInitializerScope(locals, variableNames ?? ArrayUtils.EmptyStrings, parent); } [Emitted] public static RubyBlockScope/*!*/ CreateBlockScope(MutableTuple locals, string[] variableNames, BlockParam/*!*/ blockParam, object selfObject, InterpretedFrame interpretedFrame) { // Block entry too: `n.times { }` and `each { }` loop in C#, where no back edge is emitted. RubyUtils.SafePoint(); return CreateBlockScopeSafePointInBody(locals, variableNames, blockParam, selfObject, interpretedFrame); } /// /// for a block whose whole body is `begin ... rescue ... end` /// (`n.times do ... rescue ... end`): its entry safe point is the first thing inside the /// rescued statements (Body.SafePointFirst). At the entry, a Thread#raise that came in while /// the previous call was returning would be raised outside the rescue written for it - MRI /// has no check there, only at the block's `leave` and the loop's back edge, which leaves it /// a window of a few instructions where IronRuby's was a block return and a C# loop step. /// [Emitted] public static RubyBlockScope/*!*/ CreateBlockScopeSafePointInBody(MutableTuple locals, string[] variableNames, BlockParam/*!*/ blockParam, object selfObject, InterpretedFrame interpretedFrame) { RequireSufficientStack(); var scope = new RubyBlockScope(locals, variableNames ?? ArrayUtils.EmptyStrings, blockParam, selfObject, interpretedFrame); if ((TracePoint.ActiveEvents & (int)(TraceEvents.BCall | TraceEvents.Call)) != 0) { TracePoint.OnBlockCall(scope); } return scope; } /// /// The entry of a block whose body never uses its scope (see BlockDefinition.IsScopeFreeBody): /// the safe point, and the scope only if a TracePoint wants the call event. Null otherwise; a /// TracePoint hook enabled later makes it then (). /// [Emitted] [MethodImpl(MethodImplOptions.AggressiveOptimization)] public static RubyBlockScope CreateBlockScopeIfTraced(MutableTuple locals, string[] variableNames, BlockParam/*!*/ blockParam, object selfObject, InterpretedFrame interpretedFrame) { RubyUtils.SafePoint(); RequireSufficientStack(); if ((TracePoint.ActiveEvents & (int)(TraceEvents.BCall | TraceEvents.Call)) != 0) { var scope = new RubyBlockScope(locals, variableNames ?? ArrayUtils.EmptyStrings, blockParam, selfObject, interpretedFrame); TracePoint.OnBlockCall(scope); return scope; } return null; } /// /// of a block that keeps its locals in CLR variables (ScopeBuilder.TryUseRegisterLocals), /// whose tuple is made only with the scope: the safe point, and whether a TracePoint wants the call event, in which case the /// block makes its scope and reports it (). /// [Emitted] [MethodImpl(MethodImplOptions.AggressiveOptimization)] public static bool EnterScopeFreeBlock() { RubyUtils.SafePoint(); RequireSufficientStack(); return (TracePoint.ActiveEvents & (int)(TraceEvents.BCall | TraceEvents.Call)) != 0; } [Emitted] public static RubyBlockScope/*!*/ TraceBlockCall(RubyBlockScope/*!*/ scope) { TracePoint.OnBlockCall(scope); return scope; } /// /// Whether a TracePoint wants the call event of a light-frame method, which then makes its scope (CreateMethodScopeIfTraced) /// and, if it keeps its locals in CLR variables, their tuple first. /// [Emitted, MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool IsMethodCallTraced() { return (TracePoint.ActiveEvents & (int)(TraceEvents.Call | TraceEvents.CCall)) != 0; } /// /// The first step of the entry of a block whose body needs its scope but does not define a closure /// over it: the safe point, then the scope the last finished call through this BlockParam left there /// (each, map, times, a yield in a loop; see BlockParam.ParkScope) unless something captured it, or null. /// The scope comes off the BlockParam here, after the safe point (which can run a trap handler or a /// finalizer) and together with its locals tuple: a call that starts in between finds nothing kept and /// makes a scope and tuple of its own, and a call that does not finish normally keeps nothing. /// The block reuses the scope's tuple (see ) and hands the scope to /// ; parks it again. /// [Emitted, MethodImpl(MethodImplOptions.AggressiveInlining)] public static RubyBlockScope TakeReusableBlockScope(BlockParam/*!*/ blockParam) { // Block entry too: `n.times { }` and `each { }` loop in C#, where no back edge is emitted. RubyUtils.SafePoint(); return TakeReusableBlockScopeSafePointInBody(blockParam); } /// /// for a block whose safe point is inside its body (see /// ). Whatever that safe point runs - a trap /// handler, a finalizer - runs after the scope is taken, so a call of the block it makes finds /// nothing kept and makes a scope of its own. /// [Emitted, MethodImpl(MethodImplOptions.AggressiveInlining)] public static RubyBlockScope TakeReusableBlockScopeSafePointInBody(BlockParam/*!*/ blockParam) { if (blockParam.MayHaveReusableScope) { var scope = blockParam.ReusableScope; if (scope != null && !scope.IsCaptured) { blockParam.TakeReusableScope(); return scope; } } return null; } /// /// The locals tuple of a scope took, for the block to reuse (the /// last call cleared it); null if it took none. /// [Emitted, MethodImpl(MethodImplOptions.AggressiveInlining)] public static MutableTuple GetReusableLocals(RubyBlockScope reusable) { return (reusable != null) ? reusable._locals : null; } /// /// The scope of a block call that began: the scope it took, made the /// scope of this call, or a new one. /// [Emitted, MethodImpl(MethodImplOptions.AggressiveInlining)] public static RubyBlockScope/*!*/ CreateReusableBlockScope(MutableTuple locals, string[] variableNames, BlockParam/*!*/ blockParam, object selfObject, InterpretedFrame interpretedFrame, RubyBlockScope reusable) { RequireSufficientStack(); RubyBlockScope scope; if (reusable != null) { scope = reusable; scope.Reinitialize(locals, blockParam, selfObject, interpretedFrame); } else { scope = new RubyBlockScope(locals, variableNames ?? ArrayUtils.EmptyStrings, blockParam, selfObject, interpretedFrame); } if ((TracePoint.ActiveEvents & (int)(TraceEvents.BCall | TraceEvents.Call)) != 0) { scope.MarkCaptured(); TracePoint.OnBlockCall(scope); } return scope; } /// /// The normal exit of a block that made its scope by . /// An exception leaving the body skips this, and the scope is simply not reused. True if the /// scope was kept on the BlockParam, with its locals tuple when : the /// block then clears the tuple, so that the next call finds its locals nil. The first call through /// a BlockParam keeps nothing (see BlockParam.ParkScope): a block called once pays no more than a flag. /// [Emitted, MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool LeaveReusableBlockScope(RubyBlockScope/*!*/ scope, bool keepLocals) { return scope.BlockFlowControl.ParkScope(scope); } [Emitted] public static RubyBlockScope/*!*/ CreateBlockScopeLazy(MutableTuple locals, string[] variableNames, BlockParam/*!*/ blockParam, object selfObject, InterpretedFrame interpretedFrame) { return new RubyBlockScope(locals, variableNames ?? ArrayUtils.EmptyStrings, blockParam, selfObject, interpretedFrame); } // TracePoint hooks; emitted code only calls them when TracePoint.ActiveEvents has the event. // Emitted behind a test of TracePoint.ActiveEvents in front of every statement: kept out of // line, the never-taken branch is one call; inlined, it was a few hundred bytes of machine // code per statement in every compiled method body. [Emitted, MethodImpl(MethodImplOptions.NoInlining)] public static void TraceLineEvent(RubyScope scope, string path, int line) { // objspace's allocation tracing forces this event on and takes the allocation site of // every object created by the statement from here. if (ObjectTracking.IsTracingAllocations) { ObjectTracking.SetCurrentLine(scope, path, line); } TracePoint.OnLine(scope, path, line); } [Emitted] public static void TraceReturnEvent(RubyScope scope, object value, string path, int line) { TracePoint.OnReturn(scope, value, path, line); } [Emitted] public static void TraceClassEvent(RubyScope scope, string path, int line) { if ((TracePoint.ActiveEvents & (int)TraceEvents.Class) != 0) { TracePoint.OnClass(scope, path, line); } } [Emitted] public static void PrintInteractiveResult(RubyScope/*!*/ scope, MutableString/*!*/ value) { var writer = scope.RubyContext.DomainManager.SharedIO.OutputStream; writer.WriteByte((byte)'='); writer.WriteByte((byte)'>'); writer.WriteByte((byte)' '); var bytes = value.ToByteArray(); writer.Write(bytes, 0, bytes.Length); writer.WriteByte((byte)'\r'); writer.WriteByte((byte)'\n'); } [Emitted] public static object GetLocalVariable(RubyScope/*!*/ scope, string/*!*/ name) { return scope.ResolveLocalVariable(name); } [Emitted] public static object SetLocalVariable(object value, RubyScope/*!*/ scope, string/*!*/ name) { return scope.ResolveAndSetLocalVariable(name, value); } [Emitted] public static VersionHandle/*!*/ GetSelfClassVersionHandle(RubyScope/*!*/ scope) { return scope.SelfImmediateClass.Version; } /// /// Emitted into the guard of any call-site rule whose target class has been refined. The rule is /// valid exactly while the calling scope's lexical refinement activation is the same instance it /// was bound against. /// [Emitted] public static RefinementActivation/*!*/ GetActiveRefinements(RubyScope/*!*/ scope) { return scope.GetActiveRefinements(); } /// /// The same for the guard of a call-site rule, which is only built once a refinement exists: in a /// runtime where one ever has, GetActiveRefinements' first test is always false. /// [Emitted] public static RefinementActivation/*!*/ GetLexicalRefinements(RubyScope/*!*/ scope) { return scope.GetLexicalRefinements(); } // The implicit calls a conversion site makes carry no scope, so they cannot see refinements. // The two below run ahead of such a site where MRI honours the refinements of the lexical // position - "#{x}" and &x - and answer null (use the site) unless one is actually in play. // Unless some refinement has the method in question they cost a field read and a hash lookup. /// /// "#{x}" where a refinement of x's class defines #to_s. /// [Emitted] public static MutableString TryConvertToSWithRefinements(RubyScope/*!*/ scope, object value) { var context = scope.RubyContext; if (!context.HasRefinements || value is MutableString || !context.IsRefinedMethodName("to_s")) { return null; } if (!IsRefinedHere(scope, value, "to_s")) { return null; } var site = context.GetOrCreateSendSite>( "to_s", new RubyCallSignature(0, RubyCallFlags.HasScope | RubyCallFlags.HasImplicitSelf) ); return ToSDefaultConversion(context, value, site.Target(site, scope, value)); } /// /// A block argument (&x) in a scope where refinements are active: a Symbol's proc calls the method /// with the refinements of this place (MRI's refinement-aware symbol proc), and a refined #to_proc /// is the one that converts. /// [Emitted] public static Proc TryConvertBlockWithRefinements(RubyScope/*!*/ scope, object value) { var context = scope.RubyContext; if (!context.HasRefinements || value == null || value is Proc) { return null; } // Only a refinement with #to_proc, or - for a Symbol - with the method the proc calls, makes a // difference to what &x passes. var symbol = value as RubySymbol; bool toProcRefined = context.IsRefinedMethodName(Symbols.ToProc); if (!toProcRefined && (symbol == null || !context.IsRefinedMethodName(symbol.ToString()))) { return null; } if (scope.GetActiveRefinements().IsEmpty) { return null; } if (!toProcRefined || !IsRefinedHere(scope, value, Symbols.ToProc)) { // Symbol#to_proc, but with the calls it makes bound to this scope: return (symbol != null) ? Proc.CreateMethodInvoker(scope, symbol.ToString()) : null; } var site = context.GetOrCreateSendSite>( Symbols.ToProc, new RubyCallSignature(0, RubyCallFlags.HasScope | RubyCallFlags.HasImplicitSelf) ); return ToProcValidator(context.GetClassName(value), site.Target(site, scope, value)); } // True if a refinement active in scope supplies value's method of that name. private static bool IsRefinedHere(RubyScope/*!*/ scope, object value, string/*!*/ name) { var context = scope.RubyContext; if (scope.GetActiveRefinements().IsEmpty) { return false; } var method = context.ResolveMethodWithRefinements(value, name, VisibilityContext.AllVisible, scope); return method.Found && method.Info.DeclaringModule.IsRefinement; } #endregion #region Context [Emitted] public static RubyContext/*!*/ GetContextFromModule(RubyModule/*!*/ module) { return module.Context; } [Emitted] public static RubyContext/*!*/ GetContextFromIRubyObject(IRubyObject/*!*/ obj) { return obj.ImmediateClass.Context; } [Emitted] public static RubyContext/*!*/ GetContextFromScope(RubyScope/*!*/ scope) { return scope.RubyContext; } [Emitted] public static RubyContext/*!*/ GetContextFromMethod(RubyMethod/*!*/ method) { return method.Info.Context; } [Emitted] public static RubyContext/*!*/ GetContextFromBlockParam(BlockParam/*!*/ block) { return block.RubyContext; } [Emitted] public static RubyContext/*!*/ GetContextFromProc(Proc/*!*/ proc) { return proc.LocalScope.RubyContext; } [Emitted] public static Scope/*!*/ GetGlobalScopeFromScope(RubyScope/*!*/ scope) { return scope.GlobalScope.Scope; } #endregion #region Blocks [Emitted] [MethodImpl(MethodImplOptions.AggressiveOptimization)] public static Proc InstantiateBlock(RubyScope/*!*/ scope, object self, BlockDispatcher/*!*/ dispatcher) { return dispatcher.HasMethod ? new Proc(ProcKind.Block, self, scope, dispatcher) : null; } [Emitted] [MethodImpl(MethodImplOptions.AggressiveOptimization)] public static Proc InstantiateLambda(RubyScope/*!*/ scope, object self, BlockDispatcher/*!*/ dispatcher) { return dispatcher.HasMethod ? new Proc(ProcKind.Lambda, self, scope, dispatcher) : null; } /// /// InstantiateBlock of a literal that may reuse its Proc (BlockDispatcher.ReusesProc): is the /// Proc this frame made for the literal last time, kept in a variable of the frame, made again if nothing kept it. /// [Emitted] [MethodImpl(MethodImplOptions.AggressiveOptimization)] public static Proc InstantiateReusableBlock(Proc previous, RubyScope/*!*/ scope, object self, BlockDispatcher/*!*/ dispatcher) { if (previous != null && previous.TryReuse()) { return previous; } return dispatcher.HasMethod ? new Proc(ProcKind.Block, self, scope, dispatcher) : null; } [Emitted] public static Proc/*!*/ DefineBlock(RubyScope/*!*/ scope, object self, BlockDispatcher/*!*/ dispatcher, object/*!*/ clrMethod) { return new Proc(ProcKind.Block, self, scope, dispatcher.SetMethod(clrMethod)); } [Emitted] public static Proc/*!*/ DefineLambda(RubyScope/*!*/ scope, object self, BlockDispatcher/*!*/ dispatcher, object/*!*/ clrMethod) { return new Proc(ProcKind.Lambda, self, scope, dispatcher.SetMethod(clrMethod)); } /// /// Used in a method call with a block to reset proc-kind when the call is retried /// [Emitted] public static void InitializeBlock(Proc/*!*/ proc) { Assert.NotNull(proc); proc.Kind = ProcKind.Block; // a retried call may hand the block to a method that keeps it: only a light frame marks it reusable again proc.ClearReusable(); } /// /// Implements END block - like if it was a call to at_exit { ... } library method. /// [Emitted] public static void RegisterShutdownHandler(Proc/*!*/ proc) { proc.LocalScope.RubyContext.RegisterShutdownHandler(proc); } #endregion #region Yield: TODO: generate // AggressiveOptimization, here and on the other helpers a yield, a Proc#call or a light-frame method goes through // (block dispatchers, BlockParam and RuntimeFlowControl factories): they are called from compiled Ruby code from // the first iteration on, but tiered compilation leaves them Tier-0 for the first second or two of a process - // its call counting waits until no method has been jitted for 100 ms, which a program that keeps compiling Ruby // code does not give it - so a short script ran its yields through unoptimized getters and casts. Steady state // is the same either way (Util/bench, best of 6 after a warm-up); a short run is 5-15% faster. [Emitted] [MethodImpl(MethodImplOptions.AggressiveOptimization)] public static object Yield0(Proc procArg, object self, BlockParam/*!*/ blockParam) { object result; var proc = blockParam.Proc; RequireLambdaArity(proc, 0); try { result = proc.Dispatcher.Invoke(blockParam, self, procArg); } catch(EvalUnwinder evalUnwinder) { result = blockParam.GetUnwinderResult(evalUnwinder); } return result; } /// /// A lambda checks the number of arguments wherever it is called, including when it is /// the block a method yields to. A plain block does not. /// public static void RequireLambdaArity(Proc/*!*/ proc, int argCount) { RequireLambdaArity(proc, argCount, null); } /// /// is the last of the arguments. A /// block whose keywords were lowered onto positional parameters cannot tell a keyword hash /// from a positional one by the count alone, so its declared signature decides: MRI counts /// only the positional arguments against a lambda, and `|**nil|` refuses keywords in a /// proc as well as in a lambda. /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void RequireLambdaArity(Proc/*!*/ proc, int argCount, object lastArg) { // A plain block takes any arguments - the common case, so it does not make the call: // only a lambda, or a signature with keywords or **nil, has anything to check. if (proc.Kind == ProcKind.Lambda || proc.Dispatcher.ChecksKeywords) { CheckLambdaArity(proc, argCount, lastArg); } } private static void CheckLambdaArity(Proc/*!*/ proc, int argCount, object lastArg) { var signature = proc.Dispatcher.ParameterSignature; // A symbol proc reports [[:req], [:rest]] but checks for its receiver itself // ("no receiver given"). if (signature != null && proc.SymbolName == null) { Hash keywords = null; // = signature.TakesKeywords || signature.RefusesKeywords, cached if (proc.Dispatcher.ChecksKeywords) { keywords = argCount > 0 ? lastArg as Hash : null; if (keywords != null && !keywords.IsKeywordArguments) { keywords = null; } if (keywords != null && keywords.Count > 0 && signature.RefusesKeywords) { throw RubyExceptions.CreateArgumentError("no keywords accepted"); } } if (proc.Kind != ProcKind.Lambda) { return; } // The signature knows the positional bounds exactly ("1..2" for |a, b = 1|), where // the arity alone would only say "1+". int positional = argCount - (keywords != null ? 1 : 0); int min = signature.MinPositionalCount, max = signature.MaxPositionalCount; if (positional < min || (max >= 0 && positional > max)) { throw MakeWrongNumberOfArgumentsErrorN(positional, signature.DescribeExpectedArguments()); } return; } if (proc.Kind != ProcKind.Lambda) { return; } int arity = proc.Dispatcher.Arity; if (argCount == arity) { return; } if (arity >= 0) { throw MakeWrongNumberOfArgumentsError(argCount, arity); } if (argCount < -arity - 1) { // MRI spells an unbounded arity "expected 1+". throw MakeWrongNumberOfArgumentsErrorN(argCount, (-arity - 1).ToString(System.Globalization.CultureInfo.InvariantCulture) + "+"); } } [Emitted] [MethodImpl(MethodImplOptions.AggressiveOptimization)] public static object Yield1(object arg1, Proc procArg, object self, BlockParam/*!*/ blockParam) { object result; var proc = blockParam.Proc; RequireLambdaArity(proc, 1, arg1); try { // a lambda takes the single argument as it is: no auto-splatting result = proc.Kind == ProcKind.Lambda ? proc.Dispatcher.InvokeNoAutoSplat(blockParam, self, procArg, arg1) : proc.Dispatcher.Invoke(blockParam, self, procArg, arg1); } catch (EvalUnwinder evalUnwinder) { result = blockParam.GetUnwinderResult(evalUnwinder); } return result; } // YieldNoAutoSplat1 uses InvokeNoAutoSplat instead of Invoke (used by Call1) [MethodImpl(MethodImplOptions.AggressiveOptimization)] internal static object YieldNoAutoSplat1(object arg1, Proc procArg, object self, BlockParam/*!*/ blockParam) { object result; var proc = blockParam.Proc; try { result = proc.Dispatcher.InvokeNoAutoSplat(blockParam, self, procArg, arg1); } catch (EvalUnwinder evalUnwinder) { result = blockParam.GetUnwinderResult(evalUnwinder); } return result; } [Emitted] [MethodImpl(MethodImplOptions.AggressiveOptimization)] public static object Yield2(object arg1, object arg2, Proc procArg, object self, BlockParam/*!*/ blockParam) { object result; var proc = blockParam.Proc; RequireLambdaArity(proc, 2, arg2); try { result = proc.Dispatcher.Invoke(blockParam, self, procArg, arg1, arg2); } catch (EvalUnwinder evalUnwinder) { result = blockParam.GetUnwinderResult(evalUnwinder); } return result; } [Emitted] [MethodImpl(MethodImplOptions.AggressiveOptimization)] public static object Yield3(object arg1, object arg2, object arg3, Proc procArg, object self, BlockParam/*!*/ blockParam) { object result; var proc = blockParam.Proc; RequireLambdaArity(proc, 3, arg3); try { result = proc.Dispatcher.Invoke(blockParam, self, procArg, arg1, arg2, arg3); } catch (EvalUnwinder evalUnwinder) { result = blockParam.GetUnwinderResult(evalUnwinder); } return result; } [Emitted] [MethodImpl(MethodImplOptions.AggressiveOptimization)] public static object Yield4(object arg1, object arg2, object arg3, object arg4, Proc procArg, object self, BlockParam/*!*/ blockParam) { object result; var proc = blockParam.Proc; RequireLambdaArity(proc, 4, arg4); try { result = proc.Dispatcher.Invoke(blockParam, self, procArg, arg1, arg2, arg3, arg4); } catch (EvalUnwinder evalUnwinder) { result = blockParam.GetUnwinderResult(evalUnwinder); } return result; } [Emitted] [MethodImpl(MethodImplOptions.AggressiveOptimization)] public static object YieldN(object[]/*!*/ args, Proc procArg, object self, BlockParam/*!*/ blockParam) { Debug.Assert(args.Length > BlockDispatcher.MaxBlockArity); object result; var proc = blockParam.Proc; RequireLambdaArity(proc, args.Length, args[args.Length - 1]); try { result = proc.Dispatcher.Invoke(blockParam, self, procArg, args); } catch (EvalUnwinder evalUnwinder) { result = blockParam.GetUnwinderResult(evalUnwinder); } return result; } internal static object Yield(object[]/*!*/ args, Proc procArg, object self, BlockParam/*!*/ blockParam) { switch (args.Length) { case 0: return RubyOps.Yield0(procArg, self, blockParam); case 1: return RubyOps.Yield1(args[0], procArg, self, blockParam); case 2: return RubyOps.Yield2(args[0], args[1], procArg, self, blockParam); case 3: return RubyOps.Yield3(args[0], args[1], args[2], procArg, self, blockParam); case 4: return RubyOps.Yield4(args[0], args[1], args[2], args[3], procArg, self, blockParam); default: return RubyOps.YieldN(args, procArg, self, blockParam); } } [Emitted] public static object YieldSplat0(IList/*!*/ splattee, Proc procArg, object self, BlockParam/*!*/ blockParam) { object result; var proc = blockParam.Proc; RequireLambdaArity(proc, 0 + splattee.Count, splattee.Count > 0 ? splattee[splattee.Count - 1] : null); try { result = proc.Dispatcher.InvokeSplat(blockParam, self, procArg, splattee); } catch (EvalUnwinder evalUnwinder) { result = blockParam.GetUnwinderResult(evalUnwinder); } return result; } [Emitted] public static object YieldSplat1(object arg1, IList/*!*/ splattee, Proc procArg, object self, BlockParam/*!*/ blockParam) { object result; var proc = blockParam.Proc; RequireLambdaArity(proc, 1 + splattee.Count, splattee.Count > 0 ? splattee[splattee.Count - 1] : arg1); try { result = proc.Dispatcher.InvokeSplat(blockParam, self, procArg, arg1, splattee); } catch (EvalUnwinder evalUnwinder) { result = blockParam.GetUnwinderResult(evalUnwinder); } return result; } [Emitted] public static object YieldSplat2(object arg1, object arg2, IList/*!*/ splattee, Proc procArg, object self, BlockParam/*!*/ blockParam) { object result; var proc = blockParam.Proc; RequireLambdaArity(proc, 2 + splattee.Count, splattee.Count > 0 ? splattee[splattee.Count - 1] : arg2); try { result = proc.Dispatcher.InvokeSplat(blockParam, self, procArg, arg1, arg2, splattee); } catch (EvalUnwinder evalUnwinder) { result = blockParam.GetUnwinderResult(evalUnwinder); } return result; } [Emitted] public static object YieldSplat3(object arg1, object arg2, object arg3, IList/*!*/ splattee, Proc procArg, object self, BlockParam/*!*/ blockParam) { object result; var proc = blockParam.Proc; RequireLambdaArity(proc, 3 + splattee.Count, splattee.Count > 0 ? splattee[splattee.Count - 1] : arg3); try { result = proc.Dispatcher.InvokeSplat(blockParam, self, procArg, arg1, arg2, arg3, splattee); } catch (EvalUnwinder evalUnwinder) { result = blockParam.GetUnwinderResult(evalUnwinder); } return result; } [Emitted] public static object YieldSplat4(object arg1, object arg2, object arg3, object arg4, IList/*!*/ splattee, Proc procArg, object self, BlockParam/*!*/ blockParam) { object result; var proc = blockParam.Proc; RequireLambdaArity(proc, 4 + splattee.Count, splattee.Count > 0 ? splattee[splattee.Count - 1] : arg4); try { result = proc.Dispatcher.InvokeSplat(blockParam, self, procArg, arg1, arg2, arg3, arg4, splattee); } catch (EvalUnwinder evalUnwinder) { result = blockParam.GetUnwinderResult(evalUnwinder); } return result; } [Emitted] public static object YieldSplatN(object[]/*!*/ args, IList/*!*/ splattee, Proc procArg, object self, BlockParam/*!*/ blockParam) { object result; var proc = blockParam.Proc; RequireLambdaArity(proc, args.Length + splattee.Count, splattee.Count > 0 ? splattee[splattee.Count - 1] : args[args.Length - 1]); try { result = proc.Dispatcher.InvokeSplat(blockParam, self, procArg, args, splattee); } catch (EvalUnwinder evalUnwinder) { result = blockParam.GetUnwinderResult(evalUnwinder); } return result; } [Emitted] public static object YieldSplatNRhs(object[]/*!*/ args, IList/*!*/ splattee, object rhs, Proc procArg, object self, BlockParam/*!*/ blockParam) { object result; var proc = blockParam.Proc; RequireLambdaArity(proc, args.Length + splattee.Count + 1, rhs); try { result = proc.Dispatcher.InvokeSplatRhs(blockParam, self, procArg, args, splattee, rhs); } catch (EvalUnwinder evalUnwinder) { result = blockParam.GetUnwinderResult(evalUnwinder); } return result; } #endregion #region Methods [Emitted] // MethodDeclaration: public static object DefineMethod(object target, RubyScope/*!*/ scope, RubyMethodBody/*!*/ body) { Assert.NotNull(body, scope); RubyModule instanceOwner, singletonOwner; RubyMemberFlags instanceFlags, singletonFlags; bool moduleFunction = false; // a top-level `def' of code run in a host's scope: see below bool hostedTopLevel = false; if (body.HasTarget) { if (!RubyUtils.CanDefineSingletonMethod(target)) { throw RubyExceptions.CreateTypeError("can't define singleton"); } instanceOwner = null; instanceFlags = RubyMemberFlags.Invalid; singletonOwner = scope.RubyContext.GetOrCreateSingletonClass(target); singletonFlags = RubyMemberFlags.Public; } else { var attributesScope = scope.GetMethodAttributesDefinitionScope(); if ((attributesScope.MethodAttributes & RubyMethodAttributes.ModuleFunction) == RubyMethodAttributes.ModuleFunction) { // Singleton module-function's scope points to the instance method's RubyMemberInfo. // This affects: // 1) super call // Super call is looking for Method.DeclaringModule while searching MRO, which would fail if the singleton module-function // was in MRO. Since module-function can only be used on module the singleton method could only be on module's singleton. // Module's singleton is never part of MRO so we are safe. // 2) trace // Method call trace reports non-singleton module. // The instance method owner is GetMethodDefinitionOwner. That can be a class: Class has no // #module_function, but the default a module's `module_function' sets reaches the class body // it is called from (RubyScope.GetVisibilityScope), and MRI then defines both halves there // too (#133). instanceOwner = scope.GetMethodDefinitionOwner(); // `1.instance_eval { M.send(:module_function); def x; end }': the dummy singleton of an // Integer takes no methods, module function or not var dummy = instanceOwner as RubyClass; if (dummy != null && dummy.IsDummySingletonClass) { throw RubyExceptions.CreateTypeError("can't define singleton"); } instanceFlags = RubyMemberFlags.Private; singletonOwner = instanceOwner.GetOrCreateSingletonClass(); singletonFlags = RubyMemberFlags.Public; moduleFunction = true; } else { bool atTopLevel; instanceOwner = scope.GetMethodDefinitionOwner(out atTopLevel); hostedTopLevel = GetHostedTopLevelMirror(scope, instanceOwner, atTopLevel) != null; // A `def' inside `1.5.instance_eval { }' lands on the singleton class of the // number, which is as far as MRI lets it get: running the block is fine, // defining a method in it is not. var singleton = instanceOwner as RubyClass; if (singleton != null && singleton.IsSingletonClass) { if (singleton.IsDummySingletonClass) { // `1.instance_eval { def f; end }' - the dummy stands in for a // singleton class the receiver can never have throw RubyExceptions.CreateTypeError("can't define singleton"); } RubyUtils.RequireDefinableSingleton(singleton.SingletonClassOf); } instanceFlags = (RubyMemberFlags)RubyUtils.GetSpecialMethodVisibility(attributesScope.Visibility, body.Name); singletonOwner = null; singletonFlags = RubyMemberFlags.Invalid; } } RubyMethodInfo instanceMethod = null, singletonMethod = null; if (body.Ast.UsesBlock) { scope.RubyContext.NoteMethodUsingBlock(body.Name); } if (instanceOwner != null) { var definitionScope = DefinitionScope(scope, instanceOwner); if (scope.RubyContext.HasRefinements) { body.SetDefinitionRefinements(instanceOwner, definitionScope.GetActiveRefinements()); } else { body.SetDefinedWithoutRefinements(); } SetMethod(scope.RubyContext, instanceMethod = hostedTopLevel // the scope's copy of Object's method, below: compiled and labelled as Object's (issue 177) ? new RubyMethodInfo(body, definitionScope, instanceOwner, instanceFlags, scope.RubyContext.ObjectClass) : new RubyMethodInfo(body, definitionScope, instanceOwner, instanceFlags) ); } // Code run in a host's scope (ScriptCode.Run(Scope), ScriptEngine.Execute(code, scope)) defines a top-level // method in main's singleton class, so that the host reads it from the scope and the top level of another // scope does not see it (RubyHosting1D). Ruby code expects it to be a private method of Object, which an // instance of any class calls (issue 163): `class Foo; def bar = many; end' raised NameError. So it is // defined there too. Object's is the one CRuby has, and so last-one-wins across scopes, as in CRuby; the // top level of each scope still finds its own first. RubyMethodInfo objectMethod = null; if (hostedTopLevel) { var objectClass = scope.RubyContext.ObjectClass; var definitionScope = DefinitionScope(scope, objectClass); if (scope.RubyContext.HasRefinements) { body.SetDefinitionRefinements(objectClass, definitionScope.GetActiveRefinements()); } else { body.SetDefinedWithoutRefinements(); } SetMethod(scope.RubyContext, objectMethod = new RubyMethodInfo(body, definitionScope, objectClass, instanceFlags) ); } if (singletonOwner != null) { if (scope.RubyContext.HasRefinements) { body.SetDefinitionRefinements(singletonOwner, scope.GetActiveRefinements()); } else { body.SetDefinedWithoutRefinements(); } SetMethod(scope.RubyContext, singletonMethod = new RubyMethodInfo(body, scope, singletonOwner, singletonFlags) ); } if (instanceOwner != null && scope.RubyContext.IsWarningEnabled("performance")) { WarnOptimizedMethodRedefinition(scope.RubyContext, instanceOwner, body.Name); } // the method's scope saves the result => singleton module-function uses instance-method var method = instanceMethod ?? singletonMethod; // :methods coverage records the definition, so that a method that is never called is // still reported (with 0); the body's prologue does the counting. var coverage = body.Coverage; if (coverage != null && (coverage.State.Modes & CoverageModes.Methods) != 0) { var location = body.Ast.Location; coverage.AddMethod(body, method.BodyModule, body.Name, location.Start.Line, location.Start.Column - 1, location.End.Line, location.End.Column - 1); } // Ruby 2.1+: def returns the method name as a symbol (enables `private def foo`). Interned before // method_added, which interns the name as well: the hook then finds it in the identifier cache. object result = scope.RubyContext.StringifyIdentifier(body.Name); if (objectMethod != null) { objectMethod.DeclaringModule.MethodAdded(body.Name); } method.DeclaringModule.MethodAdded(body.Name); if (moduleFunction) { method.DeclaringModule.GetOrCreateSingletonClass().MethodAdded(body.Name); } return result; } // MRI's vm_init_redefined_flag: the core methods its instructions inline, by class. private static readonly Dictionary/*!*/ _optimizedMethods = new Dictionary { { "Integer", new[] { "+", "-", "*", "/", "%", "==", "===", "<", "<=", ">", ">=", "[]", "succ", "&", "|" } }, { "Float", new[] { "+", "-", "*", "/", "%", "==", "===", "<", "<=", ">", ">=" } }, { "String", new[] { "+", "==", "===", "<<", "length", "size", "empty?", "succ", "=~", "freeze", "-@" } }, { "Array", new[] { "+", "<<", "[]", "[]=", "length", "size", "empty?", "max", "min", "hash", "pack", "include?" } }, { "Hash", new[] { "[]", "[]=", "length", "size", "empty?" } }, { "Symbol", new[] { "==", "===" } }, { "NilClass", new[] { "===", "nil?" } }, { "TrueClass", new[] { "===" } }, { "FalseClass", new[] { "===" } }, { "Regexp", new[] { "=~" } }, { "Proc", new[] { "call" } }, { "BasicObject", new[] { "!", "!=" } }, }; /// /// Warning[:performance]: redefining a core method MRI's interpreter optimises is reported. /// private static void WarnOptimizedMethodRedefinition(RubyContext/*!*/ context, RubyModule/*!*/ owner, string/*!*/ name) { string[] names; if (owner.IsClass && owner.Name != null && _optimizedMethods.TryGetValue(owner.Name, out names) && Array.IndexOf(names, name) >= 0) { context.ReportCategoryWarning("performance", String.Format("Redefining '{0}#{1}' disables interpreter and JIT optimizations", owner.Name, name)); } } /// /// The scope a method body is compiled against. Normally the one the `def' was written /// in; for a method of a refinement, one that also has the refinement's holder in use, so /// that the body sees the other refinements the same module declares. The refine block /// had them active while it ran, but that activation goes away with the block, and the /// body runs later. /// private static RubyScope/*!*/ DefinitionScope(RubyScope/*!*/ scope, RubyModule/*!*/ owner) { if (!owner.IsRefinement || owner.RefinementHolder == null) { return scope; } var result = new RubyModuleScope(scope, owner); result.ActivateRefinements(owner.RefinementHolder); return result; } private static void SetMethod(RubyContext/*!*/ callerContext, RubyMethodInfo/*!*/ method) { var owner = method.DeclaringModule; // Do not trigger the add-method event just yet, we need to assign the result into closure before executing any user code. // If the method being defined is "method_added" itself, we would call that method before the info gets assigned to the closure. owner.SetMethodNoEvent(callerContext, method.DefinitionName, method); // expose RubyMethod in the scope (the method is bound to the main singleton instance): if (owner.GlobalScope != null) { RubyOps.ScopeSetMember( owner.GlobalScope.Scope, method.DefinitionName, new RubyMethod(owner.GlobalScope.MainObject, method, method.DefinitionName) ); } } [Emitted] // AliasStatement: public static void AliasMethod(RubyScope/*!*/ scope, string/*!*/ newName, string/*!*/ oldName) { bool atTopLevel; RubyModule owner = scope.GetMethodDefinitionOwner(out atTopLevel); // Object's alias is the one CRuby makes, and so is its NameError when neither has the method. One that only // main's singleton class has (`def self.x') is aliased there alone, as it was before. // And main's singleton class leaves out a method it no longer has (`undef'ed there) that Object has from another // scope: CRuby has that one and aliases it. RubyModule mirror = GetHostedTopLevelMirror(scope, owner, atTopLevel); if (mirror != null) { bool inOwner = owner.ResolveMethod(oldName, VisibilityContext.AllVisible).Found; if (!inOwner || mirror.ResolveMethod(oldName, VisibilityContext.AllVisible).Found) { mirror.AddMethodAlias(newName, oldName); } if (!inOwner) { return; } } owner.AddMethodAlias(newName, oldName); } [Emitted] // UndefineMethod: public static void UndefineMethod(RubyScope/*!*/ scope, string/*!*/ name) { bool atTopLevel; RubyModule owner = scope.GetMethodDefinitionOwner(out atTopLevel); RubyModule mirror = GetHostedTopLevelMirror(scope, owner, atTopLevel); // as for `alias': Object's, and its NameError, as in CRuby; a singleton method of main only in main's singleton class owner.WarnIfUndefiningCoreMethod(name); bool inMirror = mirror != null && mirror.ResolveMethod(name, VisibilityContext.AllVisible).Found; if (!inMirror && !owner.ResolveMethod(name, VisibilityContext.AllVisible).Found) { throw RubyExceptions.CreateUndefinedMethodError(mirror ?? owner, name); } if (inMirror) { mirror.UndefineMethod(name); } owner.UndefineMethod(name); } /// /// Code run in a host's scope (ScriptCode.Run(Scope), ScriptEngine.Execute(code, scope), ScriptSource.Execute(scope)) /// has main's singleton class, the scope's, for the owner of its top-level methods (RubyScope.MethodLookupModule), so /// that the host reads them from the scope and the top level of another scope does not see them (RubyHosting1D). Ruby /// code expects them in Object, where CRuby has them and an instance of any class calls them (issues 163, 177). So a /// top-level `def', `alias', `undef', `public :x'/`private :x' and main.define_method act on Object as well: this is /// Object when is such a scope's owner, reached from its top level (not from a `def' in /// main.instance_eval, which defines a singleton method of main), and null otherwise. /// internal static RubyModule GetHostedTopLevelMirror(RubyScope/*!*/ scope, RubyModule/*!*/ owner, bool atTopLevel) { return atTopLevel && owner is RubyClass cls && cls.IsHostedTopLevelMethodOwner ? scope.RubyContext.ObjectClass : null; } #endregion #region Modules [Emitted] public static RubyModule/*!*/ DefineGlobalModule(RubyScope/*!*/ scope, string/*!*/ name, string sourcePath, int sourceLine) { return DefineModule(scope, scope.Top.TopModuleOrObject, name, sourcePath, sourceLine); } [Emitted] public static RubyModule/*!*/ DefineNestedModule(RubyScope/*!*/ scope, string/*!*/ name, string sourcePath, int sourceLine) { return DefineModule(scope, scope.GetInnerMostModuleForConstantLookup(), name, sourcePath, sourceLine); } [Emitted] public static RubyModule/*!*/ DefineModule(RubyScope/*!*/ scope, object target, string/*!*/ name, string sourcePath, int sourceLine) { var owner = RubyUtils.GetModuleFromObject(scope, target); CheckConstantVisibility(owner, name); return DefineModule(scope, owner, name, sourcePath, sourceLine); } /// /// `module Owner::Name` and `class Owner::Name` name the constant explicitly, so a /// private one is out of reach here just as it is for a reference. /// private static void CheckConstantVisibility(RubyModule/*!*/ owner, string/*!*/ name) { bool isPrivate; RubyModule declaringOwner; using (owner.Context.ClassHierarchyLocker()) { isPrivate = owner.IsPrivateConstantInAncestors(name); declaringOwner = isPrivate ? owner.GetConstantOwnerNoLock(name) : null; } if (isPrivate) { RubyContext.SetPrivateConstantReference(declaringOwner ?? owner); owner.Context.ResolveMissingConstant(owner, name); } } // thread-safe: private static RubyModule/*!*/ DefineModule(RubyScope/*!*/ scope, RubyModule/*!*/ owner, string/*!*/ name, string sourcePath, int sourceLine) { Assert.NotNull(scope, owner); ConstantStorage existing; if (owner.TryGetConstant(scope.GlobalScope, name, out existing)) { RubyModule module = existing.Value as RubyModule; if (module == null || module.IsClass) { throw RubyExceptions.CreateTypeError(DescribePreviousDefinition(owner, name, "{0} is not a module")); } return module; } else { // create class/module object: var result = owner.Context.DefineModule(owner, name); owner.SetConstantLocation(name, sourcePath, sourceLine); return result; } } #endregion #region Classes [Emitted] public static RubyClass/*!*/ DefineSingletonClass(RubyScope/*!*/ scope, object obj) { RubyUtils.RequireDefinableSingleton(obj); var result = scope.RubyContext.GetOrCreateSingletonClass(obj); // MRI's rb_singleton_class: a class's singleton class, once exposed, gets a singleton of its // own, which descends from the superclass's - so D.singleton_class responds to methods // defined in `class << self; class << self` of D's superclass. if (obj is RubyClass) { result.GetOrCreateSingletonClass(); } return result; } [Emitted] public static RubyModule/*!*/ DefineGlobalClass(RubyScope/*!*/ scope, string/*!*/ name, object superClassObject, string sourcePath, int sourceLine) { return DefineClass(scope, scope.Top.TopModuleOrObject, name, superClassObject, sourcePath, sourceLine); } [Emitted] public static RubyModule/*!*/ DefineNestedClass(RubyScope/*!*/ scope, string/*!*/ name, object superClassObject, string sourcePath, int sourceLine) { return DefineClass(scope, scope.GetInnerMostModuleForConstantLookup(), name, superClassObject, sourcePath, sourceLine); } [Emitted] public static RubyModule/*!*/ DefineClass(RubyScope/*!*/ scope, object target, string/*!*/ name, object superClassObject, string sourcePath, int sourceLine) { var owner = RubyUtils.GetModuleFromObject(scope, target); CheckConstantVisibility(owner, name); return DefineClass(scope, owner, name, superClassObject, sourcePath, sourceLine); } // thread-safe: private static RubyClass/*!*/ DefineClass(RubyScope/*!*/ scope, RubyModule/*!*/ owner, string/*!*/ name, object superClassObject, string sourcePath, int sourceLine) { Assert.NotNull(owner); RubyClass superClass = ToSuperClass(owner.Context, superClassObject); // only the owner's own constants: a class of that name in a module Object includes is // not reopened, a new one is defined (MRI's rb_const_defined_at) ConstantStorage existing; if (owner.TryGetConstant(scope.GlobalScope, name, out existing)) { RubyClass cls = existing.Value as RubyClass; if (cls == null || !cls.IsClass) { throw RubyExceptions.CreateTypeError(DescribePreviousDefinition(owner, name, "{0} is not a class")); } if (superClassObject != null && !ReferenceEquals(cls.SuperClass, superClass)) { throw RubyExceptions.CreateTypeError("superclass mismatch for class {0}", name); } return cls; } else { var result = owner.Context.DefineClass(owner, name, superClass, null); owner.SetConstantLocation(name, sourcePath, sourceLine); return result; } } /// /// MRI points at the existing definition when a class/module name is already taken: /// Foo is not a class /// foo.rb:2: previous definition of Foo was here /// private static string/*!*/ DescribePreviousDefinition(RubyModule/*!*/ owner, string/*!*/ name, string/*!*/ format) { var message = String.Format(format, name); string path; int line; if (owner.TryGetConstantLocation(name, out path, out line)) { message += String.Format("\n{0}:{1}: previous definition of {2} was here", path, line, name); } return message; } private static RubyClass/*!*/ ToSuperClass(RubyContext/*!*/ ec, object superClassObject) { return (superClassObject != null) ? RubyClass.CheckInheritable(ec, superClassObject) : ec.ObjectClass; } #endregion #region Constants /// /// A /// ::A /// [Emitted] public static object GetUnqualifiedConstant(RubyScope/*!*/ scope, ConstantSiteCache/*!*/ cache, string/*!*/ name, bool isGlobal) { object result = null; RubyModule missingConstantOwner; RubyModule owner = null; var context = scope.RubyContext; using (context.ClassHierarchyLocker()) { // Thread safety: // Another thread could have already updated the value, so the site version might be the same as CAV. // We do the lookup anyways since it is no-op and this only happens rarely. // // An important invariant holds here: in any time after initialized for the first time the Value field contains a valid value. // Threads can read an older value (the previous version) but that is still correct since we don't guarantee immediate // propagation of the constant write to all readers. // // if (site.Version = CAV) { // <- another thread could increment CAV here - we may return old or new value (both are ok) // value = site.Value; // } else { // <- another thread could get here as well and update the site before we get to update it. // GetConstant(...) // } // Constants might be updated during constant resolution due to autoload. // Any such updates need to invalidate the cache hence we need to capture the version before resolving the constant. int newVersion = context.ConstantAccessVersion; ConstantStorage storage; bool isPrivateGlobal = false; if (!isGlobal) { missingConstantOwner = scope.TryResolveConstantNoLock(scope.GlobalScope, name, out storage, out owner); } else if (context.ObjectClass.TryResolveConstantNoLock(scope.GlobalScope, name, out storage)) { if (context.ObjectClass.IsPrivateConstant(name)) { // `::NAME' is a qualified reference, which a private constant of Object is out of reach of RubyContext.SetPrivateConstantReference(context.ObjectClass); missingConstantOwner = context.ObjectClass; storage = default(ConstantStorage); // a cached miss would lose the private-constant message isPrivateGlobal = true; } else { missingConstantOwner = null; owner = context.ObjectClass; } } else { missingConstantOwner = context.ObjectClass; } object newCacheValue; bool deprecated = false; if (missingConstantOwner == null) { result = storage.Value; newCacheValue = MakeConstantCacheValue(context, storage, owner, name, cache.IsCoreLibrary); deprecated = ReportConstantDeprecation(context, owner, name); } else { newCacheValue = ConstantSiteCache.WeakMissingConstant; } if (!context.IsAutoloadInProgress && !deprecated && !isPrivateGlobal && (isGlobal || !IsLexicallyInObjectSingleton(scope))) { cache.Update(newCacheValue, newVersion); } } if (missingConstantOwner != null) { result = missingConstantOwner.ConstantMissing(name); } else if (context.RactorsStarted && !cache.IsCoreLibrary) { RactorIsolation.CheckConstantRead(context, owner, name, result); } return result; } /// /// What a constant site caches for a constant's value: its weak reference if it has one. /// Once the runtime has started Ractors, a value a non-main Ractor might not be allowed /// to see is wrapped, so that a cached read checks (). /// private static object/*!*/ MakeConstantCacheValue(RubyContext/*!*/ context, ConstantStorage storage, RubyModule owner, string/*!*/ name, bool isCoreLibrary) { object cached = storage.WeakValue ?? storage.Value; if (context.RactorsStarted && !isCoreLibrary && !RactorIsolation.IsTriviallyShareable(storage.Value)) { return new RactorCheckedConstant(context, cached, owner, name); } return cached; } /// /// A cached constant read of a value wrapped: the value, /// if the current Ractor may see it. /// [Emitted] public static object ReadRactorCheckedConstant(object/*!*/ cached) { var constant = (RactorCheckedConstant)cached; var weak = constant.Value as WeakReference; object value = (weak != null) ? weak.Target : constant.Value; return RactorIsolation.CheckConstantRead(constant.Context, constant.Owner, constant.Name, value); } /// /// A1::..::AN /// ::A1::..::AN /// [Emitted] public static object GetQualifiedConstant(RubyScope/*!*/ scope, ConstantSiteCache/*!*/ cache, string/*!*/[]/*!*/ qualifiedName, bool isGlobal) { // The lookup may call Ruby code - #inspect for the TypeError of a qualifier that is no module, const_missing - from a // light frame that has no call site and so checked no stack at its entry (MethodDefinition.LightFrame.MakesCalls): // `def inspect = Obj::X' with Obj not a module recurses through here alone. Only a read the cache cannot answer gets here. RequireSufficientStack(); var globalScope = scope.GlobalScope; var context = globalScope.Context; using (context.ClassHierarchyLocker()) { int newVersion = context.ConstantAccessVersion; ConstantStorage storage; bool anyMissing; RubyModule topModule = isGlobal ? context.ObjectClass : null; RubyModule owner; object result = ResolveQualifiedConstant(scope, qualifiedName, topModule, true, out storage, out anyMissing, out owner); string name = qualifiedName[qualifiedName.Length - 1]; // cache result only if no constant was missing: if (!anyMissing && !context.IsAutoloadInProgress && (isGlobal || !IsLexicallyInObjectSingleton(scope))) { Debug.Assert(result == storage.Value); cache.Update(MakeConstantCacheValue(context, storage, owner, name, cache.IsCoreLibrary), newVersion); } return (context.RactorsStarted && !cache.IsCoreLibrary) ? RactorIsolation.CheckConstantRead(context, owner, name, result) : result; } } /// /// {expr}::A1::..::AN /// [Emitted] public static object GetExpressionQualifiedConstant(object target, RubyScope/*!*/ scope, ExpressionQualifiedConstantSiteCache/*!*/ cache, string/*!*/[]/*!*/ qualifiedName) { RubyModule module = target as RubyModule; if (module == null) { throw RubyUtils.CreateNotModuleException(scope, target); } var condition = cache.Condition; RubyContext context = module.Context; // Note that the module can be bound to another runtime: if (module.Id == condition.ModuleId && context.ConstantAccessVersion == condition.Version) { object value = cache.Value; if (value.GetType() == typeof(WeakReference)) { return ((WeakReference)value).Target; } else if (value.GetType() == typeof(RactorCheckedConstant)) { return ReadRactorCheckedConstant(value); } else { return value; } } using (context.ClassHierarchyLocker()) { int newVersion = context.ConstantAccessVersion; ConstantStorage storage; bool anyMissing; RubyModule owner; object result = ResolveQualifiedConstant(scope, qualifiedName, module, true, out storage, out anyMissing, out owner); string name = qualifiedName[qualifiedName.Length - 1]; // cache result only if no constant was missing: if (!anyMissing && !context.IsAutoloadInProgress) { Debug.Assert(result == storage.Value); cache.Update(MakeConstantCacheValue(context, storage, owner, name, cache.IsCoreLibrary), newVersion, module); } return (context.RactorsStarted && !cache.IsCoreLibrary) ? RactorIsolation.CheckConstantRead(context, owner, name, result) : result; } } /// /// Code in `class << obj' runs once per obj when it sits in a loop or a method, and every run /// has its own singleton class - and its own classes nested in it - to find constants in, /// while a constant site's cache is keyed only on the constant version. Such lookups are not /// cached. `class << self' in a class or module body, the common form, keeps the cache. /// internal static bool IsLexicallyInObjectSingleton(RubyScope/*!*/ scope) { for (RubyScope s = scope; s != null; s = s.Parent) { var cls = s.Module as RubyClass; if (cls != null && cls.IsSingletonClass && !(cls.SingletonClassOf is RubyModule)) { return true; } } return false; } /// /// defined? A /// [Emitted] public static bool IsDefinedUnqualifiedConstant(RubyScope/*!*/ scope, IsDefinedConstantSiteCache/*!*/ cache, string/*!*/ name) { var context = scope.RubyContext; using (context.ClassHierarchyLocker()) { int newVersion = context.ConstantAccessVersion; ConstantStorage storage; bool exists = scope.TryResolveConstantNoLock(null, name, out storage) == null; if (!context.IsAutoloadInProgress && !IsLexicallyInObjectSingleton(scope)) { cache.Update(exists, newVersion); } return exists; } } /// /// defined? ::A /// [Emitted] public static bool IsDefinedGlobalConstant(RubyScope/*!*/ scope, IsDefinedConstantSiteCache/*!*/ cache, string/*!*/ name) { var context = scope.RubyContext; using (context.ClassHierarchyLocker()) { int newVersion = context.ConstantAccessVersion; ConstantStorage storage; bool exists = context.ObjectClass.TryResolveConstantNoLock(null, name, out storage) && !context.ObjectClass.IsPrivateConstant(name); if (!context.IsAutoloadInProgress) { cache.Update(exists, newVersion); } return exists; } } /// /// defined? A1::..::AN /// defined? ::A1::..::AN /// [Emitted] public static bool IsDefinedQualifiedConstant(RubyScope/*!*/ scope, IsDefinedConstantSiteCache/*!*/ cache, string/*!*/[]/*!*/ qualifiedName, bool isGlobal) { var context = scope.RubyContext; using (context.ClassHierarchyLocker()) { int newVersion = context.ConstantAccessVersion; ConstantStorage storage; bool anyMissing; RubyModule topModule = isGlobal ? context.ObjectClass : null; RubyModule owner; try { owner = ResolveQualifiedConstant(scope, qualifiedName, topModule, false, out storage, out anyMissing, out _) as RubyModule; } catch { // autoload can raise an exception scope.RubyContext.SetCurrentException(null); return false; } // Note that the owner could be another runtime's module: bool exists = IsVisibleConstantDefined(owner, context, qualifiedName[qualifiedName.Length - 1], out storage); // cache result only if no constant was missing: if (!anyMissing && !context.IsAutoloadInProgress && (isGlobal || !IsLexicallyInObjectSingleton(scope))) { cache.Update(exists, newVersion); } return exists; } } /// /// defined? {expr}::A /// defined? {expr}::A1::..::AN /// [Emitted] public static bool IsDefinedExpressionQualifiedConstant(object target, RubyScope/*!*/ scope, ExpressionQualifiedIsDefinedConstantSiteCache/*!*/ cache, string/*!*/[]/*!*/ qualifiedName) { RubyModule module = target as RubyModule; if (module == null) { return false; } var condition = cache.Condition; RubyContext context = module.Context; // Note that the module can be bound to another runtime: if (module.Id == condition.ModuleId && context.ConstantAccessVersion == condition.Version) { return cache.Value; } using (context.ClassHierarchyLocker()) { int newVersion = context.ConstantAccessVersion; ConstantStorage storage; bool exists; if (qualifiedName.Length == 1) { // Note that the owner could be another runtime's module: exists = IsVisibleConstantDefined(module, context, qualifiedName[0], out storage); } else { bool anyMissing; RubyModule owner; try { owner = ResolveQualifiedConstant(scope, qualifiedName, module, false, out storage, out anyMissing, out _) as RubyModule; } catch { // autoload can raise an exception: return false; } // Note that the owner could be another runtime's module: exists = IsVisibleConstantDefined(owner, context, qualifiedName[qualifiedName.Length - 1], out storage); // cache result only if no constant was missing: if (anyMissing) { return exists; } } if (!context.IsAutoloadInProgress) { cache.Update(exists, newVersion, module); } return exists; } } private static object ResolveQualifiedConstant(RubyScope/*!*/ scope, string/*!*/[]/*!*/ qualifiedName, RubyModule topModule, bool isGet, out ConstantStorage storage, out bool anyMissing, out RubyModule resultOwner) { Debug.Assert(qualifiedName.Length >= 2 || qualifiedName.Length == 1 && isGet); RubyContext context = scope.RubyContext; context.RequiresClassHierarchyLock(); RubyModule missingConstantOwner; RubyGlobalScope globalScope = scope.GlobalScope; int nameCount = (isGet) ? qualifiedName.Length : qualifiedName.Length - 1; string name = qualifiedName[0]; RubyModule firstOwner = null; if (topModule == null) { // the first name of `A::B` is an ordinary lexical lookup, where a private // constant of an enclosing module is perfectly visible missingConstantOwner = scope.TryResolveConstantNoLock(globalScope, name, out storage, out firstOwner); } else if (TryResolveVisibleConstant(topModule, context, globalScope, name, out storage)) { missingConstantOwner = null; firstOwner = topModule; } else { missingConstantOwner = topModule; } // defined?(A::B::C) never calls const_missing (MRI): a name that is not there makes the // whole path undefined. Only the get does what evaluating A::B would. object result; resultOwner = firstOwner ?? missingConstantOwner; if (missingConstantOwner == null) { result = storage.Value; anyMissing = ReportConstantDeprecation(context, firstOwner, name); } else if (!isGet) { anyMissing = true; RubyContext.ClearPrivateConstantReference(); return null; } else { anyMissing = true; using (context.ClassHierarchyUnlocker()) { result = missingConstantOwner.ConstantMissing(name); } } for (int i = 1; i < nameCount; i++) { RubyModule owner = RubyUtils.GetModuleFromObject(scope, result); resultOwner = owner; // Note that the owner could be another runtime's module: name = qualifiedName[i]; if (TryResolveVisibleConstant(owner, context, globalScope, name, out storage)) { // Constant write updates constant version in a single runtime only. // Therefore if the chain mixes modules from different runtimes we cannot cache the result. if (owner.Context != context) { anyMissing = true; } if (ReportConstantDeprecation(context, owner, name)) { anyMissing = true; } result = storage.Value; } else if (!isGet) { anyMissing = true; RubyContext.ClearPrivateConstantReference(); return null; } else { anyMissing = true; using (context.ClassHierarchyUnlocker()) { result = owner.ConstantMissing(name); } } } return result; } /// /// Emits the warning Module#deprecate_constant asks for, if the constant that just /// resolved is deprecated, and reports whether it did - the caller uses that to leave the /// call site uncached, so the warning is not swallowed by the second reference onwards. /// /// Warning.warn can be overridden in Ruby, so the class hierarchy lock is dropped around /// the call exactly as the #const_missing dispatches here do. /// private static bool ReportConstantDeprecation(RubyContext/*!*/ context, RubyModule owner, string/*!*/ name) { if (!context.HasDeprecatedConstants || owner == null || owner.Context != context) { return false; } RubyModule deprecatedOwner = owner.GetDeprecatedConstantOwnerNoLock(name); if (deprecatedOwner == null) { return false; } using (context.ClassHierarchyUnlocker()) { context.ReportConstantDeprecation(deprecatedOwner, name); } return true; } /// /// A qualified constant reference - `Mod::NAME` - does not see a constant the owner /// declared private. MRI reports it as missing, which routes it through #const_missing, /// and remembers that it was really there so the default #const_missing can say so. /// private static bool TryResolveVisibleConstant(RubyModule/*!*/ owner, RubyContext/*!*/ context, RubyGlobalScope globalScope, string/*!*/ name, out ConstantStorage storage) { if (!owner.TryResolveConstant(context, globalScope, name, out storage)) { return false; } if (owner.Context != context) { return true; } if (owner.IsPrivateConstantInAncestors(name)) { // the NameError names the module that made the constant private, as MRI's does RubyContext.SetPrivateConstantReference(owner.GetConstantOwnerNoLock(name) ?? owner); storage = default(ConstantStorage); return false; } if (owner.IsTopLevelConstantOnly(name)) { storage = default(ConstantStorage); return false; } return true; } /// /// `defined?(Mod::NAME)` is nil for a constant the owner declared private - and unlike a /// reference it must not leave a private-constant reference behind for #const_missing. /// private static bool IsVisibleConstantDefined(RubyModule owner, RubyContext/*!*/ context, string/*!*/ name, out ConstantStorage storage) { storage = default(ConstantStorage); if (owner == null || !owner.TryResolveConstant(context, null, name, out storage)) { return false; } return owner.Context != context || (!owner.IsPrivateConstantInAncestors(name) && !owner.IsTopLevelConstantOnly(name)); } [Emitted] public static object GetMissingConstant(RubyScope/*!*/ scope, ConstantSiteCache/*!*/ cache, string/*!*/ name) { return scope.GetInnerMostModuleForConstantLookup().ConstantMissing(name); } [Emitted] public static object GetGlobalMissingConstant(RubyScope/*!*/ scope, ConstantSiteCache/*!*/ cache, string/*!*/ name) { return scope.RubyContext.ObjectClass.ConstantMissing(name); } [Emitted] // ConstantVariable: public static object SetGlobalConstant(object value, RubyScope/*!*/ scope, string/*!*/ name, string sourcePath, int sourceLine, RubyEncoding/*!*/ encoding) { RubyUtils.SetConstant(scope.RubyContext.ObjectClass, name, value, sourcePath, sourceLine, encoding); return value; } [Emitted] // ConstantVariable: public static object SetUnqualifiedConstant(object value, RubyScope/*!*/ scope, string/*!*/ name, string sourcePath, int sourceLine, RubyEncoding/*!*/ encoding) { RubyUtils.SetConstant(scope.GetInnerMostModuleForConstantLookup(), name, value, sourcePath, sourceLine, encoding); return value; } [Emitted] // ConstantVariable: public static object SetQualifiedConstant(object value, object target, RubyScope/*!*/ scope, string/*!*/ name, string sourcePath, int sourceLine, RubyEncoding/*!*/ encoding) { RubyUtils.SetConstant(RubyUtils.GetModuleFromObject(scope, target), name, value, sourcePath, sourceLine, encoding); return value; } #endregion // MakeArray* public const int OptimizedOpCallParamCount = 5; #region MakeArray [Emitted] public static RubyArray/*!*/ MakeArray0() { return new RubyArray(0); } /// /// Ruby 3 semantics for a call-site `**splat`: the keyword hash is passed as a trailing /// positional argument, but an *empty* one is dropped entirely -- `f(1, **{})` calls /// `f(1)`, not `f(1, {})`. A literal `f(1, {})` still passes the hash, which is why this /// is only emitted for argument hashes that actually contain a `**` splat. /// The result is fed to the ordinary splatting machinery, so it is either [] or [hash]. /// /// /// `f(k: v, **h)`: h's entries added to the keyword hash being built for the call - the /// call's own, so it is filled in place - which is returned; nil adds nothing. Null if h is /// anything else: that goes through Hash#merge, for #to_hash. /// [Emitted] public static object MergeKeywordSplat(RubyScope/*!*/ scope, object keywords, object splatted) { if (splatted == null) { return keywords; } var source = splatted as Hash; var target = keywords as Hash; if (source == null || target == null) { return null; } foreach (var entry in source) { RubyUtils.SetHashElement(scope.RubyContext, target, entry.Key, entry.Value); } return target; } /// /// for a that has to be a Hash - nil /// included, anything else raises TypeError - and a that is one: what /// the parameterless super of `def m(k: 1, **rest)` does with rest (KeywordSplatMerge.HashOnly). /// [Emitted] public static object/*!*/ MergeKeywordHash(RubyScope/*!*/ scope, object keywords, object splatted) { if (!(splatted is Hash)) { throw RubyExceptions.CreateUnexpectedTypeError(scope.RubyContext, splatted, "Hash"); } return MergeKeywordSplat(scope, keywords, splatted); } /// /// `f(**h)` with h a Hash or nil: what makes of it. Null for /// anything else, which the call site first converts with #to_hash (by way of Hash#merge). /// [Emitted] public static RubyArray SplatKeywordHashIfHash(RubyScope/*!*/ scope, object hash) { return (hash == null || hash is Hash) ? SplatKeywordHash(scope, hash) : null; } [Emitted] public static RubyArray/*!*/ SplatKeywordHash(RubyScope/*!*/ scope, object hash) { var keywords = KeywordSplatArgument(scope, hash); if (keywords == null) { return new RubyArray(0); } var result = new RubyArray(1); result.Add(keywords); return result; } /// /// `f(**h)` with h a Hash or nil, as the one argument it passes: null when it passes none at /// all (nil, or an empty Hash), and a copy marked as keyword arguments otherwise. A call /// site that tests for null and passes the hash plainly (SuperCall) avoids the one-or-zero /// element array makes and the splatted call it takes. /// [Emitted] public static Hash KeywordSplatArgument(RubyScope/*!*/ scope, object hash) { // `f(**nil)` passes no keywords at all if (hash == null) { return null; } var dict = hash as IDictionary; if (dict == null) { throw RubyExceptions.CreateImplicitConversionError(scope.RubyContext.GetClassDisplayName(hash), "Hash"); } if (dict.Count == 0) { return null; } // MRI copies: the callee's **rest must not alias the hash the caller splatted var source = dict as Hash; var copy = (source != null) ? source.TryCopyAsKeywordArguments(scope.RubyContext.EqualityComparer) : null; if (copy == null) { copy = new Hash(scope.RubyContext.EqualityComparer, dict.Count); foreach (var entry in dict) { copy[entry.Key] = entry.Value; } copy.IsKeywordArguments = true; } return copy; } /// /// The `**rest' a parameterless `super' passes in `def m(a, **rest)' (see KeywordArgumentSplat.PassingOn): /// of , except that while rest still holds the /// very entries the call passed - the usual case - the hash the call passed them in, /// , is passed on instead of a copy. That hash is the call's own (see /// ), and the method has bound its parameters from it and stops /// holding it as it passes it on, so the parent may keep it or change it. Not when it is rest itself - /// the empty default rest took over (), or a hash the method already /// passed on. /// [Emitted] public static Hash PassOnKeywordRest(RubyScope/*!*/ scope, object rest, object keywords) { var given = keywords as Hash; var hash = rest as Hash; if (given != null && hash != null && given != hash && given.IsKeywordArguments && given.HasSameEntries(hash)) { return (given.Count > 0) ? given : null; } return KeywordSplatArgument(scope, rest); } [Emitted] public static RubyArray/*!*/ MakeArray1(object item1) { return new RubyArray(new object[] { item1, null, null, null }, 0, 1); } [Emitted] public static RubyArray/*!*/ MakeArray2(object item1, object item2) { return new RubyArray(new object[] { item1, item2, null, null }, 0, 2); } /// /// `[item1, item2].min' / `.max' answered without the array (); /// of the result says the call is to be made as written. /// [Emitted] public static object ArrayLiteralExtreme(RubyScope/*!*/ scope, object item1, object item2, ArrayExtremeSite/*!*/ site) { return site.Invoke(scope, item1, item2); } [Emitted] public static bool IsArrayExtremeNotHandled(object result) { return ReferenceEquals(result, ArrayExtremeSite.NotHandled); } [Emitted] public static RubyArray/*!*/ MakeArray3(object item1, object item2, object item3) { return new RubyArray(new object[] { item1, item2, item3, null }, 0, 3); } [Emitted] public static RubyArray/*!*/ MakeArray4(object item1, object item2, object item3, object item4) { return new RubyArray(new object[] { item1, item2, item3, item4 }, 0, 4); } [Emitted] public static RubyArray/*!*/ MakeArray5(object item1, object item2, object item3, object item4, object item5) { return new RubyArray(new object[] { item1, item2, item3, item4, item5 }, 0, 5); } // The elements of an array literal of more than five, in an array built for it alone // (ReflectionCache.MakeArrayOpCall): the Array takes it over instead of copying it. [Emitted] public static RubyArray/*!*/ MakeArrayOwned(object[]/*!*/ items) { Debug.Assert(items.Length >= Utils.MinListSize); return new RubyArray(items, 0, items.Length); } [Emitted] public static RubyArray/*!*/ MakeArrayN(object[]/*!*/ items) { Debug.Assert(items != null); var array = new RubyArray(items.Length); array.AddVector(items, 0, items.Length); return array; } #endregion #region MakeHash [Emitted] public static Hash/*!*/ MakeHash0(RubyScope/*!*/ scope) { return new Hash(scope.RubyContext.EqualityComparer, 0); } [Emitted] public static Hash/*!*/ MakeHash(RubyScope/*!*/ scope, object[]/*!*/ items) { return RubyUtils.SetHashElements(scope.RubyContext, new Hash(scope.RubyContext.EqualityComparer, items.Length / 2), items); } /// /// The trailing hash a call site builds for keyword arguments, marked as such so the /// callee can tell `f(a: 1)` from `f({a: 1})`. /// [Emitted] public static Hash/*!*/ MakeKeywordArgumentsHash(RubyScope/*!*/ scope, object[]/*!*/ items) { var result = MakeHash(scope, items); result.IsKeywordArguments = true; return result; } [Emitted] public static Hash/*!*/ MakeKeywordArgumentsHash0(RubyScope/*!*/ scope) { var result = MakeHash0(scope); result.IsKeywordArguments = true; return result; } /// /// The default of a method's or block's hidden keyword parameter: the call passed no keywords. /// Like , and marked as a hash no one else holds /// (Hash.IsOmittedKeywords), which a **rest parameter then takes over (). /// [Emitted] public static Hash/*!*/ MakeOmittedKeywordArguments(RubyScope/*!*/ scope) { var result = MakeHash0(scope); result.IsKeywordArguments = true; result.IsOmittedKeywords = true; return result; } /// /// The same default for a method without **rest: its prologue only reads the hash - whether it /// is keyword arguments, whether it matches the declared keywords, the value of each - and an /// empty one answers all of that without being made anew on every call. The runtime's one /// shared, frozen hash (RubyContext.OmittedKeywordArguments); MRI allocates nothing here either. /// [Emitted] public static Hash/*!*/ GetOmittedKeywordArguments(RubyScope/*!*/ scope) { return scope.RubyContext.OmittedKeywordArguments; } // `f(a: x)`, `f(a: x, b: y)` and `f(a: x, b: y, c: z)` with Symbol literals for keys, the // usual keyword call: no item array to allocate, and none of the conversions // SetHashElement applies to a String or nil key. A repeated key keeps the last value, as // in MRI (and SetHashElements). [Emitted] public static Hash/*!*/ MakeKeywordArgumentsHash1(RubyScope/*!*/ scope, object key1, object value1) { var result = Hash.CreateKeywordArguments(scope.RubyContext.EqualityComparer, 1); result.AppendUnlinked(key1, value1); return result; } [Emitted] public static Hash/*!*/ MakeKeywordArgumentsHash2(RubyScope/*!*/ scope, object key1, object value1, object key2, object value2) { var result = Hash.CreateKeywordArguments(scope.RubyContext.EqualityComparer, 2); result.AppendUnlinked(key1, value1); result.AppendUnlinked(key2, value2); return result; } [Emitted] public static Hash/*!*/ MakeKeywordArgumentsHash3(RubyScope/*!*/ scope, object key1, object value1, object key2, object value2, object key3, object value3) { var result = Hash.CreateKeywordArguments(scope.RubyContext.EqualityComparer, 3); result.AppendUnlinked(key1, value1); result.AppendUnlinked(key2, value2); result.AppendUnlinked(key3, value3); return result; } // `{a: x}`, `{a: x, b: y}` and `{a: x, b: y, c: z}`: a hash literal with Symbol literals for // keys, built the way the keyword hashes above are - no item array, none of SetHashElement's // String/nil key conversions (a Symbol needs none), and no bucket array until the hash is // first probed. [Emitted] public static Hash/*!*/ MakeSymbolHash1(RubyScope/*!*/ scope, object key1, object value1) { var result = Hash.CreateUnlinked(scope.RubyContext.EqualityComparer, 1); result.AppendUnlinked(key1, value1); return result; } [Emitted] public static Hash/*!*/ MakeSymbolHash2(RubyScope/*!*/ scope, object key1, object value1, object key2, object value2) { var result = Hash.CreateUnlinked(scope.RubyContext.EqualityComparer, 2); result.AppendUnlinked(key1, value1); result.AppendUnlinked(key2, value2); return result; } [Emitted] public static Hash/*!*/ MakeSymbolHash3(RubyScope/*!*/ scope, object key1, object value1, object key2, object value2, object key3, object value3) { var result = Hash.CreateUnlinked(scope.RubyContext.EqualityComparer, 3); result.AppendUnlinked(key1, value1); result.AppendUnlinked(key2, value2); result.AppendUnlinked(key3, value3); return result; } /// True for the hash a call site built out of keyword syntax. [Emitted] public static bool IsKeywordArgumentsHash(object obj) { var hash = obj as Hash; return hash != null && hash.IsKeywordArguments; } // The keyword-parameter prologue (see PrismAstBridge.BindKeywordsFrom) reads the hash a // keyword call passed with the helpers below rather than with Ruby-level Hash calls. MRI // binds keywords without dispatching to Hash methods either, so a redefined Hash#[] or // Hash#key? has no say in it. The hash is the one the call site built (or a copy of a // splatted one), so it is a plain Hash with the default comparer. /// /// True when holds every required keyword - the first /// of - and, unless the method /// takes **rest, no keyword it does not declare. False means the prologue has an /// ArgumentError to word, which it does the slow way. /// [Emitted] public static bool KeywordArgumentsMatch(object keywords, RubySymbol/*!*/[]/*!*/ names, int requiredCount, bool acceptsRest) { var hash = keywords as Hash; return hash != null && hash.HasKeywords(names, requiredCount, acceptsRest); } /// /// The value the call passed for keyword , or /// if it passed none. is where the /// method declares the keyword, which is where a call usually puts it too. /// [Emitted] public static object GetKeywordArgument(object keywords, RubySymbol/*!*/ name, int position) { var hash = keywords as Hash; object value; return (hash != null && hash.TryGetValueByIdentity(name, position, out value)) ? value : DefaultArgument; } // A signature that mixes keywords with optional, rest or post positionals is lowered to a // single splat that its prologue takes apart (PrismAstBridge.LowerGeneralParameters) with // the helpers below rather than with Ruby-level Array calls. The splat is the array the // call made for this call - or, for a block that auto-splats an Array argument, a copy of // it (see AutoSplatArgument) - so consuming it changes nothing anyone else can see. /// The arguments as they arrived, kept aside for a parameterless `super`. [Emitted] public static RubyArray/*!*/ CopySplatArguments(object args) { return new RubyArray((RubyArray)args); } /// /// Takes the hash a keyword call passed off the end of the arguments, or makes an empty one /// if the call passed no keywords. /// [Emitted] public static Hash/*!*/ PopKeywordArguments(RubyScope/*!*/ scope, object args) { var list = (RubyArray)args; int last = list.Count - 1; if (last >= 0 && IsKeywordArgumentsHash(list[last])) { return (Hash)list.TakeLast(); } var omitted = MakeHash0(scope); omitted.IsOmittedKeywords = true; return omitted; } /// A block's single Array argument, which it auto-splats. [Emitted] public static bool IsAutoSplatArgument(object args) { var list = (RubyArray)args; return list.Count == 1 && list[0] is RubyArray; } /// /// The arguments a block that auto-splats its single Array argument binds: a copy of the /// array, which the prologue consumes - the caller's array is left as it was. /// [Emitted] public static RubyArray/*!*/ AutoSplatArgument(object args) { return new RubyArray((RubyArray)((RubyArray)args)[0]); } /// /// The arity check of a method or lambda whose positionals are in the splat. A negative /// means a rest parameter took the cap off. /// is the "; required keyword: k" MRI appends. /// [Emitted] public static void CheckSplatArity(object args, int minimum, int maximum, string/*!*/ requiredKeywords) { int count = ((RubyArray)args).Count; if (count < minimum || (maximum >= 0 && count > maximum)) { var inv = System.Globalization.CultureInfo.InvariantCulture; string expected = (minimum == maximum) ? minimum.ToString(inv) : minimum.ToString(inv) + (maximum < 0 ? "+" : ".." + maximum.ToString(inv)); throw MakeWrongNumberOfArgumentsErrorN(count, expected + requiredKeywords); } } /// The next positional argument; nil if there is none left (a block's). [Emitted] public static object ShiftSplatArgument(object args) { return ((RubyArray)args).TakeFirst(); } /// /// The argument of an optional parameter: the next one if the /// post parameters leave one, otherwise. /// [Emitted] public static object ShiftOptionalSplatArgument(object args, int postCount) { var list = (RubyArray)args; return (list.Count <= postCount) ? DefaultArgument : list.TakeFirst(); } /// /// What the rest parameter takes: everything the post parameters /// leave - nothing if a block was given too few arguments to fill even those. /// [Emitted] public static RubyArray/*!*/ ShiftRestSplatArguments(object args, int postCount) { var list = (RubyArray)args; return list.TakeFirst(Math.Max(0, list.Count - postCount)); } /// /// The **rest parameter: a new hash with whatever the call passed beyond the declared /// . It is a copy, so the method may change it freely. When the call /// passed no keywords at all, the empty default the method made for itself is that new hash /// already: nobody else holds it (Hash.IsOmittedKeywords), so it is taken over, not copied. /// [Emitted] public static Hash/*!*/ MakeKeywordRest(object keywords, RubySymbol/*!*/[]/*!*/ names) { var hash = (Hash)keywords; if (hash.IsOmittedKeywords && hash.Count == 0) { hash.IsOmittedKeywords = false; hash.IsKeywordArguments = false; return hash; } return hash.CopyWithoutKeywords(names); } /// /// The argument a parameter of a method that declares no keyword parameters is about to /// be given. `f(a: 1)` there is a call that passes a positional Hash - Ruby 3 has no /// keyword arguments without keyword parameters to receive them - and the callee may keep /// that hash and pass it on for the rest of its life. So the mark comes off here, at the /// one point where it is known to have done its job; leaving it on is how a hash stored /// by `def set_filter(filter, decode_parms = nil)` and handed on later turns into the /// keyword arguments of an unrelated call. /// /// The hash is always one the call site made for this call (see /// and the copies the splat paths take), so it can /// be cleared in place. Being one of those, it is a Hash and not an instance of a subclass: /// the test is of the exact class, which for anything else is a compare, where `is Hash' /// walks the class's base types on every call made with an argument that is not a Hash. /// [Emitted, MethodImpl(MethodImplOptions.AggressiveInlining)] public static object ClearKeywordArguments(object obj) { if (obj != null && obj.GetType() == typeof(Hash)) { var hash = Unsafe.As(obj); if (hash.IsKeywordArguments) { hash.IsKeywordArguments = false; } } return obj; } /// /// The array a rest parameter is about to be given. A trailing hash that arrived as the /// keyword arguments of this call is not the keyword arguments of anything once it is /// sitting in an array, so the mark comes off - otherwise passing the array on would make /// them keywords again, which is what Ruby 3 separated. A ruby2_keywords method is the /// exception: it gets a hash marked the other way, which a later splat turns back into /// keywords. Either way the caller's hash is left alone and a copy is stored. /// [Emitted] public static RubyArray/*!*/ NormalizeRestArgument(RubyArray/*!*/ args, RubyMethodBody/*!*/ body) { return NormalizeRestArgument(args, body.Ruby2Keywords); } public static RubyArray/*!*/ NormalizeRestArgument(RubyArray/*!*/ args, bool ruby2Keywords) { int last = args.Count - 1; if (last < 0) { return args; } var hash = args[last] as Hash; if (hash == null || !hash.IsKeywordArguments) { return args; } var copy = new Hash(hash); copy.IsRuby2KeywordsHash = ruby2Keywords; args[last] = copy; return args; } /// /// A `*args' at a call site. A hash a ruby2_keywords method is passing along goes back to /// being keyword arguments here - that is the whole point of the mark - as a copy, so that /// the array the caller is holding keeps its marked one. /// /// /// The same for an argument array that was recorded rather than splatted - what an /// Enumerator keeps from the call that made it, and replays later. /// public static object[]/*!*/ RestoreRuby2Keywords(object[]/*!*/ args) { int last = args.Length - 1; if (last < 0) { return args; } var hash = args[last] as Hash; if (hash == null || !hash.IsRuby2KeywordsHash) { return args; } var copy = new Hash(hash); copy.IsKeywordArguments = true; var result = (object[])args.Clone(); result[last] = copy; return result; } [Emitted] public static IList/*!*/ SplatRuby2Keywords(IList/*!*/ list) { int last = list.Count - 1; if (last < 0) { return list; } var hash = list[last] as Hash; if (hash == null || !hash.IsRuby2KeywordsHash) { return list; } var copy = new Hash(hash); copy.IsKeywordArguments = true; var result = new RubyArray(list.Count); for (int i = 0; i < last; i++) { result.Add(list[i]); } result.Add(copy); return result; } #endregion #region Array [Emitted] public static RubyArray/*!*/ AddRange(RubyArray/*!*/ array, IList/*!*/ list) { return array.AddRange(list); } [Emitted] // method call: public static RubyArray/*!*/ AddSubRange(RubyArray/*!*/ result, IList/*!*/ array, int start, int count) { return result.AddRange(array, start, count); } [Emitted] public static RubyArray/*!*/ AddItem(RubyArray/*!*/ array, object item) { array.Add(item); return array; } /// /// `[*list, item]` as a new array of the size it ends up: what `super(*rest, **kw)` passes /// splatted when kw is not empty (SuperCall). /// [Emitted] public static RubyArray/*!*/ SplatAppendItem(IList/*!*/ list, object item) { var result = new RubyArray(list.Count + 1); result.AddRange(list); result.Add(item); return result; } /// /// `[*x]` and `a = *x`. The result is always a new plain Array: MRI copies, so that /// neither the splatted array itself nor an Array subclass nor a frozen #to_a result /// shows through. /// [Emitted] public static IList/*!*/ Unsplat(object splattee) { var list = splattee as IList; if (list == null) { var single = new RubyArray(1); single.Add(splattee); return single; } return new RubyArray(list); } // CaseExpression [Emitted] public static bool ExistsUnsplatCompare(CallSite>/*!*/ comparisonSite, object splattee, object value) { var list = splattee as IList; if (list != null) { for (int i = 0; i < list.Count; i++) { if (IsTrue(comparisonSite.Target(comparisonSite, list[i], value))) { return true; } } return false; } else { return IsTrue(comparisonSite.Target(comparisonSite, splattee, value)); } } // CaseExpression [Emitted] public static bool ExistsUnsplat(object splattee) { var list = splattee as IList; if (list != null) { for (int i = 0; i < list.Count; i++) { if (IsTrue(list[i])) { return true; } } return false; } else { return IsTrue(splattee); } } [Emitted] // parallel assignment: public static object GetArrayItem(IList/*!*/ array, int index) { Debug.Assert(index >= 0); return index < array.Count ? array[index] : null; } [Emitted] // parallel assignment: public static object GetTrailingArrayItem(IList/*!*/ array, int index, int explicitCount) { Debug.Assert(index >= 0); // Trailing l-values are counted from the end of the RHS, but only once the RHS has // enough elements to reach them: `a, b, *c, d = [1]` leaves d nil rather than // indexing past the end. int i = Math.Max(array.Count, explicitCount) - index; return i >= 0 && i < array.Count ? array[i] : null; } [Emitted] // parallel assignment: public static RubyArray/*!*/ GetArrayRange(IList/*!*/ array, int startIndex, int explicitCount) { int size = array.Count - explicitCount; if (size > 0) { RubyArray result = new RubyArray(size); for (int i = 0; i < size; i++) { result.Add(array[startIndex + i]); } return result; } else { return new RubyArray(); } } #endregion #region CLR Vectors (factories mimic Ruby Array factories) [Emitted, RubyConstructor] public static object/*!*/ CreateVector( ConversionStorage/*!*/ elementConversion, ConversionStorage>/*!*/ toAryToInt, BlockParam block, RubyClass/*!*/ self, [NotNull]object/*!*/ arrayOrSize) { Debug.Assert(typeof(TElement) == self.GetUnderlyingSystemType().GetElementType()); var site = toAryToInt.GetSite(CompositeConversionAction.Make(self.Context, CompositeConversion.ToAryToInt)); var union = site.Target(site, arrayOrSize); if (union.First != null) { // block ignored return CreateVectorInternal(elementConversion, union.First); } else if (block != null) { return PopulateVector(elementConversion, CreateVectorInternal(union.Second), block); } else { return CreateVectorInternal(union.Second); } } [Emitted, RubyConstructor] public static Array/*!*/ CreateVectorWithValues(ConversionStorage/*!*/ elementConversion, RubyClass/*!*/ self, [DefaultProtocol]int size, [DefaultProtocol]TElement value) { Debug.Assert(typeof(TElement) == self.GetUnderlyingSystemType().GetElementType()); TElement[] result = CreateVectorInternal(size); for (int i = 0; i < result.Length; i++) { result[i] = value; } return result; } private static TElement[]/*!*/ CreateVectorInternal(int size) { if (size < 0) { throw RubyExceptions.CreateArgumentError("negative array size"); } return new TElement[size]; } private static Array/*!*/ CreateVectorInternal(ConversionStorage/*!*/ elementConversion, IList/*!*/ list) { var site = elementConversion.GetDefaultConversionSite(); var result = new TElement[list.Count]; for (int i = 0; i < result.Length; i++) { object item = list[i]; result[i] = (item is TElement) ? (TElement)item : site.Target(site, item); } return result; } private static object PopulateVector(ConversionStorage/*!*/ elementConversion, TElement[]/*!*/ array, BlockParam/*!*/ block) { var site = elementConversion.GetDefaultConversionSite(); for (int i = 0; i < array.Length; i++) { object item; if (block.Yield(i, out item)) { return item; } array[i] = site.Target(site, item); } return array; } #endregion #region Global Variables [Emitted] public static object GetGlobalVariable(RubyScope/*!*/ scope, string/*!*/ name) { object value; var context = scope.RubyContext; if (context.RactorsStarted && TryGetRactorLocalGlobal(context, name, out value)) { return value; } // no error reported if the variable doesn't exist: context.TryGetGlobalVariable(scope, name, out value); return value; } /// /// A plain read of a global, which in verbose mode warns when the global was never assigned /// (MRI's rb_gvar_undef_getter). `$x ||= v' reads without the warning. /// [Emitted] public static object ReadGlobalVariable(RubyScope/*!*/ scope, string/*!*/ name) { object value; var context = scope.RubyContext; if (context.RactorsStarted && TryGetRactorLocalGlobal(context, name, out value)) { return value; } if (!context.TryGetGlobalVariable(scope, name, out value) && context.Verbose is bool && (bool)context.Verbose) { context.ReportWarning(String.Format("global variable '${0}' not initialized", name), true); } return value; } /// /// GetGlobalVariable through the site's cached variable: an ordinary global is one /// version check and a field read, with no lookup and no lock. /// [Emitted] public static object GetCachedGlobalVariable(RubyScope/*!*/ scope, GlobalVariableSite/*!*/ site) { var context = scope.RubyContext; if (context.RactorsStarted && !site.IsCoreLibrary && RactorIsolation.IsNonMain(context)) { return GetGlobalVariable(scope, site.Name); } var variable = site.GetVariable(context); var plain = variable as GlobalVariableInfo; if (plain != null) { return plain.Value; } return (variable != null) ? context.GetGlobalVariableValue(variable, scope) : null; } /// ReadGlobalVariable through the site's cached variable. [Emitted] public static object ReadCachedGlobalVariable(RubyScope/*!*/ scope, GlobalVariableSite/*!*/ site) { var context = scope.RubyContext; if (context.RactorsStarted && !site.IsCoreLibrary && RactorIsolation.IsNonMain(context)) { return ReadGlobalVariable(scope, site.Name); } var variable = site.GetVariable(context); var plain = variable as GlobalVariableInfo; if (plain != null) { return plain.Value; } if (variable != null) { return context.GetGlobalVariableValue(variable, scope); } if (context.Verbose is bool && (bool)context.Verbose) { context.ReportWarning(String.Format("global variable '${0}' not initialized", site.Name), true); } return null; } /// /// SetGlobalVariable through the site's cached variable. An ordinary global is assigned /// in place; everything else - a new global, a special or read-only one - takes the /// general path, which validates and creates. /// [Emitted] public static object SetCachedGlobalVariable(object value, RubyScope/*!*/ scope, GlobalVariableSite/*!*/ site) { var context = scope.RubyContext; if (context.RactorsStarted && !site.IsCoreLibrary && RactorIsolation.IsNonMain(context)) { return SetGlobalVariable(value, scope, site.Name); } var plain = site.GetVariable(context) as GlobalVariableInfo; if (plain != null) { plain.Assign(value); context.GlobalVariableAssigned(scope, site.Name, value); } else { context.SetGlobalVariable(scope, site.Name, value); } return value; } [Emitted] public static bool IsDefinedGlobalVariable(RubyScope/*!*/ scope, string/*!*/ name) { GlobalVariable variable; var context = scope.RubyContext; if (context.RactorsStarted) { RactorIsolation.CheckGlobalVariable(context, name); } return context.TryGetGlobalVariable(name, out variable) && variable.IsDefined; } [Emitted] public static object SetGlobalVariable(object value, RubyScope/*!*/ scope, string/*!*/ name) { var context = scope.RubyContext; if (context.RactorsStarted) { var ractor = RactorIsolation.CheckGlobalVariable(context, name); if (ractor != null && RactorIsolation.IsDebugVariable(name)) { ractor.Debug = value; return value; } } context.SetGlobalVariable(scope, name, value); return value; } [Emitted] public static void AliasGlobalVariable(RubyScope/*!*/ scope, string/*!*/ newName, string/*!*/ oldName) { var context = scope.RubyContext; if (context.RactorsStarted) { RactorIsolation.CheckGlobalVariables(context); } context.AliasGlobalVariable(newName, oldName); } /// /// A global read in a runtime that has started Ractors: one a non-main Ractor may not /// use raises, and $DEBUG is the Ractor's own. False for the rest, which read as usual. /// private static bool TryGetRactorLocalGlobal(RubyContext/*!*/ context, string/*!*/ name, out object value) { var ractor = RactorIsolation.CheckGlobalVariable(context, name); if (ractor != null && RactorIsolation.IsDebugVariable(name)) { value = ractor.Debug; return true; } value = null; return false; } #endregion #region DLR Scopes internal static bool TryGetGlobalScopeConstant(RubyContext/*!*/ context, Scope/*!*/ scope, string/*!*/ name, out object value) { string mangled; ScopeStorage scopeStorage = ((object)scope.Storage) as ScopeStorage; if (scopeStorage != null) { return scopeStorage.TryGetValue(name, false, out value) || (mangled = RubyUtils.TryMangleName(name)) != null && scopeStorage.TryGetValue(mangled, false, out value); } else { return context.Operations.TryGetMember(scope, name, out value) || (mangled = RubyUtils.TryMangleName(name)) != null && context.Operations.TryGetMember(scope, mangled, out value); } } /// /// Takes back what published, if the scope still holds that /// very object under the name. /// internal static void ScopeRemoveMember(Scope/*!*/ scope, string/*!*/ name, object value) { var scopeStorage = ((object)scope.Storage) as ScopeStorage; if (scopeStorage != null) { object current; if (scopeStorage.TryGetValue(name, false, out current) && ReferenceEquals(current, value)) { scopeStorage.DeleteValue(name, false); } return; } var stringDict = ((object)scope.Storage) as StringDictionaryExpando; if (stringDict != null) { object current; if (stringDict.Dictionary.TryGetValue(name, out current) && ReferenceEquals(current, value)) { stringDict.Dictionary.Remove(name); } } } // TODO: internal static void ScopeSetMember(Scope scope, string name, object value) { object storage = (object)scope.Storage; var scopeStorage = storage as ScopeStorage; if (scopeStorage != null) { scopeStorage.SetValue(name, false, value); return; } var stringDict = storage as StringDictionaryExpando; if (stringDict != null) { stringDict.Dictionary[name] = value; return; } throw new NotImplementedException(); } #endregion #region Regex [Emitted] //RegexMatchReference: public static MutableString GetCurrentMatchGroup(RubyScope/*!*/ scope, int index) { Debug.Assert(index >= 0); return scope.GetInnerMostClosureScope().GetCurrentMatchGroup(index); } [Emitted] //RegexMatchReference: public static MatchData GetCurrentMatchData(RubyScope/*!*/ scope) { return scope.GetInnerMostClosureScope().CurrentMatch; } [Emitted] //RegexMatchReference: public static MutableString GetCurrentMatchLastGroup(RubyScope/*!*/ scope) { return scope.GetInnerMostClosureScope().GetCurrentMatchLastGroup(); } [Emitted] //RegexMatchReference: public static MutableString GetCurrentPreMatch(RubyScope/*!*/ scope) { return scope.GetInnerMostClosureScope().GetCurrentPreMatch(); } [Emitted] //RegexMatchReference: public static MutableString GetCurrentPostMatch(RubyScope/*!*/ scope) { return scope.GetInnerMostClosureScope().GetCurrentPostMatch(); } [Emitted] //RegularExpression: public static bool MatchLastInputLine(RubyRegex/*!*/ regex, RubyScope/*!*/ scope) { var str = scope.GetInnerMostClosureScope().LastInputLine as MutableString; return (str != null) ? RubyRegex.SetCurrentMatchData(scope, regex, str) != null : false; } [Emitted] //MatchExpression: public static object MatchString(MutableString str, RubyRegex/*!*/ regex, RubyScope/*!*/ scope) { var match = RubyRegex.SetCurrentMatchData(scope, regex, str); return (match != null) ? ScriptingRuntimeHelpers.Int32ToObject(match.Index) : null; } #endregion /// /// Specialized signatures exist for upto the following number of string parts /// public const int MakeStringParamCount = 2; #region CreateRegex private static RubyRegex/*!*/ CreateRegexWorker( RubyRegexOptions options, StrongBox regexpCache, bool isLiteralWithoutSubstitutions, Func createRegex) { try { bool once = ((options & RubyRegexOptions.Once) == RubyRegexOptions.Once) || isLiteralWithoutSubstitutions; if (once) { // Note that the user is responsible for thread synchronization if (regexpCache.Value == null) { regexpCache.Value = CreateFrozen(createRegex); } return regexpCache.Value; } else { // In the future, we can consider caching the last Regexp. For some regexp literals // with substitution, the substition will be the same most of the time return CreateFrozen(createRegex); } } catch (RegexpError e) { if (isLiteralWithoutSubstitutions) { // Ideally, this should be thrown during parsing of the source, even if the // expression happens to be unreachable at runtime. throw new SyntaxError(e.Message); } else { throw; } } } /// /// MRI freezes a regexp literal, interpolated or not, but leaves Regexp.new unfrozen. /// Every literal is built through here, so this is the one place that has to do it. /// It does not intern them: /x/.equal?(/x/) is false in MRI too. /// private static RubyRegex/*!*/ CreateFrozen(Func createRegex) { var result = createRegex(); result.Freeze(); return result; } // A literal without substitutions is built once and cached in its site's box, so every // later evaluation only reads the box. The construction is split off: the delegate it // makes captures the parameters, and a closure over parameters is allocated on entry to // the method that declares them - on every evaluation, even one that never needs it. [Emitted] public static RubyRegex/*!*/ CreateRegexB(byte[]/*!*/ bytes, RubyEncoding/*!*/ encoding, RubyRegexOptions options, StrongBox regexpCache) { return regexpCache.Value ?? CreateRegexBWorker(bytes, encoding, options, regexpCache); } private static RubyRegex/*!*/ CreateRegexBWorker(byte[]/*!*/ bytes, RubyEncoding/*!*/ encoding, RubyRegexOptions options, StrongBox regexpCache) { Func createRegex = delegate { return new RubyRegex(CreateMutableStringB(bytes, encoding), options); }; return CreateRegexWorker(options, regexpCache, true, createRegex); } [Emitted] public static RubyRegex/*!*/ CreateRegexL(string/*!*/ str1, RubyEncoding/*!*/ encoding, RubyRegexOptions options, StrongBox regexpCache) { return regexpCache.Value ?? CreateRegexLWorker(str1, encoding, options, regexpCache); } private static RubyRegex/*!*/ CreateRegexLWorker(string/*!*/ str1, RubyEncoding/*!*/ encoding, RubyRegexOptions options, StrongBox regexpCache) { Func createRegex = delegate { return new RubyRegex(CreateMutableStringL(str1, encoding), options); }; return CreateRegexWorker(options, regexpCache, true, createRegex); } [Emitted] public static RubyRegex/*!*/ CreateRegexM(MutableString str1, RubyEncoding/*!*/ encoding, RubyRegexOptions options, StrongBox regexpCache) { Func createRegex = delegate { return new RubyRegex(CreateMutableStringM(str1, encoding), options); }; return CreateRegexWorker(options, regexpCache, false, createRegex); } [Emitted] public static RubyRegex/*!*/ CreateRegexLM(string/*!*/ str1, MutableString str2, RubyEncoding/*!*/ encoding, RubyRegexOptions options, StrongBox regexpCache) { Func createRegex = delegate { return new RubyRegex(CreateMutableStringLM(str1, str2, encoding), options); }; return CreateRegexWorker(options, regexpCache, false, createRegex); } [Emitted] public static RubyRegex/*!*/ CreateRegexML(MutableString str1, string/*!*/ str2, RubyEncoding/*!*/ encoding, RubyRegexOptions options, StrongBox regexpCache) { Func createRegex = delegate { return new RubyRegex(CreateMutableStringML(str1, str2, encoding), options); }; return CreateRegexWorker(options, regexpCache, false, createRegex); } [Emitted] public static RubyRegex/*!*/ CreateRegexMM(MutableString str1, MutableString str2, RubyEncoding/*!*/ encoding, RubyRegexOptions options, StrongBox regexpCache) { Func createRegex = delegate { return new RubyRegex(CreateMutableStringMM(str1, str2, encoding), options); }; return CreateRegexWorker(options, regexpCache, false, createRegex); } [Emitted] public static RubyRegex/*!*/ CreateRegexN(MutableString[]/*!*/ strings, RubyRegexOptions options, StrongBox regexpCache) { Func createRegex = delegate { return new RubyRegex(CreateMutableStringN(strings), options); }; return CreateRegexWorker(options, regexpCache, false, createRegex); } #endregion #region CreateMutableString [Emitted] public static MutableString/*!*/ CreateMutableStringB(byte[]/*!*/ bytes, RubyEncoding/*!*/ encoding) { return MutableString.CreateBinary(bytes, encoding); } [Emitted] public static MutableString/*!*/ CreateMutableStringL(string/*!*/ str1, RubyEncoding/*!*/ encoding) { return MutableString.Create(str1, encoding); } [Emitted] public static MutableString/*!*/ CreateMutableStringM(MutableString str1, RubyEncoding/*!*/ encoding) { var result = MutableString.CreateInternal(str1, encoding); // MRI's "#{x}" is "" + x.to_s: an empty literal in the source encoding, which an // ASCII-only x leaves as it is - unless that is US-ASCII, which gives way to any other if (str1 != null && result.Encoding != encoding && encoding != RubyEncoding.Ascii && encoding.IsAsciiIdentity && result.IsAscii()) { result.ForceEncoding(encoding); } return result; } #region frozen string literals /// /// MRI's fstring table. Under `# frozen_string_literal: true` two literals with the same /// bytes and encoding are the *same* object, not merely two frozen equal ones, so /// "foo".equal?("foo") is true. Entries live as long as the runtime, as they do in MRI. /// private static readonly ConcurrentDictionary/*!*/ _frozenStringLiterals = new ConcurrentDictionary(new FrozenStringLiteralComparer()); /// /// Exact identity of bytes and encoding, which is #eql? rather than #== - the latter calls /// a zero length string comparable with any other whatever its encoding, and two literals /// that disagree about their encoding must not share a frozen instance. /// private sealed class FrozenStringLiteralComparer : IEqualityComparer { public bool Equals(MutableString x, MutableString y) { return x.Encoding == y.Encoding && x.Equals(y); } public int GetHashCode(MutableString str) { return str.GetHashCode() ^ str.Encoding.GetHashCode(); } } private static MutableString/*!*/ InternFrozenStringLiteral(MutableString/*!*/ str) { return _frozenStringLiterals.GetOrAdd(str.Freeze(), str); } /// /// The same table, for the other thing MRI puts in it: a String loaded by /// `Marshal.load(..., freeze: true)'. /// public static MutableString/*!*/ InternFrozenString(MutableString/*!*/ str) { return InternFrozenStringLiteral(str); } // The StrongBox is one per literal in the program, so the table is consulted once per // literal however often it is evaluated - the same shape the regexp literals use. [Emitted] public static MutableString/*!*/ CreateFrozenMutableStringL(string/*!*/ str1, RubyEncoding/*!*/ encoding, StrongBox/*!*/ cache) { return cache.Value ?? (cache.Value = InternFrozenStringLiteral(MutableString.Create(str1, encoding))); } [Emitted] public static MutableString/*!*/ CreateFrozenMutableStringB(byte[]/*!*/ bytes, RubyEncoding/*!*/ encoding, StrongBox/*!*/ cache) { return cache.Value ?? (cache.Value = InternFrozenStringLiteral(MutableString.CreateBinary(bytes, encoding))); } // Only emitted under --debug-frozen-string-literal, which is why the site can be threaded // through as a constant without costing an ordinary run anything. [Emitted] public static MutableString/*!*/ CreateFrozenMutableStringL(string/*!*/ str1, RubyEncoding/*!*/ encoding, StrongBox/*!*/ cache, string/*!*/ site) { return cache.Value ?? (cache.Value = MutableString.RecordLiteralSite( InternFrozenStringLiteral(MutableString.Create(str1, encoding)), site)); } [Emitted] public static MutableString/*!*/ CreateFrozenMutableStringB(byte[]/*!*/ bytes, RubyEncoding/*!*/ encoding, StrongBox/*!*/ cache, string/*!*/ site) { return cache.Value ?? (cache.Value = MutableString.RecordLiteralSite( InternFrozenStringLiteral(MutableString.CreateBinary(bytes, encoding)), site)); } /// /// A literal in a file that said nothing about frozen_string_literal. Mutable, but it /// warns the first time it is mutated - see MutableString.Chill. /// [Emitted] public static MutableString/*!*/ CreateChilledMutableStringL(string/*!*/ str1, RubyEncoding/*!*/ encoding) { return MutableString.Create(str1, encoding).Chill(); } [Emitted] public static MutableString/*!*/ CreateChilledMutableStringB(byte[]/*!*/ bytes, RubyEncoding/*!*/ encoding) { return MutableString.CreateBinary(bytes, encoding).Chill(); } /// /// A literal the compiler found to be all ASCII, in an ASCII-compatible encoding. /// [Emitted] public static MutableString/*!*/ CreateChilledMutableStringA(string/*!*/ str1, RubyEncoding/*!*/ encoding) { return MutableString.CreateChilledAsciiLiteral(str1, encoding); } [Emitted] public static MutableString/*!*/ CreateMutableStringA(string/*!*/ str1, RubyEncoding/*!*/ encoding) { return MutableString.CreateAsciiLiteral(str1, encoding); } [Emitted] public static MutableString/*!*/ CreateChilledMutableStringL(string/*!*/ str1, RubyEncoding/*!*/ encoding, string/*!*/ site) { return MutableString.RecordLiteralSite(MutableString.Create(str1, encoding).Chill(), site); } [Emitted] public static MutableString/*!*/ CreateChilledMutableStringB(byte[]/*!*/ bytes, RubyEncoding/*!*/ encoding, string/*!*/ site) { return MutableString.RecordLiteralSite(MutableString.CreateBinary(bytes, encoding).Chill(), site); } #endregion [Emitted] public static MutableString/*!*/ CreateMutableStringLM(string/*!*/ str1, MutableString str2, RubyEncoding/*!*/ encoding) { return MutableString.CreateMutable(str1, encoding).Append(str2); } [Emitted] public static MutableString/*!*/ CreateMutableStringML(MutableString str1, string/*!*/ str2, RubyEncoding/*!*/ encoding) { return MutableString.CreateInternal(str1, encoding).Append(str2); } [Emitted] public static MutableString/*!*/ CreateMutableStringMM(MutableString str1, MutableString str2, RubyEncoding/*!*/ encoding) { var result = MutableString.TryConcatenateCharacters(str1, str2, encoding); if (result != null) { return result; } result = MutableString.CreateInternal(str1, encoding); AppendInterpolatedPart(result, str2); return result; } /// /// MRI's rb_str_concat_literals: append, and a result that is still US-ASCII takes the encoding /// of the first part that is not - "#{1}a" in a UTF-8 file is UTF-8, "#{y}#{bin}" is binary. /// private static void AppendInterpolatedPart(MutableString/*!*/ result, MutableString part) { if (part != null) { result.Append(part); if (part.Encoding != RubyEncoding.Ascii && result.Encoding == RubyEncoding.Ascii) { result.ForceEncoding(part.Encoding); } } } // TODO: we should emit Append calls directly, and not create an array first [Emitted] public static MutableString/*!*/ CreateMutableStringN(MutableString/*!*/[]/*!*/ parts) { Debug.Assert(parts.Length > 0); var concatenated = MutableString.TryConcatenateCharacters(parts); if (concatenated != null) { return concatenated; } // room for all of it up front rather than a regrowth per part (a part counts its // characters or its bytes, whichever it holds, which is what a capacity hint needs) int capacity = 0; for (int i = 0; i < parts.Length; i++) { if (parts[i] != null) { capacity += parts[i].Length; } } // MRI's rb_str_concat_literals starts from the first part's encoding: "a#{1}b" in a UTF-8 // file is UTF-8, though every part is ASCII and the Integer's is US-ASCII var result = MutableString.CreateMutable(capacity, (parts[0] != null) ? parts[0].Encoding : RubyEncoding.Ascii); for (int i = 0; i < parts.Length; i++) { AppendInterpolatedPart(result, parts[i]); } return result; } #endregion #region CreateSymbol [Emitted] public static RubySymbol/*!*/ CreateSymbolM(MutableString str1, RubyEncoding/*!*/ encoding, RubyScope/*!*/ scope) { return CreateInterpolatedSymbol(scope.RubyContext, CreateMutableStringM(str1, encoding)); } [Emitted] public static RubySymbol/*!*/ CreateSymbolLM(string/*!*/ str1, MutableString str2, RubyEncoding/*!*/ encoding, RubyScope/*!*/ scope) { return CreateInterpolatedSymbol(scope.RubyContext, CreateMutableStringLM(str1, str2, encoding)); } [Emitted] public static RubySymbol/*!*/ CreateSymbolML(MutableString str1, string/*!*/ str2, RubyEncoding/*!*/ encoding, RubyScope/*!*/ scope) { return CreateInterpolatedSymbol(scope.RubyContext, CreateMutableStringML(str1, str2, encoding)); } [Emitted] public static RubySymbol/*!*/ CreateSymbolMM(MutableString str1, MutableString str2, RubyEncoding/*!*/ encoding, RubyScope/*!*/ scope) { return CreateInterpolatedSymbol(scope.RubyContext, CreateMutableStringMM(str1, str2, encoding)); } [Emitted] public static RubySymbol/*!*/ CreateSymbolN(MutableString[]/*!*/ strings, RubyScope/*!*/ scope) { return CreateInterpolatedSymbol(scope.RubyContext, CreateMutableStringN(strings)); } /// /// :"#{...}" and %I[#{...}] are String#to_sym of what was interpolated: bytes that are not /// valid in the string's encoding cannot name a symbol (MRI's rb_str_intern), where an /// ASCII byte means itself. /// private static RubySymbol/*!*/ CreateInterpolatedSymbol(RubyContext/*!*/ context, MutableString/*!*/ str) { if (str.Encoding.IsAsciiIdentity && str.ContainsInvalidCharacters()) { throw new EncodingError(RubyExceptions.FormatMessage("invalid symbol in encoding {0} :{1}", str.Encoding.Name, context.Inspect(str).ToString())); } return context.CreateSymbol(str, false); } #endregion #region Strings, Encodings [Emitted] public static RubyEncoding/*!*/ CreateEncoding(int codepage) { return RubyEncoding.GetRubyEncoding(codepage); } [Emitted, Obsolete("Internal only")] public static byte[]/*!*/ GetMutableStringBytes(MutableString/*!*/ str) { // The callee (a Win32API function) may write into the buffer: it must not be shared with // another string (copy-on-write, see MutableString.Content.Clone). int byteCount; var result = str.GetWritableByteArray(out byteCount); return result; } #endregion #region Booleans // AggressiveInlining: the JIT left these calls in compiled method bodies (1.4% of Json and // DeltaBlue in RubyOps.IsTrue alone), where a condition is a type test and a compare. [Emitted] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool IsTrue(object obj) { return (obj is bool) ? (bool)obj == true : obj != null; } [Emitted] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static bool IsFalse(object obj) { return (obj is bool) ? (bool)obj == false : obj == null; } [Emitted] public static object NullIfFalse(object obj) { return (obj is bool && !(bool)obj) ? null : obj; } [Emitted] public static object NullIfTrue(object obj) { return (obj is bool && !(bool)obj || obj == null) ? DefaultArgument : null; } #endregion #region Exceptions // // NOTE: // Exception Ops go directly to the current exception object. MRI ignores potential aliases. // /// /// Called in try-filter that wraps the entire top-level code. /// We just need to capture stack trace, should not filter out any exception. /// [Emitted] public static bool TraceTopLevelCodeFrame(RubyScope/*!*/ scope, Exception/*!*/ exception) { RubyExceptionData.GetInstance(exception).CaptureExceptionTrace(scope); return false; } /// /// MRI 3.4+ (vm_insnhelper.c warn_unused_block): a block passed from Ruby code to a method /// that never uses it may be ignored. Reported once per method, and - unless /// Warning[:strict_unused_block] is on - not at all for a name some method that does use a /// block also has, since the call may well have meant that one. /// private static readonly System.Text.RegularExpressions.Regex _ironRubyLibraryFile = new System.Text.RegularExpressions.Regex(@"[/\\](StdLib|Lib)[/\\]ironruby[/\\]"); [Emitted] public static void WarnUnusedBlock(Proc block, RubyMethodInfo/*!*/ method) { if (block == null || method.Body.UnusedBlockReported) { return; } var context = method.Context; bool strict = context.IsWarningEnabled("strict_unused_block"); if (!strict && context.IsMethodNameUsingBlock(method.DefinitionName)) { return; } method.Body.UnusedBlockReported = true; string file = method.Document != null ? method.Document.FileName : null; if (file != null && _ironRubyLibraryFile.IsMatch(file)) { // IronRuby's own Ruby implementations of what MRI writes in C, which never warns return; } if (context.Verbose is bool && ((bool)context.Verbose || strict)) { string label = IronRuby.Compiler.Ast.MethodDefinition.QualifyFrameLabel(method.DefinitionName, method.BodyModule); context.ReportWarning(file != null ? String.Format("the block passed to '{0}' defined at {1}:{2} may be ignored", label, file, method.SourceSpan.Start.Line) : String.Format("the block may be ignored because '{0}' does not use a block", label) ); } } /// /// The exception filter around a file's top-level code: a return from a block at the top /// level unwinds to here and ends the file; anything else gets the frame traced and passes. /// [Emitted] public static bool IsTopLevelReturn(RubyScope/*!*/ scope, Exception/*!*/ exception) { var unwinder = exception as MethodUnwinder; if (unwinder != null && unwinder.TargetFrame == scope) { return true; } RubyExceptionData.GetInstance(exception).CaptureExceptionTrace(scope); return false; } // Ruby method exit filter. It only sees MethodUnwinders: the backtrace of any other exception // is captured by whatever handles it (see MethodDefinition.TransformBody). [Emitted] public static bool IsMethodUnwinderTargetFrame(RubyScope/*!*/ scope, Exception/*!*/ exception) { var unwinder = exception as MethodUnwinder; return unwinder != null && unwinder.TargetFrame == scope.FlowControlScope; } [Emitted] public static object GetMethodUnwinderReturnValue(Exception/*!*/ exception) { return ((MethodUnwinder)exception).ReturnValue; } [Emitted] public static void LeaveMethodFrame(RuntimeFlowControl/*!*/ rfc) { // no write to a frame that is not active: CreateRfcForMethod's shared inactive frame stays untouched if (rfc._activeFlowControlScope != null) { rfc.LeaveMethod(); ReleaseScopeFrame(rfc as RubyScope); } } // A captured scope needs its locals, not a completed interpreter frame's temporary // operands (which may include the Fiber handle returned by its factory method). [Emitted] public static void ReleaseScopeFrame(RubyScope scope) { if (scope != null) { scope.InterpretedFrame = null; } } /// /// Filters exceptions raised from EH-body, EH-rescue and EH-else clauses. /// [Emitted] public static bool CanRescue(RubyScope/*!*/ scope, Exception/*!*/ exception) { return CanRescueCore(scope, exception, false, null, -1); } /// /// CanRescue of a body with rescue clauses, for the exceptions its statements throw: the walk of the stack below the frame /// may wait until the exception is exposed (RubyExceptionData.TryDeferCatchSite). The frame either rescues the exception, /// restoring $! once the rescue clause is done, or throws it on (Body.TransformExceptionHandling), both before it is left. /// Not so an ensure clause, which may end the exception with a jump and leave it in $!. /// public static bool CanRescueDeferTrace(RubyScope/*!*/ scope, Exception/*!*/ exception) { return CanRescueCore(scope, exception, true, null, -1); } /// /// CanRescueDeferTrace of a frame whose record in PrecompiledFrames has the index /// (CompiledFrameDepthExpression; -1 if it has none): the frame's record is known to be the innermost one - the frame has /// caught the exception - without reading the exception's CLR trace (see RubyExceptionData.TryDeferCatchSite). /// public static bool CanRescueDeferTrace(RubyScope/*!*/ scope, Exception/*!*/ exception, int frameDepth) { return CanRescueCore(scope, exception, true, null, frameDepth); } /// /// CanRescue of the handler around a body's rescue and else clauses (Body.TransformExceptionHandling). /// is the exception the rescue clauses ran for, if they ran: an exception they raise is raised /// while $! is that one, which is its cause (MRI), but the clauses' finally block has restored $! by the time this runs. /// Kernel#raise has decided the cause of its own exceptions already; this is for those C# threw - `1/0', `nil.foo', a /// rescue class list that is not a class (`rescue 42'). /// public static bool CanRescueAfterClauses(RubyScope/*!*/ scope, Exception/*!*/ exception, Exception handled) { return CanRescueCore(scope, exception, false, handled, -1); } /// /// The filter around an ensure clause's statements (Body.TransformExceptionHandling), compiled: an exception leaving them /// gets $! - the exception the clause runs for, if any - as its cause unless it has one, before the clause's finally block /// restores $! (it is an exception raised while $! was that one). Not a jump: a StackUnwinder has no cause. Always false, /// and so the filter lets the exception through. /// public static bool SetCauseLeavingEnsure(RubyScope/*!*/ scope, Exception/*!*/ exception) { try { if (!(exception is StackUnwinder) && !RubyUtils.IsReclaimingFiber) { var data = RubyExceptionData.GetInstance(exception); if (!data.HasCause) { data.TrySetCause(scope.RubyContext.CurrentException); } } } catch (Exception) { // the cause is what the next frame decides } return false; } /// /// The same for the interpreter, which has no exception filters: it catches the exception and throws it again. The catch /// is the first Ruby frame the exception reaches, so it takes the exception's backtrace too, as CanRescue would have: /// thrown again, the exception's own trace would show the frame a second time (and every frame below it a frame lower). /// public static bool SetCauseLeavingInterpretedEnsure(RubyScope/*!*/ scope, Exception/*!*/ exception) { SetCauseLeavingEnsure(scope, exception); if (!(exception is StackUnwinder) && !RubyUtils.IsReclaimingFiber) { RubyExceptionData.GetInstance(exception).CaptureExceptionTrace(scope); } return false; } private static bool CanRescueCore(RubyScope/*!*/ scope, Exception/*!*/ exception, bool deferTrace, Exception handled, int frameDepth) { // A CLR finally may replace the reclaim signal with an ordinary exception. // That still belongs to a discarded stack and must not enter a Ruby rescue. if (RubyUtils.IsReclaimingFiber) { return false; } if (exception is StackUnwinder) { return false; } LocalJumpError lje = exception as LocalJumpError; if (lje != null && lje.SkipFrame == scope.FlowControlScope) { return false; } // What ensure clauses the exception went through took of its backtrace (RubyExceptionData.EnterPassedEnsure), which this // frame completes: taken over before $! - still the exception, which the clauses ran for - is read below. var passed = RubyExceptionData.TryGetInstance(exception)?.TakePassedEnsureCapture(); // Only an exception nothing can have kept: one Kernel#raise made itself. One C# threw may be kept by C# code that hands // it to Ruby later (Encoding::Converter#last_error), and so may a CLR exception thrown again. if (deferTrace) { var raised = RubyExceptionData.TryGetInstance(exception); deferTrace = raised != null && raised.RaisedUnkept; } // calls "new" on the exception class if it hasn't been called yet: exception = RubyExceptionData.HandleException(scope, exception); // Exception#cause: the exception that was being handled when this one was raised. // $! still holds it here - we are about to overwrite it below. Kernel#raise has // already decided the cause for the exceptions it throws, and TrySetCause leaves // those (and any re-raise of an already-raised exception) alone. // One lookup, not two: the data lives in Exception.Data, whose store is a linear scan. var data = RubyExceptionData.GetInstance(exception); if (handled == null || handled == exception) { data.TrySetCause(scope.RubyContext.CurrentException); } else if (data.TrySetCause(handled)) { // the cause is exposed to Ruby code (Exception#cause), and this is the frame that rescued it RubyExceptionData.ResolveCatchSite(handled); } scope.RubyContext.CurrentException = exception; data.CaptureExceptionTrace(scope, deferTrace, frameDepth, passed); return true; } /// /// What returns for an exception that goes through an ensure-only body the way it goes through the /// handler CanRescue turns down: without $! being set for the ensure clause. /// internal static readonly Exception/*!*/ PassEnsureWithoutCurrentException = new Exception("#pass-ensure"); // 512 KB of stack beyond what StackLimit keeps: what an ensure clause gets at least when it runs on top of the raise private const ulong PassEnsureStackRoom = 512 * 1024; // the depth of compiled frames from which an ensure clause takes the catch (see PassEnsure) private const int PassEnsureMaxDepth = 1000; /// /// The exception filter of a compiled body that has an ensure clause and nothing else (Body.TransformExceptionHandling, /// EnsureGuardExpression), run in the CLR's first pass, while the stack is searched for a handler. Null: catch the exception, /// which then takes CanRescue's path - $! set, the backtrace captured, and the exception thrown again after the ensure clause. /// Anything else: let it go through, the ensure clause running from a CLR finally block as it passes, and set $! to the /// result first unless it is . A catch and a throw again is a full exception /// dispatch of its own for every ensure an exception passes; going through costs next to nothing. /// /// It goes through only where the catch would have nothing left to do that the ensure clause could tell: the exception is /// one CanRescue turns down, or one whose Ruby #new has run (Handled) and whose backtrace a frame it passed has already /// captured - its throw-site trace is not needed again, and a throw again would only start it over. The cause is decided /// here rather than in CanRescue, from the same $!: the ensure clauses that ran in between restore it. And there is room /// on the stack: a finally block runs on top of the frames the exception came out of, a catch below them (#128). /// /// An exception Kernel#raise made whose backtrace no frame has taken yet goes through as well, in a frame whose record in /// PrecompiledFrames is known () and in which nothing throws it again (EnsureJumpFinder): the /// catch only took the backtrace, and that waits until the exception is caught or exposed (RubyExceptionData.EnterPassedEnsure). /// The walk of the whole stack the catch took was most of what a raise through an ensure clause cost. /// public static Exception PassEnsure(RubyScope/*!*/ scope, Exception/*!*/ exception) { return PassEnsure(scope, exception, -1); } public static Exception PassEnsure(RubyScope/*!*/ scope, Exception/*!*/ exception, int frameDepth) { // A stack overflow, or a stack thousands of frames deep: one CLR dispatch across thousands of frames with filters costs // more than a catch and a throw again per frame (SystemStackError through 10k ensure clauses: 2.5-4x slower than // the catch on net8.0). if (exception is SystemStackError || exception is InsufficientExecutionStackException || Precompiled.PrecompiledFrames.CurrentDepth > PassEnsureMaxDepth || !StackLimit.HasRoom(PassEnsureStackRoom)) { return null; } if (RubyUtils.IsReclaimingFiber || exception is StackUnwinder) { return PassEnsureWithoutCurrentException; } LocalJumpError lje = exception as LocalJumpError; if (lje != null && lje.SkipFrame == scope.FlowControlScope) { return PassEnsureWithoutCurrentException; } // `raise ""message""' makes a RuntimeError that no #new has marked handled: CanRescue would only mark it (IsHandledInPlace) var data = RubyExceptionData.TryGetInstance(exception); if (data == null || !(data.Handled || frameDepth >= 0 && RubyExceptionData.IsHandledInPlace(scope.RubyContext, exception)) || !(data.HasBacktrace || data.RaiseBacktrace != null || frameDepth >= 0 && data.CanPassUntraced(scope, frameDepth))) { return null; } data.Handled = true; data.TrySetCause(scope.RubyContext.CurrentException); // the ensure clause will run Ruby code before an outer frame's rescue clauses are matched (see RescuePassMayTest) data.PendingPassedEnsures++; return exception; } /// /// The fault block of EnsureGuardExpression: the exception let through is leaving the body, and the /// ensure clause sees it in $!, as it would had CanRescue caught it. /// public static void EnterPassedEnsure(RubyScope/*!*/ scope, Exception passing) { EnterPassedEnsure(scope, passing, -1); } /// The index of the frame's record, as PassEnsure had it. public static void EnterPassedEnsure(RubyScope/*!*/ scope, Exception passing, int frameDepth) { if (passing != null && passing != PassEnsureWithoutCurrentException) { scope.RubyContext.CurrentException = passing; var data = RubyExceptionData.TryGetInstance(passing); if (data != null) { if (data.PendingPassedEnsures > 0) { data.PendingPassedEnsures--; } if (frameDepth >= 0) { // the ensure clause may expose the exception: where its backtrace is still to be taken, it is this frame's data.EnterPassedEnsure(scope, frameDepth); } } } } /// /// The exception filter of a compiled body with rescue clauses (Body.TransformExceptionHandling, RubyFilterExpression), run in /// the CLR's first pass: RescuePassUnwinder || RescuePassMayTest && (one CannotMatch call for each class of each /// clause), each site a constant of the expression. True: none of the clauses can match the exception, which goes through /// - no catch, no `===', no throw again. False: the exception is caught and matched as before (CanRescue, then the clauses). /// /// Ruby matches the clauses after the ensure clauses of the frames the exception left have run, and evaluates each class /// expression and its `===' then; this decides now. That is the same answer only where nothing can tell the difference: /// - every class of the list is a constant read through a site whose cache is current (ConstantAccessVersion): the read /// would return the cached module and run nothing (no autoload, no const_missing); /// - each `===' is Module#===, which only tests the class of the exception (and so is the default StandardError's); /// - no Ruby code runs before the clauses would be matched: no compiled ensure clause has let the exception through /// (PendingPassedEnsures); an interpreted one, or one with a jump, catches it, so this frame is not reached in this pass; /// - CanRescue would match the exception itself, not a new one made by its class's Ruby #new (IsHandledInPlace), and would /// not call a #set_backtrace of the exception's own when it captures the backtrace (CaptureRunsNoRubyCode); /// - no TracePoint is on. /// An exception CanRescue turns down (an unwinder: throw, break, return from a block, Thread#kill) goes through as well: the /// clauses are not matched against it at all. A SystemStackError, an InsufficientExecutionStackException or a stack more /// than PassEnsureMaxDepth compiled frames deep takes the catch, as in PassEnsure. /// public static bool RescuePassUnwinder(RubyScope/*!*/ scope, Exception/*!*/ exception) { try { if (RubyUtils.IsReclaimingFiber || exception is StackUnwinder) { return !IsDeepOrOverflow(exception); } LocalJumpError lje = exception as LocalJumpError; return lje != null && lje.SkipFrame == scope.FlowControlScope && !IsDeepOrOverflow(exception); } catch (Exception) { // catch it (see RescuePassMayTest) return false; } } /// /// The part of 's description that is about the exception rather than the clauses: whether the /// class list may be tested now (RescueSiteCannotMatch, RescueDefaultCannotMatch, one call for each class). /// public static bool RescuePassMayTest(RubyScope/*!*/ scope, Exception/*!*/ exception) { // The CLR takes an exception thrown in a filter for a filter that turned the exception down - here, for "goes through". // These say "catch it" instead. try { // A refinement of `===' is what the rescue clauses call (CompareException), which CannotMatch does not see. if (IsDeepOrOverflow(exception) || TracePoint.ActiveEvents != 0 || scope.RubyContext.HasRefinements && scope.RubyContext.IsRefinedMethodName("===")) { return false; } var data = RubyExceptionData.TryGetInstance(exception); return (data == null || data.PendingPassedEnsures == 0) && RubyExceptionData.IsHandledInPlace(scope.RubyContext, exception) && RubyExceptionData.CaptureRunsNoRubyCode(scope.RubyContext, exception); } catch (Exception) { return false; } } /// /// The class a rescue clause reads from - the cached value, if the cache is current - cannot match. /// public static bool RescueSiteCannotMatch(RubyScope/*!*/ scope, Exception/*!*/ exception, ConstantSiteCache/*!*/ site) { try { var context = scope.RubyContext; if (site.Version != context.ConstantAccessVersion) { return false; } object value = site.Value; var weak = value as WeakReference; if (weak != null) { value = (weak == ConstantSiteCache.WeakMissingConstant) ? null : weak.Target; } var module = value as RubyModule; return module != null && CannotMatch(context, exception, module); } catch (Exception) { return false; } } /// /// The last test of the rescue filter, once every clause has been found not to match: the exception goes through, and gets the /// cause CanRescue would have given it here - this frame's $!, which the frame restores to its own on the way out (an /// exception C# threw from an ensure clause in the frame has the exception that clause ran for as its cause). An exception /// Kernel#raise made has one already. Always true. /// public static bool RescuePassCommit(RubyScope/*!*/ scope, Exception/*!*/ exception) { try { RubyExceptionData.GetInstance(exception).TrySetCause(scope.RubyContext.CurrentException); } catch (Exception) { // goes through all the same: the cause is what an outer frame then decides } return true; } /// A bare `rescue' (StandardError) cannot match. public static bool RescueDefaultCannotMatch(RubyScope/*!*/ scope, Exception/*!*/ exception) { try { return CannotMatch(scope.RubyContext, exception, scope.RubyContext.StandardErrorClass); } catch (Exception) { return false; } } private static bool IsDeepOrOverflow(Exception/*!*/ exception) { return exception is SystemStackError || exception is InsufficientExecutionStackException || Precompiled.PrecompiledFrames.CurrentDepth > PassEnsureMaxDepth; } // `module === exception' is Module#=== and false (the class test first: a clause that matches is caught either way) private static bool CannotMatch(RubyContext/*!*/ context, Exception/*!*/ exception, RubyModule/*!*/ module) { return !context.IsKindOf(exception, module) && IsBuiltinCaseEquality(context, module); } [Emitted] public static Exception/*!*/ MarkException(Exception/*!*/ exception) { RubyExceptionData.GetInstance(exception).Handled = true; return exception; } // $! saved by a body with exception handling, to be restored later (see Body.TransformExceptionHandling) [Emitted] public static Exception GetCurrentException(RubyScope/*!*/ scope) { return scope.RubyContext.CurrentExceptionNoExpose; } /// /// $! handed to Ruby code: `rescue => e'. Its backtrace is completed first (RubyExceptionData.TryDeferCatchSite). /// public static Exception GetCurrentExceptionForRescueTarget(RubyScope/*!*/ scope) { return scope.RubyContext.CurrentException; } /// /// The exception a body throws again on its way out (Body.TransformExceptionHandling): its backtrace is completed before /// the frame that caught it is left (RubyExceptionData.TryDeferCatchSite). /// public static Exception/*!*/ ExposeException(Exception/*!*/ exception) { RubyExceptionData.ResolveCatchSite(exception); return exception; } /// /// Sets $!. Used in EH finally clauses to restore exception stored in oldExceptionVariable local. /// [Emitted] public static void SetCurrentException(RubyScope/*!*/ scope, Exception exception) { scope.RubyContext.CurrentException = exception; } [Emitted] //RescueClause: public static bool CompareException(BinaryOpStorage/*!*/ comparisonStorage, RubyScope/*!*/ scope, object classObject) { var context = scope.RubyContext; if (!(classObject is RubyModule)) { throw RubyExceptions.CreateTypeError("class or module required for rescue clause"); } // A refinement of `===' active where the rescue clause is written is what MRI's rescue calls (check_match calls // `===' with the refinements of the clause's cref); the comparison site has no scope to see one. bool refined = context.HasRefinements && context.IsRefinedMethodName("===") && IsRefinedHere(scope, classObject, "==="); // Module#=== only tests the class of the exception; any other #=== is handed the exception, and may keep it var exception = context.CurrentExceptionNoExpose; if (exception != null && RubyExceptionData.IsCatchSitePending(exception) && (refined || !IsBuiltinCaseEquality(context, (RubyModule)classObject))) { RubyExceptionData.ResolveCatchSite(exception); } bool result; if (refined) { var refinedSite = context.GetOrCreateSendSite>( "===", new RubyCallSignature(1, RubyCallFlags.HasScope | RubyCallFlags.HasImplicitSelf) ); result = IsTrue(refinedSite.Target(refinedSite, scope, classObject, exception)); } else { var site = comparisonStorage.GetCallSite("==="); result = IsTrue(site.Target(site, classObject, exception)); } if (result) { // the frames the exception unwound to get here are no part of what is raised from here on Precompiled.PrecompiledFrames.ForgetLeft(); TraceRescue(scope, exception); } return result; } private static bool IsBuiltinCaseEquality(RubyContext/*!*/ context, RubyModule/*!*/ module) { var method = context.GetImmediateClassOf(module).ResolveMethod("===", VisibilityContext.AllVisible).Info; return method is RubyLibraryMethodInfo && method.DeclaringModule == context.ModuleClass; } [Emitted] //RescueClause: public static bool CompareSplattedExceptions(BinaryOpStorage/*!*/ comparisonStorage, RubyScope/*!*/ scope, IList/*!*/ classObjects) { for (int i = 0; i < classObjects.Count; i++) { if (CompareException(comparisonStorage, scope, classObjects[i])) { return true; } } return false; } [Emitted] //RescueClause: public static bool CompareDefaultException(BinaryOpStorage/*!*/ comparisonStorage, RubyScope/*!*/ scope) { // a bare rescue is "rescue StandardError": CRuby calls StandardError.=== too return CompareException(comparisonStorage, scope, scope.RubyContext.StandardErrorClass); } private static void TraceRescue(RubyScope/*!*/ scope, Exception exception) { if ((TracePoint.ActiveEvents & (int)TraceEvents.Rescue) != 0 && exception != null) { TracePoint.OnException(TraceEvents.Rescue, scope, scope.RubyContext, exception); } } [Emitted] public static string/*!*/ GetDefaultExceptionMessage(RubyClass/*!*/ exceptionClass) { return exceptionClass.Name; } [Emitted] public static ArgumentException/*!*/ CreateArgumentsError(string message) { return (ArgumentException)RubyExceptions.CreateArgumentError(message); } [Emitted] public static ArgumentException/*!*/ CreateArgumentsErrorForMissingBlock() { return (ArgumentException)RubyExceptions.CreateArgumentError("tried to create Proc object without a block"); } [Emitted] public static ArgumentException/*!*/ CreateArgumentsErrorForProc(string className) { return (ArgumentException)RubyExceptions.CreateArgumentError(String.Format("wrong type argument {0} (should be callable)", className)); } [Emitted] public static ArgumentException/*!*/ MakeWrongNumberOfArgumentsError(int actual, int expected) { // MRI wording since 1.9: "wrong number of arguments (given 1, expected 0)". return new ArgumentException(String.Format("wrong number of arguments (given {0}, expected {1})", actual, expected)); } /// /// Same, but with MRI's variable-arity spelling of the expected count: "1..3", "1+" or "2, 3, or 5". /// The description is a compile-time constant produced by the binder. /// [Emitted] public static ArgumentException/*!*/ MakeWrongNumberOfArgumentsErrorN(int actual, string/*!*/ expected) { return new ArgumentException(String.Format("wrong number of arguments (given {0}, expected {1})", actual, expected)); } [Emitted] //SuperCall public static Exception/*!*/ MakeTopLevelSuperException() { return new MissingMethodException("super called outside of method"); } [Emitted] //SuperCallAction public static Exception/*!*/ MakeMissingSuperException(RubyContext/*!*/ context, object self, string/*!*/ name) { return RubyExceptions.CreateNoSuperclassMethod(context, self, name); } [Emitted] public static Exception/*!*/ MakeVirtualClassInstantiatedError() { return RubyExceptions.CreateTypeError("can't create instance of virtual class"); } [Emitted] public static Exception/*!*/ MakeUninitializedClassInstantiatedError() { return RubyExceptions.CreateTypeError("can't instantiate uninitialized class"); } [Emitted] public static Exception/*!*/ MakeTypeConversionError(RubyContext/*!*/ context, object value, Type/*!*/ type) { return RubyExceptions.CreateTypeConversionError(context.GetClassDisplayName(value), context.GetTypeName(type, true)); } [Emitted] public static Exception/*!*/ MakeAmbiguousMatchError(string/*!*/ message) { // TODO: return new AmbiguousMatchException(message); } [Emitted] public static Exception/*!*/ MakeAllocatorUndefinedError(RubyClass/*!*/ classObj) { return RubyExceptions.CreateAllocatorUndefinedError(classObj); } [Emitted] public static Exception/*!*/ MakeNotClrTypeError(RubyClass/*!*/ classObj) { return RubyExceptions.CreateNotClrTypeError(classObj); } [Emitted] public static Exception/*!*/ MakeConstructorUndefinedError(RubyClass/*!*/ classObj) { return RubyExceptions.CreateTypeError(String.Format("'{0}' doesn't have a visible CLR constructor", classObj.Context.GetTypeName(classObj.TypeTracker.Type, true) )); } [Emitted] public static Exception/*!*/ MakeMissingDefaultConstructorError(RubyClass/*!*/ classObj, string/*!*/ initializerOwnerName) { return RubyExceptions.CreateMissingDefaultConstructorError(classObj, initializerOwnerName); } [Emitted] public static Exception/*!*/ MakePrivateMethodCalledError(RubyContext/*!*/ context, object target, string/*!*/ methodName) { return RubyExceptions.CreatePrivateMethodCalled(context, target, methodName); } [Emitted] public static Exception/*!*/ MakeProtectedMethodCalledError(RubyContext/*!*/ context, object target, string/*!*/ methodName) { return RubyExceptions.CreateProtectedMethodCalled(context, target, methodName); } [Emitted] public static Exception/*!*/ MakeClrProtectedMethodCalledError(RubyContext/*!*/ context, object target, string/*!*/ methodName) { return new MissingMethodException( RubyExceptions.FormatMethodMissingMessage(context, target, methodName, "CLR protected method '{0}' called for {1}; " + "CLR protected methods can only be called with a receiver whose class is a Ruby subclass of the class declaring the method") ); } [Emitted] public static Exception/*!*/ MakeClrVirtualMethodCalledError(RubyContext/*!*/ context, object target, string/*!*/ methodName) { return new MissingMethodException( RubyExceptions.FormatMethodMissingMessage(context, target, methodName, "Virtual CLR method '{0}' called via super from {1}; " + "Super calls to virtual CLR methods can only be used in a Ruby subclass of the class declaring the method") ); } [Emitted] public static Exception/*!*/ MakeImplicitSuperInBlockMethodError() { return RubyExceptions.CreateRuntimeError("implicit argument passing of super from method defined by define_method() is not supported. Specify all arguments explicitly."); } [Emitted] public static Exception/*!*/ MakeMissingMethodError(RubyContext/*!*/ context, object self, string/*!*/ methodName) { // the rule throws it at once (RubyCallAction.BuildMethodMissingAccess, SetMissingMethodArguments in between) return RubyExceptionData.MarkUnkept(RubyExceptions.CreateMethodMissing(context, self, methodName)); } // MRI's method_missing_reason: whether the call that is about to reach a user-defined // method_missing had an implicit receiver, and whether it was a bare name that could have // been a local variable, read back by BasicObject#method_missing when that definition // calls super, so that NoMethodError#private_call? can answer and a bare name raises // NameError ("undefined local variable or method"). [ThreadStatic] private static bool _methodMissingPrivateCall; [ThreadStatic] private static bool _methodMissingVariableCall; public static bool MethodMissingPrivateCall { get { return _methodMissingPrivateCall; } set { _methodMissingPrivateCall = value; } } public static bool MethodMissingVariableCall { get { return _methodMissingVariableCall; } set { _methodMissingVariableCall = value; } } [Emitted] public static void SetMethodMissingReason(bool privateCall, bool variableCall) { _methodMissingPrivateCall = privateCall; _methodMissingVariableCall = variableCall; } [Emitted] public static Exception/*!*/ SetMissingMethodArguments(Exception/*!*/ error, object[]/*!*/ args, IList splat, object rhs, bool hasRhs, bool privateCall) { var list = new RubyArray(args); if (splat != null) { list.AddRange(splat); } if (hasRhs) { list.Add(rhs); } var data = RubyExceptionData.GetInstance(error); data.Arguments = list; data.PrivateCall = privateCall; return error; } /// /// The frame of the method an ArgumentError for the wrong number of arguments was raised for: /// the backtrace starts with it, as MRI's does (see MetaObjectBuilder.AddCalleeFrame). No file /// for a library method, whose frame is at the call. /// [Emitted] public static Exception/*!*/ SetCalleeFrame(Exception/*!*/ error, string file, int line, string/*!*/ label) { RubyExceptionData.GetInstance(error).SetCalleeFrame(file, line, label); return error; } [Emitted] public static Exception/*!*/ MakeUndefinedLocalOrMethodError(RubyContext/*!*/ context, RubyScope/*!*/ scope, object self, string/*!*/ methodName) { var error = RubyExceptions.CreateUndefinedLocalOrMethod(context, self, methodName); // NameError#local_variables: the locals the name might have been meant as (did_you_mean) RubyExceptionData.GetInstance(error).LocalVariables = scope.GetLocalVariableSymbols(); // the rule throws it at once (RubyCallAction.BuildMethodMissingAccess) return RubyExceptionData.MarkUnkept(error); } [Emitted] public static Exception/*!*/ MakeMissingMemberError(string/*!*/ memberName) { return new MissingMemberException(String.Format(CultureInfo.InvariantCulture, "undefined member: '{0}'", memberName)); } #endregion #region Ranges // A Range literal takes its context from the comparison storage, which the literal's own code made for its runtime: it // needs no scope, so a method whose body makes one, `(@first..(@last - 1)).each { }', can be a light frame. [Emitted] public static Range/*!*/ CreateInclusiveRange(object begin, object end, BinaryOpStorage/*!*/ comparisonStorage) { return new Range(comparisonStorage, comparisonStorage.Context, begin, end, false); } [Emitted] public static Range/*!*/ CreateExclusiveRange(object begin, object end, BinaryOpStorage/*!*/ comparisonStorage) { return new Range(comparisonStorage, comparisonStorage.Context, begin, end, true); } #endregion #region Dynamic Operations // allocator for struct instances: [Emitted] public static RubyStruct/*!*/ AllocateStructInstance(RubyClass/*!*/ self) { return RubyStruct.Create(self); } // factory for struct instances: [Emitted] public static RubyStruct/*!*/ CreateStructInstance(RubyClass/*!*/ self, [NotNull]params object[]/*!*/ items) { var result = RubyStruct.Create(self); result.SetValues(items); return result; } [Emitted] public static DynamicMetaObject/*!*/ GetMetaObject(IRubyObject/*!*/ obj, MSA.Expression/*!*/ parameter) { return new RubyObject.Meta(parameter, BindingRestrictions.Empty, obj); } [Emitted] public static RubyMethod/*!*/ CreateBoundMember(object target, RubyMemberInfo/*!*/ info, string/*!*/ name) { return new RubyMethod(target, info, name); } [Emitted] public static RubyMethod/*!*/ CreateBoundMissingMember(object target, RubyMemberInfo/*!*/ info, string/*!*/ name) { return RubyMethod.CreateMethodMissing(target, info, name); } [Emitted] public static bool IsClrSingletonRuleValid(RubyContext/*!*/ context, object/*!*/ target, int expectedVersion) { RubyInstanceData data; RubyClass immediate; // TODO: optimize this (we can have a hashtable of singletons per class: Weak(object) => Struct { ImmediateClass, InstanceVariables, Flags }): return context.TryGetClrTypeInstanceData(target, out data) && (immediate = data.ImmediateClass) != null && immediate.IsSingletonClass && immediate.Version.Method == expectedVersion; } [Emitted] public static bool IsClrNonSingletonRuleValid(RubyContext/*!*/ context, object/*!*/ target, VersionHandle/*!*/ versionHandle, int expectedVersion) { RubyInstanceData data; RubyClass immediate; return versionHandle.Method == expectedVersion // TODO: optimize this (we can have a hashtable of singletons per class: Weak(object) => Struct { ImmediateClass, InstanceVariables, Flags }): && !(context.TryGetClrTypeInstanceData(target, out data) && (immediate = data.ImmediateClass) != null && (immediate.IsSingletonClass || immediate.IsRubyClass)); } // :c_call / :c_return around a library method call; only in rules bound while TracePoint.CCallTracing [Emitted] public static void TraceLibraryCall(RubyScope scope, object self, RubyMemberInfo/*!*/ method, string/*!*/ name) { TracePoint.OnLibraryCall(TraceEvents.CCall, scope, self, method, name, null); } [Emitted] public static void TraceLibraryReturn(RubyScope scope, object self, RubyMemberInfo/*!*/ method, string/*!*/ name, object value) { TracePoint.OnLibraryCall(TraceEvents.CReturn, scope, self, method, name, value); } /// /// A rule for an object of a sealed CLR type whose class is a Ruby subclass of that type (see /// RubyContext.AdoptClrObject) holds for objects with exactly that immediate class. /// [Emitted] public static bool IsAdoptedClrRuleValid(RubyContext/*!*/ context, object/*!*/ target, RubyClass/*!*/ expectedClass, int expectedVersion) { RubyInstanceData data; return context.TryGetClrTypeInstanceData(target, out data) && ReferenceEquals(data.ImmediateClass, expectedClass) && expectedClass.Version.Method == expectedVersion; } // super call condition [Emitted] public static object GetSuperCallTarget(RubyScope/*!*/ scope, int targetId) { object currentSelf = scope.SelfObject; while (true) { switch (scope.Kind) { case ScopeKind.Method: if (targetId != 0) { return NeedsUpdate; } RequireSuperSelf(scope, currentSelf, ((RubyMethodScope)scope).DeclaringModule); return scope.SelfObject; case ScopeKind.BlockMethod: var method = ((RubyBlockScope)scope).BlockFlowControl.Proc.Method; if (targetId != method.Id) { return NeedsUpdate; } RequireSuperSelf(scope, currentSelf, method.DeclaringModule); return scope.SelfObject; case ScopeKind.TopLevel: // This method is only called if there was method or block-method scope in lexical scope chain. // Once there is it cannot be undone. It can only be shadowed by a block scope that became block-method scope, or // a block-method scope's target-id can be changed. throw Assert.Unreachable; } scope = scope.Parent; } } /// /// A `super' in a block that runs with another self - module_eval, instance_eval or instance_exec on an /// object that is not an instance of the method's class or module - is a TypeError, as MRI's /// vm_search_super_method makes it (#136); it called the method's super with the method's self. /// private static void RequireSuperSelf(RubyScope/*!*/ methodScope, object currentSelf, RubyModule declaringModule) { object methodSelf = methodScope.SelfObject; // identity, not Equals: a Ruby object's Equals is its #eql?, which a program may define (and would // then run on every super); only a boxed Integer, Float, true... compares by value if (ReferenceEquals(currentSelf, methodSelf) || (currentSelf != null && currentSelf.GetType().IsValueType && currentSelf.Equals(methodSelf)) || declaringModule == null || declaringModule.RefinedModule != null) { return; } var context = methodScope.RubyContext; if (!context.IsKindOf(currentSelf, declaringModule)) { throw RubyExceptions.CreateTypeError(String.Format("self has wrong type to call super in this context: {0} (expected {1})", context.GetClassDisplayName(currentSelf), declaringModule.GetDisplayName(context, true))); } } // super call condition [Emitted] public static bool IsSuperOutOfMethodScope(RubyScope/*!*/ scope) { while (true) { switch (scope.Kind) { case ScopeKind.Method: case ScopeKind.BlockMethod: return false; // a class or module body - inside `eval' in a method, too - is no place for super (#136) case ScopeKind.Module: case ScopeKind.TopLevel: return true; } scope = scope.Parent; } } #endregion #region Conversions [Emitted] // ProtocolConversionAction public static Proc/*!*/ ToProcValidator(string/*!*/ className, object obj) { Proc result = obj as Proc; if (result == null) { throw RubyExceptions.CreateReturnTypeError(className, "to_proc", "Proc", obj); } return result; } // Used for implicit conversions from System.String to MutableString (to_str conversion like). [Emitted] public static MutableString/*!*/ StringToMutableString(string/*!*/ str) { return MutableString.Create(str, RubyEncoding.UTF8); } // Used for implicit conversions from System.Object to MutableString (to_s conversion like). [Emitted] public static MutableString/*!*/ ObjectToMutableString(object/*!*/ value) { return (value != null) ? MutableString.Create(value.ToString(), RubyEncoding.UTF8) : MutableString.FrozenEmpty; } /// MRI's rb_check_string_type: a #to_str answering nil is "not a String after all". [Emitted] // ProtocolConversionAction public static MutableString TryToStringValidator(string/*!*/ className, object obj) { return (obj == null) ? null : ToStringValidator(className, obj); } [Emitted] // ProtocolConversionAction public static MutableString/*!*/ ToStringValidator(string/*!*/ className, object obj) { MutableString result = obj as MutableString; if (result == null) { throw RubyExceptions.CreateReturnTypeError(className, "to_str", "String", obj); } return result; } [Emitted] // ProtocolConversionAction public static string/*!*/ ToSymbolValidator(string/*!*/ className, object obj) { var str = obj as MutableString; if (str == null) { throw RubyExceptions.CreateReturnTypeError(className, "to_str", "String", obj); } return str.ConvertToString(); } [Emitted] // ProtocolConversionAction public static string/*!*/ ConvertSymbolToClrString(RubySymbol/*!*/ value) { return value.ToString(); } [Emitted] // ProtocolConversionAction public static string/*!*/ ConvertMutableStringToClrString(MutableString/*!*/ value) { return value.ConvertToString(); } [Emitted] // ProtocolConversionAction public static RubyRegex/*!*/ ToRegexValidator(string/*!*/ className, object obj) { return RubyRegex.FromLiteralPattern(ToStringValidator(className, obj)); } /// /// MRI's rb_check_array_type: a #to_ary that answers nil means "not an Array after all", /// which is not an error - the caller keeps the object as it is. Anything else that is not /// an Array is a broken promise and still raises. /// [Emitted] // ProtocolConversionAction public static IList TryToArrayValidator(string/*!*/ className, object obj) { return (obj == null) ? null : ToArrayValidator(className, obj); } [Emitted] // ProtocolConversionAction public static IList TryToAValidator(string/*!*/ className, object obj) { return (obj == null) ? null : ToAValidator(className, obj); } [Emitted] // ProtocolConversionAction public static IDictionary TryToHashValidator(string/*!*/ className, object obj) { return (obj == null) ? null : ToHashValidator(className, obj); } [Emitted] // ImplicitTrySplatAction public static object TrySplatToAryValidator(string/*!*/ className, object splattee, object obj) { return (obj == null) ? splattee : ToArrayValidator(className, obj); } [Emitted] // ProtocolConversionAction public static IList/*!*/ ToArrayValidator(string/*!*/ className, object obj) { var result = obj as IList; if (result == null) { throw RubyExceptions.CreateReturnTypeError(className, "to_ary", "Array", obj); } return result; } [Emitted] // ProtocolConversionAction public static IList/*!*/ ToAValidator(string/*!*/ className, object obj) { var result = obj as IList; if (result == null) { throw RubyExceptions.CreateReturnTypeError(className, "to_a", "Array", obj); } return result; } /// /// Validates the result of #to_a called on a splatted argument. MRI's rb_check_array_type: /// a #to_a that answers nil means "not an Array after all" and the splattee itself becomes /// the single element; any other non-Array result is a broken promise and raises. /// [Emitted] // ProtocolConversionAction public static IList/*!*/ SplatToAValidator(string/*!*/ className, object splattee, object obj) { if (obj == null) { var wrapped = new RubyArray(1); wrapped.Add(splattee); return wrapped; } var result = obj as IList; if (result == null) { throw RubyExceptions.CreateReturnTypeError(className, "to_a", "Array", obj); } return result; } /// Same for #to_ary (multiple assignment). [Emitted] // ProtocolConversionAction public static IList/*!*/ SplatToAryValidator(string/*!*/ className, object splattee, object obj) { if (obj == null) { var wrapped = new RubyArray(1); wrapped.Add(splattee); return wrapped; } var result = obj as IList; if (result == null) { throw RubyExceptions.CreateReturnTypeError(className, "to_ary", "Array", obj); } return result; } [Emitted] // ProtocolConversionAction public static IDictionary/*!*/ ToHashValidator(string/*!*/ className, object obj) { var result = obj as IDictionary; if (result == null) { throw RubyExceptions.CreateReturnTypeError(className, "to_hash", "Hash", obj); } return result; } private static int ToIntValidator(string/*!*/ className, string/*!*/ targetType, object obj) { if (obj is int) { return (int)obj; } if (obj is long wide) { if (wide >= Int32.MinValue && wide <= Int32.MaxValue) { return (int)wide; } throw RubyExceptions.CreateRangeError("bignum too big to convert into {0}", targetType); } if (obj is BigInteger bignum) { int fixnum; if (bignum.AsInt32(out fixnum)) { return fixnum; } throw RubyExceptions.CreateRangeError("bignum too big to convert into {0}", targetType); } throw RubyExceptions.CreateReturnTypeError(className, "to_int", "Integer", obj); } [Emitted] // ProtocolConversionAction public static int ToFixnumValidator(string/*!*/ className, object obj) { return ToIntValidator(className, "Integer", obj); } [Emitted] // ProtocolConversionAction public static Byte ToByteValidator(string/*!*/ className, object obj) { return Converter.ToByte(ToIntValidator(className, "System::Byte", obj)); } [Emitted] // ProtocolConversionAction public static SByte ToSByteValidator(string/*!*/ className, object obj) { return Converter.ToSByte(ToIntValidator(className, "System::SByte", obj)); } [Emitted] // ProtocolConversionAction public static Int16 ToInt16Validator(string/*!*/ className, object obj) { return Converter.ToInt16(ToIntValidator(className, "System::Int16", obj)); } [Emitted] // ProtocolConversionAction public static UInt16 ToUInt16Validator(string/*!*/ className, object obj) { return Converter.ToUInt16(ToIntValidator(className, "System::UInt16", obj)); } [Emitted] // ProtocolConversionAction public static UInt32 ToUInt32Validator(string/*!*/ className, object obj) { if (obj is int) { return Converter.ToUInt32((int)obj); } if (obj is long wide) { return Converter.ToUInt32((BigInteger)wide); } if (obj is BigInteger bignum) { return Converter.ToUInt32(bignum); } throw RubyExceptions.CreateReturnTypeError(className, "to_int", "Integer", obj); } [Emitted] // ProtocolConversionAction public static Int64 ToInt64Validator(string/*!*/ className, object obj) { if (obj is int) { return (int)obj; } if (obj is long wide) { return wide; } if (obj is BigInteger bignum) { return Converter.ToInt64(bignum); } throw RubyExceptions.CreateReturnTypeError(className, "to_int", "Integer", obj); } [Emitted] // ProtocolConversionAction public static UInt64 ToUInt64Validator(string/*!*/ className, object obj) { if (obj is int) { return Converter.ToUInt64((int)obj); } if (obj is long wide) { return Converter.ToUInt64((BigInteger)wide); } if (obj is BigInteger bignum) { return Converter.ToUInt64(bignum); } throw RubyExceptions.CreateReturnTypeError(className, "to_int", "Integer", obj); } [Emitted] // ProtocolConversionAction public static BigInteger ToBignumValidator(string/*!*/ className, object obj) { if (obj is int) { return (int)obj; } if (obj is long wide) { return wide; } if (obj is BigInteger bignum) { return bignum; } throw RubyExceptions.CreateReturnTypeError(className, "to_int", "Integer", obj); } [Emitted] // ProtocolConversionAction public static IntegerValue ToIntegerValidator(string/*!*/ className, object obj) { return ToIntegerValue(className, "to_int", obj); } [Emitted] // ProtocolConversionAction (Kernel#Integer's #to_i step) public static IntegerValue ToIValidator(string/*!*/ className, object obj) { return ToIntegerValue(className, "to_i", obj); } private static IntegerValue ToIntegerValue(string/*!*/ className, string/*!*/ methodName, object obj) { return obj switch { int fixnum => new IntegerValue(fixnum), long wide => new IntegerValue((BigInteger)wide), BigInteger bignum => new IntegerValue(bignum), _ => throw RubyExceptions.CreateReturnTypeError(className, methodName, "Integer", obj) }; } [Emitted] // ProtocolConversionAction public static double ToDoubleValidator(string/*!*/ className, object obj) { if (obj is double) { return (double)obj; } if (obj is float) { return (double)(float)obj; } throw RubyExceptions.CreateReturnTypeError(className, "to_f", "Float", obj); } [Emitted] // ProtocolConversionAction public static float ToSingleValidator(string/*!*/ className, object obj) { if (obj is double) { return (float)(double)obj; } if (obj is float) { return (float)obj; } throw RubyExceptions.CreateReturnTypeError(className, "to_f", "System::Single", obj); } [Emitted] public static double ConvertBignumToFloat(BigInteger/*!*/ value) { double result; return value.TryToFloat64(out result) ? result : (value.IsNegative() ? Double.NegativeInfinity : Double.PositiveInfinity); } [Emitted] public static double ConvertMutableStringToFloat(RubyContext/*!*/ context, MutableString/*!*/ value) { return ConvertStringToFloat(context, value.ConvertToString(), value); } [Emitted] public static double ConvertStringToFloat(RubyContext/*!*/ context, string/*!*/ value) { return ConvertStringToFloat(context, value, null); } // MRI's Kernel#Float is written in Ruby, so its warning about a number out of range names the line of // that converts it rather than the caller's: the one for Float(x), or the one for Float(x, exception: false). private const int KernelFloatLine = 196, KernelFloatNoExceptionLine = 198; /// Kernel#Float's conversion of a String. public static double ConvertKernelFloat(RubyContext/*!*/ context, MutableString/*!*/ value) { return ConvertKernelFloat(context, value, false); } /// /// Kernel#Float's conversion of a String; when the call passed exception:, which in /// MRI takes the other line of its kernel.rb even when it says true. /// public static double ConvertKernelFloat(RubyContext/*!*/ context, MutableString/*!*/ value, bool keywordCall) { return ConvertStringToFloat(context, value.ConvertToString(), value, keywordCall ? KernelFloatNoExceptionLine : KernelFloatLine); } /// Kernel#Float's conversion of a String with exception: false. public static bool TryConvertKernelFloat(RubyContext/*!*/ context, MutableString/*!*/ value, out double result) { string text = value.ConvertToString(); if (!TryParseRubyFloat(text, out result)) { return false; } if (MayWarnFloatOutOfRange(context, result)) { WarnIfFloatOutOfRange(context, text.Trim(_FloatWhitespace).Replace("_", ""), result, KernelFloatNoExceptionLine); } return true; } private static double ConvertStringToFloat(RubyContext/*!*/ context, string/*!*/ value, MutableString original, int kernelFloatLine = 0) { double result; if (TryParseRubyFloat(value, out result)) { if (MayWarnFloatOutOfRange(context, result)) { WarnIfFloatOutOfRange(context, value.Trim(_FloatWhitespace).Replace("_", ""), result, kernelFloatLine); } return result; } throw InvalidFloatValue(original != null ? original.ToByteArray() : Encoding.UTF8.GetBytes(value)); } private static readonly char[] _FloatWhitespace = { ' ', '\t', '\n', '\v', '\f', '\r' }; // whether WarnIfFloatOutOfRange can warn about this result: the text it quotes is made only then private static bool MayWarnFloatOutOfRange(RubyContext/*!*/ context, double result) { return (Double.IsInfinity(result) || result == 0) && context.Verbose is bool verbose && verbose; } /// /// MRI's rb_cstr_to_dbl warns (verbose only) when strtod over- or underflows, quoting at /// most 20 characters of the number it parsed, underscores taken out: /// "Float 1e400 out of range" /// public static void WarnIfFloatOutOfRange(RubyContext/*!*/ context, string/*!*/ number, double result) { WarnIfFloatOutOfRange(context, number, result, 0); } private static void WarnIfFloatOutOfRange(RubyContext/*!*/ context, string/*!*/ number, double result, int kernelFloatLine) { if (!(context.Verbose is bool verbose && verbose)) { return; } if (!Double.IsInfinity(result) && !(result == 0 && HasNonZeroMantissa(number))) { return; } if (number.Length > 20) { number = number.Substring(0, 20) + "..."; } if (kernelFloatLine != 0) { context.ReportWarningAt("", kernelFloatLine, "Float " + number + " out of range"); } else { context.ReportWarning("Float " + number + " out of range", true); } } private static bool HasNonZeroMantissa(string/*!*/ number) { int i = (number.Length > 0 && (number[0] == '+' || number[0] == '-')) ? 1 : 0; bool hex = i + 1 < number.Length && number[i] == '0' && (number[i + 1] == 'x' || number[i + 1] == 'X'); for (i += hex ? 2 : 0; i < number.Length; i++) { char c = number[i]; if (hex ? (c == 'p' || c == 'P') : (c == 'e' || c == 'E')) { break; } if ((c >= '1' && c <= '9') || (hex && ((c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')))) { return true; } } return false; } /// /// MRI quotes the string byte by byte, as a binary string's inspect would: every byte /// outside printable ASCII shows as \xNN, so a non-ASCII character is spelled out in its bytes. /// private static Exception/*!*/ InvalidFloatValue(byte[]/*!*/ bytes) { var text = new StringBuilder(bytes.Length + 8); foreach (byte b in bytes) { switch (b) { case (byte)'\n': text.Append("\\n"); break; case (byte)'\t': text.Append("\\t"); break; case (byte)'\r': text.Append("\\r"); break; case (byte)'\f': text.Append("\\f"); break; case (byte)'\v': text.Append("\\v"); break; case (byte)'\a': text.Append("\\a"); break; case (byte)'\b': text.Append("\\b"); break; case 27: text.Append("\\e"); break; case (byte)'"': text.Append("\\\""); break; case (byte)'\\': text.Append("\\\\"); break; default: if (b < 0x20 || b >= 0x7f) { text.Append("\\x").Append(b.ToString("X2", CultureInfo.InvariantCulture)); } else { text.Append((char)b); } break; } } // thrown at once by its caller, kept by nothing return RubyExceptionData.MarkUnkept(RubyExceptions.CreateArgumentError("invalid value for Float(): \"{0}\"", text.ToString())); } #region Kernel#Float string grammar // Kernel#Float does not accept what C's strtod does. Surrounding whitespace is allowed // but an embedded NUL is not; '_' may separate digits; a trailing '.' is rejected while a // leading one is not; hexadecimal literals are accepted; "inf", "Infinity" and "nan" are // not. The rules below were derived by differential testing against CRuby 3.3.8. private static bool IsFloatWhitespace(char c) { return c == ' ' || (c >= '\t' && c <= '\r'); } private static bool IsDecimalDigit(char c) { return c >= '0' && c <= '9'; } private static bool IsHexadecimalDigit(char c) { return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'); } /// /// Consumes a run of digits, appending them to at /// , which has room for every character up to . When /// is set an '_' may appear between two digits of the /// run, so "1_0" scans but "1__0", "_1" and "1_" do not. Returns the number of digits /// consumed, or -1 for a misplaced underscore. /// private static int ScanDigitRun(string/*!*/ str, int end, ref int index, Span digits, ref int length, bool hexadecimal, bool allowUnderscore) { int count = 0; while (index < end) { char c = str[index]; if (hexadecimal ? IsHexadecimalDigit(c) : IsDecimalDigit(c)) { digits[length++] = c; count++; index++; } else if (c == '_' && allowUnderscore && count > 0) { char next = (index + 1 < end) ? str[index + 1] : '\0'; if (!(hexadecimal ? IsHexadecimalDigit(next) : IsDecimalDigit(next))) { return -1; } index++; } else { break; } } return count; } /// Scans "[eEpP] [+-] digits"; returns false if no digits follow. private static bool ScanExponent(string/*!*/ str, int end, ref int index, out int exponent) { exponent = 0; bool negative = false; if (index < end && (str[index] == '+' || str[index] == '-')) { negative = (str[index] == '-'); index++; } // the digits, underscores and all, are within what is left of the string Span digits = (end - index) <= 64 ? stackalloc char[end - index] : new char[end - index]; int length = 0; if (ScanDigitRun(str, end, ref index, digits, ref length, false, true) <= 0) { return false; } long value = 0; for (int i = 0; i < length; i++) { value = value * 10 + (digits[i] - '0'); if (value > Int32.MaxValue) { // Far past the point where the result saturates to 0 or Infinity either way. value = 99999; break; } } exponent = negative ? -(int)value : (int)value; return true; } // 10**0 .. 10**22, every one of them exact in a double private static readonly double[]/*!*/ _ExactPowersOfTen = { 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, 1e20, 1e21, 1e22 }; public static bool TryParseRubyFloat(string/*!*/ str, out double result) { result = 0.0; int index = 0; int end = str.Length; while (index < end && IsFloatWhitespace(str[index])) { index++; } while (end > index && IsFloatWhitespace(str[end - 1])) { end--; } if (index == end) { return false; } bool negative = false; if (str[index] == '+' || str[index] == '-') { negative = (str[index] == '-'); index++; } bool parsed = (index + 1 < end && str[index] == '0' && (str[index + 1] == 'x' || str[index + 1] == 'X')) ? TryParseHexadecimalFloat(str, index + 2, end, out result) : TryParseDecimalFloat(str, index, end, out result); if (parsed && negative) { result = -result; } return parsed; } private static bool TryParseDecimalFloat(string/*!*/ str, int index, int end, out double result) { result = 0.0; // the digits, then "E" and the power for Double.TryParse: no more than the rest of the string and 8 int capacity = end - index + 8; Span digits = capacity <= 128 ? stackalloc char[capacity] : new char[capacity]; int length = 0; int integerDigits = ScanDigitRun(str, end, ref index, digits, ref length, false, true); if (integerDigits < 0) { return false; } int scale = 0; if (index < end && str[index] == '.') { index++; int fractionDigits = ScanDigitRun(str, end, ref index, digits, ref length, false, true); if (fractionDigits < 0) { return false; } if (fractionDigits == 0) { // Ruby 3.4 accepts a point with nothing after it, so Float("10.") is 10.0 and // Float("1.e5") is 100000.0, but there has to be something before it: "." and // ".5e1" are still errors. if (integerDigits == 0) { return false; } } scale = -fractionDigits; } else if (integerDigits == 0) { return false; } int exponent = 0; if (index < end && (str[index] == 'e' || str[index] == 'E')) { index++; if (!ScanExponent(str, end, ref index, out exponent)) { return false; } } if (index != end) { return false; } long power = (long)exponent + scale; if (power > 99999) { power = 99999; } else if (power < -99999) { power = -99999; } // The usual number has at most 15 significant digits and a small power of ten: then the // digits and the power are both exact in a double, and one multiplication or division // rounds the result correctly (Clinger's fast path), as strtod and Double.Parse would. int first = 0; while (first < length && digits[first] == '0') { first++; } if (length - first <= 15 && power >= -22 && power <= 22) { long mantissa = 0; for (int i = first; i < length; i++) { mantissa = mantissa * 10 + (digits[i] - '0'); } result = power >= 0 ? (double)mantissa * _ExactPowersOfTen[power] : (double)mantissa / _ExactPowersOfTen[-power]; return true; } // Double.Parse saturates to Infinity or zero rather than failing, which is what Ruby // does with "1e400" and "1e-400". digits[length++] = 'E'; power.TryFormat(digits.Slice(length), out int written, default, CultureInfo.InvariantCulture); length += written; return Double.TryParse(digits.Slice(0, length), NumberStyles.Float, CultureInfo.InvariantCulture, out result); } private static bool TryParseHexadecimalFloat(string/*!*/ str, int index, int end, out double result) { result = 0.0; // '_' may separate two hexadecimal digits, exactly as in the decimal form. Span digits = (end - index) <= 128 ? stackalloc char[end - index] : new char[end - index]; int length = 0; int integerDigits = ScanDigitRun(str, end, ref index, digits, ref length, true, true); if (integerDigits < 0) { return false; } int scale = 0; if (index < end && str[index] == '.') { index++; int fractionDigits = ScanDigitRun(str, end, ref index, digits, ref length, true, true); if (fractionDigits <= 0) { return false; } scale = -4 * fractionDigits; } else if (integerDigits == 0) { return false; } int exponent = 0; if (index < end && (str[index] == 'p' || str[index] == 'P')) { index++; if (!ScanExponent(str, end, ref index, out exponent)) { return false; } } if (index != end) { return false; } BigInteger mantissa = BigInteger.Zero; for (int i = 0; i < length; i++) { char c = digits[i]; int digit = (c <= '9') ? (c - '0') : ((c | 0x20) - 'a' + 10); mantissa = mantissa * 16 + digit; } long power = (long)exponent + scale; if (power > 99999) { power = 99999; } else if (power < -99999) { power = -99999; } result = Math.ScaleB((double)mantissa, (int)power); return true; } #endregion [Emitted] // ProtocolConversionAction public static Exception/*!*/ CreateTypeConversionError(string/*!*/ fromType, string/*!*/ toType) { return RubyExceptions.CreateTypeConversionError(fromType, toType); } [Emitted] // ProtocolConversionAction public static Exception/*!*/ CreateImplicitConversionError(string/*!*/ fromType, string/*!*/ toType) { return RubyExceptions.CreateImplicitConversionError(fromType, toType); } [Emitted] // ConvertToRegexAction public static Exception/*!*/ CreateWrongArgumentTypeError(string/*!*/ fromType, string/*!*/ toType) { return RubyExceptions.CreateTypeError("wrong argument type {0} (expected {1})", RubyExceptions.MessageTypeName(fromType), toType); } /// /// A method name that is neither a Symbol nor a String gets its own wording in MRI, naming /// the value rather than its class: "[] is not a symbol nor a string". Kernel#send, /// #public_send, #respond_to? and Module#instance_method all report it that way. /// [Emitted] public static Exception/*!*/ CreateNotSymbolNorStringError(RubyContext/*!*/ context, object obj) { return RubyExceptions.CreateTypeError("{0} is not a symbol nor a string", context.Inspect(obj).ToAsciiString()); } /// /// The same message where only the argument's class is known -- the overload resolver /// rejects nil and friends without holding on to the value. /// [Emitted] public static Exception/*!*/ CreateNotSymbolNorStringErrorFor(string/*!*/ description) { return RubyExceptions.CreateTypeError("{0} is not a symbol nor a string", description); } [Emitted] // ConvertToFixnumAction public static int ConvertBignumToFixnum(BigInteger/*!*/ bignum) { int fixnum; if (bignum.AsInt32(out fixnum)) { return fixnum; } throw RubyExceptions.CreateRangeError("bignum too big to convert into 'long'"); } [Emitted] // Converter.ExplicitConvert public static int ConvertInt64ToFixnum(long value) { if (value >= Int32.MinValue && value <= Int32.MaxValue) { return (int)value; } // Same wording as the BigInteger case: which of the two CLR types happens to carry // the value is an implementation detail Ruby code must not be able to see. throw RubyExceptions.CreateRangeError("bignum too big to convert into 'long'"); } [Emitted] // ConvertDoubleToFixnum public static int ConvertDoubleToFixnum(double value) { try { return checked((int)value); } catch (OverflowException) { throw RubyExceptions.CreateRangeError(String.Format("float {0} out of range of integer", value)); } } [Emitted] // ConvertToSAction public static MutableString/*!*/ ToSDefaultConversion(RubyContext/*!*/ context, object target, object converted) { return converted as MutableString ?? RubyUtils.ObjectToMutableString(context, target); } #endregion #region Instance variable support // `@x`: a Ruby object whose shape the site has seen is a reference compare and an array // load. See RubyInstanceData for the layout and why no lock is needed. // // The cached case is inlined into the compiled method body (the JIT inlines it into the // dynamic method): a type check, three loads, the acquire read of element 0, a compare and // the slot load. Everything else - a cache miss, a module, a non-RubyObject - is a call to // the out-of-line rest. It is RubyInstanceData's read protocol unchanged: one read of // _ivars, an acquire read of its element 0, and a slot read the shape there vouches for. [Emitted] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static object GetInstanceVariable(RubyScope/*!*/ scope, object self, InstanceVariableSite/*!*/ site) { object value; if (self is RubyObject obj && TryReadCached(obj._instanceData, site, out value)) { return value; } return GetInstanceVariableUncached(scope, self, site); } // The cached read of a variable of an object's instance data (null for no variables yet). [MethodImpl(MethodImplOptions.AggressiveInlining)] internal static bool TryReadCached(RubyInstanceData data, InstanceVariableSite/*!*/ site, out object value) { if (data != null) { object[] slots = data._ivars; var entry = site.Cache; // a raw shape is never PlainShape (RubyInstanceData, "Float locations"): this inlined read // is the compare and the load it always was; a raw object's read is the first thing // the out-of-line readers try (ReadInstanceVariable and the rest) if (RubyInstanceData.ShapeOf(slots) == entry.PlainShape) { value = RubyInstanceData.GetSlot(slots, entry.Index); return true; } } value = null; return false; } // The cached read of a variable of an object whose shape is raw (RubyInstanceData, "Float // locations"), for a caller that TryReadCached has just turned away: the same protocol, and // a Float comes boxed (the same box again while the value is unchanged). [MethodImpl(MethodImplOptions.AggressiveInlining)] internal static bool TryReadCachedRaw(RubyInstanceData data, InstanceVariableSite/*!*/ site, out object value) { if (data != null) { object[] slots = data._ivars; var entry = site.Cache; if (RubyInstanceData.ShapeOf(slots) == entry.Shape) { value = RubyInstanceData.ValueOf(RubyInstanceData.GetSlot(slots, entry.Index)); return true; } } value = null; return false; } // The site's second shape, for the typed code, whose sites see a class's objects in two shapes // once some have been promoted to raw Floats (RubyInstanceData, "Float locations"). // A raw second shape becomes the first, so that the inlined path reads it from then on; a plain // one stays second, so that a site reading both kinds settles instead of swapping on every read. [MethodImpl(MethodImplOptions.NoInlining)] private static bool TryReadCachedFloatSlow(object[]/*!*/ slots, InstanceVariableSite/*!*/ site, InstanceVariableSite.Entry/*!*/ entry, out double value) { object shape = RubyInstanceData.ShapeOf(slots); if (shape == entry.Shape) { // a plain object: a boxed Float or nothing for us object slot = RubyInstanceData.GetSlot(slots, entry.Index); if (slot is double) { value = (double)slot; return true; } } else if (shape == entry.OtherShape) { object slot = RubyInstanceData.GetSlot(slots, entry.OtherIndex); if (entry.OtherRaw) { site.Update(entry.Remember(entry.OtherShape, entry.OtherIndex)); return RubyInstanceData.TryReadFloat(slot, out value); } if (slot is double) { value = (double)slot; return true; } } value = 0; return false; } // TryReadCached of a variable that holds a Float, for the typed code: the double, without // boxing a raw one. False for a miss or a variable that holds anything else. [MethodImpl(MethodImplOptions.AggressiveInlining)] internal static bool TryReadCachedFloat(RubyInstanceData data, InstanceVariableSite/*!*/ site, out double value) { if (data != null) { object[] slots = data._ivars; var entry = site.Cache; if (RubyInstanceData.ShapeOf(slots) == entry.Shape) { object slot = RubyInstanceData.GetSlot(slots, entry.Index); if (entry.Raw) { return RubyInstanceData.TryReadFloat(slot, out value); } // a plain object's slot never holds a FloatLocation: the type test GuardDouble made value = (slot is double) ? (double)slot : 0; return slot is double; } // kept small, for it is inlined into every typed read: the rest out of line return TryReadCachedFloatSlow(slots, site, entry, out value); } value = 0; return false; } // TryWriteCached of a Float the typed code holds raw: into the variable's FloatLocation // (RubyInstanceData.TryWriteFloat). False for a miss, an add, a frozen object or a shape // changed under it: the caller boxes the Float and takes the ordinary path. [MethodImpl(MethodImplOptions.AggressiveInlining)] internal static bool TryWriteCachedFloat(RubyInstanceData data, double value, InstanceVariableSite/*!*/ site, RubyClass ownerClass) { if (data != null && !data._frozen) { object[] slots = data._ivars; var entry = site.Cache; object shape = RubyInstanceData.ShapeOf(slots); if (shape == entry.Shape && entry.Raw && entry.Next == null) { return RubyInstanceData.TryWriteFloat(slots, shape, entry.Index, value); } // kept small, for it is inlined into every typed store: the rest out of line return TryWriteCachedFloatSlow(data, slots, shape, value, site, ownerClass); } return false; } [MethodImpl(MethodImplOptions.NoInlining)] private static bool TryWriteCachedFloatSlow(RubyInstanceData/*!*/ data, object[]/*!*/ slots, object shape, double value, InstanceVariableSite/*!*/ site, RubyClass ownerClass) { var entry = site.Cache; { if (shape == entry.Shape && entry.Next == null) { return TryWriteCachedFloatPlain(data, slots, entry.Shape, entry.Index, value, site, ownerClass); } if (shape == entry.Shape) { // adding the variable (a constructor's `@x = 1.5'): boxed, by the cached add return data.TrySetCached(entry, value, ownerClass); } if (shape == entry.OtherShape) { // the second shape: promoted and plain objects of one class side by side if (entry.OtherRaw) { // raw objects now come first at this site (see TryReadCachedFloatOther) site.Update(entry.Remember(entry.OtherShape, entry.OtherIndex)); return RubyInstanceData.TryWriteFloat(slots, shape, entry.OtherIndex, value); } return TryWriteCachedFloatPlain(data, slots, entry.OtherShape, entry.OtherIndex, value, site, ownerClass); } } return false; } // Not raw yet: a boxed store, or the promotion (RubyInstanceData.TryWriteFloatPlain), after // which the site knows the raw shape first and the plain one second. [MethodImpl(MethodImplOptions.NoInlining)] private static bool TryWriteCachedFloatPlain(RubyInstanceData/*!*/ data, object[]/*!*/ slots, InstanceVariableShape/*!*/ shape, int index, double value, InstanceVariableSite/*!*/ site, RubyClass ownerClass) { var now = data.TryWriteFloatPlain(slots, shape, index, value, ownerClass); if (now != null && now != shape) { site.Update(site.Cache.Remember(now, index)); } return now != null; } [MethodImpl(MethodImplOptions.NoInlining)] private static object GetInstanceVariableUncached(RubyScope/*!*/ scope, object self, InstanceVariableSite/*!*/ site) { RubyInstanceData data; var obj = self as RubyObject; if (obj != null) { data = obj._instanceData; } else { // class-level @x var module = self as RubyModule; if (module == null) { return GetInstanceVariableSlow(scope.RubyContext, self, site); } if (module.Context.RactorsStarted && !site.IsCoreLibrary && RactorIsolation.IsNonMain(module.Context)) { return GetModuleInstanceVariableChecked(module, site); } data = module._instanceData; } if (data == null) { return null; } object[] slots = data._ivars; var entry = site.Cache; object shape = RubyInstanceData.ShapeOf(slots); if (shape == entry.PlainShape) { return RubyInstanceData.GetSlot(slots, entry.Index); } if (shape == entry.Shape) { return RubyInstanceData.ValueOf(RubyInstanceData.GetSlot(slots, entry.Index)); } return data.GetInstanceVariable(site); } public static object ReadInstanceVariable(RubyContext/*!*/ context, object self, InstanceVariableSite/*!*/ site) { RubyInstanceData data; var obj = self as RubyObject; if (obj != null) { data = obj._instanceData; } else { // class-level @x var module = self as RubyModule; if (module == null) { return GetInstanceVariableSlow(context, self, site); } if (module.Context.RactorsStarted && !site.IsCoreLibrary && RactorIsolation.IsNonMain(module.Context)) { return GetModuleInstanceVariableChecked(module, site); } data = module._instanceData; } if (data == null) { return null; } object[] slots = data._ivars; var entry = site.Cache; object shape = RubyInstanceData.ShapeOf(slots); if (shape == entry.PlainShape) { return RubyInstanceData.GetSlot(slots, entry.Index); } if (shape == entry.Shape) { return RubyInstanceData.ValueOf(RubyInstanceData.GetSlot(slots, entry.Index)); } return data.GetInstanceVariable(site); } /// /// A class-level `@x` once the runtime has started Ractors: a non-main Ractor may only /// read a shareable value (RactorIsolation). /// private static object GetModuleInstanceVariableChecked(RubyModule/*!*/ module, InstanceVariableSite/*!*/ site) { RubyInstanceData data = module._instanceData; object value = (data != null) ? data.GetInstanceVariable(site) : null; return RactorIsolation.CheckInstanceVariableRead(module.Context, module, site.Name, value); } private static object GetInstanceVariableSlow(RubyContext/*!*/ context, object self, InstanceVariableSite/*!*/ site) { if (context.RactorsStarted && !site.IsCoreLibrary) { RactorIsolation.CheckInstanceVariableRead(context, self, site.Name, null); } var owner = self as IRubyInstanceDataOwner; RubyInstanceData data; if (owner == null || !owner.TryGetInstanceData(context, out data)) { data = context.TryGetInstanceData(self); } if (data == null) { return null; } object[] slots = data._ivars; var entry = site.Cache; object shape = RubyInstanceData.ShapeOf(slots); if (shape == entry.PlainShape) { return RubyInstanceData.GetSlot(slots, entry.Index); } if (shape == entry.Shape) { return RubyInstanceData.ValueOf(RubyInstanceData.GetSlot(slots, entry.Index)); } return data.GetInstanceVariable(site); } [Emitted] public static bool IsDefinedInstanceVariable(RubyScope/*!*/ scope, object self, string/*!*/ name) { RubyInstanceData data = scope.RubyContext.TryGetInstanceData(self); if (data == null) return false; object value; return data.TryGetInstanceVariable(name, out value); } // `@x = v`: a Ruby object whose shape the site has seen stores without a lock; adding a // variable the site has seen added is a CAS. See RubyInstanceData. // // The store into a variable the object already has, at the slot the site has seen, is // inlined into the compiled method; the rest (adding, a miss, frozen, other receivers) is // out of line. It is RubyInstanceData's write protocol: store the slot, then re-read // element 0 and redo the write the slow way if the shape changed under it - a Move's // process-wide barrier makes either the store visible to its copy or the re-read see Moved. [Emitted] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static object SetInstanceVariable(object self, object value, RubyScope/*!*/ scope, InstanceVariableSite/*!*/ site) { if (self is RubyObject obj && TryWriteCached(obj._instanceData, value, site)) { return value; } return SetInstanceVariableUncached(self, value, scope, site); } // The cached store into an existing variable of an object's instance data (null for no // variables yet): false for a miss, an add, a frozen object or a shape changed under it. [MethodImpl(MethodImplOptions.AggressiveInlining)] internal static bool TryWriteCached(RubyInstanceData data, object value, InstanceVariableSite/*!*/ site) { if (data != null && !data._frozen) { object[] slots = data._ivars; var entry = site.Cache; object shape = RubyInstanceData.ShapeOf(slots); if (shape == entry.Shape && entry.Next == null) { RubyInstanceData.SetSlot(slots, entry.Index, value); return RubyInstanceData.ShapeOf(slots) == shape; } } return false; } [MethodImpl(MethodImplOptions.NoInlining)] private static object SetInstanceVariableUncached(object self, object value, RubyScope/*!*/ scope, InstanceVariableSite/*!*/ site) { var obj = self as RubyObject; if (obj != null) { var data = obj._instanceData ?? GetInstanceData(ref obj._instanceData); if (!data._frozen && data.TrySetCached(site.Cache, value, obj)) { return value; } } else { var context = scope.RubyContext; if (context.RactorsStarted && !site.IsCoreLibrary) { RactorIsolation.CheckInstanceVariableWrite(context, self); } if (TrySetRubyObjectInstanceVariable(self, value, site) || TrySetOwnedInstanceVariable(context, self, value, site)) { return value; } } scope.RubyContext.SetInstanceVariable(self, value, site); return value; } /// /// An attribute writer (attr_writer, attr_accessor): of a value that is a positional /// argument, even a Hash passed as keywords (o.send(:x=, k: 1)) - kept, it stops saying so (). /// Here rather than in the writer's rule, which is interpreted for its first calls. /// [Emitted] [MethodImpl(MethodImplOptions.AggressiveInlining)] public static object SetAttribute(object self, object value, RubyScope/*!*/ scope, InstanceVariableSite/*!*/ site) { return SetInstanceVariable(self, ClearKeywordArguments(value), scope, site); } // The cached store of an instance of a Ruby subclass of a built-in CLR class - Array, Hash, // Exception, Enumerator - which is an IRubyObject without being a RubyObject. Those went the // slow way for every write: four `@x = v' in an Enumerator subclass's initialize cost ~0.6 us // more than in a plain object's. The object keeps its frozen flag itself (IRubyObjectState), // and a String also warns about a chilled literal, so a String is left to the slow path. internal static bool TrySetRubyObjectInstanceVariable(object self, object value, InstanceVariableSite/*!*/ site) { var obj = self as IRubyObject; if (obj == null || obj.IsFrozen || self is MutableString) { return false; } return obj.GetInstanceData().TrySetCached(site.Cache, value, obj.ImmediateClass); } // The cached store of a CLR object that keeps its own instance data (IRubyInstanceDataOwner); // the slow path - the frozen error among the rest - is the caller's when this says no. private static bool TrySetOwnedInstanceVariable(RubyContext/*!*/ context, object self, object value, InstanceVariableSite/*!*/ site) { var owner = self as IRubyInstanceDataOwner; if (owner == null) { return false; } var data = owner.GetInstanceData(context); return data != null && !data._frozen && data.TrySetCached(site.Cache, value, data.ImmediateClass); } public static object WriteInstanceVariable(RubyContext/*!*/ context, object self, object value, InstanceVariableSite/*!*/ site) { var obj = self as RubyObject; if (obj != null) { var data = obj._instanceData ?? GetInstanceData(ref obj._instanceData); if (!data._frozen && data.TrySetCached(site.Cache, value, obj)) { return value; } } else { if (context.RactorsStarted && !site.IsCoreLibrary) { RactorIsolation.CheckInstanceVariableWrite(context, self); } if (TrySetRubyObjectInstanceVariable(self, value, site) || TrySetOwnedInstanceVariable(context, self, value, site)) { return value; } } context.SetInstanceVariable(self, value, site); return value; } #endregion #region Class Variables /// /// The module whose class variables @@x names: the innermost class or module body. MRI /// refuses the access where there is none - the top level, a method or block defined there. /// private static RubyModule/*!*/ GetClassVariableOwner(RubyScope/*!*/ scope, string/*!*/ name) { RubyModule owner = scope.GetInnerMostModuleForClassVariableAccess(); if (owner == null) { throw new RuntimeError("class variable access from toplevel"); } if (owner.Context.RactorsStarted) { RactorIsolation.CheckClassVariable(owner, name); } return owner; } [Emitted] public static object GetClassVariable(RubyScope/*!*/ scope, string/*!*/ name) { // owner is the first module in scope: RubyModule owner = GetClassVariableOwner(scope, name); return GetClassVariableInternal(owner, name); } private static object GetClassVariableInternal(RubyModule/*!*/ module, string/*!*/ name) { object value; if (module.TryResolveClassVariable(name, out value) == null) { throw RubyExceptions.WithNameAndReceiver(module.Context, RubyExceptions.CreateNameError(String.Format("uninitialized class variable {0} in {1}", name, module.Name)), name, module); } return value; } [Emitted] public static object TryGetClassVariable(RubyScope/*!*/ scope, string/*!*/ name) { object value; // owner is the first module in scope: GetClassVariableOwner(scope, name).TryResolveClassVariable(name, out value); return value; } [Emitted] public static bool IsDefinedClassVariable(RubyScope/*!*/ scope, string/*!*/ name) { // owner is the first module in scope: RubyModule owner = scope.GetInnerMostModuleForClassVariableLookup(); if (owner.Context.RactorsStarted) { RactorIsolation.CheckClassVariable(owner, name); } object value; return owner.TryResolveClassVariable(name, out value) != null; } [Emitted] public static object SetClassVariable(object value, RubyScope/*!*/ scope, string/*!*/ name) { return SetClassVariableInternal(GetClassVariableOwner(scope, name), name, value); } private static object SetClassVariableInternal(RubyModule/*!*/ lexicalOwner, string/*!*/ name, object value) { object oldValue; RubyModule owner = lexicalOwner.TryResolveClassVariable(name, out oldValue); (owner ?? lexicalOwner).SetClassVariable(name, value); return value; } #endregion #region Ruby Types [Emitted] public static string/*!*/ ObjectToString(IRubyObject/*!*/ obj) { return RubyUtils.ObjectToMutableString(obj).ToString(); } [Emitted] //RubyTypeBuilder public static RubyInstanceData/*!*/ GetInstanceData(ref RubyInstanceData/*!*/ instanceData) { if (instanceData == null) { Interlocked.CompareExchange(ref instanceData, new RubyInstanceData(), null); } return instanceData; } [Emitted] public static bool IsObjectFrozen(RubyInstanceData instanceData) { return instanceData != null && instanceData.IsFrozen; } [Emitted] public static void FreezeObject(ref RubyInstanceData instanceData) { RubyOps.GetInstanceData(ref instanceData).Freeze(); } [Emitted(UseReflection = true)] //RubyTypeBuilder public static void DeserializeObject(out RubyInstanceData/*!*/ instanceData, out RubyClass/*!*/ immediateClass, SerializationInfo/*!*/ info) { immediateClass = (RubyClass)info.GetValue(RubyUtils.SerializationInfoClassKey, typeof(RubyClass)); RubyInstanceData newInstanceData = null; foreach (SerializationEntry entry in info) { if (entry.Name.StartsWith("@", StringComparison.Ordinal)) { if (newInstanceData == null) { newInstanceData = new RubyInstanceData(); } newInstanceData.SetInstanceVariable(entry.Name, entry.Value); } } instanceData = newInstanceData; } [Emitted(UseReflection = true)] //RubyTypeBuilder public static void SerializeObject(RubyInstanceData instanceData, RubyClass/*!*/ immediateClass, SerializationInfo/*!*/ info) { info.AddValue(RubyUtils.SerializationInfoClassKey, immediateClass, typeof(RubyClass)); if (instanceData != null) { string[] instanceNames = instanceData.GetInstanceVariableNames(); foreach (string name in instanceNames) { object value; if (!instanceData.TryGetInstanceVariable(name, out value)) { value = null; } info.AddValue(name, value, typeof(object)); } } } #endregion #region Delegates, Events /// /// Hooks up an event to call a proc at hand. /// EventInfo is passed in as object since it is an internal type. /// [Emitted] public static Proc/*!*/ HookupEvent(RubyEventInfo/*!*/ eventInfo, object/*!*/ target, Proc/*!*/ proc) { eventInfo.Tracker.AddHandler(target, proc, eventInfo.Context.DelegateCreator); return proc; } [Emitted] public static RubyEvent/*!*/ CreateEvent(RubyEventInfo/*!*/ eventInfo, object/*!*/ target, string/*!*/ name) { return new RubyEvent(target, eventInfo, name); } [Emitted] public static Delegate/*!*/ CreateDelegateFromProc(Type/*!*/ type, Proc proc) { if (proc == null) { throw RubyExceptions.NoBlockGiven(); } // Each invocation calls the proc the way Proc#call does, with its own flow-control frame, so // a break in a block passed to a CLR method as a delegate unwinds to that call, and a // return in a lambda returns from the lambda. return proc.LocalScope.RubyContext.DelegateCreator.GetDelegate(proc, type); } [Emitted] public static Delegate/*!*/ CreateDelegateFromMethod(Type/*!*/ type, RubyMethod/*!*/ method) { return method.Info.Context.DelegateCreator.GetDelegate(method, type); } #endregion #region Tuples // Instance variable storages needs MT to be a subclass of MT for all n > m. // This property is not true if we used DynamicNull as a generic argument for arities that are not powers of 2 like MutableTuple.MakeTupleType does. // We make this property true for all simple tuples, thus instance variable storages can only use tuples of size <= 128. internal static Type/*!*/ MakeObjectTupleType(int fieldCount) { if (fieldCount <= MutableTuple.MaxSize) { if (fieldCount <= 1) { return typeof(MutableTuple); } else if (fieldCount <= 2) { return typeof(MutableTuple); } else if (fieldCount <= 4) { return typeof(MutableTuple); } else if (fieldCount <= 8) { return typeof(MutableTuple); } else if (fieldCount <= 16) { return typeof(MutableTuple); } else if (fieldCount <= 32) { return typeof(MutableTuple); } else if (fieldCount <= 64) { return typeof(MutableTuple); } else { return MakeObjectTupleType128(); } } Type[] types = new Type[fieldCount]; for (int i = 0; i < types.Length; i++) { types[i] = typeof(object); } return MutableTuple.MakeTupleType(types); } [MethodImpl(MethodImplOptions.NoInlining)] private static Type/*!*/ MakeObjectTupleType128() { return typeof(MutableTuple); } #endregion [Emitted] public static void X(string marker) { } /// /// The constructor of a value type that declares no parameterless one: T.new is default(T). /// Instantiated for the type, it returns T, so keywords passed to new can set the new value's /// fields and properties like those passed to a real constructor. (Not constrained to struct: Nullable<T> /// is a value type that does not satisfy that constraint, and System::Nullable[Integer].new is nil.) /// public static T CreateDefaultValue() { return default(T); } internal static readonly MethodInfo/*!*/ CreateDefaultValueMethod = typeof(RubyOps).GetMethod(nameof(CreateDefaultValue)); // A Ruby Array or Hash passed where a CLR method takes a typed collection (see Converter.TryConvertToCollection): // a copy, each item converted by the site the binder made for the item type. public static T[]/*!*/ ConvertToVector(IList/*!*/ list, CallSite>/*!*/ itemConversion) { var result = new T[list.Count]; for (int i = 0; i < result.Length; i++) { result[i] = itemConversion.Target(itemConversion, list[i]); } return result; } public static List/*!*/ ConvertToList(IList/*!*/ list, CallSite>/*!*/ itemConversion) { var result = new List(list.Count); for (int i = 0; i < list.Count; i++) { result.Add(itemConversion.Target(itemConversion, list[i])); } return result; } public static Dictionary/*!*/ ConvertToDictionary(IDictionary/*!*/ hash, CallSite>/*!*/ keyConversion, CallSite>/*!*/ valueConversion) { var result = new Dictionary(hash.Count); foreach (var entry in hash) { result[keyConversion.Target(keyConversion, entry.Key)] = valueConversion.Target(valueConversion, entry.Value); } return result; } /// /// The module or class of a type group's non-generic member, where a type group is passed for a module /// (see Converter.TryConvertTypeGroup). /// public static RubyModule/*!*/ ConvertTypeGroupToModule(RubyContext/*!*/ context, Microsoft.Scripting.Actions.TypeGroup/*!*/ typeGroup) { Type type; if (!typeGroup.TryGetNonGenericType(out type)) { throw RubyExceptions.CreateTypeError(String.Format("{0} has no non-generic type to stand for a module; use {0}[...] or {0}.of(...)", typeGroup.Name)); } return context.GetModule(type); } /// /// A Ruby index passed for a System.Index: a negative one counts from the end, -1 is ^1. /// public static System.Index ConvertToIndex(int index) { return index < 0 ? System.Index.FromEnd(-index) : System.Index.FromStart(index); } /// /// A Ruby Range of Integers passed for a System.Range. Ruby's end is inclusive unless the range excludes it, /// and either end may be nil (beginless, endless): 1..3 is 1..4, 1...-1 is 1..^1, -3..-1 is ^3..^0. /// public static System.Range ConvertToRange(Range/*!*/ range) { System.Index start = range.Begin == null ? System.Index.Start : ConvertToIndex(GetRangeEndpoint(range.Begin)); System.Index end; if (range.End == null) { end = System.Index.End; } else { int last = GetRangeEndpoint(range.End); if (range.ExcludeEnd) { end = ConvertToIndex(last); } else { end = last >= 0 ? System.Index.FromStart(last + 1) : System.Index.FromEnd(-last - 1); } } return new System.Range(start, end); } private static int GetRangeEndpoint(object value) { if (value is int) { return (int)value; } throw RubyExceptions.CreateTypeError(String.Format("no implicit conversion of {0} into System::Range (Integer endpoints expected)", value == null ? "nil" : value.GetType().Name)); } #region Constructing instances of Ruby classes derived from CLR classes // Instances new allocated without running a constructor - their CLR base class has no parameterless one - // that initialize has yet to construct by calling super. private static readonly ConditionalWeakTable/*!*/ _unconstructedInstances = new ConditionalWeakTable(); /// /// Allocates an instance of the CLR type emitted for a Ruby class derived from a CLR class that has no /// parameterless constructor, without running any constructor. The Ruby class's initialize constructs it: /// its super goes to the base class's constructor (see InvokeClrConstructor). /// public static object/*!*/ AllocateUnconstructed(RubyClass/*!*/ rubyClass, Type/*!*/ type) { var instance = (IRubyObject)RuntimeHelpers.GetUninitializedObject(type); instance.ImmediateClass = rubyClass; _unconstructedInstances.AddOrUpdate(instance, null); return instance; } /// /// The result of new for an instance allocated by AllocateUnconstructed, once initialize has returned: an /// error if initialize did not construct it, which C# would not have compiled. /// public static object EnsureConstructed(object result, Type/*!*/ baseType) { object unused; if (result != null && _unconstructedInstances.TryGetValue(result, out unused)) { _unconstructedInstances.Remove(result); throw RubyExceptions.CreateTypeError(String.Format( "{0}.new: initialize did not call super, and {1} has no parameterless constructor to construct the instance with", ((IRubyObject)result).ImmediateClass.NominalClass.Name, baseType.Name)); } return result; } /// /// super in initialize of a Ruby class derived from a CLR class: runs the base class's constructor on the /// instance new has allocated. The parameterless constructor has already run if there is one, as allocation /// uses it, so calling it again does nothing. /// public static object/*!*/ InvokeClrConstructor(ConstructorInfo/*!*/ constructor, object/*!*/ instance, object[]/*!*/ args) { object unused; bool unconstructed = _unconstructedInstances.TryGetValue(instance, out unused); if (args.Length == 0 && !unconstructed) { return instance; } try { constructor.Invoke(instance, args); } catch (TargetInvocationException e) { System.Runtime.ExceptionServices.ExceptionDispatchInfo.Capture(e.InnerException ?? e).Throw(); } if (unconstructed) { _unconstructedInstances.Remove(instance); } return instance; } /// /// An argument passed for a CLR method's Object parameter: a Ruby String as a System.String (see /// RubyOverloadResolver.Convert), anything else as it is. /// public static object ToClrArgument(object value) { var str = value as MutableString; return (str != null) ? str.ConvertToString() : value; } /// /// The test of a rule whose overload was chosen by the items of a Ruby Array argument (see /// RubyOverloadResolver.SelectBestItemConversion): whether are the distinct CLR types of /// the items, typeof(DynamicNull) for nil - each item has one of them, and each is some item's. Linear in the items, /// as is the conversion of the Array that follows. /// public static bool HasItemTypes(IList/*!*/ list, Type/*!*/[]/*!*/ itemTypes) { Span seen = itemTypes.Length <= 32 ? stackalloc bool[itemTypes.Length] : new bool[itemTypes.Length]; int seenCount = 0; for (int i = 0; i < list.Count; i++) { object item = list[i]; int index = Array.IndexOf(itemTypes, (item != null) ? item.GetType() : typeof(DynamicNull)); if (index < 0) { return false; } if (!seen[index]) { seen[index] = true; seenCount++; } } return seenCount == itemTypes.Length; } internal static readonly MethodInfo/*!*/ HasItemTypesMethod = typeof(RubyOps).GetMethod(nameof(HasItemTypes)); internal static readonly MethodInfo/*!*/ ToClrArgumentMethod = typeof(RubyOps).GetMethod(nameof(ToClrArgument)); internal static readonly MethodInfo/*!*/ AllocateUnconstructedMethod = typeof(RubyOps).GetMethod(nameof(AllocateUnconstructed)); internal static readonly MethodInfo/*!*/ EnsureConstructedMethod = typeof(RubyOps).GetMethod(nameof(EnsureConstructed)); internal static readonly MethodInfo/*!*/ InvokeClrConstructorMethod = typeof(RubyOps).GetMethod(nameof(InvokeClrConstructor)); #endregion internal static readonly MethodInfo/*!*/ ConvertToIndexMethod = typeof(RubyOps).GetMethod(nameof(ConvertToIndex)); internal static readonly MethodInfo/*!*/ ConvertToRangeMethod = typeof(RubyOps).GetMethod(nameof(ConvertToRange)); internal static readonly MethodInfo/*!*/ ConvertTypeGroupToModuleMethod = typeof(RubyOps).GetMethod(nameof(ConvertTypeGroupToModule)); internal static readonly MethodInfo/*!*/ ConvertToVectorMethod = typeof(RubyOps).GetMethod(nameof(ConvertToVector)); internal static readonly MethodInfo/*!*/ ConvertToListMethod = typeof(RubyOps).GetMethod(nameof(ConvertToList)); internal static readonly MethodInfo/*!*/ ConvertToDictionaryMethod = typeof(RubyOps).GetMethod(nameof(ConvertToDictionary)); } }