summaryrefslogtreecommitdiffhomepage
path: root/mrbgems/mruby-complex/src/complex.c
diff options
context:
space:
mode:
authordearblue <[email protected]>2019-06-02 11:05:53 +0900
committerdearblue <[email protected]>2019-06-05 23:19:16 +0900
commit82831c6acd116852e918094e940266152897056e (patch)
tree35c6d76200efc647525f55e3327774262193969f /mrbgems/mruby-complex/src/complex.c
parent97e999c409fbbe46fd4154e4be292f182f42b771 (diff)
downloadmruby-82831c6acd116852e918094e940266152897056e.tar.gz
mruby-82831c6acd116852e918094e940266152897056e.zip
Fix not frozen in `Complex` method by `RData`
Object allocation was separated, and initialization was made common.
Diffstat (limited to 'mrbgems/mruby-complex/src/complex.c')
-rw-r--r--mrbgems/mruby-complex/src/complex.c42
1 files changed, 24 insertions, 18 deletions
diff --git a/mrbgems/mruby-complex/src/complex.c b/mrbgems/mruby-complex/src/complex.c
index a727eb604..8a0569d68 100644
--- a/mrbgems/mruby-complex/src/complex.c
+++ b/mrbgems/mruby-complex/src/complex.c
@@ -15,18 +15,15 @@ struct mrb_complex {
#define complex_ptr(mrb, v) (struct mrb_complex*)mrb_istruct_ptr(v)
-static mrb_value
-complex_new(mrb_state *mrb, mrb_float real, mrb_float imaginary)
+static struct RBasic*
+complex_alloc(mrb_state *mrb, struct RClass *c, struct mrb_complex **p)
{
- struct RClass *c = mrb_class_get(mrb, "Complex");
- struct RIStruct *s = (struct RIStruct*)mrb_obj_alloc(mrb, MRB_TT_ISTRUCT, c);
- mrb_value comp = mrb_obj_value(s);
- struct mrb_complex *p = complex_ptr(mrb, comp);
- p->real = real;
- p->imaginary = imaginary;
- MRB_SET_FROZEN_FLAG(s);
+ struct RIStruct *s;
+
+ s = (struct RIStruct*)mrb_obj_alloc(mrb, MRB_TT_ISTRUCT, c);
+ *p = (struct mrb_complex*)s->inline_data;
- return comp;
+ return (struct RBasic*)s;
}
#else
@@ -35,18 +32,14 @@ complex_new(mrb_state *mrb, mrb_float real, mrb_float imaginary)
static const struct mrb_data_type mrb_complex_type = {"Complex", mrb_free};
-static mrb_value
-complex_new(mrb_state *mrb, mrb_float real, mrb_float imaginary)
+static struct RBasic*
+complex_alloc(mrb_state *mrb, struct RClass *c, struct mrb_complex **p)
{
- struct RClass *c = mrb_class_get(mrb, "Complex");
- struct mrb_complex *p;
struct RData *d;
- Data_Make_Struct(mrb, c, struct mrb_complex, &mrb_complex_type, p, d);
- p->real = real;
- p->imaginary = imaginary;
+ Data_Make_Struct(mrb, c, struct mrb_complex, &mrb_complex_type, *p, d);
- return mrb_obj_value(d);
+ return (struct RBasic*)d;
}
static struct mrb_complex*
@@ -63,6 +56,19 @@ complex_ptr(mrb_state *mrb, mrb_value v)
#endif
static mrb_value
+complex_new(mrb_state *mrb, mrb_float real, mrb_float imaginary)
+{
+ struct RClass *c = mrb_class_get(mrb, "Complex");
+ struct mrb_complex *p;
+ struct RBasic *comp = complex_alloc(mrb, c, &p);
+ p->real = real;
+ p->imaginary = imaginary;
+ MRB_SET_FROZEN_FLAG(comp);
+
+ return mrb_obj_value(comp);
+}
+
+static mrb_value
complex_real(mrb_state *mrb, mrb_value self)
{
struct mrb_complex *p = complex_ptr(mrb, self);