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

ruby-changes:59238

From: Nobuyoshi <ko1@a...>
Date: Fri, 13 Dec 2019 23:51:56 +0900 (JST)
Subject: [ruby-changes:59238] 0b5268afbc (master): Moved Kernel#warn to warning.rb

https://git.ruby-lang.org/ruby.git/commit/?id=0b5268afbc

From 0b5268afbcf11c299e11102c366e836ae55cc39f Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Tue, 10 Dec 2019 20:22:42 +0900
Subject: Moved Kernel#warn to warning.rb


diff --git a/common.mk b/common.mk
index b8a3301..debc6d5 100644
--- a/common.mk
+++ b/common.mk
@@ -1001,6 +1001,7 @@ BUILTIN_RB_SRCS = \ https://github.com/ruby/ruby/blob/trunk/common.mk#L1001
 		$(srcdir)/io.rb \
 		$(srcdir)/pack.rb \
 		$(srcdir)/trace_point.rb \
+		$(srcdir)/warning.rb \
 		$(srcdir)/prelude.rb \
 		$(srcdir)/gem_prelude.rb \
 		$(empty)
@@ -1974,6 +1975,7 @@ error.$(OBJEXT): $(CCAN_DIR)/str/str.h https://github.com/ruby/ruby/blob/trunk/common.mk#L1975
 error.$(OBJEXT): $(hdrdir)/ruby.h
 error.$(OBJEXT): $(hdrdir)/ruby/ruby.h
 error.$(OBJEXT): {$(VPATH)}assert.h
+error.$(OBJEXT): {$(VPATH)}builtin.h
 error.$(OBJEXT): {$(VPATH)}config.h
 error.$(OBJEXT): {$(VPATH)}defines.h
 error.$(OBJEXT): {$(VPATH)}encoding.h
@@ -1995,6 +1997,7 @@ error.$(OBJEXT): {$(VPATH)}thread_$(THREAD_MODEL).h https://github.com/ruby/ruby/blob/trunk/common.mk#L1997
 error.$(OBJEXT): {$(VPATH)}thread_native.h
 error.$(OBJEXT): {$(VPATH)}vm_core.h
 error.$(OBJEXT): {$(VPATH)}vm_opts.h
+error.$(OBJEXT): {$(VPATH)}warning.rbinc
 eval.$(OBJEXT): $(CCAN_DIR)/check_type/check_type.h
 eval.$(OBJEXT): $(CCAN_DIR)/container_of/container_of.h
 eval.$(OBJEXT): $(CCAN_DIR)/list/list.h
@@ -2385,6 +2388,7 @@ miniinit.$(OBJEXT): {$(VPATH)}thread_native.h https://github.com/ruby/ruby/blob/trunk/common.mk#L2388
 miniinit.$(OBJEXT): {$(VPATH)}trace_point.rb
 miniinit.$(OBJEXT): {$(VPATH)}vm_core.h
 miniinit.$(OBJEXT): {$(VPATH)}vm_opts.h
+miniinit.$(OBJEXT): {$(VPATH)}warning.rb
 mjit.$(OBJEXT): $(CCAN_DIR)/check_type/check_type.h
 mjit.$(OBJEXT): $(CCAN_DIR)/container_of/container_of.h
 mjit.$(OBJEXT): $(CCAN_DIR)/list/list.h
diff --git a/error.c b/error.c
index 13d1910..14114fc 100644
--- a/error.c
+++ b/error.c
@@ -14,6 +14,7 @@ https://github.com/ruby/ruby/blob/trunk/error.c#L14
 #include "internal.h"
 #include "ruby_assert.h"
 #include "vm_core.h"
+#include "builtin.h"
 
 #include <stdio.h>
 #include <stdarg.h>
@@ -291,73 +292,27 @@ warning_write(int argc, VALUE *argv, VALUE buf) https://github.com/ruby/ruby/blob/trunk/error.c#L292
     return buf;
 }
 
-/*
- * call-seq:
- *    warn(*msgs, uplevel: nil)   -> nil
- *
- * If warnings have been disabled (for example with the
- * <code>-W0</code> flag), does nothing.  Otherwise,
- * converts each of the messages to strings, appends a newline
- * character to the string if the string does not end in a newline,
- * and calls Warning.warn with the string.
- *
- *    warn("warning 1", "warning 2")
- *
- *  <em>produces:</em>
- *
- *    warning 1
- *    warning 2
- *
- * If the <code>uplevel</code> keyword argument is given, the string will
- * be prepended with information for the given caller frame in
- * the same format used by the <code>rb_warn</code> C function.
- *
- *    # In baz.rb
- *    def foo
- *      warn("invalid call to foo", uplevel: 1)
- *    end
- *
- *    def bar
- *      foo
- *    end
- *
- *    bar
- *
- *  <em>produces:</em>
- *
- *    baz.rb:6: warning: invalid call to foo
- */
-
 static VALUE
