Permalink
Feb 10, 2018
Feb 27, 2018
Feb 10, 2018
Feb 11, 2018
Feb 11, 2018
Feb 10, 2018
Feb 10, 2018
May 19, 2017
Feb 11, 2018
Dec 18, 2015
Feb 11, 2018
Feb 10, 2018
Feb 10, 2018
Feb 11, 2018
Feb 11, 2018
Feb 11, 2018
Feb 11, 2018
Feb 11, 2018
Jan 25, 2017
Feb 10, 2018
Feb 10, 2018
Feb 11, 2018
Feb 11, 2018
Feb 11, 2018
Feb 11, 2018
Feb 11, 2018
Feb 11, 2018
Feb 11, 2018
Feb 11, 2018
Newer
100644
728 lines (624 sloc)
21.9 KB
6
7
function $err(op,other){
8
var msg = "unsupported operand type(s) for "+op
9
msg += ": 'int' and '"+$B.get_class(other).__name__+"'"
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
}
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
}
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)
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++) {
94
// compare object "self" to class "int"
95
if(other===undefined) return self===int
96
if(isinstance(other,int)) return self.valueOf()==other.valueOf()
119
switch(fmt.type){
120
case undefined:
121
case 'd':
122
return self.toString()
123
case 'b':
143
if(fmt.type && 'eEfFgG%'.indexOf(fmt.type)!=-1){
144
// Call __format__ on float(self)
172
}
173
if(hasattr(other,'__rfloordiv__')){
174
return getattr(other,'__rfloordiv__')(self)
175
}
176
$err("//",other)
177
}
178
181
return int.__hashvalue__ || $B.$py_next_hash-- // for hash of int type (not instance of int)
192
if(value===undefined){value=0}
193
self.toString = function(){return value}
194
//self.valueOf = function(){return value}
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
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}
220
}
221
if(hasattr(other,'__rmod__')) return getattr(other,'__rmod__')(self)
222
$err('%',other)
223
}
224
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}
239
else{return int.$factory($B.long_int.__mul__($B.long_int.$factory(self),
240
$B.long_int.$factory(other)))}
250
return $B.make_complex(int.__mul__(self, other.$real),
251
int.__mul__(self, other.$imag))
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)
259
return res
260
}
261
if(hasattr(other,'__rmul__')) return getattr(other,'__rmul__')(self)
262
$err("*",other)
263
}
264
270
if(cls===undefined){throw _b_.TypeError.$factory('int.__new__(): not enough arguments')}
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
}
298
return int.$factory($B.long_int.__pow__($B.long_int.$factory(self),
299
$B.long_int.$factory(other)))
303
if(self>=0){return new Number(Math.pow(self, other.valueOf()))}
304
else{
305
// use complex power
312
}
313
if(hasattr(other,'__rpow__')) return getattr(other,'__rpow__')(self)
314
$err("**",other)
315
}
316
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
}
338
throw _b_.AttributeError.$factory("'int' object attribute '"+attr+"' is read-only")
362
}
363
if(hasattr(other,'__rtruediv__')) return getattr(other,'__rtruediv__')(self)
364
$err("/",other)
365
}
366
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
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}
388
if(other.__class__===$B.long_int){
389
return $B.long_int.__sub__($B.long_int.$factory(self), $B.long_int.$factory(other))
393
return $B.long_int.__sub__($B.long_int.$factory(self), $B.long_int.$factory(other))
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
404
for(var $op in $ops){
405
var opf = $op_func.replace(/-/gm,$op)
406
opf = opf.replace(new RegExp('sub','gm'),$ops[$op])
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}
416
else{return $B.long_int.__sub__($B.long_int.$factory(self),
417
$B.long_int.$factory(other))}
421
return $B.long_int.__sub__($B.long_int.$factory(self),
422
$B.long_int.$factory(other))
430
}
431
if(isinstance(other,_b_.bool)){
432
var bool_value=0;
433
if(other.valueOf()) bool_value=1;
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])
452
if (other.__class__ === $B.long_int) {
453
return $B.long_int.__lt__(other, $B.long_int.$factory(self))
464
// See if other has the opposite operator, eg < for >
465
var inv_op = getattr(other, '__lt__', null)
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
505
if(base!==undefined){
506
if(!isinstance(value,[_b_.str,_b_.bytes,_b_.bytearray])){
550
var z = parseInt(value.value)
551
if(z>$B.min_int && z<$B.max_int){return z}
552
else{return value}
553
}
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')) {
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') {
572
}
573
}
574
var _digits=$valid_digits(base)
575
var _re=new RegExp('^[+-]?['+_digits+']+$', 'i')
578
"invalid literal for int() with base "+base +": '"+
579
_b_.str.$factory(value)+"'")
583
"invalid literal for int() with base "+base +": '"+
584
_b_.str.$factory(value)+"'")
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){
596
base +": "+_b_.repr(value))
597
}
598
}
599
return Number(parseInt(getattr(value,'decode')('latin-1'), base))
600
}
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){
609
$B.get_class(res).__name__+')')
610
}
611
var res=int_func()
612
if(isinstance(res, int)){return res}
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 = {
647
__module__: "builtins",
648
__mro__: [int, object],
649
__name__: "bool",
650
$is_class: true,
651
$native: true
652
}
658
bool.__and__ = function(self, other){
659
return $B.$bool(int.__and__(self, other))
662
bool.__eq__ = function(self,other){
663
return self ? $B.$bool(other) : !$B.$bool(other)
666
bool.__ne__ = function(self,other){
667
return self ? !$B.$bool(other) : $B.$bool(other)
670
bool.__ge__ = function(self,other){
671
return _b_.int.__ge__(bool.__hash__(self),other)
674
bool.__gt__ = function(self,other){
675
return _b_.int.__gt__(bool.__hash__(self),other)
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