Beispiel #1
0
        public static RubyTopLevelScope/*!*/ CreateMainTopLevelScope(LocalsDictionary/*!*/ locals, Scope/*!*/ globalScope, LanguageContext/*!*/ language,
            out object self, out RuntimeFlowControl/*!*/ rfc, string dataPath, int dataOffset) {
            Assert.NotNull(locals, globalScope, language);

            RubyContext context = (RubyContext)language;
            RubyGlobalScope rubyGlobalScope = context.InitializeGlobalScope(globalScope, false);

            RubyTopLevelScope scope = new RubyTopLevelScope(rubyGlobalScope, null, locals);
            scope.Initialize(new RuntimeFlowControl(), RubyMethodAttributes.PrivateInstance, rubyGlobalScope.MainObject);
            scope.SetDebugName("top-main");

            var objectClass = context.ObjectClass;
            objectClass.SetConstant("TOPLEVEL_BINDING", new Binding(scope));
            if (dataOffset >= 0) {
                RubyFile dataFile;
                if (context.DomainManager.Platform.FileExists(dataPath)) {
                    dataFile = new RubyFile(context, dataPath, RubyFileMode.RDONLY);
                    dataFile.Seek(dataOffset, SeekOrigin.Begin);
                } else {
                    dataFile = null;
                }

                objectClass.SetConstant("DATA", dataFile);
            }

            self = scope.SelfObject;
            rfc = scope.RuntimeFlowControl;

            return scope;
        }
Beispiel #2
0
        public static MutableString /*!*/ Read(ConversionStorage <int> /*!*/ fixnumCast, RubyClass /*!*/ self,
                                               [DefaultProtocol, NotNull] MutableString /*!*/ path, [DefaultParameterValue(null)] object lengthObj,
                                               [DefaultParameterValue(0)] object offsetObj)
        {
            var site   = fixnumCast.GetSite(ConvertToFixnumAction.Make(fixnumCast.Context));
            int length = (lengthObj != null) ? site.Target(site, lengthObj) : 0;
            int offset = (offsetObj != null) ? site.Target(site, offsetObj) : 0;

            if (offset < 0)
            {
                throw RubyExceptions.CreateEINVAL();
            }

            if (length < 0)
            {
                throw RubyExceptions.CreateArgumentError("negative length {0} given", length);
            }

            using (RubyIO io = new RubyFile(self.Context, path.ConvertToString(), IOMode.ReadOnly)) {
                if (offset > 0)
                {
                    io.Seek(offset, SeekOrigin.Begin);
                }

                if (lengthObj == null)
                {
                    return(Read(io));
                }
                else
                {
                    return(Read(io, length, null));
                }
            }
        }
Beispiel #3
0
        public static RubyTopLevelScope/*!*/ CreateMainTopLevelScope(LocalsDictionary/*!*/ locals, Scope/*!*/ globalScope, LanguageContext/*!*/ language,
            out object self, out RuntimeFlowControl/*!*/ rfc, string dataPath, int dataOffset) {
            Assert.NotNull(locals, globalScope, language);

            GlobalScopeExtension rubyGlobalScope = (GlobalScopeExtension)language.EnsureScopeExtension(globalScope);

            RubyTopLevelScope scope = new RubyTopLevelScope(rubyGlobalScope, null, locals);
            scope.Initialize(new RuntimeFlowControl(), RubyMethodAttributes.PrivateInstance, rubyGlobalScope.MainObject);
            scope.SetDebugName(rubyGlobalScope.IsHosted ? "top-primary-hosted" : "top-primary");

            // define TOPLEVEL_BINDING constant:
            if (!rubyGlobalScope.IsHosted) {
                var objectClass = rubyGlobalScope.Context.ObjectClass;
                    
                objectClass.SetConstant("TOPLEVEL_BINDING", new Binding(scope));
                if (dataOffset >= 0) {
                    RubyFile dataFile;
                    if (File.Exists(dataPath)) {
                        dataFile = new RubyFile(rubyGlobalScope.Context, dataPath, RubyFileMode.RDONLY);
                        dataFile.Seek(dataOffset, SeekOrigin.Begin);
                    } else {
                        dataFile = null;
                    }

                    objectClass.SetConstant("DATA", dataFile);
                }
            }

            self = scope.SelfObject;
            rfc = scope.RuntimeFlowControl;

            return scope;
        }
Beispiel #4
0
        public static void SetDataConstant(RubyScope/*!*/ scope, string/*!*/ dataPath, int dataOffset) {
            Debug.Assert(dataOffset >= 0);
            RubyFile dataFile;
            RubyContext context = scope.RubyContext;
            if (context.DomainManager.Platform.FileExists(dataPath)) {
                dataFile = new RubyFile(context, dataPath, RubyFileMode.RDONLY);
                dataFile.Seek(dataOffset, SeekOrigin.Begin);
            } else {
                dataFile = null;
            }

            context.ObjectClass.SetConstant("DATA", dataFile);
        }
