Allow Class#allocate to be prohibited

Calling `Class#allocate` with `UnboundMethod#bind_call` usually succeeds without problems.
If this behavior does not make us happy, we can now prohibit it with `MRB_SET_INSTANCE_TT(klass, MRB_TT_UNDEF)`.

At the same time, it applies to the `Binding`, `Complex`, `Data`, `Float`, `Integer`, `Method`, `Rational` and `UnboundMethod` classes.
This commit is contained in:
dearblue
2023-04-09 21:14:07 +09:00
parent c3ba76794f
commit d3128ce58a
8 changed files with 13 additions and 5 deletions
+3
View File
@@ -1945,6 +1945,9 @@ mrb_instance_alloc(mrb_state *mrb, mrb_value cv)
ttype = MRB_TT_OBJECT;
}
if (ttype <= MRB_TT_CPTR) {
if (ttype == MRB_TT_UNDEF) {
mrb_raisef(mrb, E_TYPE_ERROR, "allocator undefined for %v", cv);
}
mrb_raisef(mrb, E_TYPE_ERROR, "can't create instance of %v", cv);
}
o = (struct RObject*)mrb_obj_alloc(mrb, ttype, c);
+1
View File
@@ -515,6 +515,7 @@ mrb_obj_alloc(mrb_state *mrb, enum mrb_vtype ttype, struct RClass *cls)
}
tt = MRB_INSTANCE_TT(cls);
if (tt != MRB_TT_FALSE &&
tt != MRB_TT_UNDEF &&
ttype != MRB_TT_SCLASS &&
ttype != MRB_TT_ICLASS &&
ttype != MRB_TT_ENV &&
+2 -2
View File
@@ -2219,7 +2219,7 @@ mrb_init_numeric(mrb_state *mrb)
/* Integer Class */
mrb->integer_class = integer = mrb_define_class(mrb, "Integer", numeric); /* 15.2.8 */
MRB_SET_INSTANCE_TT(integer, MRB_TT_INTEGER);
MRB_SET_INSTANCE_TT(integer, MRB_TT_UNDEF);
mrb_undef_class_method(mrb, integer, "new");
mrb_define_method(mrb, integer, "**", int_pow, MRB_ARGS_REQ(1));
mrb_define_method(mrb, integer, "<=>", num_cmp, MRB_ARGS_REQ(1)); /* 15.2.8.3.1 */
@@ -2264,7 +2264,7 @@ mrb_init_numeric(mrb_state *mrb)
#ifndef MRB_NO_FLOAT
/* Float Class */
mrb->float_class = fl = mrb_define_class(mrb, "Float", numeric); /* 15.2.9 */
MRB_SET_INSTANCE_TT(fl, MRB_TT_FLOAT);
MRB_SET_INSTANCE_TT(fl, MRB_TT_UNDEF);
mrb_undef_class_method(mrb, fl, "new");
mrb_define_method(mrb, fl, "**", flo_pow, MRB_ARGS_REQ(1));
mrb_define_method(mrb, fl, "/", flo_div, MRB_ARGS_REQ(1)); /* 15.2.9.3.6 */