Use `MRB_TT_ISTRUCT` for `Complex` numbers if possible.

parent fccec896
...@@ -7,6 +7,29 @@ struct mrb_complex { ...@@ -7,6 +7,29 @@ struct mrb_complex {
mrb_float imaginary; mrb_float imaginary;
}; };
#if defined(MRB_64BIT) || defined(MRB_USE_FLOAT)
#define COMPLEX_USE_ISTRUCT
/* use TT_ISTRUCT */
#include <mruby/istruct.h>
#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)
{
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;
return comp;
}
#else
/* use TT_DATA */
#include <mruby/data.h> #include <mruby/data.h>
static const struct mrb_data_type mrb_complex_type = {"Complex", mrb_free}; static const struct mrb_data_type mrb_complex_type = {"Complex", mrb_free};
...@@ -35,6 +58,7 @@ complex_ptr(mrb_state *mrb, mrb_value v) ...@@ -35,6 +58,7 @@ complex_ptr(mrb_state *mrb, mrb_value v)
} }
return p; return p;
} }
#endif
static mrb_value static mrb_value
complex_real(mrb_state *mrb, mrb_value self) complex_real(mrb_state *mrb, mrb_value self)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment