From fccec8964a1b30cef8ff9d97e0e01f3a348e318b Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Tue, 21 May 2019 16:11:25 +0900 Subject: Implements part of `Complex` class in C. --- mrbgems/mruby-complex/src/complex.c | 116 ++++++++++++++++++++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 mrbgems/mruby-complex/src/complex.c (limited to 'mrbgems/mruby-complex/src/complex.c') diff --git a/mrbgems/mruby-complex/src/complex.c b/mrbgems/mruby-complex/src/complex.c new file mode 100644 index 000000000..5de0ae9a4 --- /dev/null +++ b/mrbgems/mruby-complex/src/complex.c @@ -0,0 +1,116 @@ +#include +#include +#include + +struct mrb_complex { + mrb_float real; + mrb_float imaginary; +}; + +#include + +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) +{ + struct RClass *c = mrb_class_get(mrb, "Complex"); + struct mrb_complex *p; + + p = (struct mrb_complex*)mrb_malloc(mrb, sizeof(struct mrb_complex)); + p->real = real; + p->imaginary = imaginary; + + return mrb_obj_value(Data_Wrap_Struct(mrb, c, &mrb_complex_type, p)); +} + +static struct mrb_complex* +complex_ptr(mrb_state *mrb, mrb_value v) +{ + struct mrb_complex *p; + + p = DATA_GET_PTR(mrb, v, &mrb_complex_type, struct mrb_complex); + if (!p) { + mrb_raise(mrb, E_ARGUMENT_ERROR, "uninitialized complex"); + } + return p; +} + +static mrb_value +complex_real(mrb_state *mrb, mrb_value self) +{ + struct mrb_complex *p = complex_ptr(mrb, self); + return mrb_float_value(mrb, p->real); +} + +static mrb_value +complex_imaginary(mrb_state *mrb, mrb_value self) +{ + struct mrb_complex *p = complex_ptr(mrb, self); + return mrb_float_value(mrb, p->imaginary); +} + +static mrb_value +complex_s_new(mrb_state *mrb, mrb_value self) +{ + mrb_float real, imaginary; + + mrb_get_args(mrb, "ff", &real, &imaginary); + return complex_new(mrb, real, imaginary); +} + +#ifndef MRB_WITHOUT_FLOAT +static mrb_value +complex_to_f(mrb_state *mrb, mrb_value self) +{ + struct mrb_complex *p = complex_ptr(mrb, self); + + if (p->imaginary != 0) { + mrb_raisef(mrb, E_RANGE_ERROR, "can't convert %S into Float", self); + } + + return mrb_float_value(mrb, p->real); +} +#endif + +static mrb_value +complex_to_i(mrb_state *mrb, mrb_value self) +{ + struct mrb_complex *p = complex_ptr(mrb, self); + + if (p->imaginary != 0) { + mrb_raisef(mrb, E_RANGE_ERROR, "can't convert %S into Float", self); + } + return mrb_int_value(mrb, p->real); +} + +static mrb_value +complex_to_c(mrb_state *mrb, mrb_value self) +{ + return self; +} + +void mrb_mruby_complex_gem_init(mrb_state *mrb) +{ + struct RClass *comp; + +#ifdef COMPLEX_USE_ISTRUCT + mrb_assert(sizeof(struct mrb_complex) < ISTRUCT_DATA_SIZE); +#endif + comp = mrb_define_class(mrb, "Complex", mrb_class_get(mrb, "Numeric")); + //MRB_SET_INSTANCE_TT(comp, MRB_TT_ISTRUCT); + mrb_undef_class_method(mrb, comp, "new"); + mrb_define_class_method(mrb, comp, "_new", complex_s_new, MRB_ARGS_REQ(2)); + mrb_define_method(mrb, comp, "real", complex_real, MRB_ARGS_NONE()); + mrb_define_method(mrb, comp, "imaginary", complex_imaginary, MRB_ARGS_NONE()); +#ifndef MRB_WITHOUT_FLOAT + mrb_define_method(mrb, comp, "to_f", complex_to_f, MRB_ARGS_NONE()); +#endif + mrb_define_method(mrb, comp, "to_i", complex_to_i, MRB_ARGS_NONE()); + mrb_define_method(mrb, comp, "to_c", complex_to_c, MRB_ARGS_NONE()); +} + +void +mrb_mruby_complex_gem_final(mrb_state* mrb) +{ +} -- cgit v1.2.3 From c02e63eb77b68377239287acccf371516177a8b9 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Tue, 21 May 2019 16:17:21 +0900 Subject: Use `MRB_TT_ISTRUCT` for `Complex` numbers if possible. --- mrbgems/mruby-complex/src/complex.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'mrbgems/mruby-complex/src/complex.c') diff --git a/mrbgems/mruby-complex/src/complex.c b/mrbgems/mruby-complex/src/complex.c index 5de0ae9a4..c6fb7a829 100644 --- a/mrbgems/mruby-complex/src/complex.c +++ b/mrbgems/mruby-complex/src/complex.c @@ -7,6 +7,29 @@ struct mrb_complex { mrb_float imaginary; }; +#if defined(MRB_64BIT) || defined(MRB_USE_FLOAT) + +#define COMPLEX_USE_ISTRUCT +/* use TT_ISTRUCT */ +#include + +#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 static const struct mrb_data_type mrb_complex_type = {"Complex", mrb_free}; @@ -35,6 +58,7 @@ complex_ptr(mrb_state *mrb, mrb_value v) } return p; } +#endif static mrb_value complex_real(mrb_state *mrb, mrb_value self) -- cgit v1.2.3 From 48903850e9041e74c526fef5e63857007d2cac38 Mon Sep 17 00:00:00 2001 From: KOBAYASHI Shuji Date: Thu, 23 May 2019 13:40:49 +0900 Subject: Freeze `Rational` and `Complex` objects --- mrbgems/mruby-complex/src/complex.c | 1 + mrbgems/mruby-complex/test/complex.rb | 8 +++++++- mrbgems/mruby-rational/src/rational.c | 1 + mrbgems/mruby-rational/test/rational.rb | 6 ++++++ 4 files changed, 15 insertions(+), 1 deletion(-) (limited to 'mrbgems/mruby-complex/src/complex.c') diff --git a/mrbgems/mruby-complex/src/complex.c b/mrbgems/mruby-complex/src/complex.c index c6fb7a829..1b030c317 100644 --- a/mrbgems/mruby-complex/src/complex.c +++ b/mrbgems/mruby-complex/src/complex.c @@ -24,6 +24,7 @@ complex_new(mrb_state *mrb, mrb_float real, mrb_float imaginary) struct mrb_complex *p = complex_ptr(mrb, comp); p->real = real; p->imaginary = imaginary; + MRB_SET_FROZEN_FLAG(s); return comp; } diff --git a/mrbgems/mruby-complex/test/complex.rb b/mrbgems/mruby-complex/test/complex.rb index e7fcc7322..6996eb214 100644 --- a/mrbgems/mruby-complex/test/complex.rb +++ b/mrbgems/mruby-complex/test/complex.rb @@ -1,5 +1,5 @@ def assert_complex(real, exp) - assert_float real.real, exp.real + assert_float real.real, exp.real assert_float real.imaginary, exp.imaginary end @@ -126,3 +126,9 @@ assert 'Complex::to_i' do Complex(1, 2).to_i end end + +assert 'Complex#frozen?' do + assert_predicate(1i, :frozen?) + assert_predicate(Complex(2,3), :frozen?) + assert_predicate(4+5i, :frozen?) +end diff --git a/mrbgems/mruby-rational/src/rational.c b/mrbgems/mruby-rational/src/rational.c index 2a3f6df09..fa061c0b8 100644 --- a/mrbgems/mruby-rational/src/rational.c +++ b/mrbgems/mruby-rational/src/rational.c @@ -37,6 +37,7 @@ rational_new(mrb_state *mrb, mrb_int numerator, mrb_int denominator) struct mrb_rational *p = rational_ptr(rat); p->numerator = numerator; p->denominator = denominator; + MRB_SET_FROZEN_FLAG(s); return rat; } diff --git a/mrbgems/mruby-rational/test/rational.rb b/mrbgems/mruby-rational/test/rational.rb index 914f8505e..1ed3b3a07 100644 --- a/mrbgems/mruby-rational/test/rational.rb +++ b/mrbgems/mruby-rational/test/rational.rb @@ -278,3 +278,9 @@ assert 'Rational#negative?' do assert_not_predicate(Rational(2,3), :negative?) assert_not_predicate(Rational(0), :negative?) end + +assert 'Rational#frozen?' do + assert_predicate(1r, :frozen?) + assert_predicate(Rational(2,3), :frozen?) + assert_predicate(4/5r, :frozen?) +end -- cgit v1.2.3 From 4261ba944330f27dcb80e5e4580f73bd6b3a7106 Mon Sep 17 00:00:00 2001 From: "Yukihiro \"Matz\" Matsumoto" Date: Fri, 24 May 2019 13:15:26 +0900 Subject: Remove some overhead from methods defined in Ruby in Complex. --- mrbgems/mruby-complex/mrblib/complex.rb | 37 +++++++++------------------------ mrbgems/mruby-complex/src/complex.c | 9 ++++---- 2 files changed, 15 insertions(+), 31 deletions(-) (limited to 'mrbgems/mruby-complex/src/complex.c') diff --git a/mrbgems/mruby-complex/mrblib/complex.rb b/mrbgems/mruby-complex/mrblib/complex.rb index 4c0c19c70..0299e7675 100644 --- a/mrbgems/mruby-complex/mrblib/complex.rb +++ b/mrbgems/mruby-complex/mrblib/complex.rb @@ -1,19 +1,8 @@ class Complex < Numeric - def initialize(real, imaginary) - real = real.to_f unless real.is_a? Numeric - imaginary = imaginary.to_f unless imaginary.is_a? Numeric - @real = real - @imaginary = imaginary - end - def self.polar(abs, arg = 0) Complex(abs * Math.cos(arg), abs * Math.sin(arg)) end - def self.rectangular(real, imaginary = 0) - _new(real, imaginary) - end - def inspect "(#{to_s})" end @@ -23,43 +12,43 @@ class Complex < Numeric end def +@ - Complex._new(real, imaginary) + Complex(real, imaginary) end def -@ - Complex._new(-real, -imaginary) + Complex(-real, -imaginary) end def +(rhs) if rhs.is_a? Complex - Complex._new(real + rhs.real, imaginary + rhs.imaginary) + Complex(real + rhs.real, imaginary + rhs.imaginary) elsif rhs.is_a? Numeric - Complex._new(real + rhs, imaginary) + Complex(real + rhs, imaginary) end end def -(rhs) if rhs.is_a? Complex - Complex._new(real - rhs.real, imaginary - rhs.imaginary) + Complex(real - rhs.real, imaginary - rhs.imaginary) elsif rhs.is_a? Numeric - Complex._new(real - rhs, imaginary) + Complex(real - rhs, imaginary) end end def *(rhs) if rhs.is_a? Complex - Complex._new(real * rhs.real - imaginary * rhs.imaginary, real * rhs.imaginary + rhs.real * imaginary) + Complex(real * rhs.real - imaginary * rhs.imaginary, real * rhs.imaginary + rhs.real * imaginary) elsif rhs.is_a? Numeric - Complex._new(real * rhs, imaginary * rhs) + Complex(real * rhs, imaginary * rhs) end end def /(rhs) if rhs.is_a? Complex div = rhs.real * rhs.real + rhs.imaginary * rhs.imaginary - Complex._new((real * rhs.real + imaginary * rhs.imaginary) / div, (rhs.real * imaginary - real * rhs.imaginary) / div) + Complex((real * rhs.real + imaginary * rhs.imaginary) / div, (rhs.real * imaginary - real * rhs.imaginary) / div) elsif rhs.is_a? Numeric - Complex._new(real / rhs, imaginary / rhs) + Complex(real / rhs, imaginary / rhs) end end alias_method :quo, :/ @@ -117,12 +106,6 @@ class Complex < Numeric alias_method :imag, :imaginary end -module Kernel - def Complex(real, imaginary = 0) - Complex.rectangular(real, imaginary) - end -end - [Fixnum, Float].each do |cls| [:+, :-, :*, :/, :==].each do |op| cls.instance_exec do diff --git a/mrbgems/mruby-complex/src/complex.c b/mrbgems/mruby-complex/src/complex.c index 1b030c317..0678d4b26 100644 --- a/mrbgems/mruby-complex/src/complex.c +++ b/mrbgems/mruby-complex/src/complex.c @@ -76,11 +76,11 @@ complex_imaginary(mrb_state *mrb, mrb_value self) } static mrb_value -complex_s_new(mrb_state *mrb, mrb_value self) +complex_s_rect(mrb_state *mrb, mrb_value self) { - mrb_float real, imaginary; + mrb_float real, imaginary = 0.0; - mrb_get_args(mrb, "ff", &real, &imaginary); + mrb_get_args(mrb, "f|f", &real, &imaginary); return complex_new(mrb, real, imaginary); } @@ -125,7 +125,8 @@ void mrb_mruby_complex_gem_init(mrb_state *mrb) comp = mrb_define_class(mrb, "Complex", mrb_class_get(mrb, "Numeric")); //MRB_SET_INSTANCE_TT(comp, MRB_TT_ISTRUCT); mrb_undef_class_method(mrb, comp, "new"); - mrb_define_class_method(mrb, comp, "_new", complex_s_new, MRB_ARGS_REQ(2)); + mrb_define_class_method(mrb, comp, "rectangular", complex_s_rect, MRB_ARGS_REQ(1)|MRB_ARGS_OPT(1)); + mrb_define_method(mrb, mrb->kernel_module, "Complex", complex_s_rect, MRB_ARGS_REQ(1)|MRB_ARGS_OPT(1)); mrb_define_method(mrb, comp, "real", complex_real, MRB_ARGS_NONE()); mrb_define_method(mrb, comp, "imaginary", complex_imaginary, MRB_ARGS_NONE()); #ifndef MRB_WITHOUT_FLOAT -- cgit v1.2.3 From 0bdb185a6fc1a6440b90b015bdb9d137db57ab77 Mon Sep 17 00:00:00 2001 From: KOBAYASHI Shuji Date: Sun, 26 May 2019 20:17:03 +0900 Subject: Add `Complex.rect` --- mrbgems/mruby-complex/src/complex.c | 1 + 1 file changed, 1 insertion(+) (limited to 'mrbgems/mruby-complex/src/complex.c') diff --git a/mrbgems/mruby-complex/src/complex.c b/mrbgems/mruby-complex/src/complex.c index 0678d4b26..5371332cd 100644 --- a/mrbgems/mruby-complex/src/complex.c +++ b/mrbgems/mruby-complex/src/complex.c @@ -126,6 +126,7 @@ void mrb_mruby_complex_gem_init(mrb_state *mrb) //MRB_SET_INSTANCE_TT(comp, MRB_TT_ISTRUCT); mrb_undef_class_method(mrb, comp, "new"); mrb_define_class_method(mrb, comp, "rectangular", complex_s_rect, MRB_ARGS_REQ(1)|MRB_ARGS_OPT(1)); + mrb_define_class_method(mrb, comp, "rect", complex_s_rect, MRB_ARGS_REQ(1)|MRB_ARGS_OPT(1)); mrb_define_method(mrb, mrb->kernel_module, "Complex", complex_s_rect, MRB_ARGS_REQ(1)|MRB_ARGS_OPT(1)); mrb_define_method(mrb, comp, "real", complex_real, MRB_ARGS_NONE()); mrb_define_method(mrb, comp, "imaginary", complex_imaginary, MRB_ARGS_NONE()); -- cgit v1.2.3 From 97e999c409fbbe46fd4154e4be292f182f42b771 Mon Sep 17 00:00:00 2001 From: dearblue Date: Sun, 2 Jun 2019 10:43:54 +0900 Subject: Fix memory leak in `Complex` method by `RData` If `Data_Wrap_Struct()` raises a `NoMemoryError` exception, it will leak memory if it does `mrb_malloc()` first. --- mrbgems/mruby-complex/src/complex.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'mrbgems/mruby-complex/src/complex.c') diff --git a/mrbgems/mruby-complex/src/complex.c b/mrbgems/mruby-complex/src/complex.c index 5371332cd..a727eb604 100644 --- a/mrbgems/mruby-complex/src/complex.c +++ b/mrbgems/mruby-complex/src/complex.c @@ -40,12 +40,13 @@ complex_new(mrb_state *mrb, mrb_float real, mrb_float imaginary) { struct RClass *c = mrb_class_get(mrb, "Complex"); struct mrb_complex *p; + struct RData *d; - p = (struct mrb_complex*)mrb_malloc(mrb, sizeof(struct mrb_complex)); + Data_Make_Struct(mrb, c, struct mrb_complex, &mrb_complex_type, p, d); p->real = real; p->imaginary = imaginary; - return mrb_obj_value(Data_Wrap_Struct(mrb, c, &mrb_complex_type, p)); + return mrb_obj_value(d); } static struct mrb_complex* -- cgit v1.2.3 From 82831c6acd116852e918094e940266152897056e Mon Sep 17 00:00:00 2001 From: dearblue Date: Sun, 2 Jun 2019 11:05:53 +0900 Subject: Fix not frozen in `Complex` method by `RData` Object allocation was separated, and initialization was made common. --- mrbgems/mruby-complex/src/complex.c | 42 +++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 18 deletions(-) (limited to 'mrbgems/mruby-complex/src/complex.c') 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* @@ -62,6 +55,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) { -- cgit v1.2.3