/* * call-seq: * Struct.new( [aString] [, aSym]+> ) -> StructClass * StructClass.new(arg, ...) -> obj * StructClass[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, * 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>. * * The remaining methods listed in this section (class and instance) * are defined for this generated class. * * # Create a structure with a name in Struct * Struct.new("Customer", :name, :address) #=> Struct::Customer * Struct::Customer.new("Dave", "123 Main") #=> #<struct Struct::Customer name="Dave", address="123 Main"> * * # 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"> */ static mrb_value mrb_struct_s_def(mrb_state *mrb, mrb_value klass) { mrb_value name, rest; mrb_value *pargv; int argcnt; mrb_int i; mrb_value b, st; mrb_sym id; mrb_value *argv; int argc; name = mrb_nil_value(); rest = mrb_nil_value(); mrb_get_args(mrb, "*&", &argv, &argc, &b); if (argc == 0) { /* special case to avoid crash */ rest = mrb_ary_new(mrb); } 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); } for (i=0; i<RARRAY_LEN(rest); i++) { id = mrb_obj_to_sym(mrb, RARRAY_PTR(rest)[i]); RARRAY_PTR(rest)[i] = mrb_symbol_value(id); } } st = make_struct(mrb, name, rest, struct_class(mrb)); if (!mrb_nil_p(b)) { mrb_yield_with_class(mrb, b, 1, &st, st, mrb_class_ptr(klass)); } return st; }
/* * call-seq: * Struct.new( [aString] [, aSym]+> ) -> StructClass * StructClass.new(arg, ...) -> obj * StructClass[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, * 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>. * * The remaining methods listed in this section (class and instance) * are defined for this generated class. * * # Create a structure with a name in Struct * Struct.new("Customer", :name, :address) #=> Struct::Customer * Struct::Customer.new("Dave", "123 Main") #=> #<struct Struct::Customer name="Dave", address="123 Main"> * * # 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"> */ static mrb_value mrb_struct_s_def(mrb_state *mrb, mrb_value klass) { mrb_value name, rest; mrb_value *pargv; int argcnt; long i; mrb_value b, st; mrb_sym id; mrb_value *argv; int argc; name = mrb_nil_value(); rest = mrb_nil_value(); mrb_get_args(mrb, "&*", &b, &argv, &argc); if (argc > 0) name = argv[0]; if (argc > 1) rest = argv[1]; //mrb_scan_args(argc, argv, "1*", &name, &rest); if (mrb_type(rest) == MRB_TT_ARRAY) { if (!mrb_nil_p(name) && SYMBOL_P(name)) { /* 1stArgument:symbol -> name=nil rest=argv[0]-[n] */ mrb_ary_unshift(mrb, rest, name); name = mrb_nil_value(); } for (i=0; i<RARRAY_LEN(rest); i++) { id = mrb_to_id(mrb, RARRAY_PTR(rest)[i]); RARRAY_PTR(rest)[i] = mrb_str_new_cstr(mrb, mrb_sym2name(mrb, id)); } } else { pargv = &argv[1]; argcnt = argc-1; if (!mrb_nil_p(name) && SYMBOL_P(name)) { /* 1stArgument:symbol -> name=nil rest=argv[0]-[n] */ //mrb_ary_unshift(mrb, rest, name); name = mrb_nil_value(); pargv = &argv[0]; argcnt++; } rest = mrb_ary_new_from_values(mrb, pargv, argcnt); } st = make_struct(mrb, name, rest, struct_class(mrb)); if (!mrb_nil_p(b)) { mrb_funcall(mrb, b, "call", 1, &st); } return st; }
static inline mrb_value struct_ivar_get(mrb_state *mrb, mrb_value c, mrb_sym id) { struct RClass* kclass; struct RClass* sclass = struct_class(mrb); mrb_value ans; for (;;) { ans = mrb_iv_get(mrb, c, id); if (!mrb_nil_p(ans)) return ans; kclass = RCLASS_SUPER(c); if (kclass == 0 || kclass == sclass) return mrb_nil_value(); c = mrb_obj_value(kclass); } }
mrb_value mrb_struct_define(mrb_state *mrb, const char *name, ...) { va_list ar; mrb_value nm, ary; char *mem; if (!name) nm = mrb_nil_value(); else nm = mrb_str_new_cstr(mrb, name); ary = mrb_ary_new(mrb); va_start(ar, name); while ((mem = va_arg(ar, char*)) != 0) { mrb_sym slot = mrb_intern_cstr(mrb, mem); mrb_ary_push(mrb, ary, mrb_symbol_value(slot)); } va_end(ar); return make_struct(mrb, nm, ary, struct_class(mrb)); }