greybel-interpreter 2.6.0 → 2.6.2

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
@@ -67,12 +67,14 @@ export interface ContextOptions {
67
67
  cps?: CPS;
68
68
  processState?: ProcessState;
69
69
  environmentVariables?: Map<string, string>;
70
+ ignoreOuter?: boolean;
70
71
  }
71
72
  export interface ContextForkOptions {
72
73
  type: ContextType;
73
74
  state: ContextState;
74
75
  target?: string;
75
76
  injected?: boolean;
77
+ ignoreOuter?: boolean;
76
78
  }
77
79
  export declare class OperationContext {
78
80
  target: string;
@@ -112,6 +114,7 @@ export declare class OperationContext {
112
114
  lookupApi(): OperationContext;
113
115
  lookupGlobals(): OperationContext;
114
116
  lookupLocals(): OperationContext;
117
+ lookupOuter(): OperationContext;
115
118
  extend(map: ObjectValue): OperationContext;
116
119
  set(path: Path<CustomValue> | CustomValue, value: CustomValue): void;
117
120
  get(path: Path<CustomValue> | CustomValue): CustomValue;
package/dist/context.js CHANGED
@@ -149,7 +149,7 @@ class FunctionState {
149
149
  exports.FunctionState = FunctionState;
150
150
  class OperationContext {
151
151
  constructor(options = {}) {
152
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
152
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o;
153
153
  this.target = (_a = options.target) !== null && _a !== void 0 ? _a : 'unknown';
154
154
  this.stackTrace = (_b = options.stackTrace) !== null && _b !== void 0 ? _b : [];
155
155
  this.previous = (_c = options.previous) !== null && _c !== void 0 ? _c : null;
@@ -168,7 +168,7 @@ class OperationContext {
168
168
  this.api = this.lookupApi();
169
169
  this.globals = this.lookupGlobals();
170
170
  this.locals = (_o = this.lookupLocals()) !== null && _o !== void 0 ? _o : this;
171
- this.outer = (_q = (_p = this.locals.previous) === null || _p === void 0 ? void 0 : _p.lookupLocals()) !== null && _q !== void 0 ? _q : null;
171
+ this.outer = options.ignoreOuter ? null : this.lookupOuter();
172
172
  }
173
173
  isIgnoredInDebugging(op) {
174
174
  return op instanceof operation_1.OperationBlock || op instanceof noop_1.Noop;
@@ -272,6 +272,10 @@ class OperationContext {
272
272
  lookupLocals() {
273
273
  return this.lookupType(OperationContext.lookupLocalsType);
274
274
  }
275
+ lookupOuter() {
276
+ var _a, _b;
277
+ return (_b = (_a = this.locals.previous) === null || _a === void 0 ? void 0 : _a.lookupLocals()) !== null && _b !== void 0 ? _b : null;
278
+ }
275
279
  extend(map) {
276
280
  var _a;
277
281
  if (this.state === ContextState.Temporary) {
@@ -316,6 +320,7 @@ class OperationContext {
316
320
  previous: this,
317
321
  type: options.type,
318
322
  state: options.state,
323
+ ignoreOuter: options.ignoreOuter,
319
324
  isProtected: false,
320
325
  injected: this.injected,
321
326
  debugger: this.debugger,
@@ -11,6 +11,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.AssignSelf = void 0;
13
13
  const default_1 = require("../types/default");
14
+ const function_1 = require("./function");
14
15
  const operation_1 = require("./operation");
15
16
  class AssignSelf extends operation_1.Operation {
16
17
  constructor(item, target) {
@@ -25,7 +26,13 @@ class AssignSelf extends operation_1.Operation {
25
26
  }
26
27
  handle(ctx) {
27
28
  return __awaiter(this, void 0, void 0, function* () {
28
- const rightValue = yield this.right.handle(ctx);
29
+ let rightValue;
30
+ if (this.right instanceof function_1.FunctionOperation) {
31
+ rightValue = yield this.right.handle(ctx, true);
32
+ }
33
+ else {
34
+ rightValue = yield this.right.handle(ctx);
35
+ }
29
36
  ctx.functionState.context = rightValue;
30
37
  return default_1.DefaultType.Void;
31
38
  });
@@ -12,6 +12,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.Assign = void 0;
13
13
  const default_1 = require("../types/default");
14
14
  const create_resolve_1 = require("../utils/create-resolve");
15
+ const function_1 = require("./function");
15
16
  const operation_1 = require("./operation");
16
17
  const resolve_1 = require("./resolve");
17
18
  class Assign extends operation_1.Operation {
@@ -33,10 +34,16 @@ class Assign extends operation_1.Operation {
33
34
  if (ctx.isExit()) {
34
35
  return default_1.DefaultType.Void;
35
36
  }
36
- const rightValue = yield this.right.handle(ctx);
37
37
  if (resolveResult.path.count() === 0) {
38
38
  throw new Error('Resolve path cannot be empty.');
39
39
  }
40
+ let rightValue;
41
+ if (this.right instanceof function_1.FunctionOperation) {
42
+ rightValue = yield this.right.handle(ctx, true);
43
+ }
44
+ else {
45
+ rightValue = yield this.right.handle(ctx);
46
+ }
40
47
  if (!(resolveResult.handle instanceof resolve_1.ResolveNil)) {
41
48
  const resultValueCtx = resolveResult.handle;
42
49
  resultValueCtx.set(resolveResult.path, rightValue);
@@ -17,6 +17,7 @@ const string_1 = require("../types/string");
17
17
  const set_immediate_1 = require("../utils/set-immediate");
18
18
  const block_1 = require("./block");
19
19
  const operation_1 = require("./operation");
20
+ const FOR_BATCH_SIZE = 30;
20
21
  class For extends operation_1.OperationBlock {
21
22
  constructor(item, target) {
22
23
  super(null, target);
@@ -49,32 +50,41 @@ class For extends operation_1.OperationBlock {
49
50
  const idxIdentifier = new string_1.CustomString(`__${identifier}_idx`);
50
51
  const varIdentifier = new string_1.CustomString(identifier);
51
52
  let iteratorResult = iterator.next();
52
- const iteration = () => __awaiter(this, void 0, void 0, function* () {
53
- try {
54
- if (iteratorResult.done) {
55
- resolve(default_1.DefaultType.Void);
56
- return;
57
- }
58
- const current = iteratorResult.value;
59
- loopState.isContinue = false;
60
- forCtx.set(idxIdentifier, new number_1.CustomNumber(iterator.index - 1));
61
- forCtx.set(varIdentifier, current);
62
- yield this.block.handle(forCtx);
63
- if (loopState.isBreak ||
64
- forCtx.functionState.isReturn ||
65
- ctx.isExit()) {
66
- resolve(default_1.DefaultType.Void);
67
- return;
68
- }
69
- const idxValue = forCtx.get(idxIdentifier).toNumber();
70
- iterator.index += idxValue - (iterator.index - 1);
71
- iteratorResult = iterator.next();
72
- (0, set_immediate_1.setImmediate)(iteration);
53
+ const next = () => __awaiter(this, void 0, void 0, function* () {
54
+ if (iteratorResult.done) {
55
+ return false;
73
56
  }
74
- catch (err) {
75
- reject(err);
57
+ const current = iteratorResult.value;
58
+ loopState.isContinue = false;
59
+ forCtx.set(idxIdentifier, new number_1.CustomNumber(iterator.index - 1));
60
+ forCtx.set(varIdentifier, current);
61
+ yield this.block.handle(forCtx);
62
+ if (loopState.isBreak ||
63
+ forCtx.functionState.isReturn ||
64
+ ctx.isExit()) {
65
+ return false;
76
66
  }
67
+ const idxValue = forCtx.get(idxIdentifier).toNumber();
68
+ iterator.index += idxValue - (iterator.index - 1);
69
+ iteratorResult = iterator.next();
70
+ return true;
77
71
  });
72
+ const iteration = function () {
73
+ return __awaiter(this, void 0, void 0, function* () {
74
+ try {
75
+ for (let index = 0; index < FOR_BATCH_SIZE; index++) {
76
+ if (!(yield next())) {
77
+ resolve(default_1.DefaultType.Void);
78
+ return;
79
+ }
80
+ }
81
+ (0, set_immediate_1.setImmediate)(iteration);
82
+ }
83
+ catch (err) {
84
+ reject(err);
85
+ }
86
+ });
87
+ };
78
88
  iteration();
79
89
  });
80
90
  });
@@ -15,5 +15,5 @@ export declare class FunctionOperation extends Operation {
15
15
  args: FunctionOperationArgument[];
16
16
  constructor(item: ASTFunctionStatement, target?: string);
17
17
  build(visit: CPSVisit): Promise<Operation>;
18
- handle(ctx: OperationContext): Promise<CustomValue>;
18
+ handle(ctx: OperationContext, assignOuter?: boolean): Promise<CustomValue>;
19
19
  }
@@ -54,7 +54,7 @@ class FunctionOperation extends operation_1.Operation {
54
54
  return this;
55
55
  });
56
56
  }
57
- handle(ctx) {
57
+ handle(ctx, assignOuter = false) {
58
58
  const func = new function_1.CustomFunction(ctx, 'anonymous', (fnCtx, self, args, next) => __awaiter(this, void 0, void 0, function* () {
59
59
  const functionState = new context_1.FunctionState();
60
60
  functionState.context = self;
@@ -73,7 +73,7 @@ class FunctionOperation extends operation_1.Operation {
73
73
  fnCtx.functionState = functionState;
74
74
  yield this.block.handle(fnCtx);
75
75
  return functionState.value;
76
- }));
76
+ }), assignOuter);
77
77
  for (const item of this.args) {
78
78
  func.addArgument(item.name, item.op);
79
79
  }
@@ -15,6 +15,7 @@ const default_1 = require("../types/default");
15
15
  const set_immediate_1 = require("../utils/set-immediate");
16
16
  const block_1 = require("./block");
17
17
  const operation_1 = require("./operation");
18
+ const WHILE_BATCH_SIZE = 30;
18
19
  class While extends operation_1.OperationBlock {
19
20
  constructor(item, target) {
20
21
  super(null, target);
@@ -37,27 +38,38 @@ class While extends operation_1.OperationBlock {
37
38
  const loopState = new context_1.LoopState();
38
39
  whileCtx.loopState = loopState;
39
40
  return new Promise((resolve, reject) => {
40
- const iteration = () => __awaiter(this, void 0, void 0, function* () {
41
- try {
42
- const conditionResult = yield whileCtx.step(this.condition);
43
- if (!conditionResult.toTruthy()) {
44
- resolve(default_1.DefaultType.Void);
45
- return;
46
- }
47
- loopState.isContinue = false;
48
- yield this.block.handle(whileCtx);
49
- if (loopState.isBreak ||
50
- whileCtx.functionState.isReturn ||
51
- ctx.isExit()) {
52
- resolve(default_1.DefaultType.Void);
53
- return;
54
- }
55
- (0, set_immediate_1.setImmediate)(iteration);
41
+ const next = () => __awaiter(this, void 0, void 0, function* () {
42
+ const conditionResult = yield whileCtx.step(this.condition);
43
+ if (!conditionResult.toTruthy()) {
44
+ resolve(default_1.DefaultType.Void);
45
+ return false;
56
46
  }
57
- catch (err) {
58
- reject(err);
47
+ loopState.isContinue = false;
48
+ yield this.block.handle(whileCtx);
49
+ if (loopState.isBreak ||
50
+ whileCtx.functionState.isReturn ||
51
+ ctx.isExit()) {
52
+ resolve(default_1.DefaultType.Void);
53
+ return false;
59
54
  }
55
+ return true;
60
56
  });
57
+ const iteration = function () {
58
+ return __awaiter(this, void 0, void 0, function* () {
59
+ try {
60
+ for (let index = 0; index < WHILE_BATCH_SIZE; index++) {
61
+ if (!(yield next())) {
62
+ resolve(default_1.DefaultType.Void);
63
+ return;
64
+ }
65
+ }
66
+ (0, set_immediate_1.setImmediate)(iteration);
67
+ }
68
+ catch (err) {
69
+ reject(err);
70
+ }
71
+ });
72
+ };
61
73
  iteration();
62
74
  });
63
75
  });
@@ -21,10 +21,11 @@ export declare class CustomFunction extends CustomValue {
21
21
  readonly name: string;
22
22
  readonly value: Callback;
23
23
  readonly argumentDefs: Array<Argument>;
24
+ readonly assignOuter: boolean;
24
25
  static createExternalAnonymous(callback: Callback): CustomFunction;
25
26
  static createExternal(name: string, callback: Callback): CustomFunction;
26
27
  static createExternalWithSelf(name: string, callback: Callback): CustomFunction;
27
- constructor(scope: OperationContext, name: string, callback: Callback);
28
+ constructor(scope: OperationContext, name: string, callback: Callback, assignOuter?: boolean);
28
29
  addArgument(name: string, defaultValue?: Operation | CustomValue): CustomFunction;
29
30
  fork(): CustomValue;
30
31
  getCustomType(): string;
@@ -52,12 +52,13 @@ class CustomFunction extends base_1.CustomValue {
52
52
  static createExternalWithSelf(name, callback) {
53
53
  return new CustomFunction(null, name, callback).addArgument(exports.SELF_NAMESPACE);
54
54
  }
55
- constructor(scope, name, callback) {
55
+ constructor(scope, name, callback, assignOuter = false) {
56
56
  super();
57
57
  this.scope = scope;
58
58
  this.name = name;
59
59
  this.value = callback;
60
60
  this.argumentDefs = [];
61
+ this.assignOuter = assignOuter;
61
62
  }
62
63
  addArgument(name, defaultValue = default_1.DefaultType.Void) {
63
64
  this.argumentDefs.push(new Argument(name, defaultValue));
@@ -112,7 +113,8 @@ class CustomFunction extends base_1.CustomValue {
112
113
  }
113
114
  const fnCtx = (_a = this.scope) === null || _a === void 0 ? void 0 : _a.fork({
114
115
  type: context_1.ContextType.Function,
115
- state: context_1.ContextState.Default
116
+ state: context_1.ContextState.Default,
117
+ ignoreOuter: !this.assignOuter
116
118
  });
117
119
  const argMap = new Map();
118
120
  const hasSelf = !(self instanceof nil_1.CustomNil);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "greybel-interpreter",
3
- "version": "2.6.0",
3
+ "version": "2.6.2",
4
4
  "description": "Interpreter",
5
5
  "main": "dist/index",
6
6
  "typings": "dist/index",