GCC Code Coverage Report


Directory: ./
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 96.8% 30 / 0 / 31
Functions: 100.0% 2 / 0 / 2
Branches: 81.0% 34 / 0 / 42

ruby/interpreter/RubyEval.hpp
Line Branch Exec Source
1 /***********************************************************************************************************************
2 * OpenStudio(R), Copyright (c) Alliance for Energy Innovation, LLC.
3 * See also https://openstudio.net/license
4 ***********************************************************************************************************************/
5
6 #ifndef RUBYEVAL_included
7 #define RUBYEVAL_included
8
9 #include <ruby.h>
10 #include "RubyException.hpp"
11
12 namespace openstudio {
13
14 2111 inline VALUE evaluateSimpleImpl(VALUE arg) {
15 2111 return rb_eval_string(StringValuePtr(arg));
16 }
17
18 2111 inline VALUE evalString(const std::string& t_str) {
19
1/1
✓ Branch 2 taken 2111 times.
2111 VALUE val = rb_str_new2(t_str.c_str());
20 int error;
21 // save and restore the current working directory in case the call to ruby upsets it
22
1/1
✓ Branch 1 taken 2111 times.
2111 VALUE result = rb_protect(evaluateSimpleImpl, val, &error);
23
2/2
✓ Branch 0 taken 1 time.
✓ Branch 1 taken 2110 times.
2111 if (error != 0) {
24
25 // VALUE errval = rb_eval_string("$!.to_s");
26 // char* str = StringValuePtr(errval);
27 // std::string err(str);
28
29
1/1
✓ Branch 1 taken 1 time.
1 VALUE errinfo = rb_errinfo();
30
1/1
✓ Branch 1 taken 1 time.
1 VALUE exception_class = rb_obj_class(errinfo);
31
1/1
✓ Branch 1 taken 1 time.
1 VALUE classNameValue = rb_class_name(exception_class);
32
2/2
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
1 std::string className(StringValuePtr(classNameValue));
33
34
1/1
✓ Branch 1 taken 1 time.
1 VALUE errstr = rb_obj_as_string(errinfo);
35
2/2
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
1 std::string errMessage(StringValuePtr(errstr));
36
37
2/2
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
1 std::string totalErr = className + ": " + errMessage;
38
39 // I just **cannot** figure out a way to get the error location in C, without calling $@.to_s. Seems nothing is available here
40 int locationError;
41
1/1
✓ Branch 1 taken 1 time.
1 VALUE locval = rb_eval_string_protect("$@.to_s", &locationError);
42 1 std::string loc;
43
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 if (locationError == 0) {
44
2/2
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
1 loc = StringValuePtr(locval);
45 } else {
46 ✗ loc = "Failed to get VM location";
47 }
48
49 // Generally speaking, the backtrace is there, but not for the case where it's a stack too deep error
50
1/2
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
1 const ID ID_backtrace = rb_intern_const("backtrace");
51
4/7
✓ Branch 0 taken 1 time.
✗ Branch 1 not taken.
✓ Branch 3 taken 1 time.
✓ Branch 5 taken 1 time.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 time.
✗ Branch 8 not taken.
1 if (exception_class != rb_eSysStackError && rb_respond_to(errinfo, ID_backtrace)) {
52 // std::vector<std::string> backtrace_lines;
53 1 std::string btlines;
54 /*volatile*/ VALUE backtrace;
55
2/3
✓ Branch 1 taken 1 time.
✓ Branch 3 taken 1 time.
✗ Branch 4 not taken.
1 if (!NIL_P(backtrace = rb_funcall(errinfo, ID_backtrace, 0))) {
56
2/2
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
1 VALUE backtracejoin = rb_ary_join(backtrace, rb_str_new2("\n"));
57
2/2
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
1 btlines = StringValuePtr(backtracejoin);
58
59 // Get the backing C array of the ruby array
60 // VALUE* elements = RARRAY_PTR(backtrace);
61 // for (long c = 0; c < RARRAY_LEN(backtrace); ++c) {
62 // VALUE entry = elements[c];
63 // [[maybe_unused]] char* backtrace_line = RSTRING_PTR(entry);
64 // char* backtrace_line2 = StringValuePtr(entry);
65 // backtrace_lines.emplace_back(backtrace_line2);
66 // }
67 }
68
69
1/2
✓ Branch 1 taken 1 time.
✗ Branch 2 not taken.
1 if (!btlines.empty()) {
70
2/2
✓ Branch 1 taken 1 time.
✓ Branch 4 taken 1 time.
1 loc += "\n\nTraceback:\n" + btlines;
71 }
72 1 } else {
73 }
74
75 // Clear the last error, we raise it on our side only, so Ruby needs to be cleared.
76 // Otherwise later API calls might read the old value and think another exception has occurred
77
1/1
✓ Branch 1 taken 1 time.
1 rb_set_errinfo(Qnil);
78
79
1/1
✓ Branch 2 taken 1 time.
1 throw RubyException(totalErr, loc);
80 4 }
81
82 2110 return result;
83 }
84
85 } // namespace openstudio
86
87 #endif // RUBYEVAL_included
88