mruby-data (mrb_data_initialize): update argument error conditions

- raise ArgumentError if arguments size differs from data size.
  Previously it accepted smaller argument size.

- the message is updated to "wrong number of arguments". It's not
  "struct" any longer.
This commit is contained in:
Yukihiro "Matz" Matsumoto
2022-11-29 16:08:46 +09:00
parent fce2c5131f
commit 138adbe282
+13 -27
View File
@@ -145,31 +145,21 @@ make_data(mrb_state *mrb, mrb_value members, struct RClass *klass)
/*
* call-seq:
* StructClass.new(arg, ...) -> obj
* StructClass[arg, ...] -> obj
* DataClass.new(arg, ...) -> obj
*
* Creates a new class, named by <i>aString</i>, containing accessor
* methods for the given symbols. If the name <i>aString</i> is
* omitted, an anonymous structure class will be created. Otherwise,
* the name of this struct will appear as a constant in class
* <code>Struct</code>, so it must be unique for all
* <code>Struct</code>s in the system and should start with a capital
* letter. Assigning a structure class to a constant effectively gives
* the class the name of the constant.
*
* <code>Struct::new</code> returns a new <code>Class</code> object,
* <code>Data::define</code> returns a new <code>Class</code> object,
* which can then be used to create specific instances of the new
* structure. The number of actual parameters must be
* less than or equal to the number of attributes defined for this
* class; unset parameters default to <code>nil</code>. Passing too many
* parameters will raise an <code>ArgumentError</code>.
* data structure. The number of actual parameters must be
* equal to the number of attributes defined for this class.
* Passing too many or too less parameters will raise an
* <code>ArgumentError</code>.
*
* The remaining methods listed in this section (class and instance)
* are defined for this generated class.
*
* # Create a structure named by its constant
* Customer = Struct.new(:name, :address) #=> Customer
* Customer.new("Dave", "123 Main") #=> #<struct Customer name="Dave", address="123 Main">
* Customer = Data.new(:name, :address) #=> Customer
* Customer.new("Dave", "123 Main") #=> #<data name="Dave", address="123 Main">
*/
static mrb_value
mrb_data_s_def(mrb_state *mrb, mrb_value klass)
@@ -219,16 +209,13 @@ mrb_data_initialize(mrb_state *mrb, mrb_value self)
mrb_get_args(mrb, "*!", &argv, &argc);
n = num_members(mrb, klass);
if (n < argc) {
mrb_raise(mrb, E_ARGUMENT_ERROR, "struct size differs");
if (n != argc) {
mrb_raise(mrb, E_ARGUMENT_ERROR, "wrong number of arguments");
}
for (i = 0; i < argc; i++) {
mrb_ary_set(mrb, self, i, argv[i]);
}
for (i = argc; i < n; i++) {
mrb_ary_set(mrb, self, i, mrb_nil_value());
}
return self;
}
@@ -254,7 +241,7 @@ mrb_data_init_copy(mrb_state *mrb, mrb_value copy)
*
* Equality---Returns <code>true</code> if <i>other_data</i> is
* equal to this one: they must be of the same class as generated by
* <code>Struct::define</code>, and all values of must be equal
* <code>Data::define</code>, and all values of must be equal
* (according to <code>Object#==</code>).
*
* Customer = Data.define(:name, :address, :zip)
@@ -385,15 +372,14 @@ mrb_data_to_s(mrb_state *mrb, mrb_value self)
* attributes together, using accessor methods, without having to write
* an explicit class.
*
* The <code>Struct</code> class is a generator of specific classes,
* The <code>Data</code> class is a generator of specific classes,
* each one of which is defined to hold a set of variables and their
* accessors. In these examples, we'll call the generated class
* "<i>Customer</i>Class," and we'll show an example instance of that
* class as "<i>Customer</i>Inst."
*
* In the descriptions that follow, the parameter <i>symbol</i> refers
* to a symbol, which is either a quoted string or a
* <code>Symbol</code> (such as <code>:name</code>).
* to a symbol, which is a <code>Symbol</code> (such as <code>:name</code>).
*/
void
mrb_mruby_data_gem_init(mrb_state* mrb)