greybel-interpreter 2.4.0 → 2.5.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.
package/dist/index.d.ts CHANGED
@@ -40,6 +40,7 @@ export { CustomNil } from './types/nil';
40
40
  export { CustomNumber } from './types/number';
41
41
  export { CustomString, CustomStringIterator } from './types/string';
42
42
  export { CustomObject, CustomValueWithIntrinsics } from './types/with-intrinsics';
43
+ export { deepEqual } from './utils/deep-equal';
43
44
  export { PrepareError, RuntimeError } from './utils/error';
44
45
  export { ObjectValue } from './utils/object-value';
45
46
  export { Path } from './utils/path';
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Operation = exports.Not = exports.Noop = exports.NewInstance = exports.NegatedBinary = exports.MapOperation = exports.Literal = exports.List = exports.Include = exports.Import = exports.IfStatement = exports.Clause = exports.FunctionOperation = exports.For = exports.StringProcessorHandler = exports.NumberProcessorHandler = exports.MapProcessorHandler = exports.ListProcessorHandler = exports.handleString = exports.handleNumber = exports.handleMap = exports.handleList = exports.handle = exports.GenericProcessorHandler = exports.Evaluate = exports.DebuggerStatement = exports.Continue = exports.Chunk = exports.Call = exports.Break = exports.Block = exports.Assign = exports.Interpreter = exports.HandlerContainer = exports.ResourceHandler = exports.DefaultResourceHandler = exports.OutputHandler = exports.DefaultOutputHandler = exports.ErrorHandler = exports.DefaultErrorHandler = exports.CPSContext = exports.CPS = exports.Scope = exports.ProcessState = exports.OperationContext = exports.LoopState = exports.FunctionState = exports.Debugger = exports.ContextType = exports.ContextState = void 0;
4
- exports.Path = exports.ObjectValue = exports.RuntimeError = exports.PrepareError = exports.CustomValueWithIntrinsics = exports.CustomObject = exports.CustomStringIterator = exports.CustomString = exports.CustomNumber = exports.CustomNil = exports.CustomMapIterator = exports.CustomMap = exports.CustomListIterator = exports.CustomList = exports.CustomFunction = exports.Argument = exports.DefaultType = exports.CustomBoolean = exports.CustomValue = exports.While = exports.Return = exports.ResolveResult = exports.Resolve = exports.OperationSegment = exports.IndexSegment = exports.IdentifierSegment = exports.Reference = void 0;
4
+ exports.Path = exports.ObjectValue = exports.RuntimeError = exports.PrepareError = exports.deepEqual = exports.CustomValueWithIntrinsics = exports.CustomObject = exports.CustomStringIterator = exports.CustomString = exports.CustomNumber = exports.CustomNil = exports.CustomMapIterator = exports.CustomMap = exports.CustomListIterator = exports.CustomList = exports.CustomFunction = exports.Argument = exports.DefaultType = exports.CustomBoolean = exports.CustomValue = exports.While = exports.Return = exports.ResolveResult = exports.Resolve = exports.OperationSegment = exports.IndexSegment = exports.IdentifierSegment = exports.Reference = void 0;
5
5
  var context_1 = require("./context");
6
6
  Object.defineProperty(exports, "ContextState", { enumerable: true, get: function () { return context_1.ContextState; } });
7
7
  Object.defineProperty(exports, "ContextType", { enumerable: true, get: function () { return context_1.ContextType; } });
