greybel-interpreter 5.2.0 → 5.2.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.
@@ -8,23 +8,23 @@ export declare enum OpCode {
8
8
  CALL = 2,
9
9
  CALL_INTERNAL = 3,
10
10
  CALL_WITH_CONTEXT = 4,
11
- CONSTRUCT_MAP = 5,
12
- CONSTRUCT_LIST = 6,
13
- GET_VARIABLE = 7,
14
- GET_PROPERTY = 8,
15
- GET_ENVAR = 9,
16
- GET_SELF = 10,
17
- GET_OUTER = 11,
18
- GET_GLOBALS = 12,
19
- GET_LOCALS = 13,
20
- GET_SUPER = 14,
21
- GET_SUPER_PROPERTY = 15,
22
- CALL_SUPER_PROPERTY = 16,
23
- FALSIFY = 17,
24
- NEGATE = 18,
25
- NEW = 19,
26
- SLICE = 20,
27
- ASSIGN = 21,
11
+ CALL_SUPER_PROPERTY = 5,
12
+ CONSTRUCT_MAP = 6,
13
+ CONSTRUCT_LIST = 7,
14
+ GET_VARIABLE = 8,
15
+ GET_PROPERTY = 9,
16
+ GET_ENVAR = 10,
17
+ GET_SELF = 11,
18
+ GET_OUTER = 12,
19
+ GET_GLOBALS = 13,
20
+ GET_LOCALS = 14,
21
+ GET_SUPER = 15,
22
+ GET_SUPER_PROPERTY = 16,
23
+ ASSIGN = 17,
24
+ FALSIFY = 18,
25
+ NEGATE = 19,
26
+ NEW = 20,
27
+ SLICE = 21,
28
28
  PUSH = 22,
29
29
  POP = 23,
30
30
  ISA = 24,
@@ -8,23 +8,23 @@ var OpCode;
8
8
  OpCode[OpCode["CALL"] = 2] = "CALL";
9
9
  OpCode[OpCode["CALL_INTERNAL"] = 3] = "CALL_INTERNAL";
10
10
  OpCode[OpCode["CALL_WITH_CONTEXT"] = 4] = "CALL_WITH_CONTEXT";
11
- OpCode[OpCode["CONSTRUCT_MAP"] = 5] = "CONSTRUCT_MAP";
12
- OpCode[OpCode["CONSTRUCT_LIST"] = 6] = "CONSTRUCT_LIST";
13
- OpCode[OpCode["GET_VARIABLE"] = 7] = "GET_VARIABLE";
14
- OpCode[OpCode["GET_PROPERTY"] = 8] = "GET_PROPERTY";
15
- OpCode[OpCode["GET_ENVAR"] = 9] = "GET_ENVAR";
16
- OpCode[OpCode["GET_SELF"] = 10] = "GET_SELF";
17
- OpCode[OpCode["GET_OUTER"] = 11] = "GET_OUTER";
18
- OpCode[OpCode["GET_GLOBALS"] = 12] = "GET_GLOBALS";
19
- OpCode[OpCode["GET_LOCALS"] = 13] = "GET_LOCALS";
20
- OpCode[OpCode["GET_SUPER"] = 14] = "GET_SUPER";
21
- OpCode[OpCode["GET_SUPER_PROPERTY"] = 15] = "GET_SUPER_PROPERTY";
22
- OpCode[OpCode["CALL_SUPER_PROPERTY"] = 16] = "CALL_SUPER_PROPERTY";
23
- OpCode[OpCode["FALSIFY"] = 17] = "FALSIFY";
24
- OpCode[OpCode["NEGATE"] = 18] = "NEGATE";
25
- OpCode[OpCode["NEW"] = 19] = "NEW";
26
- OpCode[OpCode["SLICE"] = 20] = "SLICE";
27
- OpCode[OpCode["ASSIGN"] = 21] = "ASSIGN";
11
+ OpCode[OpCode["CALL_SUPER_PROPERTY"] = 5] = "CALL_SUPER_PROPERTY";
12
+ OpCode[OpCode["CONSTRUCT_MAP"] = 6] = "CONSTRUCT_MAP";
13
+ OpCode[OpCode["CONSTRUCT_LIST"] = 7] = "CONSTRUCT_LIST";
14
+ OpCode[OpCode["GET_VARIABLE"] = 8] = "GET_VARIABLE";
15
+ OpCode[OpCode["GET_PROPERTY"] = 9] = "GET_PROPERTY";
16
+ OpCode[OpCode["GET_ENVAR"] = 10] = "GET_ENVAR";
17
+ OpCode[OpCode["GET_SELF"] = 11] = "GET_SELF";
18
+ OpCode[OpCode["GET_OUTER"] = 12] = "GET_OUTER";
19
+ OpCode[OpCode["GET_GLOBALS"] = 13] = "GET_GLOBALS";
20
+ OpCode[OpCode["GET_LOCALS"] = 14] = "GET_LOCALS";
21
+ OpCode[OpCode["GET_SUPER"] = 15] = "GET_SUPER";
22
+ OpCode[OpCode["GET_SUPER_PROPERTY"] = 16] = "GET_SUPER_PROPERTY";
23
+ OpCode[OpCode["ASSIGN"] = 17] = "ASSIGN";
24
+ OpCode[OpCode["FALSIFY"] = 18] = "FALSIFY";
25
+ OpCode[OpCode["NEGATE"] = 19] = "NEGATE";
26
+ OpCode[OpCode["NEW"] = 20] = "NEW";
27
+ OpCode[OpCode["SLICE"] = 21] = "SLICE";
28
28
  OpCode[OpCode["PUSH"] = 22] = "PUSH";
29
29
  OpCode[OpCode["POP"] = 23] = "POP";
30
30
  OpCode[OpCode["ISA"] = 24] = "ISA";
@@ -3,8 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.CustomFunction = exports.CustomFunctionIterator = void 0;
4
4
  const instruction_1 = require("../bytecode-generator/instruction");
5
5
  const keywords_1 = require("../bytecode-generator/keywords");
6
- const hash_1 = require("../utils/hash");
7
6
  const object_value_1 = require("../utils/object-value");
7
+ const string_hash_1 = require("../utils/string-hash");
8
8
  const uuid_1 = require("../utils/uuid");
9
9
  const default_1 = require("./default");
10
10
  const nil_1 = require("./nil");
@@ -130,7 +130,7 @@ class CustomFunction extends with_intrinsics_1.CustomValueWithIntrinsics {
130
130
  };
131
131
  }
