summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorYukihiro "Matz" Matsumoto <[email protected]>2013-03-23 08:30:03 -0700
committerYukihiro "Matz" Matsumoto <[email protected]>2013-03-23 08:30:03 -0700
commit6954153fa06630aa9aaf642f8f1221cddb01793a (patch)
tree20e4d43b0078807f58ca1e2b1a8e716fa614edac
parentf0ac204f21661d7f1dd2fbd1e3f08a95b81d0b21 (diff)
parentb69761e28623c88f142fbff92d7b361d1129ee5f (diff)
downloadmruby-6954153fa06630aa9aaf642f8f1221cddb01793a.tar.gz
mruby-6954153fa06630aa9aaf642f8f1221cddb01793a.zip
Merge pull request #1057 from mattn/mruby-print
mruby-print gem
-rw-r--r--build_config.rb3
-rw-r--r--mrbgems/mruby-print/mrbgem.rake4
-rw-r--r--mrbgems/mruby-print/mrblib/print.rb64
-rw-r--r--mrbgems/mruby-print/src/print.c44
-rw-r--r--mrblib/print.rb84
-rw-r--r--src/init.c4
-rw-r--r--src/print.c23
7 files changed, 126 insertions, 100 deletions
diff --git a/build_config.rb b/build_config.rb
index c3f529656..951f4b7a3 100644
--- a/build_config.rb
+++ b/build_config.rb
@@ -11,6 +11,9 @@ MRuby::Build.new do |conf|
# conf.gem :github => 'masuidrive/mrbgems-example', :branch => 'master'
# conf.gem :git => '[email protected]:masuidrive/mrbgems-example.git', :branch => 'master', :options => '-v'
+ # Use standard print/puts/p
+ conf.gem "#{root}/mrbgems/mruby-print"
+
# Use standard Math module
conf.gem "#{root}/mrbgems/mruby-math"
diff --git a/mrbgems/mruby-print/mrbgem.rake b/mrbgems/mruby-print/mrbgem.rake
new file mode 100644
index 000000000..dc7831280
--- /dev/null
+++ b/mrbgems/mruby-print/mrbgem.rake
@@ -0,0 +1,4 @@
+MRuby::Gem::Specification.new('mruby-print') do |spec|
+ spec.license = 'MIT'
+ spec.authors = 'mruby developers'
+end
diff --git a/mrbgems/mruby-print/mrblib/print.rb b/mrbgems/mruby-print/mrblib/print.rb
new file mode 100644
index 000000000..38a10661b
--- /dev/null
+++ b/mrbgems/mruby-print/mrblib/print.rb
@@ -0,0 +1,64 @@
+##
+# Kernel
+#
+# ISO 15.3.1
+module Kernel
+ ##
+ # Invoke method +print+ on STDOUT and passing +*args+
+ #
+ # ISO 15.3.1.2.10
+ def print(*args)
+ i = 0
+ len = args.size
+ while i < len
+ __printstr__ args[i].to_s
+ i += 1
+ end
+ end
+
+ ##
+ # Invoke method +puts+ on STDOUT and passing +*args*+
+ #
+ # ISO 15.3.1.2.11
+ def puts(*args)
+ i = 0
+ len = args.size
+ while i < len
+ s = args[i].to_s
+ __printstr__ s
+ __printstr__ "\n" if (s[-1] != "\n")
+ i += 1
+ end
+ __printstr__ "\n" if len == 0
+ nil
+ end
+
+ ##
+ # Print human readable object description
+ #
+ # ISO 15.3.1.3.34
+ def p(*args)
+ i = 0
+ len = args.size
+ while i < len
+ __printstr__ args[i].inspect
+ __printstr__ "\n"
+ i += 1
+ end
+ args[0]
+ end
+
+ unless Kernel.respond_to?(:sprintf)
+ def printf(*args)
+ raise NotImplementedError.new('printf not available')
+ end
+ def sprintf(*args)
+ raise NotImplementedError.new('sprintf not available')
+ end
+ else
+ def printf(*args)
+ __printstr__(sprintf(*args))
+ nil
+ end
+ end
+end
diff --git a/mrbgems/mruby-print/src/print.c b/mrbgems/mruby-print/src/print.c
new file mode 100644
index 000000000..608e3cc2d
--- /dev/null
+++ b/mrbgems/mruby-print/src/print.c
@@ -0,0 +1,44 @@
+#include "mruby.h"
+#include "mruby/string.h"
+#include <stdio.h>
+
+static void
+printstr(mrb_state *mrb, mrb_value obj)
+{
+ struct RString *str;
+ char *s;
+ int len;
+
+ if (mrb_string_p(obj)) {
+ str = mrb_str_ptr(obj);
+ s = str->ptr;
+ len = str->len;
+ fwrite(s, len, 1, stdout);
+ }
+}
+
+/* 15.3.1.2.9 */
+/* 15.3.1.3.34 */
+mrb_value
+mrb_printstr(mrb_state *mrb, mrb_value self)
+{
+ mrb_value argv;
+
+ mrb_get_args(mrb, "o", &argv);
+ printstr(mrb, argv);
+
+ return argv;
+}
+
+void
+mrb_mruby_print_gem_init(mrb_state* mrb)
+{
+ struct RClass *krn;
+ krn = mrb->kernel_module;
+ mrb_define_method(mrb, krn, "__printstr__", mrb_printstr, ARGS_REQ(1));
+}
+
+void
+mrb_mruby_print_gem_final(mrb_state* mrb)
+{
+}
diff --git a/mrblib/print.rb b/mrblib/print.rb
index 68c7837f3..1ae3ae84b 100644
--- a/mrblib/print.rb
+++ b/mrblib/print.rb
@@ -3,78 +3,16 @@
#
# ISO 15.3.1
module Kernel
- unless Kernel.respond_to?(:__printstr__)
- def print(*a)
- raise NotImplementedError.new('print not available')
- end
- def puts(*a)
- raise NotImplementedError.new('puts not available')
- end
- def p(*a)
- raise NotImplementedError.new('p not available')
- end
- def printf(*args)
- raise NotImplementedError.new('printf not available')
- end
- else
- unless Kernel.respond_to?(:sprintf)
- def printf(*args)
- raise NotImplementedError.new('printf not available')
- end
- def sprintf(*args)
- raise NotImplementedError.new('sprintf not available')
- end
- end
-
-
- ##
- # Invoke method +print+ on STDOUT and passing +*args+
- #
- # ISO 15.3.1.2.10
- def print(*args)
- i = 0
- len = args.size
- while i < len
- __printstr__ args[i].to_s
- i += 1
- end
- end
-
- ##
- # Invoke method +puts+ on STDOUT and passing +*args*+
- #
- # ISO 15.3.1.2.11
- def puts(*args)
- i = 0
- len = args.size
- while i < len
- s = args[i].to_s
- __printstr__ s
- __printstr__ "\n" if (s[-1] != "\n")
- i += 1
- end
- __printstr__ "\n" if len == 0
- nil
- end
-
- ##
- # Print human readable object description
- #
- # ISO 15.3.1.3.34
- def p(*args)
- i = 0
- len = args.size
- while i < len
- __printstr__ args[i].inspect
- __printstr__ "\n"
- i += 1
- end
- args[0]
- end
-
- def printf(*args)
- __printstr__(sprintf(*args))
- nil
- end
+ def print(*a)
+ raise NotImplementedError.new('print not available')
+ end
+ def puts(*a)
+ raise NotImplementedError.new('puts not available')
+ end
+ def p(*a)
+ raise NotImplementedError.new('p not available')
+ end
+ def printf(*args)
+ raise NotImplementedError.new('printf not available')
end
end
diff --git a/src/init.c b/src/init.c
index 0d1a24881..e97c72d68 100644
--- a/src/init.c
+++ b/src/init.c
@@ -21,7 +21,6 @@ void mrb_init_hash(mrb_state*);
void mrb_init_numeric(mrb_state*);
void mrb_init_range(mrb_state*);
void mrb_init_gc(mrb_state*);
-void mrb_init_print(mrb_state*);
void mrb_init_math(mrb_state*);
void mrb_init_mrblib(mrb_state*);
void mrb_init_mrbgems(mrb_state*);
@@ -48,9 +47,6 @@ mrb_init_core(mrb_state *mrb)
mrb_init_numeric(mrb); DONE;
mrb_init_range(mrb); DONE;
mrb_init_gc(mrb); DONE;
-#ifdef ENABLE_STDIO
- mrb_init_print(mrb); DONE;
-#endif
mrb_init_mrblib(mrb); DONE;
#ifndef DISABLE_GEMS
mrb_init_mrbgems(mrb); DONE;
diff --git a/src/print.c b/src/print.c
index 9d8a751f7..5367781f5 100644
--- a/src/print.c
+++ b/src/print.c
@@ -34,29 +34,6 @@ mrb_p(mrb_state *mrb, mrb_value obj)
#endif
}
-/* 15.3.1.2.9 */
-/* 15.3.1.3.34 */
-mrb_value
-mrb_printstr(mrb_state *mrb, mrb_value self)
-{
- mrb_value argv;
-
- mrb_get_args(mrb, "o", &argv);
- printstr(mrb, argv);
-
- return argv;
-}
-
-void
-mrb_init_print(mrb_state *mrb)
-{
- struct RClass *krn;
-
- krn = mrb->kernel_module;
-
- mrb_define_method(mrb, krn, "__printstr__", mrb_printstr, ARGS_REQ(1));
-}
-
void
mrb_show_version(mrb_state *mrb)
{