@@ -117,6 +117,8 @@ Object.defineProperty(exports, "CustomStringIterator", { enumerable: true, get:
117
117
  var with_intrinsics_1 = require("./types/with-intrinsics");
118
118
  Object.defineProperty(exports, "CustomObject", { enumerable: true, get: function () { return with_intrinsics_1.CustomObject; } });
119
119
  Object.defineProperty(exports, "CustomValueWithIntrinsics", { enumerable: true, get: function () { return with_intrinsics_1.CustomValueWithIntrinsics; } });
120
+ var deep_equal_1 = require("./utils/deep-equal");
121
+ Object.defineProperty(exports, "deepEqual", { enumerable: true, get: function () { return deep_equal_1.deepEqual; } });
120
122
  var error_2 = require("./utils/error");
121
123
  Object.defineProperty(exports, "PrepareError", { enumerable: true, get: function () { return error_2.PrepareError; } });
122
124
  Object.defineProperty(exports, "RuntimeError", { enumerable: true, get: function () { return error_2.RuntimeError; } });
@@ -19,6 +19,7 @@ const map_1 = require("../types/map");
19
19
  const nil_1 = require("../types/nil");
20
20
  const number_1 = require("../types/number");
21
21
  const string_1 = require("../types/string");
22
+ const deep_equal_1 = require("../utils/deep-equal");
22
23
  const operation_1 = require("./operation");
23
24
  exports.GenericProcessorHandler = {
24
25
  [greyscript_core_1.Operator.And]: (a, b) => new boolean_1.CustomBoolean(a.toTruthy() && b.toTruthy()),
@@ -125,13 +126,13 @@ exports.ListProcessorHandler = {
125
126
  },
126
127
  [greyscript_core_1.Operator.Equal]: (left, right) => {
127
128
  if (right instanceof list_1.CustomList) {
128
- return new boolean_1.CustomBoolean(left.hash() === right.hash());
129
+ return new boolean_1.CustomBoolean((0, deep_equal_1.deepEqual)(left, right));
129
130
  }
130
131
  return default_1.DefaultType.Void;
131
132
  },
132
133
  [greyscript_core_1.Operator.NotEqual]: (left, right) => {
133
134
  if (right instanceof list_1.CustomList) {
134
- return new boolean_1.CustomBoolean(left.hash() !== right.hash());
135
+ return new boolean_1.CustomBoolean(!(0, deep_equal_1.deepEqual)(left, right));
135
136
  }
136
137
  return default_1.DefaultType.Void;
137
138
  },
@@ -157,13 +158,13 @@ exports.MapProcessorHandler = {
157
158
  },
158
159
  [greyscript_core_1.Operator.Equal]: (left, right) => {
159
160
  if (right instanceof map_1.CustomMap) {
160
- return new boolean_1.CustomBoolean(left.hash() === right.hash());
161
+ return new boolean_1.CustomBoolean((0, deep_equal_1.deepEqual)(left, right));
161
162
  }
162
163
  return default_1.DefaultType.Void;
163
164
  },
164
165
  [greyscript_core_1.Operator.NotEqual]: (left, right) => {
165
166
  if (right instanceof map_1.CustomMap) {
166
- return new boolean_1.CustomBoolean(left.hash() !== right.hash());
167
+ return new boolean_1.CustomBoolean(!(0, deep_equal_1.deepEqual)(left, right));
167
168
  }
168
169
  return default_1.DefaultType.Void;
169
170
  }
@@ -8,5 +8,4 @@ export declare abstract class CustomValue {
8
8
  abstract toTruthy(): boolean;
9
9
  abstract fork(): CustomValue;
10
10
  abstract instanceOf(value: CustomValue): boolean;
11
- abstract hash(recursionDepth?: number): number;
12
11
  }
@@ -35,5 +35,4 @@ export declare class CustomFunction extends CustomValue {
35
35
  toTruthy(): boolean;
36
36
  instanceOf(v: CustomValue): boolean;
37
37
  run(self: CustomValue, args: Array<CustomValue>, callContext: OperationContext, next?: CustomMap): Promise<CustomValue>;
38
- hash(): number;
39
38
  }
@@ -14,7 +14,6 @@ const context_1 = require("../context");
14
14
  const literal_1 = require("../operations/literal");
15
15
  const operation_1 = require("../operations/operation");
16
16
  const reference_1 = require("../operations/reference");
17
- const hash_1 = require("../utils/hash");
18
17
  const object_value_1 = require("../utils/object-value");
19
18
  const base_1 = require("./base");
20
19
  const default_1 = require("./default");
@@ -135,9 +134,6 @@ class CustomFunction extends base_1.CustomValue {
135
134
  return this.value(fnCtx !== null && fnCtx !== void 0 ? fnCtx : callContext, selfValue, argMap, isa);
136
135
  });
137
136
  }
138
- hash() {
139
- return (0, hash_1.getStringHashCode)(this.toString());
140
- }
141
137
  }
142
138
  CustomFunction.intrinsics = new object_value_1.ObjectValue();
143
139
  exports.CustomFunction = CustomFunction;
@@ -28,5 +28,4 @@ export declare class CustomList extends CustomObject {
28
28
  has(path: Path<CustomValue> | CustomValue): boolean;
29
29
  set(path: Path<CustomValue> | CustomValue, newValue: CustomValue): void;
30
30
  get(path: Path<CustomValue> | CustomValue): CustomValue;
31
- hash(recursionDepth?: number): number;
32
31
  }
@@ -1,7 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.CustomList = exports.CustomListIterator = void 0;
4
- const hash_1 = require("../utils/hash");
5
4
  const object_value_1 = require("../utils/object-value");
