From 138adbe282b14969bbc34bd47277934a7cbcc446 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Tue, 29 Nov 2022 16:08:46 +0900 Subject: [PATCH] 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. --- mrbgems/mruby-data/src/data.c | 40 ++++++++++++----------------------- 1 file changed, 13 insertions(+), 27 deletions(-) diff --git a/mrbgems/mruby-data/src/data.c b/mrbgems/mruby-data/src/data.c index 28b703105..56cfe7905 100644 --- a/mrbgems/mruby-data/src/data.c +++ b/mrbgems/mruby-data/src/data.c @@ -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 aString, containing accessor - * methods for the given symbols. If the name aString is - * omitted, an anonymous structure class will be created. Otherwise, - * the name of this struct will appear as a constant in class - * Struct, so it must be unique for all - * Structs 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. - * - * Struct::new returns a new Class object, + * Data::define returns a new Class 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 nil. Passing too many - * parameters will raise an ArgumentError. + * 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 + * ArgumentError. * * 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") #=> # + * Customer = Data.new(:name, :address) #=> Customer + * Customer.new("Dave", "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 true if other_data is * equal to this one: they must be of the same class as generated by - * Struct::define, and all values of must be equal + * Data::define, and all values of must be equal * (according to Object#==). * * 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 Struct class is a generator of specific classes, + * The Data 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 * "CustomerClass," and we'll show an example instance of that * class as "CustomerInst." * * In the descriptions that follow, the parameter symbol refers - * to a symbol, which is either a quoted string or a - * Symbol (such as :name). + * to a symbol, which is a Symbol (such as :name). */ void mrb_mruby_data_gem_init(mrb_state* mrb)