@jdeighan/coffee-utils 11.0.3 → 11.0.5

Sign up to get free protection for your applications and to get access to all the features.
package/src/stack.js DELETED
@@ -1,191 +0,0 @@
1
- // Generated by CoffeeScript 2.7.0
2
- // stack.coffee
3
- var doDebugStack;
4
-
5
- import {
6
- assert,
7
- croak
8
- } from '@jdeighan/exceptions';
9
-
10
- import {
11
- undef,
12
- defined,
13
- OL,
14
- escapeStr,
15
- deepCopy,
16
- isArray,
17
- isBoolean
18
- } from '@jdeighan/coffee-utils';
19
-
20
- doDebugStack = false;
21
-
22
- // ---------------------------------------------------------------------------
23
- export var debugStack = function(flag = true) {
24
- doDebugStack = flag;
25
- };
26
-
27
- // ---------------------------------------------------------------------------
28
- export var CallStack = class CallStack {
29
- constructor() {
30
- this.lStack = [];
31
- }
32
-
33
- // ........................................................................
34
- reset() {
35
- if (doDebugStack) {
36
- console.log("RESET STACK");
37
- }
38
- this.lStack = [];
39
- }
40
-
41
- // ........................................................................
42
- indent() {
43
- return ' '.repeat(this.lStack.length);
44
- }
45
-
46
- // ........................................................................
47
- enter(funcName, lArgs = [], isLogged) {
48
- var _, hStackItem, ident1, ident2, lMatches;
49
- // --- funcName might be <object>.<method>
50
- assert(isArray(lArgs), "missing lArgs");
51
- assert(isBoolean(isLogged), "missing isLogged");
52
- if (doDebugStack) {
53
- console.log(this.indent() + `[--> ENTER ${funcName}]`);
54
- }
55
- lMatches = funcName.match(/^([A-Za-z_][A-Za-z0-9_]*)(?:\.([A-Za-z_][A-Za-z0-9_]*))?$/);
56
- assert(defined(lMatches), `Bad funcName: ${OL(funcName)}`);
57
- [_, ident1, ident2] = lMatches;
58
- if (ident2) {
59
- hStackItem = {
60
- fullName: funcName, // "#{ident1}.#{ident2}"
61
- funcName: ident2,
62
- isLogged,
63
- lArgs: deepCopy(lArgs)
64
- };
65
- } else {
66
- hStackItem = {
67
- fullName: funcName,
68
- funcName: ident1,
69
- isLogged,
70
- lArgs: deepCopy(lArgs)
71
- };
72
- }
73
- this.lStack.push(hStackItem);
74
- return hStackItem;
75
- }
76
-
77
- // ........................................................................
78
- getLevel() {
79
- var item, j, len, level, ref;
80
- level = 0;
81
- ref = this.lStack;
82
- for (j = 0, len = ref.length; j < len; j++) {
83
- item = ref[j];
84
- if (item.isLogged) {
85
- level += 1;
86
- }
87
- }
88
- return level;
89
- }
90
-
91
- // ........................................................................
92
- isLogging() {
93
- if (this.lStack.length === 0) {
94
- return false;
95
- } else {
96
- return this.lStack[this.lStack.length - 1].isLogged;
97
- }
98
- }
99
-
100
- // ........................................................................
101
- // --- if stack is empty, log the error, but continue
102
- returnFrom(fName) {
103
- var fullName, isLogged;
104
- if (this.lStack.length === 0) {
105
- console.log(`ERROR: returnFrom('${fName}') but stack is empty`);
106
- return;
107
- }
108
- ({fullName, isLogged} = this.lStack.pop());
109
- if (doDebugStack) {
110
- console.log(this.indent() + `[<-- BACK ${fName}]`);
111
- }
112
- if (fullName !== fName) {
113
- console.log(`ERROR: returnFrom('${fName}') but TOS is ${fullName}`);
114
- return;
115
- }
116
- }
117
-
118
- // ........................................................................
119
- curFunc() {
120
- if (this.lStack.length === 0) {
121
- return 'main';
122
- } else {
123
- return this.lStack[this.lStack.length - 1].funcName;
124
- }
125
- }
126
-
127
- // ........................................................................
128
- isActive(funcName) {
129
- var h, j, len, ref;
130
- ref = this.lStack;
131
- // --- funcName won't be <obj>.<method>
132
- // but the stack might contain that form
133
- for (j = 0, len = ref.length; j < len; j++) {
134
- h = ref[j];
135
- if (h.funcName === funcName) {
136
- return true;
137
- }
138
- }
139
- return false;
140
- }
141
-
142
- // ........................................................................
143
- dump(label = "CALL STACK") {
144
- var i, item, j, lLines, len, ref;
145
- lLines = [label];
146
- if (this.lStack.length === 0) {
147
- lLines.push(" <EMPTY>");
148
- } else {
149
- ref = this.lStack;
150
- for (i = j = 0, len = ref.length; j < len; i = ++j) {
151
- item = ref[i];
152
- lLines.push(" " + this.callStr(i, item));
153
- }
154
- }
155
- return lLines.join("\n");
156
- }
157
-
158
- // ........................................................................
159
- callStr(i, item) {
160
- var arg, j, len, ref, str, sym;
161
- sym = item.isLogged ? '*' : '-';
162
- str = `${i}: ${sym}${item.fullName}`;
163
- ref = item.lArgs;
164
- for (j = 0, len = ref.length; j < len; j++) {
165
- arg = ref[j];
166
- str += ` ${OL(arg)}`;
167
- }
168
- return str;
169
- }
170
-
171
- // ........................................................................
172
- sdump(label = 'CALL STACK') {
173
- var item, j, lFuncNames, len, ref;
174
- lFuncNames = [];
175
- ref = this.lStack;
176
- for (j = 0, len = ref.length; j < len; j++) {
177
- item = ref[j];
178
- if (item.isLogged) {
179
- lFuncNames.push('*' + item.fullName);
180
- } else {
181
- lFuncNames.push(item.fullName);
182
- }
183
- }
184
- if (this.lStack.length === 0) {
185
- return `${label} <EMPTY>`;
186
- } else {
187
- return `${label} ${lFuncNames.join(' ')}`;
188
- }
189
- }
190
-
191
- };