6
5
  const path_1 = require("../utils/path");
7
6
  const base_1 = require("./base");
@@ -157,15 +156,6 @@ class CustomList extends with_intrinsics_1.CustomObject {
157
156
  }
158
157
  throw new Error(`Unknown path in list ${path.toString()}.`);
159
158
  }
160
- hash(recursionDepth = 0) {
161
- let result = (0, hash_1.getHashCode)(this.value.length);
162
- if (recursionDepth > 16)
163
- return result;
164
- this.value.forEach((value) => {
165
- result = (0, hash_1.rotateBits)(result) ^ value.hash(recursionDepth + 1);
166
- });
167
- return result;
168
- }
169
159
  }
170
160
  CustomList.intrinsics = new object_value_1.ObjectValue();
171
161
  exports.CustomList = CustomList;
@@ -34,5 +34,4 @@ export declare class CustomMap extends CustomObject {
34
34
  get(path: Path<CustomValue> | CustomValue): CustomValue;
35
35
  createInstance(): CustomMap;
36
36
  getIsa(): CustomMap | null;
37
- hash(recursionDepth?: number): number;
38
37
  }
package/dist/types/map.js CHANGED
@@ -1,7 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.CustomMap = exports.CustomMapIterator = exports.CUSTOM_MAP_MAX_DEPTH_VALUE = exports.CUSTOM_MAP_MAX_DEPTH = exports.ISA_PROPERTY = exports.CLASS_ID_PROPERTY = void 0;
4
- const hash_1 = require("../utils/hash");
5
4
  const object_value_1 = require("../utils/object-value");
6
5
  const path_1 = require("../utils/path");
7
6
  const base_1 = require("./base");
@@ -182,16 +181,6 @@ class CustomMap extends with_intrinsics_1.CustomObject {
182
181
  const isa = this.value.get(exports.ISA_PROPERTY);
183
182
  return isa instanceof CustomMap ? isa : null;
184
183
  }
185
- hash(recursionDepth = 0) {
186
- let result = (0, hash_1.getHashCode)(this.value.size);
187
- if (recursionDepth > 16)
188
- return result;
189
- this.value.forEach((value, key) => {
190
- result ^= key.hash(recursionDepth + 1);
191
- result ^= value.hash(recursionDepth + 1);
192
- });
193
- return result;
194
- }
195
184
  }
196
185
  CustomMap.intrinsics = new object_value_1.ObjectValue();
197
186
  exports.CustomMap = CustomMap;
@@ -9,6 +9,5 @@ export declare class CustomNil extends CustomValue {
9
9
  toInt(): number;
10
10
  toTruthy(): boolean;
11
11
  instanceOf(v: CustomValue): boolean;
12
- hash(): number;
13
12
  }
14
13
  export declare const Void: CustomNil;
package/dist/types/nil.js CHANGED
@@ -31,9 +31,6 @@ class CustomNil extends base_1.CustomValue {
31
31
  instanceOf(v) {
32
32
  return v instanceof CustomNil;
33
33
  }
34
- hash() {
35
- return 0;
36
- }
37
34
  }
38
35
  exports.CustomNil = CustomNil;
39
36
  exports.Void = new CustomNil();
@@ -22,7 +22,6 @@ export declare class CustomNumber extends CustomValueWithIntrinsics {
22
22
  has(_path: Path<CustomValue> | CustomValue): boolean;
23
23
  set(_path: Path<CustomValue> | CustomValue, _newValue: CustomValue): void;
24
24
  get(path: Path<CustomValue> | CustomValue): CustomValue;
25
- hash(): number;
26
25
  }
27
26
  export declare const NegativeOne: CustomNumber;
28
27
  export declare const PositiveOne: CustomNumber;
@@ -1,7 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Zero = exports.PositiveOne = exports.NegativeOne = exports.CustomNumber = exports.CustomNumberIterator = void 0;
4
- const hash_1 = require("../utils/hash");
5
4
  const object_value_1 = require("../utils/object-value");
6
5
  const path_1 = require("../utils/path");
7
6
  const base_1 = require("./base");
@@ -68,9 +67,6 @@ class CustomNumber extends with_intrinsics_1.CustomValueWithIntrinsics {
68
67
  }
69
68
  throw new Error(`Unknown path in number ${path.toString()}.`);
70
69
  }
71
- hash() {
72
- return (0, hash_1.getHashCode)(this.value);
73
- }
74
70
  }
75
71
  CustomNumber.intrinsics = new object_value_1.ObjectValue();
76
72
  exports.CustomNumber = CustomNumber;