Beispiel #5
0
        public static MutableString /*!*/ Read(
            ConversionStorage <IDictionary <object, object> > /*!*/ toHash,
            ConversionStorage <int> /*!*/ fixnumCast,
            ConversionStorage <MutableString> /*!*/ toPath,
            RubyClass /*!*/ self,
            object path,
            [Optional] object optionsOrLength,
            [Optional] object optionsOrOffset,
            [DefaultParameterValue(null), DefaultProtocol] IDictionary <object, object> options)
        {
            TryConvertToOptions(toHash, ref options, ref optionsOrLength, ref optionsOrOffset);
            var site = fixnumCast.GetSite(ConvertToFixnumAction.Make(fixnumCast.Context));

            int length = (optionsOrLength != Missing.Value && optionsOrLength != null) ? site.Target(site, optionsOrLength) : 0;
            int offset = (optionsOrOffset != Missing.Value && optionsOrOffset != null) ? site.Target(site, optionsOrOffset) : 0;

            if (offset < 0)
            {
                throw RubyExceptions.CreateEINVAL();
            }

            if (length < 0)
            {
                throw RubyExceptions.CreateArgumentError("negative length {0} given", length);
            }

            using (RubyIO io = new RubyFile(self.Context, Protocols.CastToPath(toPath, path), IOMode.ReadOnly)) {
                if (offset > 0)
                {
                    io.Seek(offset, SeekOrigin.Begin);
                }

                if (optionsOrLength != Missing.Value && optionsOrLength != null)
                {
                    return(Read(io, length, null));
                }
                else
                {
                    return(Read(io));
                }
            }
        }
Beispiel #6
0
        public static MutableString/*!*/ Read(
            ConversionStorage<IDictionary<object, object>>/*!*/ toHash,
            ConversionStorage<int>/*!*/ fixnumCast,
            ConversionStorage<MutableString>/*!*/ toPath,
            RubyClass/*!*/ self,
            object path, 
            [Optional]object optionsOrLength, 
            [Optional]object optionsOrOffset,
            [DefaultParameterValue(null), DefaultProtocol]IDictionary<object, object> options) {

            Protocols.TryConvertToOptions(toHash, ref options, ref optionsOrLength, ref optionsOrOffset);
            var site = fixnumCast.GetSite(ConvertToFixnumAction.Make(fixnumCast.Context));

            int length = (optionsOrLength != Missing.Value && optionsOrLength != null) ? site.Target(site, optionsOrLength) : 0;
            int offset = (optionsOrOffset != Missing.Value && optionsOrOffset != null) ? site.Target(site, optionsOrOffset) : 0;

            if (offset < 0) {
                throw RubyExceptions.CreateEINVAL();
            }

            if (length < 0) {
                throw RubyExceptions.CreateArgumentError("negative length {0} given", length);
            }

            // TODO: options

            using (RubyIO io = new RubyFile(self.Context, self.Context.DecodePath(Protocols.CastToPath(toPath, path)), IOMode.ReadOnly)) {
                if (offset > 0) {
                    io.Seek(offset, SeekOrigin.Begin);
                }

                if (optionsOrLength != Missing.Value && optionsOrLength != null) {
                    return Read(io, length, null);
                } else {
                    return Read(io);
                }
            }
        }
Beispiel #7
0
        public static MutableString/*!*/ Read(ConversionStorage<int>/*!*/ fixnumCast, RubyClass/*!*/ self,
            [DefaultProtocol, NotNull]MutableString/*!*/ path, [DefaultParameterValue(null)]object lengthObj, 
            [DefaultParameterValue(0)]object offsetObj) {

            var site = fixnumCast.GetSite(ConvertToFixnumAction.Make(fixnumCast.Context));
            int length = (lengthObj != null) ? site.Target(site, lengthObj) : 0;
            int offset = (offsetObj != null) ? site.Target(site, offsetObj) : 0;

            if (offset < 0) {
                throw RubyExceptions.CreateEINVAL();
            }

            if (length < 0) {
                throw RubyExceptions.CreateArgumentError("negative length {0} given", length);
            }

            using (RubyIO io = new RubyFile(self.Context, path.ConvertToString(), IOMode.ReadOnly)) {
                if (offset > 0) {
                    io.Seek(offset, SeekOrigin.Begin);
                }

                if (lengthObj == null) {
                    return Read(io);
                } else {
                    return Read(io, length, null);
                }
            }
        }
Beispiel #8
0
        public static MutableString/*!*/ Binread(RubyClass/*!*/ self,
            [DefaultProtocol, NotNull]MutableString/*!*/ path,
            [DefaultProtocol, Optional]int? length,
            [DefaultProtocol, Optional]int? offset)
        {
            if (offset.HasValue && offset.Value < 0) {
                throw RubyExceptions.CreateEINVAL();
            }

            if (length.HasValue && length.Value < 0) {
                throw RubyExceptions.CreateArgumentError("negative length {0} given", length);
            }

            using (RubyIO io = new RubyFile(self.Context, path.ToString(), IOMode.ReadOnly)) {
                if (offset.HasValue && offset.Value > 0) {
                    io.Seek(offset.Value, SeekOrigin.Begin);
                }

                return (length.HasValue) ? Read(io, length.Value, null) : Read(io);
            }
        }