[前][次][番号順一覧][スレッド一覧]

ruby-changes:60673

From: Nobuyoshi <ko1@a...>
Date: Mon, 6 Apr 2020 22:22:44 +0900 (JST)
Subject: [ruby-changes:60673] 310054b240 (master): Moved `Dir.open` and `Dir#initialize` to dir.rb

https://git.ruby-lang.org/ruby.git/commit/?id=310054b240

From 310054b240a511f888ec5092eb32fed29e4109c9 Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Sat, 18 Jan 2020 13:59:21 +0900
Subject: Moved `Dir.open` and `Dir#initialize` to dir.rb


diff --git a/.document b/.document
index 1cb2722..b18ca80 100644
--- a/.document
+++ b/.document
@@ -12,6 +12,7 @@ prelude.rb https://github.com/ruby/ruby/blob/trunk/.document#L12
 rbconfig.rb
 array.rb
 ast.rb
+dir.rb
 gc.rb
 io.rb
 kernel.rb
diff --git a/common.mk b/common.mk
index f1b836f..35f19f7 100644
--- a/common.mk
+++ b/common.mk
@@ -1000,6 +1000,7 @@ BUILTIN_RB_SRCS = \ https://github.com/ruby/ruby/blob/trunk/common.mk#L1000
 		$(srcdir)/ast.rb \
 		$(srcdir)/gc.rb \
 		$(srcdir)/io.rb \
+		$(srcdir)/dir.rb \
 		$(srcdir)/pack.rb \
 		$(srcdir)/trace_point.rb \
 		$(srcdir)/warning.rb \
@@ -2024,9 +2025,11 @@ dir.$(OBJEXT): $(top_srcdir)/internal/string.h https://github.com/ruby/ruby/blob/trunk/common.mk#L2025
 dir.$(OBJEXT): $(top_srcdir)/internal/vm.h
 dir.$(OBJEXT): $(top_srcdir)/internal/warnings.h
 dir.$(OBJEXT): {$(VPATH)}assert.h
+dir.$(OBJEXT): {$(VPATH)}builtin.h
 dir.$(OBJEXT): {$(VPATH)}config.h
 dir.$(OBJEXT): {$(VPATH)}defines.h
 dir.$(OBJEXT): {$(VPATH)}dir.c
+dir.$(OBJEXT): {$(VPATH)}dir.rbinc
 dir.$(OBJEXT): {$(VPATH)}encindex.h
 dir.$(OBJEXT): {$(VPATH)}encoding.h
 dir.$(OBJEXT): {$(VPATH)}id.h
@@ -2840,6 +2843,7 @@ miniinit.$(OBJEXT): {$(VPATH)}ast.rb https://github.com/ruby/ruby/blob/trunk/common.mk#L2843
 miniinit.$(OBJEXT): {$(VPATH)}builtin.h
 miniinit.$(OBJEXT): {$(VPATH)}config.h
 miniinit.$(OBJEXT): {$(VPATH)}defines.h
+miniinit.$(OBJEXT): {$(VPATH)}dir.rb
 miniinit.$(OBJEXT): {$(VPATH)}encoding.h
 miniinit.$(OBJEXT): {$(VPATH)}gc.rb
 miniinit.$(OBJEXT): {$(VPATH)}gem_prelude.rb
diff --git a/dir.c b/dir.c
index 5bd4ff4..5c543cc 100644
--- a/dir.c
+++ b/dir.c
@@ -116,6 +116,7 @@ char *strchr(char*,char); https://github.com/ruby/ruby/blob/trunk/dir.c#L116
 #include "ruby/ruby.h"
 #include "ruby/thread.h"
 #include "ruby/util.h"
+#include "builtin.h"
 
 #ifndef AT_FDCWD
 # define AT_FDCWD -1
@@ -520,40 +521,13 @@ opendir_without_gvl(const char *path) https://github.com/ruby/ruby/blob/trunk/dir.c#L521
 	return opendir(path);
 }
 
-/*
- *  call-seq:
- *     Dir.new( string ) -> aDir
- *     Dir.new( string, encoding: enc ) -> aDir
- *
- *  Returns a new directory object for the named directory.
- *
- *  The optional <i>encoding</i> keyword argument specifies the encoding of the directory.
- *  If not specified, the filesystem encoding is used.
- */
 static VALUE
-dir_initialize(int argc, VALUE *argv, VALUE dir)
+dir_initialize(rb_execution_context_t *ec, VALUE dir, VALUE dirname, VALUE enc)
 {
     struct dir_data *dp;
-    rb_encoding  *fsenc;
-    VALUE dirname, opt, orig;
-    static ID keyword_ids[1];
+    VALUE orig;
     const char *path;
-
-    if (!keyword_ids[0]) {
-	keyword_ids[0] = rb_id_encoding();
-    }
-
-    fsenc = rb_filesystem_encoding();
-
-    rb_scan_args(argc, argv, "1:", &dirname, &opt);
-
-    if (!NIL_P(opt)) {
-	VALUE enc;
-	rb_get_kwargs(opt, keyword_ids, 0, 1, &enc);
-	if (enc != Qundef && !NIL_P(enc)) {
-	    fsenc = rb_to_encoding(enc);
-	}
-    }
+    rb_encoding *fsenc = NIL_P(enc) ? rb_filesystem_encoding() : rb_to_encoding(enc);
 
     FilePathValue(dirname);
     orig = rb_str_dup_frozen(dirname);
@@ -591,35 +565,23 @@ dir_initialize(int argc, VALUE *argv, VALUE dir) https://github.com/ruby/ruby/blob/trunk/dir.c#L565
     return dir;
 }
 