132
132
  hash() {
133
- return (0, hash_1.getStringHashCode)(this.toString());
133
+ return (0, string_hash_1.getStringHashCode)(this.toString());
134
134
  }
135
135
  }
136
136
  CustomFunction.intrinsics = new object_value_1.ObjectValue();
@@ -1,7 +1,7 @@
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");
4
+ const number_hash_1 = require("../utils/number-hash");
5
5
  const object_value_1 = require("../utils/object-value");
6
6
  const uuid_1 = require("../utils/uuid");
7
7
  const number_1 = require("./number");
@@ -130,11 +130,11 @@ class CustomList extends with_intrinsics_1.CustomObject {
130
130
  };
131
131
  }
132
132
  hash(recursionDepth = 0) {
133
- let result = (0, hash_1.getHashCode)(this.value.length);
133
+ let result = (0, number_hash_1.getNumberHashCode)(this.value.length);
134
134
  if (recursionDepth > 4)
135
135
  return result;
136
136
  this.value.forEach((value) => {
137
- result = (0, hash_1.rotateBits)(result) ^ value.hash(recursionDepth + 1);
137
+ result = (0, number_hash_1.rotateBits)(result) ^ value.hash(recursionDepth + 1);
138
138
  });
139
139
  return result;
140
140
  }
package/dist/types/map.js CHANGED
@@ -1,7 +1,7 @@
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 = void 0;
4
- const hash_1 = require("../utils/hash");
4
+ const number_hash_1 = require("../utils/number-hash");
5
5
  const object_value_1 = require("../utils/object-value");
6
6
  const uuid_1 = require("../utils/uuid");
7
7
  const nil_1 = require("./nil");
@@ -170,7 +170,7 @@ class CustomMap extends with_intrinsics_1.CustomObject {
170
170
  return isa instanceof CustomMap ? isa : null;
171
171
  }
