Skip to content
Permalink
Newer
Older
100644 795 lines (693 sloc) 24 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
return self.toString()
Mar 10, 2018
163
case "b":
164
return (fmt.alternate ? "0b" : "") + self.toString(2)
165
case "c":
166
return _b_.chr(self)
Mar 10, 2018
167
case "o":
168
return (fmt.alternate ? "0o" : "") + self.toString(8)
169
case "x":
170
return (fmt.alternate ? "0x" : "") + self.toString(16)
171
case "X":
172
return (fmt.alternate ? "0X" : "") + self.toString(16).toUpperCase()
173
case "n":
174
return self // fix me
175
}
177
return res
178
}
179
180
Mar 10, 2018
181
int.__format__ = function(self, format_spec){
182
var fmt = new $B.parse_format_spec(format_spec)
Mar 10, 2018
183
if(fmt.type && 'eEfFgG%'.indexOf(fmt.type) != -1){
184
// Call __format__ on float(self)
185
return _b_.float.__format__(self, format_spec)
Mar 10, 2018
187
fmt.align = fmt.align || ">"
188
var res = preformat(self, fmt)
189
if(fmt.comma){
Mar 10, 2018
190
var sign = res[0] == "-" ? "-" : "",
191
rest = res.substr(sign.length),
192
len = rest.length,
193
nb = Math.ceil(rest.length/3),
194
chunks = []
Mar 10, 2018
195
for(var i = 0; i < nb; i++){
196
chunks.push(rest.substring(len - 3 * i - 3, len - 3 * i))
197
}
198
chunks.reverse()
Mar 10, 2018
199
res = sign + chunks.join(",")
200
}
201
return $B.format_width(res, fmt)
Sep 5, 2014
202
}
203
204
int.__floordiv__ = function(self,other){
Mar 10, 2018
205
if(isinstance(other, int)){
206
if(other == 0){throw ZeroDivisionError.$factory("division by zero")}
207
return Math.floor(self / other)
Sep 5, 2014
208
}
Mar 10, 2018
209
if(isinstance(other, _b_.float)){
210
if(!other.valueOf()){
211
throw ZeroDivisionError.$factory("division by zero")
212
}
213
return Math.floor(self / other)
Sep 5, 2014
214
}
Mar 10, 2018
215
if(hasattr(other, "__rfloordiv__")){
216
return getattr(other, "__rfloordiv__")(self)
Sep 5, 2014
217
}
Mar 10, 2018
218
$err("//", other)
Sep 5, 2014
219
}
220
221
int.__hash__ = function(self){
Mar 23, 2018
222
if(self === undefined){
223
return int.__hashvalue__ || $B.$py_next_hash-- // for hash of int type (not instance of int)
224
}
225
return self.valueOf()
226
}
Sep 5, 2014
227
228
//int.__ior__ = function(self,other){return self | other} // bitwise OR
Sep 5, 2014
229
230
int.__index__ = function(self){return self}
Sep 5, 2014
231
232
int.__init__ = function(self,value){
Mar 10, 2018
233
if(value === undefined){value = 0}
Sep 5, 2014
234
self.toString = function(){return value}
Sep 5, 2014
236
}
237
238
int.__int__ = function(self){return self}
Sep 5, 2014
239
240
int.__invert__ = function(self){return ~self}
Sep 5, 2014
241
Mar 10, 2018
243
int.__lshift__ = function(self, other){
244
if(isinstance(other, int)){
Mar 10, 2018
245
return int.$factory($B.long_int.__lshift__($B.long_int.$factory(self),
246
$B.long_int.$factory(other)))
Mar 10, 2018
248
var rlshift = getattr(other, "__rlshift__", None)
249
if(rlshift !== None){return rlshift(self)}
250
$err("<<", other)
Mar 10, 2018
253
int.__mod__ = function(self, other) {
Sep 5, 2014
254
// can't use Javascript % because it works differently for negative numbers
Mar 10, 2018
255
if(isinstance(other,_b_.tuple) && other.length == 1){other = other[0]}
256
if(isinstance(other, [int, _b_.float, bool])){
257
if(other === false){other = 0}
258
else if(other === true){other = 1}
259
if(other == 0){throw _b_.ZeroDivisionError.$factory(
260
"integer division or modulo by zero")}
Mar 10, 2018
261
return (self % other + other) % other
Sep 5, 2014
262
}
Mar 10, 2018
263
if(hasattr(other, "__rmod__")){return getattr(other, "__rmod__")(self)}
264
$err("%", other)
Sep 5, 2014
265
}
266
267
int.__mro__ = [object]
Sep 5, 2014
268
Mar 10, 2018
269
int.__mul__ = function(self, other){
270
Sep 5, 2014
271
var val = self.valueOf()
Jan 22, 2015
272
273
// this will be quick check, so lets do it early.
Mar 10, 2018
274
if(typeof other === "string") {
Jan 22, 2015
275
return other.repeat(val)
276
}
277
Mar 10, 2018
278
if(isinstance(other, int)){
279
var res = self * other
280
if(res > $B.min_int && res < $B.max_int){return res}
281
else{
282
return int.$factory($B.long_int.__mul__($B.long_int.$factory(self),
283
$B.long_int.$factory(other)))
284
}
Mar 10, 2018
286
if(isinstance(other, _b_.float)){
287
return new Number(self * other)
Mar 10, 2018
289
if(isinstance(other, _b_.bool)){
290
if(other.valueOf()){return self}
291
return int.$factory(0)
Sep 5, 2014
292
}
Mar 10, 2018
293
if(isinstance(other, _b_.complex)){
294
return $B.make_complex(int.__mul__(self, other.$real),
295
int.__mul__(self, other.$imag))
Sep 5, 2014
296
}
Mar 10, 2018
297
if(isinstance(other, [_b_.list, _b_.tuple])){
Sep 5, 2014
298
var res = []
299
// make temporary copy of list
Mar 10, 2018
300
var $temp = other.slice(0, other.length)
301
for(var i = 0; i < val; i++){res = res.concat($temp)}
302
if(isinstance(other, _b_.tuple)){res = _b_.tuple.$factory(res)}
Sep 5, 2014
303
return res
304
}
Mar 10, 2018
305
if(hasattr(other, "__rmul__")){return getattr(other, "__rmul__")(self)}
306
$err("*", other)
Sep 5, 2014
307
}
308
309
int.__neg__ = function(self){return -self}
Sep 5, 2014
310
311
int.__new__ = function(cls){
Mar 10, 2018
312
if(cls === undefined){
313
throw _b_.TypeError.$factory("int.__new__(): not enough arguments")
314
}
Sep 5, 2014
316
}
317
318
int.__pos__ = function(self){return self}
Sep 5, 2014
319
Mar 10, 2018
320
int.__pow__ = function(self, other, z){
Sep 5, 2014
321
if(isinstance(other, int)) {
Feb 9, 2015
322
switch(other.valueOf()) {
Mar 10, 2018
323
case 0:
324
return int.$factory(1)
325
case 1:
326
return int.$factory(self.valueOf())
Feb 9, 2015
327
}
May 19, 2017
328
if(z !== undefined && z !== null){
329
// If z is provided, the algorithm is faster than computing
330
// self ** other then applying the modulo z
331
var res = (self % z + z) % z
Mar 10, 2018
332
for(var i = 1; i < other; i++){
May 19, 2017
333
res *= self
334
res = (res % z + z) % z
335
}
336
return res
337
}
Mar 10, 2018
338
var res = Math.pow(self.valueOf(), other.valueOf())
339
if(res > $B.min_int && res < $B.max_int){return res}
May 19, 2017
340
else if(res !== Infinity && !isFinite(res)){return res}
Feb 11, 2018
342
return int.$factory($B.long_int.__pow__($B.long_int.$factory(self),
343
$B.long_int.$factory(other)))
May 19, 2017
344
}
Sep 5, 2014
345
}
346
if(isinstance(other, _b_.float)) {
Mar 10, 2018
347
if(self >= 0){return new Number(Math.pow(self, other.valueOf()))}
348
else{
349
// use complex power
350
return _b_.complex.__pow__($B.make_complex(self, 0), other)
352
}else if(isinstance(other, _b_.complex)){
Mar 10, 2018
353
var preal = Math.pow(self, other.$real),
354
ln = Math.log(self)
Mar 10, 2018
355
return $B.make_complex(preal * Math.cos(ln), preal * Math.sin(ln))
Sep 5, 2014
356
}
Mar 10, 2018
357
if(hasattr(other, "__rpow__")){return getattr(other, "__rpow__")(self)}
358
$err("**", other)
Sep 5, 2014
359
}
360
361
int.__repr__ = function(self){
Mar 10, 2018
362
if(self === int){return "<class 'int'>"}
Sep 5, 2014
363
return self.toString()
364
}
365
366
// bitwise right shift
Mar 10, 2018
367
int.__rshift__ = function(self, other){
368
if(isinstance(other, int)){
Feb 11, 2018
369
return int.$factory($B.long_int.__rshift__($B.long_int.$factory(self),
370
$B.long_int.$factory(other)))
Mar 10, 2018
372
var rrshift = getattr(other, "__rrshift__", None)
373
if(rrshift !== None){return rrshift(self)}
374
$err('>>', other)
375
}
Sep 5, 2014
376
377
int.__setattr__ = function(self,attr,value){
Mar 10, 2018
378
if(typeof self == "number"){
379
if(int.$factory[attr] === undefined){
380
throw _b_.AttributeError.$factory(
381
"'int' object has no attribute '" + attr + "'")
Mar 10, 2018
383
throw _b_.AttributeError.$factory(
384
"'int' object attribute '" + attr + "' is read-only")
Sep 5, 2014
386
}
387
// subclasses of int can have attributes set
388
self[attr] = value
Sep 5, 2014
390
}
391
392
int.__str__ = int.__repr__
Sep 5, 2014
393
Mar 10, 2018
394
int.__truediv__ = function(self, other){
395
if(isinstance(other, int)){
396
if(other == 0){throw ZeroDivisionError.$factory("division by zero")}
397
if(other.__class__ === $B.long_int){
398
return new Number(self / parseInt(other.value))
399
}
400
return new Number(self / other)
Sep 5, 2014
401
}
Mar 10, 2018
402
if(isinstance(other, _b_.float)){
403
if(!other.valueOf()){
404
throw ZeroDivisionError.$factory("division by zero")
405
}
406
return new Number(self / other)
Sep 5, 2014
407
}
Mar 10, 2018
408
if(isinstance(other, _b_.complex)){
409
var cmod = other.$real * other.$real + other.$imag * other.$imag
410
if(cmod == 0){throw ZeroDivisionError.$factory("division by zero")}
411
return $B.make_complex(self * other.$real / cmod,
412
-self * other.$imag / cmod)
Sep 5, 2014
413
}
Mar 10, 2018
414
if(hasattr(other, "__rtruediv__")){
415
return getattr(other, "__rtruediv__")(self)
416
}
417
$err("/", other)
Sep 5, 2014
418
}
419
420
//int.__xor__ = function(self,other){return self ^ other} // bitwise XOR
Sep 5, 2014
421
422
int.bit_length = function(self){
Sep 5, 2014
423
s = bin(self)
Mar 10, 2018
424
s = getattr(s, "lstrip")("-0b") // remove leading zeros and minus sign
Sep 5, 2014
425
return s.length // len('100101') --> 6
426
}
427
428
// descriptors
429
int.numerator = function(self){return self}
430
int.denominator = function(self){return int.$factory(1)}
431
int.imag = function(self){return int.$factory(0)}
432
int.real = function(self){return self}
433
Mar 10, 2018
434
$B.max_int32 = (1 << 30) * 2 - 1
435
$B.min_int32 = - $B.max_int32
437
// code for operands & | ^
Mar 10, 2018
438
var $op_func = function(self, other){
439
if(isinstance(other, int)) {
440
if(other.__class__ === $B.long_int){
441
return $B.long_int.__sub__($B.long_int.$factory(self),
442
$B.long_int.$factory(other))
Mar 23, 2018
444
if(self > $B.max_int32 || self < $B.min_int32 ||
445
other > $B.max_int32 || other < $B.min_int32){
Mar 10, 2018
446
return $B.long_int.__sub__($B.long_int.$factory(self),
447
$B.long_int.$factory(other))
Mar 21, 2018
449
return self - other
Jun 7, 2015
450
}
Mar 10, 2018
451
if(isinstance(other, _b_.bool)){return self - other}
452
if(hasattr(other, "__rsub__")){return getattr(other, "__rsub__")(self)}
453
$err("-", other)
Sep 5, 2014
454
}
455
Mar 10, 2018
456
$op_func += "" // source code
457
var $ops = {"&": "and", "|": "or", "^": "xor"}
Sep 5, 2014
458
for(var $op in $ops){
Mar 10, 2018
459
var opf = $op_func.replace(/-/gm, $op)
460
opf = opf.replace(new RegExp("sub", "gm"), $ops[$op])
461
eval("int.__" + $ops[$op] + "__ = " + opf)
Sep 5, 2014
462
}
463
464
// code for + and -
Mar 10, 2018
465
var $op_func = function(self, other){
466
if(isinstance(other, int)){
467
if(typeof other == "number"){
468
var res = self.valueOf() - other.valueOf()
469
if(res > $B.min_int && res < $B.max_int){return res}
Feb 11, 2018
470
else{return $B.long_int.__sub__($B.long_int.$factory(self),
471
$B.long_int.$factory(other))}
Mar 10, 2018
472
}else if(typeof other == "boolean"){
Mar 21, 2018
473
return other ? self - 1 : self
474
}else{
Feb 11, 2018
475
return $B.long_int.__sub__($B.long_int.$factory(self),
476
$B.long_int.$factory(other))
Sep 5, 2014
478
}
Mar 10, 2018
479
if(isinstance(other, _b_.float)){
480
return new Number(self - other)
Sep 5, 2014
481
}
Mar 10, 2018
482
if(isinstance(other, _b_.complex)){
483
return $B.make_complex(self - other.$real, -other.$imag)
Sep 5, 2014
484
}
Mar 10, 2018
485
if(isinstance(other, _b_.bool)){
486
var bool_value = 0;
487
if(other.valueOf()){bool_value = 1}
488
return self - bool_value
Sep 5, 2014
489
}
Mar 10, 2018
490
if(isinstance(other, _b_.complex)){
491
return $B.make_complex(self.valueOf() - other.$real, other.$imag)
Sep 5, 2014
492
}
Mar 10, 2018
493
var rsub = $B.$getattr(other, "__rsub__", None)
494
if(rsub !== None){return rsub(self)}
495
throw $err("-", other)
Sep 5, 2014
496
}
Mar 10, 2018
497
$op_func += "" // source code
498
var $ops = {"+": "add", "-": "sub"}
Sep 5, 2014
499
for(var $op in $ops){
Mar 10, 2018
500
var opf = $op_func.replace(/-/gm, $op)
501
opf = opf.replace(new RegExp("sub", "gm"), $ops[$op])
502
eval("int.__" + $ops[$op] + "__ = " + opf)
Sep 5, 2014
503
}
504
505
// comparison methods
Mar 10, 2018
506
var $comp_func = function(self, other){
Mar 23, 2018
507
if(other.__class__ === $B.long_int){
Feb 11, 2018
508
return $B.long_int.__lt__(other, $B.long_int.$factory(self))
Mar 10, 2018
510
if(isinstance(other, int)){return self.valueOf() > other.valueOf()}
511
if(isinstance(other, _b_.float)){return self.valueOf() > other.valueOf()}
512
if(isinstance(other, _b_.bool)) {
Feb 11, 2018
513
return self.valueOf() > _b_.bool.__hash__(other)
Sep 5, 2014
514
}
Mar 10, 2018
515
if(hasattr(other, "__int__") || hasattr(other, "__index__")){
516
return int.__gt__(self, $B.$GetInt(other))
519
// See if other has the opposite operator, eg < for >
Mar 10, 2018
520
var inv_op = $B.$getattr(other, "__lt__", None)
521
if(inv_op !== None){return inv_op(self)}
523
throw _b_.TypeError.$factory(
Mar 21, 2018
524
"unorderable types: int() > " + $B.get_class(other).__name__ + "()")
Sep 5, 2014
525
}
Mar 10, 2018
526
$comp_func += "" // source code
Sep 5, 2014
528
for(var $op in $B.$comps){
Mar 10, 2018
529
eval("int.__"+$B.$comps[$op] + "__ = " +
530
$comp_func.replace(/>/gm, $op).
531
replace(/__gt__/gm,"__" + $B.$comps[$op] + "__").
532
replace(/__lt__/, "__" + $B.$inv_comps[$op] + "__"))
Sep 5, 2014
533
}
534
535
// add "reflected" methods
536
$B.make_rmethods(int)
Sep 5, 2014
537
Mar 10, 2018
538
var $valid_digits = function(base) {
539
var digits = ""
540
if(base === 0){return "0"}
541
if(base < 10){
Mar 21, 2018
542
for(var i = 0; i < base; i++){digits += String.fromCharCode(i + 48)}
Sep 5, 2014
543
return digits
544
}
545
Mar 10, 2018
546
var digits = "0123456789"
Sep 5, 2014
547
// A = 65 (10 + 55)
Mar 21, 2018
548
for (var i = 10; i < base; i++) {digits += String.fromCharCode(i + 55)}
Sep 5, 2014
549
return digits
550
}
551
552
int.$factory = function(value, base){
553
// int() with no argument returns 0
Mar 10, 2018
554
if(value === undefined){return 0}
556
// int() of an integer returns the integer if base is undefined
Mar 10, 2018
557
if(typeof value == "number" &&
558
(base === undefined || base == 10)){return parseInt(value)}
Mar 10, 2018
560
if(base !== undefined){
561
if(! isinstance(value, [_b_.str, _b_.bytes, _b_.bytearray])){
562
throw TypeError.$factory(
563
"int() can't convert non-string with explicit base")
Dec 28, 2014
564
}
565
}
566
Mar 10, 2018
567
if(isinstance(value, _b_.complex)){
568
throw TypeError.$factory("can't convert complex to int")
Dec 28, 2014
569
}
570
Mar 10, 2018
571
var $ns = $B.args("int", 2, {x:null, base:null}, ["x", "base"], arguments,
572
{"base": 10}, null, null),
573
value = $ns["x"],
574
base = $ns["base"]
Mar 10, 2018
576
if(isinstance(value, _b_.float) && base == 10){
577
if(value < $B.min_int || value > $B.max_int){
Feb 11, 2018
578
return $B.long_int.$from_float(value)
Mar 10, 2018
580
else{return value > 0 ? Math.floor(value) : Math.ceil(value)}
Sep 5, 2014
582
Mar 10, 2018
583
if(! (base >=2 && base <= 36)){
Dec 26, 2014
584
// throw error (base must be 0, or 2-36)
Mar 10, 2018
585
if(base != 0){throw _b_.ValueError.$factory("invalid base")}
Dec 26, 2014
586
}
587
Mar 10, 2018
588
if(typeof value == "number"){
Mar 10, 2018
590
if(base == 10){
591
if(value < $B.min_int || value > $B.max_int){
592
return $B.long_int.$factory(value)
593
}
Mar 10, 2018
595
}else if(value.toString().search("e") > -1){
Dec 26, 2014
596
// can't convert to another base if value is too big
Mar 10, 2018
597
throw _b_.OverflowError.$factory("can't convert to base " + base)
Dec 26, 2014
598
}else{
Mar 10, 2018
599
var res = parseInt(value, base)
600
if(value < $B.min_int || value > $B.max_int){
601
return $B.long_int.$factory(value, base)
602
}
Dec 26, 2014
604
}
605
}
Sep 5, 2014
606
Mar 10, 2018
607
if(value === true){return Number(1)}
608
if(value === false){return Number(0)}
609
if(value.__class__ === $B.long_int){
610
var z = parseInt(value.value)
Mar 10, 2018
611
if(z > $B.min_int && z < $B.max_int){return z}
612
else{return value}
613
}
Sep 5, 2014
614
Mar 10, 2018
615
base = $B.$GetInt(base)
Sep 5, 2014
616
Mar 10, 2018
617
if(isinstance(value, _b_.str)){value = value.valueOf()}
618
if(typeof value == "string") {
619
var _value = value.trim() // remove leading/trailing whitespace
620
if(_value.length == 2 && base == 0 &&
621
(_value == "0b" || _value == "0o" || _value == "0x")){
622
throw _b_.ValueError.$factory("invalid value")
623
}
624
if(_value.length >2) {
625
var _pre = _value.substr(0, 2).toUpperCase()
626
if(base == 0){
627
if(_pre == "0B"){base = 2}
628
if(_pre == "0O"){base = 8}
629
if(_pre == "0X"){base = 16}
630
}
631
if(_pre == "0B" || _pre == "0O" || _pre == "0X"){
632
_value = _value.substr(2)
633
}
634
}
635
var _digits = $valid_digits(base)
636
var _re = new RegExp("^[+-]?[" + _digits + "]+$", "i")
637
if(! _re.test(_value)){
638
throw _b_.ValueError.$factory(
639
"invalid literal for int() with base " + base + ": '" +
640
_b_.str.$factory(value) + "'")
641
}
642
if(base <= 10 && ! isFinite(value)){
643
throw _b_.ValueError.$factory(
644
"invalid literal for int() with base " + base + ": '" +
645
_b_.str.$factory(value) + "'")
646
}
647
var res = parseInt(_value, base)
648
if(res < $B.min_int || res > $B.max_int){
649
return $B.long_int.$factory(_value, base)
650
}
651
return res
Sep 5, 2014
652
}
Mar 10, 2018
654
if(isinstance(value, [_b_.bytes, _b_.bytearray])){
655
var _digits = $valid_digits(base)
Mar 10, 2018
656
for(var i = 0; i < value.source.length; i++){
657
if(_digits.indexOf(String.fromCharCode(value.source[i])) == -1){
658
throw _b_.ValueError.$factory(
659
"invalid literal for int() with base " + base + ": " +
660
_b_.repr(value))
Mar 10, 2018
663
return Number(parseInt(getattr(value, "decode")("latin-1"), base))
Sep 5, 2014
665
Mar 10, 2018
666
if(hasattr(value, "__int__")){return getattr(value, "__int__")()}
667
if(hasattr(value, "__index__")){return getattr(value, "__index__")()}
668
if(hasattr(value, "__trunc__")){
669
var res = getattr(value, "__trunc__")(),
670
int_func = _b_.getattr(res, "__int__", null)
671
if(int_func === null){
672
throw TypeError.$factory("__trunc__ returned non-Integral (type "+
Mar 21, 2018
673
$B.get_class(res).__name__ + ")")
Mar 10, 2018
675
var res = int_func()
676
if(isinstance(res, int)){return res}
Mar 10, 2018
677
throw TypeError.$factory("__trunc__ returned non-Integral (type "+
Mar 21, 2018
678
$B.get_class(res).__name__ + ")")
Mar 10, 2018
680
throw _b_.TypeError.$factory(
681
"int() argument must be a string, a bytes-like " +
682
"object or a number, not '" + $B.get_class(value).__name__ + "'")
Sep 5, 2014
683
}
684
685
$B.set_func_names(int, "builtins")
Sep 5, 2014
687
_b_.int = int
688
Feb 11, 2018
690
$B.$bool = function(obj){ // return true or false
Mar 10, 2018
691
if(obj === null || obj === undefined ){ return false}
692
switch(typeof obj){
693
case "boolean":
694
return obj
695
case "number":
696
case "string":
697
if(obj){return true}
698
return false
699
default:
700
var ce = $B.current_exception
701
try{return getattr(obj, "__bool__")()}
702
catch(err){
703
$B.current_exception = ce
Mar 21, 2018
704
try{return getattr(obj, "__len__")() > 0}
Mar 10, 2018
705
catch(err){return true}
706
}
707
}
Feb 11, 2018
708
}
709
710
var bool = {
Feb 11, 2018
711
__class__: _b_.type,
Feb 11, 2018
712
__module__: "builtins",
713
__mro__: [int, object],
714
__name__: "bool",
715
$is_class: true,
716
$native: true
717
}
Feb 11, 2018
719
bool.__add__ = function(self,other){
Mar 10, 2018
720
return (other ? 1 : 0) + (self ? 1 : 0)
Feb 11, 2018
723
bool.__and__ = function(self, other){
724
return $B.$bool(int.__and__(self, other))
Feb 11, 2018
727
bool.__eq__ = function(self,other){
728
return self ? $B.$bool(other) : !$B.$bool(other)
Feb 11, 2018
731
bool.__ne__ = function(self,other){
732
return self ? !$B.$bool(other) : $B.$bool(other)
Feb 11, 2018
735
bool.__ge__ = function(self,other){
736
return _b_.int.__ge__(bool.__hash__(self),other)
Feb 11, 2018
739
bool.__gt__ = function(self,other){
740
return _b_.int.__gt__(bool.__hash__(self),other)
Mar 10, 2018
743
bool.__hash__ = bool.__index__ = bool.__int__ = function(self){
744
if(self.valueOf()) return 1
745
return 0
746
}
747
Mar 10, 2018
748
bool.__le__ = function(self, other){return ! bool.__gt__(self, other)}
Mar 10, 2018
750
bool.__lshift__ = function(self, other){return self.valueOf() << other}
Mar 10, 2018
752
bool.__lt__ = function(self, other){return ! bool.__ge__(self, other)}
Mar 10, 2018
754
bool.__mul__ = function(self, other){
Feb 11, 2018
758
bool.__neg__ = function(self){return -$B.int_or_bool(self)}
Feb 11, 2018
760
bool.__or__ = function(self, other){
761
return $B.$bool(int.__or__(self, other))
Feb 11, 2018
764
bool.__pos__ = $B.int_or_bool
Feb 11, 2018
766
bool.__repr__ = bool.__str__ = function(self){
767
return self ? "True" : "False"
Feb 11, 2018
770
bool.__setattr__ = function(self, attr){
771
return no_set_attr(bool, attr)
Feb 11, 2018
774
bool.__sub__ = function(self,other){
775
return (self ? 1 : 0) - (other ? 1 : 0)
Feb 11, 2018
778
bool.__xor__ = function(self, other) {
779
return self.valueOf() != other.valueOf()
780
}
781
Feb 11, 2018
782
bool.$factory = function(){
783
// Calls $B.$bool, which is used inside the generated JS code and skips
784
// arguments control.
Mar 10, 2018
785
var $ = $B.args("bool", 1, {x: null}, ["x"],
786
arguments,{x: false}, null, null)
Feb 11, 2018
787
return $B.$bool($.x)
788
}
789
790
_b_.bool = bool
Feb 11, 2018
792
$B.set_func_names(bool, "builtins")
Sep 5, 2014
794
})(__BRYTHON__)
Mar 10, 2018
795