Esempio n. 1
1
        public static object EachObject(BlockParam block, RubyModule/*!*/ self, [NotNull]RubyClass/*!*/ theClass) {
            Type classType = theClass.GetType();
            bool isClass = (classType == typeof(RubyClass));
            if (!isClass && classType != typeof(RubyModule)) {
                throw new NotSupportedException("each_object only supported for objects of type Class or Module");
            }
            if (block == null) {
                throw RubyExceptions.NoBlockGiven();
            }

            Dictionary<RubyModule, object> visited = new Dictionary<RubyModule, object>();
            Stack<RubyModule> modules = new Stack<RubyModule>();
            modules.Push(theClass.Context.ObjectClass);
            while (modules.Count > 0) {
                RubyModule next = modules.Pop();
                RubyClass asClass = next as RubyClass;

                if (!isClass || asClass != null) {
                    object result;
                    if (block.Yield(next, out result)) {
                        return result;
                    }
                }
                next.EnumerateConstants(delegate(RubyModule module, string name, object value) {
                    RubyModule constAsModule = (value as RubyModule);
                    if (constAsModule != null && !visited.ContainsKey(constAsModule)) {
                        modules.Push(constAsModule);
                        visited[module] = null;
                    }
                    return false;
                });
            }
            return visited.Count;
        }
Esempio n. 2
0
 public static Hash CreateHash(BlockParam block, RubyClass/*!*/ self, object defaultValue)
 {
     if (block != null) {
         throw RubyExceptions.CreateArgumentError("wrong number of arguments");
     }
     return new Hash(self.Context.EqualityComparer, null, defaultValue);
 }
Esempio n. 3
0
        public static object Reinitialize(BlockParam block, RubyModule/*!*/ self) {
            // no class can be reinitialized:
            if (self.IsClass) {
                throw RubyExceptions.CreateTypeError("already initialized class");
            }

            return (block != null) ? RubyUtils.EvaluateInModule(self, block, null) : null;
        }
Esempio n. 4
0
 public static object BlockRetry(BlockParam/*!*/ blockFlowControl) {
     if (blockFlowControl.CallerKind == BlockCallerKind.Yield) {
         blockFlowControl.SetFlowControl(BlockReturnReason.Retry, null, blockFlowControl.Proc.Kind);
         return RetrySingleton;
     } else {
         throw new LocalJumpError("retry from proc-closure");
     }
 }
Esempio n. 5
0
 public static Hash/*!*/ Initialize(BlockParam block, Hash/*!*/ self, object defaultValue) {
     Assert.NotNull(self);
     if (block != null) {
         throw RubyExceptions.CreateArgumentError("wrong number of arguments");
     }
     self.DefaultProc = null;
     self.DefaultValue = defaultValue;
     return self;
 }
Esempio n. 6
0
        /// <summary>
        /// Struct#new
        /// Creates Struct classes with the specified name and members
        /// </summary>
        private static object Create(BlockParam block, RubyClass/*!*/ self, string className, string/*!*/[]/*!*/ attributeNames) {
            var result = RubyStruct.DefineStruct(self, className, attributeNames);

            if (block != null) {
                return RubyUtils.EvaluateInModule(result, block, null, result);
            }

            return result;
        }
Esempio n. 7
0
        public static Proc AtExit(BlockParam/*!*/ block, object self)
        {
            if (block == null) {
                throw RubyExceptions.CreateArgumentError("called without a block");
            }

            block.RubyContext.RegisterShutdownHandler(block.Proc);
            return block.Proc;
        }
Esempio n. 8
0
 public static Object Scan(ConversionStorage<MutableString>/*!*/ toMutableStringStorage, RespondToStorage/*!*/ respondsTo, 
     BinaryOpStorage/*!*/ readIOStorage, BlockParam block, RubyModule/*!*/ self, Object/*!*/ source, Hash/*!*/ options)
 {
     Object elementContent;
     if (!self.TryGetConstant(null, "ElementContent", out elementContent) && !(elementContent is Hash)) {
         throw new Exception("Hpricot::ElementContent is missing or it is not an Hash");
     }
     var scanner = new HpricotScanner(toMutableStringStorage, readIOStorage, block);
     return scanner.Scan(source, options, elementContent as Hash);
 }
