Skip to content
Permalink
Newer
Older
100644 728 lines (624 sloc) 21.9 KB
Sep 5, 2014
1
;(function($B){
2
3
eval($B.InjectBuiltins())
4
5
var object = _b_.object, $N = _b_.None
Sep 5, 2014
6
7
function $err(op,other){
8
var msg = "unsupported operand type(s) for "+op
9
msg += ": 'int' and '"+$B.get_class(other).__name__+"'"
10
throw _b_.TypeError.$factory(msg)
Sep 5, 2014
11
}
12
13
// dictionary for built-in class 'int'
Feb 11, 2018
14
var int = {__class__: _b_.type,
15
__name__: 'int',
16
__dir__: object.__dir__,
17
$is_class: true,
18
$native: true,
19
$descriptors: {
20
'numerator': true,
21
'denominator': true,
22
'imag': true,
23
'real': true
24
}
Sep 5, 2014
25
}
26
27
int.from_bytes = function() {
28
var $=$B.args("from_bytes", 3,
29
{bytes:null, byteorder:null, signed:null},
30
['bytes', 'byteorder', 'signed'],
31
arguments, {signed:False}, null, null)
Sep 5, 2014
32
33
var x = $.bytes,
34
byteorder = $.byteorder,
35
signed = $.signed
37
if (isinstance(x, [_b_.bytes, _b_.bytearray])) {
38
_bytes=x.source
39
_len=x.source.length
Jan 14, 2015
41
} else {
42
_bytes = _b_.list.$factory(x)
43
_len = _bytes.length
44
for(var i=0;i<_len;i++){
45
_b_.bytes.$factory([_bytes[i]])
46
}
Sep 5, 2014
47
}
48
switch(byteorder) {
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)
Jan 14, 2015
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)
Jan 14, 2015
69
if (!signed) return num
70
if (_bytes[_len - 1] < 128) return num
71
return $B.sub(num, _mult)
Sep 5, 2014
72
}
73
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)}
93
int.__eq__ = function(self,other){
Sep 5, 2014
94
// compare object "self" to class "int"
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()
Sep 5, 2014
98
if(isinstance(other,_b_.complex)){
99
if (other.$imag != 0) return False
100
return self.valueOf() == other.$real
Sep 5, 2014
101
}
102
103
if (hasattr(other, '__eq__')) return getattr(other, '__eq__')(self)
104
Sep 5, 2014
105
return self.valueOf()===other
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)}
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:
121
case 'd':
122
return self.toString()
123
case 'b':
124
return (fmt.alternate ? '0b' : '') + self.toString(2)
125
case 'c':
126
return _b_.chr(self)
127
case 'o':
128
return (fmt.alternate ? '0o' : '') + self.toString(8)
130
return (fmt.alternate ? '0x' : '') + self.toString(16)
132
return (fmt.alternate ? '0X' : '') + self.toString(16).toUpperCase()
133
case 'n':
134
return self // fix me
135
}
137
return res
138
}
139
140
141
int.__format__ = function(self,format_spec){
142
var fmt = new $B.parse_format_spec(format_spec)
143
if(fmt.type && 'eEfFgG%'.indexOf(fmt.type)!=-1){
144
// Call __format__ on float(self)
145
return _b_.float.__format__(self, format_spec)
147
fmt.align = fmt.align || '>'
148
var res = preformat(self, fmt)
149
if(fmt.comma){
150
var sign = res[0]=='-' ? '-' : '',
151
rest = res.substr(sign.length),
152
len = rest.length,
153
nb = Math.ceil(rest.length/3),
154
chunks = []
155
for(var i=0;i<nb;i++){
156
chunks.push(rest.substring(len-3*i-3, len-3*i))
157
}
158
chunks.reverse()
159
res = sign+chunks.join(',')
160
}
161
return $B.format_width(res, fmt)
Sep 5, 2014
162
}
163
164
int.__floordiv__ = function(self,other){
Sep 5, 2014
165
if(isinstance(other,int)){
166
if(other==0) throw ZeroDivisionError.$factory('division by zero')
Sep 5, 2014
167
return Math.floor(self/other)
168
}
169
if(isinstance(other,_b_.float)){
170
if(!other.valueOf()) throw ZeroDivisionError.$factory('division by zero')
171
return Math.floor(self/other)
Sep 5, 2014
172
}
173
if(hasattr(other,'__rfloordiv__')){
174
return getattr(other,'__rfloordiv__')(self)
175
}
176
$err("//",other)
177
}
178
179
int.__hash__ = function(self){
181
return int.__hashvalue__ || $B.$py_next_hash-- // for hash of int type (not instance of int)
182
}
183
184
return self.valueOf()
185
}
Sep 5, 2014
186
187
//int.__ior__ = function(self,other){return self | other} // bitwise OR
Sep 5, 2014
188
189
int.__index__ = function(self){return self}
Sep 5, 2014
190
191
int.__init__ = function(self,value){
Sep 5, 2014
192
if(value===undefined){value=0}
193
self.toString = function(){return value}
194
//self.valueOf = 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
203
int.__lshift__ = function(self,other){
204
if(isinstance(other, int)){
Feb 11, 2018
205
return int.$factory($B.long_int.__lshift__($B.long_int.$factory(self), $B.long_int.$factory(other)))
206
}
207
var rlshift = getattr(other, '__rlshift__', null)
208
if(rlshift!==null){return rlshift(self)}
209
$err('<<', other)
210
}
211
212
int.__mod__ = function(self,other) {
Sep 5, 2014
213
// can't use Javascript % because it works differently for negative numbers
214
if(isinstance(other,_b_.tuple) && other.length==1) other=other[0]
215
if(isinstance(other,[int, _b_.float, bool])){
216
if(other===false){other=0}else if(other===true){other=1}
217
if(other==0){throw _b_.ZeroDivisionError.$factory(
218
"integer division or modulo by zero")}
219
return (self%other+other)%other
Sep 5, 2014
220
}
221
if(hasattr(other,'__rmod__')) return getattr(other,'__rmod__')(self)
222
$err('%',other)
223
}
224
225
int.__mro__ = [object]
Sep 5, 2014
226
227
int.__mul__ = function(self,other){
228
Sep 5, 2014
229
var val = self.valueOf()
Jan 22, 2015
230
231
// this will be quick check, so lets do it early.
232
if(typeof other==="string") {
233
return other.repeat(val)
234
}
235
236
if(isinstance(other,int)){
237
var res = self*other
238
if(res>$B.min_int && res<$B.max_int){return res}
Feb 11, 2018
239
else{return int.$factory($B.long_int.__mul__($B.long_int.$factory(self),
240
$B.long_int.$factory(other)))}
242
if(isinstance(other,_b_.float)){
243
return new Number(self*other)
Sep 5, 2014
245
if(isinstance(other,_b_.bool)){
246
if (other.valueOf()) return self
247
return int.$factory(0)
Sep 5, 2014
248
}
249
if(isinstance(other,_b_.complex)){
250
return $B.make_complex(int.__mul__(self, other.$real),
251
int.__mul__(self, other.$imag))
Sep 5, 2014
252
}
253
if(isinstance(other,[_b_.list,_b_.tuple])){
254
var res = []
255
// make temporary copy of list
256
var $temp = other.slice(0,other.length)
257
for(var i=0;i<val;i++) res=res.concat($temp)
Feb 11, 2018
258
if(isinstance(other,_b_.tuple)) res=_b_.tuple.$factory(res)
Sep 5, 2014
259
return res
260
}
261
if(hasattr(other,'__rmul__')) return getattr(other,'__rmul__')(self)
262
$err("*",other)
263
}
264
265
int.__name__ = 'int'
Sep 5, 2014
266
267
int.__neg__ = function(self){return -self}
Sep 5, 2014
268
269
int.__new__ = function(cls){
270
if(cls===undefined){throw _b_.TypeError.$factory('int.__new__(): not enough arguments')}
Sep 5, 2014
272
}
273
274
int.__pos__ = function(self){return self}
Sep 5, 2014
275
276
int.__pow__ = function(self,other,z){
Sep 5, 2014
277
if(isinstance(other, int)) {
Feb 9, 2015
278
switch(other.valueOf()) {
279
case 0:
280
return int.$factory(1)
Feb 9, 2015
281
case 1:
282
return int.$factory(self.valueOf())
Feb 9, 2015
283
}
May 19, 2017
284
if(z !== undefined && z !== null){
285
// If z is provided, the algorithm is faster than computing
286
// self ** other then applying the modulo z
287
var res = (self % z + z) % z
288
for(var i=1; i<other; i++){
289
res *= self
290
res = (res % z + z) % z
291
}
292
return res
293
}
294
var res = Math.pow(self.valueOf(),other.valueOf())
295
if(res>$B.min_int && res<$B.max_int){return res}
May 19, 2017
296
else if(res !== Infinity && !isFinite(res)){return res}
Feb 11, 2018
298
return int.$factory($B.long_int.__pow__($B.long_int.$factory(self),
299
$B.long_int.$factory(other)))
May 19, 2017
300
}
Sep 5, 2014
301
}
302
if(isinstance(other, _b_.float)) {
303
if(self>=0){return new Number(Math.pow(self, other.valueOf()))}
304
else{
305
// use complex power
306
return _b_.complex.__pow__($B.make_complex(self, 0), other)
308
}else if(isinstance(other, _b_.complex)){
309
var preal = Math.pow(self, other.$real),
310
ln = Math.log(self)
311
return $B.make_complex(preal*Math.cos(ln), preal*Math.sin(ln))
Sep 5, 2014
312
}
313
if(hasattr(other,'__rpow__')) return getattr(other,'__rpow__')(self)
314
$err("**",other)
315
}
316
317
int.__repr__ = function(self){
Sep 5, 2014
318
if(self===int) return "<class 'int'>"
319
return self.toString()
320
}
321
322
// bitwise right shift
323
int.__rshift__ = function(self,other){
324
if(isinstance(other, int)){
Feb 11, 2018
325
return int.$factory($B.long_int.__rshift__($B.long_int.$factory(self),
326
$B.long_int.$factory(other)))
327
}
328
var rrshift = getattr(other, '__rrshift__', null)
329
if(rrshift!==null){return rrshift(self)}
330
$err('>>', other)
331
}
Sep 5, 2014
332
333
int.__setattr__ = function(self,attr,value){
335
if(int.$factory[attr]===undefined){
336
throw _b_.AttributeError.$factory("'int' object has no attribute '"+attr+"'")
338
throw _b_.AttributeError.$factory("'int' object attribute '"+attr+"' is read-only")
Sep 5, 2014
340
}
341
// subclasses of int can have attributes set
342
self[attr] = value
Sep 5, 2014
344
}
345
346
int.__str__ = int.__repr__
Sep 5, 2014
347
348
int.__truediv__ = function(self,other){
Sep 5, 2014
349
if(isinstance(other,int)){
350
if(other==0) throw ZeroDivisionError.$factory('division by zero')
Feb 11, 2018
351
if(other.__class__==$B.long_int){return new Number(self/parseInt(other.value))}
352
return new Number(self/other)
Sep 5, 2014
353
}
354
if(isinstance(other,_b_.float)){
355
if(!other.valueOf()) throw ZeroDivisionError.$factory('division by zero')
356
return new Number(self/other)
Sep 5, 2014
357
}
358
if(isinstance(other,_b_.complex)){
359
var cmod = other.$real*other.$real+other.$imag*other.$imag
360
if(cmod==0) throw ZeroDivisionError.$factory('division by zero')
361
return $B.make_complex(self*other.$real/cmod,-self*other.$imag/cmod)
Sep 5, 2014
362
}
363
if(hasattr(other,'__rtruediv__')) return getattr(other,'__rtruediv__')(self)
364
$err("/",other)
365
}
366
367
//int.__xor__ = function(self,other){return self ^ other} // bitwise XOR
Sep 5, 2014
368
369
int.bit_length = function(self){
Sep 5, 2014
370
s = bin(self)
371
s = getattr(s,'lstrip')('-0b') // remove leading zeros and minus sign
372
return s.length // len('100101') --> 6
373
}
374
375
// descriptors
376
int.numerator = function(self){return self}
377
int.denominator = function(self){return int.$factory(1)}
378
int.imag = function(self){return int.$factory(0)}
379
int.real = function(self){return self}
380
382
$B.max_int32= (1<<30) * 2 - 1
383
$B.min_int32= - $B.max_int32
384
385
// code for operands & | ^
Sep 5, 2014
386
var $op_func = function(self,other){
Jun 7, 2015
387
if(isinstance(other,int)) {
Feb 11, 2018
388
if(other.__class__===$B.long_int){
389
return $B.long_int.__sub__($B.long_int.$factory(self), $B.long_int.$factory(other))
391
if (self > $B.max_int32 || self < $B.min_int32 ||
392
other > $B.max_int32 || other < $B.min_int32) {
Feb 11, 2018
393
return $B.long_int.__sub__($B.long_int.$factory(self), $B.long_int.$factory(other))
394
}
395
return self-other
Jun 7, 2015
396
}
Sep 5, 2014
397
if(isinstance(other,_b_.bool)) return self-other
398
if(hasattr(other,'__rsub__')) return getattr(other,'__rsub__')(self)
399
$err("-",other)
400
}
401
402
$op_func += '' // source code
403
var $ops = {'&':'and','|':'or','^':'xor'}
Sep 5, 2014
404
for(var $op in $ops){
405
var opf = $op_func.replace(/-/gm,$op)
406
opf = opf.replace(new RegExp('sub','gm'),$ops[$op])
407
eval('int.__'+$ops[$op]+'__ = '+opf)
Sep 5, 2014
408
}
409
410
// code for + and -
411
var $op_func = function(self,other){
412
if(isinstance(other,int)){
413
if(typeof other=='number'){
414
var res = self.valueOf()-other.valueOf()
415
if(res>=$B.min_int && res<=$B.max_int){return res}
Feb 11, 2018
416
else{return $B.long_int.__sub__($B.long_int.$factory(self),
417
$B.long_int.$factory(other))}
418
}else if(typeof other=="boolean"){
419
return other ? self-1 : self
420
}else{
Feb 11, 2018
421
return $B.long_int.__sub__($B.long_int.$factory(self),
422
$B.long_int.$factory(other))
Sep 5, 2014
424
}
425
if(isinstance(other,_b_.float)){
426
return new Number(self-other)
Sep 5, 2014
427
}
428
if(isinstance(other,_b_.complex)){
429
return $B.make_complex(self-other.$real,-other.$imag)
Sep 5, 2014
430
}
431
if(isinstance(other,_b_.bool)){
432
var bool_value=0;
433
if(other.valueOf()) bool_value=1;
Sep 5, 2014
435
}
436
if(isinstance(other,_b_.complex)){
437
return $B.make_complex(self.valueOf() - other.$real, other.$imag)
Sep 5, 2014
438
}
439
if(hasattr(other,'__rsub__')) return getattr(other,'__rsub__')(self)
440
throw $err('-',other)
441
}
442
$op_func += '' // source code
443
var $ops = {'+':'add','-':'sub'}
444
for(var $op in $ops){
445
var opf = $op_func.replace(/-/gm,$op)
446
opf = opf.replace(new RegExp('sub','gm'),$ops[$op])
447
eval('int.__'+$ops[$op]+'__ = '+opf)
Sep 5, 2014
448
}
449
450
// comparison methods
451
var $comp_func = function(self,other){
Feb 11, 2018
452
if (other.__class__ === $B.long_int) {
453
return $B.long_int.__lt__(other, $B.long_int.$factory(self))
Sep 5, 2014
455
if(isinstance(other,int)) return self.valueOf() > other.valueOf()
456
if(isinstance(other,_b_.float)) return self.valueOf() > other.valueOf()
Sep 5, 2014
457
if(isinstance(other,_b_.bool)) {
Feb 11, 2018
458
return self.valueOf() > _b_.bool.__hash__(other)
Sep 5, 2014
459
}
460
if (hasattr(other, '__int__') || hasattr(other, '__index__')) {
461
return int.__gt__(self, $B.$GetInt(other))
464
// See if other has the opposite operator, eg < for >
465
var inv_op = getattr(other, '__lt__', null)
466
if(inv_op !== null){return inv_op(self)}
467
468
throw _b_.TypeError.$factory(
469
"unorderable types: int() > "+$B.get_class(other).__name__+"()")
Sep 5, 2014
470
}
471
$comp_func += '' // source code
Sep 5, 2014
473
for(var $op in $B.$comps){
474
eval("int.__"+$B.$comps[$op]+'__ = '+
475
$comp_func.replace(/>/gm,$op).
476
replace(/__gt__/gm,'__'+$B.$comps[$op]+'__').
477
replace(/__lt__/, '__'+$B.$inv_comps[$op]+'__'))
Sep 5, 2014
478
}
479
480
// add "reflected" methods
481
$B.make_rmethods(int)
Sep 5, 2014
482
483
var $valid_digits=function(base) {
484
var digits=''
485
if (base === 0) return '0'
486
if (base < 10) {
487
for (var i=0; i < base; i++) digits+=String.fromCharCode(i+48)
488
return digits
489
}
490
491
var digits='0123456789'
492
// A = 65 (10 + 55)
493
for (var i=10; i < base; i++) digits+=String.fromCharCode(i+55)
494
return digits
495
}
496
497
int.$factory = function(value, base){
498
// int() with no argument returns 0
499
if(value===undefined){return 0}
501
// int() of an integer returns the integer if base is undefined
502
if(typeof value=='number' &&
503
(base===undefined || base==10)){return parseInt(value)}
Dec 28, 2014
505
if(base!==undefined){
506
if(!isinstance(value,[_b_.str,_b_.bytes,_b_.bytearray])){
507
throw TypeError.$factory("int() can't convert non-string with explicit base")
Dec 28, 2014
508
}
509
}
510
511
if(isinstance(value,_b_.complex)){
512
throw TypeError.$factory("can't convert complex to int")
Dec 28, 2014
513
}
514
515
var $ns=$B.args('int',2,{x:null,base:null},['x','base'],arguments,
516
{'base':10},'null','null')
517
var value = $ns['x']
518
var base = $ns['base']
520
if(isinstance(value, _b_.float) && base===10){
521
if(value<$B.min_int || value>$B.max_int){
Feb 11, 2018
522
return $B.long_int.$from_float(value)
524
else{return value>0 ? Math.floor(value) : Math.ceil(value)}
Sep 5, 2014
526
Dec 26, 2014
527
if (!(base >=2 && base <= 36)) {
528
// throw error (base must be 0, or 2-36)
529
if (base != 0) throw _b_.ValueError.$factory("invalid base")
Dec 26, 2014
530
}
531
532
if (typeof value == 'number'){
Feb 11, 2018
535
if(value < $B.min_int || value > $B.max_int) return $B.long_int.$factory(value)
536
return value
537
}else if(value.toString().search('e')>-1){
Dec 26, 2014
538
// can't convert to another base if value is too big
539
throw _b_.OverflowError.$factory("can't convert to base "+base)
Dec 26, 2014
540
}else{
541
var res=parseInt(value, base)
Feb 11, 2018
542
if(res < $B.min_int || res > $B.max_int) return $B.long_int.$factory(value,base)
Dec 26, 2014
544
}
545
}
Sep 5, 2014
546
547
if(value===true) return Number(1)
548
if(value===false) return Number(0)
Feb 11, 2018
549
if(value.__class__===$B.long_int){
550
var z = parseInt(value.value)
551
if(z>$B.min_int && z<$B.max_int){return z}
552
else{return value}
553
}
Sep 5, 2014
554
Jan 22, 2015
555
base=$B.$GetInt(base)
Sep 5, 2014
556
557
if(isinstance(value, _b_.str)) value=value.valueOf()
558
if(typeof value=="string") {
559
var _value=value.trim() // remove leading/trailing whitespace
560
if (_value.length == 2 && base==0 && (_value=='0b' || _value=='0o' || _value=='0x')) {
561
throw _b_.ValueError.$factory('invalid value')
Sep 5, 2014
562
}
563
if (_value.length >2) {
564
var _pre=_value.substr(0,2).toUpperCase()
Sep 5, 2014
565
if (base == 0) {
566
if (_pre == '0B') base=2
567
if (_pre == '0O') base=8
568
if (_pre == '0X') base=16
569
}
570
if (_pre=='0B' || _pre=='0O' || _pre=='0X') {
Sep 5, 2014
572
}
573
}
574
var _digits=$valid_digits(base)
575
var _re=new RegExp('^[+-]?['+_digits+']+$', 'i')
577
throw _b_.ValueError.$factory(
str
Feb 10, 2018
578
"invalid literal for int() with base "+base +": '"+
579
_b_.str.$factory(value)+"'")
Sep 5, 2014
580
}
581
if(base <= 10 && !isFinite(value)) {
582
throw _b_.ValueError.$factory(
str
Feb 10, 2018
583
"invalid literal for int() with base "+base +": '"+
584
_b_.str.$factory(value)+"'")
586
var res=parseInt(_value, base)
Feb 11, 2018
587
if(res < $B.min_int || res > $B.max_int) return $B.long_int.$factory(_value, base)
Sep 5, 2014
589
}
591
if(isinstance(value,[_b_.bytes,_b_.bytearray])){
592
var _digits = $valid_digits(base)
593
for(var i=0;i<value.source.length;i++){
594
if(_digits.indexOf(String.fromCharCode(value.source[i]))==-1){
595
throw _b_.ValueError.$factory("invalid literal for int() with base "+
596
base +": "+_b_.repr(value))
597
}
598
}
599
return Number(parseInt(getattr(value,'decode')('latin-1'), base))
600
}
Sep 5, 2014
601
602
if(hasattr(value, '__int__')) return getattr(value,'__int__')()
603
if(hasattr(value, '__index__')) return getattr(value,'__index__')()
604
if(hasattr(value, '__trunc__')) {
605
var res = getattr(value,'__trunc__')(),
606
int_func = _b_.getattr(res, '__int__', null)
607
if(int_func===null){
608
throw TypeError.$factory('__trunc__ returned non-Integral (type '+
609
$B.get_class(res).__name__+')')
610
}
611
var res=int_func()
612
if(isinstance(res, int)){return res}
613
throw TypeError.$factory('__trunc__ returned non-Integral (type '+
614
$B.get_class(res).__name__+')')
615
}
616
throw _b_.TypeError.$factory("int() argument must be a string, a bytes-like "+
617
"object or a number, not '"+$B.get_class(value).__name__+"'")
Sep 5, 2014
618
}
619
620
$B.set_func_names(int, "builtins")
Sep 5, 2014
622
_b_.int = int
623
Feb 11, 2018
625
$B.$bool = function(obj){ // return true or false
626
if(obj===null || obj === undefined ) return false
627
switch(typeof obj) {
628
case 'boolean':
629
return obj
630
case 'number':
631
case 'string':
632
if(obj) return true
633
return false
634
default:
635
var ce = $B.current_exception
636
try{return getattr(obj,'__bool__')()}
637
catch(err){
638
$B.current_exception = ce
639
try{return getattr(obj,'__len__')()>0}
640
catch(err){return true}
641
}
642
}// switch
643
}
644
645
var bool = {
Feb 11, 2018
646
__class__: _b_.type,
Feb 11, 2018
647
__module__: "builtins",
648
__mro__: [int, object],
649
__name__: "bool",
650
$is_class: true,
651
$native: true
652
}
Feb 11, 2018
654
bool.__add__ = function(self,other){
655
return (other ? 1 : 0)+(self ? 1 : 0)
Feb 11, 2018
658
bool.__and__ = function(self, other){
659
return $B.$bool(int.__and__(self, other))
Feb 11, 2018
662
bool.__eq__ = function(self,other){
663
return self ? $B.$bool(other) : !$B.$bool(other)
Feb 11, 2018
666
bool.__ne__ = function(self,other){
667
return self ? !$B.$bool(other) : $B.$bool(other)
Feb 11, 2018
670
bool.__ge__ = function(self,other){
671
return _b_.int.__ge__(bool.__hash__(self),other)
Feb 11, 2018
674
bool.__gt__ = function(self,other){
675
return _b_.int.__gt__(bool.__hash__(self),other)
Feb 11, 2018
678
bool.__hash__ = bool.__index__= bool.__int__=function(self) {
679
if(self.valueOf()) return 1
680
return 0
681
}
682
Feb 11, 2018
683
bool.__le__ = function(self,other){return !bool.__gt__(self,other)}
Feb 11, 2018
685
bool.__lshift__ = function(self,other){return self.valueOf() << other}
Feb 11, 2018
687
bool.__lt__ = function(self,other){return !bool.__ge__(self,other)}
Feb 11, 2018
689
bool.__mul__ = function(self,other){
Feb 11, 2018
693
bool.__neg__ = function(self){return -$B.int_or_bool(self)}
Feb 11, 2018
695
bool.__or__ = function(self, other){
696
return $B.$bool(int.__or__(self, other))
Feb 11, 2018
699
bool.__pos__ = $B.int_or_bool
Feb 11, 2018
701
bool.__repr__ = bool.__str__ = function(self){
702
return self ? "True" : "False"
Feb 11, 2018
705
bool.__setattr__ = function(self, attr){
706
return no_set_attr(bool, attr)
Feb 11, 2018
709
bool.__sub__ = function(self,other){
710
return (self ? 1 : 0) - (other ? 1 : 0)
Feb 11, 2018
713
bool.__xor__ = function(self, other) {
714
return self.valueOf() != other.valueOf()
715
}
716
Feb 11, 2018
717
bool.$factory = function(){
718
// Calls $B.$bool, which is used inside the generated JS code and skips
719
// arguments control.
720
var $=$B.args('bool', 1, {x:null}, ['x'], arguments,{x:false},null,null)
721
return $B.$bool($.x)
722
}
723
724
_b_.bool = bool
Feb 11, 2018
726
$B.set_func_names(bool, "builtins")
Sep 5, 2014
728
})(__BRYTHON__)