greybel-interpreter 5.2.1 → 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.
- package/dist/bytecode-generator/instruction.d.ts +17 -17
- package/dist/bytecode-generator/instruction.js +17 -17
- package/dist/types/function.js +2 -2
- package/dist/types/list.js +3 -3
- package/dist/types/map.js +2 -2
- package/dist/types/number.js +2 -2
- package/dist/types/string.js +2 -2
- package/dist/utils/deep-hash.js +4 -4
- package/dist/utils/lru-cache.d.ts +10 -0
- package/dist/utils/lru-cache.js +65 -0
- package/dist/utils/number-hash.d.ts +2 -0
- package/dist/utils/number-hash.js +15 -0
- package/dist/utils/string-hash.d.ts +1 -0
- package/dist/utils/string-hash.js +19 -0
- package/package.json +1 -1
- package/dist/utils/hash.d.ts +0 -3
- package/dist/utils/hash.js +0 -23
|
@@ -8,23 +8,23 @@ export declare enum OpCode {
|
|
|
8
8
|
CALL = 2,
|
|
9
9
|
CALL_INTERNAL = 3,
|
|
10
10
|
CALL_WITH_CONTEXT = 4,
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
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["
|
|
12
|
-
OpCode[OpCode["
|
|
13
|
-
OpCode[OpCode["
|
|
14
|
-
OpCode[OpCode["
|
|
15
|
-
OpCode[OpCode["
|
|
16
|
-
OpCode[OpCode["
|
|
17
|
-
OpCode[OpCode["
|
|
18
|
-
OpCode[OpCode["
|
|
19
|
-
OpCode[OpCode["
|
|
20
|
-
OpCode[OpCode["
|
|
21
|
-
OpCode[OpCode["
|
|
22
|
-
OpCode[OpCode["
|
|
23
|
-
OpCode[OpCode["
|
|
24
|
-
OpCode[OpCode["
|
|
25
|
-
OpCode[OpCode["
|
|
26
|
-
OpCode[OpCode["
|
|
27
|
-
OpCode[OpCode["
|
|
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";
|
package/dist/types/function.js
CHANGED
|
@@ -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,
|
|
133
|
+
return (0, string_hash_1.getStringHashCode)(this.toString());
|
|
134
134
|
}
|
|
135
135
|
}
|
|
136
136
|
CustomFunction.intrinsics = new object_value_1.ObjectValue();
|
package/dist/types/list.js
CHANGED
|
@@ -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
|
|
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,
|
|
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,
|
|
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
|
|
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,
|
|
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) => {
|
package/dist/types/number.js
CHANGED
|
@@ -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
|
|
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,
|
|
74
|
+
return (0, number_hash_1.getNumberHashCode)(this.value);
|
|
75
75
|
}
|
|
76
76
|
}
|
|
77
77
|
CustomNumber.intrinsics = new object_value_1.ObjectValue();
|
package/dist/types/string.js
CHANGED
|
@@ -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,
|
|
121
|
+
return (0, string_hash_1.getStringHashCode)(this.value);
|
|
122
122
|
}
|
|
123
123
|
}
|
|
124
124
|
CustomString.intrinsics = new object_value_1.ObjectValue();
|
package/dist/utils/deep-hash.js
CHANGED
|
@@ -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
|
|
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,
|
|
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,
|
|
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,
|
|
43
|
+
result ^= (0, number_hash_1.rotateBits)(result);
|
|
44
44
|
}
|
|
45
45
|
return result;
|
|
46
46
|
}
|
|
@@ -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,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
package/dist/utils/hash.d.ts
DELETED
package/dist/utils/hash.js
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
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 = exports.getHashCode = exports.rotateBits = void 0;
|
|
7
|
-
const imurmurhash_1 = __importDefault(require("imurmurhash"));
|
|
8
|
-
function rotateBits(n) {
|
|
9
|
-
return (n >> 1) | (n << 31);
|
|
10
|
-
}
|
|
11
|
-
exports.rotateBits = rotateBits;
|
|
12
|
-
function getHashCode(value, offset = 0) {
|
|
13
|
-
let unsigned = value >>> 0;
|
|
14
|
-
unsigned = ((unsigned >> 16) ^ unsigned) * 0x45d9f3b;
|
|
15
|
-
unsigned = ((unsigned >> 16) ^ unsigned) * 0x45d9f3b;
|
|
16
|
-
unsigned = (unsigned >> 16) ^ unsigned;
|
|
17
|
-
return ((offset << 5) - offset + unsigned) | 0;
|
|
18
|
-
}
|
|
19
|
-
exports.getHashCode = getHashCode;
|
|
20
|
-
const getStringHashCode = (str) => {
|
|
21
|
-
return (0, imurmurhash_1.default)(str).result();
|
|
22
|
-
};
|
|
23
|
-
exports.getStringHashCode = getStringHashCode;
|