From d060c8a713cbfd356fd3814339bf6248d76f3507 Mon Sep 17 00:00:00 2001 From: Yukihiro Matsumoto Date: Sat, 2 Jun 2012 23:21:12 +0900 Subject: add Hash#{select/reject} to return Hash as 1.9 --- mrblib/hash.rb | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) (limited to 'mrblib') diff --git a/mrblib/hash.rb b/mrblib/hash.rb index d6ad55e47..dc85c8f5e 100644 --- a/mrblib/hash.rb +++ b/mrblib/hash.rb @@ -80,6 +80,62 @@ class Hash end h end + + # 1.8/1.9 Hash#reject! returns Hash; ISO says nothing. + def reject!(&b) + keys = [] + self.each_key{|k| + v = self[k] + if b.call(k, v) + keys.push(k) + end + } + return nil if keys.size == 0 + keys.each{|k| + self.delete(k) + } + self + end + + # 1.8/1.9 Hash#reject returns Hash; ISO says nothing. + def reject(&b) + h = {} + self.each_key{|k| + v = self[k] + unless b.call(k, v) + h[k] = v + end + } + h + end + + # 1.9 Hash#select! returns Hash; ISO says nothing. + def reject!(&b) + keys = [] + self.each_key{|k| + v = self[k] + unless b.call(k, v) + keys.push(k) + end + } + return nil if keys.size == 0 + keys.each{|k| + self.delete(k) + } + self + end + + # 1.9 Hash#select returns Hash; ISO says nothing. + def select(&b) + h = {} + self.each_key{|k| + v = self[k] + if b.call(k, v) + h[k] = v + end + } + h + end end ## -- cgit v1.2.3 From 15b9d74fc7d043a6e62eecbbca14a4c2f89bfcb6 Mon Sep 17 00:00:00 2001 From: Yukihiro Matsumoto Date: Sun, 3 Jun 2012 02:14:30 +0900 Subject: stupid naming error --- mrblib/hash.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'mrblib') diff --git a/mrblib/hash.rb b/mrblib/hash.rb index dc85c8f5e..7b7b3f8ac 100644 --- a/mrblib/hash.rb +++ b/mrblib/hash.rb @@ -110,7 +110,7 @@ class Hash end # 1.9 Hash#select! returns Hash; ISO says nothing. - def reject!(&b) + def select!(&b) keys = [] self.each_key{|k| v = self[k] -- cgit v1.2.3 From 479df23bc0985d0050c27c87285291b43b8bcfbc Mon Sep 17 00:00:00 2001 From: Yukihiro Matsumoto Date: Sun, 3 Jun 2012 15:58:15 +0900 Subject: puts should return nil; close #215 --- mrblib/print.rb | 1 + 1 file changed, 1 insertion(+) (limited to 'mrblib') diff --git a/mrblib/print.rb b/mrblib/print.rb index 52beb26cb..0d33c66d2 100644 --- a/mrblib/print.rb +++ b/mrblib/print.rb @@ -30,6 +30,7 @@ module Kernel i += 1 end __printstr__ "\n" if len == 0 + nil end ## -- cgit v1.2.3 From b5ffbea4c2f914409465aa87d906f12b5a57972e Mon Sep 17 00:00:00 2001 From: Yukihiro Matsumoto Date: Tue, 12 Jun 2012 18:32:42 +0900 Subject: remove Ruby version of Kernel#instance_eval --- mrblib/kernel.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'mrblib') diff --git a/mrblib/kernel.rb b/mrblib/kernel.rb index e769741b7..f5099c561 100644 --- a/mrblib/kernel.rb +++ b/mrblib/kernel.rb @@ -33,10 +33,10 @@ module Kernel #end # 15.3.1.3.18 - def instance_eval(string=nil, &block) - ### *** TODO *** ### - raise "Not implemented yet" - end + #def instance_eval(string=nil, &block) + # ### *** TODO *** ### + # raise "Not implemented yet" + #end ## # Alias for +Kernel.lambda+. -- cgit v1.2.3 From a74ab0c2daf1855b968ef8498b9161eadb04a386 Mon Sep 17 00:00:00 2001 From: Yukihiro Matsumoto Date: Tue, 12 Jun 2012 23:39:54 +0900 Subject: Kernel.eval to raise NotImplementedError --- mrblib/error.rb | 7 +++++++ mrblib/kernel.rb | 14 +++++++++----- 2 files changed, 16 insertions(+), 5 deletions(-) (limited to 'mrblib') diff --git a/mrblib/error.rb b/mrblib/error.rb index 5660d8235..3fa7f21e3 100644 --- a/mrblib/error.rb +++ b/mrblib/error.rb @@ -12,3 +12,10 @@ class Exception self.new(*args, &block) end end + +# ISO 15.2.37 +class ScriptError < Exception +end + +class NotImplementedError < ScriptError +end diff --git a/mrblib/kernel.rb b/mrblib/kernel.rb index f5099c561..c263ec403 100644 --- a/mrblib/kernel.rb +++ b/mrblib/kernel.rb @@ -24,6 +24,11 @@ module Kernel end end + # 15.3.1.2.3 + def self.eval(s) + raise NotImplementedError.new("eval not implemented") + end + ## # Alias for +send+. # @@ -32,11 +37,10 @@ module Kernel ### *** TODO *** ### #end - # 15.3.1.3.18 - #def instance_eval(string=nil, &block) - # ### *** TODO *** ### - # raise "Not implemented yet" - #end + # 15.3.1.3.12 + def eval(s) + Kernel.eval(s) + end ## # Alias for +Kernel.lambda+. -- cgit v1.2.3 From cfd5f5157d06f9c62a04660843c3c02014d8a3a5 Mon Sep 17 00:00:00 2001 From: Masaki Muranaka Date: Fri, 15 Jun 2012 13:06:43 +0900 Subject: Move Subclasses of ScriptError to mrblib. --- mrblib/error.rb | 8 ++++++++ src/error.c | 3 --- test/t/exception.rb | 30 ++++++++++++++++++++++++++++++ 3 files changed, 38 insertions(+), 3 deletions(-) (limited to 'mrblib') diff --git a/mrblib/error.rb b/mrblib/error.rb index 3fa7f21e3..1cb2b1150 100644 --- a/mrblib/error.rb +++ b/mrblib/error.rb @@ -17,5 +17,13 @@ end class ScriptError < Exception end +# ISO 15.2.38 +class SyntaxError < ScriptError +end + +# ISO 15.2.39 +class LoadError < ScriptError +end + class NotImplementedError < ScriptError end diff --git a/src/error.c b/src/error.c index 548527f07..590fad5a5 100644 --- a/src/error.c +++ b/src/error.c @@ -385,9 +385,6 @@ mrb_init_exception(mrb_state *mrb) eNameError = mrb_define_class(mrb, "NameError", mrb->eStandardError_class); /* 15.2.31 */ mrb_define_class(mrb, "NoMethodError", eNameError); /* 15.2.32 */ - // eScriptError = mrb_define_class(mrb, "ScriptError", mrb->eException_class); /* 15.2.37 */ - // mrb_define_class(mrb, "SyntaxError", eScriptError); /* 15.2.38 */ - // mrb_define_class(mrb, "LoadError", eScriptError); /* 15.2.39 */ // mrb_define_class(mrb, "SystemCallError", mrb->eStandardError_class); /* 15.2.36 */ mrb_define_class(mrb, "LocalJumpError", mrb->eStandardError_class); /* 15.2.25 */ diff --git a/test/t/exception.rb b/test/t/exception.rb index 22795161f..d7226a368 100644 --- a/test/t/exception.rb +++ b/test/t/exception.rb @@ -41,6 +41,36 @@ assert('Exception.exception', '15.2.22.4.1') do e.message == 'a' end +assert('ScriptError', '15.2.37') do + begin + raise ScriptError.new + rescue ScriptError + true + else + false + end +end + +assert('SyntaxError', '15.2.38') do + begin + raise SyntaxError.new + rescue SyntaxError + true + else + false + end +end + +assert('LoadError', '15.2.39') do + begin + raise LoadError.new + rescue LoadError + true + else + false + end +end + # Not ISO specified assert('Exception 1') do -- cgit v1.2.3 From 39f47c191557ca401b6aaceb04b395227431b62f Mon Sep 17 00:00:00 2001 From: Yukihiro Matsumoto Date: Fri, 15 Jun 2012 13:55:27 +0900 Subject: remove LoadError --- mrblib/error.rb | 4 ---- test/t/exception.rb | 12 +----------- 2 files changed, 1 insertion(+), 15 deletions(-) (limited to 'mrblib') diff --git a/mrblib/error.rb b/mrblib/error.rb index 1cb2b1150..5d49ec1e4 100644 --- a/mrblib/error.rb +++ b/mrblib/error.rb @@ -21,9 +21,5 @@ end class SyntaxError < ScriptError end -# ISO 15.2.39 -class LoadError < ScriptError -end - class NotImplementedError < ScriptError end diff --git a/test/t/exception.rb b/test/t/exception.rb index d7226a368..0aed0e2e6 100644 --- a/test/t/exception.rb +++ b/test/t/exception.rb @@ -24,7 +24,7 @@ end assert('Exception#message', '15.2.22.5.2') do e = Exception.exception('a') - + e.message == 'a' end @@ -61,16 +61,6 @@ assert('SyntaxError', '15.2.38') do end end -assert('LoadError', '15.2.39') do - begin - raise LoadError.new - rescue LoadError - true - else - false - end -end - # Not ISO specified assert('Exception 1') do -- cgit v1.2.3 From 955a48d964a2bbe175617880c868ca8b862da74e Mon Sep 17 00:00:00 2001 From: Yukihiro Matsumoto Date: Mon, 18 Jun 2012 10:30:17 +0900 Subject: move exception definiton to mrblib --- mrblib/error.rb | 39 +++++++++++++++++++++++++++++++++++++++ src/error.c | 25 ------------------------- 2 files changed, 39 insertions(+), 25 deletions(-) (limited to 'mrblib') diff --git a/mrblib/error.rb b/mrblib/error.rb index 5d49ec1e4..16e88eefb 100644 --- a/mrblib/error.rb +++ b/mrblib/error.rb @@ -13,6 +13,44 @@ class Exception end end +# ISO 15.2.24 +class ArgumentError < StandardError +end + +# ISO 15.2.25 +class LocalJumpError < StandardError +end + +# ISO 15.2.26 +class RangeError < StandardError +end + +class FloatDomainError < RangeError +end + +# ISO 15.2.26 +class RegexpError < StandardError +end + +# ISO 15.2.29 +class TypeError < StandardError +end + +# ISO 15.2.31 +class NameError < StandardError +end + +# ISO 15.2.32 +class NoMethodError < NameError +end + +# ISO 15.2.33 +class IndexError < StandardError +end + +class KeyError < IndexError +end + # ISO 15.2.37 class ScriptError < Exception end @@ -23,3 +61,4 @@ end class NotImplementedError < ScriptError end + diff --git a/src/error.c b/src/error.c index 1d8d2ddeb..981b2abab 100644 --- a/src/error.c +++ b/src/error.c @@ -359,9 +359,6 @@ void mrb_init_exception(mrb_state *mrb) { struct RClass *e; - struct RClass *eIndexError; - struct RClass *eRangeError; - struct RClass *eNameError; mrb->eException_class = e = mrb_define_class(mrb, "Exception", mrb->object_class); /* 15.2.22 */ mrb_define_class_method(mrb, e, "exception", mrb_instance_new, ARGS_ANY()); @@ -374,26 +371,4 @@ mrb_init_exception(mrb_state *mrb) mrb->eStandardError_class = mrb_define_class(mrb, "StandardError", mrb->eException_class); /* 15.2.23 */ mrb->eRuntimeError_class = mrb_define_class(mrb, "RuntimeError", mrb->eStandardError_class); /* 15.2.28 */ - - mrb_define_class(mrb, "TypeError", mrb->eStandardError_class); /* 15.2.29 */ - mrb_define_class(mrb, "ArgumentError", mrb->eStandardError_class); /* 15.2.24 */ - eIndexError = mrb_define_class(mrb, "IndexError", mrb->eStandardError_class); /* 15.2.33 */ - eRangeError = mrb_define_class(mrb, "RangeError", mrb->eStandardError_class); /* 15.2.26 */ - eNameError = mrb_define_class(mrb, "NameError", mrb->eStandardError_class); /* 15.2.31 */ - - mrb_define_class(mrb, "NoMethodError", eNameError); /* 15.2.32 */ - // mrb_define_class(mrb, "SystemCallError", mrb->eStandardError_class); /* 15.2.36 */ - mrb_define_class(mrb, "LocalJumpError", mrb->eStandardError_class); /* 15.2.25 */ - -#ifdef INCLUDE_REGEX - mrb_define_class(mrb, "RegexpError", mrb->eStandardError_class); /* 15.2.27 */ -#endif - -#ifdef INCLUDE_ENCODING - mrb_define_class(mrb, "EncodingError", mrb->eStandardError_class); -#endif - // mrb_define_class(mrb, "ZeroDivisionError", mrb->eStandardError_class); /* 15.2.30 */ - - mrb_define_class(mrb, "FloatDomainError", eRangeError); - mrb_define_class(mrb, "KeyError", eIndexError); } -- cgit v1.2.3 From 83e5999d7efcad648e9ecbd64c51b305b6261999 Mon Sep 17 00:00:00 2001 From: Daniel Bovensiepen Date: Mon, 18 Jun 2012 11:22:05 +0800 Subject: Make CFLAG choices in Makefiles more flexible --- Makefile | 17 ++++++++++++----- mrblib/Makefile | 17 ++++++++++++----- src/Makefile | 17 ++++++++++++----- test/Makefile | 17 ++++++++++++----- tools/mirb/Makefile | 13 ++++++++++--- tools/mrbc/Makefile | 17 ++++++++++++----- tools/mruby/Makefile | 17 ++++++++++++----- 7 files changed, 82 insertions(+), 33 deletions(-) (limited to 'mrblib') diff --git a/Makefile b/Makefile index e7408d793..a960fc14a 100644 --- a/Makefile +++ b/Makefile @@ -7,12 +7,19 @@ export LL = gcc export AR = ar export YACC = bison -DEBUG_MODE = 1 -ifeq ($(DEBUG_MODE),1) -CFLAGS = -g -O3 -else -CFLAGS = -O3 +ifeq ($(strip $(COMPILE_MODE)),) + # default compile option + COMPILE_MODE = debug +endif + +ifeq ($(COMPILE_MODE),debug) + CFLAGS = -g -O3 +else ifeq ($(COMPILE_MODE),release) + CFLAGS = -O3 +else ifeq ($(COMPILE_MODE),small) + CFLAGS = -Os endif + ALL_CFLAGS = -Wall -Werror-implicit-function-declaration $(CFLAGS) ifeq ($(OS),Windows_NT) MAKE_FLAGS = --no-print-directory CC=$(CC) LL=$(LL) ALL_CFLAGS='$(ALL_CFLAGS)' diff --git a/mrblib/Makefile b/mrblib/Makefile index c7226ddcd..01a5a6198 100644 --- a/mrblib/Makefile +++ b/mrblib/Makefile @@ -18,12 +18,19 @@ LIBR := ../lib/libmruby.a # libraries, includes INCLUDES = -I../src -I../include -DEBUG_MODE = 1 -ifeq ($(DEBUG_MODE),1) -CFLAGS = -g -else -CFLAGS = -O3 +ifeq ($(strip $(COMPILE_MODE)),) + # default compile option + COMPILE_MODE = debug +endif + +ifeq ($(COMPILE_MODE),debug) + CFLAGS = -g -O3 +else ifeq ($(COMPILE_MODE),release) + CFLAGS = -O3 +else ifeq ($(COMPILE_MODE),small) + CFLAGS = -Os endif + ALL_CFLAGS = -Wall -Werror-implicit-function-declaration $(CFLAGS) ifeq ($(OS),Windows_NT) MAKE_FLAGS = CC=$(CC) LL=$(LL) ALL_CFLAGS="$(ALL_CFLAGS)" diff --git a/src/Makefile b/src/Makefile index 61012ea68..13f80b694 100644 --- a/src/Makefile +++ b/src/Makefile @@ -19,12 +19,19 @@ OBJS := $(OBJ1) $(OBJ2) $(OBJ3) # libraries, includes INCLUDES = -I$(BASEDIR) -I$(BASEDIR)/../include -DEBUG_MODE = 1 -ifeq ($(DEBUG_MODE),1) -CFLAGS = -g -O3 -else -CFLAGS = -O3 +ifeq ($(strip $(COMPILE_MODE)),) + # default compile option + COMPILE_MODE = debug endif + +ifeq ($(COMPILE_MODE),debug) + CFLAGS = -g -O3 +else ifeq ($(COMPILE_MODE),release) + CFLAGS = -O3 +else ifeq ($(COMPILE_MODE),small) + CFLAGS = -Os +endif + ALL_CFLAGS = -Wall -Werror-implicit-function-declaration $(CFLAGS) diff --git a/test/Makefile b/test/Makefile index 170c1dac8..921442b28 100644 --- a/test/Makefile +++ b/test/Makefile @@ -20,12 +20,19 @@ OBJS := driver.o $(MLIB) LIBS = -lm INCLUDES = -I$(BASEDIR)/../src -I$(BASEDIR)/../include -DEBUG_MODE = 1 -ifeq ($(DEBUG_MODE),1) -CFLAGS = -g -else -CFLAGS = -O3 +ifeq ($(strip $(COMPILE_MODE)),) + # default compile option + COMPILE_MODE = debug +endif + +ifeq ($(COMPILE_MODE),debug) + CFLAGS = -g -O3 +else ifeq ($(COMPILE_MODE),release) + CFLAGS = -O3 +else ifeq ($(COMPILE_MODE),small) + CFLAGS = -Os endif + ALL_CFLAGS = -Wall -Werror-implicit-function-declaration $(CFLAGS) ifeq ($(OS),Windows_NT) MAKE_FLAGS = CC=$(CC) LL=$(LL) ALL_CFLAGS="$(ALL_CFLAGS)" diff --git a/tools/mirb/Makefile b/tools/mirb/Makefile index ba307227c..52941f242 100644 --- a/tools/mirb/Makefile +++ b/tools/mirb/Makefile @@ -21,12 +21,19 @@ EXTS := $(EXT1) LIBS = -lm INCLUDES = -I$(BASEDIR) -I$(BASEDIR)/../include -DEBUG_MODE = 1 -ifeq ($(DEBUG_MODE),1) +ifeq ($(strip $(COMPILE_MODE)),) + # default compile option + COMPILE_MODE = debug +endif + +ifeq ($(COMPILE_MODE),debug) CFLAGS = -g -O3 -else +else ifeq ($(COMPILE_MODE),release) CFLAGS = -O3 +else ifeq ($(COMPILE_MODE),small) + CFLAGS = -Os endif + ALL_CFLAGS = -Wall -Werror-implicit-function-declaration $(CFLAGS) ifeq ($(OS),Windows_NT) MAKE_FLAGS = CC=$(CC) LL=$(LL) ALL_CFLAGS="$(ALL_CFLAGS)" diff --git a/tools/mrbc/Makefile b/tools/mrbc/Makefile index 99f5830e6..eea0c02cb 100644 --- a/tools/mrbc/Makefile +++ b/tools/mrbc/Makefile @@ -23,12 +23,19 @@ LIBS = -lm INCLUDES = -I$(BASEDIR) -I$(BASEDIR)/../include # compiler, linker (gcc) -DEBUG_MODE = 1 -ifeq ($(DEBUG_MODE),1) -CFLAGS = -g -O3 -else -CFLAGS = -O3 +ifeq ($(strip $(COMPILE_MODE)),) + # default compile option + COMPILE_MODE = debug +endif + +ifeq ($(COMPILE_MODE),debug) + CFLAGS = -g -O3 +else ifeq ($(COMPILE_MODE),release) + CFLAGS = -O3 +else ifeq ($(COMPILE_MODE),small) + CFLAGS = -Os endif + ALL_CFLAGS = -Wall -Werror-implicit-function-declaration $(CFLAGS) ifeq ($(OS),Windows_NT) MAKE_FLAGS = CC=$(CC) LL=$(LL) ALL_CFLAGS="$(ALL_CFLAGS)" diff --git a/tools/mruby/Makefile b/tools/mruby/Makefile index 0442bd422..9955b4302 100644 --- a/tools/mruby/Makefile +++ b/tools/mruby/Makefile @@ -26,12 +26,19 @@ LIBS = -lm INCLUDES = -I$(BASEDIR) -I$(BASEDIR)/../include # compiler, linker (gcc) -DEBUG_MODE = 1 -ifeq ($(DEBUG_MODE),1) -CFLAGS = -g -O3 -else -CFLAGS = -O3 +ifeq ($(strip $(COMPILE_MODE)),) + # default compile option + COMPILE_MODE = debug +endif + +ifeq ($(COMPILE_MODE),debug) + CFLAGS = -g -O3 +else ifeq ($(COMPILE_MODE),release) + CFLAGS = -O3 +else ifeq ($(COMPILE_MODE),small) + CFLAGS = -Os endif + ALL_CFLAGS = -Wall -Werror-implicit-function-declaration $(CFLAGS) ifeq ($(OS),Windows_NT) MAKE_FLAGS = CC=$(CC) LL=$(LL) ALL_CFLAGS="$(ALL_CFLAGS)" -- cgit v1.2.3 From 97e5ab22f66d0b70643ac2b6d76aef55a5122746 Mon Sep 17 00:00:00 2001 From: Yukihiro Matsumoto Date: Wed, 20 Jun 2012 13:44:32 +0900 Subject: add Module#{attr,attr_reader,attr_writer,attr_accessor}; close #257 --- mrblib/class.rb | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 mrblib/class.rb (limited to 'mrblib') diff --git a/mrblib/class.rb b/mrblib/class.rb new file mode 100644 index 000000000..9c3ef91fd --- /dev/null +++ b/mrblib/class.rb @@ -0,0 +1,24 @@ +class Module + # 15.2.2.4.13 + def attr_reader(*names) + names.each{|name| + define_method(name){self.instance_variable_get(name)} + } + end + # 15.2.2.4.14 + def attr_writer(*names) + names.each{|name| + aset = (name.to_s+"=").intern + define_method(aset){|v|self.instance_variable_set(name,v)} + } + end + # 15.2.2.4.12 + def attr_accessor(*names) + attr_reader(*names) + attr_writer(*names) + end + # 15.2.2.4.11 + def attr(name) + attr_reader(name) + end +end -- cgit v1.2.3 From b11d4647e91bdcd6dfaecfaccdc4c350b1bc413f Mon Sep 17 00:00:00 2001 From: Yukihiro Matsumoto Date: Wed, 20 Jun 2012 15:41:22 +0900 Subject: ISO conforming lambda --- mrblib/kernel.rb | 20 -------------------- src/kernel.c | 18 ------------------ src/proc.c | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 38 deletions(-) (limited to 'mrblib') diff --git a/mrblib/kernel.rb b/mrblib/kernel.rb index c263ec403..f29a80973 100644 --- a/mrblib/kernel.rb +++ b/mrblib/kernel.rb @@ -3,17 +3,6 @@ # # ISO 15.3.1 module Kernel - - ## - # Takes the given block, create a lambda - # out of it and +call+ it. - # - # ISO 15.3.1.2.6 - def self.lambda(&block) - ### *** TODO *** ### - block # dummy - end - ## # Calls the given block repetitively. # @@ -42,15 +31,6 @@ module Kernel Kernel.eval(s) end - ## - # Alias for +Kernel.lambda+. - # - # ISO 15.3.1.3.27 - def lambda(&block) - ### *** TODO *** ### - block # dummy - end - ## # Alias for +Kernel.loop+. # diff --git a/src/kernel.c b/src/kernel.c index b5bde628a..45eda6d2f 100644 --- a/src/kernel.c +++ b/src/kernel.c @@ -794,22 +794,6 @@ mrb_obj_is_kind_of_m(mrb_state *mrb, mrb_value self) } } -/* 15.3.1.2.6 */ -/* 15.3.1.3.27 */ -/* - * call-seq: - * lambda { |...| block } -> a_proc - * - * Equivalent to Proc.new, except the resulting Proc objects - * check the number of parameters passed when called. - */ -mrb_value -proc_lambda(mrb_state *mrb, mrb_value self) -{ - //return mrb_block_lambda(); - return mrb_nil_value(); /* dummy */ -} - static void method_entry_loop(mrb_state *mrb, struct RClass* klass, mrb_value ary) { @@ -1205,7 +1189,6 @@ mrb_init_kernel(mrb_state *mrb) mrb_define_class_method(mrb, krn, "block_given?", mrb_f_block_given_p_m, ARGS_NONE()); /* 15.3.1.2.2 */ mrb_define_class_method(mrb, krn, "global_variables", mrb_f_global_variables, ARGS_NONE()); /* 15.3.1.2.4 */ mrb_define_class_method(mrb, krn, "iterator?", mrb_f_block_given_p_m, ARGS_NONE()); /* 15.3.1.2.5 */ - mrb_define_class_method(mrb, krn, "lambda", proc_lambda, ARGS_NONE()); /* 15.3.1.2.6 */ ; /* 15.3.1.2.11 */ mrb_define_class_method(mrb, krn, "raise", mrb_f_raise, ARGS_ANY()); /* 15.3.1.2.12 */ @@ -1236,7 +1219,6 @@ mrb_init_kernel(mrb_state *mrb) mrb_define_method(mrb, krn, "is_a?", mrb_obj_is_kind_of_m, ARGS_REQ(1)); /* 15.3.1.3.24 */ mrb_define_method(mrb, krn, "iterator?", mrb_f_block_given_p_m, ARGS_NONE()); /* 15.3.1.3.25 */ mrb_define_method(mrb, krn, "kind_of?", mrb_obj_is_kind_of_m, ARGS_REQ(1)); /* 15.3.1.3.26 */ - mrb_define_method(mrb, krn, "lambda", proc_lambda, ARGS_NONE()); /* 15.3.1.3.27 */ mrb_define_method(mrb, krn, "methods", mrb_obj_methods_m, ARGS_ANY()); /* 15.3.1.3.31 */ mrb_define_method(mrb, krn, "nil?", mrb_false, ARGS_NONE()); /* 15.3.1.3.32 */ mrb_define_method(mrb, krn, "object_id", mrb_obj_id_m, ARGS_NONE()); /* 15.3.1.3.33 */ diff --git a/src/proc.c b/src/proc.c index c64bb88ac..98f753ac6 100644 --- a/src/proc.c +++ b/src/proc.c @@ -59,6 +59,7 @@ mrb_proc_new_cfunc(mrb_state *mrb, mrb_func_t func) static inline void proc_copy(struct RProc *a, struct RProc *b) { + a->flags = b->flags; a->body = b->body; a->target_class = b->target_class; a->env = b->env; @@ -111,6 +112,35 @@ mrb_proc_iseq(mrb_state *mrb, struct RProc *p) return p->body.irep->iseq; } +/* 15.3.1.2.6 */ +/* 15.3.1.3.27 */ +/* + * call-seq: + * lambda { |...| block } -> a_proc + * + * Equivalent to Proc.new, except the resulting Proc objects + * check the number of parameters passed when called. + */ +static mrb_value +proc_lambda(mrb_state *mrb, mrb_value self) +{ + mrb_value blk; + struct RProc *p; + + mrb_get_args(mrb, "&", &blk); + if (mrb_nil_p(blk)) { + mrb_raise(mrb, E_ARGUMENT_ERROR, "tried to create Proc object without a block"); + } + p = mrb_proc_ptr(blk); + if (!MRB_PROC_STRICT_P(p)) { + struct RProc *p2 = (struct RProc*)mrb_obj_alloc(mrb, MRB_TT_PROC, p->c); + proc_copy(p2, p); + p2->flags |= MRB_PROC_STRICT; + return mrb_obj_value(p2); + } + return self; +} + void mrb_init_proc(mrb_state *mrb) { @@ -136,4 +166,7 @@ mrb_init_proc(mrb_state *mrb) m = mrb_proc_new(mrb, call_irep); mrb_define_method_raw(mrb, mrb->proc_class, mrb_intern(mrb, "call"), m); mrb_define_method_raw(mrb, mrb->proc_class, mrb_intern(mrb, "[]"), m); + + mrb_define_class_method(mrb, mrb->kernel_module, "lambda", proc_lambda, ARGS_NONE()); /* 15.3.1.2.6 */ + mrb_define_method(mrb, mrb->kernel_module, "lambda", proc_lambda, ARGS_NONE()); /* 15.3.1.3.27 */ } -- cgit v1.2.3 From 6f22a61135fd077c1dcff2dec92ab215e3a83d79 Mon Sep 17 00:00:00 2001 From: Yukihiro Matsumoto Date: Wed, 20 Jun 2012 15:42:51 +0900 Subject: __send__ added --- mrblib/kernel.rb | 8 -------- src/kernel.c | 1 + 2 files changed, 1 insertion(+), 8 deletions(-) (limited to 'mrblib') diff --git a/mrblib/kernel.rb b/mrblib/kernel.rb index f29a80973..ad3bc72f7 100644 --- a/mrblib/kernel.rb +++ b/mrblib/kernel.rb @@ -18,14 +18,6 @@ module Kernel raise NotImplementedError.new("eval not implemented") end - ## - # Alias for +send+. - # - # ISO 15.3.1.3.4 - #def __send__(symbol, *args, &block) - ### *** TODO *** ### - #end - # 15.3.1.3.12 def eval(s) Kernel.eval(s) diff --git a/src/kernel.c b/src/kernel.c index 45eda6d2f..5b65714b6 100644 --- a/src/kernel.c +++ b/src/kernel.c @@ -1229,6 +1229,7 @@ mrb_init_kernel(mrb_state *mrb) mrb_define_method(mrb, krn, "remove_instance_variable", mrb_obj_remove_instance_variable,ARGS_REQ(1)); /* 15.3.1.3.41 */ mrb_define_method(mrb, krn, "respond_to?", obj_respond_to, ARGS_ANY()); /* 15.3.1.3.43 */ mrb_define_method(mrb, krn, "send", mrb_f_send, ARGS_ANY()); /* 15.3.1.3.44 */ + mrb_define_method(mrb, krn, "__send__", mrb_f_send, ARGS_ANY()); /* 15.3.1.3.4 */ mrb_define_method(mrb, krn, "singleton_methods", mrb_obj_singleton_methods_m, ARGS_ANY()); /* 15.3.1.3.45 */ mrb_define_method(mrb, krn, "to_s", mrb_any_to_s, ARGS_NONE()); /* 15.3.1.3.46 */ -- cgit v1.2.3 From e61bdbe3960aa471a8b4e5d8a89614d1433959aa Mon Sep 17 00:00:00 2001 From: Yukihiro Matsumoto Date: Wed, 20 Jun 2012 20:45:04 +0900 Subject: add printf method --- mrblib/print.rb | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'mrblib') diff --git a/mrblib/print.rb b/mrblib/print.rb index 0d33c66d2..452ba53a9 100644 --- a/mrblib/print.rb +++ b/mrblib/print.rb @@ -47,4 +47,8 @@ module Kernel end args[0] end + + def printf(*args) + __printstr__(sprintf(*args)) + end end -- cgit v1.2.3 From 162b3625d0f7ab3cba2163544bf0a2d1256d3000 Mon Sep 17 00:00:00 2001 From: Daniel Bovensiepen Date: Thu, 21 Jun 2012 15:01:13 +0800 Subject: Only open struct class if there is actually already a class --- mrblib/struct.rb | 78 +++++++++++++++++++++++++++++--------------------------- 1 file changed, 41 insertions(+), 37 deletions(-) (limited to 'mrblib') diff --git a/mrblib/struct.rb b/mrblib/struct.rb index 4b6d767a9..5d0ede90f 100644 --- a/mrblib/struct.rb +++ b/mrblib/struct.rb @@ -2,45 +2,49 @@ # Struct # # ISO 15.2.18 -class Struct - ## - # Calls the given block for each element of +self+ - # and pass the respective element. - # - # ISO 15.2.18.4.4 - def each(&block) - self.class.members.each{|field| - block.call(self[field]) - } - self - end +if Object.const_defined?(:Struct) + class Struct - ## - # Calls the given block for each element of +self+ - # and pass the name and value of the respectiev - # element. - # - # ISO 15.2.18.4.5 - def each_pair(&block) - self.class.members.each{|field| - block.call(field.to_sym, self[field]) - } - self - end + ## + # Calls the given block for each element of +self+ + # and pass the respective element. + # + # ISO 15.2.18.4.4 + def each(&block) + self.class.members.each{|field| + block.call(self[field]) + } + self + end - ## - # Calls the given block for each element of +self+ - # and returns an array with all elements of which - # block is not false. - # - # ISO 15.2.18.4.7 - def select(&block) - ary = [] - self.class.members.each{|field| - val = self[field] - ary.push(val) if block.call(val) - } - ary + ## + # Calls the given block for each element of +self+ + # and pass the name and value of the respectiev + # element. + # + # ISO 15.2.18.4.5 + def each_pair(&block) + self.class.members.each{|field| + block.call(field.to_sym, self[field]) + } + self + end + + ## + # Calls the given block for each element of +self+ + # and returns an array with all elements of which + # block is not false. + # + # ISO 15.2.18.4.7 + def select(&block) + ary = [] + self.class.members.each{|field| + val = self[field] + ary.push(val) if block.call(val) + } + ary + end end end + -- cgit v1.2.3 From 2436ee817adfca3de7b758fa83f959d4079f134f Mon Sep 17 00:00:00 2001 From: Daniel Bovensiepen Date: Thu, 21 Jun 2012 15:44:27 +0800 Subject: Make printf optional based on sprintf --- mrblib/print.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'mrblib') diff --git a/mrblib/print.rb b/mrblib/print.rb index 452ba53a9..7ec6d70ef 100644 --- a/mrblib/print.rb +++ b/mrblib/print.rb @@ -48,7 +48,12 @@ module Kernel args[0] end - def printf(*args) - __printstr__(sprintf(*args)) + if Kernel.respond_to?(:sprintf) + ## + # Invoke method +sprintf+ and pass +*args+ to it. + # Pass return value to *print* of STDOUT. + def printf(*args) + __printstr__(sprintf(*args)) + end end end -- cgit v1.2.3 From 529fb1b9923286817f4e368cf283a546d06f8687 Mon Sep 17 00:00:00 2001 From: Yuichiro MASUI Date: Fri, 22 Jun 2012 17:47:23 +0900 Subject: fixed attr_*: forgot '@' --- mrblib/class.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'mrblib') diff --git a/mrblib/class.rb b/mrblib/class.rb index 9c3ef91fd..e984376a4 100644 --- a/mrblib/class.rb +++ b/mrblib/class.rb @@ -2,14 +2,14 @@ class Module # 15.2.2.4.13 def attr_reader(*names) names.each{|name| - define_method(name){self.instance_variable_get(name)} + define_method(name){self.instance_variable_get('@'+name.to_s)} } end # 15.2.2.4.14 def attr_writer(*names) names.each{|name| aset = (name.to_s+"=").intern - define_method(aset){|v|self.instance_variable_set(name,v)} + define_method(aset){|v|self.instance_variable_set('@'+name.to_s,v)} } end # 15.2.2.4.12 -- cgit v1.2.3 From 6d3b35e064ce46cc97530a4fc41e64e28c555c1a Mon Sep 17 00:00:00 2001 From: Yukihiro Matsumoto Date: Sun, 24 Jun 2012 01:22:53 +0900 Subject: avoid generating iv name each time in accessors --- mrblib/class.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'mrblib') diff --git a/mrblib/class.rb b/mrblib/class.rb index e984376a4..4f268b6c8 100644 --- a/mrblib/class.rb +++ b/mrblib/class.rb @@ -2,14 +2,16 @@ class Module # 15.2.2.4.13 def attr_reader(*names) names.each{|name| - define_method(name){self.instance_variable_get('@'+name.to_s)} + name2 = ('@'+name.to_s).intern + define_method(name){self.instance_variable_get(name2)} } end # 15.2.2.4.14 def attr_writer(*names) names.each{|name| - aset = (name.to_s+"=").intern - define_method(aset){|v|self.instance_variable_set('@'+name.to_s,v)} + name2 = ('@'+name.to_s).intern + name = (name.to_s+"=").intern + define_method(name){|v|self.instance_variable_set(name2,v)} } end # 15.2.2.4.12 -- cgit v1.2.3 From 8f5017e768b6cdc9b7ef824018561b9d300dfa5e Mon Sep 17 00:00:00 2001 From: Yukihiro Matsumoto Date: Tue, 26 Jun 2012 14:01:26 +0900 Subject: raise NotImplementedError from regexp related string methods; close #319 --- mrblib/string.rb | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) (limited to 'mrblib') diff --git a/mrblib/string.rb b/mrblib/string.rb index ad7e1fca1..d09b787da 100644 --- a/mrblib/string.rb +++ b/mrblib/string.rb @@ -33,12 +33,15 @@ class String end ### *** TODO *** ### + unless Object.const_defined?(:Regexp) + raise NotImplementedError, "gsub not available (yet)" + end end ## # Replace all matches of +pattern+ with +replacement+. # Call block (if given) for each match and replace - # +pattern+ with the value of the block. Modify + # +pattern+ with the value of the block. Modify # +self+ with the final value. # # ISO 15.2.10.5.19 @@ -56,15 +59,18 @@ class String # Calls the given block for each match of +pattern+ # If no block is given return an array with all # matches of +pattern+. - # + # # ISO 15.2.10.5.32 def scan(reg, &block) ### *** TODO *** ### + unless Object.const_defined?(:Regexp) + raise NotImplementedError, "scan not available (yet)" + end end ## - # Replace only the first match of +pattern+ with - # +replacement+. Call block (if given) for each + # Replace only the first match of +pattern+ with + # +replacement+. Call block (if given) for each # match and replace +pattern+ with the value of the # block. Return the final value. # @@ -75,12 +81,15 @@ class String end ### *** TODO *** ### + unless Object.const_defined?(:Regexp) + raise NotImplementedError, "sub not available (yet)" + end end ## - # Replace only the first match of +pattern+ with - # +replacement+. Call block (if given) for each - # match and replace +pattern+ with the value of the + # Replace only the first match of +pattern+ with + # +replacement+. Call block (if given) for each + # match and replace +pattern+ with the value of the # block. Modify +self+ with the final value. # # ISO 15.2.10.5.37 -- cgit v1.2.3 From e841135cc11b8f935e8ab19cb6f1793883cda65c Mon Sep 17 00:00:00 2001 From: Daniel Bovensiepen Date: Wed, 27 Jun 2012 14:13:11 +0800 Subject: raise NotImplementedError in case of sprintf is missing --- mrblib/print.rb | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'mrblib') diff --git a/mrblib/print.rb b/mrblib/print.rb index 7ec6d70ef..bf8e6d726 100644 --- a/mrblib/print.rb +++ b/mrblib/print.rb @@ -48,12 +48,14 @@ module Kernel args[0] end - if Kernel.respond_to?(:sprintf) - ## - # Invoke method +sprintf+ and pass +*args+ to it. - # Pass return value to *print* of STDOUT. - def printf(*args) + ## + # Invoke method +sprintf+ and pass +*args+ to it. + # Pass return value to *print* of STDOUT. + def printf(*args) + if Kernel.respond_to?(:sprintf) __printstr__(sprintf(*args)) + else + raise NotImplementedError.new('sprintf not available') end end end -- cgit v1.2.3 From 7f9b91517bdad2848659fd4bcaee71545e0562ea Mon Sep 17 00:00:00 2001 From: Daniel Bovensiepen Date: Wed, 27 Jun 2012 14:20:19 +0800 Subject: raise NoImplementedError also for sprintf in case it isn't available --- mrblib/print.rb | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'mrblib') diff --git a/mrblib/print.rb b/mrblib/print.rb index bf8e6d726..3ebd77ee6 100644 --- a/mrblib/print.rb +++ b/mrblib/print.rb @@ -50,7 +50,7 @@ module Kernel ## # Invoke method +sprintf+ and pass +*args+ to it. - # Pass return value to *print* of STDOUT. + # Pass return value to +print+ of STDOUT. def printf(*args) if Kernel.respond_to?(:sprintf) __printstr__(sprintf(*args)) @@ -58,4 +58,14 @@ module Kernel raise NotImplementedError.new('sprintf not available') end end + + ## + # +sprintf+ is defined in +src/sprintf.c+ + # This stub method is only to inform the user + # that +sprintf+ isn't implemented. + unless Kernel.respond_to?(:sprintf) + def sprintf(*args) + raise NotImplementedError.new('sprintf not available') + end + end end -- cgit v1.2.3 From a9247f723abf401fcf940fffaa984b516cc61052 Mon Sep 17 00:00:00 2001 From: Yukihiro Matsumoto Date: Sun, 15 Jul 2012 17:31:51 +0900 Subject: condition updated to preven printf from sprintf redefinition --- mrblib/print.rb | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'mrblib') diff --git a/mrblib/print.rb b/mrblib/print.rb index 3ebd77ee6..c4ce74d6b 100644 --- a/mrblib/print.rb +++ b/mrblib/print.rb @@ -51,11 +51,13 @@ module Kernel ## # Invoke method +sprintf+ and pass +*args+ to it. # Pass return value to +print+ of STDOUT. - def printf(*args) - if Kernel.respond_to?(:sprintf) + if Kernel.respond_to?(:sprintf) and Kernel.respond_to?(:__printstr__) + def printf(*args) __printstr__(sprintf(*args)) - else - raise NotImplementedError.new('sprintf not available') + end + else + def printf(*args) + raise NotImplementedError.new('printf not available') end end -- cgit v1.2.3 From a18699a80c14e0e44e904b7537d6d57463b6f39c Mon Sep 17 00:00:00 2001 From: Yukihiro Matsumoto Date: Tue, 17 Jul 2012 23:56:57 +0900 Subject: print.rb: raise NotImplementedError for disabled methods --- mrblib/print.rb | 117 +++++++++++++++++++++++++++++--------------------------- 1 file changed, 61 insertions(+), 56 deletions(-) (limited to 'mrblib') diff --git a/mrblib/print.rb b/mrblib/print.rb index c4ce74d6b..dea088e2b 100644 --- a/mrblib/print.rb +++ b/mrblib/print.rb @@ -3,71 +3,76 @@ # # 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 + + ## + # 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 - 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 - __printstr__ args[i].to_s - __printstr__ "\n" - i += 1 + ## + # 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 + __printstr__ args[i].to_s + __printstr__ "\n" + i += 1 + end + __printstr__ "\n" if len == 0 + nil 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 + ## + # 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 - args[0] - end - ## - # Invoke method +sprintf+ and pass +*args+ to it. - # Pass return value to +print+ of STDOUT. - if Kernel.respond_to?(:sprintf) and Kernel.respond_to?(:__printstr__) def printf(*args) __printstr__(sprintf(*args)) end - else - def printf(*args) - raise NotImplementedError.new('printf not available') - end - end - - ## - # +sprintf+ is defined in +src/sprintf.c+ - # This stub method is only to inform the user - # that +sprintf+ isn't implemented. - unless Kernel.respond_to?(:sprintf) - def sprintf(*args) - raise NotImplementedError.new('sprintf not available') - end end end -- cgit v1.2.3