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);

            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 #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);

            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 #4
0
        public void ResetCurrentStream()
        {
            string file   = CurrentFileName.ToString();
            Stream stream = RubyFile.OpenFileStream(_context, file, _defaultMode);

            SingletonStream = new RubyIO(_context, stream, _defaultMode);
        }
Beispiel #5
0
 public static int ChangeOwner(RubyContext /*!*/ context, RubyFile /*!*/ self, object owner, object group)
 {
     if ((owner == null || owner is int) && (group == null || group is int))
     {
         return(0);
     }
     throw RubyExceptions.CreateUnexpectedTypeError(context, owner, "Fixnum");
 }
Beispiel #6
0
 public static MutableString /*!*/ Inspect(RubyContext /*!*/ context, RubyFile /*!*/ self)
 {
     return(MutableString.CreateMutable(context.GetPathEncoding()).
            Append("#<File:").
            Append(self.Path).
            Append(self.Closed ? " (closed)" : "").
            Append('>'));
 }
Beispiel #7
0
        public static RubyIO /*!*/ Reopen(RubyIO /*!*/ self, [DefaultProtocol, NotNull] MutableString /*!*/ path, int mode)
        {
            Stream newStream = RubyFile.OpenFileStream(self.Context, path.ConvertToString(), (IOMode)mode);

            self.Context.SetStream(self.GetFileDescriptor(), newStream);
            self.SetStream(newStream);
            self.Mode = (IOMode)mode;
            return(self);
        }
Beispiel #8
0
        public static int Truncate(RubyFile /*!*/ self, [DefaultProtocol] int size)
        {
            if (size < 0)
            {
                throw new InvalidError();
            }

            self.Length = size;
            return(0);
        }
Beispiel #9
0
 public static int Truncate(RubyClass /*!*/ self, [DefaultProtocol, NotNull] MutableString /*!*/ path, [DefaultProtocol] int size)
 {
     if (size < 0)
     {
         throw new InvalidError();
     }
     using (RubyFile f = new RubyFile(self.Context, path, IOMode.ReadWrite)) {
         f.Length = size;
     }
     return(0);
 }
Beispiel #10
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 #11
0
        public static int SysOpen(RubyClass /*!*/ self, [NotNull] MutableString path, [Optional] MutableString mode, [Optional] int perm)
        {
            if (RubyFileOps.DirectoryExists(self.Context, path))
            {
                // TODO: What file descriptor should be returned for a directory?
                return(-1);
            }
            RubyIO io       = new RubyFile(self.Context, path.ToString(), IOModeEnum.Parse(mode));
            int    fileDesc = io.GetFileDescriptor();

            io.Close();
            return(fileDesc);
        }
Beispiel #12
0
        public void ResetCurrentStream()
        {
            string file = CurrentFileName.ToString();

            if (file == "-")
            {
                SingletonStream = this.Context.StandardInput as RubyIO;
            }
            else
            {
                Stream stream = RubyFile.OpenFileStream(_context, file, _defaultMode);
                SingletonStream = new RubyIO(_context, stream, _defaultMode);
            }
        }
Beispiel #13
0
        public static RubyFile/*!*/ Reinitialize(
            ConversionStorage<int?>/*!*/ toInt,
            ConversionStorage<IDictionary<object, object>>/*!*/ toHash,
            ConversionStorage<MutableString>/*!*/ toPath,
            ConversionStorage<MutableString>/*!*/ toStr,
            RubyFile/*!*/ self,
            object descriptorOrPath, 
            [Optional]object optionsOrMode, 
            [Optional]object optionsOrPermissions,
            [DefaultParameterValue(null), DefaultProtocol]IDictionary<object, object> options) {

            var context = self.Context;
            
            Protocols.TryConvertToOptions(toHash, ref options, ref optionsOrMode, ref optionsOrPermissions);
            var toIntSite = toInt.GetSite(TryConvertToFixnumAction.Make(toInt.Context));

            IOInfo info = new IOInfo();
            if (optionsOrMode != Missing.Value) {
                int? m = toIntSite.Target(toIntSite, optionsOrMode);
                info = m.HasValue ? new IOInfo((IOMode)m) : IOInfo.Parse(context, Protocols.CastToString(toStr, optionsOrMode));
            }

            int permissions = 0;
            if (optionsOrPermissions != Missing.Value) {
                int? p = toIntSite.Target(toIntSite, optionsOrPermissions);
                if (!p.HasValue) {
                    throw RubyExceptions.CreateTypeConversionError(context.GetClassName(optionsOrPermissions), "Integer");
                }
                permissions = p.Value;
            }

            if (options != null) {
                info = info.AddOptions(toStr, options);
            }

            // TODO: permissions
            
            // descriptor or path:
            int? descriptor = toIntSite.Target(toIntSite, descriptorOrPath);
            if (descriptor.HasValue) {
                RubyIOOps.Reinitialize(self, descriptor.Value, info);
            } else {
                Reinitialize(self, Protocols.CastToPath(toPath, descriptorOrPath), info, permissions);
            }

            return self;
        }
