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