greybel-interpreter 1.7.7 → 1.7.8

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 CHANGED
@@ -54,6 +54,7 @@ export declare class FunctionState {
54
54
  }
55
55
  export interface ContextOptions {
56
56
  target?: string;
57
+ stackTrace?: Operation[];
57
58
  previous?: OperationContext;
58
59
  type?: ContextType;
59
60
  state?: ContextState;
@@ -73,7 +74,7 @@ export interface ContextForkOptions {
73
74
  }
74
75
  export declare class OperationContext {
75
76
  target: string;
76
- stackItem: ASTBase;
77
+ stackTrace: Operation[];
77
78
  debugger: Debugger;
78
79
  environmentVariables: Map<string, string>;
79
80
  handler: HandlerContainer;
@@ -94,7 +95,7 @@ export declare class OperationContext {
94
95
  private static readonly lookupGlobalsType;
95
96
  private static readonly lookupLocalsType;
96
97
  constructor(options?: ContextOptions);
97
- step(op: Operation): Promise<void>;
98
+ step(op: Operation): Promise<CustomValue>;
98
99
  setLastActive(ctx: OperationContext): OperationContext;
99
100
  getLastActive(): OperationContext;
100
101
  isExit(): boolean;
package/dist/context.js CHANGED
@@ -1,4 +1,13 @@
1
1
  "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
2
11
  Object.defineProperty(exports, "__esModule", { value: true });
3
12
  exports.OperationContext = exports.FunctionState = exports.LoopState = exports.ProcessState = exports.Debugger = exports.Scope = exports.ContextState = exports.ContextType = void 0;
4
13
  const handler_container_1 = require("./handler-container");
@@ -132,7 +141,7 @@ exports.FunctionState = FunctionState;
132
141
  class OperationContext {
133
142
  constructor(options = {}) {
134
143
  this.target = options.target || 'unknown';
135
- this.stackItem = null;
144
+ this.stackTrace = options.stackTrace || [];
136
145
  this.previous = options.previous || null;
137
146
  this.type = options.type || ContextType.Api;
138
147
  this.state = options.state || ContextState.Default;
@@ -149,16 +158,20 @@ class OperationContext {
149
158
  this.locals = this.lookupLocals() || this;
150
159
  }
151
160
  step(op) {
152
- if (!this.injected) {
153
- this.stackItem = op.item;
154
- this.target = op.target || this.target;
155
- this.setLastActive(this);
156
- if (this.debugger.getBreakpoint(this)) {
157
- this.debugger.interact(this, op.item, op);
158
- return this.debugger.resume();
161
+ return __awaiter(this, void 0, void 0, function* () {
162
+ if (!this.injected) {
163
+ this.target = op.target || this.target;
164
+ this.setLastActive(this);
165
+ if (this.debugger.getBreakpoint(this)) {
166
+ this.debugger.interact(this, op.item, op);
167
+ yield this.debugger.resume();
168
+ }
159
169
  }
160
- }
161
- return Promise.resolve();
170
+ this.stackTrace.unshift(op);
171
+ const result = yield op.handle(this);
172
+ this.stackTrace.shift();
173
+ return result;
174
+ });
162
175
  }
163
176
  setLastActive(ctx) {
164
177
  if (!ctx.injected) {
@@ -276,6 +289,7 @@ class OperationContext {
276
289
  fork(options) {
277
290
  const newContext = new OperationContext({
278
291
  target: options.target || this.target,
292
+ stackTrace: this.stackTrace,
279
293
  previous: this,
280
294
  type: options.type,
281
295
  state: options.state,
package/dist/cps.js CHANGED
@@ -95,7 +95,7 @@ const visit = (context, stack, item) => __awaiter(void 0, void 0, void 0, functi
95
95
  }
96
96
  try {
97
97
  const subVisit = visit.bind(null, context, [...stack, target]);
98
- const importStatement = yield new import_1.Import(importExpr, target, code).build(subVisit);
98
+ const importStatement = yield new import_1.Import(importExpr, currentTarget, target, code).build(subVisit);
99
99
  return importStatement;
100
100
  }
101
101
  catch (err) {
@@ -124,7 +124,7 @@ const visit = (context, stack, item) => __awaiter(void 0, void 0, void 0, functi
124
124
  }
125
125
  try {
126
126
  const subVisit = visit.bind(null, context, [...stack, target]);
127
- const importStatement = yield new include_1.Include(includeExpr, target, code).build(subVisit);
127
+ const importStatement = yield new include_1.Include(includeExpr, currentTarget, target, code).build(subVisit);
128
128
  return importStatement;
129
129
  }
130
130
  catch (err) {
@@ -157,7 +157,7 @@ const visit = (context, stack, item) => __awaiter(void 0, void 0, void 0, functi
157
157
  }
158
158
  try {
159
159
  const subVisit = visit.bind(null, context, [...stack, target]);
160
- const importStatement = yield new include_1.Include(importExpr, target, code).build(subVisit);
160
+ const importStatement = yield new include_1.Include(importExpr, currentTarget, target, code).build(subVisit);
161
161
  return importStatement;
162
162
  }
163
163
  catch (err) {
@@ -36,7 +36,6 @@ class Block extends operation_1.Operation {
36
36
  }
37
37
  const op = this.stack[index];
38
38
  yield ctx.step(op);
39
- yield op.handle(ctx);
40
39
  }
41
40
  return default_1.DefaultType.Void;
42
41
  });
@@ -69,7 +69,7 @@ class IfStatement extends operation_1.Operation {
69
69
  return __awaiter(this, void 0, void 0, function* () {
70
70
  for (let index = 0; index < this.clauses.length; index++) {
71
71
  const clause = this.clauses[index];
72
- const clauseResult = yield clause.condition.handle(ctx);
72
+ const clauseResult = yield ctx.step(clause.condition);
73
73
  if (clauseResult.toTruthy()) {
74
74
  yield clause.block.handle(ctx);
75
75
  break;
@@ -10,10 +10,11 @@ export declare const EXPORTS_PROPERTY: CustomString;
10
10
  export declare const EXPORTS_PATH: Path<CustomString>;
11
11
  export declare class Import extends Operation {
12
12
  readonly item: ASTFeatureImportExpression;
13
+ newTarget: string;
13
14
  code: string;
14
15
  chunk: ASTBase;
15
16
  top: Operation;
16
- constructor(item: ASTFeatureImportExpression, target: string, code: string);
17
+ constructor(item: ASTFeatureImportExpression, target: string, newTarget: string, code: string);
17
18
  build(visit: CPSVisit): Promise<Operation>;
18
19
  handle(ctx: OperationContext): Promise<CustomValue>;
19
20
  }
@@ -21,8 +21,9 @@ exports.MODULE_PROPERTY = new string_1.CustomString('module');
21
21
  exports.EXPORTS_PROPERTY = new string_1.CustomString('exports');
22
22
  exports.EXPORTS_PATH = new path_1.Path([exports.MODULE_PROPERTY, exports.EXPORTS_PROPERTY]);
23
23
  class Import extends operation_1.Operation {
24
- constructor(item, target, code) {
24
+ constructor(item, target, newTarget, code) {
25
25
  super(null, target);
26
+ this.newTarget = newTarget;
26
27
  this.item = item;
27
28
  this.code = code;
28
29
  }
@@ -39,7 +40,7 @@ class Import extends operation_1.Operation {
39
40
  const importCtx = ctx.fork({
40
41
  type: context_1.ContextType.External,
41
42
  state: context_1.ContextState.Default,
42
- target: this.target
43
+ target: this.newTarget
43
44
  });
44
45
  importCtx.locals.scope.set(exports.MODULE_PROPERTY, new map_1.CustomMap());
45
46
  yield this.top.handle(importCtx);
@@ -4,10 +4,11 @@ import { CustomValue } from '../types/base';
4
4
  import { CPSVisit, Operation } from './operation';
5
5
  export declare class Include extends Operation {
6
6
  readonly item: ASTBase;
7
+ newTarget: string;
7
8
  code: string;
8
9
  chunk: ASTBase;
9
10
  top: Operation;
10
- constructor(item: ASTBase, target: string, code: string);
11
+ constructor(item: ASTBase, target: string, newTarget: string, code: string);
11
12
  build(visit: CPSVisit): Promise<Operation>;
12
13
  handle(ctx: OperationContext): Promise<CustomValue>;
13
14
  }
@@ -14,8 +14,9 @@ const greybel_core_1 = require("greybel-core");
14
14
  const context_1 = require("../context");
15
15
  const operation_1 = require("./operation");
16
16
  class Include extends operation_1.Operation {
17
- constructor(item, target, code) {
17
+ constructor(item, target, newTarget, code) {
18
18
  super(null, target);
19
+ this.newTarget = newTarget;
19
20
  this.item = item;
20
21
  this.code = code;
21
22
  }
@@ -31,7 +32,7 @@ class Include extends operation_1.Operation {
31
32
  const importCtx = ctx.fork({
32
33
  type: context_1.ContextType.External,
33
34
  state: context_1.ContextState.Temporary,
34
- target: this.target
35
+ target: this.newTarget
35
36
  });
36
37
  return this.top.handle(importCtx);
37
38
  }
@@ -38,7 +38,7 @@ class While extends operation_1.Operation {
38
38
  return new Promise((resolve, reject) => {
39
39
  const iteration = () => __awaiter(this, void 0, void 0, function* () {
40
40
  try {
41
- const conditionResult = yield this.condition.handle(whileCtx);
41
+ const conditionResult = yield whileCtx.step(this.condition);
42
42
  if (!conditionResult.toTruthy()) {
43
43
  resolve(default_1.DefaultType.Void);
44
44
  return;
@@ -1,12 +1,12 @@
1
1
  import { ASTBase } from 'greyscript-core';
2
+ import { Operation } from '../operations/operation';
2
3
  interface RuntimeContext {
3
- previous?: RuntimeContext;
4
- stackItem?: ASTBase;
4
+ stackTrace?: Operation[];
5
5
  target: string;
6
6
  }
7
7
  export declare class RuntimeError extends Error {
8
- relatedItem: ASTBase | null;
9
8
  relatedTarget: string;
9
+ stackTrace: Operation[];
10
10
  source?: Error;
11
11
  constructor(message: string, context: RuntimeContext, source?: Error);
12
12
  private createTrace;
@@ -4,24 +4,18 @@ exports.PrepareError = exports.RuntimeError = void 0;
4
4
  class RuntimeError extends Error {
5
5
  constructor(message, context, source) {
6
6
  super(message);
7
- this.relatedItem = context.stackItem || null;
8
7
  this.relatedTarget = context.target;
9
- this.stack = this.createTrace(context);
8
+ this.stackTrace = context.stackTrace || [];
9
+ this.stack = this.createTrace();
10
10
  this.source = source;
11
11
  }
12
- createTrace(context) {
13
- var _a, _b;
14
- const related = new Map();
15
- let item = context;
16
- while (item) {
17
- related.set(item.stackItem, item.target);
18
- item = item.previous;
19
- }
20
- const lines = [];
21
- for (const [item, target] of related) {
22
- lines.push(`${target} at ${(_a = item === null || item === void 0 ? void 0 : item.start.line) !== null && _a !== void 0 ? _a : 0}:${(_b = item === null || item === void 0 ? void 0 : item.start.character) !== null && _b !== void 0 ? _b : 0}`);
23
- }
24
- return lines.join('\n');
12
+ createTrace() {
13
+ return this.stackTrace
14
+ .map((op) => {
15
+ var _a, _b, _c, _d;
16
+ return `${op.target} at ${(_b = (_a = op.item) === null || _a === void 0 ? void 0 : _a.start.line) !== null && _b !== void 0 ? _b : 0}:${(_d = (_c = op.item) === null || _c === void 0 ? void 0 : _c.start.character) !== null && _d !== void 0 ? _d : 0}`;
17
+ })
18
+ .join('\n');
25
19
  }
26
20
  }
27
21
  exports.RuntimeError = RuntimeError;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "greybel-interpreter",
3
- "version": "1.7.7",
3
+ "version": "1.7.8",
4
4
  "description": "Interpreter",
5
5
  "main": "dist/index",
6
6
  "typings": "dist/index",