172
172
  hash(recursionDepth = 0) {
173
- let result = (0, hash_1.getHashCode)(this.value.size);
173
+ let result = (0, number_hash_1.getNumberHashCode)(this.value.size);
174
174
  if (recursionDepth > 4)
175
175
  return result;
176
176
  this.value.forEach((value, key) => {
@@ -1,7 +1,7 @@
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");
4
+ const number_hash_1 = require("../utils/number-hash");
5
5
  const object_value_1 = require("../utils/object-value");
6
6
  const with_intrinsics_1 = require("./with-intrinsics");
7
7
  class CustomNumberIterator {
@@ -71,7 +71,7 @@ class CustomNumber extends with_intrinsics_1.CustomValueWithIntrinsics {
71
71
  };
72
72
  }
73
73
  hash() {
74
- return (0, hash_1.getHashCode)(this.value);
74
+ return (0, number_hash_1.getNumberHashCode)(this.value);
75
75
  }
76
76
  }
77
77
  CustomNumber.intrinsics = new object_value_1.ObjectValue();
@@ -2,8 +2,8 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.Outer = exports.Locals = exports.Globals = exports.Super = exports.Isa = exports.Self = exports.CustomString = exports.CustomStringIterator = void 0;
4
4
  const keywords_1 = require("../bytecode-generator/keywords");
5
- const hash_1 = require("../utils/hash");
6
5
  const object_value_1 = require("../utils/object-value");
6
+ const string_hash_1 = require("../utils/string-hash");
7
7
  const number_1 = require("./number");
8
8
  const with_intrinsics_1 = require("./with-intrinsics");
9
9
  class CustomStringIterator {
@@ -118,7 +118,7 @@ class CustomString extends with_intrinsics_1.CustomValueWithIntrinsics {
118
118
  };
119
119
  }
120
120
  hash() {
121
- return (0, hash_1.getStringHashCode)(this.value);
121
+ return (0, string_hash_1.getStringHashCode)(this.value);
122
122
  }
123
123
  }
124
124
  CustomString.intrinsics = new object_value_1.ObjectValue();
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.deepHash = void 0;
4
4
  const with_intrinsics_1 = require("../types/with-intrinsics");
5
- const hash_1 = require("./hash");
5
+ const number_hash_1 = require("./number-hash");
6
6
  function deepHash(value) {
7
7
  let result = 0;
8
8
  const stack = [];
@@ -15,7 +15,7 @@ function deepHash(value) {
15
15
  visited.add(item.id);
16
16
  if (item instanceof with_intrinsics_1.CustomObject) {
17
17
  if (Array.isArray(item.value)) {
18
- result ^= (0, hash_1.getHashCode)(item.value.length);
18
+ result ^= (0, number_hash_1.getNumberHashCode)(item.value.length);
19
19
  for (let j = item.value.length - 1; j >= 0; j--) {
20
20
  const child = item.value[j];
21
21
  if (!(child instanceof with_intrinsics_1.CustomObject) || !visited.has(child.id))
@@ -23,7 +23,7 @@ function deepHash(value) {
23
23
  }
24
24
  }
25
25
  else {
26
- result ^= (0, hash_1.getHashCode)(item.value.length);
26
+ result ^= (0, number_hash_1.getNumberHashCode)(item.value.length);
27
27
  const chunk = [];
28
28
  const entries = item.value.entries();
29
29
  for (let j = 0; j < entries.length; j++) {
@@ -40,7 +40,7 @@ function deepHash(value) {
40
40
  result ^= item.hash();
41
41
  }
42
42
  }
43
- result ^= (0, hash_1.rotateBits)(result);
43
+ result ^= (0, number_hash_1.rotateBits)(result);
44
44
  }
45
45
  return result;
46
46
  }
@@ -0,0 +1,10 @@
1
+ export declare class LRUCache<K, T> {
2
+ private _capacity;
3
+ private _head;
4
+ private _tail;
5
+ private _cache;
6
+ constructor(capacity: number);
7
+ private moveNodeToHead;
8
+ get(key: K): T | null;
9
+ set(key: K, value: T): void;
10
+ }
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.LRUCache = void 0;
4
+ const KEY_INDEX = 0;
5
+ const VALUE_INDEX = 1;
6
+ const PREV_INDEX = 2;
7
+ const NEXT_INDEX = 3;
8
+ class LRUCache {
9
+ constructor(capacity) {
10
+ if (capacity <= 0) {
11
+ throw new Error('Unexpected _capacity value. _capacity needs to be at least 1.');
12
+ }
13
+ this._capacity = capacity;
14
+ this._head = null;
15
+ this._tail = null;
16
+ this._cache = new Map();
17
+ }
18
+ moveNodeToHead(node) {
19
+ if (node[NEXT_INDEX]) {
20
+ if (node[PREV_INDEX]) {
21
+ node[NEXT_INDEX][PREV_INDEX] = node[PREV_INDEX];
22
+ }
23
+ else {
24
+ this._head = node[NEXT_INDEX];
25
+ }
26
+ }
27
+ if (node[PREV_INDEX]) {
28
+ if (node[NEXT_INDEX]) {
29
+ node[PREV_INDEX][NEXT_INDEX] = node[NEXT_INDEX];
30
+ }
31
+ else {
32
+ this._tail = node[PREV_INDEX];
33
+ }
34
+ }
35
+ const headNode = this._head;
36
+ this._head = node;
37
+ node[PREV_INDEX] = null;
38
+ node[NEXT_INDEX] = headNode;
39
+ }
40
+ get(key) {
41
+ const node = this._cache.get(key);
42
+ if (!node) {
43
+ return null;
44
+ }
45
+ this.moveNodeToHead(node);
46
+ return node[VALUE_INDEX];
47
+ }
48
+ set(key, value) {
49
+ if (this._cache.size >= this._capacity) {
50
+ const lastNode = this._tail;
51
+ this._tail = lastNode[PREV_INDEX];
52
+ this._cache.delete(lastNode[KEY_INDEX]);
53
+ }
54
+ const headNode = this._head;
55
+ this._head = [key, value, null, headNode];
56
+ if (this._tail === null) {
57
+ this._tail = this._head;
58
+ }
59
+ else {
60
+ headNode[PREV_INDEX] = this._head;
61
+ }
62
+ this._cache.set(key, this._head);
63
+ }
64
+ }
65
+ exports.LRUCache = LRUCache;
@@ -0,0 +1,2 @@
1
+ export declare function rotateBits(n: number): number;
2
+ export declare function getNumberHashCode(value: number, offset?: number): number;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getNumberHashCode = exports.rotateBits = void 0;
4
+ function rotateBits(n) {
5
+ return (n >> 1) | (n << 31);
6
+ }
7
+ exports.rotateBits = rotateBits;
8
+ function getNumberHashCode(value, offset = 0) {
9
+ let unsigned = value >>> 0;
10
+ unsigned = ((unsigned >> 16) ^ unsigned) * 0x45d9f3b;
11
+ unsigned = ((unsigned >> 16) ^ unsigned) * 0x45d9f3b;
12
+ unsigned = (unsigned >> 16) ^ unsigned;
13
+ return ((offset << 5) - offset + unsigned) | 0;
14
+ }
15
+ exports.getNumberHashCode = getNumberHashCode;
@@ -0,0 +1 @@
1
+ export declare function getStringHashCode(str: string): number;
@@ -0,0 +1,19 @@
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
+ exports.getStringHashCode = void 0;
7
+ const imurmurhash_1 = __importDefault(require("imurmurhash"));
8
+ const lru_cache_1 = require("./lru-cache");
9
+ const CACHE = new lru_cache_1.LRUCache(1000);
10
+ function getStringHashCode(str) {
11
+ const cachedHash = CACHE.get(str);
12
+ if (cachedHash) {
13
+ return cachedHash;
14
+ }
15
+ const hash = (0, imurmurhash_1.default)(str).result();
16
+ CACHE.set(str, hash);
17
+ return hash;
18
+ }
19
+ exports.getStringHashCode = getStringHashCode;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "greybel-interpreter",
3
- "version": "5.2.0",
3
+ "version": "5.2.2",
4
4
  "description": "Interpreter",
5
5
  "main": "dist/index",
6
6
  "typings": "dist/index",
@@ -28,6 +28,7 @@
28
28
  },
29
29
  "homepage": "https://github.com/ayecue/greybel-interpreter#readme",
30
30
  "devDependencies": {
31
+ "@types/imurmurhash": "^0.1.4",
31
32
  "@types/jest": "^27.0.3",
32
33
  "@types/node": "^20.14.11",
33
34
  "@types/uuid": "^8.3.3",
@@ -51,6 +52,7 @@
51
52
  "dependencies": {
52
53
  "greybel-core": "~2.2.0",
53
54
  "hyperid": "^3.2.0",
55
+ "imurmurhash": "^0.1.4",
54
56
  "non-blocking-schedule": "^0.2.0"
55
57
  },
56
58
  "keywords": [
@@ -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,35 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getStringHashCode = exports.getHashCode = exports.rotateBits = void 0;
4
- function rotateBits(n) {
5
- return (n >> 1) | (n << 31);
6
- }
7
- exports.rotateBits = rotateBits;
8
- function getHashCode(value, offset = 0) {
9
- let unsigned = value >>> 0;
10
- unsigned = ((unsigned >> 16) ^ unsigned) * 0x45d9f3b;
11
- unsigned = ((unsigned >> 16) ^ unsigned) * 0x45d9f3b;
12
- unsigned = (unsigned >> 16) ^ unsigned;
13
- return ((offset << 5) - offset + unsigned) | 0;
14
- }
15
- exports.getHashCode = getHashCode;
16
- exports.getStringHashCode = (function () {
17
- const cache = new Map();
18
- const generateHash = (s) => {
19
- for (var i = 0, h = 0; i < s.length; i++)
20
- h = (Math.imul(31, h) + s.charCodeAt(i)) | 0;
21
- return h;
22
- };
23
- return (value) => {
24
- if (value.length === 0) {
25
- return 0;
26
- }
27
- const cachedHash = cache.get(value);
28
- if (cachedHash !== undefined) {
29
- return cachedHash;
30
- }
31
- const hash = generateHash(value);
32
- cache.set(value, hash);
33
- return hash;
34
- };
35
- })();