Skip to content
Permalink
Newer
Older
100644 805 lines (705 sloc) 24.3 KB
Sep 5, 2014
1
;(function($B){
2
3
eval($B.InjectBuiltins())
4
Mar 10, 2018
5
var object = _b_.object,
6
$N = _b_.None
Sep 5, 2014
7
Mar 10, 2018
8
function $err(op, other){
9
var msg = "unsupported operand type(s) for " + op +
10
": 'int' and '" + $B.get_class(other).__name__ + "'"
11
throw _b_.TypeError.$factory(msg)
Sep 5, 2014
12
}
13
14
// dictionary for built-in class 'int'
Feb 11, 2018
15
var int = {__class__: _b_.type,
Mar 10, 2018
16
__name__: "int",
17
__dir__: object.__dir__,
18
$is_class: true,
19
$native: true,
20
$descriptors: {
Mar 10, 2018
21
"numerator": true,
22
"denominator": true,
23
"imag": true,
24
"real": true
Sep 5, 2014
26
}
27
28
int.from_bytes = function() {
Mar 10, 2018
29
var $ = $B.args("from_bytes", 3,
30
{bytes:null, byteorder:null, signed:null},
Mar 10, 2018
31
["bytes", "byteorder", "signed"],
32
arguments, {signed: False}, null, null)
Sep 5, 2014
33
34
var x = $.bytes,
35
byteorder = $.byteorder,
Mar 10, 2018
36
signed = $.signed,
37
_bytes, _len
38
if(isinstance(x, [_b_.bytes, _b_.bytearray])){
39
_bytes = x.source
40
_len = x.source.length
41
}else{
42
_bytes = _b_.list.$factory(x)
43
_len = _bytes.length
Mar 10, 2018
44
for(var i = 0; i < _len; i++){
45
_b_.bytes.$factory([_bytes[i]])
46
}
Sep 5, 2014
47
}
Mar 10, 2018
49
case "big":
50
var num = _bytes[_len - 1]
51
var _mult = 256
52
for(var i = _len - 2; i >= 0; i--){
53
// For operations, use the functions that can take or return
54
// big integers
55
num = $B.add($B.mul(_mult, _bytes[i]), num)
56
_mult = $B.mul(_mult,256)
57
}
58
if(! signed){return num}
59
if(_bytes[0] < 128){return num}
60
return $B.sub(num, _mult)
61
case "little":
62
var num = _bytes[0]
63
if(num >= 128){num = num - 256}
64
var _mult = 256
65
for(var i = 1; i < _len; i++){
66
num = $B.add($B.mul(_mult, _bytes[i]), num)
67
_mult = $B.mul(_mult, 256)
68
}
69
if(! signed){return num}
70
if(_bytes[_len - 1] < 128){return num}
71
return $B.sub(num, _mult)
Sep 5, 2014
72
}
73
Mar 10, 2018
74
throw _b_.ValueError.$factory("byteorder must be either 'little' or 'big'")
Sep 5, 2014
75
}
76
77
int.to_bytes = function(){
78
var $ = $B.args("to_bytes", 3,
79
{self: null, len: null, byteorder: null},
80
["self", "len", "byteorder"],
81
arguments, {}, "args", "kw"),
82
self = $.self,
83
len = $.len,
84
byteorder = $.byteorder,
85
kwargs = $.kw
86
if(! _b_.isinstance(len, _b_.int)){
87
throw _b_.TypeError.$factory("integer argument expected, got " +
88
$B.get_class(len).__name__)
89
}
90
if(["little", "big"].indexOf(byteorder) == -1){
91
throw _b_.ValueError.$factory("byteorder must be either 'little' or 'big'")
92
}
93
var signed = kwargs.$string_dict["signed"] || false,
94
res = []
95
96
if(self < 0){
97
if(! signed){
98
throw _b_.OverflowError.$factory("can't convert negative int to unsigned")
99
}
100
self = Math.pow(256, len) + self
101
}
102
var value = self
103
while(true){
104
var quotient = Math.floor(value / 256),
105
rest = value - 256 * quotient
106
res.push(rest)
107
if(quotient == 0){
108
break
109
}
110
value = quotient
111
}
112
if(res.length > len){
113
throw _b_.OverflowError.$factory("int too big to convert")
114
}
115
if(byteorder == "big"){res = res.reverse()}
116
return {
117
__class__: _b_.bytes,
118
source: res
119
}
Sep 5, 2014
120
}
121
122
123
//int.__and__ = function(self,other){return self & other} // bitwise AND
Sep 5, 2014
124
125
int.__abs__ = function(self){return abs(self)}
127
int.__bool__ = function(self){return new Boolean(self.valueOf())}
Sep 5, 2014
128
129
int.__ceil__ = function(self){return Math.ceil(self)}
131
int.__divmod__ = function(self, other){return divmod(self, other)}
Mar 10, 2018
133
int.__eq__ = function(self, other){
Sep 5, 2014
134
// compare object "self" to class "int"
Mar 10, 2018
135
if(other === undefined){return self === int}
136
if(isinstance(other, int)){return self.valueOf() == other.valueOf()}
137
if(isinstance(other, _b_.float)){return self.valueOf() == other.valueOf()}
138
if(isinstance(other, _b_.complex)){
139
if(other.$imag != 0){return False}
140
return self.valueOf() == other.$real
Sep 5, 2014
141
}
Mar 10, 2018
143
if(hasattr(other, "__eq__")){return getattr(other, "__eq__")(self)}
Mar 10, 2018
145
return self.valueOf() === other
Sep 5, 2014
146
}
147
148
int.__float__ = function(self){
149
return new Number(self)
150
}
151
152
function preformat(self, fmt){
str
Feb 10, 2018
153
if(fmt.empty){return _b_.str.$factory(self)}
Mar 10, 2018
154
if(fmt.type && 'bcdoxXn'.indexOf(fmt.type) == -1){
155
throw _b_.ValueError.$factory("Unknown format code '" + fmt.type +
156
"' for object of type 'int'")
157
}
159
switch(fmt.type){
160
case undefined:
Mar 10, 2018
161
case "d":
162
res = self.toString()
163
break
Mar 10, 2018
164
case "b":
165
res = (fmt.alternate ? "0b" : "") + self.toString(2)
166
break
Mar 10, 2018
167
case "c":
168
res = _b_.chr(self)
169
break
Mar 10, 2018
170
case "o":
171
res = (fmt.alternate ? "0o" : "") + self.toString(8)
172
break
Mar 10, 2018
173
case "x":
174
res = (fmt.alternate ? "0x" : "") + self.toString(16)
175
break
Mar 10, 2018
176
case "X":
177
res = (fmt.alternate ? "0X" : "") + self.toString(16).toUpperCase()
178
break
Mar 10, 2018
179
case "n":
180
return self // fix me
181
}
183
if(fmt.sign !== undefined){
184
if((fmt.sign == " " || fmt.sign == "+" ) && self >= 0){
185
res = fmt.sign + res
186
}
187
}
188
return res
189
}
190
191
Mar 10, 2018
192
int.__format__ = function(self, format_spec){
193
var fmt = new $B.parse_format_spec(format_spec)
Mar 10, 2018
194
if(fmt.type && 'eEfFgG%'.indexOf(fmt.type) != -1){
195
// Call __format__ on float(self)
196
return _b_.float.__format__(self, format_spec)
Mar 10, 2018
198
fmt.align = fmt.align || ">"
199
var res = preformat(self, fmt)
200
if(fmt.comma){
Mar 10, 2018
201
var sign = res[0] == "-" ? "-" : "",
202
rest = res.substr(sign.length),
203
len = rest.length,
204
nb = Math.ceil(rest.length/3),
205
chunks = []
Mar 10, 2018
206
for(var i = 0; i < nb; i++){
207
chunks.push(rest.substring(len - 3 * i - 3, len - 3 * i))
208
}
209
chunks.reverse()
Mar 10, 2018
210
res = sign + chunks.join(",")
211
}
212
return $B.format_width(res, fmt)
Sep 5, 2014
213
}
214
215
int.__floordiv__ = function(self,other){
Mar 10, 2018
216
if(isinstance(other, int)){
217
if(other == 0){throw ZeroDivisionError.$factory("division by zero")}
218
return Math.floor(self / other)
Sep 5, 2014
219
}
Mar 10, 2018
220
if(isinstance(other, _b_.float)){
221
if(!other.valueOf()){
222
throw ZeroDivisionError.$factory("division by zero")
223
}
224
return Math.floor(self / other)
Sep 5, 2014
225
}
Mar 10, 2018
226
if(hasattr(other, "__rfloordiv__")){
227
return getattr(other, "__rfloordiv__")(self)
Sep 5, 2014
228
}
Mar 10, 2018
229
$err("//", other)
Sep 5, 2014
230
}
231
232
int.__hash__ = function(self){
Mar 23, 2018
233
if(self === undefined){
234
return int.__hashvalue__ || $B.$py_next_hash-- // for hash of int type (not instance of int)
235
}
236
return self.valueOf()
237
}
Sep 5, 2014
238
239
//int.__ior__ = function(self,other){return self | other} // bitwise OR
Sep 5, 2014
240
241
int.__index__ = function(self){return self}
Sep 5, 2014
242
243
int.__init__ = function(self,value){
Mar 10, 2018
244
if(value === undefined){value = 0}
Sep 5, 2014
245
self.toString = function(){return value}
Sep 5, 2014
247
}
248
249
int.__int__ = function(self){return self}
Sep 5, 2014
250
251
int.__invert__ = function(self){return ~self}
Sep 5, 2014
252
Mar 10, 2018
254
int.__lshift__ = function(self, other){
255
if(isinstance(other, int)){
Mar 10, 2018
256
return int.$factory($B.long_int.__lshift__($B.long_int.$factory(self),
257
$B.long_int.$factory(other)))
Mar 10, 2018
259
var rlshift = getattr(other, "__rlshift__", None)
260
if(rlshift !== None){return rlshift(self)}
261
$err("<<", other)
Mar 10, 2018
264
int.__mod__ = function(self, other) {
Sep 5, 2014
265
// can't use Javascript % because it works differently for negative numbers
Mar 10, 2018
266
if(isinstance(other,_b_.tuple) && other.length == 1){other = other[0]}
267
if(isinstance(other, [int, _b_.float, bool])){
268
if(other === false){other = 0}
269
else if(other === true){other = 1}
270
if(other == 0){throw _b_.ZeroDivisionError.$factory(
271
"integer division or modulo by zero")}
Mar 10, 2018
272
return (self % other + other) % other
Sep 5, 2014
273
}
Mar 10, 2018
274
if(hasattr(other, "__rmod__")){return getattr(other, "__rmod__")(self)}
275
$err("%", other)
Sep 5, 2014
276
}
277
278
int.__mro__ = [object]
Sep 5, 2014
279
Mar 10, 2018
280
int.__mul__ = function(self, other){
281
Sep 5, 2014
282
var val = self.valueOf()
Jan 22, 2015
283
284
// this will be quick check, so lets do it early.
Mar 10, 2018
285
if(typeof other === "string") {
Jan 22, 2015
286
return other.repeat(val)
287
}
288
Mar 10, 2018
289
if(isinstance(other, int)){
290
var res = self * other
291
if(res > $B.min_int && res < $B.max_int){return res}
292
else{
293
return int.$factory($B.long_int.__mul__($B.long_int.$factory(self),
294
$B.long_int.$factory(other)))
295
}
Mar 10, 2018
297
if(isinstance(other, _b_.float)){
298
return new Number(self * other)
Mar 10, 2018
300
if(isinstance(other, _b_.bool)){
301
if(other.valueOf()){return self}
302
return int.$factory(0)
Sep 5, 2014
303
}
Mar 10, 2018
304
if(isinstance(other, _b_.complex)){
305
return $B.make_complex(int.__mul__(self, other.$real),
306
int.__mul__(self, other.$imag))
Sep 5, 2014
307
}
Mar 10, 2018
308
if(isinstance(other, [_b_.list, _b_.tuple])){
Sep 5, 2014
309
var res = []
310
// make temporary copy of list
Mar 10, 2018
311
var $temp = other.slice(0, other.length)
312
for(var i = 0; i < val; i++){res = res.concat($temp)}
313
if(isinstance(other, _b_.tuple)){res = _b_.tuple.$factory(res)}
Sep 5, 2014
314
return res
315
}
Mar 10, 2018
316
if(hasattr(other, "__rmul__")){return getattr(other, "__rmul__")(self)}
317
$err("*", other)
Sep 5, 2014
318
}
319
320
int.__neg__ = function(self){return -self}
Sep 5, 2014
321
322
int.__new__ = function(cls){
Mar 10, 2018
323
if(cls === undefined){
324
throw _b_.TypeError.$factory("int.__new__(): not enough arguments")
325
}
Sep 5, 2014
327
}
328
329
int.__pos__ = function(self){return self}
Sep 5, 2014
330
Mar 10, 2018
331
int.__pow__ = function(self, other, z){
Sep 5, 2014
332
if(isinstance(other, int)) {
Feb 9, 2015
333
switch(other.valueOf()) {
Mar 10, 2018
334
case 0:
335
return int.$factory(1)
336
case 1:
337
return int.$factory(self.valueOf())
Feb 9, 2015
338
}
May 19, 2017
339
if(z !== undefined && z !== null){
340
// If z is provided, the algorithm is faster than computing
341
// self ** other then applying the modulo z
342
var res = (self % z + z) % z
Mar 10, 2018
343
for(var i = 1; i < other; i++){
May 19, 2017
344
res *= self
345
res = (res % z + z) % z
346
}
347
return res
348
}
Mar 10, 2018
349
var res = Math.pow(self.valueOf(), other.valueOf())
350
if(res > $B.min_int && res < $B.max_int){return res}
May 19, 2017
351
else if(res !== Infinity && !isFinite(res)){return res}
Feb 11, 2018
353
return int.$factory($B.long_int.__pow__($B.long_int.$factory(self),
354
$B.long_int.$factory(other)))
May 19, 2017
355
}
Sep 5, 2014
356
}
357
if(isinstance(other, _b_.float)) {
Mar 10, 2018
358
if(self >= 0){return new Number(Math.pow(self, other.valueOf()))}
359
else{
360
// use complex power
361
return _b_.complex.__pow__($B.make_complex(self, 0), other)
363
}else if(isinstance(other, _b_.complex)){
Mar 10, 2018
364
var preal = Math.pow(self, other.$real),
365
ln = Math.log(self)
Mar 10, 2018
366
return $B.make_complex(preal * Math.cos(ln), preal * Math.sin(ln))
Sep 5, 2014
367
}
Mar 10, 2018
368
if(hasattr(other, "__rpow__")){return getattr(other, "__rpow__")(self)}
369
$err("**", other)
Sep 5, 2014
370
}
371
372
int.__repr__ = function(self){
Mar 10, 2018
373
if(self === int){return "<class 'int'>"}
Sep 5, 2014
374
return self.toString()
375
}
376
377
// bitwise right shift
Mar 10, 2018
378
int.__rshift__ = function(self, other){
379
if(isinstance(other, int)){
Feb 11, 2018
380
return int.$factory($B.long_int.__rshift__($B.long_int.$factory(self),
381
$B.long_int.$factory(other)))
Mar 10, 2018
383
var rrshift = getattr(other, "__rrshift__", None)
384
if(rrshift !== None){return rrshift(self)}
385
$err('>>', other)
386
}
Sep 5, 2014
387
388
int.__setattr__ = function(self,attr,value){
Mar 10, 2018
389
if(typeof self == "number"){
390
if(int.$factory[attr] === undefined){
391
throw _b_.AttributeError.$factory(
392
"'int' object has no attribute '" + attr + "'")
Mar 10, 2018
394
throw _b_.AttributeError.$factory(
395
"'int' object attribute '" + attr + "' is read-only")
Sep 5, 2014
397
}
398
// subclasses of int can have attributes set
399
self[attr] = value
Sep 5, 2014
401
}
402
403
int.__str__ = int.__repr__
Sep 5, 2014
404
Mar 10, 2018
405
int.__truediv__ = function(self, other){
406
if(isinstance(other, int)){
407
if(other == 0){throw ZeroDivisionError.$factory("division by zero")}
408
if(other.__class__ === $B.long_int){
409
return new Number(self / parseInt(other.value))
410
}
411
return new Number(self / other)
Sep 5, 2014
412
}
Mar 10, 2018
413
if(isinstance(other, _b_.float)){
414
if(!other.valueOf()){
415
throw ZeroDivisionError.$factory("division by zero")
416
}
417
return new Number(self / other)
Sep 5, 2014
418
}
Mar 10, 2018
419
if(isinstance(other, _b_.complex)){
420
var cmod = other.$real * other.$real + other.$imag * other.$imag
421
if(cmod == 0){throw ZeroDivisionError.$factory("division by zero")}
422
return $B.make_complex(self * other.$real / cmod,
423
-self * other.$imag / cmod)
Sep 5, 2014
424
}
Mar 10, 2018
425
if(hasattr(other, "__rtruediv__")){
426
return getattr(other, "__rtruediv__")(self)
427
}
428
$err("/", other)
Sep 5, 2014
429
}
430
431
//int.__xor__ = function(self,other){return self ^ other} // bitwise XOR
Sep 5, 2014
432
433
int.bit_length = function(self){
Sep 5, 2014
434
s = bin(self)
Mar 10, 2018
435
s = getattr(s, "lstrip")("-0b") // remove leading zeros and minus sign
Sep 5, 2014
436
return s.length // len('100101') --> 6
437
}
438
439
// descriptors
440
int.numerator = function(self){return self}
441
int.denominator = function(self){return int.$factory(1)}
442
int.imag = function(self){return int.$factory(0)}
443
int.real = function(self){return self}
444
Mar 10, 2018
445
$B.max_int32 = (1 << 30) * 2 - 1
446
$B.min_int32 = - $B.max_int32
448
// code for operands & | ^
Mar 10, 2018
449
var $op_func = function(self, other){
450
if(isinstance(other, int)) {
451
if(other.__class__ === $B.long_int){
452
return $B.long_int.__sub__($B.long_int.$factory(self),
453
$B.long_int.$factory(other))
Mar 23, 2018
455
if(self > $B.max_int32 || self < $B.min_int32 ||
456
other > $B.max_int32 || other < $B.min_int32){
Mar 10, 2018
457
return $B.long_int.__sub__($B.long_int.$factory(self),
458
$B.long_int.$factory(other))
Mar 21, 2018
460
return self - other
Jun 7, 2015
461
}
Mar 10, 2018
462
if(isinstance(other, _b_.bool)){return self - other}
463
if(hasattr(other, "__rsub__")){return getattr(other, "__rsub__")(self)}
464
$err("-", other)
Sep 5, 2014
465
}
466
Mar 10, 2018
467
$op_func += "" // source code
468
var $ops = {"&": "and", "|": "or", "^": "xor"}
Sep 5, 2014
469
for(var $op in $ops){
Mar 10, 2018
470
var opf = $op_func.replace(/-/gm, $op)
471
opf = opf.replace(new RegExp("sub", "gm"), $ops[$op])
472
eval("int.__" + $ops[$op] + "__ = " + opf)
Sep 5, 2014
473
}
474
475
// code for + and -
Mar 10, 2018
476
var $op_func = function(self, other){
477
if(isinstance(other, int)){
478
if(typeof other == "number"){
479
var res = self.valueOf() - other.valueOf()
480
if(res > $B.min_int && res < $B.max_int){return res}
Feb 11, 2018
481
else{return $B.long_int.__sub__($B.long_int.$factory(self),
482
$B.long_int.$factory(other))}
Mar 10, 2018
483
}else if(typeof other == "boolean"){
Mar 21, 2018
484
return other ? self - 1 : self
485
}else{
Feb 11, 2018
486
return $B.long_int.__sub__($B.long_int.$factory(self),
487
$B.long_int.$factory(other))
Sep 5, 2014
489
}
Mar 10, 2018
490
if(isinstance(other, _b_.float)){
491
return new Number(self - other)
Sep 5, 2014
492
}
Mar 10, 2018
493
if(isinstance(other, _b_.complex)){
494
return $B.make_complex(self - other.$real, -other.$imag)
Sep 5, 2014
495
}
Mar 10, 2018
496
if(isinstance(other, _b_.bool)){
497
var bool_value = 0;
498
if(other.valueOf()){bool_value = 1}
499
return self - bool_value
Sep 5, 2014
500
}
Mar 10, 2018
501
if(isinstance(other, _b_.complex)){
502
return $B.make_complex(self.valueOf() - other.$real, other.$imag)
Sep 5, 2014
503
}
Mar 10, 2018
504
var rsub = $B.$getattr(other, "__rsub__", None)
505
if(rsub !== None){return rsub(self)}
506
throw $err("-", other)
Sep 5, 2014
507
}
Mar 10, 2018
508
$op_func += "" // source code
509
var $ops = {"+": "add", "-": "sub"}
Sep 5, 2014
510
for(var $op in $ops){
Mar 10, 2018
511
var opf = $op_func.replace(/-/gm, $op)
512
opf = opf.replace(new RegExp("sub", "gm"), $ops[$op])
513
eval("int.__" + $ops[$op] + "__ = " + opf)
Sep 5, 2014
514
}
515
516
// comparison methods
Mar 10, 2018
517
var $comp_func = function(self, other){
Mar 23, 2018
518
if(other.__class__ === $B.long_int){
Feb 11, 2018
519
return $B.long_int.__lt__(other, $B.long_int.$factory(self))
Mar 10, 2018
521
if(isinstance(other, int)){return self.valueOf() > other.valueOf()}
522
if(isinstance(other, _b_.float)){return self.valueOf() > other.valueOf()}
523
if(isinstance(other, _b_.bool)) {
Feb 11, 2018
524
return self.valueOf() > _b_.bool.__hash__(other)
Sep 5, 2014
525
}
Mar 10, 2018
526
if(hasattr(other, "__int__") || hasattr(other, "__index__")){
527
return int.__gt__(self, $B.$GetInt(other))
530
// See if other has the opposite operator, eg < for >
Mar 10, 2018
531
var inv_op = $B.$getattr(other, "__lt__", None)
532
if(inv_op !== None){return inv_op(self)}
534
throw _b_.TypeError.$factory(
Mar 21, 2018
535
"unorderable types: int() > " + $B.get_class(other).__name__ + "()")
Sep 5, 2014
536
}
Mar 10, 2018
537
$comp_func += "" // source code
Sep 5, 2014
539
for(var $op in $B.$comps){
Mar 10, 2018
540
eval("int.__"+$B.$comps[$op] + "__ = " +
541
$comp_func.replace(/>/gm, $op).
542
replace(/__gt__/gm,"__" + $B.$comps[$op] + "__").
543
replace(/__lt__/, "__" + $B.$inv_comps[$op] + "__"))
Sep 5, 2014
544
}
545
546
// add "reflected" methods
547
$B.make_rmethods(int)
Sep 5, 2014
548
Mar 10, 2018
549
var $valid_digits = function(base) {
550
var digits = ""
551
if(base === 0){return "0"}
552
if(base < 10){
Mar 21, 2018
553
for(var i = 0; i < base; i++){digits += String.fromCharCode(i + 48)}
Sep 5, 2014
554
return digits
555
}
556
Mar 10, 2018
557
var digits = "0123456789"
Sep 5, 2014
558
// A = 65 (10 + 55)
Mar 21, 2018
559
for (var i = 10; i < base; i++) {digits += String.fromCharCode(i + 55)}
Sep 5, 2014
560
return digits
561
}
562
563
int.$factory = function(value, base){
564
// int() with no argument returns 0
Mar 10, 2018
565
if(value === undefined){return 0}
567
// int() of an integer returns the integer if base is undefined
Mar 10, 2018
568
if(typeof value == "number" &&
569
(base === undefined || base == 10)){return parseInt(value)}
Mar 10, 2018
571
if(base !== undefined){
572
if(! isinstance(value, [_b_.str, _b_.bytes, _b_.bytearray])){
573
throw TypeError.$factory(
574
"int() can't convert non-string with explicit base")
Dec 28, 2014
575
}
576
}
577
Mar 10, 2018
578
if(isinstance(value, _b_.complex)){
579
throw TypeError.$factory("can't convert complex to int")
Dec 28, 2014
580
}
581
Mar 10, 2018
582
var $ns = $B.args("int", 2, {x:null, base:null}, ["x", "base"], arguments,
583
{"base": 10}, null, null),
584
value = $ns["x"],
585
base = $ns["base"]
Mar 10, 2018
587
if(isinstance(value, _b_.float) && base == 10){
588
if(value < $B.min_int || value > $B.max_int){
Feb 11, 2018
589
return $B.long_int.$from_float(value)
Mar 10, 2018
591
else{return value > 0 ? Math.floor(value) : Math.ceil(value)}
Sep 5, 2014
593
Mar 10, 2018
594
if(! (base >=2 && base <= 36)){
Dec 26, 2014
595
// throw error (base must be 0, or 2-36)
Mar 10, 2018
596
if(base != 0){throw _b_.ValueError.$factory("invalid base")}
Dec 26, 2014
597
}
598
Mar 10, 2018
599
if(typeof value == "number"){
Mar 10, 2018
601
if(base == 10){
602
if(value < $B.min_int || value > $B.max_int){
603
return $B.long_int.$factory(value)
604
}
Mar 10, 2018
606
}else if(value.toString().search("e") > -1){
Dec 26, 2014
607
// can't convert to another base if value is too big
Mar 10, 2018
608
throw _b_.OverflowError.$factory("can't convert to base " + base)
Dec 26, 2014
609
}else{
Mar 10, 2018
610
var res = parseInt(value, base)
611
if(value < $B.min_int || value > $B.max_int){
612
return $B.long_int.$factory(value, base)
613
}
Dec 26, 2014
615
}
616
}
Sep 5, 2014
617
Mar 10, 2018
618
if(value === true){return Number(1)}
619
if(value === false){return Number(0)}
620
if(value.__class__ === $B.long_int){
621
var z = parseInt(value.value)
Mar 10, 2018
622
if(z > $B.min_int && z < $B.max_int){return z}
623
else{return value}
624
}
Sep 5, 2014
625
Mar 10, 2018
626
base = $B.$GetInt(base)
Sep 5, 2014
627
Mar 10, 2018
628
if(isinstance(value, _b_.str)){value = value.valueOf()}
629
if(typeof value == "string") {
630
var _value = value.trim() // remove leading/trailing whitespace
631
if(_value.length == 2 && base == 0 &&
632
(_value == "0b" || _value == "0o" || _value == "0x")){
633
throw _b_.ValueError.$factory("invalid value")
634
}
635
if(_value.length >2) {
636
var _pre = _value.substr(0, 2).toUpperCase()
637
if(base == 0){
638
if(_pre == "0B"){base = 2}
639
if(_pre == "0O"){base = 8}
640
if(_pre == "0X"){base = 16}
641
}
642
if(_pre == "0B" || _pre == "0O" || _pre == "0X"){
643
_value = _value.substr(2)
644
}
645
}
646
var _digits = $valid_digits(base)
647
var _re = new RegExp("^[+-]?[" + _digits + "]+$", "i")
648
if(! _re.test(_value)){
649
throw _b_.ValueError.$factory(
650
"invalid literal for int() with base " + base + ": '" +
651
_b_.str.$factory(value) + "'")
652
}
653
if(base <= 10 && ! isFinite(value)){
654
throw _b_.ValueError.$factory(
655
"invalid literal for int() with base " + base + ": '" +
656
_b_.str.$factory(value) + "'")
657
}
658
var res = parseInt(_value, base)
659
if(res < $B.min_int || res > $B.max_int){
660
return $B.long_int.$factory(_value, base)
661
}
662
return res
Sep 5, 2014
663
}
Mar 10, 2018
665
if(isinstance(value, [_b_.bytes, _b_.bytearray])){
666
var _digits = $valid_digits(base)
Mar 10, 2018
667
for(var i = 0; i < value.source.length; i++){
668
if(_digits.indexOf(String.fromCharCode(value.source[i])) == -1){
669
throw _b_.ValueError.$factory(
670
"invalid literal for int() with base " + base + ": " +
671
_b_.repr(value))
Mar 10, 2018
674
return Number(parseInt(getattr(value, "decode")("latin-1"), base))
Sep 5, 2014
676
Mar 10, 2018
677
if(hasattr(value, "__int__")){return getattr(value, "__int__")()}
678
if(hasattr(value, "__index__")){return getattr(value, "__index__")()}
679
if(hasattr(value, "__trunc__")){
680
var res = getattr(value, "__trunc__")(),
681
int_func = _b_.getattr(res, "__int__", null)
682
if(int_func === null){
683
throw TypeError.$factory("__trunc__ returned non-Integral (type "+
Mar 21, 2018
684
$B.get_class(res).__name__ + ")")
Mar 10, 2018
686
var res = int_func()
687
if(isinstance(res, int)){return res}
Mar 10, 2018
688
throw TypeError.$factory("__trunc__ returned non-Integral (type "+
Mar 21, 2018
689
$B.get_class(res).__name__ + ")")
Mar 10, 2018
691
throw _b_.TypeError.$factory(
692
"int() argument must be a string, a bytes-like " +
693
"object or a number, not '" + $B.get_class(value).__name__ + "'")
Sep 5, 2014
694
}
695
696
$B.set_func_names(int, "builtins")
Sep 5, 2014
698
_b_.int = int
699
Feb 11, 2018
701
$B.$bool = function(obj){ // return true or false
Mar 10, 2018
702
if(obj === null || obj === undefined ){ return false}
703
switch(typeof obj){
704
case "boolean":
705
return obj
706
case "number":
707
case "string":
708
if(obj){return true}
709
return false
710
default:
711
var ce = $B.current_exception
712
try{return getattr(obj, "__bool__")()}
713
catch(err){
714
$B.current_exception = ce
Mar 21, 2018
715
try{return getattr(obj, "__len__")() > 0}
Mar 10, 2018
716
catch(err){return true}
717
}
718
}
Feb 11, 2018
719
}
720
721
var bool = {
Feb 11, 2018
722
__class__: _b_.type,
Feb 11, 2018
723
__module__: "builtins",
724
__mro__: [int, object],
725
__name__: "bool",
726
$is_class: true,
727
$native: true
728
}
Feb 11, 2018
730
bool.__add__ = function(self,other){
Mar 10, 2018
731
return (other ? 1 : 0) + (self ? 1 : 0)
Feb 11, 2018
734
bool.__and__ = function(self, other){
735
return $B.$bool(int.__and__(self, other))
Feb 11, 2018
738
bool.__eq__ = function(self,other){
739
return self ? $B.$bool(other) : !$B.$bool(other)
Feb 11, 2018
742
bool.__ne__ = function(self,other){
743
return self ? !$B.$bool(other) : $B.$bool(other)
Feb 11, 2018
746
bool.__ge__ = function(self,other){
747
return _b_.int.__ge__(bool.__hash__(self),other)
Feb 11, 2018
750
bool.__gt__ = function(self,other){
751
return _b_.int.__gt__(bool.__hash__(self),other)
Mar 10, 2018
754
bool.__hash__ = bool.__index__ = bool.__int__ = function(self){
755
if(self.valueOf()) return 1
756
return 0
757
}
758
Mar 10, 2018
759
bool.__le__ = function(self, other){return ! bool.__gt__(self, other)}
Mar 10, 2018
761
bool.__lshift__ = function(self, other){return self.valueOf() << other}
Mar 10, 2018
763
bool.__lt__ = function(self, other){return ! bool.__ge__(self, other)}
Mar 10, 2018
765
bool.__mul__ = function(self, other){
Feb 11, 2018
769
bool.__neg__ = function(self){return -$B.int_or_bool(self)}
Feb 11, 2018
771
bool.__or__ = function(self, other){
772
return $B.$bool(int.__or__(self, other))
Feb 11, 2018
775
bool.__pos__ = $B.int_or_bool
Feb 11, 2018
777
bool.__repr__ = bool.__str__ = function(self){
778
return self ? "True" : "False"
Feb 11, 2018
781
bool.__setattr__ = function(self, attr){
782
return no_set_attr(bool, attr)
Feb 11, 2018
785
bool.__sub__ = function(self,other){
786
return (self ? 1 : 0) - (other ? 1 : 0)
Feb 11, 2018
789
bool.__xor__ = function(self, other) {
790
return self.valueOf() != other.valueOf()
791
}
792
Feb 11, 2018
793
bool.$factory = function(){
794
// Calls $B.$bool, which is used inside the generated JS code and skips
795
// arguments control.
Mar 10, 2018
796
var $ = $B.args("bool", 1, {x: null}, ["x"],
797
arguments,{x: false}, null, null)
Feb 11, 2018
798
return $B.$bool($.x)
799
}
800
801
_b_.bool = bool
Feb 11, 2018
803
$B.set_func_names(bool, "builtins")
Sep 5, 2014
805
})(__BRYTHON__)