-/*
- *  call-seq:
- *     Dir.open( string ) -> aDir
- *     Dir.open( string, encoding: enc ) -> aDir
- *     Dir.open( string ) {| aDir | block } -> anObject
- *     Dir.open( string, encoding: enc ) {| aDir | block } -> anObject
- *
- *  The optional <i>encoding</i> keyword argument specifies the encoding of the directory.
- *  If not specified, the filesystem encoding is used.
- *
- *  With no block, <code>open</code> is a synonym for Dir::new. If a
- *  block is present, it is passed <i>aDir</i> as a parameter. The
- *  directory is closed at the end of the block, and Dir::open returns
- *  the value of the block.
- */
 static VALUE
-dir_s_open(int argc, VALUE *argv, VALUE klass)
+dir_s_open(rb_execution_context_t *ec, VALUE klass, VALUE dirname, VALUE enc)
 {
     struct dir_data *dp;
     VALUE dir = TypedData_Make_Struct(klass, struct dir_data, &dir_data_type, dp);
 
-    dir_initialize(argc, argv, dir);
-    if (rb_block_given_p()) {
-	return rb_ensure(rb_yield, dir, dir_close, dir);
-    }
+    dir_initialize(ec, dir, dirname, enc);
 
     return dir;
 }
 
+static VALUE
+dir_s_close(rb_execution_context_t *ec, VALUE klass, VALUE dir)
+{
+    return dir_close(dir);
+}
+
 NORETURN(static void dir_closed(void));
 
 static void
@@ -3599,13 +3561,11 @@ Init_Dir(void) https://github.com/ruby/ruby/blob/trunk/dir.c#L3561
     rb_include_module(rb_cDir, rb_mEnumerable);
 
     rb_define_alloc_func(rb_cDir, dir_s_alloc);
-    rb_define_singleton_method(rb_cDir, "open", dir_s_open, -1);
     rb_define_singleton_method(rb_cDir, "foreach", dir_foreach, -1);
     rb_define_singleton_method(rb_cDir, "entries", dir_entries, -1);
     rb_define_singleton_method(rb_cDir, "each_child", dir_s_each_child, -1);
     rb_define_singleton_method(rb_cDir, "children", dir_s_children, -1);
 
-    rb_define_method(rb_cDir,"initialize", dir_initialize, -1);
     rb_define_method(rb_cDir,"fileno", dir_fileno, 0);
     rb_define_method(rb_cDir,"path", dir_path, 0);
     rb_define_method(rb_cDir,"to_path", dir_path, 0);
@@ -3687,3 +3647,5 @@ Init_Dir(void) https://github.com/ruby/ruby/blob/trunk/dir.c#L3647
      */
     rb_file_const("FNM_SHORTNAME", INT2FIX(FNM_SHORTNAME));
 }
+
+#include "dir.rbinc"
diff --git a/dir.rb b/dir.rb
new file mode 100644
index 0000000..e383c0e
--- /dev/null
+++ b/dir.rb
@@ -0,0 +1,37 @@ https://github.com/ruby/ruby/blob/trunk/dir.rb#L1
+class Dir
+  #    Dir.open( string ) -> aDir
+  #    Dir.open( string, encoding: enc ) -> aDir
+  #    Dir.open( string ) {| aDir | block } -> anObject
+  #    Dir.open( string, encoding: enc ) {| aDir | block } -> anObject
+  #
+  # The optional <i>encoding</i> keyword argument specifies the encoding of the directory.
+  # If not specified, the filesystem encoding is used.
+  #
+  # With no block, <code>open</code> is a synonym for Dir::new. If a
+  # block is present, it is passed <i>aDir</i> as a parameter. The
+  # directory is closed at the end of the block, and Dir::open returns
+  # the value of the block.
+  def self.open(name, encoding: nil, &block)
+    dir = __builtin_dir_s_open(name, encoding)
+    if block
+      begin
+        yield dir
+      ensure
+        __builtin_dir_s_close(dir)
+      end
+    else
+      dir
+    end
+  end
+
+  #    Dir.new( string ) -> aDir
+  #    Dir.new( string, encoding: enc ) -> aDir
+  #
+  # Returns a new directory object for the named directory.
+  #
+  # The optional <i>encoding</i> keyword argument specifies the encoding of the directory.
+  # If not specified, the filesystem encoding is used.
+  def initialize(name, encoding: nil)
+    __builtin_dir_initialize(name, encoding)
+  end
+end
diff --git a/inits.c b/inits.c
index dae5e1f..92cec6f 100644
--- a/inits.c
+++ b/inits.c
@@ -79,6 +79,7 @@ rb_call_inits(void) https://github.com/ruby/ruby/blob/trunk/inits.c#L79
 #define BUILTIN(n) CALL(builtin_##n)
     BUILTIN(gc);
     BUILTIN(io);
+    BUILTIN(dir);
     BUILTIN(ast);
     BUILTIN(trace_point);
     BUILTIN(pack);
-- 
cgit v0.10.2


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]