/
hash.js
92 lines (72 loc) · 1.73 KB
/
hash.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
'use strict';
var NQPObject = require('./nqp-object.js');
var Null = require('./null.js');
let HashIter = require('./hash-iter.js');
var repossession = require('./repossession.js');
var compilingSCs = repossession.compilingSCs;
class Hash extends NQPObject {
constructor() {
super();
this._SC = undefined;
this.content = new Map();
}
$$bindkey(key, value) {
this.content.set(key, value);
if (this._SC !== undefined) this.$$scwb();
return value;
}
$$atkey(key) {
return this.content.has(key) ? this.content.get(key) : Null;
}
$$existskey(key) {
return this.content.has(key);
}
$$deletekey(key) {
if (this._SC !== undefined) this.$$scwb();
this.content.delete(key);
return this;
}
$$clone() {
var clone = new Hash();
this.content.forEach(function(value, key, map) {
clone.content.set(key, value);
});
return clone;
}
$$elems() {
return this.content.size;
}
$$numify() {
return this.$$elems();
}
$$toObject() {
var ret = {};
this.content.forEach(function(value, key, map) {
ret[key] = value;
});
return ret;
}
$$toBool() {
return this.content.size == 0 ? 0 : 1;
}
$$iterator() {
return new HashIter(this);
}
//TODO: avoid copy and paste
$$scwb() {
if (compilingSCs.length == 0 || repossession.scwbDisableDepth || repossession.neverRepossess.get(this)) {
return;
}
if (compilingSCs[compilingSCs.length - 1] !== this._SC) {
var owned = this._SC.ownedObjects.get(this);
compilingSCs[compilingSCs.length - 1].repossessObject(owned === undefined ? this : owned);
}
}
$$istype(ctx, type) {
return 0;
}
$$can(obj, name) {
return 0;
}
};
module.exports = Hash;