Beispiel #14
0
        public static RubyFile/*!*/ Reinitialize(RubyFile/*!*/ self,
            [DefaultProtocol, NotNull]Union<int, MutableString> descriptorOrPath, int mode, [Optional]int permission) {

            // TODO: remove duplicity (constructors vs initializers)

            if (descriptorOrPath.IsFixnum()) {
                RubyIOOps.Reinitialize(self, descriptorOrPath.Fixnum(), mode);
            } else {
                var path = self.Context.DecodePath(descriptorOrPath.Second);
                var stream = RubyFile.OpenFileStream(self.Context, path, (IOMode)mode);

                self.Path = path;
                self.Mode = (IOMode)mode;
                self.SetStream(stream);
                self.SetFileDescriptor(self.Context.AllocateFileDescriptor(stream));
            }
            // TODO: permission
            return self;
        }
Beispiel #15
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 #16
0
 public static MutableString /*!*/ GetPath(RubyFile /*!*/ self)
 {
     return(MutableString.Create(self.Path));
 }
Beispiel #17
0
 public static DateTime ModifiedTime(RubyContext /*!*/ context, RubyFile /*!*/ self)
 {
     return(RubyStatOps.ModifiedTime(RubyStatOps.Create(context, self.Path)));
 }
Beispiel #18
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 #19
0
        public static RubyIO/*!*/ Open(
            RubyContext/*!*/ context, 
            object self,
            [DefaultProtocol, NotNull]MutableString/*!*/ path, 
            int mode,
            [DefaultProtocol, DefaultParameterValue(RubyFileOps.ReadWriteMode)]int permission) {

            string fileName = path.ConvertToString();
            if (fileName.Length > 0 && fileName[0] == '|') {
                throw new NotImplementedError();
            }

            RubyIO file = new RubyFile(context, fileName, (RubyFileMode)mode);

            SetPermission(context, fileName, permission);

            return file;
        }
Beispiel #20
0
 public static object Open(BlockParam block, RubyClass/*!*/ self, [DefaultProtocol, NotNull]MutableString/*!*/ path)
 {
     var strPath = self.Context.DecodePath(path);
     var file = new RubyFile(self.Context, strPath, IOMode.ReadOnly);
     var gzipReader = Create(self, file);
     return gzipReader.Do(block);
 }
Beispiel #21
0
 public static MutableString GetPath(RubyFile/*!*/ self) {
     self.RequireInitialized();
     return self.Path != null ? self.Context.EncodePath(self.Path) : null;
 }
Beispiel #22
0
 public static FileSystemInfo Stat(RubyFile/*!*/ self) {
     return RubyStatOps.Create(self);
 }
Beispiel #23
0
 public static object ParseFile(RubyModule/*!*/ self, [DefaultProtocol, NotNull]MutableString/*!*/ path) {
     using (Stream stream = new RubyFile(self.Context, path.ConvertToString(), IOMode.Default).GetReadableStream()) {
         foreach (Node obj in MakeComposer(self.Context, stream)) {
             return obj;
         }
     }
     return ScriptingRuntimeHelpers.False;
 }
Beispiel #24
0
 public static object LoadFile(RubyScope/*!*/ scope, RubyModule/*!*/ self, [DefaultProtocol, NotNull]MutableString/*!*/ path) {
     using (RubyFile file = new RubyFile(self.Context, path.ConvertToString(), IOMode.Default)) {
         foreach (object obj in MakeConstructor(scope.GlobalScope, file.GetReadableStream())) {
             return obj;
         }
     }
     return null;
 }
Beispiel #25
0
 public static MutableString GetPath(RubyFile/*!*/ self) {
     return self.Path != null ? self.Context.EncodePath(self.Path) : null;
 }
Beispiel #26
0
 public static MutableString/*!*/ Inspect(RubyContext/*!*/ context, RubyFile/*!*/ self) {
     return MutableString.CreateMutable(context.GetPathEncoding()).
         Append("#<File:").
         Append(self.Path).
         Append(self.Closed ? " (closed)" : "").
         Append('>');
 }
Beispiel #27
0
 public static FileSystemInfo Stat(RubyContext/*!*/ context, RubyFile/*!*/ self) {
     return RubyStatOps.Create(context, self.Path);
 }
Beispiel #28
0
 public static RubyTime CreateTime(RubyContext/*!*/ context, RubyFile/*!*/ self) {
     return RubyStatOps.CreateTime(RubyStatOps.Create(context, self.Path));
 }
Beispiel #29
0
 public static MutableString GetPath(RubyFile /*!*/ self)
 {
     return(self.Path != null?self.Context.EncodePath(self.Path) : null);
 }
Beispiel #30
0
 public static int ChangeOwner(RubyFile /*!*/ self, [DefaultProtocol] int owner, [DefaultProtocol] int group)
 {
     return(0);
 }
Beispiel #31
0
 public static RubyTime ModifiedTime(RubyContext/*!*/ context, RubyFile/*!*/ self) {
     return RubyStatOps.ModifiedTime(RubyStatOps.Create(self));
 }
