From 310054b240a511f888ec5092eb32fed29e4109c9 Mon Sep 17 00:00:00 2001 From: Nobuyoshi Nakada Date: Sat, 18 Jan 2020 13:59:21 +0900 Subject: Moved `Dir.open` and `Dir#initialize` to dir.rb --- .document | 1 + common.mk | 4 ++++ dir.c | 66 ++++++++++++++------------------------------------------------- dir.rb | 37 +++++++++++++++++++++++++++++++++++ inits.c | 1 + 5 files changed, 57 insertions(+), 52 deletions(-) create mode 100644 dir.rb diff --git a/.document b/.document index 1cb2722327..b18ca80971 100644 --- a/.document +++ b/.document @@ -12,6 +12,7 @@ prelude.rb rbconfig.rb array.rb ast.rb +dir.rb gc.rb io.rb kernel.rb diff --git a/common.mk b/common.mk index f1b836fc09..35f19f7253 100644 --- a/common.mk +++ b/common.mk @@ -1000,6 +1000,7 @@ BUILTIN_RB_SRCS = \ $(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 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 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 5bd4ff42af..5c543cca15 100644 --- a/dir.c +++ b/dir.c @@ -116,6 +116,7 @@ char *strchr(char*,char); #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) 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 encoding 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) 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 encoding keyword argument specifies the encoding of the directory. - * If not specified, the filesystem encoding is used. - * - * With no block, open is a synonym for Dir::new. If a - * block is present, it is passed aDir 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) 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) */ rb_file_const("FNM_SHORTNAME", INT2FIX(FNM_SHORTNAME)); } + +#include "dir.rbinc" diff --git a/dir.rb b/dir.rb new file mode 100644 index 0000000000..e383c0ea9f --- /dev/null +++ b/dir.rb @@ -0,0 +1,37 @@ +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 encoding keyword argument specifies the encoding of the directory. + # If not specified, the filesystem encoding is used. + # + # With no block, open is a synonym for Dir::new. If a + # block is present, it is passed aDir 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 encoding 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 dae5e1ff41..92cec6f4b4 100644 --- a/inits.c +++ b/inits.c @@ -79,6 +79,7 @@ rb_call_inits(void) #define BUILTIN(n) CALL(builtin_##n) BUILTIN(gc); BUILTIN(io); + BUILTIN(dir); BUILTIN(ast); BUILTIN(trace_point); BUILTIN(pack); -- cgit v1.2.3