-rb_warn_m(int argc, VALUE *argv, VALUE exc)
-{
-    VALUE opts, location = Qnil;
-
-    if (!NIL_P(ruby_verbose) && argc > 0 &&
-            (argc = rb_scan_args(argc, argv, "*:", NULL, &opts)) > 0) {
-	VALUE str = argv[0], uplevel = Qnil;
-	if (!NIL_P(opts)) {
-	    static ID kwds[1];
-	    if (!kwds[0]) {
-		CONST_ID(kwds[0], "uplevel");
-	    }
-	    rb_get_kwargs(opts, kwds, 0, 1, &uplevel);
-	    if (uplevel == Qundef) {
-		uplevel = Qnil;
-	    }
-	    else if (!NIL_P(uplevel)) {
-		VALUE args[2];
-		long lev = NUM2LONG(uplevel);
-		if (lev < 0) {
-		    rb_raise(rb_eArgError, "negative level (%ld)", lev);
-		}
-		args[0] = LONG2NUM(lev + 1);
-		args[1] = INT2FIX(1);
-		location = rb_vm_thread_backtrace_locations(2, args, GET_THREAD()->self);
-		if (!NIL_P(location)) {
-		    location = rb_ary_entry(location, 0);
-		}
-	    }
+rb_warn_m(rb_execution_context_t *ec, VALUE exc, VALUE msgs, VALUE uplevel)
+{
+    VALUE location = Qnil;
+    int argc = RARRAY_LENINT(msgs);
+    const VALUE *argv = RARRAY_CONST_PTR(msgs);
+
+    if (!NIL_P(ruby_verbose) && argc > 0) {
+        VALUE str = argv[0];
+        if (!NIL_P(uplevel)) {
+            VALUE args[2];
+            long lev = NUM2LONG(uplevel);
+            if (lev < 0) {
+                rb_raise(rb_eArgError, "negative level (%ld)", lev);
+            }
+            args[0] = LONG2NUM(lev + 1);
+            args[1] = INT2FIX(1);
+            location = rb_vm_thread_backtrace_locations(2, args, GET_THREAD()->self);
+            if (!NIL_P(location)) {
+                location = rb_ary_entry(location, 0);
+            }
 	}
 	if (argc > 1 || !NIL_P(uplevel) || !end_with_asciichar(str, '\n')) {
 	    VALUE path;
@@ -2555,8 +2510,6 @@ Init_Exception(void) https://github.com/ruby/ruby/blob/trunk/error.c#L2510
     rb_cWarningBuffer = rb_define_class_under(rb_mWarning, "buffer", rb_cString);
     rb_define_method(rb_cWarningBuffer, "write", warning_write, -1);
 
-    rb_define_global_function("warn", rb_warn_m, -1);
-
     id_cause = rb_intern_const("cause");
     id_message = rb_intern_const("message");
     id_backtrace = rb_intern_const("backtrace");
@@ -2991,6 +2944,14 @@ Init_syserr(void) https://github.com/ruby/ruby/blob/trunk/error.c#L2944
 #undef undefined_error
 }
 
+#include "warning.rbinc"
+
+void
+Init_warning(void)
+{
+    load_warning();
+}
+
 /*!
  * \}
  */
diff --git a/inits.c b/inits.c
index 490506e..79a6cf0 100644
--- a/inits.c
+++ b/inits.c
@@ -76,6 +76,7 @@ rb_call_inits(void) https://github.com/ruby/ruby/blob/trunk/inits.c#L76
     CALL(ast);
     CALL(vm_trace);
     CALL(pack);
+    CALL(warning);
     load_prelude();
 }
 #undef CALL
diff --git a/warning.rb b/warning.rb
new file mode 100644
index 0000000..8361176
--- /dev/null
+++ b/warning.rb
@@ -0,0 +1,44 @@ https://github.com/ruby/ruby/blob/trunk/warning.rb#L1
+# encoding: utf-8
+# fronzen-string-literal: true
+
+module Kernel
+
+  # call-seq:
+  #    warn(*msgs, uplevel: nil)   -> nil
+  #
+  # If warnings have been disabled (for example with the
+  # <code>-W0</code> flag), does nothing.  Otherwise,
+  # converts each of the messages to strings, appends a newline
+  # character to the string if the string does not end in a newline,
+  # and calls Warning.warn with the string.
+  #
+  #    warn("warning 1", "warning 2")
+  #
+  #  <em>produces:</em>
+  #
+  #    warning 1
+  #    warning 2
+  #
+  # If the <code>uplevel</code> keyword argument is given, the string will
+  # be prepended with information for the given caller frame in
+  # the same format used by the <code>rb_warn</code> C function.
+  #
+  #    # In baz.rb
+  #    def foo
+  #      warn("invalid call to foo", uplevel: 1)
+  #    end
+  #
+  #    def bar
+  #      foo
+  #    end
+  #
+  #    bar
+  #
+  #  <em>produces:</em>
+  #
+  #    baz.rb:6: warning: invalid call to foo
+  #
+  def warn(*msgs, uplevel: nil)
+    __builtin_rb_warn_m(msgs, uplevel)
+  end
+end
-- 
cgit v0.10.2


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

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