Skip to content

Commit 9fbfcd8

Browse files
committed
Error handling for unary and binary operations, one arg and two arg functions
1 parent 8cf9e08 commit 9fbfcd8

File tree

5 files changed

+63
-38
lines changed

5 files changed

+63
-38
lines changed

ext/symengine/ruby_basic.c

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ VALUE cbasic_alloc(VALUE klass)
1919
}
2020

2121
VALUE cbasic_binary_op(VALUE self, VALUE operand2,
22-
void (*cwfunc_ptr)(basic_struct *, const basic_struct *,
23-
const basic_struct *))
22+
int (*cwfunc_ptr)(basic_struct *, const basic_struct *,
23+
const basic_struct *))
2424
{
2525
basic_struct *this, *cresult;
2626
VALUE result;
@@ -32,28 +32,37 @@ VALUE cbasic_binary_op(VALUE self, VALUE operand2,
3232
sympify(operand2, cbasic_operand2);
3333

3434
cresult = basic_new_heap();
35-
cwfunc_ptr(cresult, this, cbasic_operand2);
36-
result = Data_Wrap_Struct(Klass_of_Basic(cresult), NULL, cbasic_free_heap,
37-
cresult);
38-
basic_free_stack(cbasic_operand2);
3935

40-
return result;
36+
int error_code = cwfunc_ptr(cresult, this, cbasic_operand2);
37+
if (error_code == 0) {
38+
result = Data_Wrap_Struct(Klass_of_Basic(cresult), NULL,
39+
cbasic_free_heap, cresult);
40+
basic_free_stack(cbasic_operand2);
41+
return result;
42+
} else {
43+
basic_free_stack(cbasic_operand2);
44+
rb_raise(rb_eRuntimeError, "Runtime Error");
45+
}
4146
}
4247

4348
VALUE cbasic_unary_op(VALUE self,
44-
void (*cwfunc_ptr)(basic_struct *, const basic_struct *))
49+
int (*cwfunc_ptr)(basic_struct *, const basic_struct *))
4550
{
4651
basic_struct *this, *cresult;
4752
VALUE result;
4853

4954
Data_Get_Struct(self, basic_struct, this);
5055

5156
cresult = basic_new_heap();
52-
cwfunc_ptr(cresult, this);
53-
result = Data_Wrap_Struct(Klass_of_Basic(cresult), NULL, cbasic_free_heap,
54-
cresult);
5557

56-
return result;
58+
int error_code = cwfunc_ptr(cresult, this);
59+
if (error_code == 0) {
60+
result = Data_Wrap_Struct(Klass_of_Basic(cresult), NULL,
61+
cbasic_free_heap, cresult);
62+
return result;
63+
} else {
64+
rb_raise(rb_eRuntimeError, "Runtime Error");
65+
}
5766
}
5867

5968
VALUE cbasic_add(VALUE self, VALUE operand2)

ext/symengine/ruby_basic.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ void cbasic_free_heap(void *ptr);
1313
VALUE cbasic_alloc(VALUE klass);
1414

1515
VALUE cbasic_binary_op(VALUE self, VALUE operand2,
16-
void (*cwfunc_ptr)(basic_struct *, const basic_struct *,
17-
const basic_struct *));
16+
int (*cwfunc_ptr)(basic_struct *, const basic_struct *,
17+
const basic_struct *));
1818

1919
VALUE cbasic_unary_op(VALUE self,
20-
void (*cwfunc_ptr)(basic_struct *, const basic_struct *));
20+
int (*cwfunc_ptr)(basic_struct *, const basic_struct *));
2121

2222
VALUE cbasic_add(VALUE self, VALUE operand2);
2323

ext/symengine/ruby_utils.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,14 @@ VALUE cutils_evalf(VALUE self, VALUE operand, VALUE prec, VALUE real)
2323
cresult = basic_new_heap();
2424

2525
sympify(operand, cresult);
26-
basic_evalf(cresult, cresult, NUM2INT(prec), (real == Qtrue ? 1 : 0));
27-
result = Data_Wrap_Struct(Klass_of_Basic(cresult), NULL, cbasic_free_heap,
28-
cresult);
29-
30-
return result;
26+
int error_code
27+
= basic_evalf(cresult, cresult, NUM2INT(prec), (real == Qtrue ? 1 : 0));
28+
29+
if (error_code == 0) {
30+
result = Data_Wrap_Struct(Klass_of_Basic(cresult), NULL,
31+
cbasic_free_heap, cresult);
32+
return result;
33+
} else {
34+
rb_raise(rb_eRuntimeError, "Runtime Error");
35+
}
3136
}

