@typescript-guy/fn-monitor 1.0.0

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.
Files changed (62) hide show
  1. package/ACKNOWLEDGEMENTS.md +10 -0
  2. package/LICENSE.md +10 -0
  3. package/README.md +201 -0
  4. package/dist/custom-types.d.ts +239 -0
  5. package/dist/custom-types.js +341 -0
  6. package/dist/evaluate/declaration.d.ts +27 -0
  7. package/dist/evaluate/declaration.js +289 -0
  8. package/dist/evaluate/expression.d.ts +40 -0
  9. package/dist/evaluate/expression.js +604 -0
  10. package/dist/evaluate/helper.d.ts +18 -0
  11. package/dist/evaluate/helper.js +270 -0
  12. package/dist/evaluate/identifier.d.ts +7 -0
  13. package/dist/evaluate/identifier.js +27 -0
  14. package/dist/evaluate/index.d.ts +3 -0
  15. package/dist/evaluate/index.js +112 -0
  16. package/dist/evaluate/literal.d.ts +3 -0
  17. package/dist/evaluate/literal.js +8 -0
  18. package/dist/evaluate/pattern.d.ts +13 -0
  19. package/dist/evaluate/pattern.js +118 -0
  20. package/dist/evaluate/program.d.ts +3 -0
  21. package/dist/evaluate/program.js +20 -0
  22. package/dist/evaluate/statement.d.ts +35 -0
  23. package/dist/evaluate/statement.js +323 -0
  24. package/dist/evaluate_n/declaration.d.ts +27 -0
  25. package/dist/evaluate_n/declaration.js +289 -0
  26. package/dist/evaluate_n/expression.d.ts +38 -0
  27. package/dist/evaluate_n/expression.js +596 -0
  28. package/dist/evaluate_n/helper.d.ts +18 -0
  29. package/dist/evaluate_n/helper.js +238 -0
  30. package/dist/evaluate_n/identifier.d.ts +7 -0
  31. package/dist/evaluate_n/identifier.js +27 -0
  32. package/dist/evaluate_n/index.d.ts +3 -0
  33. package/dist/evaluate_n/index.js +76 -0
  34. package/dist/evaluate_n/literal.d.ts +3 -0
  35. package/dist/evaluate_n/literal.js +8 -0
  36. package/dist/evaluate_n/pattern.d.ts +13 -0
  37. package/dist/evaluate_n/pattern.js +118 -0
  38. package/dist/evaluate_n/program.d.ts +3 -0
  39. package/dist/evaluate_n/program.js +20 -0
  40. package/dist/evaluate_n/statement.d.ts +35 -0
  41. package/dist/evaluate_n/statement.js +301 -0
  42. package/dist/examples/example-2.d.ts +1 -0
  43. package/dist/examples/example.d.ts +1 -0
  44. package/dist/helper-functions.d.ts +15 -0
  45. package/dist/helper-functions.js +104 -0
  46. package/dist/index.d.ts +55 -0
  47. package/dist/index.js +312 -0
  48. package/dist/q-list.d.ts +39 -0
  49. package/dist/q-list.js +146 -0
  50. package/dist/scope/index.d.ts +81 -0
  51. package/dist/scope/index.js +168 -0
  52. package/dist/scope/variable.d.ts +20 -0
  53. package/dist/scope/variable.js +38 -0
  54. package/dist/share/async.d.ts +7 -0
  55. package/dist/share/async.js +43 -0
  56. package/dist/share/const.d.ts +25 -0
  57. package/dist/share/const.js +21 -0
  58. package/dist/share/util.d.ts +33 -0
  59. package/dist/share/util.js +379 -0
  60. package/dist/sval.d.ts +15 -0
  61. package/dist/sval.js +104 -0
  62. package/package.json +54 -0
