-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbigint.dummy.js
129 lines (98 loc) · 2.76 KB
/
bigint.dummy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
* A dummy version of bigint for Helios
*
* no math is done in JavaScript, but the BigInt abstraction exists so that
* higher-level data structures can be parsed/serialized appropriately.
*/
// A wrapper for java.math.BigInteger with some appropriate extra functions for JSON and
// generally being a nice JavaScript object.
BigInt = Class.extend({
init: function(value, radix) {
if (radix != 10)
throw "in dummy, only radix=10, here radix=" + radix;
this.value = value;
},
toString: function() {
return this.value;
},
toJSONObject: function() {
// toString is apparently not overridden in IE, so we reproduce it here.
return this.value;
},
add: function(other) {
throw "dummy, no add!";
},
bitLength: function() {
throw "dummy, nobitlength!";
},
mod: function(modulus) {
throw "dummy, no mod!";
},
equals: function(other) {
throw "dummy, no equals!";
},
modPow: function(exp, modulus) {
throw "dummy, no modpow!";
},
negate: function() {
throw "dummy, no negate!";
},
multiply: function(other) {
throw "dummy, no multiply!";
},
modInverse: function(modulus) {
throw "dummy, no modInverse";
}
});
BigInt.ready_p = false;
BigInt.use_applet = false;
BigInt.is_dummy = true;
BigInt.fromJSONObject = function(s) {
return new BigInt(s, 10);
};
BigInt.fromInt = function(i) {
return BigInt.fromJSONObject("" + i);
};
// Set up the pointer to the applet if necessary, and some
// basic Big Ints that everyone needs (0, 1, 2, and 42)
BigInt._setup = function() {
try {
BigInt.ZERO = new BigInt("0",10);
BigInt.ONE = new BigInt("1",10);
BigInt.TWO = new BigInt("2",10);
BigInt.FORTY_TWO = new BigInt("42",10);
BigInt.ready_p = true;
} catch (e) {
// not ready
// count how many times we've tried
if (this.num_invocations == null)
this.num_invocations = 0;
this.num_invocations += 1;
if (this.num_invocations > 5) {
if (BigInt.setup_interval)
window.clearInterval(BigInt.setup_interval);
if (BigInt.setup_fail) {
BigInt.setup_fail();
} else {
alert('bigint failed!');
}
}
return;
}
if (BigInt.setup_interval)
window.clearInterval(BigInt.setup_interval);
if (BigInt.setup_callback)
BigInt.setup_callback();
};
BigInt.setup = function(callback, fail_callback) {
if (callback)
BigInt.setup_callback = callback;
if (fail_callback)
BigInt.setup_fail = fail_callback;
BigInt.setup_interval = window.setInterval("BigInt._setup()", 1000);
}
// .onload instead of .ready, as I don't think the applet is ready until onload.
// FIXME: something wrong here in the first load
$(document).ready(function() {
BigInt.use_applet = false;
});