@jdeighan/coffee-utils 6.0.3 → 6.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@jdeighan/coffee-utils",
3
3
  "type": "module",
4
- "version": "6.0.3",
4
+ "version": "6.0.6",
5
5
  "description": "A set of utility functions for CoffeeScript",
6
6
  "main": "coffee_utils.js",
7
7
  "exports": {
@@ -1,7 +1,16 @@
1
1
  # call_stack.coffee
2
2
 
3
- import {undef, croak} from '@jdeighan/coffee-utils'
4
- import {log} from '@jdeighan/coffee-utils/log'
3
+ import {undef, croak, assert} from '@jdeighan/coffee-utils'
4
+ import {log, LOG} from '@jdeighan/coffee-utils/log'
5
+
6
+ doDebugStack = false
7
+
8
+ # ---------------------------------------------------------------------------
9
+
10
+ export debugStack = (flag=true) ->
11
+
12
+ doDebugStack = flag
13
+ return
5
14
 
6
15
  # ---------------------------------------------------------------------------
7
16
 
@@ -15,6 +24,9 @@ export class CallStack
15
24
 
16
25
  call: (funcName, hInfo) ->
17
26
 
27
+ if doDebugStack
28
+ prefix = ' '.repeat(@lStack.length)
29
+ LOG "#{prefix}[CALL #{funcName}]"
18
30
  @lStack.push({funcName, hInfo})
19
31
  return
20
32
 
@@ -23,17 +35,29 @@ export class CallStack
23
35
  returnFrom: (fName) ->
24
36
 
25
37
  if @lStack.length == 0
26
- croak "returnFrom('#{fName}') but stack is empty"
38
+ LOG "returnFrom('#{fName}') but stack is empty"
39
+ return undef
40
+ if doDebugStack
41
+ prefix = ' '.repeat(@lStack.length-1)
42
+ LOG "#{prefix}[RETURN FROM #{fName}]"
27
43
  {funcName, hInfo} = @lStack.pop()
28
- if funcName != fName
44
+ while (funcName != fName) && (@lStack.length > 0)
45
+ LOG "[MISSING RETURN FROM #{funcName} (return from #{fName})]"
46
+ {funcName, hInfo} = @lStack.pop()
47
+
48
+ if (funcName == fName)
49
+ return hInfo
50
+ else
29
51
  @dump()
30
- croak "returnFrom('#{fName}') but TOS is '#{funcName}'"
31
- return hInfo
52
+ LOG "BAD returnFrom('#{fName}')"
53
+ return undef
32
54
 
33
55
  # ........................................................................
34
56
 
35
57
  reset: () ->
36
58
 
59
+ if doDebugStack
60
+ LOG "RESET STACK"
37
61
  @lStack = []
38
62
 
39
63
  # ........................................................................
@@ -42,5 +66,5 @@ export class CallStack
42
66
 
43
67
  console.log "#{label}:"
44
68
  for item, i in @lStack
45
- console.log "#{i}: #{JSON.stringify(item)}"
69
+ LOG "#{i}: #{JSON.stringify(item)}"
46
70
  return
package/src/call_stack.js CHANGED
@@ -1,14 +1,25 @@
1
1
  // Generated by CoffeeScript 2.6.1
2
- // call_stack.coffee
2
+ // call_stack.coffee
3
+ var doDebugStack;
4
+
3
5
  import {
4
6
  undef,
5
- croak
7
+ croak,
8
+ assert
6
9
  } from '@jdeighan/coffee-utils';
7
10
 
8
11
  import {
9
- log
12
+ log,
13
+ LOG
10
14
  } from '@jdeighan/coffee-utils/log';
11
15
 
16
+ doDebugStack = false;
17
+
18
+ // ---------------------------------------------------------------------------
19
+ export var debugStack = function(flag = true) {
20
+ doDebugStack = flag;
21
+ };
22
+
12
23
  // ---------------------------------------------------------------------------
13
24
  export var CallStack = class CallStack {
14
25
  constructor() {
@@ -17,25 +28,44 @@ export var CallStack = class CallStack {
17
28
 
18
29
  // ........................................................................
19
30
  call(funcName, hInfo) {
31
+ var prefix;
32
+ if (doDebugStack) {
33
+ prefix = ' '.repeat(this.lStack.length);
34
+ LOG(`${prefix}[CALL ${funcName}]`);
35
+ }
20
36
  this.lStack.push({funcName, hInfo});
21
37
  }
22
38
 
23
39
  // ........................................................................
24
40
  returnFrom(fName) {
25
- var funcName, hInfo;
41
+ var funcName, hInfo, prefix;
26
42
  if (this.lStack.length === 0) {
27
- croak(`returnFrom('${fName}') but stack is empty`);
43
+ LOG(`returnFrom('${fName}') but stack is empty`);
44
+ return undef;
45
+ }
46
+ if (doDebugStack) {
47
+ prefix = ' '.repeat(this.lStack.length - 1);
48
+ LOG(`${prefix}[RETURN FROM ${fName}]`);
28
49
  }
29
50
  ({funcName, hInfo} = this.lStack.pop());
30
- if (funcName !== fName) {
51
+ while ((funcName !== fName) && (this.lStack.length > 0)) {
52
+ LOG(`[MISSING RETURN FROM ${funcName} (return from ${fName})]`);
53
+ ({funcName, hInfo} = this.lStack.pop());
54
+ }
55
+ if (funcName === fName) {
56
+ return hInfo;
57
+ } else {
31
58
  this.dump();
32
- croak(`returnFrom('${fName}') but TOS is '${funcName}'`);
59
+ LOG(`BAD returnFrom('${fName}')`);
60
+ return undef;
33
61
  }
34
- return hInfo;
35
62
  }
36
63
 
37
64
  // ........................................................................
38
65
  reset() {
66
+ if (doDebugStack) {
67
+ LOG("RESET STACK");
68
+ }
39
69
  return this.lStack = [];
40
70
  }
41
71
 
@@ -46,7 +76,7 @@ export var CallStack = class CallStack {
46
76
  ref = this.lStack;
47
77
  for (i = j = 0, len = ref.length; j < len; i = ++j) {
48
78
  item = ref[i];
49
- console.log(`${i}: ${JSON.stringify(item)}`);
79
+ LOG(`${i}: ${JSON.stringify(item)}`);
50
80
  }
51
81
  }
52
82
 
@@ -150,7 +150,7 @@ export debug = (lArgs...) ->
150
150
  returning = true
151
151
  curFunc = lMatches[1]
152
152
  hInfo = stack.returnFrom(curFunc)
153
- if DEBUGDEBUG
153
+ if DEBUGDEBUG && hInfo
154
154
  LOG "RETURN FROM #{curFunc}, debugging = #{hInfo.debugging}"
155
155
 
156
156
  if shouldLogString(label)
@@ -179,7 +179,8 @@ export debug = (lArgs...) ->
179
179
  if returning
180
180
  if debugLevel > 0
181
181
  debugLevel -= 1
182
- setEnv(hInfo)
182
+ if hInfo
183
+ setEnv(hInfo)
183
184
  else if entering
184
185
  if debugging
185
186
  debugLevel += 1
@@ -176,7 +176,7 @@ export var debug = function(...lArgs) {
176
176
  returning = true;
177
177
  curFunc = lMatches[1];
178
178
  hInfo = stack.returnFrom(curFunc);
179
- if (DEBUGDEBUG) {
179
+ if (DEBUGDEBUG && hInfo) {
180
180
  LOG(`RETURN FROM ${curFunc}, debugging = ${hInfo.debugging}`);
181
181
  }
182
182
  }
@@ -205,7 +205,9 @@ export var debug = function(...lArgs) {
205
205
  if (debugLevel > 0) {
206
206
  debugLevel -= 1;
207
207
  }
208
- setEnv(hInfo);
208
+ if (hInfo) {
209
+ setEnv(hInfo);
210
+ }
209
211
  } else if (entering) {
210
212
  if (debugging) {
211
213
  debugLevel += 1;