greybel-interpreter 1.6.4 → 1.6.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/dist/context.d.ts +2 -0
- package/dist/context.js +3 -1
- package/dist/cps.js +28 -6
- package/dist/interpreter.d.ts +2 -0
- package/dist/interpreter.js +3 -1
- package/dist/operations/envar.d.ts +10 -0
- package/dist/operations/envar.js +23 -0
- package/package.json +1 -1
package/dist/context.d.ts
CHANGED
|
@@ -63,6 +63,7 @@ export interface ContextOptions {
|
|
|
63
63
|
handler?: HandlerContainer;
|
|
64
64
|
cps?: CPS;
|
|
65
65
|
processState?: ProcessState;
|
|
66
|
+
environmentVariables?: Map<string, string>;
|
|
66
67
|
}
|
|
67
68
|
export interface ContextForkOptions {
|
|
68
69
|
type: ContextType;
|
|
@@ -74,6 +75,7 @@ export default class OperationContext {
|
|
|
74
75
|
target: string;
|
|
75
76
|
stackItem: ASTBase;
|
|
76
77
|
debugger: Debugger;
|
|
78
|
+
environmentVariables: Map<string, string>;
|
|
77
79
|
handler: HandlerContainer;
|
|
78
80
|
previous: OperationContext;
|
|
79
81
|
readonly type: ContextType;
|
package/dist/context.js
CHANGED
|
@@ -146,6 +146,7 @@ class OperationContext {
|
|
|
146
146
|
this.handler = options.handler || new handler_container_1.default();
|
|
147
147
|
this.cps = options.cps || null;
|
|
148
148
|
this.processState = options.processState || new ProcessState();
|
|
149
|
+
this.environmentVariables = options.environmentVariables || new Map();
|
|
149
150
|
this.api = this.lookupApi();
|
|
150
151
|
this.globals = this.lookupGlobals();
|
|
151
152
|
this.locals = this.lookupLocals() || this;
|
|
@@ -286,7 +287,8 @@ class OperationContext {
|
|
|
286
287
|
debugger: this.debugger,
|
|
287
288
|
handler: this.handler,
|
|
288
289
|
cps: this.cps,
|
|
289
|
-
processState: this.processState
|
|
290
|
+
processState: this.processState,
|
|
291
|
+
environmentVariables: this.environmentVariables
|
|
290
292
|
});
|
|
291
293
|
if (options.type !== ContextType.Function) {
|
|
292
294
|
if (options.type !== ContextType.Loop) {
|
package/dist/cps.js
CHANGED
|
@@ -38,6 +38,7 @@ const not_1 = __importDefault(require("./operations/not"));
|
|
|
38
38
|
const resolve_1 = __importDefault(require("./operations/resolve"));
|
|
39
39
|
const return_1 = __importDefault(require("./operations/return"));
|
|
40
40
|
const while_1 = __importDefault(require("./operations/while"));
|
|
41
|
+
const envar_1 = __importDefault(require("./operations/envar"));
|
|
41
42
|
class CPSContext {
|
|
42
43
|
constructor(target, handler) {
|
|
43
44
|
this.target = target;
|
|
@@ -45,8 +46,9 @@ class CPSContext {
|
|
|
45
46
|
}
|
|
46
47
|
}
|
|
47
48
|
exports.CPSContext = CPSContext;
|
|
48
|
-
const visit = (context,
|
|
49
|
-
const
|
|
49
|
+
const visit = (context, stack, item) => __awaiter(void 0, void 0, void 0, function* () {
|
|
50
|
+
const currentTarget = stack[stack.length - 1];
|
|
51
|
+
const defaultVisit = visit.bind(null, context, stack);
|
|
50
52
|
switch (item.type) {
|
|
51
53
|
case greyscript_core_1.ASTType.MapConstructorExpression:
|
|
52
54
|
return new map_1.default(item, currentTarget).build(defaultVisit);
|
|
@@ -82,38 +84,58 @@ const visit = (context, currentTarget, item) => __awaiter(void 0, void 0, void 0
|
|
|
82
84
|
case greybel_core_1.ASTType.FeatureImportExpression: {
|
|
83
85
|
const importExpr = item;
|
|
84
86
|
const target = yield context.handler.resourceHandler.getTargetRelativeTo(currentTarget, importExpr.path);
|
|
87
|
+
if (stack.includes(target)) {
|
|
88
|
+
console.warn('Found circluar dependency. Using noop operation.');
|
|
89
|
+
return new noop_1.default(item, target);
|
|
90
|
+
}
|
|
91
|
+
stack.push(target);
|
|
85
92
|
const code = yield context.handler.resourceHandler.get(target);
|
|
86
93
|
if (code == null) {
|
|
87
94
|
throw new Error(`Cannot find import ${currentTarget}.`);
|
|
88
95
|
}
|
|
89
|
-
const subVisit = visit.bind(null, context,
|
|
96
|
+
const subVisit = visit.bind(null, context, stack);
|
|
90
97
|
const importStatement = yield new import_1.default(importExpr, target, code).build(subVisit);
|
|
98
|
+
stack.pop();
|
|
91
99
|
return importStatement;
|
|
92
100
|
}
|
|
93
101
|
case greybel_core_1.ASTType.FeatureIncludeExpression: {
|
|
94
102
|
const includeExpr = item;
|
|
95
103
|
const target = yield context.handler.resourceHandler.getTargetRelativeTo(currentTarget, includeExpr.path);
|
|
104
|
+
if (stack.includes(target)) {
|
|
105
|
+
console.warn('Found circluar dependency. Using noop operation.');
|
|
106
|
+
return new noop_1.default(item, target);
|
|
107
|
+
}
|
|
108
|
+
stack.push(target);
|
|
96
109
|
const code = yield context.handler.resourceHandler.get(target);
|
|
97
110
|
if (code == null) {
|
|
98
111
|
throw new Error(`Cannot find include ${currentTarget}.`);
|
|
99
112
|
}
|
|
100
|
-
const subVisit = visit.bind(null, context,
|
|
113
|
+
const subVisit = visit.bind(null, context, stack);
|
|
101
114
|
const importStatement = yield new include_1.default(includeExpr, target, code).build(subVisit);
|
|
115
|
+
stack.pop();
|
|
102
116
|
return importStatement;
|
|
103
117
|
}
|
|
104
118
|
case greyscript_core_1.ASTType.ImportCodeExpression: {
|
|
105
119
|
const importExpr = item;
|
|
106
120
|
const target = yield context.handler.resourceHandler.getTargetRelativeTo(currentTarget, importExpr.fileSystemDirectory);
|
|
121
|
+
if (stack.includes(target)) {
|
|
122
|
+
console.warn('Found circluar dependency. Using noop operation.');
|
|
123
|
+
return new noop_1.default(item, target);
|
|
124
|
+
}
|
|
125
|
+
stack.push(target);
|
|
107
126
|
const code = yield context.handler.resourceHandler.get(target);
|
|
108
127
|
if (code == null) {
|
|
109
128
|
throw new Error(`Cannot find native import ${currentTarget}.`);
|
|
110
129
|
}
|
|
111
|
-
const subVisit = visit.bind(null, context,
|
|
130
|
+
const subVisit = visit.bind(null, context, stack);
|
|
112
131
|
const importStatement = yield new include_1.default(importExpr, target, code).build(subVisit);
|
|
132
|
+
stack.pop();
|
|
113
133
|
return importStatement;
|
|
114
134
|
}
|
|
115
135
|
case greybel_core_1.ASTType.FeatureDebuggerExpression:
|
|
116
136
|
return new debugger_statement_1.default(item, currentTarget);
|
|
137
|
+
case greybel_core_1.ASTType.FeatureEnvarExpression:
|
|
138
|
+
return new envar_1.default(item, currentTarget);
|
|
117
139
|
case greyscript_core_1.ASTType.BooleanLiteral:
|
|
118
140
|
case greyscript_core_1.ASTType.StringLiteral:
|
|
119
141
|
case greyscript_core_1.ASTType.NumericLiteral:
|
|
@@ -152,7 +174,7 @@ const visit = (context, currentTarget, item) => __awaiter(void 0, void 0, void 0
|
|
|
152
174
|
class CPS {
|
|
153
175
|
constructor(context) {
|
|
154
176
|
this.context = context;
|
|
155
|
-
this.__visit = visit.bind(null, context, context.target);
|
|
177
|
+
this.__visit = visit.bind(null, context, [context.target]);
|
|
156
178
|
}
|
|
157
179
|
visit(item) {
|
|
158
180
|
return this.__visit(item);
|
package/dist/interpreter.d.ts
CHANGED
|
@@ -14,11 +14,13 @@ export interface InterpreterOptions {
|
|
|
14
14
|
params?: Array<string>;
|
|
15
15
|
handler?: HandlerContainer;
|
|
16
16
|
debugger?: Debugger;
|
|
17
|
+
environmentVariables?: Map<string, string>;
|
|
17
18
|
}
|
|
18
19
|
export default class Interpreter extends EventEmitter {
|
|
19
20
|
target: string;
|
|
20
21
|
api: ObjectValue;
|
|
21
22
|
params: Array<string>;
|
|
23
|
+
environmentVariables: Map<string, string>;
|
|
22
24
|
handler: HandlerContainer;
|
|
23
25
|
debugger: Debugger;
|
|
24
26
|
apiContext: OperationContext;
|
package/dist/interpreter.js
CHANGED
|
@@ -56,6 +56,7 @@ class Interpreter extends events_1.default {
|
|
|
56
56
|
this.debugger = options.debugger || new context_1.Debugger();
|
|
57
57
|
this.api = options.api || new object_value_1.default();
|
|
58
58
|
this.params = options.params || [];
|
|
59
|
+
this.environmentVariables = options.environmentVariables || new Map();
|
|
59
60
|
this.apiContext = null;
|
|
60
61
|
this.globalContext = null;
|
|
61
62
|
this.setTarget(options.target || 'unknown');
|
|
@@ -72,7 +73,8 @@ class Interpreter extends events_1.default {
|
|
|
72
73
|
isProtected: true,
|
|
73
74
|
debugger: this.debugger,
|
|
74
75
|
handler: this.handler,
|
|
75
|
-
cps: this.cps
|
|
76
|
+
cps: this.cps,
|
|
77
|
+
environmentVariables: this.environmentVariables
|
|
76
78
|
});
|
|
77
79
|
this.globalContext = this.apiContext.fork({
|
|
78
80
|
type: context_1.ContextType.Global,
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { ASTFeatureEnvarExpression } from 'greybel-core';
|
|
2
|
+
import context from '../context';
|
|
3
|
+
import CustomValue from '../types/base';
|
|
4
|
+
import Operation, { CPSVisit } from './operation';
|
|
5
|
+
export default class EnvarExpression extends Operation {
|
|
6
|
+
readonly item: ASTFeatureEnvarExpression;
|
|
7
|
+
constructor(item: ASTFeatureEnvarExpression, target?: string);
|
|
8
|
+
build(_visit: CPSVisit): Promise<Operation>;
|
|
9
|
+
handle(ctx: context): Promise<CustomValue>;
|
|
10
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const string_1 = __importDefault(require("../types/string"));
|
|
7
|
+
const operation_1 = __importDefault(require("./operation"));
|
|
8
|
+
class EnvarExpression extends operation_1.default {
|
|
9
|
+
constructor(item, target) {
|
|
10
|
+
super(null, target);
|
|
11
|
+
this.item = item;
|
|
12
|
+
}
|
|
13
|
+
build(_visit) {
|
|
14
|
+
return Promise.resolve(this);
|
|
15
|
+
}
|
|
16
|
+
handle(ctx) {
|
|
17
|
+
if (ctx.environmentVariables.has(this.item.name)) {
|
|
18
|
+
return Promise.resolve(new string_1.default(ctx.environmentVariables.get(this.item.name)));
|
|
19
|
+
}
|
|
20
|
+
throw new Error('Unknown environment variable.');
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
exports.default = EnvarExpression;
|