NEURON
ocjump.cpp
Go to the documentation of this file.
1 #include <../../nrnconf.h>
2 
3 #include "nrnfilewrap.h"
4 #include "nrnoc2iv.h"
5 #include "ocfunc.h"
6 #include "ocjump.h"
7 #if HAVE_IV
8 #include "ivoc.h"
9 #endif
10 
11 #include <InterViews/resource.h>
12 
13 #include <utility>
14 
17 extern Symlist* hoc_symlist;
18 extern Object* hoc_thisobject;
19 extern int hoc_execerror_messages;
20 
21 bool hoc_valid_stmt(const char* stmt, Object* ob) {
22  std::string s{stmt};
23  s.append(1, '\n');
24  return OcJump::execute(s.c_str(), ob);
25 }
26 
27 void hoc_execute1() {
28  Object* ob{};
29  int hem{1};
30  if (ifarg(2)) {
31  if (hoc_is_object_arg(2)) {
32  ob = *hoc_objgetarg(2);
33  if (ifarg(3)) {
34  hem = chkarg(3, 0., 1.);
35  }
36  } else {
37  hem = chkarg(2, 0., 1.);
38  }
39  }
40 
41  auto const hemold = std::exchange(hoc_execerror_messages, hem);
42  auto const old_mpiabort_flag = std::exchange(nrn_mpiabort_on_error_, 0);
43  bool const b = hoc_valid_stmt(hoc_gargstr(1), ob);
44  nrn_mpiabort_on_error_ = old_mpiabort_flag;
45  hoc_execerror_messages = hemold;
46  hoc_retpushx(b);
47 }
48 
49 #if HAVE_IV
50 bool Oc::valid_expr(Symbol* s) {
51  return OcJump::execute(s->u.u_proc->defn.in);
52 }
53 
54 bool Oc::valid_stmt(const char* stmt, Object* ob) {
55  return hoc_valid_stmt(stmt, ob);
56 }
57 #endif
58 //------------------------------------------------------------------
59 void hoc_execute(Inst*);
60 
61 namespace {
62 struct saved_state {
63  saved_state() {
64  // not complete but it is good for expressions and it can be improved
65  oc_save_hoc_oop(&o1, &o2, &o4, &o5);
66  oc_save_code(&c1, &c2, c3, &c4, &c5, &c6, &c7, &c8, c9, &c10, &c11, &c12);
67  oc_save_input_info(&i1, &i2, &i3, &i4);
68  oc_save_cabcode(&cc1, &cc2);
69  }
70  void restore() {
71  oc_restore_hoc_oop(&o1, &o2, &o4, &o5);
72  oc_restore_code(&c1, &c2, c3, &c4, &c5, &c6, &c7, &c8, c9, &c10, &c11, &c12);
73  oc_restore_input_info(i1, i2, i3, i4);
74  oc_restore_cabcode(&cc1, &cc2);
75  }
76 
77  private:
78  // hoc_oop
79  Object* o1{};
80  Objectdata* o2{};
81  int o4{};
82  Symlist* o5{};
83 
84  // code
85  Inst* c1{};
86  Inst* c2{};
87  std::size_t c3{};
88  nrn::oc::frame* c4{};
89  int c5{};
90  int c6{};
91  Inst* c7{};
92  nrn::oc::frame* c8{};
93  std::size_t c9{};
94  Symlist* c10{};
95  Inst* c11{};
96  int c12{};
97 
98  // input_info
99  const char* i1{};
100  int i2{};
101  int i3{};
102  NrnFILEWrap* i4{};
103 
104  // cabcode
105  int cc1{};
106  int cc2{};
107 };
108 } // namespace
109 
111  saved_state before{};
112  try_catch_depth_increment tell_children_we_will_catch{};
113  try {
114  hoc_execute(p);
115  return true;
116  } catch (...) {
117  before.restore();
118  return false;
119  }
120 }
121 
122 bool OcJump::execute(const char* stmt, Object* ob) {
123  saved_state before{};
124  try_catch_depth_increment tell_children_we_will_catch{};
125  try {
126  hoc_obj_run(stmt, ob);
127  return true;
128  } catch (...) {
129  before.restore();
130  return false;
131  }
132 }
133 
135  saved_state before{};
136  try_catch_depth_increment tell_children_we_will_catch{};
137  try {
138  hoc_call_ob_proc(obj, sym, narg);
139  } catch (...) {
140  before.restore();
141  throw;
142  }
143 }
144 
146  // NOTE: return value is left on the stack
147  saved_state before{};
148  try_catch_depth_increment tell_children_we_will_catch{};
149  try {
151  } catch (...) {
152  before.restore();
153  throw;
154  }
155 }
156 
157 void* OcJump::fpycall(void* (*f)(void*, void*), void* a, void* b) {
158  saved_state before{};
159  try_catch_depth_increment tell_children_we_will_catch{};
160  try {
161  return (*f)(a, b);
162  } catch (...) {
163  before.restore();
164  throw;
165  }
166 }
167 
169  oc_save_hoc_oop(&a1, &a2, &a4, &a5);
170  hoc_thisobject = obj;
171  if (obj) {
172  hoc_objectdata = obj->u.dataspace;
174  } else {
177  }
178 }
179 
181  oc_restore_hoc_oop(&a1, &a2, &a4, &a5);
182 }
static double restore(void *v)
void oc_save_cabcode(int *a1, int *a2)
Definition: cabcode.cpp:117
void oc_restore_cabcode(int *a1, int *a2)
Definition: cabcode.cpp:122
static bool valid_expr(Symbol *)
static bool valid_stmt(const char *, Object *ob=NULL)
void oc_save_code(Inst **a1, Inst **a2, std::size_t &a3, Frame **a4, int *a5, int *a6, Inst **a7, Frame **a8, std::size_t &a9, Symlist **a10, Inst **a11, int *a12)
Definition: code.cpp:509
void oc_restore_code(Inst **a1, Inst **a2, std::size_t &a3, Frame **a4, int *a5, int *a6, Inst **a7, Frame **a8, std::size_t &a9, Symlist **a10, Inst **a11, int *a12)
Definition: code.cpp:535
double chkarg(int, double low, double high)
Definition: code2.cpp:626
int hoc_is_object_arg(int narg)
Definition: code.cpp:876
void hoc_call_ob_proc(Object *ob, Symbol *sym, int narg)
Definition: hoc_oop.cpp:648
char * hoc_gargstr(int)
Objectdata * hoc_objectdata
Definition: hoc_oop.cpp:122
void hoc_retpushx(double x)
Definition: hocusr.cpp:154
void hoc_call_func_result_on_stack(Symbol *s, int narg)
Definition: code.cpp:1482
int nrn_mpiabort_on_error_
Definition: hoc.cpp:80
void oc_restore_input_info(const char *i1, int i2, int i3, NrnFILEWrap *i4)
Definition: hoc.cpp:1307
void oc_save_input_info(const char **i1, int *i2, int *i3, NrnFILEWrap **i4)
Definition: hoc.cpp:1301
void oc_save_hoc_oop(Object **a1, Objectdata **a2, int *a4, Symlist **a5)
Definition: hoc_oop.cpp:423
void oc_restore_hoc_oop(Object **a1, Objectdata **a2, int *a4, Symlist **a5)
Definition: hoc_oop.cpp:439
Object ** hoc_objgetarg(int)
Definition: code.cpp:1614
static int narg()
Definition: ivocvect.cpp:121
int hoc_obj_run(const char *, Object *)
Definition: hoc_oop.cpp:315
FILE NrnFILEWrap
Definition: nrnfilewrap.h:8
size_t p
s
Definition: multisend.cpp:521
int ifarg(int)
Definition: code.cpp:1607
int hoc_execerror_messages
Definition: hoc.cpp:607
Symlist * hoc_top_level_symlist
Definition: symdir.cpp:16
bool hoc_valid_stmt(const char *stmt, Object *ob)
Definition: ocjump.cpp:21
Symlist * hoc_symlist
Definition: symbol.cpp:34
Objectdata * hoc_top_level_data
Definition: hoc_oop.cpp:123
void hoc_execute(Inst *)
Definition: code.cpp:2531
void hoc_execute1()
Definition: ocjump.cpp:27
Object * hoc_thisobject
Definition: hoc_oop.cpp:121
Symlist * a5
Definition: ocjump.h:37
Objectdata * a2
Definition: ocjump.h:35
Object * a1
Definition: ocjump.h:34
ObjectContext(Object *)
Definition: ocjump.cpp:168
Definition: hocdec.h:173
Objectdata * dataspace
Definition: hocdec.h:177
cTemplate * ctemplate
Definition: hocdec.h:180
union Object::@47 u
static bool execute(Inst *p)
Definition: ocjump.cpp:110
static void execute_throw_on_exception(Symbol *sym, int narg)
Definition: ocjump.cpp:145
static void * fpycall(void *(*)(void *, void *), void *, void *)
Definition: ocjump.cpp:157
Definition: model.h:47
Definition: hocdec.h:75
Symlist * symtable
Definition: hocdec.h:148
Helper type for incrementing/decrementing nrn_try_catch_nest_depth.
Definition: ocjump.h:20
Definition: hocdec.h:42