Esempio n. 9
0
        public static object NewStruct(BlockParam block, RubyClass/*!*/ self, [DefaultProtocol]MutableString className,
            [DefaultProtocol, NotNullItems]params string/*!*/[]/*!*/ attributeNames) {

            if (className == null) {
                return Create(block, self, null, attributeNames);
            }

            string strName = className.ConvertToString();
            RubyUtils.CheckConstantName(strName);
            return Create(block, self, strName, attributeNames);
        }
Esempio n. 10
0
 public static object Synchronize(BlockParam criticalSection, RubyMutex/*!*/ self) {
     lock (self._mutex) {
         self._isLocked = true;
         try {
             object result;
             criticalSection.Yield(out result);
             return result;
         } finally {
             self._isLocked = false;
         }
     }
 }
Esempio n. 11
0
        public static object NewStruct(BlockParam block, RubyClass/*!*/ self, [DefaultProtocol, Optional]MutableString className,
            [NotNull]params object[]/*!*/ attributeNames) {

            string[] symbols = Protocols.CastToSymbols(self.Context, attributeNames);

            if (className == null) {
                return Create(block, self, null, symbols);
            }

            string strName = className.ConvertToString();
            RubyUtils.CheckConstantName(strName);
            return Create(block, self, strName, symbols);
        }
Esempio n. 12
0
 public static RubyArray Map(RubyContext/*!*/ context, BlockParam collector, object self) {
     RubyArray result = new RubyArray();
     Each(context, self, Proc.Create(context, delegate(BlockParam/*!*/ selfBlock, object item) {
         if (collector != null) {
             if (collector.Yield(item, out item)) {
                 return item;
             }
         }
         result.Add(item);
         return null;
     }));
     return result;
 }
Esempio n. 13
0
 public static RubyArray Map(CallSiteStorage<EachSite>/*!*/ each, BlockParam collector, object self) {
     RubyArray result = new RubyArray();
     Each(each, self, Proc.Create(each.Context, delegate(BlockParam/*!*/ selfBlock, object _, object item) {
         if (collector != null) {
             if (collector.Yield(item, out item)) {
                 return item;
             }
         }
         result.Add(item);
         return null;
     }));
     return result;
 }
Esempio n. 14
0
        public static object Each(BlockParam block, IEnumerable/*!*/ self) {
            foreach (object obj in self) {
                object result;
                if (block == null) {
                    throw RubyExceptions.NoBlockGiven();
                }

                if (block.Yield(obj, out result)) {
                    return result;
                }
            }
            return self;
        }
Esempio n. 15
0
        public static object CreateArray(ConversionStorage<Union<IList, int>>/*!*/ toAryToInt,
            BlockParam block, RubyClass/*!*/ self, [NotNull]object/*!*/ arrayOrSize) {

            var site = toAryToInt.GetSite(CompositeConversionAction.Make(toAryToInt.Context, CompositeConversion.ToAryToInt));
            var union = site.Target(site, arrayOrSize);

            if (union.First != null) {
                // block ignored
                return CreateArray(union.First);
            } else if (block != null) {
                return CreateArray(block, union.Second);
            } else {
                return CreateArray(self, union.Second, null);
            }
        }
Esempio n. 16
0
 public static object Synchronize(BlockParam criticalSection, RubyMutex/*!*/ self) {
     bool lockTaken = false;
     try {
         MonitorUtils.Enter(self._mutex, ref lockTaken);
         self._isLocked = lockTaken;
         object result;
         criticalSection.Yield(out result);
         return result;
     } finally {
         if (lockTaken) {
             MonitorUtils.Exit(self._mutex, ref lockTaken);
             self._isLocked = lockTaken;
         }
     }
 }
Esempio n. 17
0
        public static object EachType(RubyContext/*!*/ context, BlockParam/*!*/ block, TypeGroup/*!*/ self) {
            if (block == null) {
                throw RubyExceptions.NoBlockGiven();
            }

            foreach (Type type in self.Types) {
                RubyModule module = context.GetModule(type);
                object result;
                if (block.Yield(module, out result)) {
                    return result;
                }
            }

            return self;
        }
