forked from skulpt/skulpt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathint.js
1180 lines (991 loc) · 35.9 KB
/
int.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* jslint nomen: true, bitwise: true */
/* global Sk: true */
/**
* @namespace Sk.builtin
*/
/**
* @constructor
* Sk.builtin.int_
*
* @description
* Constructor for Python int. If provided number is greater than integer threshold, will return a Python long instead.
*
* type int, all integers are created with this method, it is also used
* for the builtin int()
*
* Takes also implemented `__int__` and `__trunc__` methods for x into account
* and tries to use `__index__` and/or `__int__` if base is not a number
*
* @extends {Sk.builtin.numtype}
*
* @param {!(Object|number)} x Python object or Javascript number to convert to Python int
* @param {!(Object|number)=} base Optional base, can only be used when x is Sk.builtin.str
* @return {(Sk.builtin.int_|Sk.builtin.lng)} Python int (or long, if overflow)
*/
Sk.builtin.int_ = function (x, base) {
"use strict";
var val;
var ret; // return value
var magicName; // name of magic method
if (!(this instanceof Sk.builtin.int_)) {
return new Sk.builtin.int_(x, base);
}
if (this instanceof Sk.builtin.bool) {
return this;
}
if (x instanceof Sk.builtin.int_ && base === undefined) {
this.v = x.v;
return this;
}
// if base is not of type int, try calling .__index__
if(base !== undefined && !Sk.builtin.checkInt(base)) {
if (Sk.builtin.checkFloat(base)) {
throw new Sk.builtin.TypeError("integer argument expected, got " + Sk.abstr.typeName(base));
} else if (base.__index__) {
base = Sk.misceval.callsim(base.__index__, base);
} else if(base.__int__) {
base = Sk.misceval.callsim(base.__int__, base);
} else {
throw new Sk.builtin.AttributeError(Sk.abstr.typeName(base) + " instance has no attribute '__index__' or '__int__'");
}
}
if (x instanceof Sk.builtin.str) {
base = Sk.builtin.asnum$(base);
val = Sk.str2number(x.v, base, parseInt, function (x) {
return -x;
}, "int");
if ((val > Sk.builtin.int_.threshold$) || (val < -Sk.builtin.int_.threshold$)) {
// Too big for int, convert to long
return new Sk.builtin.lng(x, base);
}
this.v = val;
return this;
}
if (base !== undefined) {
throw new Sk.builtin.TypeError("int() can't convert non-string with explicit base");
}
if (x === undefined || x === Sk.builtin.none) {
x = 0;
}
/**
* try calling special methods:
* 1. __int__
* 2. __trunc__
*/
if(x !== undefined && (x.tp$getattr && x.tp$getattr("__int__"))) {
// calling a method which contains im_self and im_func
// causes skulpt to automatically map the im_self as first argument
ret = Sk.misceval.callsim(x.tp$getattr("__int__"));
magicName = "__int__";
} else if(x !== undefined && x.__int__) {
// required for internal types
// __int__ method is on prototype
ret = Sk.misceval.callsim(x.__int__, x);
magicName = "__int__";
} else if(x !== undefined && (x.tp$getattr && x.tp$getattr("__trunc__"))) {
ret = Sk.misceval.callsim(x.tp$getattr("__trunc__"));
magicName = "__trunc__";
} else if(x !== undefined && x.__trunc__) {
ret = Sk.misceval.callsim(x.__trunc__, x);
magicName = "__trunc__";
}
// check return type of magic methods
if(ret !== undefined && !Sk.builtin.checkInt(ret)) {
throw new Sk.builtin.TypeError(magicName + " returned non-Integral (type " + Sk.abstr.typeName(ret)+")");
} else if(ret !== undefined){
x = ret; // valid return value, proceed in function
}
// check type even without magic numbers
if(!Sk.builtin.checkNumber(x)) {
throw new Sk.builtin.TypeError("int() argument must be a string or a number, not '" + Sk.abstr.typeName(x) + "'");
}
x = Sk.builtin.asnum$(x);
if (x > Sk.builtin.int_.threshold$ || x < -Sk.builtin.int_.threshold$) {
return new Sk.builtin.lng(x);
}
if ((x > -1) && (x < 1)) {
x = 0;
}
this.v = parseInt(x, base);
return this;
};
Sk.builtin.int_.$shiftconsts = [0.5, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608, 16777216, 33554432, 67108864, 134217728, 268435456, 536870912, 1073741824, 2147483648, 4294967296, 8589934592, 17179869184, 34359738368, 68719476736, 137438953472, 274877906944, 549755813888, 1099511627776, 2199023255552, 4398046511104, 8796093022208, 17592186044416, 35184372088832, 70368744177664, 140737488355328, 281474976710656, 562949953421312, 1125899906842624, 2251799813685248, 4503599627370496, 9007199254740992];
Sk.abstr.setUpInheritance("int", Sk.builtin.int_, Sk.builtin.numtype);
/* NOTE: See constants used for kwargs in constants.js */
Sk.builtin.int_.prototype.nb$int_ = function () {
return this;
};
Sk.builtin.int_.prototype.nb$float_ = function() {
return new Sk.builtin.float_(this.v);
};
Sk.builtin.int_.prototype.nb$lng = function () {
return new Sk.builtin.lng(this.v);
};
/**
* Python wrapper of `__trunc__` dunder method.
*
* @instance
*/
Sk.builtin.int_.prototype.__trunc__ = new Sk.builtin.func(function(self) {
return self;
});
/**
* Python wrapper of `__index__` dunder method.
*
* @instance
*/
Sk.builtin.int_.prototype.__index__ = new Sk.builtin.func(function(self) {
return self;
});
/**
* Python wrapper of `__complex__` dunder method.
*
* @instance
*/
Sk.builtin.int_.prototype.__complex__ = new Sk.builtin.func(function(self) {
return Sk.builtin.NotImplemented.NotImplemented$;
});
/**
* Return this instance's Javascript value.
*
* Javascript function, returns Javascript object.
*
* @return {number} This instance's value.
*/
Sk.builtin.int_.prototype.tp$index = function () {
return this.v;
};
/** @override */
Sk.builtin.int_.prototype.tp$hash = function () {
//the hash of all numbers should be an int and since javascript doesn't really
//care every number can be an int.
return new Sk.builtin.int_(this.v);
};
/**
* Threshold to determine when types should be converted to long.
*
* Note: be sure to check against threshold in both positive and negative directions.
*
* @type {number}
*/
Sk.builtin.int_.threshold$ = Math.pow(2, 53) - 1;
/**
* Returns a copy of this instance.
*
* Javascript function, returns Python object.
*
* @return {Sk.builtin.int_} The copy
*/
Sk.builtin.int_.prototype.clone = function () {
return new Sk.builtin.int_(this.v);
};
/** @override */
Sk.builtin.int_.prototype.nb$add = function (other) {
var thisAsLong, thisAsFloat;
if (other instanceof Sk.builtin.int_) {
return new Sk.builtin.int_(this.v + other.v);
}
if (other instanceof Sk.builtin.lng) {
thisAsLong = new Sk.builtin.lng(this.v);
return thisAsLong.nb$add(other);
}
if (other instanceof Sk.builtin.float_) {
thisAsFloat = new Sk.builtin.float_(this.v);
return thisAsFloat.nb$add(other);
}
return Sk.builtin.NotImplemented.NotImplemented$;
};
/** @override */
Sk.builtin.int_.prototype.nb$reflected_add = function (other) {
// Should not automatically call this.nb$add, as nb$add may have
// been overridden by a subclass
return Sk.builtin.int_.prototype.nb$add.call(this, other);
};
/** @override */
Sk.builtin.int_.prototype.nb$subtract = function (other) {
var thisAsLong, thisAsFloat;
if (other instanceof Sk.builtin.int_) {
return new Sk.builtin.int_(this.v - other.v);
}
if (other instanceof Sk.builtin.lng) {
thisAsLong = new Sk.builtin.lng(this.v);
return thisAsLong.nb$subtract(other);
}
if (other instanceof Sk.builtin.float_) {
thisAsFloat = new Sk.builtin.float_(this.v);
return thisAsFloat.nb$subtract(other);
}
return Sk.builtin.NotImplemented.NotImplemented$;
};
/** @override */
Sk.builtin.int_.prototype.nb$reflected_subtract = function (other) {
// Should not automatically call this.nb$add, as nb$add may have
// been overridden by a subclass
var negative_this = this.nb$negative();
return Sk.builtin.int_.prototype.nb$add.call(negative_this, other);
};
/** @override */
Sk.builtin.int_.prototype.nb$multiply = function (other) {
var product, thisAsLong, thisAsFloat;
if (other instanceof Sk.builtin.int_) {
product = this.v * other.v;
if (product > Sk.builtin.int_.threshold$ ||
product < -Sk.builtin.int_.threshold$) {
thisAsLong = new Sk.builtin.lng(this.v);
return thisAsLong.nb$multiply(other);
} else {
return new Sk.builtin.int_(product);
}
}
if (other instanceof Sk.builtin.lng) {
thisAsLong = new Sk.builtin.lng(this.v);
return thisAsLong.nb$multiply(other);
}
if (other instanceof Sk.builtin.float_) {
thisAsFloat = new Sk.builtin.float_(this.v);
return thisAsFloat.nb$multiply(other);
}
return Sk.builtin.NotImplemented.NotImplemented$;
};
/** @override */
Sk.builtin.int_.prototype.nb$reflected_multiply = function (other) {
// Should not automatically call this.nb$multiply, as nb$multiply may have
// been overridden by a subclass
return Sk.builtin.int_.prototype.nb$multiply.call(this, other);
};
/** @override */
Sk.builtin.int_.prototype.nb$divide = function (other) {
var thisAsLong, thisAsFloat;
if (Sk.python3) {
thisAsFloat = new Sk.builtin.float_(this.v);
return thisAsFloat.nb$divide(other);
}
if (other instanceof Sk.builtin.int_) {
return this.nb$floor_divide(other);
}
if (other instanceof Sk.builtin.lng) {
thisAsLong = new Sk.builtin.lng(this.v);
return thisAsLong.nb$divide(other);
}
if (other instanceof Sk.builtin.float_) {
thisAsFloat = new Sk.builtin.float_(this.v);
return thisAsFloat.nb$divide(other);
}
return Sk.builtin.NotImplemented.NotImplemented$;
};
/** @override */
Sk.builtin.int_.prototype.nb$reflected_divide = function (other) {
return this.nb$reflected_floor_divide(other);
};
/** @override */
Sk.builtin.int_.prototype.nb$floor_divide = function (other) {
var thisAsLong, thisAsFloat;
if (other instanceof Sk.builtin.int_) {
if (other.v === 0) {
throw new Sk.builtin.ZeroDivisionError("integer division or modulo by zero");
}
return new Sk.builtin.int_(Math.floor(this.v / other.v));
}
if (other instanceof Sk.builtin.lng) {
thisAsLong = new Sk.builtin.lng(this.v);
return thisAsLong.nb$floor_divide(other);
}
if (other instanceof Sk.builtin.float_) {
thisAsFloat = new Sk.builtin.float_(this.v);
return thisAsFloat.nb$floor_divide(other);
}
return Sk.builtin.NotImplemented.NotImplemented$;
};
/** @override */
Sk.builtin.int_.prototype.nb$reflected_floor_divide = function (other) {
if (other instanceof Sk.builtin.int_) {
return other.nb$divide(this);
}
return Sk.builtin.NotImplemented.NotImplemented$;
};
/** @override */
Sk.builtin.int_.prototype.nb$remainder = function (other) {
var thisAsLong, thisAsFloat;
var tmp;
var divResult;
if (other instanceof Sk.builtin.int_) {
// Javacript logic on negatives doesn't work for Python... do this instead
divResult = Sk.abstr.numberBinOp(this, other, "FloorDiv");
tmp = Sk.abstr.numberBinOp(divResult, other, "Mult");
tmp = Sk.abstr.numberBinOp(this, tmp, "Sub");
tmp = tmp.v;
if (other.v < 0 && tmp === 0) {
tmp = -0.0; // otherwise the sign gets lost by javascript modulo
} else if (tmp === 0 && Infinity/tmp === -Infinity) {
tmp = 0.0;
}
return new Sk.builtin.int_(tmp);
}
if (other instanceof Sk.builtin.lng) {
thisAsLong = new Sk.builtin.lng(this.v);
return thisAsLong.nb$remainder(other);
}
if (other instanceof Sk.builtin.float_) {
thisAsFloat = new Sk.builtin.float_(this.v);
return thisAsFloat.nb$remainder(other);
}
return Sk.builtin.NotImplemented.NotImplemented$;
};
/** @override */
Sk.builtin.int_.prototype.nb$reflected_remainder = function (other) {
if (other instanceof Sk.builtin.int_) {
return other.nb$remainder(this);
}
return Sk.builtin.NotImplemented.NotImplemented$;
};
/** @override */
Sk.builtin.int_.prototype.nb$divmod = function (other) {
var thisAsLong, thisAsFloat;
if (other instanceof Sk.builtin.int_) {
return new Sk.builtin.tuple([
this.nb$floor_divide(other),
this.nb$remainder(other)
]);
}
if (other instanceof Sk.builtin.lng) {
thisAsLong = new Sk.builtin.lng(this.v);
return thisAsLong.nb$divmod(other);
}
if (other instanceof Sk.builtin.float_) {
thisAsFloat = new Sk.builtin.float_(this.v);
return thisAsFloat.nb$divmod(other);
}
return Sk.builtin.NotImplemented.NotImplemented$;
};
/** @override */
Sk.builtin.int_.prototype.nb$reflected_divmod = function (other) {
if (other instanceof Sk.builtin.int_) {
return new Sk.builtin.tuple([
other.nb$floor_divide(this),
other.nb$remainder(this)
]);
}
return Sk.builtin.NotImplemented.NotImplemented$;
};
/** @override */
Sk.builtin.int_.prototype.nb$power = function (other, mod) {
var power, ret, thisAsLong, thisAsFloat;
if (other instanceof Sk.builtin.int_ && (mod === undefined || mod instanceof Sk.builtin.int_)) {
power = Math.pow(this.v, other.v);
if (power > Sk.builtin.int_.threshold$ ||
power < -Sk.builtin.int_.threshold$) {
thisAsLong = new Sk.builtin.lng(this.v);
ret = thisAsLong.nb$power(other, mod);
} else if (other.v < 0) {
ret = new Sk.builtin.float_(power);
} else {
ret = new Sk.builtin.int_(power);
}
if (mod !== undefined) {
if (other.v < 0) {
throw new Sk.builtin.TypeError("pow() 2nd argument cannot be negative when 3rd argument specified");
}
return ret.nb$remainder(mod);
} else {
return ret;
}
}
if (other instanceof Sk.builtin.lng) {
thisAsLong = new Sk.builtin.lng(this.v);
return thisAsLong.nb$power(other);
}
if (other instanceof Sk.builtin.float_) {
thisAsFloat = new Sk.builtin.float_(this.v);
return thisAsFloat.nb$power(other);
}
return Sk.builtin.NotImplemented.NotImplemented$;
};
/** @override */
Sk.builtin.int_.prototype.nb$reflected_power = function (other, mod) {
if (other instanceof Sk.builtin.int_) {
return other.nb$power(this, mod);
}
return Sk.builtin.NotImplemented.NotImplemented$;
};
/** @override */
Sk.builtin.int_.prototype.nb$abs = function () {
return new Sk.builtin.int_(Math.abs(this.v));
};
/**
* Compute the bitwise AND of this instance and a Python object (i.e. this & other).
*
* Returns NotImplemented if bitwise AND operation between int and other type is unsupported.
*
* Javscript function, returns Python object.
*
* @param {!Sk.builtin.object} other The Python object to AND with this one
* @return {(Sk.builtin.int_|Sk.builtin.lng|Sk.builtin.NotImplemented)} The result of the conjunction
*/
Sk.builtin.int_.prototype.nb$and = function (other) {
var thisAsLong, thisAsFloat;
if (other instanceof Sk.builtin.int_) {
var tmp;
other = Sk.builtin.asnum$(other);
tmp = this.v & other;
if ((tmp !== undefined) && (tmp < 0)) {
tmp = tmp + 4294967296; // convert back to unsigned
}
if (tmp !== undefined) {
return new Sk.builtin.int_(tmp);
}
}
if (other instanceof Sk.builtin.lng) {
thisAsLong = new Sk.builtin.lng(this.v);
return thisAsLong.nb$and(other);
}
return Sk.builtin.NotImplemented.NotImplemented$;
};
Sk.builtin.int_.prototype.nb$reflected_and = Sk.builtin.int_.prototype.nb$and;
/**
* Compute the bitwise OR of this instance and a Python object (i.e. this | other).
*
* Returns NotImplemented if bitwise OR operation between int and other type is unsupported.
*
* Javscript function, returns Python object.
*
* @param {!Sk.builtin.object} other The Python object to OR with this one
* @return {(Sk.builtin.int_|Sk.builtin.lng|Sk.builtin.NotImplemented)} The result of the disjunction
*/
Sk.builtin.int_.prototype.nb$or = function (other) {
var thisAsLong;
if (other instanceof Sk.builtin.int_) {
var tmp;
other = Sk.builtin.asnum$(other);
tmp = this.v | other;
if ((tmp !== undefined) && (tmp < 0)) {
tmp = tmp + 4294967296; // convert back to unsigned
}
if (tmp !== undefined) {
return new Sk.builtin.int_(tmp);
}
}
if (other instanceof Sk.builtin.lng) {
thisAsLong = new Sk.builtin.lng(this.v);
return thisAsLong.nb$and(other);
}
return Sk.builtin.NotImplemented.NotImplemented$;
};
Sk.builtin.int_.prototype.nb$reflected_or = Sk.builtin.int_.prototype.nb$or;
/**
* Compute the bitwise XOR of this instance and a Python object (i.e. this ^ other).
*
* Returns NotImplemented if bitwise XOR operation between int and other type is unsupported.
*
* Javscript function, returns Python object.
*
* @param {!Sk.builtin.object} other The Python object to XOR with this one
* @return {(Sk.builtin.int_|Sk.builtin.lng|Sk.builtin.NotImplemented)} The result of the exclusive disjunction
*/
Sk.builtin.int_.prototype.nb$xor = function (other) {
var thisAsLong;
if (other instanceof Sk.builtin.int_) {
var tmp;
other = Sk.builtin.asnum$(other);
tmp = this.v ^ other;
if ((tmp !== undefined) && (tmp < 0)) {
tmp = tmp + 4294967296; // convert back to unsigned
}
if (tmp !== undefined) {
return new Sk.builtin.int_(tmp);
}
}
if (other instanceof Sk.builtin.lng) {
thisAsLong = new Sk.builtin.lng(this.v);
return thisAsLong.nb$xor(other);
}
return Sk.builtin.NotImplemented.NotImplemented$;
};
Sk.builtin.int_.prototype.nb$reflected_xor = Sk.builtin.int_.prototype.nb$xor;
/**
* Compute the bitwise left shift of this instance by a Python object (i.e. this << other).
*
* Returns NotImplemented if bitwise left shift operation between int and other type is unsupported.
*
* Javscript function, returns Python object.
*
* @param {!Sk.builtin.object} other The Python object by which to left shift
* @return {(Sk.builtin.int_|Sk.builtin.lng|Sk.builtin.NotImplemented)} The result of the left shift
*/
Sk.builtin.int_.prototype.nb$lshift = function (other) {
var thisAsLong;
if (this.v === 0) {
return this;
}
if (other instanceof Sk.builtin.int_) {
var tmp;
var shift = Sk.builtin.asnum$(other);
if (shift !== undefined) {
if (shift < 0) {
throw new Sk.builtin.ValueError("negative shift count");
}
if (shift > 53) {
return new Sk.builtin.lng(this.v).nb$lshift(new Sk.builtin.int_(shift));
}
tmp = this.v * 2 * Sk.builtin.int_.$shiftconsts[shift];
if (tmp > Sk.builtin.int_.threshold$ || tmp < -Sk.builtin.int_.threshold$) {
// Fail, recompute with longs
return new Sk.builtin.lng(tmp);
}
}
if (tmp !== undefined) {
tmp = /** @type {number} */ (tmp);
return new Sk.builtin.int_(tmp);
}
}
if (other instanceof Sk.builtin.lng) {
thisAsLong = new Sk.builtin.lng(this.v);
return thisAsLong.nb$lshift(other);
}
return Sk.builtin.NotImplemented.NotImplemented$;
};
Sk.builtin.int_.prototype.nb$reflected_lshift = function (other) {
if (other instanceof Sk.builtin.int_) {
return other.nb$lshift(this);
}
return Sk.builtin.NotImplemented.NotImplemented$;
};
/**
* Compute the bitwise right shift of this instance by a Python object (i.e. this >> other).
*
* Returns NotImplemented if bitwise right shift operation between int and other type is unsupported.
*
* Javscript function, returns Python object.
*
* @param {!Sk.builtin.object} other The Python object by which to right shift
* @return {(Sk.builtin.int_|Sk.builtin.lng|Sk.builtin.NotImplemented)} The result of the right shift
*/
Sk.builtin.int_.prototype.nb$rshift = function (other) {
var thisAsLong;
if (other instanceof Sk.builtin.int_) {
var tmp;
var shift = Sk.builtin.asnum$(other);
if (shift !== undefined) {
if (shift < 0) {
throw new Sk.builtin.ValueError("negative shift count");
}
tmp = this.v >> shift;
if ((this.v > 0) && (tmp < 0)) {
// Fix incorrect sign extension
tmp = tmp & (Math.pow(2, 32 - shift) - 1);
}
}
if (tmp !== undefined) {
tmp = /** @type {number} */ (tmp);
return new Sk.builtin.int_(tmp);
}
}
if (other instanceof Sk.builtin.lng) {
thisAsLong = new Sk.builtin.lng(this.v);
return thisAsLong.nb$rshift(other);
}
return Sk.builtin.NotImplemented.NotImplemented$;
};
Sk.builtin.int_.prototype.nb$reflected_rshift = function (other) {
if (other instanceof Sk.builtin.int_) {
return other.nb$rshift(this);
}
return Sk.builtin.NotImplemented.NotImplemented$;
};
/**
* Compute the bitwise inverse of this instance (i.e. ~this).
*
* Javscript function, returns Python object.
*
* @return {Sk.builtin.int_} The result of the inversion
*/
Sk.builtin.int_.prototype.nb$invert = function () {
return new Sk.builtin.int_(~this.v);
};
/** @override */
Sk.builtin.int_.prototype.nb$inplace_add = Sk.builtin.int_.prototype.nb$add;
/** @override */
Sk.builtin.int_.prototype.nb$inplace_subtract = Sk.builtin.int_.prototype.nb$subtract;
/** @override */
Sk.builtin.int_.prototype.nb$inplace_multiply = Sk.builtin.int_.prototype.nb$multiply;
/** @override */
Sk.builtin.int_.prototype.nb$inplace_divide = Sk.builtin.int_.prototype.nb$divide;
/** @override */
Sk.builtin.int_.prototype.nb$inplace_remainder = Sk.builtin.int_.prototype.nb$remainder;
/** @override */
Sk.builtin.int_.prototype.nb$inplace_floor_divide = Sk.builtin.int_.prototype.nb$floor_divide;
/** @override */
Sk.builtin.int_.prototype.nb$inplace_power = Sk.builtin.int_.prototype.nb$power;
/**
* @function
* @name nb$inplace_and
* @memberOf Sk.builtin.int_.prototype
* @description
* Compute the bitwise AND of this instance and a Python object (i.e. this &= other).
*
* Returns NotImplemented if inplace bitwise AND operation between int and other type is unsupported.
*
* Javscript function, returns Python object.
*
* @param {!Sk.builtin.object} other The Python object to AND with this one
* @return {(Sk.builtin.int_|Sk.builtin.lng|Sk.builtin.NotImplemented)} The result of the conjunction
*/
Sk.builtin.int_.prototype.nb$inplace_and = Sk.builtin.int_.prototype.nb$and;
/**
* @function
* @name nb$inplace_or
* @memberOf Sk.builtin.int_.prototype
* @description
* Compute the bitwise OR of this instance and a Python object (i.e. this |= other).
*
* Returns NotImplemented if inplace bitwise OR operation between int and other type is unsupported.
*
* Javscript function, returns Python object.
*
* @param {!Sk.builtin.object} other The Python object to OR with this one
* @return {(Sk.builtin.int_|Sk.builtin.lng|Sk.builtin.NotImplemented)} The result of the disjunction
*/
Sk.builtin.int_.prototype.nb$inplace_or = Sk.builtin.int_.prototype.nb$or;
/**
* @function
* @name nb$inplace_xor
* @memberOf Sk.builtin.int_.prototype
* @description
* Compute the bitwise XOR of this instance and a Python object (i.e. this ^= other).
*
* Returns NotImplemented if inplace bitwise XOR operation between int and other type is unsupported.
*
* Javscript function, returns Python object.
*
* @param {!Sk.builtin.object} other The Python object to XOR with this one
* @return {(Sk.builtin.int_|Sk.builtin.lng|Sk.builtin.NotImplemented)} The result of the exclusive disjunction
*/
Sk.builtin.int_.prototype.nb$inplace_xor = Sk.builtin.int_.prototype.nb$xor;
/**
* @function
* @name nb$inplace_lshift
* @memberOf Sk.builtin.int_.prototype
* @description
* Compute the bitwise left shift of this instance by a Python object (i.e. this <<= other).
*
* Returns NotImplemented if inplace bitwise left shift operation between int and other type is unsupported.
*
* Javscript function, returns Python object.
*
* @param {!Sk.builtin.object} other The Python object by which to left shift
* @return {(Sk.builtin.int_|Sk.builtin.lng|Sk.builtin.NotImplemented)} The result of the left shift
*/
Sk.builtin.int_.prototype.nb$inplace_lshift = Sk.builtin.int_.prototype.nb$lshift;
/**
* @function
* @name nb$inplace_rshift
* @memberOf Sk.builtin.int_.prototype
* @description
* Compute the bitwise right shift of this instance by a Python object (i.e. this >>= other).
*
* Returns NotImplemented if inplace bitwise right shift operation between int and other type is unsupported.
*
* Javscript function, returns Python object.
*
* @param {!Sk.builtin.object} other The Python object by which to right shift
* @return {(Sk.builtin.int_|Sk.builtin.lng|Sk.builtin.NotImplemented)} The result of the right shift
*/
Sk.builtin.int_.prototype.nb$inplace_rshift = Sk.builtin.int_.prototype.nb$rshift;
/**
* @override
*
* @return {Sk.builtin.int_} A copy of this instance with the value negated.
*/
Sk.builtin.int_.prototype.nb$negative = function () {
return new Sk.builtin.int_(-this.v);
};
/** @override */
Sk.builtin.int_.prototype.nb$positive = function () {
return this.clone();
};
/** @override */
Sk.builtin.int_.prototype.nb$nonzero = function () {
return this.v !== 0;
};
/** @override */
Sk.builtin.int_.prototype.nb$isnegative = function () {
return this.v < 0;
};
/** @override */
Sk.builtin.int_.prototype.nb$ispositive = function () {
return this.v >= 0;
};
/**
* Compare this instance's value to another Python object's value.
*
* Returns NotImplemented if comparison between int and other type is unsupported.
*
* Javscript function, returns Javascript object or Sk.builtin.NotImplemented.
*
* @return {(number|Sk.builtin.NotImplemented)} negative if this < other, zero if this == other, positive if this > other
*/
Sk.builtin.int_.prototype.numberCompare = function (other) {
if (other instanceof Sk.builtin.int_) {
return this.v - other.v;
}
if (other instanceof Sk.builtin.lng) {
return -other.longCompare(this);
}
if (other instanceof Sk.builtin.float_) {
return -other.numberCompare(this);
}
return Sk.builtin.NotImplemented.NotImplemented$;
};
// Despite what jshint may want us to do, these two functions need to remain
// as == and != Unless you modify the logic of numberCompare do not change
// these.
/** @override */
Sk.builtin.int_.prototype.ob$eq = function (other) {
if (other instanceof Sk.builtin.int_ || other instanceof Sk.builtin.lng ||
other instanceof Sk.builtin.float_) {
return new Sk.builtin.bool(this.numberCompare(other) == 0); //jshint ignore:line
} else if (other instanceof Sk.builtin.none) {
return Sk.builtin.bool.false$;
} else {
return Sk.builtin.NotImplemented.NotImplemented$;
}
};
/** @override */
Sk.builtin.int_.prototype.ob$ne = function (other) {
if (other instanceof Sk.builtin.int_ || other instanceof Sk.builtin.lng ||
other instanceof Sk.builtin.float_) {
return new Sk.builtin.bool(this.numberCompare(other) != 0); //jshint ignore:line
} else if (other instanceof Sk.builtin.none) {
return Sk.builtin.bool.true$;
} else {
return Sk.builtin.NotImplemented.NotImplemented$;
}
};
/** @override */
Sk.builtin.int_.prototype.ob$lt = function (other) {
if (other instanceof Sk.builtin.int_ || other instanceof Sk.builtin.lng ||
other instanceof Sk.builtin.float_) {
return new Sk.builtin.bool(this.numberCompare(other) < 0);
} else {
return Sk.builtin.NotImplemented.NotImplemented$;
}
};
/** @override */
Sk.builtin.int_.prototype.ob$le = function (other) {
if (other instanceof Sk.builtin.int_ || other instanceof Sk.builtin.lng ||
other instanceof Sk.builtin.float_) {
return new Sk.builtin.bool(this.numberCompare(other) <= 0);
} else {
return Sk.builtin.NotImplemented.NotImplemented$;
}
};
/** @override */
Sk.builtin.int_.prototype.ob$gt = function (other) {
if (other instanceof Sk.builtin.int_ || other instanceof Sk.builtin.lng ||
other instanceof Sk.builtin.float_) {
return new Sk.builtin.bool(this.numberCompare(other) > 0);
} else {
return Sk.builtin.NotImplemented.NotImplemented$;
}
};
/** @override */
Sk.builtin.int_.prototype.ob$ge = function (other) {
if (other instanceof Sk.builtin.int_ || other instanceof Sk.builtin.lng ||
other instanceof Sk.builtin.float_) {
return new Sk.builtin.bool(this.numberCompare(other) >= 0);
} else {
return Sk.builtin.NotImplemented.NotImplemented$;
}
};
/**
* Round this instance to a given number of digits, or zero if omitted.
*
* Implements `__round__` dunder method.
*
* Javascript function, returns Python object.
*
* @param {Sk.builtin.int_} self This instance.
* @param {Object|number=} ndigits The number of digits after the decimal point to which to round.
* @return {Sk.builtin.int_} The rounded integer.
*/
Sk.builtin.int_.prototype.round$ = function (self, ndigits) {
Sk.builtin.pyCheckArgs("__round__", arguments, 1, 2);
var result, multiplier, number, num10, rounded, bankRound, ndigs;
if ((ndigits !== undefined) && !Sk.misceval.isIndex(ndigits)) {
throw new Sk.builtin.TypeError("'" + Sk.abstr.typeName(ndigits) + "' object cannot be interpreted as an index");
}
number = Sk.builtin.asnum$(self);
if (ndigits === undefined) {
ndigs = 0;
} else {
ndigs = Sk.misceval.asIndex(ndigits);
}
if (Sk.python3) {
num10 = number * Math.pow(10, ndigs);
rounded = Math.round(num10);
bankRound = (((((num10>0)?num10:(-num10))%1)===0.5)?(((0===(rounded%2)))?rounded:(rounded-1)):rounded);
result = bankRound / Math.pow(10, ndigs);
return new Sk.builtin.int_(result);
} else {
multiplier = Math.pow(10, ndigs);
result = Math.round(number * multiplier) / multiplier;
return new Sk.builtin.int_(result);
}
};
Sk.builtin.int_.prototype.__format__= function (obj, format_spec) {