@@ -30,5 +30,4 @@ export declare class CustomString extends CustomValueWithIntrinsics {
30
30
  has(path: Path<CustomValue> | CustomValue): boolean;
31
31
  set(_path: Path<CustomValue> | CustomValue, _newValue: CustomValue): void;
32
32
  get(path: Path<CustomValue> | CustomValue): CustomValue;
33
- hash(): number;
34
33
  }
@@ -1,7 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.CustomString = exports.CustomStringIterator = void 0;
4
- const hash_1 = require("../utils/hash");
5
4
  const object_value_1 = require("../utils/object-value");
6
5
  const path_1 = require("../utils/path");
7
6
  const base_1 = require("./base");
@@ -119,9 +118,6 @@ class CustomString extends with_intrinsics_1.CustomValueWithIntrinsics {
119
118
  }
120
119
  throw new Error(`Unknown path in string ${path.toString()}.`);
121
120
  }
122
- hash() {
123
- return (0, hash_1.getStringHashCode)(this.value);
124
- }
125
121
  }
126
122
  CustomString.intrinsics = new object_value_1.ObjectValue();
127
123
  exports.CustomString = CustomString;
@@ -0,0 +1 @@
1
+ export declare function deepEqual(a: any, b: any, maxDepth?: number): boolean;
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.deepEqual = void 0;
4
+ const with_intrinsics_1 = require("../types/with-intrinsics");
5
+ function equalInner(a, b, maxDepth, depth = 0) {
6
+ if (maxDepth <= depth)
7
+ return a === b;
8
+ if (a.value === b.value)
9
+ return true;
10
+ if (a && b && a instanceof with_intrinsics_1.CustomObject && b instanceof with_intrinsics_1.CustomObject) {
11
+ if (a.constructor !== b.constructor)
12
+ return false;
13
+ if (Array.isArray(a.value)) {
14
+ const length = a.value.length;
15
+ if (length !== b.value.length)
16
+ return false;
17
+ for (let i = length; i-- !== 0;)
18
+ if (!equalInner(a.value[i], b.value[i], maxDepth, depth + 1))
19
+ return false;
20
+ return true;
21
+ }
22
+ if (a.value instanceof Map) {
23
+ if (a.value.size !== b.value.size)
24
+ return false;
25
+ for (const i of a.value.keys())
26
+ if (!b.has(i) || !equalInner(a.get(i), b.get(i), maxDepth, depth + 1))
27
+ return false;
28
+ return true;
29
+ }
30
+ return false;
31
+ }
32
+ // true if both NaN, false otherwise
33
+ return Number.isNaN(a.value) && Number.isNaN(b.value);
34
+ }
35
+ function deepEqual(a, b, maxDepth = 10) {
36
+ return equalInner(a, b, maxDepth);
37
+ }
38
+ exports.deepEqual = deepEqual;
@@ -1,16 +1,8 @@
1
1
  import { CustomValue } from '../types/base';