Esempio n. 18
0
        public static int Count(CallSiteStorage<EachSite>/*!*/ each, BinaryOpStorage/*!*/ equals, BlockParam comparer, object self, object value)
        {
            if (comparer != null) {
                each.Context.ReportWarning("given block not used");
            }

            int result = 0;
            Each(each, self, Proc.Create(each.Context, delegate(BlockParam/*!*/ selfBlock, object _, object item) {
                if (Protocols.IsEqual(equals, item, value)) {
                    result++;
                }
                return null;
            }));
            return result;
        }
Esempio n. 19
0
        public static object BlockReturn(BlockParam/*!*/ blockFlowControl, object returnValue)
        {
            Proc proc = blockFlowControl.Proc;
            if (blockFlowControl.CallerKind == BlockCallerKind.Call && proc.Kind == ProcKind.Lambda) {
                return returnValue;
            }

            RuntimeFlowControl owner = proc.LocalScope.FlowControlScope;
            if (owner.IsActiveMethod) {
                blockFlowControl.ReturnReason = BlockReturnReason.Return;
                return new BlockReturnResult(owner, returnValue);
            }

            throw new LocalJumpError("unexpected return");
        }
Esempio n. 20
0
        public static object Each(BlockParam block, RubyStruct/*!*/ self)
        {
            if (block == null && self.ItemCount > 0) {
                throw RubyExceptions.NoBlockGiven();
            }

            foreach (var value in self.Values) {
                object result;
                if (block.Yield(value, out result)) {
                    return result;
                }
            }

            return self;
        }
Esempio n. 21
0
        public static object Trap(
            RubyContext/*!*/ context, 
            BlockParam block, 
            object self, 
            object signalId) {

            if ((signalId is MutableString) && ((MutableString)signalId).ConvertToString() == "INT") {
                context.InterruptSignalHandler = delegate() { object result; block.Yield(out result); };
            } else {
                // TODO: For now, just ignore unknown signals. This should be changed to throw an
                // exception. We are not doing it yet as it is close to the V1 RTM, and throwing
                // an exception might cause some app to misbehave whereas it might have happenned
                // to work if no exception is thrown
            }
            return null;
        }
Esempio n. 22
0
        public static object EachPair(BlockParam block, RubyStruct/*!*/ self)
        {
            if (block == null && self.ItemCount > 0) {
                throw RubyExceptions.NoBlockGiven();
            }

            var context = self.ImmediateClass.Context;
            foreach (KeyValuePair<string, object> entry in self.GetItems()) {
                object result;
                if (block.Yield(context.EncodeIdentifier(entry.Key), entry.Value, out result)) {
                    return result;
                }
            }

            return self;
        }
Esempio n. 23
0
        public static object AddDomainType(RubyContext/*!*/ context, BlockParam/*!*/ block, RubyModule/*!*/ self, 
            MutableString/*!*/ domainAndDate, RubyRegex/*!*/ typeRegex)
        {
            if (block == null) {
                throw RubyExceptions.NoBlockGiven();
            }

            MutableString tag = MutableString.CreateMutable(typeRegex.Encoding).
                Append("tag:").
                Append(domainAndDate).
                Append(':').
                Append(typeRegex.Pattern);

            RubyConstructor.AddExternalMultiConstructor(tag.ConvertToString(), block);
            return null;
        }
Esempio n. 24
0
        public static object CreateArray(ConversionStorage<int>/*!*/ conversionStorage, BlockParam block, RubyClass/*!*/ self, 
            [NotNull]object/*!*/ arrayOrSize) {

            IList array = Protocols.AsArray(self.Context, arrayOrSize);
            if (array != null) {
                // block ignored
                return CreateArray(array); 
            }

            int size = Protocols.CastToFixnum(conversionStorage, self.Context, arrayOrSize);
            if (block != null) {
                return CreateArray(block, size);
            } else {
                return CreateArray(self, size, null);
            }
        }