ext/symengine/symengine_utils.c

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ VALUE Klass_of_Basic(const basic_struct *basic_ptr)
199199
}
200200
}
201201

202-
VALUE function_onearg(void (*cwfunc_ptr)(basic_struct *, const basic_struct *),
202+
VALUE function_onearg(int (*cwfunc_ptr)(basic_struct *, const basic_struct *),
203203
VALUE operand1)
204204
{
205205
basic_struct *cresult;
@@ -210,16 +210,21 @@ VALUE function_onearg(void (*cwfunc_ptr)(basic_struct *, const basic_struct *),
210210
sympify(operand1, cbasic_operand1);
211211

212212
cresult = basic_new_heap();
213-
cwfunc_ptr(cresult, cbasic_operand1);
214-
result = Data_Wrap_Struct(Klass_of_Basic(cresult), NULL, cbasic_free_heap,
215-
cresult);
216-
basic_free_stack(cbasic_operand1);
213+
int error_code = cwfunc_ptr(cresult, cbasic_operand1);
214+
if (error_code == 0) {
215+
result = Data_Wrap_Struct(Klass_of_Basic(cresult), NULL,
216+
cbasic_free_heap, cresult);
217+
basic_free_stack(cbasic_operand1);
217218

218-
return result;
219+
return result;
220+
} else {
221+
basic_free_stack(cbasic_operand1);
222+
rb_raise(rb_eRuntimeError, "Runtime Error");
223+
}
219224
}
220225

221-
VALUE function_twoarg(void (*cwfunc_ptr)(basic_struct *, const basic_struct *,
222-
const basic_struct *),
226+
VALUE function_twoarg(int (*cwfunc_ptr)(basic_struct *, const basic_struct *,
227+
const basic_struct *),
223228
VALUE operand1, VALUE operand2)
224229
{
225230
basic_struct *cresult;
@@ -234,11 +239,17 @@ VALUE function_twoarg(void (*cwfunc_ptr)(basic_struct *, const basic_struct *,
234239
sympify(operand2, cbasic_operand2);
235240

236241
cresult = basic_new_heap();
237-
cwfunc_ptr(cresult, cbasic_operand1, cbasic_operand2);
238-
result = Data_Wrap_Struct(Klass_of_Basic(cresult), NULL, cbasic_free_heap,
239-
cresult);
240-
basic_free_stack(cbasic_operand1);
241-
basic_free_stack(cbasic_operand2);
242-
243-
return result;
242+
int error_code = cwfunc_ptr(cresult, cbasic_operand1, cbasic_operand2);
243+
244+
if (error_code == 0) {
245+
result = Data_Wrap_Struct(Klass_of_Basic(cresult), NULL,
246+
cbasic_free_heap, cresult);
247+
basic_free_stack(cbasic_operand1);
248+
basic_free_stack(cbasic_operand2);
249+
return result;
250+
} else {
251+
basic_free_stack(cbasic_operand1);
252+
basic_free_stack(cbasic_operand2);
253+
rb_raise(rb_eRuntimeError, "Runtime Error");
254+
}
244255
}

ext/symengine/symengine_utils.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ void get_symintfromval(VALUE operand2, basic_struct *cbasic_operand2);
1717
VALUE Klass_of_Basic(const basic_struct *basic_ptr);
1818
// Returns the result from the function pointed by cwfunc_ptr: for one argument
1919
// functions
20-
VALUE function_onearg(void (*cwfunc_ptr)(basic_struct *, const basic_struct *),
20+
VALUE function_onearg(int (*cwfunc_ptr)(basic_struct *, const basic_struct *),
2121
VALUE operand1);
2222
// Returns the result from the function pointed by cwfunc_ptr: for two argument
2323
// functions
24-
VALUE function_twoarg(void (*cwfunc_ptr)(basic_struct *, const basic_struct *,
25-
const basic_struct *),
24+
VALUE function_twoarg(int (*cwfunc_ptr)(basic_struct *, const basic_struct *,
25+
const basic_struct *),
2626
VALUE operand1, VALUE operand2);
2727

2828
#endif // SYMENGINE_UTILS_H_

0 commit comments

Comments
 (0)