2
- export type ObjectValueKeyPair = [CustomValue, CustomValue];
3
- export declare class ObjectValue {
4
- private data;
5
- constructor(entries?: ObjectValue | ObjectValueKeyPair[] | null);
2
+ export declare class ObjectValue extends Map<CustomValue, CustomValue> {
6
3
  get(mapKey: CustomValue): CustomValue;
7
4
  has(mapKey: CustomValue): boolean;
8
5
  set(mapKey: CustomValue, mapValue: CustomValue): this;
9
6
  delete(mapKey: CustomValue): boolean;
10
- values(): CustomValue[];
11
- keys(): CustomValue[];
12
- entries(): ObjectValueKeyPair[];
13
- get size(): number;
14
- forEach(callback: (value: CustomValue, key: CustomValue, map: ObjectValue) => any): void;
15
7
  extend(objVal: ObjectValue): this;
16
8
  }
@@ -2,62 +2,45 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ObjectValue = void 0;
4
4
  const nil_1 = require("../types/nil");
5
- class ObjectValue {
6
- constructor(entries) {
7
- if (entries == null) {
8
- this.data = new Map();
9
- }
10
- else if (entries instanceof ObjectValue) {
11
- this.data = new Map(entries.data);
12
- }
13
- else if (Array.isArray(entries)) {
14
- this.data = new Map();
15
- for (const [key, value] of entries) {
16
- this.set(key, value);
5
+ const deep_equal_1 = require("./deep-equal");
6
+ class ObjectValue extends Map {
7
+ get(mapKey) {
8
+ for (const [key, value] of this.entries()) {
9
+ if ((0, deep_equal_1.deepEqual)(key, mapKey)) {
10
+ return value;
17
11
  }
18
12
  }
19
- if (this.data == null) {
20
- throw new Error('Unknown entries type.');
21
- }
22
- }
23
- get(mapKey) {
24
- const hash = mapKey.hash();
25
- if (!this.data.has(hash))
26
- return nil_1.Void;
27
- return this.data.get(hash)[1];
13
+ return nil_1.Void;
28
14
  }
29
15
  has(mapKey) {
30
- const hash = mapKey.hash();
31
- return this.data.has(hash);
16
+ for (const key of this.keys()) {
17
+ if ((0, deep_equal_1.deepEqual)(key, mapKey)) {
18
+ return true;
19
+ }
20
+ }
21
+ return false;
32
22
  }
33
23
  set(mapKey, mapValue) {
34
- const hash = mapKey.hash();
35
- this.data.set(hash, [mapKey, mapValue]);
24
+ for (const key of this.keys()) {
25
+ if ((0, deep_equal_1.deepEqual)(key, mapKey)) {
26
+ super.set(key, mapValue);
27
+ return;
28
+ }
29
+ }
30
+ super.set(mapKey, mapValue);
36
31
  return this;
37
32
  }
38
33
  delete(mapKey) {
39
- const hash = mapKey.hash();
40
- return this.data.delete(hash);
41
- }
42
- values() {
43
- return [...this.data.values()].map(([_, v]) => v);
44
- }
45
- keys() {
46
- return [...this.data.values()].map(([k]) => k);
47
- }
48
- entries() {
49
- return [...this.data.values()];
50
- }
51
- get size() {
52
- return this.data.size;
53
- }
54
- forEach(callback) {
55
- for (const [key, value] of this.data.values()) {
56
- callback(value, key, this);
34
+ for (const key of this.keys()) {
35
+ if ((0, deep_equal_1.deepEqual)(key, mapKey)) {
36
+ super.delete(key);
37
+ return true;
38
+ }
57
39
  }
40
+ return false;
58
41
  }
59
42
  extend(objVal) {
60
- for (const [key, value] of objVal.entries()) {
43
+ for (const [key, value] of objVal) {
61
44
  this.set(key, value);
62
45
  }
63
46
  return this;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "greybel-interpreter",
3
- "version": "2.4.0",
3
+ "version": "2.5.0",
4
4
  "description": "Interpreter",
5
5
  "main": "dist/index",
6
6
  "typings": "dist/index",
@@ -49,8 +49,7 @@
49
49
  },
50
50
  "dependencies": {
51
51
  "greybel-core": "^0.9.12",
52
- "greyscript-core": "^0.9.13",
53
- "lru-cache": "^10.0.1"
52
+ "greyscript-core": "^0.9.13"
54
53
  },
55
54
  "keywords": [
56
55
  "greyscript",
@@ -1,3 +0,0 @@
1
- export declare function rotateBits(n: number): number;
2
- export declare function getHashCode(value: number, offset?: number): number;
3
- export declare const getStringHashCode: (value: string) => number;
@@ -1,41 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getStringHashCode = exports.getHashCode = exports.rotateBits = void 0;
4
- const lru_cache_1 = require("lru-cache");
5
- function rotateBits(n) {
6
- return (n >> 1) | (n << 31);
7
- }
8
- exports.rotateBits = rotateBits;
9
- function getHashCode(value, offset = 0) {
10
- let unsigned = value >>> 0;
11
- unsigned = ((unsigned >> 16) ^ unsigned) * 0x45d9f3b;
12
- unsigned = ((unsigned >> 16) ^ unsigned) * 0x45d9f3b;
13
- unsigned = (unsigned >> 16) ^ unsigned;
14
- return ((offset << 5) - offset + unsigned) | 0;
15
- }
16
- exports.getHashCode = getHashCode;
17
- exports.getStringHashCode = (function () {
18
- const cache = new lru_cache_1.LRUCache({
19
- ttl: 1000 * 60 * 5,
20
- max: 500
21
- });
22
- const generateHash = (value) => {
23
- let hash = 0;
24
- for (let i = 0; i < value.length; i++) {
25
- const chr = value.charCodeAt(i);
26
- hash = getHashCode(chr, hash);
27
- }
28
- return hash;
29
- };
30
- return (value) => {
31
- if (value.length === 0) {
32
- return 0;
33
- }
34
- if (cache.has(value)) {
35
- return cache.get(value);
36
- }
37
- const hash = generateHash(value);
38
- cache.set(value, hash);
39
- return hash;
40
- };
41
- })();