@@ -0,0 +1,168 @@
1
+ import { NOINIT, DEADZONE } from '../share/const.js';
2
+ import { Prop, Var } from './variable.js';
3
+ import { create, define } from '../share/util.js';
4
+
5
+ class Scope {
6
+ /**
7
+ * The parent scope along the scope chain
8
+ * @private
9
+ * @readonly
10
+ */
11
+ parent;
12
+ /**
13
+ * To distinguish function scope and block scope
14
+ * The value is true for function scope or false for block scope
15
+ * @private
16
+ * @readonly
17
+ */
18
+ isolated;
19
+ /**
20
+ * Context simulation object
21
+ * @private
22
+ * @readonly
23
+ */
24
+ context = create(null);
25
+ /**
26
+ * To memoize the object for with-statement context
27
+ * @private
28
+ */
29
+ withContext = create(null);
30
+ /**
31
+ * Create a simulated scope
32
+ * @param parent the parent scope along the scope chain (default: null)
33
+ * @param isolated true for function scope or false for block scope (default: false)
34
+ */
35
+ interpreter = void 0;
36
+ constructor(parent, isolated = false, interpreter) {
37
+ this.parent = parent;
38
+ this.isolated = isolated;
39
+ this.interpreter = interpreter || (parent ? parent.interpreter : void 0);
40
+ }
41
+ hasParent() {
42
+ return this.parent !== null;
43
+ }
44
+ get scopeContext() {
45
+ return this.context;
46
+ }
47
+ get scopeParent() {
48
+ return this.parent;
49
+ }
50
+ get scopeDepth() {
51
+ let d = 0;
52
+ let currentScope = this;
53
+ while (currentScope && currentScope.hasParent()) {
54
+ d++;
55
+ currentScope = currentScope.scopeParent;
56
+ }
57
+ return d;
58
+ }
59
+ /**
60
+ * Get global scope
61
+ */
62
+ global() {
63
+ let scope = this;
64
+ while (scope.parent) {
65
+ scope = scope.parent;
66
+ }
67
+ return scope;
68
+ }
69
+ /**
70
+ * Find a variable along scope chain
71
+ * @param name variable identifier name
72
+ */
73
+ find(name) {
74
+ if (this.context[name]) {
75
+ return this.context[name];
76
+ } else if (name in this.withContext) {
77
+ return new Prop(this.withContext, name);
78
+ } else if (this.parent) {
79
+ return this.parent.find(name);
80
+ } else {
81
+ const win = this.global().find("window").get();
82
+ if (name in win) {
83
+ return new Prop(win, name);
84
+ } else {
85
+ return null;
86
+ }
87
+ }
88
+ }
89
+ /**
90
+ * Declare a var variable
91
+ * @param name variable identifier name
92
+ * @param value variable value
93
+ */
94
+ var(name, value) {
95
+ let scope = this;
96
+ while (scope.parent && !scope.isolated) {
97
+ scope = scope.parent;
98
+ }
99
+ const variable = scope.context[name];
100
+ if (!variable) {
101
+ scope.context[name] = new Var("var", value === NOINIT ? void 0 : value);
102
+ } else {
103
+ if (variable.kind === "var") {
104
+ if (value !== NOINIT) {
105
+ variable.set(value);
106
+ }
107
+ } else {
108
+ throw new SyntaxError(`Identifier '${name}' has already been declared`);
109
+ }
110
+ }
111
+ if (!scope.parent) {
112
+ const win = scope.find("window").get();
113
+ if (value !== NOINIT) {
114
+ define(win, name, { value, writable: true, enumerable: true });
115
+ }
116
+ }
117
+ }
118
+ /**
119
+ * Declare a let variable
120
+ * @param name variable identifier name
121
+ * @param value variable value
122
+ */
123
+ let(name, value) {
124
+ const variable = this.context[name];
125
+ if (!variable || variable.get() === DEADZONE) {
126
+ this.context[name] = new Var("let", value);
127
+ } else {
128
+ throw new SyntaxError(`Identifier '${name}' has already been declared`);
129
+ }
130
+ }
131
+ /**
132
+ * Declare a const variable
133
+ * @param name variable identifier name
134
+ * @param value variable value
135
+ */
136
+ const(name, value) {
137
+ const variable = this.context[name];
138
+ if (!variable || variable.get() === DEADZONE) {
139
+ this.context[name] = new Var("const", value);
140
+ } else {
141
+ throw new SyntaxError(`Identifier '${name}' has already been declared`);
142
+ }
143
+ }
144
+ /**
145
+ * Declare a function
146
+ * @param name function name
147
+ * @param value function
148
+ */
149
+ func(name, value) {
150
+ const variable = this.context[name];
151
+ if (!variable || variable.kind === "var") {
152
+ this.context[name] = new Var("var", value);
153
+ } else {
154
+ throw new SyntaxError(`Identifier '${name}' has already been declared`);
155
+ }
156
+ }
157
+ /**
158
+ * Memoize the object for with-statement context
159
+ * @param value object
160
+ */
161
+ with(value) {
162
+ if (Object.keys(value)) {
163
+ this.withContext = value;
164
+ }
165
+ }
166
+ }
167
+
168
+ export { Scope as default };
@@ -0,0 +1,20 @@
1
+ export type VarKind = 'var' | 'let' | 'const';
2
+ export interface Variable {
3
+ get(): any;
4
+ set(value: any): boolean;
5
+ }
6
+ export declare class Var implements Variable {
7
+ readonly kind: VarKind;
8
+ private value;
9
+ constructor(kind: VarKind, value: any);
10
+ get(): any;
11
+ set(value: any): any;
12
+ }
13
+ export declare class Prop implements Variable {
14
+ private readonly object;
15
+ private readonly property;
16
+ constructor(object: any, property: string);
17
+ get(): any;
18
+ set(value: any): boolean;
19
+ del(): boolean;
20
+ }
@@ -0,0 +1,38 @@
1
+ class Var {
2
+ kind;
3
+ value;
4
+ constructor(kind, value) {
5
+ this.kind = kind;
6
+ this.value = value;
7
+ }
8
+ get() {
9
+ return this.value;
10
+ }
11
+ set(value) {
12
+ if (this.kind === "const") {
13
+ throw new TypeError("Assignment to constant variable");
14
+ } else {
15
+ return this.value = value;
16
+ }
17
+ }
18
+ }
19
+ class Prop {
20
+ object;
21
+ property;
22
+ constructor(object, property) {
23
+ this.object = object;
24
+ this.property = property;
25
+ }
26
+ get() {
27
+ return this.object[this.property];
28
+ }
29
+ set(value) {
30
+ this.object[this.property] = value;
31
+ return true;
32
+ }
33
+ del() {
34
+ return delete this.object[this.property];
35
+ }
36
+ }
37
+
38
+ export { Prop, Var };
@@ -0,0 +1,7 @@
1
+ export interface runAsyncOptions {
2
+ res?: any;
3
+ err?: any;
4
+ ret?: any;
5
+ fullRet?: boolean;
6
+ }
7
+ export declare function runAsync(iterator: IterableIterator<any>, options?: runAsyncOptions): Promise<any>;
@@ -0,0 +1,43 @@
1
+ import { AWAIT } from './const.js';
2
+
3
+ function runAsync(iterator, options = {}) {
4
+ const { res, err, ret, fullRet } = options;
5
+ return new Promise((resolve, reject) => {
6
+ if ("ret" in options) {
7
+ return resolve(iterator.return(ret));
8
+ }
9
+ if ("err" in options) {
10
+ onRejected(err);
11
+ } else {
12
+ onFulfilled(res);
13
+ }
14
+ function onFulfilled(res2) {
15
+ let ret2;
16
+ try {
17
+ ret2 = iterator.next(res2);
18
+ } catch (e) {
19
+ return reject(e);
20
+ }
21
+ next(ret2);
22
+ return null;
23
+ }
24
+ function onRejected(err2) {
25
+ let ret2;
26
+ try {
27
+ ret2 = iterator.throw(err2);
28
+ } catch (e) {
29
+ return reject(e);
30
+ }
31
+ next(ret2);
32
+ }
33
+ function next(ret2) {
34
+ if (ret2.done) return resolve(fullRet ? ret2 : ret2.value);
35
+ if (ret2.value !== AWAIT) return resolve(ret2);
36
+ const awaitValue = ret2.value.RES;
37
+ const value = awaitValue && awaitValue.then === "function" ? awaitValue : Promise.resolve(awaitValue);
38
+ return value.then(onFulfilled, onRejected);
39
+ }
40
+ });
41
+ }
42
+
43
+ export { runAsync };
@@ -0,0 +1,25 @@
1
+ export declare const AWAIT: {
2
+ RES: any;
3
+ };
4
+ export declare const RETURN: {
5
+ RES: any;
6
+ };
7
+ export declare const CONTINUE: {
8
+ LABEL: string;
9
+ };
10
+ export declare const BREAK: {
11
+ LABEL: string;
12
+ };
13
+ export declare const SUPER: string;
14
+ export declare const SUPERCALL: string;
15
+ export declare const NOCTOR: string;
16
+ export declare const CLSCTOR: string;
17
+ export declare const NEWTARGET: string;
18
+ export declare const PRIVATE: string;
19
+ export declare const NOINIT: string;
20
+ export declare const DEADZONE: string;
21
+ export declare const OPTCHAIN: string;
22
+ export declare const IMPORT: string;
23
+ export declare const EXPORTS: string;
24
+ export declare const STRICT: string;
25
+ export declare const STRICT_FN: string;
@@ -0,0 +1,21 @@
1
+ import { createSymbol } from './util.js';
2
+
3
+ const AWAIT = { RES: void 0 };
4
+ const RETURN = { RES: void 0 };
5
+ const CONTINUE = { LABEL: void 0 };
6
+ const BREAK = { LABEL: void 0 };
7
+ const SUPER = createSymbol("super");
8
+ const SUPERCALL = createSymbol("supercall");
9
+ const NOCTOR = createSymbol("noctor");
10
+ const CLSCTOR = createSymbol("clsctor");
11
+ const NEWTARGET = createSymbol("newtarget");
12
+ const PRIVATE = createSymbol("private");
13
+ const NOINIT = createSymbol("noinit");
14
+ const DEADZONE = createSymbol("deadzone");
15
+ const OPTCHAIN = createSymbol("optchain");
16
+ const IMPORT = createSymbol("import");
17
+ const EXPORTS = createSymbol("exports");
18
+ const STRICT = createSymbol("strict");
19
+ const STRICT_FN = createSymbol("strict-fn");
20
+
21
+ export { AWAIT, BREAK, CLSCTOR, CONTINUE, DEADZONE, EXPORTS, IMPORT, NEWTARGET, NOCTOR, NOINIT, OPTCHAIN, PRIVATE, RETURN, STRICT, STRICT_FN, SUPER, SUPERCALL };
@@ -0,0 +1,33 @@
1
+ export declare const freeze: {
2
+ <T extends Function>(f: T): T;
3
+ <T extends {
4
+ [idx: string]: U | null | undefined | object;
5
+ }, U extends string | bigint | number | boolean | symbol>(o: T): Readonly<T>;
6
+ <T>(o: T): Readonly<T>;
7
+ };
8
+ export declare const define: <T>(o: T, p: PropertyKey, attributes: PropertyDescriptor & ThisType<any>) => T;
9
+ export declare const getDptor: (o: any, p: PropertyKey) => PropertyDescriptor | undefined;
10
+ export declare function hasOwn(obj: any, key: string): boolean;
11
+ export declare const getOwnNames: (o: any) => string[];
12
+ export declare function setProto(obj: any, proto: any): void;
13
+ export declare function getProto(obj: any): any;
14
+ export declare function getGetter(obj: any, key: string): (() => any) | ((v: any) => void);
15
+ export declare function getSetter(obj: any, key: string): (() => any) | ((v: any) => void);
16
+ export declare const create: {
17
+ (o: object | null): any;
18
+ (o: object | null, properties: PropertyDescriptorMap & ThisType<any>): any;
19
+ };
20
+ export declare function inherits(subClass: (...args: any[]) => any, superClass: (...args: any[]) => any): void;
21
+ export declare function callSuper(target: any, superClass: (...args: any[]) => any, args?: any[]): any;
22
+ export declare function _assign(target: any): any;
23
+ export declare const assign: {
24
+ <T extends {}, U>(target: T, source: U): T & U;
25
+ <T extends {}, U, V>(target: T, source1: U, source2: V): T & U & V;
26
+ <T extends {}, U, V, W>(target: T, source1: U, source2: V, source3: W): T & U & V & W;
27
+ (target: object, ...sources: any[]): any;
28
+ };
29
+ export declare let globalObj: any;
30
+ export declare const WINDOW: string;
31
+ export declare function createSandBox(): any;
32
+ export declare function createSymbol(key: string): string;
33
+ export declare function getAsyncIterator(obj: any): any;