Qore Programming Language  0.9.4.6
ThreadClosureVariableStack.h
1 /* -*- mode: c++; indent-tabs-mode: nil -*- */
2 /*
3  ThreadClosureVariableStack.h
4 
5  Qore Programming Language
6 
7  Copyright (C) 2003 - 2020 Qore Technologies, s.r.o.
8 
9  Permission is hereby granted, free of charge, to any person obtaining a
10  copy of this software and associated documentation files (the "Software"),
11  to deal in the Software without restriction, including without limitation
12  the rights to use, copy, modify, merge, publish, distribute, sublicense,
13  and/or sell copies of the Software, and to permit persons to whom the
14  Software is furnished to do so, subject to the following conditions:
15 
16  The above copyright notice and this permission notice shall be included in
17  all copies or substantial portions of the Software.
18 
19  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25  DEALINGS IN THE SOFTWARE.
26 
27  Note that the Qore library is released under a choice of three open-source
28  licenses: MIT (as above), LGPL 2+, or GPL 2+; see README-LICENSE for more
29  information.
30 */
31 
32 #ifndef _QORE_INTERN_THREADCLOSUREVARIABLESTACK_H
33 #define _QORE_INTERN_THREADCLOSUREVARIABLESTACK_H
34 
35 class ThreadClosureVariableStack : public ThreadLocalData<ClosureVarValue*> {
36 private:
37  DLLLOCAL void instantiateIntern(ClosureVarValue* cvar) {
38  //printd(5, "ThreadClosureVariableStack::instantiateIntern(%p = '%s') this: %p pgm: %p\n", cvar->id, cvar->id, this, getProgram());
39 
40  if (curr->pos == QORE_THREAD_STACK_BLOCK) {
41  if (curr->next)
42  curr = curr->next;
43  else {
44  curr->next = new Block(curr);
45  //printf("this: %p: add curr: %p, curr->next: %p\n", this, curr, curr->next);
46  curr = curr->next;
47  }
48  }
49  curr->var[curr->pos++] = cvar;
50  }
51 
52 public:
53  // clears and marks all variables as finalized on the stack
54  DLLLOCAL void finalize(arg_vec_t*& cl) {
55  ThreadClosureVariableStack::iterator i(curr);
56  while (i.next()) {
57  ValueHolder n(i.get()->finalize(), nullptr);
58  if (n->derefCanThrowException()) {
59  if (!cl) {
60  cl = new arg_vec_t;
61  }
62  cl->push_back(n.release());
63  }
64  }
65  }
66 
67  // deletes everything on the stack
68  DLLLOCAL void del(ExceptionSink* xsink) {
69  while (curr->prev || curr->pos)
70  uninstantiate(xsink);
71  }
72 
73  DLLLOCAL ClosureVarValue* instantiate(const char* id, const QoreTypeInfo* typeInfo, QoreValue& nval, bool assign) {
74  ClosureVarValue* cvar = new ClosureVarValue(id, typeInfo, nval, assign);
75  instantiateIntern(cvar);
76  return cvar;
77  }
78 
79  DLLLOCAL void instantiate(ClosureVarValue* cvar) {
80  instantiateIntern(cvar);
81  }
82 
83  DLLLOCAL void uninstantiateIntern() {
84 #if 0
85  if (!curr->pos)
86  printd(5, "ThreadClosureVariableStack::uninstantiate() this: %p pos: %d %p %s\n", this, curr->prev->pos - 1, curr->prev->var[curr->prev->pos - 1]->id, curr->prev->var[curr->prev->pos - 1]->id);
87  else
88  printd(5, "ThreadClosureVariableStack::uninstantiate() this: %p pos: %d %p %s\n", this, curr->pos - 1, curr->var[curr->pos - 1]->id, curr->var[curr->pos - 1]->id);
89 #endif
90  if (!curr->pos) {
91  if (curr->next) {
92  //printf("this %p: del curr: %p, curr->next: %p\n", this, curr, curr->next);
93  delete curr->next;
94  curr->next = 0;
95  }
96  curr = curr->prev;
97  assert(curr);
98  }
99  --curr->pos;
100  }
101 
102  DLLLOCAL void uninstantiate(ExceptionSink* xsink) {
103  uninstantiateIntern();
104  assert(curr->var[curr->pos]);
105  curr->var[curr->pos]->deref(xsink);
106  }
107 
108  DLLLOCAL ClosureVarValue* find(const char* id) {
109  printd(5, "ThreadClosureVariableStack::find() this: %p id: %p\n", this, id);
110  Block* w = curr;
111  while (true) {
112  int p = w->pos;
113  while (p) {
114  --p;
115  ClosureVarValue* rv = w->var[p];
116  //printd(5, "ThreadClosureVariableStack::find(%p '%s') this: %p checking %p '%s'\n", id, id, this, rv ? rv->id : nullptr, rv ? rv->id : "n/a");
117  if (rv && rv->id == id) {
118  //printd(5, "ThreadClosureVariableStack::find(%p '%s') this: %p returning: %p\n", id, id, this, rv);
119  return rv;
120  }
121  }
122  w = w->prev;
123 #ifdef DEBUG
124  if (!w) {
125  printd(0, "ThreadClosureVariableStack::find() this: %p no closure-bound local variable '%s' (%p) on stack (pgm: %p) p: %d curr->prev: %p\n", this, id, id, getProgram(), p, curr->prev);
126  w = curr;
127  while (w) {
128  p = w->pos;
129  while (p) {
130  --p;
131  ClosureVarValue* cvv = w->var[p];
132  printd(0, "var p: %d: %s (%p)\n", p, cvv ? cvv->id : "frame boundary", cvv ? cvv->id : nullptr);
133  }
134  w = w->prev;
135  }
136  }
137 #endif
138  assert(w);
139  }
140  // to avoid a warning on most compilers - note that this generates a warning on aCC!
141  return nullptr;
142  }
143 
144  DLLLOCAL cvv_vec_t* getAll() const {
145  cvv_vec_t* cv = 0;
146  Block* w = curr;
147  while (w) {
148  int p = w->pos;
149  while (p) {
150  --p;
151  ClosureVarValue* cvv = w->var[p];
152  // skip frame boundaries
153  if (!cvv)
154  continue;
155  if (!cv)
156  cv = new cvv_vec_t;
157  cv->push_back(cvv->refSelf());
158  }
159  w = w->prev;
160  }
161  //printd(5, "ThreadClosureVariableStack::getAll() this: %p cv: %p size: %d\n", this, cv, cv ? cv->size() : 0);
162  return cv;
163  }
164 
165  DLLLOCAL void pushFrameBoundary() {
166  ++frame_count;
167  //printd(5, "ThreadClosureVariableStack::pushFrameBoundary(): fc:%d\n", frame_count);
168  instantiateIntern(nullptr);
169  }
170 
171  DLLLOCAL void popFrameBoundary() {
172  assert(frame_count >= 0);
173  --frame_count;
174  //printd(5, "ThreadClosureVariableStack::popFrameBoundary(): fc:%d\n", frame_count);
175  uninstantiateIntern();
176  assert(!curr->var[curr->pos]);
177  }
178 
179  DLLLOCAL int getFrame(int frame, Block*& w, int& p);
180 
181  DLLLOCAL void getLocalVars(QoreHashNode& h, int frame, ExceptionSink* xsink);
182 
183  // returns 0 = OK, 1 = no such variable, -1 exception setting variable
184  DLLLOCAL int setVarValue(int frame, const char* name, const QoreValue& val, ExceptionSink* xsink);
185 };
186 
187 #endif
This is the hash or associative list container type in Qore, dynamically allocated only...
Definition: QoreHashNode.h:50
DLLEXPORT QoreProgram * getProgram()
returns the current QoreProgram
The main value class in Qore, designed to be passed by value.
Definition: QoreValue.h:262
container for holding Qore-language exception information and also for registering a "thread_exit" ca...
Definition: ExceptionSink.h:46
std::vector< QoreValue > arg_vec_t
vector of value information for default argument lists
Definition: common.h:254
holds an object and dereferences it in the destructor
Definition: QoreValue.h:452