summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--doc/language/README.md2
-rw-r--r--include/mruby.h1
-rw-r--r--include/mruby/dump.h2
-rw-r--r--include/mruby/range.h1
-rw-r--r--include/mruby/value.h1
-rw-r--r--mrbgems/mruby-array-ext/mrblib/array.rb1
-rw-r--r--mrbgems/mruby-array-ext/src/array.c13
-rw-r--r--mrbgems/mruby-array-ext/test/array.rb10
-rw-r--r--mrbgems/mruby-bin-mirb/tools/mirb/mirb.c12
-rw-r--r--mrbgems/mruby-hash-ext/mrblib/hash.rb73
-rw-r--r--mrbgems/mruby-hash-ext/test/hash.rb34
-rw-r--r--mrbgems/mruby-math/src/math.c39
-rw-r--r--mrbgems/mruby-sprintf/src/sprintf.c2
-rw-r--r--mrbgems/mruby-struct/src/struct.c65
-rw-r--r--mrbgems/mruby-struct/test/struct.rb7
-rw-r--r--src/dump.c6
-rw-r--r--src/error.c31
-rw-r--r--src/gc.c6
-rw-r--r--src/numeric.c6
-rw-r--r--src/range.c80
-rw-r--r--src/variable.c2
-rw-r--r--tasks/toolchains/visualcpp.rake3
22 files changed, 299 insertions, 98 deletions
diff --git a/doc/language/README.md b/doc/language/README.md
index 67946fe64..24dd598ac 100644
--- a/doc/language/README.md
+++ b/doc/language/README.md
@@ -6,4 +6,4 @@ which are provided together with mruby.
## Built-In Class and Modules
-see *doc/lang/Core.md*
+see *doc/language/Core.md*
diff --git a/include/mruby.h b/include/mruby.h
index 9cf578836..db3b06aa8 100644
--- a/include/mruby.h
+++ b/include/mruby.h
@@ -381,6 +381,7 @@ mrb_value mrb_yield_with_class(mrb_state *mrb, mrb_value b, mrb_int argc, const
void mrb_gc_protect(mrb_state *mrb, mrb_value obj);
mrb_value mrb_to_int(mrb_state *mrb, mrb_value val);
+#define mrb_int(mrb, val) mrb_fixnum(mrb_to_int(mrb, val))
void mrb_check_type(mrb_state *mrb, mrb_value x, enum mrb_vtype t);
typedef enum call_type {
diff --git a/include/mruby/dump.h b/include/mruby/dump.h
index e5a853bad..5a63933d4 100644
--- a/include/mruby/dump.h
+++ b/include/mruby/dump.h
@@ -14,7 +14,7 @@ extern "C" {
#include "mruby.h"
#include "mruby/irep.h"
-int dump_irep(mrb_state *mrb, mrb_irep *irep, int debug_info, uint8_t **bin, size_t *bin_size);
+int mrb_dump_irep(mrb_state *mrb, mrb_irep *irep, int debug_info, uint8_t **bin, size_t *bin_size);
#ifdef ENABLE_STDIO
int mrb_dump_irep_binary(mrb_state*, mrb_irep*, int, FILE*);
int mrb_dump_irep_cfunc(mrb_state *mrb, mrb_irep*, int, FILE *f, const char *initname);
diff --git a/include/mruby/range.h b/include/mruby/range.h
index 828ec2691..61beb2319 100644
--- a/include/mruby/range.h
+++ b/include/mruby/range.h
@@ -27,6 +27,7 @@ struct RRange {
mrb_value mrb_range_new(mrb_state*, mrb_value, mrb_value, mrb_bool);
mrb_bool mrb_range_beg_len(mrb_state *mrb, mrb_value range, mrb_int *begp, mrb_int *lenp, mrb_int len);
+mrb_value mrb_get_values_at(mrb_state *mrb, mrb_value obj, mrb_int olen, mrb_int argc, const mrb_value *argv, mrb_value (*func)(mrb_state*, mrb_value, mrb_int));
#if defined(__cplusplus)
} /* extern "C" { */
diff --git a/include/mruby/value.h b/include/mruby/value.h
index 9ba714cae..5c24e8843 100644
--- a/include/mruby/value.h
+++ b/include/mruby/value.h
@@ -60,6 +60,7 @@ typedef short mrb_sym;
# define snprintf _snprintf
# if _MSC_VER < 1800
# include <float.h>
+# define isfinite(n) _finite(n)
# define isnan _isnan
# define isinf(n) (!_finite(n) && !_isnan(n))
# define signbit(n) (_copysign(1.0, (n)) < 0.0)
diff --git a/mrbgems/mruby-array-ext/mrblib/array.rb b/mrbgems/mruby-array-ext/mrblib/array.rb
index df3ab97e4..fd80fa0bb 100644
--- a/mrbgems/mruby-array-ext/mrblib/array.rb
+++ b/mrbgems/mruby-array-ext/mrblib/array.rb
@@ -617,7 +617,6 @@ class Array
return to_enum :delete_if unless block_given?
idx = 0
- len = self.size
while idx < self.size do
if block.call(self[idx])
self.delete_at(idx)
diff --git a/mrbgems/mruby-array-ext/src/array.c b/mrbgems/mruby-array-ext/src/array.c
index ad6cead2c..ae9d8296e 100644
--- a/mrbgems/mruby-array-ext/src/array.c
+++ b/mrbgems/mruby-array-ext/src/array.c
@@ -1,6 +1,7 @@
#include "mruby.h"
#include "mruby/value.h"
#include "mruby/array.h"
+#include "mruby/range.h"
/*
* call-seq:
@@ -122,6 +123,17 @@ mrb_ary_at(mrb_state *mrb, mrb_value ary)
return mrb_ary_entry(ary, pos);
}
+static mrb_value
+mrb_ary_values_at(mrb_state *mrb, mrb_value self)
+{
+ mrb_int argc;
+ mrb_value *argv;
+
+ mrb_get_args(mrb, "*", &argv, &argc);
+
+ return mrb_get_values_at(mrb, self, RARRAY_LEN(self), argc, argv, mrb_ary_ref);
+}
+
void
mrb_mruby_array_ext_gem_init(mrb_state* mrb)
{
@@ -132,6 +144,7 @@ mrb_mruby_array_ext_gem_init(mrb_state* mrb)
mrb_define_method(mrb, a, "assoc", mrb_ary_assoc, MRB_ARGS_REQ(1));
mrb_define_method(mrb, a, "at", mrb_ary_at, MRB_ARGS_REQ(1));
mrb_define_method(mrb, a, "rassoc", mrb_ary_rassoc, MRB_ARGS_REQ(1));
+ mrb_define_method(mrb, a, "values_at", mrb_ary_values_at, MRB_ARGS_ANY());
}
void
diff --git a/mrbgems/mruby-array-ext/test/array.rb b/mrbgems/mruby-array-ext/test/array.rb
index b0ad94b38..1fa7cfc04 100644
--- a/mrbgems/mruby-array-ext/test/array.rb
+++ b/mrbgems/mruby-array-ext/test/array.rb
@@ -280,3 +280,13 @@ assert("Array#select!") do
assert_equal [4, 5], a.select! { |val| val > 3 }
assert_equal [4, 5], a
end
+
+assert('Array#values_at') do
+ a = %w{red green purple white none}
+
+ assert_equal %w{red purple none}, a.values_at(0, 2, 4)
+ assert_equal ['green', 'white', nil, nil], a.values_at(1, 3, 5, 7)
+ assert_equal ['none', 'white', 'white', nil], a.values_at(-1, -2, -2, -7)
+ assert_equal ['none', nil, nil, 'red', 'green', 'purple'], a.values_at(4..6, 0...3)
+ assert_raise(TypeError) { a.values_at 'tt' }
+end
diff --git a/mrbgems/mruby-bin-mirb/tools/mirb/mirb.c b/mrbgems/mruby-bin-mirb/tools/mirb/mirb.c
index e7bc69495..c8ea0a055 100644
--- a/mrbgems/mruby-bin-mirb/tools/mirb/mirb.c
+++ b/mrbgems/mruby-bin-mirb/tools/mirb/mirb.c
@@ -8,11 +8,7 @@
#include <stdlib.h>
#include <string.h>
-#include "mruby.h"
-#include "mruby/array.h"
-#include "mruby/proc.h"
-#include "mruby/compile.h"
-#include "mruby/string.h"
+#include <stdio.h>
#ifdef ENABLE_READLINE
#include <readline/readline.h>
@@ -38,6 +34,12 @@ static const char *history_file_name = ".mirb_history";
char history_path[PATH_MAX];
#endif
+#include "mruby.h"
+#include "mruby/array.h"
+#include "mruby/proc.h"
+#include "mruby/compile.h"
+#include "mruby/string.h"
+
static void
p(mrb_state *mrb, mrb_value obj, int prompt)
{
diff --git a/mrbgems/mruby-hash-ext/mrblib/hash.rb b/mrbgems/mruby-hash-ext/mrblib/hash.rb
index bf3836514..ce8fa3577 100644
--- a/mrbgems/mruby-hash-ext/mrblib/hash.rb
+++ b/mrbgems/mruby-hash-ext/mrblib/hash.rb
@@ -1,4 +1,26 @@
class Hash
+
+ ##
+ # call-seq:
+ # hsh.merge!(other_hash) -> hsh
+ # hsh.merge!(other_hash){|key, oldval, newval| block} -> hsh
+ #
+ # Adds the contents of _other_hash_ to _hsh_. If no block is specified,
+ # entries with duplicate keys are overwritten with the values from
+ # _other_hash_, otherwise the value of each duplicate key is determined by
+ # calling the block with the key, its value in _hsh_ and its value in
+ # _other_hash_.
+ #
+ # h1 = { "a" => 100, "b" => 200 }
+ # h2 = { "b" => 254, "c" => 300 }
+ # h1.merge!(h2) #=> {"a"=>100, "b"=>254, "c"=>300}
+ #
+ # h1 = { "a" => 100, "b" => 200 }
+ # h2 = { "b" => 254, "c" => 300 }
+ # h1.merge!(h2) { |key, v1, v2| v1 }
+ # #=> {"a"=>100, "b"=>200, "c"=>300}
+ #
+
def merge!(other, &block)
raise "can't convert argument into Hash" unless other.respond_to?(:to_hash)
if block
@@ -14,6 +36,34 @@ class Hash
alias each_pair each
alias update merge!
+ ##
+ # call-seq:
+ # hsh.fetch(key [, default] ) -> obj
+ # hsh.fetch(key) {| key | block } -> obj
+ #
+ # Returns a value from the hash for the given key. If the key can't be
+ # found, there are several options: With no other arguments, it will
+ # raise an <code>KeyError</code> exception; if <i>default</i> is
+ # given, then that will be returned; if the optional code block is
+ # specified, then that will be run and its result returned.
+ #
+ # h = { "a" => 100, "b" => 200 }
+ # h.fetch("a") #=> 100
+ # h.fetch("z", "go fish") #=> "go fish"
+ # h.fetch("z") { |el| "go fish, #{el}"} #=> "go fish, z"
+ #
+ # The following example shows that an exception is raised if the key
+ # is not found and a default value is not supplied.
+ #
+ # h = { "a" => 100, "b" => 200 }
+ # h.fetch("z")
+ #
+ # <em>produces:</em>
+ #
+ # prog.rb:2:in `fetch': key not found (KeyError)
+ # from prog.rb:2
+ #
+
def fetch(key, none=NONE, &block)
unless self.key?(key)
if block
@@ -27,4 +77,27 @@ class Hash
self[key]
end
end
+
+ ##
+ # call-seq:
+ # hsh.delete_if {| key, value | block } -> hsh
+ # hsh.delete_if -> an_enumerator
+ #
+ # Deletes every key-value pair from <i>hsh</i> for which <i>block</i>
+ # evaluates to <code>true</code>.
+ #
+ # If no block is given, an enumerator is returned instead.
+ #
+ # h = { "a" => 100, "b" => 200, "c" => 300 }
+ # h.delete_if {|key, value| key >= "b" } #=> {"a"=>100}
+ #
+
+ def delete_if(&block)
+ return to_enum :delete_if unless block_given?
+
+ self.each do |k, v|
+ self.delete(k) if block.call(k, v)
+ end
+ self
+ end
end
diff --git a/mrbgems/mruby-hash-ext/test/hash.rb b/mrbgems/mruby-hash-ext/test/hash.rb
index 83a064811..4c7dbb217 100644
--- a/mrbgems/mruby-hash-ext/test/hash.rb
+++ b/mrbgems/mruby-hash-ext/test/hash.rb
@@ -39,3 +39,37 @@ assert('Hash#fetch') do
assert_kind_of(StandardError, e);
end
end
+
+assert("Hash#delete_if") do
+ base = { 1 => 'one', 2 => false, true => 'true', 'cat' => 99 }
+ h1 = { 1 => 'one', 2 => false, true => 'true' }
+ h2 = { 2 => false, 'cat' => 99 }
+ h3 = { 2 => false }
+
+ h = base.dup
+ assert_equal(h, h.delete_if { false })
+ assert_equal({}, h.delete_if { true })
+
+ h = base.dup
+ assert_equal(h1, h.delete_if {|k,v| k.instance_of?(String) })
+ assert_equal(h1, h)
+
+ h = base.dup
+ assert_equal(h2, h.delete_if {|k,v| v.instance_of?(String) })
+ assert_equal(h2, h)
+
+ h = base.dup
+ assert_equal(h3, h.delete_if {|k,v| v })
+ assert_equal(h3, h)
+
+ h = base.dup
+ n = 0
+ h.delete_if {|*a|
+ n += 1
+ assert_equal(2, a.size)
+ assert_equal(base[a[0]], a[1])
+ h.shift
+ true
+ }
+ assert_equal(base.size, n)
+end
diff --git a/mrbgems/mruby-math/src/math.c b/mrbgems/mruby-math/src/math.c
index a699a12f0..871cba301 100644
--- a/mrbgems/mruby-math/src/math.c
+++ b/mrbgems/mruby-math/src/math.c
@@ -9,8 +9,14 @@
#include <math.h>
-#define domain_error(msg) \
- mrb_raise(mrb, E_RANGE_ERROR, "Numerical argument is out of domain - " #msg)
+static void
+domain_error(mrb_state *mrb, const char *func)
+{
+ struct RClass *math = mrb_module_get(mrb, "Math");
+ struct RClass *domainerror = mrb_class_get_under(mrb, math, "DomainError");
+ mrb_value str = mrb_str_new_cstr(mrb, func);
+ mrb_raisef(mrb, domainerror, "Numerical argument is out of domain - %S", str);
+}
/* math functions not provided by Microsoft Visual C++ 2012 or older */
#if defined _MSC_VER && _MSC_VER < 1800
@@ -172,6 +178,9 @@ math_asin(mrb_state *mrb, mrb_value obj)
mrb_float x;
mrb_get_args(mrb, "f", &x);
+ if (x < -1.0 || x > 1.0) {
+ domain_error(mrb, "asin");
+ }
x = asin(x);
return mrb_float_value(mrb, x);
@@ -189,6 +198,9 @@ math_acos(mrb_state *mrb, mrb_value obj)
mrb_float x;
mrb_get_args(mrb, "f", &x);
+ if (x < -1.0 || x > 1.0) {
+ domain_error(mrb, "acos");
+ }
x = acos(x);
return mrb_float_value(mrb, x);
@@ -334,6 +346,9 @@ math_acosh(mrb_state *mrb, mrb_value obj)
mrb_float x;
mrb_get_args(mrb, "f", &x);
+ if (x < 1.0) {
+ domain_error(mrb, "acosh");
+ }
x = acosh(x);
return mrb_float_value(mrb, x);
@@ -351,6 +366,9 @@ math_atanh(mrb_state *mrb, mrb_value obj)
mrb_float x;
mrb_get_args(mrb, "f", &x);
+ if (x < -1.0 || x > 1.0) {
+ domain_error(mrb, "atanh");
+ }
x = atanh(x);
return mrb_float_value(mrb, x);
@@ -404,8 +422,14 @@ math_log(mrb_state *mrb, mrb_value obj)
int argc;
argc = mrb_get_args(mrb, "f|f", &x, &base);
+ if (x < 0.0) {
+ domain_error(mrb, "log");
+ }
x = log(x);
if (argc == 2) {
+ if (base < 0.0) {
+ domain_error(mrb, "log");
+ }
x /= log(base);
}
return mrb_float_value(mrb, x);
@@ -429,6 +453,9 @@ math_log2(mrb_state *mrb, mrb_value obj)
mrb_float x;
mrb_get_args(mrb, "f", &x);
+ if (x < 0.0) {
+ domain_error(mrb, "log2");
+ }
x = log2(x);
return mrb_float_value(mrb, x);
@@ -451,6 +478,9 @@ math_log10(mrb_state *mrb, mrb_value obj)
mrb_float x;
mrb_get_args(mrb, "f", &x);
+ if (x < 0.0) {
+ domain_error(mrb, "log10");
+ }
x = log10(x);
return mrb_float_value(mrb, x);
@@ -469,6 +499,9 @@ math_sqrt(mrb_state *mrb, mrb_value obj)
mrb_float x;
mrb_get_args(mrb, "f", &x);
+ if (x < 0.0) {
+ domain_error(mrb, "sqrt");
+ }
x = sqrt(x);
return mrb_float_value(mrb, x);
@@ -624,6 +657,8 @@ mrb_mruby_math_gem_init(mrb_state* mrb)
struct RClass *mrb_math;
mrb_math = mrb_define_module(mrb, "Math");
+ mrb_define_class_under(mrb, mrb_math, "DomainError", mrb->eStandardError_class);
+
#ifdef M_PI
mrb_define_const(mrb, mrb_math, "PI", mrb_float_value(mrb, M_PI));
#else
diff --git a/mrbgems/mruby-sprintf/src/sprintf.c b/mrbgems/mruby-sprintf/src/sprintf.c
index a82a674cf..31f22975d 100644
--- a/mrbgems/mruby-sprintf/src/sprintf.c
+++ b/mrbgems/mruby-sprintf/src/sprintf.c
@@ -999,7 +999,7 @@ retry:
char fbuf[32];
fval = mrb_float(mrb_Float(mrb, val));
- if (isnan(fval) || isinf(fval)) {
+ if (!isfinite(fval)) {
const char *expr;
const int elen = 3;
diff --git a/mrbgems/mruby-struct/src/struct.c b/mrbgems/mruby-struct/src/struct.c
index a4d70ae1a..a15655dbb 100644
--- a/mrbgems/mruby-struct/src/struct.c
+++ b/mrbgems/mruby-struct/src/struct.c
@@ -12,6 +12,7 @@
#include "mruby/class.h"
#include "mruby/variable.h"
#include "mruby/hash.h"
+#include "mruby/range.h"
#define RSTRUCT_LEN(st) RARRAY_LEN(st)
#define RSTRUCT_PTR(st) RARRAY_PTR(st)
@@ -531,7 +532,7 @@ mrb_struct_init_copy(mrb_state *mrb, mrb_value copy)
}
static mrb_value
-mrb_struct_aref_id(mrb_state *mrb, mrb_value s, mrb_sym id)
+struct_aref_sym(mrb_state *mrb, mrb_value s, mrb_sym id)
{
mrb_value *ptr, members, *ptr_members;
mrb_int i, len;
@@ -549,6 +550,21 @@ mrb_struct_aref_id(mrb_state *mrb, mrb_value s, mrb_sym id)
return mrb_nil_value(); /* not reached */
}
+static mrb_value
+struct_aref_int(mrb_state *mrb, mrb_value s, mrb_int i)
+{
+ if (i < 0) i = RSTRUCT_LEN(s) + i;
+ if (i < 0)
+ mrb_raisef(mrb, E_INDEX_ERROR,
+ "offset %S too small for struct(size:%S)",
+ mrb_fixnum_value(i), mrb_fixnum_value(RSTRUCT_LEN(s)));
+ if (RSTRUCT_LEN(s) <= i)
+ mrb_raisef(mrb, E_INDEX_ERROR,
+ "offset %S too large for struct(size:%S)",
+ mrb_fixnum_value(i), mrb_fixnum_value(RSTRUCT_LEN(s)));
+ return RSTRUCT_PTR(s)[i];
+}
+
/* 15.2.18.4.2 */
/*
* call-seq:
@@ -569,10 +585,11 @@ mrb_struct_aref_id(mrb_state *mrb, mrb_value s, mrb_sym id)
* joe[0] #=> "Joe Smith"
*/
mrb_value
-mrb_struct_aref_n(mrb_state *mrb, mrb_value s, mrb_value idx)
+mrb_struct_aref(mrb_state *mrb, mrb_value s)
{
- mrb_int i;
+ mrb_value idx;
+ mrb_get_args(mrb, "o", &idx);
if (mrb_string_p(idx)) {
mrb_value sym = mrb_check_intern_str(mrb, idx);
@@ -582,33 +599,13 @@ mrb_struct_aref_n(mrb_state *mrb, mrb_value s, mrb_value idx)
idx = sym;
}
if (mrb_symbol_p(idx)) {
- return mrb_struct_aref_id(mrb, s, mrb_symbol(idx));
+ return struct_aref_sym(mrb, s, mrb_symbol(idx));
}
-
- i = mrb_fixnum(idx);
- if (i < 0) i = RSTRUCT_LEN(s) + i;
- if (i < 0)
- mrb_raisef(mrb, E_INDEX_ERROR,
- "offset %S too small for struct(size:%S)",
- mrb_fixnum_value(i), mrb_fixnum_value(RSTRUCT_LEN(s)));
- if (RSTRUCT_LEN(s) <= i)
- mrb_raisef(mrb, E_INDEX_ERROR,
- "offset %S too large for struct(size:%S)",
- mrb_fixnum_value(i), mrb_fixnum_value(RSTRUCT_LEN(s)));
- return RSTRUCT_PTR(s)[i];
-}
-
-mrb_value
-mrb_struct_aref(mrb_state *mrb, mrb_value s)
-{
- mrb_value idx;
-
- mrb_get_args(mrb, "o", &idx);
- return mrb_struct_aref_n(mrb, s, idx);
+ return struct_aref_int(mrb, s, mrb_int(mrb, idx));
}
static mrb_value
-mrb_struct_aset_id(mrb_state *mrb, mrb_value s, mrb_sym id, mrb_value val)
+mrb_struct_aset_sym(mrb_state *mrb, mrb_value s, mrb_sym id, mrb_value val)
{
mrb_value members, *ptr, *ptr_members;
mrb_int i, len;
@@ -663,8 +660,8 @@ mrb_struct_aset(mrb_state *mrb, mrb_value s)
mrb_get_args(mrb, "oo", &idx, &val);
- if (mrb_string_p(idx) || mrb_symbol_p(idx)) {
- return mrb_struct_aset_id(mrb, s, mrb_obj_to_sym(mrb, idx), val);
+ if (mrb_symbol_p(idx)) {
+ return mrb_struct_aset_sym(mrb, s, mrb_symbol(idx), val);
}
i = mrb_fixnum(idx);
@@ -828,6 +825,17 @@ mrb_struct_to_h(mrb_state *mrb, mrb_value self)
return ret;
}
+static mrb_value
+mrb_struct_values_at(mrb_state *mrb, mrb_value self)
+{
+ mrb_int argc;
+ mrb_value *argv;
+
+ mrb_get_args(mrb, "*", &argv, &argc);
+
+ return mrb_get_values_at(mrb, self, RSTRUCT_LEN(self), argc, argv, struct_aref_int);
+}
+
/*
* A <code>Struct</code> is a convenient way to bundle a number of
* attributes together, using accessor methods, without having to write
@@ -866,6 +874,7 @@ mrb_mruby_struct_gem_init(mrb_state* mrb)
mrb_define_method(mrb, st, "to_a", mrb_struct_to_a, MRB_ARGS_NONE());
mrb_define_method(mrb, st, "values", mrb_struct_to_a, MRB_ARGS_NONE());
mrb_define_method(mrb, st, "to_h", mrb_struct_to_h, MRB_ARGS_NONE());
+ mrb_define_method(mrb, st, "values_at", mrb_struct_values_at, MRB_ARGS_NONE());
}
void
diff --git a/mrbgems/mruby-struct/test/struct.rb b/mrbgems/mruby-struct/test/struct.rb
index d654830ca..b3b9ce33f 100644
--- a/mrbgems/mruby-struct/test/struct.rb
+++ b/mrbgems/mruby-struct/test/struct.rb
@@ -123,3 +123,10 @@ assert('Struct#to_h') do
s = Struct.new(:white, :red, :green).new('ruuko', 'yuzuki', 'hitoe')
assert_equal(:white => 'ruuko', :red => 'yuzuki', :green => 'hitoe') { s.to_h }
end
+
+assert('Struct#values_at') do
+ a = Struct.new(:blue, :purple).new('aki', 'io')
+ assert_equal ['aki'], a.values_at(0)
+ assert_equal ['io', 'aki'], a.values_at(1, 0)
+ assert_raise(IndexError) { a.values_at 2 }
+end
diff --git a/src/dump.c b/src/dump.c
index e89617823..09ac80fac 100644
--- a/src/dump.c
+++ b/src/dump.c
@@ -726,7 +726,7 @@ is_debug_info_defined(mrb_irep *irep)
}
int
-dump_irep(mrb_state *mrb, mrb_irep *irep, int debug_info, uint8_t **bin, size_t *bin_size)
+mrb_dump_irep(mrb_state *mrb, mrb_irep *irep, int debug_info, uint8_t **bin, size_t *bin_size)
{
int result = MRB_DUMP_GENERAL_FAILURE;
size_t section_irep_size;
@@ -815,7 +815,7 @@ mrb_dump_irep_binary(mrb_state *mrb, mrb_irep *irep, int debug_info, FILE* fp)
return MRB_DUMP_INVALID_ARGUMENT;
}
- result = dump_irep(mrb, irep, debug_info, &bin, &bin_size);
+ result = mrb_dump_irep(mrb, irep, debug_info, &bin, &bin_size);
if (result == MRB_DUMP_OK) {
fwrite(bin, bin_size, 1, fp);
}
@@ -851,7 +851,7 @@ mrb_dump_irep_cfunc(mrb_state *mrb, mrb_irep *irep, int debug_info, FILE *fp, co
return MRB_DUMP_INVALID_ARGUMENT;
}
- result = dump_irep(mrb, irep, debug_info, &bin, &bin_size);
+ result = mrb_dump_irep(mrb, irep, debug_info, &bin, &bin_size);
if (result == MRB_DUMP_OK) {
fprintf(fp, "#include <stdint.h>\n"); /* for uint8_t under at least Darwin */
fprintf(fp, "const uint8_t %s[] = {", initname);
diff --git a/src/error.c b/src/error.c
index 8f13dcf77..6f7641cf1 100644
--- a/src/error.c
+++ b/src/error.c
@@ -115,7 +115,10 @@ exc_message(mrb_state *mrb, mrb_value exc)
* call-seq:
* exception.inspect -> string
*
- * Return this exception's class name an message
+ * Returns this exception's file name, line number,
+ * message and class name.
+ * If file name or line number is not set,
+ * returns message and class name.
*/
static mrb_value
@@ -444,18 +447,18 @@ mrb_init_exception(mrb_state *mrb)
{
struct RClass *e;
- 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, MRB_ARGS_ANY());
- mrb_define_method(mrb, e, "exception", exc_exception, MRB_ARGS_ANY());
- mrb_define_method(mrb, e, "initialize", exc_initialize, MRB_ARGS_ANY());
- mrb_define_method(mrb, e, "==", exc_equal, MRB_ARGS_REQ(1));
- mrb_define_method(mrb, e, "to_s", exc_to_s, MRB_ARGS_NONE());
- mrb_define_method(mrb, e, "message", exc_message, MRB_ARGS_NONE());
- mrb_define_method(mrb, e, "inspect", exc_inspect, MRB_ARGS_NONE());
- mrb_define_method(mrb, e, "backtrace", mrb_exc_backtrace, MRB_ARGS_NONE());
-
- mrb->eStandardError_class = mrb_define_class(mrb, "StandardError", mrb->eException_class); /* 15.2.23 */
+ 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, MRB_ARGS_ANY());
+ mrb_define_method(mrb, e, "exception", exc_exception, MRB_ARGS_ANY());
+ mrb_define_method(mrb, e, "initialize", exc_initialize, MRB_ARGS_ANY());
+ mrb_define_method(mrb, e, "==", exc_equal, MRB_ARGS_REQ(1));
+ mrb_define_method(mrb, e, "to_s", exc_to_s, MRB_ARGS_NONE());
+ mrb_define_method(mrb, e, "message", exc_message, MRB_ARGS_NONE());
+ mrb_define_method(mrb, e, "inspect", exc_inspect, MRB_ARGS_NONE());
+ mrb_define_method(mrb, e, "backtrace", mrb_exc_backtrace, MRB_ARGS_NONE());
+
+ mrb->eStandardError_class = mrb_define_class(mrb, "StandardError", mrb->eException_class); /* 15.2.23 */
mrb_define_class(mrb, "RuntimeError", mrb->eStandardError_class); /* 15.2.28 */
- e = mrb_define_class(mrb, "ScriptError", mrb->eException_class); /* 15.2.37 */
- mrb_define_class(mrb, "SyntaxError", e); /* 15.2.38 */
+ e = mrb_define_class(mrb, "ScriptError", mrb->eException_class); /* 15.2.37 */
+ mrb_define_class(mrb, "SyntaxError", e); /* 15.2.38 */
}
diff --git a/src/gc.c b/src/gc.c
index 2660ffcc7..4478b71f3 100644
--- a/src/gc.c
+++ b/src/gc.c
@@ -121,9 +121,9 @@ static double gc_total_time = 0;
static double
gettimeofday_time(void)
{
- struct timeval tv;
- gettimeofday(&tv, NULL);
- return tv.tv_sec + tv.tv_usec * 1e-6;
+ struct timeval tv;
+ gettimeofday(&tv, NULL);
+ return tv.tv_sec + tv.tv_usec * 1e-6;
}
#define GC_INVOKE_TIME_REPORT(with) do {\
diff --git a/src/numeric.c b/src/numeric.c
index 4c955a61e..56835edbf 100644
--- a/src/numeric.c
+++ b/src/numeric.c
@@ -309,7 +309,7 @@ flodivmod(mrb_state *mrb, mrb_float x, mrb_float y, mrb_float *divp, mrb_float *
}
else {
mod = fmod(x, y);
- if (isinf(x) && !isinf(y) && !isnan(y))
+ if (isinf(x) && isfinite(y))
div = x;
else
div = (x - mod) / y;
@@ -495,7 +495,7 @@ flo_finite_p(mrb_state *mrb, mrb_value num)
{
mrb_float value = mrb_float(num);
- return mrb_bool_value(!(isinf(value) || isnan(value)));
+ return mrb_bool_value(isfinite(value));
}
/* 15.2.9.3.10 */
@@ -626,7 +626,7 @@ flo_round(mrb_state *mrb, mrb_value num)
}
if (ndigits > 0) {
- if (isinf(number) || isnan(number)) return num;
+ if (!isfinite(number)) return num;
return mrb_float_value(mrb, number);
}
return mrb_fixnum_value((mrb_int)number);
diff --git a/src/range.c b/src/range.c
index b59b234ef..3e5af1894 100644
--- a/src/range.c
+++ b/src/range.c
@@ -8,6 +8,7 @@
#include "mruby/class.h"
#include "mruby/range.h"
#include "mruby/string.h"
+#include "mruby/array.h"
#define RANGE_CLASS (mrb_class_get(mrb, "Range"))
@@ -233,53 +234,30 @@ mrb_range_include(mrb_state *mrb, mrb_value range)
return mrb_bool_value(include_p);
}
-/*
- * call-seq:
- * rng.each {| i | block } => rng
- *
- * Iterates over the elements <i>rng</i>, passing each in turn to the
- * block. You can only iterate if the start object of the range
- * supports the +succ+ method (which means that you can't iterate over
- * ranges of +Float+ objects).
- *
- * (10..15).each do |n|
- * print n, ' '
- * end
- *
- * <em>produces:</em>
- *
- * 10 11 12 13 14 15
- */
-
-mrb_value
-mrb_range_each(mrb_state *mrb, mrb_value range)
-{
- return range;
-}
-
mrb_bool
-mrb_range_beg_len(mrb_state *mrb, mrb_value range, mrb_int *begp, mrb_int *lenp, mrb_int len)
+range_beg_len(mrb_state *mrb, mrb_value range, mrb_int *begp, mrb_int *lenp, mrb_int len, mrb_bool trunc)
{
mrb_int beg, end, b, e;
struct RRange *r = mrb_range_ptr(range);
- if (mrb_type(range) != MRB_TT_RANGE) {
- mrb_raise(mrb, E_TYPE_ERROR, "expected Range.");
- }
+ if (mrb_type(range) != MRB_TT_RANGE) return FALSE;
- beg = b = mrb_fixnum(r->edges->beg);
- end = e = mrb_fixnum(r->edges->end);
+ beg = b = mrb_int(mrb, r->edges->beg);
+ end = e = mrb_int(mrb, r->edges->end);
if (beg < 0) {
beg += len;
if (beg < 0) return FALSE;
}
- if (beg > len) return FALSE;
- if (end > len) end = len;
+ if (trunc) {
+ if (beg > len) return FALSE;
+ if (end > len) end = len;
+ }
if (end < 0) end += len;
- if (!r->excl && end < len) end++; /* include end point */
+ if (!r->excl && (!trunc || end < len))
+ end++; /* include end point */
len = end - beg;
if (len < 0) len = 0;
@@ -288,6 +266,12 @@ mrb_range_beg_len(mrb_state *mrb, mrb_value range, mrb_int *begp, mrb_int *lenp,
return TRUE;
}
+mrb_bool
+mrb_range_beg_len(mrb_state *mrb, mrb_value range, mrb_int *begp, mrb_int *lenp, mrb_int len)
+{
+ return range_beg_len(mrb, range, begp, lenp, len, TRUE);
+}
+
/* 15.2.14.4.12(x) */
/*
* call-seq:
@@ -395,6 +379,35 @@ range_initialize_copy(mrb_state *mrb, mrb_value copy)
return copy;
}
+mrb_value
+mrb_get_values_at(mrb_state *mrb, mrb_value obj, mrb_int olen, mrb_int argc, const mrb_value *argv, mrb_value (*func)(mrb_state*, mrb_value, mrb_int))
+{
+ mrb_int i, j, beg, len;
+ mrb_value result;
+ result = mrb_ary_new(mrb);
+
+ for (i = 0; i < argc; ++i) {
+ if (mrb_fixnum_p(argv[i])) {
+ mrb_ary_push(mrb, result, func(mrb, obj, mrb_fixnum(argv[i])));
+ }
+ else if (range_beg_len(mrb, argv[i], &beg, &len, olen, FALSE)) {
+ mrb_int const end = olen < beg + len ? olen : beg + len;
+ for (j = beg; j < end; ++j) {
+ mrb_ary_push(mrb, result, func(mrb, obj, j));
+ }
+
+ for (; j < beg + len; ++j) {
+ mrb_ary_push(mrb, result, mrb_nil_value());
+ }
+ }
+ else {
+ mrb_raisef(mrb, E_TYPE_ERROR, "invalid values selector: %S", argv[i]);
+ }
+ }
+
+ return result;
+}
+
void
mrb_init_range(mrb_state *mrb)
{
@@ -407,7 +420,6 @@ mrb_init_range(mrb_state *mrb)
mrb_define_method(mrb, r, "end", mrb_range_end, MRB_ARGS_NONE()); /* 15.2.14.4.5 */
mrb_define_method(mrb, r, "==", mrb_range_eq, MRB_ARGS_REQ(1)); /* 15.2.14.4.1 */
mrb_define_method(mrb, r, "===", mrb_range_include, MRB_ARGS_REQ(1)); /* 15.2.14.4.2 */
- mrb_define_method(mrb, r, "each", mrb_range_each, MRB_ARGS_NONE()); /* 15.2.14.4.4 */
mrb_define_method(mrb, r, "exclude_end?", mrb_range_excl, MRB_ARGS_NONE()); /* 15.2.14.4.6 */
mrb_define_method(mrb, r, "first", mrb_range_beg, MRB_ARGS_NONE()); /* 15.2.14.4.7 */
mrb_define_method(mrb, r, "include?", mrb_range_include, MRB_ARGS_REQ(1)); /* 15.2.14.4.8 */
diff --git a/src/variable.c b/src/variable.c
index 231697d47..f34735e75 100644
--- a/src/variable.c
+++ b/src/variable.c
@@ -118,7 +118,7 @@ iv_put(mrb_state *mrb, iv_tbl *t, mrb_sym sym, mrb_value val)
}
/*
- * Get a value for a symbol from the instance the variable table.
+ * Get a value for a symbol from the instance variable table.
*
* Parameters
* mrb
diff --git a/tasks/toolchains/visualcpp.rake b/tasks/toolchains/visualcpp.rake
index 1498ec3c9..a5726dce7 100644
--- a/tasks/toolchains/visualcpp.rake
+++ b/tasks/toolchains/visualcpp.rake
@@ -1,7 +1,8 @@
MRuby::Toolchain.new(:visualcpp) do |conf|
[conf.cc].each do |cc|
cc.command = ENV['CC'] || 'cl.exe'
- cc.flags = [ENV['CFLAGS'] || %w(/c /nologo /W3 /Zi /MD /O2 /D_CRT_SECURE_NO_WARNINGS)]
+ # C4013: implicit function declaration
+ cc.flags = [ENV['CFLAGS'] || %w(/c /nologo /W3 /we4013 /Zi /MD /O2 /D_CRT_SECURE_NO_WARNINGS)]
cc.include_paths = ["#{MRUBY_ROOT}/include"]
cc.defines = %w(DISABLE_GEMS MRB_STACK_EXTEND_DOUBLING)
cc.option_include_path = '/I%s'