Permalink
Feb 1, 2020
Jul 21, 2020
Dec 28, 2020
Dec 28, 2020
Dec 28, 2020
May 17, 2019
Feb 1, 2020
Jan 15, 2018
Apr 3, 2019
Apr 3, 2019
Feb 1, 2020
Apr 3, 2019
Dec 13, 2018
Oct 5, 2020
Dec 10, 2018
Dec 10, 2018
Mar 7, 2018
Feb 26, 2018
Feb 1, 2020
Dec 10, 2018
Dec 10, 2018
Jan 29, 2021
Jul 30, 2020
Jul 14, 2020
Jul 31, 2018
Jul 31, 2018
Dec 10, 2018
Nov 2, 2018
Jul 31, 2018
Jun 11, 2020
Dec 10, 2018
Jun 11, 2020
Jun 11, 2020
May 27, 2018
Apr 18, 2020
Feb 1, 2020
Feb 3, 2018
Feb 1, 2020
Jul 4, 2020
Jul 4, 2020
Jul 30, 2020
Jul 30, 2020
Mar 7, 2018
Mar 7, 2018
Jul 24, 2021
Nov 9, 2015
May 17, 2019
Feb 26, 2018
Jul 21, 2020
Dec 28, 2020
Feb 26, 2018
Dec 28, 2020
Dec 13, 2018
Mar 7, 2018
Jan 29, 2021
Mar 7, 2018
Feb 26, 2018
Apr 14, 2019
Dec 13, 2018
May 17, 2019
Feb 26, 2018
Dec 28, 2020
Dec 1, 2018
Jun 29, 2020
Jun 28, 2018
May 16, 2020
May 16, 2020
Feb 1, 2020
Mar 7, 2018
Aug 2, 2018
Mar 16, 2021
Newer
100644
1242 lines (1122 sloc)
36.4 KB
3
/*
4
Implementation of Python dictionaries
5
6
We can't use Javascript's Map here, because the behaviour is not exactly the
7
same (eg with keys that are instances of classes with a __hash__ method...)
8
and because Map is much slower than regular Javascript objects.
9
10
A Python dictionary is implemented as a Javascript objects with these
11
attributes:
12
. $version: an integer with an initial value of 0, incremented at each
13
insertion
14
. $numeric_dict: for keys of type int
15
. $string_dict and $str_hash: for keys of type str
16
. $object_dict: for keys of other types
17
18
The value associated to a key in $numeric_dict and $string_dict is a pair
19
[value, rank] where "value" is the value associated with the key and "rank"
20
is the value of the dict attribute $version when the pair is inserted. This
21
is required to keep track of the insertion order, mandatory since Python 3.7.
22
23
For keys that are not str or int, their hash value is computed. Since several
33
var set_ops = ["eq", "le", "lt", "ge", "gt",
34
"sub", "rsub", "and", "or", "xor"]
35
36
// methods to compare non set-like views
37
function is_sublist(t1, t2){
38
// Return true if all elements of t1 are in t2
39
for(var i = 0, ilen = t1.length; i < ilen; i++){
40
var x = t1[i],
41
flag = false
42
for(var j = 0, jlen = t2.length; j < jlen; j++){
43
if($B.rich_comp("__eq__", x, t2[j])){
44
t2.splice(j, 1)
45
flag = true
46
break
47
}
48
}
49
if(! flag){
50
return false
51
}
52
}
53
return true
54
}
55
56
dict_view_op = {
57
__eq__: function(t1, t2){
58
return t1.length == t2.length && is_sublist(t1, t2)
59
},
60
__ne__: function(t1, t2){
61
return ! dict_view_op.__eq__(t1, t2)
62
},
63
__lt__: function(t1, t2){
64
return t1.length < t2.length && is_sublist(t1, t2)
65
},
66
__gt__: function(t1, t2){
67
return dict_view_op.__lt__(t2, t1)
68
},
69
__le__: function(t1, t2){
70
return t1.length <= t2.length && is_sublist(t1, t2)
71
},
72
__ge__: function(t1, t2){
73
return dict_view_op.__le__(t2, t1)
74
},
75
__and__: function(t1, t2){
76
var items = []
77
for(var i = 0, ilen = t1.length; i < ilen; i++){
78
var x = t1[i]
79
flag = false
80
for(var j = 0, jlen = t2.length; j < jlen; j++){
81
if($B.rich_comp("__eq__", x, t2[j])){
82
t2.splice(j, 1)
83
items.push(x)
84
break
85
}
86
}
87
}
88
return items
89
},
90
__or__: function(t1, t2){
91
var items = t1
92
for(var j = 0, jlen = t2.length; j < jlen; j++){
93
var y = t2[j],
94
flag = false
95
for(var i = 0, ilen = t1.length; i < ilen; i++){
96
if($B.rich_comp("__eq__", y, t1[i])){
97
t2.splice(j, 1)
98
flag = true
99
break
100
}
101
}
102
if(! flag){
103
items.push(y)
104
}
105
}
106
return items
107
}
124
for(var i = 0, len = set_ops.length; i < len; i++){
125
var op = "__" + set_ops[i] + "__"
126
klass[op] = (function(op){
127
return function(self, other){
128
// compare set of items to other
129
if(self.set_like){
130
return _b_.set[op](_b_.set.$factory(self),
131
_b_.set.$factory(other))
132
}else{
133
// Non-set like views can only be compared to
134
// instances of the same class
135
if(other.__class__ !== klass){
136
return false
138
var other_items = _b_.list.$factory(other)
139
return dict_view_op[op](self.items, other_items)
147
it.test_change = function(){
148
return self.dict.$version != self.dict_version
149
}
157
klass.__repr__ = function(self){
158
return klass.$infos.__name__ + '(' + _b_.repr(self.items) + ')'
159
}
160
177
dict.$to_obj = function(d){
178
// Function applied to dictionary that only have string keys,
179
// return a Javascript objects with the kays mapped to the value,
180
// excluding the insertion rank
181
var res = {}
182
for(var key in d.$string_dict){
183
res[key] = d.$string_dict[key][0]
184
}
185
return res
186
}
187
197
if(val === undefined){val = _b_.NotImplemented}
198
else if(val === null){val = $N}
203
for(var k in d.$numeric_dict){
204
items.push([parseFloat(k), d.$numeric_dict[k]])
205
}
211
for(var k in d.$object_dict){
212
d.$object_dict[k].forEach(function(item){
213
items.push(item)
214
})
215
}
217
// sort by insertion order
218
items.sort(function(a, b){
219
return a[1][1] - b[1][1]
220
})
221
items = items.map(function(item){return [item[0], item[1][0]]})
226
}else{
227
items.__class__ = _b_.tuple
228
return items.map(function(item){
229
item.__class__ = _b_.tuple; return item}
230
)
231
}
242
si(left, _l[i][0], _l[i][1])
243
if(right.$version != right_version){
244
throw _b_.RuntimeError.$factory("dict mutated during update")
245
}
246
}
249
function rank(self, hash, key){
250
// Search if object key, with hash = hash(key), is in
251
// self.$object_dict
252
var pairs = self.$object_dict[hash]
253
if(pairs !== undefined){
254
for(var i = 0, len = pairs.length; i < len; i++){
255
if($B.rich_comp("__eq__", key, pairs[i][0])){
256
return i
257
}
258
}
259
}
260
return -1
261
}
262
269
dict.__class_getitem__ = function(cls, item){
270
// PEP 585
271
// Set as a classmethod at the end of this script, after $B.set_func_names()
272
if(! Array.isArray(item)){
273
item = [item]
274
}
275
return $B.GenericAlias.$factory(cls, item)
276
}
277
279
var $ = $B.args("__contains__", 2, {self: null, key: null},
280
["self", "key"], arguments, {}, null, null),
292
return self.$numeric_dict[key] !== undefined
293
}
294
295
var hash = _b_.hash(key)
296
if(self.$str_hash[hash] !== undefined &&
309
var $ = $B.args("__eq__", 2, {self: null, arg: null},
310
["self", "arg"], arguments, {}, null, null),
319
switch(typeof arg){
320
case "string":
321
if(self.$string_dict[arg] === undefined){
341
if((ix = rank(self, hash, arg)) > -1){
342
self.$object_dict[hash].splice(ix, 1)
343
}else{
352
var $ = $B.args("__eq__", 2, {self: null, other: null},
353
["self", "other"], arguments, {}, null, null),
359
if(self.$jsobj){self = jsobj2dict(self.$jsobj)}
360
if(other.$jsobj){other = jsobj2dict(other.$jsobj)}
371
if(!$B.rich_comp("__eq__", other.$numeric_dict[k][0],
372
self.$numeric_dict[k][0])){
376
var pairs = other.$object_dict[k],
377
flag = false
378
for(var i = 0, len = pairs.length; i < len; i++){
379
if($B.rich_comp("__eq__", k, pairs[i][0]) &&
380
$B.rich_comp("__eq__", self.$numeric_dict[k],
381
pairs[i][1])){
382
flag = true
383
break
384
}
399
var pairs = self.$object_dict[hash]
400
// Get all (key, value) pairs in other that have the same hash
401
var other_pairs = []
402
if(other.$numeric_dict[hash] !== undefined){
403
other_pairs.push([hash, other.$numeric_dict[hash]])
404
}
406
other_pairs = other_pairs.concat(other.$object_dict[hash])
407
}
408
if(other_pairs.length == 0){
409
return false
410
}
411
for(var i = 0, len_i = pairs.length; i < len_i; i++){
412
var flag = false
413
var key = pairs[i][0],
415
for(var j = 0, len_j = other_pairs.length; j < len_j; j++){
416
if($B.rich_comp("__eq__", key, other_pairs[j][0]) &&
431
var $ = $B.args("__getitem__", 2, {self: null, arg: null},
432
["self", "arg"], arguments, {}, null, null),
461
break
462
}
463
464
// since the key is more complex use 'default' method of getting item
465
490
if(! ignore_missing){
491
if(self.__class__ !== dict && ! ignore_missing){
492
try{
515
if(item.length != 2){
516
throw _b_.ValueError.$factory("dictionary " +
517
`update sequence element #${i} has length 1; 2 is required`)
518
}
526
if(item[0] != 0 && item[0] != 1){
527
self.$numeric_dict[item[0]] = [item[1], self.$order++]
528
self.$version++
529
break
530
}
544
for(var key in first){
545
self.$string_dict[key] = [first[key], self.$order++]
546
}
547
return _b_.None
548
}else if(first.$jsobj){
549
self.$jsobj = {}
550
for(var attr in first.$jsobj){
551
self.$jsobj[attr] = first.$jsobj[attr]
561
arguments, {}, "first", "second")
562
var args = $.first
563
if(args.length > 1){
564
throw _b_.TypeError.$factory("dict expected at most 1 argument" +
565
", got 2")
566
}else if(args.length == 1){
567
args = args[0]
568
if(args.__class__ === dict){
569
['$string_dict', '$str_hash', '$numeric_dict', '$object_dict'].
570
forEach(function(d){
571
for(key in args[d]){self[d][key] = args[d][key]}
572
})
576
var keys = $B.$getattr(args, "keys", null)
577
if(keys !== null){
578
var gi = $B.$getattr(args, "__getitem__", null)
579
if(gi !== null){
580
// has keys and __getitem__ : it's a mapping, iterate on
581
// keys and values
582
gi = $B.$call(gi)
583
var kiter = _b_.iter($B.$call(keys)())
584
while(true){
585
try{
586
var key = _b_.next(kiter),
587
value = gi(key)
588
dict.__setitem__(self, key, value)
589
}catch(err){
590
if(err.__class__ === _b_.StopIteration){
591
break
592
}
593
throw err
594
}
595
}
596
return $N
597
}
598
}
599
if(! Array.isArray(args)){
600
args = _b_.list.$factory(args)
601
}
602
// Form "dict([[key1, value1], [key2,value2], ...])"
628
dict.__ior__ = function(self, other){
629
// PEP 584
630
dict.update(self, other)
631
return self
632
}
633
642
for(var k in self.$numeric_dict){_count++}
643
for(var k in self.$string_dict){_count++}
644
for(var hash in self.$object_dict){
645
_count += self.$object_dict[hash].length
646
}
672
dict.__or__ = function(self, other){
673
// PEP 584
674
if(! _b_.isinstance(other, dict)){
675
return _b_.NotImplemented
676
}
677
var res = dict.copy(self)
678
dict.update(res, other)
679
return res
680
}
681
682
function __newobj__(){
683
// __newobj__ is called with a generator as only argument
684
var $ = $B.args('__newobj__', 0, {}, [], arguments, {}, 'args', null),
685
args = $.args
686
var res = $B.empty_dict()
687
res.__class__ = args[0]
688
return res
689
}
690
691
dict.__reduce_ex__ = function(self, protocol){
692
return $B.fast_tuple([
693
__newobj__,
694
$B.fast_tuple([self.__class__]),
695
_b_.None,
696
_b_.None,
697
dict.items(self)])
698
}
699
721
dict.__ror__ = function(self, other){
722
// PEP 584
723
if(! _b_.isinstance(other, dict)){
724
return _b_.NotImplemented
725
}
726
var res = dict.copy(other)
727
dict.update(res, self)
728
return res
729
}
730
733
["self", "key", "value"], arguments, {}, null, null)
734
return dict.$setitem($.self, $.key, $.value)
735
}
740
// If key is a string, set:
741
// - $string_dict[key] = [value, order] where "order" is an auto-increment
742
// unique id to keep track of insertion order
743
// - $str_hash[hash(key)] to key
744
//
745
// If key is a number, set $numeric_dict[key] = value
746
//
747
// If key is another object, compute its hash value:
748
// - if the hash is a key of $str_hash, and key == $str_hash[hash],
749
// replace $string_dict[$str_hash[hash]] by value
750
// - if the hash is a key of $numeric_dict, and hash == key, replace
751
// $numeric_dict[hash] by value
752
// - if the hash is a key of $object_dict: $object_dict[hash] is a list
753
// of [k, v] pairs. If key is equal to one of the "k", replace the
754
// matching v by value. Otherwise, add [key, value] to the list
755
// - else set $object_dict[hash] = [[key, value]]
756
//
757
// In all cases, increment attribute $version, used to detect dictionary
760
// Parameter $hash is only set if this method is called by setdefault.
761
// In this case the hash of key has already been computed and we
762
// know that the key is not present in the dictionary, so it's no
763
// use computing hash(key) again, nor testing equality of keys
772
// If class attribute __init__ or __new__ are reset,
773
// the factory function has to change
774
self.$jsobj.$factory = $B.$instance_creator(self.$jsobj)
775
}
776
}else{
787
if(self.$string_dict === undefined){
788
console.log("pas de string dict", self, key, value)
789
}
790
if(self.$string_dict[key] !== undefined){
791
self.$string_dict[key][0] = value
792
}else{
793
self.$string_dict[key] = [value, self.$order++]
794
self.$str_hash[str_hash(key)] = key
795
self.$version++
796
}
799
if(self.$numeric_dict[key] !== undefined){
800
// existing key: preserve order
801
self.$numeric_dict[key][0] = value
802
}else{
803
// special case for 0 and 1 if True or False are keys
804
var done = false
805
if((key == 0 || key == 1) &&
806
self.$object_dict[key] !== undefined){
807
for(const item of self.$object_dict[key]){
808
if((key == 0 && item[0] === false) ||
809
(key == 1 && item[0] === true)){
810
// replace value
811
item[1][0] = value
812
done = true
813
}
814
}
815
}
816
if(! done){
817
// new key
818
self.$numeric_dict[key] = [value, self.$order++]
819
}
823
case "boolean":
824
// true replaces 1 and false replaces 0
825
var num = key ? 1 : 0
826
if(self.$numeric_dict[num] !== undefined){
827
var order = self.$numeric_dict[num][1] // preserve order
828
self.$numeric_dict[num] = [value, order]
829
return
830
}
831
if(self.$object_dict[num] !== undefined){
832
self.$object_dict[num].push([key, [value, self.$order++]])
833
}else{
834
self.$object_dict[num] = [[key, [value, self.$order++]]]
835
}
855
// If $setitem is called from setdefault, don't test equality of key
856
// with any object
857
if($hash){
858
if(self.$object_dict[$hash] !== undefined){
862
}
863
self.$version++
864
return $N
865
}
866
var ix = rank(self, hash, key)
867
if(ix > -1){
868
// reset value
919
var $ = $B.args("fromkeys", 3, {cls: null, keys: null, value: null},
920
["cls", "keys", "value"], arguments, {value: _b_.None}, null, null),
932
if(klass === dict){dict.$setitem(res, key, value)}
933
else{$B.$getattr(res, "__setitem__")(key, value)}
944
var $ = $B.args("get", 3, {self: null, key: null, _default: null},
945
["self", "key", "_default"], arguments, {_default: $N}, null, null)
947
try{
948
// call $getitem with ignore_missign set to true
949
return dict.$getitem($.self, $.key, true)
950
}catch(err){
951
if(_b_.isinstance(err, _b_.KeyError)){return $._default}
952
else{throw err}
953
}
954
}
955
956
var dict_items = $B.make_view("dict_items", true)
957
dict_items.$iterator = $B.make_iterator_class("dict_itemiterator")
961
var _len = arguments.length - 1,
962
_msg = "items() takes no arguments (" + _len + " given)"
965
var items = to_list(self),
966
set_like = true
967
// Check if all values are hashable
968
for(var i = 0, len = items.length; i < len; i++){
969
try{
970
_b_.hash(items[i][1])
971
}catch(err){
972
set_like = false
973
break
974
}
975
}
976
var values = to_list(self)
977
var it = dict_items.$factory(self, values, set_like)
978
it.dict_version = self.$version
988
var _len = arguments.length - 1,
989
_msg = "keys() takes no arguments (" + _len + " given)"
992
var it = dict_keys.$factory(self, to_list(self, 0), true)
993
it.dict_version = self.$version
999
var missing = {},
1000
$ = $B.args("pop", 3, {self: null, key: null, _default: null},
1001
["self", "key", "_default"], arguments, {_default: missing}, null, null),
1033
var $ = $B.args("setdefault", 3, {self: null, key: null, _default: null},
1034
["self", "key", "_default"], arguments, {_default: $N}, null, null),
1038
try{
1039
// Pass 3rd argument to dict.$getitem to avoid using __missing__
1040
// Cf. issue #1598
1041
return dict.$getitem(self, key, true)
1042
}catch(err){
1047
var hash = key.$hash
1048
key.$hash = undefined
1049
dict.$setitem(self, key, _default, hash)
1056
var $ = $B.args("update", 1, {"self": null}, ["self"], arguments,
1057
{}, "args", "kw"),
1058
self = $.self,
1059
args = $.args,
1060
kw = $.kw
1061
if(args.length > 0){
1062
var o = args[0]
1069
var _keys = _b_.list.$factory($B.$call($B.$getattr(o, "keys"))())
1070
for(var i = 0, len = _keys.length; i < len; i++){
1072
dict.$setitem(self, _keys[i], _value)
1073
}
1074
}else{
1075
var it = _b_.iter(o),
1076
i = 0
1077
while(true){
1078
try{
1079
var item = _b_.next(it)
1080
}catch(err){
1081
if(err.__class__ === _b_.StopIteration){break}
1082
throw err
1083
}
1084
try{
1085
key_value = _b_.list.$factory(item)
1086
}catch(err){
1087
throw _b_.TypeError.$factory("cannot convert dictionary" +
1088
" update sequence element #" + i + " to a sequence")
1089
}
1090
if(key_value.length !== 2){
1091
throw _b_.ValueError.$factory("dictionary update " +
1092
"sequence element #" + i + " has length " +
1093
key_value.length + "; 2 is required")
1094
}
1095
dict.$setitem(self, key_value[0], key_value[1])
1096
i++
1105
var dict_values = $B.make_view("dict_values")
1106
dict_values.$iterator = $B.make_iterator_class("dict_valueiterator")
1110
var _len = arguments.length - 1,
1111
_msg = "values() takes no arguments (" + _len + " given)"
1115
var it = dict_values.$factory(self, values, false)
1116
it.dict_version = self.$version
1122
var args = [res]
1123
for(var i = 0, len = arguments.length; i < len ; i++){
1124
args.push(arguments[i])
1125
}
1126
dict.__init__.apply(null, args)
1136
$B.empty_dict = function(){
1137
return {
1138
__class__: dict,
1139
$numeric_dict : {},
1140
$object_dict : {},
1141
$string_dict : {},
1142
$str_hash: {},
1148
// This must be done after set_func_names, otherwise dict.fromkeys doesn't
1149
// have the attribute $infos
1150
dict.fromkeys = _b_.classmethod.$factory(dict.fromkeys)
1151
1152
$B.getset_descriptor = $B.make_class("getset_descriptor",
1153
function(klass, attr){
1154
return {
1155
__class__: $B.getset_descriptor,
1157
cls: klass,
1158
attr: attr
1159
}
1160
}
1161
)
1162
1163
$B.getset_descriptor.__repr__ = $B.getset_descriptor.__str__ = function(self){
1164
return `<attribute '${self.attr}' of '${self.cls.$infos.__name__}' objects>`
1165
}
1166
1167
$B.set_func_names($B.getset_descriptor, "builtins")
1168
1173
// obj is a dictionary, with $string_dict table such that
1174
// obj.$string_dict[key] = [value, rank]
1175
// Transform it into an object with attribute $jsobj such that
1176
// res.$jsobj[key] = value
1177
var res = $B.obj_dict(dict.$to_obj(obj))
1189
throw _b_.TypeError.$factory("'mappingproxy' object does not support " +
1190
"item assignment")
1193
for(var attr in dict){
1194
if(mappingproxy[attr] !== undefined ||
1195
["__class__", "__mro__", "__new__", "__init__", "__delitem__",
1196
"clear", "fromkeys", "pop", "popitem", "setdefault",
1197
"update"].indexOf(attr) > -1){
1198
continue
1199
}
1200
if(typeof dict[attr] == "function"){
1201
mappingproxy[attr] = (function(key){
1202
return function(){
1203
return dict[key].apply(null, arguments)
1204
}
1205
})(attr)
1206
}else{
1207
mappingproxy[attr] = dict[attr]
1208
}
1209
}
1210
1234
throw _b_.AttributeError.$factory("'" + $B.class_name(obj) +
1235
"' object has no attribute '__dict__'")}