Skip to content
Permalink
Newer
Older
100644 755 lines (654 sloc) 22.9 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(length, byteorder, star) {
Sep 5, 2014
78
//var len = x.length
79
throw _b_.NotImplementedError.$factory("int.to_bytes is not implemented yet")
Sep 5, 2014
80
}
81
82
83
//int.__and__ = function(self,other){return self & other} // bitwise AND
Sep 5, 2014
84
85
int.__abs__ = function(self){return abs(self)}
87
int.__bool__ = function(self){return new Boolean(self.valueOf())}
Sep 5, 2014
88
89
int.__ceil__ = function(self){return Math.ceil(self)}
91
int.__divmod__ = function(self, other){return divmod(self, other)}
Mar 10, 2018
93
int.__eq__ = function(self, other){
Sep 5, 2014
94
// compare object "self" to class "int"
Mar 10, 2018
95
if(other === undefined){return self === int}
96
if(isinstance(other, int)){return self.valueOf() == other.valueOf()}
97
if(isinstance(other, _b_.float)){return self.valueOf() == other.valueOf()}
98
if(isinstance(other, _b_.complex)){
99
if(other.$imag != 0){return False}
100
return self.valueOf() == other.$real
Sep 5, 2014
101
}
Mar 10, 2018
103
if(hasattr(other, "__eq__")){return getattr(other, "__eq__")(self)}
Mar 10, 2018
105
return self.valueOf() === other
Sep 5, 2014
106
}
107
108
int.__float__ = function(self){
109
return new Number(self)
110
}
111
112
function preformat(self, fmt){
str
Feb 10, 2018
113
if(fmt.empty){return _b_.str.$factory(self)}
Mar 10, 2018
114
if(fmt.type && 'bcdoxXn'.indexOf(fmt.type) == -1){
115
throw _b_.ValueError.$factory("Unknown format code '" + fmt.type +
116
"' for object of type 'int'")
117
}
119
switch(fmt.type){
120
case undefined:
Mar 10, 2018
121
case "d":
122
return self.toString()
Mar 10, 2018
123
case "b":
124
return (fmt.alternate ? "0b" : "") + self.toString(2)
125
case "c":
126
return _b_.chr(self)
Mar 10, 2018
127
case "o":
128
return (fmt.alternate ? "0o" : "") + self.toString(8)
129
case "x":
130
return (fmt.alternate ? "0x" : "") + self.toString(16)
131
case "X":
132
return (fmt.alternate ? "0X" : "") + self.toString(16).toUpperCase()
133
case "n":
134
return self // fix me
135
}
137
return res
138
}
139
140
Mar 10, 2018
141
int.__format__ = function(self, format_spec){
142
var fmt = new $B.parse_format_spec(format_spec)
Mar 10, 2018
143
if(fmt.type && 'eEfFgG%'.indexOf(fmt.type) != -1){
144
// Call __format__ on float(self)
145
return _b_.float.__format__(self, format_spec)
Mar 10, 2018
147
fmt.align = fmt.align || ">"
148
var res = preformat(self, fmt)
149
if(fmt.comma){
Mar 10, 2018
150
var sign = res[0] == "-" ? "-" : "",
151
rest = res.substr(sign.length),
152
len = rest.length,
153
nb = Math.ceil(rest.length/3),
154
chunks = []
Mar 10, 2018
155
for(var i = 0; i < nb; i++){
156
chunks.push(rest.substring(len - 3 * i - 3, len - 3 * i))
157
}
158
chunks.reverse()
Mar 10, 2018
159
res = sign + chunks.join(",")
160
}
161
return $B.format_width(res, fmt)
Sep 5, 2014
162
}
163
164
int.__floordiv__ = function(self,other){
Mar 10, 2018
165
if(isinstance(other, int)){
166
if(other == 0){throw ZeroDivisionError.$factory("division by zero")}
167
return Math.floor(self / other)
Sep 5, 2014
168
}
Mar 10, 2018
169
if(isinstance(other, _b_.float)){
170
if(!other.valueOf()){
171
throw ZeroDivisionError.$factory("division by zero")
172
}
173
return Math.floor(self / other)
Sep 5, 2014
174
}
Mar 10, 2018
175
if(hasattr(other, "__rfloordiv__")){
176
return getattr(other, "__rfloordiv__")(self)
Sep 5, 2014
177
}
Mar 10, 2018
178
$err("//", other)
Sep 5, 2014
179
}
180
181
int.__hash__ = function(self){
183
return int.__hashvalue__ || $B.$py_next_hash-- // for hash of int type (not instance of int)
184
}
185
return self.valueOf()
186
}
Sep 5, 2014
187
188
//int.__ior__ = function(self,other){return self | other} // bitwise OR
Sep 5, 2014
189
190
int.__index__ = function(self){return self}
Sep 5, 2014
191
192
int.__init__ = function(self,value){
Mar 10, 2018
193
if(value === undefined){value = 0}
Sep 5, 2014
194
self.toString = function(){return value}
Sep 5, 2014
196
}
197
198
int.__int__ = function(self){return self}
Sep 5, 2014
199
200
int.__invert__ = function(self){return ~self}
Sep 5, 2014
201
Mar 10, 2018
203
int.__lshift__ = function(self, other){
204
if(isinstance(other, int)){
Mar 10, 2018
205
return int.$factory($B.long_int.__lshift__($B.long_int.$factory(self),
206
$B.long_int.$factory(other)))
Mar 10, 2018
208
var rlshift = getattr(other, "__rlshift__", None)
209
if(rlshift !== None){return rlshift(self)}
210
$err("<<", other)
Mar 10, 2018
213
int.__mod__ = function(self, other) {
Sep 5, 2014
214
// can't use Javascript % because it works differently for negative numbers
Mar 10, 2018
215
if(isinstance(other,_b_.tuple) && other.length == 1){other = other[0]}
216
if(isinstance(other, [int, _b_.float, bool])){
217
if(other === false){other = 0}
218
else if(other === true){other = 1}
219
if(other == 0){throw _b_.ZeroDivisionError.$factory(
220
"integer division or modulo by zero")}
Mar 10, 2018
221
return (self % other + other) % other
Sep 5, 2014
222
}
Mar 10, 2018
223
if(hasattr(other, "__rmod__")){return getattr(other, "__rmod__")(self)}
224
$err("%", other)
Sep 5, 2014
225
}
226
227
int.__mro__ = [object]
Sep 5, 2014
228
Mar 10, 2018
229
int.__mul__ = function(self, other){
230
Sep 5, 2014
231
var val = self.valueOf()
Jan 22, 2015
232
233
// this will be quick check, so lets do it early.
Mar 10, 2018
234
if(typeof other === "string") {
Jan 22, 2015
235
return other.repeat(val)
236
}
237
Mar 10, 2018
238
if(isinstance(other, int)){
239
var res = self * other
240
if(res > $B.min_int && res < $B.max_int){return res}
241
else{
242
return int.$factory($B.long_int.__mul__($B.long_int.$factory(self),
243
$B.long_int.$factory(other)))
244
}
Mar 10, 2018
246
if(isinstance(other, _b_.float)){
247
return new Number(self * other)
Mar 10, 2018
249
if(isinstance(other, _b_.bool)){
250
if(other.valueOf()){return self}
251
return int.$factory(0)
Sep 5, 2014
252
}
Mar 10, 2018
253
if(isinstance(other, _b_.complex)){
254
return $B.make_complex(int.__mul__(self, other.$real),
255
int.__mul__(self, other.$imag))
Sep 5, 2014
256
}
Mar 10, 2018
257
if(isinstance(other, [_b_.list, _b_.tuple])){
Sep 5, 2014
258
var res = []
259
// make temporary copy of list
Mar 10, 2018
260
var $temp = other.slice(0, other.length)
261
for(var i = 0; i < val; i++){res = res.concat($temp)}
262
if(isinstance(other, _b_.tuple)){res = _b_.tuple.$factory(res)}
Sep 5, 2014
263
return res
264
}
Mar 10, 2018
265
if(hasattr(other, "__rmul__")){return getattr(other, "__rmul__")(self)}
266
$err("*", other)
Sep 5, 2014
267
}
268
269
int.__neg__ = function(self){return -self}
Sep 5, 2014
270
271
int.__new__ = function(cls){
Mar 10, 2018
272
if(cls === undefined){
273
throw _b_.TypeError.$factory("int.__new__(): not enough arguments")
274
}
Sep 5, 2014
276
}
277
278
int.__pos__ = function(self){return self}
Sep 5, 2014
279
Mar 10, 2018
280
int.__pow__ = function(self, other, z){
Sep 5, 2014
281
if(isinstance(other, int)) {
Feb 9, 2015
282
switch(other.valueOf()) {
Mar 10, 2018
283
case 0:
284
return int.$factory(1)
285
case 1:
286
return int.$factory(self.valueOf())
Feb 9, 2015
287
}
May 19, 2017
288
if(z !== undefined && z !== null){
289
// If z is provided, the algorithm is faster than computing
290
// self ** other then applying the modulo z
291
var res = (self % z + z) % z
Mar 10, 2018
292
for(var i = 1; i < other; i++){
May 19, 2017
293
res *= self
294
res = (res % z + z) % z
295
}
296
return res
297
}
Mar 10, 2018
298
var res = Math.pow(self.valueOf(), other.valueOf())
299
if(res > $B.min_int && res < $B.max_int){return res}
May 19, 2017
300
else if(res !== Infinity && !isFinite(res)){return res}
Feb 11, 2018
302
return int.$factory($B.long_int.__pow__($B.long_int.$factory(self),
303
$B.long_int.$factory(other)))
May 19, 2017
304
}
Sep 5, 2014
305
}
306
if(isinstance(other, _b_.float)) {
Mar 10, 2018
307
if(self >= 0){return new Number(Math.pow(self, other.valueOf()))}
308
else{
309
// use complex power
310
return _b_.complex.__pow__($B.make_complex(self, 0), other)
312
}else if(isinstance(other, _b_.complex)){
Mar 10, 2018
313
var preal = Math.pow(self, other.$real),
314
ln = Math.log(self)
Mar 10, 2018
315
return $B.make_complex(preal * Math.cos(ln), preal * Math.sin(ln))
Sep 5, 2014
316
}
Mar 10, 2018
317
if(hasattr(other, "__rpow__")){return getattr(other, "__rpow__")(self)}
318
$err("**", other)
Sep 5, 2014
319
}
320
321
int.__repr__ = function(self){
Mar 10, 2018
322
if(self === int){return "<class 'int'>"}
Sep 5, 2014
323
return self.toString()
324
}
325
326
// bitwise right shift
Mar 10, 2018
327
int.__rshift__ = function(self, other){
328
if(isinstance(other, int)){
Feb 11, 2018
329
return int.$factory($B.long_int.__rshift__($B.long_int.$factory(self),
330
$B.long_int.$factory(other)))
Mar 10, 2018
332
var rrshift = getattr(other, "__rrshift__", None)
333
if(rrshift !== None){return rrshift(self)}
334
$err('>>', other)
335
}
Sep 5, 2014
336
337
int.__setattr__ = function(self,attr,value){
Mar 10, 2018
338
if(typeof self == "number"){
339
if(int.$factory[attr] === undefined){
340
throw _b_.AttributeError.$factory(
341
"'int' object has no attribute '" + attr + "'")
Mar 10, 2018
343
throw _b_.AttributeError.$factory(
344
"'int' object attribute '" + attr + "' is read-only")
Sep 5, 2014
346
}
347
// subclasses of int can have attributes set
348
self[attr] = value
Sep 5, 2014
350
}
351
352
int.__str__ = int.__repr__
Sep 5, 2014
353
Mar 10, 2018
354
int.__truediv__ = function(self, other){
355
if(isinstance(other, int)){
356
if(other == 0){throw ZeroDivisionError.$factory("division by zero")}
357
if(other.__class__ === $B.long_int){
358
return new Number(self / parseInt(other.value))
359
}
360
return new Number(self / other)
Sep 5, 2014
361
}
Mar 10, 2018
362
if(isinstance(other, _b_.float)){
363
if(!other.valueOf()){
364
throw ZeroDivisionError.$factory("division by zero")
365
}
366
return new Number(self / other)
Sep 5, 2014
367
}
Mar 10, 2018
368
if(isinstance(other, _b_.complex)){
369
var cmod = other.$real * other.$real + other.$imag * other.$imag
370
if(cmod == 0){throw ZeroDivisionError.$factory("division by zero")}
371
return $B.make_complex(self * other.$real / cmod,
372
-self * other.$imag / cmod)
Sep 5, 2014
373
}
Mar 10, 2018
374
if(hasattr(other, "__rtruediv__")){
375
return getattr(other, "__rtruediv__")(self)
376
}
377
$err("/", other)
Sep 5, 2014
378
}
379
380
//int.__xor__ = function(self,other){return self ^ other} // bitwise XOR
Sep 5, 2014
381
382
int.bit_length = function(self){
Sep 5, 2014
383
s = bin(self)
Mar 10, 2018
384
s = getattr(s, "lstrip")("-0b") // remove leading zeros and minus sign
Sep 5, 2014
385
return s.length // len('100101') --> 6
386
}
387
388
// descriptors
389
int.numerator = function(self){return self}
390
int.denominator = function(self){return int.$factory(1)}
391
int.imag = function(self){return int.$factory(0)}
392
int.real = function(self){return self}
393
Mar 10, 2018
394
$B.max_int32 = (1 << 30) * 2 - 1
395
$B.min_int32 = - $B.max_int32
397
// code for operands & | ^
Mar 10, 2018
398
var $op_func = function(self, other){
399
if(isinstance(other, int)) {
400
if(other.__class__ === $B.long_int){
401
return $B.long_int.__sub__($B.long_int.$factory(self),
402
$B.long_int.$factory(other))
404
if (self > $B.max_int32 || self < $B.min_int32 ||
Mar 10, 2018
405
other > $B.max_int32 || other < $B.min_int32) {
406
return $B.long_int.__sub__($B.long_int.$factory(self),
407
$B.long_int.$factory(other))
Mar 21, 2018
409
return self - other
Jun 7, 2015
410
}
Mar 10, 2018
411
if(isinstance(other, _b_.bool)){return self - other}
412
if(hasattr(other, "__rsub__")){return getattr(other, "__rsub__")(self)}
413
$err("-", other)
Sep 5, 2014
414
}
415
Mar 10, 2018
416
$op_func += "" // source code
417
var $ops = {"&": "and", "|": "or", "^": "xor"}
Sep 5, 2014
418
for(var $op in $ops){
Mar 10, 2018
419
var opf = $op_func.replace(/-/gm, $op)
420
opf = opf.replace(new RegExp("sub", "gm"), $ops[$op])
421
eval("int.__" + $ops[$op] + "__ = " + opf)
Sep 5, 2014
422
}
423
424
// code for + and -
Mar 10, 2018
425
var $op_func = function(self, other){
426
if(isinstance(other, int)){
427
if(typeof other == "number"){
428
var res = self.valueOf() - other.valueOf()
429
if(res > $B.min_int && res < $B.max_int){return res}
Feb 11, 2018
430
else{return $B.long_int.__sub__($B.long_int.$factory(self),
431
$B.long_int.$factory(other))}
Mar 10, 2018
432
}else if(typeof other == "boolean"){
Mar 21, 2018
433
return other ? self - 1 : self
434
}else{
Feb 11, 2018
435
return $B.long_int.__sub__($B.long_int.$factory(self),
436
$B.long_int.$factory(other))
Sep 5, 2014
438
}
Mar 10, 2018
439
if(isinstance(other, _b_.float)){
440
return new Number(self - other)
Sep 5, 2014
441
}
Mar 10, 2018
442
if(isinstance(other, _b_.complex)){
443
return $B.make_complex(self - other.$real, -other.$imag)
Sep 5, 2014
444
}
Mar 10, 2018
445
if(isinstance(other, _b_.bool)){
446
var bool_value = 0;
447
if(other.valueOf()){bool_value = 1}
448
return self - bool_value
Sep 5, 2014
449
}
Mar 10, 2018
450
if(isinstance(other, _b_.complex)){
451
return $B.make_complex(self.valueOf() - other.$real, other.$imag)
Sep 5, 2014
452
}
Mar 10, 2018
453
var rsub = $B.$getattr(other, "__rsub__", None)
454
if(rsub !== None){return rsub(self)}
455
throw $err("-", other)
Sep 5, 2014
456
}
Mar 10, 2018
457
$op_func += "" // source code
458
var $ops = {"+": "add", "-": "sub"}
Sep 5, 2014
459
for(var $op in $ops){
Mar 10, 2018
460
var opf = $op_func.replace(/-/gm, $op)
461
opf = opf.replace(new RegExp("sub", "gm"), $ops[$op])
462
eval("int.__" + $ops[$op] + "__ = " + opf)
Sep 5, 2014
463
}
464
465
// comparison methods
Mar 10, 2018
466
var $comp_func = function(self, other){
Feb 11, 2018
467
if (other.__class__ === $B.long_int) {
468
return $B.long_int.__lt__(other, $B.long_int.$factory(self))
Mar 10, 2018
470
if(isinstance(other, int)){return self.valueOf() > other.valueOf()}
471
if(isinstance(other, _b_.float)){return self.valueOf() > other.valueOf()}
472
if(isinstance(other, _b_.bool)) {
Feb 11, 2018
473
return self.valueOf() > _b_.bool.__hash__(other)
Sep 5, 2014
474
}
Mar 10, 2018
475
if(hasattr(other, "__int__") || hasattr(other, "__index__")){
476
return int.__gt__(self, $B.$GetInt(other))
479
// See if other has the opposite operator, eg < for >
Mar 10, 2018
480
var inv_op = $B.$getattr(other, "__lt__", None)
481
if(inv_op !== None){return inv_op(self)}
483
throw _b_.TypeError.$factory(
Mar 21, 2018
484
"unorderable types: int() > " + $B.get_class(other).__name__ + "()")
Sep 5, 2014
485
}
Mar 10, 2018
486
$comp_func += "" // source code
Sep 5, 2014
488
for(var $op in $B.$comps){
Mar 10, 2018
489
eval("int.__"+$B.$comps[$op] + "__ = " +
490
$comp_func.replace(/>/gm, $op).
491
replace(/__gt__/gm,"__" + $B.$comps[$op] + "__").
492
replace(/__lt__/, "__" + $B.$inv_comps[$op] + "__"))
Sep 5, 2014
493
}
494
495
// add "reflected" methods
496
$B.make_rmethods(int)
Sep 5, 2014
497
Mar 10, 2018
498
var $valid_digits = function(base) {
499
var digits = ""
500
if(base === 0){return "0"}
501
if(base < 10){
Mar 21, 2018
502
for(var i = 0; i < base; i++){digits += String.fromCharCode(i + 48)}
Sep 5, 2014
503
return digits
504
}
505
Mar 10, 2018
506
var digits = "0123456789"
Sep 5, 2014
507
// A = 65 (10 + 55)
Mar 21, 2018
508
for (var i = 10; i < base; i++) {digits += String.fromCharCode(i + 55)}
Sep 5, 2014
509
return digits
510
}
511
512
int.$factory = function(value, base){
513
// int() with no argument returns 0
Mar 10, 2018
514
if(value === undefined){return 0}
516
// int() of an integer returns the integer if base is undefined
Mar 10, 2018
517
if(typeof value == "number" &&
518
(base === undefined || base == 10)){return parseInt(value)}
Mar 10, 2018
520
if(base !== undefined){
521
if(! isinstance(value, [_b_.str, _b_.bytes, _b_.bytearray])){
522
throw TypeError.$factory(
523
"int() can't convert non-string with explicit base")
Dec 28, 2014
524
}
525
}
526
Mar 10, 2018
527
if(isinstance(value, _b_.complex)){
528
throw TypeError.$factory("can't convert complex to int")
Dec 28, 2014
529
}
530
Mar 10, 2018
531
var $ns = $B.args("int", 2, {x:null, base:null}, ["x", "base"], arguments,
532
{"base": 10}, null, null),
533
value = $ns["x"],
534
base = $ns["base"]
Mar 10, 2018
536
if(isinstance(value, _b_.float) && base == 10){
537
if(value < $B.min_int || value > $B.max_int){
Feb 11, 2018
538
return $B.long_int.$from_float(value)
Mar 10, 2018
540
else{return value > 0 ? Math.floor(value) : Math.ceil(value)}
Sep 5, 2014
542
Mar 10, 2018
543
if(! (base >=2 && base <= 36)){
Dec 26, 2014
544
// throw error (base must be 0, or 2-36)
Mar 10, 2018
545
if(base != 0){throw _b_.ValueError.$factory("invalid base")}
Dec 26, 2014
546
}
547
Mar 10, 2018
548
if(typeof value == "number"){
Mar 10, 2018
550
if(base == 10){
551
if(value < $B.min_int || value > $B.max_int){
552
return $B.long_int.$factory(value)
553
}
Mar 10, 2018
555
}else if(value.toString().search("e") > -1){
Dec 26, 2014
556
// can't convert to another base if value is too big
Mar 10, 2018
557
throw _b_.OverflowError.$factory("can't convert to base " + base)
Dec 26, 2014
558
}else{
Mar 10, 2018
559
var res = parseInt(value, base)
560
if(value < $B.min_int || value > $B.max_int){
561
return $B.long_int.$factory(value, base)
562
}
Dec 26, 2014
564
}
565
}
Sep 5, 2014
566
Mar 10, 2018
567
if(value === true){return Number(1)}
568
if(value === false){return Number(0)}
569
if(value.__class__ === $B.long_int){
570
var z = parseInt(value.value)
Mar 10, 2018
571
if(z > $B.min_int && z < $B.max_int){return z}
572
else{return value}
573
}
Sep 5, 2014
574
Mar 10, 2018
575
base = $B.$GetInt(base)
Sep 5, 2014
576
Mar 10, 2018
577
if(isinstance(value, _b_.str)){value = value.valueOf()}
578
if(typeof value == "string") {
579
var _value = value.trim() // remove leading/trailing whitespace
580
if(_value.length == 2 && base == 0 &&
581
(_value == "0b" || _value == "0o" || _value == "0x")){
582
throw _b_.ValueError.$factory("invalid value")
583
}
584
if(_value.length >2) {
585
var _pre = _value.substr(0, 2).toUpperCase()
586
if(base == 0){
587
if(_pre == "0B"){base = 2}
588
if(_pre == "0O"){base = 8}
589
if(_pre == "0X"){base = 16}
590
}
591
if(_pre == "0B" || _pre == "0O" || _pre == "0X"){
592
_value = _value.substr(2)
593
}
594
}
595
var _digits = $valid_digits(base)
596
var _re = new RegExp("^[+-]?[" + _digits + "]+$", "i")
597
if(! _re.test(_value)){
598
throw _b_.ValueError.$factory(
599
"invalid literal for int() with base " + base + ": '" +
600
_b_.str.$factory(value) + "'")
601
}
602
if(base <= 10 && ! isFinite(value)){
603
throw _b_.ValueError.$factory(
604
"invalid literal for int() with base " + base + ": '" +
605
_b_.str.$factory(value) + "'")
606
}
607
var res = parseInt(_value, base)
608
if(res < $B.min_int || res > $B.max_int){
609
return $B.long_int.$factory(_value, base)
610
}
611
return res
Sep 5, 2014
612
}
Mar 10, 2018
614
if(isinstance(value, [_b_.bytes, _b_.bytearray])){
615
var _digits = $valid_digits(base)
Mar 10, 2018
616
for(var i = 0; i < value.source.length; i++){
617
if(_digits.indexOf(String.fromCharCode(value.source[i])) == -1){
618
throw _b_.ValueError.$factory(
619
"invalid literal for int() with base " + base + ": " +
620
_b_.repr(value))
Mar 10, 2018
623
return Number(parseInt(getattr(value, "decode")("latin-1"), base))
Sep 5, 2014
625
Mar 10, 2018
626
if(hasattr(value, "__int__")){return getattr(value, "__int__")()}
627
if(hasattr(value, "__index__")){return getattr(value, "__index__")()}
628
if(hasattr(value, "__trunc__")){
629
var res = getattr(value, "__trunc__")(),
630
int_func = _b_.getattr(res, "__int__", null)
631
if(int_func === null){
632
throw TypeError.$factory("__trunc__ returned non-Integral (type "+
Mar 21, 2018
633
$B.get_class(res).__name__ + ")")
Mar 10, 2018
635
var res = int_func()
636
if(isinstance(res, int)){return res}
Mar 10, 2018
637
throw TypeError.$factory("__trunc__ returned non-Integral (type "+
Mar 21, 2018
638
$B.get_class(res).__name__ + ")")
Mar 10, 2018
640
throw _b_.TypeError.$factory(
641
"int() argument must be a string, a bytes-like " +
642
"object or a number, not '" + $B.get_class(value).__name__ + "'")
Sep 5, 2014
643
}
644
645
$B.set_func_names(int, "builtins")
Sep 5, 2014
647
_b_.int = int
648
Feb 11, 2018
650
$B.$bool = function(obj){ // return true or false
Mar 10, 2018
651
if(obj === null || obj === undefined ){ return false}
652
switch(typeof obj){
653
case "boolean":
654
return obj
655
case "number":
656
case "string":
657
if(obj){return true}
658
return false
659
default:
660
var ce = $B.current_exception
661
try{return getattr(obj, "__bool__")()}
662
catch(err){
663
$B.current_exception = ce
Mar 21, 2018
664
try{return getattr(obj, "__len__")() > 0}
Mar 10, 2018
665
catch(err){return true}
666
}
667
}
Feb 11, 2018
668
}
669
670
var bool = {
Feb 11, 2018
671
__class__: _b_.type,
Feb 11, 2018
672
__module__: "builtins",
673
__mro__: [int, object],
674
__name__: "bool",
675
$is_class: true,
676
$native: true
677
}
Feb 11, 2018
679
bool.__add__ = function(self,other){
Mar 10, 2018
680
return (other ? 1 : 0) + (self ? 1 : 0)
Feb 11, 2018
683
bool.__and__ = function(self, other){
684
return $B.$bool(int.__and__(self, other))
Feb 11, 2018
687
bool.__eq__ = function(self,other){
688
return self ? $B.$bool(other) : !$B.$bool(other)
Feb 11, 2018
691
bool.__ne__ = function(self,other){
692
return self ? !$B.$bool(other) : $B.$bool(other)
Feb 11, 2018
695
bool.__ge__ = function(self,other){
696
return _b_.int.__ge__(bool.__hash__(self),other)
Feb 11, 2018
699
bool.__gt__ = function(self,other){
700
return _b_.int.__gt__(bool.__hash__(self),other)
Mar 10, 2018
703
bool.__hash__ = bool.__index__ = bool.__int__ = function(self){
704
if(self.valueOf()) return 1
705
return 0
706
}
707
Mar 10, 2018
708
bool.__le__ = function(self, other){return ! bool.__gt__(self, other)}
Mar 10, 2018
710
bool.__lshift__ = function(self, other){return self.valueOf() << other}
Mar 10, 2018
712
bool.__lt__ = function(self, other){return ! bool.__ge__(self, other)}
Mar 10, 2018
714
bool.__mul__ = function(self, other){
Feb 11, 2018
718
bool.__neg__ = function(self){return -$B.int_or_bool(self)}
Feb 11, 2018
720
bool.__or__ = function(self, other){
721
return $B.$bool(int.__or__(self, other))
Feb 11, 2018
724
bool.__pos__ = $B.int_or_bool
Feb 11, 2018
726
bool.__repr__ = bool.__str__ = function(self){
727
return self ? "True" : "False"
Feb 11, 2018
730
bool.__setattr__ = function(self, attr){
731
return no_set_attr(bool, attr)
Feb 11, 2018
734
bool.__sub__ = function(self,other){
735
return (self ? 1 : 0) - (other ? 1 : 0)
Feb 11, 2018
738
bool.__xor__ = function(self, other) {
739
return self.valueOf() != other.valueOf()
740
}
741
Feb 11, 2018
742
bool.$factory = function(){
743
// Calls $B.$bool, which is used inside the generated JS code and skips
744
// arguments control.
Mar 10, 2018
745
var $ = $B.args("bool", 1, {x: null}, ["x"],
746
arguments,{x: false}, null, null)
Feb 11, 2018
747
return $B.$bool($.x)
748
}
749
750
_b_.bool = bool
Feb 11, 2018
752
$B.set_func_names(bool, "builtins")
Sep 5, 2014
754
})(__BRYTHON__)
Mar 10, 2018
755