Beispiel #32
0
 public static int SysOpen(RubyClass/*!*/ self, [NotNull]MutableString path, [Optional]MutableString mode, [Optional]int perm) {
     if (FileTest.DirectoryExists(self.Context, path)) {
         // TODO: What file descriptor should be returned for a directory?
         return -1;
     }
     RubyIO io = new RubyFile(self.Context, path.ToString(), IOModeEnum.Parse(mode));
     int fileDesc = io.GetFileDescriptor();
     io.Close();
     return fileDesc;
 }
Beispiel #33
0
 public static MutableString/*!*/ Inspect(RubyFile/*!*/ self) {
     return MutableString.CreateMutable(self.Context.GetPathEncoding()).
         Append("#<").
         Append(self.Context.GetClassOf(self).GetName(self.Context)).
         Append(':').
         Append(self.Path).
         Append(self.Closed ? " (closed)" : "").
         Append('>');
 }
Beispiel #34
0
 public static int Chmod(RubyFile /*!*/ self, [DefaultProtocol] int permission)
 {
     Chmod(self.Path, permission);
     return(0);
 }
Beispiel #35
0
 // TODO: should work for IO and files w/o paths:
 internal static FileSystemInfo/*!*/ Create(RubyFile/*!*/ file) {
     file.RequireInitialized();
     if (file.Path == null) {
         throw new NotSupportedException("TODO: cannot get file info for files without path");
     }
     return Create(file.Context, file.Path);
 }
Beispiel #36
0
 public static int ChangeOwner(RubyFile/*!*/ self, [DefaultProtocol]int owner, [DefaultProtocol]int group) {
     return 0;
 }
Beispiel #37
0
 public static object Open(BlockParam block, RubyClass/*!*/ self,
     [DefaultProtocol, NotNull]MutableString/*!*/ path,
     [DefaultParameterValue(0)]int level,
     [DefaultParameterValue(DEFAULT_STRATEGY)]int strategy)
 {
     var strPath = self.Context.DecodePath(path);
     var file = new RubyFile(self.Context, strPath, IOMode.WriteOnly | IOMode.Truncate | IOMode.CreateIfNotExists);
     var gzipWriter = Create(self, file, level, strategy);
     return gzipWriter.Do(block);
 }
Beispiel #38
0
 public static int ChangeOwner(RubyContext/*!*/ context, RubyFile/*!*/ self, object owner, object group) {
     if ((owner == null || owner is int) && (group == null || group is int)) {
         return 0;
     }
     throw RubyExceptions.CreateUnexpectedTypeError(context, owner, "Fixnum");
 }
Beispiel #39
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);
            }
        }
Beispiel #40
0
        public static int Truncate(RubyFile/*!*/ self, [DefaultProtocol]int size) {
            if (size < 0) {
                throw new InvalidError();
            }

            self.Length = size;
            return 0;
        }
Beispiel #41
0
 public static FileSystemInfo Stat(RubyContext /*!*/ context, RubyFile /*!*/ self)
 {
     return(RubyStatOps.Create(context, self.Path));
 }
Beispiel #42
0
 public static int Truncate(ConversionStorage<MutableString>/*!*/ toPath, RubyClass/*!*/ self, object path, [DefaultProtocol]int size) {
     if (size < 0) {
         throw new InvalidError();
     }
     using (RubyFile f = new RubyFile(self.Context, self.Context.DecodePath(Protocols.CastToPath(toPath, path)), IOMode.ReadWrite)) {
         f.Length = size;
     }
     return 0;
 }
Beispiel #43
0
 public static MutableString /*!*/ Inspect(RubyContext /*!*/ context, RubyFile /*!*/ self)
 {
     return(MutableString.CreateMutable("#<File:").Append(self.Path).Append('>'));
 }
Beispiel #44
0
 public static RubyTime AccessTime(RubyContext/*!*/ context, RubyFile/*!*/ self) {
     return RubyStatOps.AccessTime(RubyStatOps.Create(self));
 }
Beispiel #45
0
        private static void Reinitialize(RubyFile/*!*/ file, MutableString/*!*/ path, IOInfo info, int permission) {
            var strPath = file.Context.DecodePath(path);
            var stream = RubyFile.OpenFileStream(file.Context, strPath, info.Mode);

            file.Path = strPath;
            file.Mode = info.Mode;
            file.SetStream(stream);
            file.SetFileDescriptor(file.Context.AllocateFileDescriptor(stream));

            if (info.HasEncoding) {
                file.ExternalEncoding = info.ExternalEncoding;
                file.InternalEncoding = info.InternalEncoding;
            }
        }
Beispiel #46
0
 public static RubyTime AccessTime(RubyContext /*!*/ context, RubyFile /*!*/ self)
 {
     return(RubyStatOps.AccessTime(RubyStatOps.Create(context, self.Path)));
 }
Beispiel #47
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 #48
0
 public static int Chmod(RubyFile/*!*/ self, [DefaultProtocol]int permission) {
     self.RequireInitialized();
     // TODO:
     if (self.Path == null) {
         throw new NotSupportedException("TODO: cannot chmod for files without path");
     }
     Chmod(self.Path, permission);
     return 0;
 }