Esempio n. 25
0
        public static object Reinitialize(ConversionStorage<Union<IList, int>>/*!*/ toAryToInt,
            BlockParam block, RubyArray/*!*/ self, [NotNull]object/*!*/ arrayOrSize) {

            var context = toAryToInt.Context;

            var site = toAryToInt.GetSite(CompositeConversionAction.Make(context, CompositeConversion.ToAryToInt));
            var union = site.Target(site, arrayOrSize);
            
            if (union.First != null) {
                // block ignored
                return Reinitialize(self, union.First);
            } else if (block != null) {
                return Reinitialize(block, self, union.Second);
            } else {
                return ReinitializeByRepeatedValue(context, self, union.Second, null);
            }
        }
Esempio n. 26
0
        public static object Reinitialize(ConversionStorage<int>/*!*/ conversionStorage, RubyContext/*!*/ context, BlockParam block, RubyArray/*!*/ self, 
            [NotNull]object/*!*/ arrayOrSize) {
            RubyUtils.RequiresNotFrozen(context, self);

            IList array = Protocols.AsArray(context, arrayOrSize);
            if (array != null) {
                // block ignored
                return Reinitialize(self, array);
            }

            int size = Protocols.CastToFixnum(conversionStorage, context, arrayOrSize);
            if (block != null) {
                return Reinitialize(block, self, size);
            } else {
                return ReinitializeByRepeatedValue(context, self, size, null);
            }
        }
Esempio n. 27
0
        public static object ChangeDirectory(BlockParam block, object/*!*/ self, MutableString dir) {
            string strDir = dir.ConvertToString();

            if (block == null) {
                SetCurrentDirectory(strDir);
                return 0;
            } else {
                string current = Directory.GetCurrentDirectory();
                try {
                    SetCurrentDirectory(strDir);
                    object result;
                    block.Yield(dir, out result);
                    return result;
                } finally {
                    SetCurrentDirectory(current);
                }
            }
        }
Esempio n. 28
0
        public static object ChangeDirectory(BlockParam block, RubyClass/*!*/ self, [DefaultProtocol, NotNull]MutableString/*!*/ dir) {
            var pal = self.Context.Platform;
            string strDir = self.Context.DecodePath(dir);

            if (block == null) {
                SetCurrentDirectory(pal, strDir);
                return 0;
            }

            string current = pal.CurrentDirectory;
            try {
                SetCurrentDirectory(pal, strDir);
                object result;
                block.Yield(dir, out result);
                return result;
            } finally {
                SetCurrentDirectory(pal, current);
            }
        }
Esempio n. 29
0
        private static object TrueForItems(CallSiteStorage<EachSite>/*!*/ each, BlockParam predicate, object self, bool expected) {
            bool result = expected;
            Each(each, self, Proc.Create(each.Context, delegate(BlockParam/*!*/ selfBlock, object _, object item) {
                if (predicate != null) {
                    if (predicate.Yield(item, out item)) {
                        return item;
                    }
                }

                bool isTrue = Protocols.IsTrue(item);
                if (isTrue != result) {
                    result = isTrue;
                    return selfBlock.Break(ScriptingRuntimeHelpers.BooleanToObject(isTrue));
                }

                return null;
            }));

            return ScriptingRuntimeHelpers.BooleanToObject(result);
        }
        public static object DeleteIf(RubyContext/*!*/ context, BlockParam block, object/*!*/ self) {
            PlatformAdaptationLayer pal = context.DomainManager.Platform;
            IDictionary variables = pal.GetEnvironmentVariables();
            if (variables.Count > 0 && block == null) {
                throw RubyExceptions.NoBlockGiven();
            }

            foreach (DictionaryEntry entry in variables) {
                MutableString key = MutableString.Create(entry.Key.ToString()).Freeze();
                MutableString value = MutableString.Create(entry.Value.ToString()).Freeze();
                object result;
                if (block.Yield(key, value, out result)) {
                    return result;
                }

                if (RubyOps.IsTrue(result)) {
                    SetVariable(context, self, key, null);
                }
            }
            return self;
        }
Esempio n. 31
0
 public static bool IsProcConverterTarget(BlockParam /*!*/ bfc, MethodUnwinder /*!*/ unwinder)
 {
     Debug.Assert(unwinder != null);
     return(bfc.IsLibProcConverter && unwinder.TargetFrame == bfc.Proc.Converter);
 }