Permalink
Browse files

Don't allow array parameter in Struct.new

  • Loading branch information...
clayton-shopify authored and bouk committed Nov 16, 2016
1 parent a630c4f commit b60d6c42aaa10ac99a03a92a3a34da4bee066af1
Showing with 14 additions and 18 deletions.
  1. +8 −18 mrbgems/mruby-struct/src/struct.c
  2. +6 −0 mrbgems/mruby-struct/test/struct.rb
@@ -273,25 +273,15 @@ mrb_struct_s_def(mrb_state *mrb, mrb_value klass)
}
else {
if (argc > 0) name = argv[0];
if (argc > 1) rest = argv[1];
if (mrb_array_p(rest)) {
if (!mrb_nil_p(name) && mrb_symbol_p(name)) {
/* 1stArgument:symbol -> name=nil rest=argv[0]-[n] */
mrb_ary_unshift(mrb, rest, name);
name = mrb_nil_value();
}
}
else {
pargv = &argv[1];
argcnt = argc-1;
if (!mrb_nil_p(name) && mrb_symbol_p(name)) {
/* 1stArgument:symbol -> name=nil rest=argv[0]-[n] */
name = mrb_nil_value();
pargv = &argv[0];
argcnt++;
}
rest = mrb_ary_new_from_values(mrb, argcnt, pargv);
pargv = &argv[1];
argcnt = argc-1;
if (!mrb_nil_p(name) && mrb_symbol_p(name)) {
/* 1stArgument:symbol -> name=nil rest=argv[0]-[n] */
name = mrb_nil_value();
pargv = &argv[0];
argcnt++;
}
rest = mrb_ary_new_from_values(mrb, argcnt, pargv);
for (i=0; i<RARRAY_LEN(rest); i++) {
id = mrb_obj_to_sym(mrb, RARRAY_PTR(rest)[i]);
mrb_ary_set(mrb, rest, i, mrb_symbol_value(id));
@@ -158,3 +158,9 @@
assert_equal 1, a.dig(:purple, :red)
assert_equal 1, a.dig(1, 0)
end
assert("Struct.new does not allow array") do
assert_raise(TypeError) do
Struct.new("Test", [:a])
end
end

0 comments on commit b60d6c4

Please sign in to comment.